💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
4.2 KiB

  1. /*
  2. * Copyright (C) 2017 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper Source Code.
  5. *
  6. * Antkeeper Source Code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper Source Code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef MENU_HPP
  20. #define MENU_HPP
  21. #include <emergent/emergent.hpp>
  22. using namespace Emergent;
  23. #include <functional>
  24. #include <string>
  25. #include <vector>
  26. class Menu;
  27. class UIContainer;
  28. class UILabel;
  29. class MenuItem
  30. {
  31. public:
  32. void setSelectedCallback(std::function<void()> callback);
  33. void setDeselectedCallback(std::function<void()> callback);
  34. void setActivatedCallback(std::function<void()> callback);
  35. void setValueChangedCallback(std::function<void(std::size_t)> callback);
  36. void setName(const std::string& text);
  37. std::size_t addValue();
  38. void removeValues();
  39. void setValueName(std::size_t index, const std::string& text);
  40. void setValueIndex(std::size_t index);
  41. std::size_t getValueCount() const;
  42. const std::string& getValue(std::size_t index) const;
  43. std::size_t getValueIndex() const;
  44. std::size_t getItemIndex() const;
  45. bool isSelected() const;
  46. private:
  47. friend class Menu;
  48. MenuItem(Menu* parent, std::size_t index);
  49. ~MenuItem();
  50. void select();
  51. void deselect();
  52. void activate();
  53. Menu* parent;
  54. std::size_t index;
  55. std::function<void()> selectedCallback;
  56. std::function<void()> deselectedCallback;
  57. std::function<void()> activatedCallback;
  58. std::function<void(std::size_t)> valueChangedCallback;
  59. std::vector<std::string> values;
  60. std::size_t valueIndex;
  61. UILabel* nameLabel;
  62. UILabel* valueLabel;
  63. UIContainer* rowContainer;
  64. };
  65. inline std::size_t MenuItem::getItemIndex() const
  66. {
  67. return index;
  68. }
  69. inline std::size_t MenuItem::getValueCount() const
  70. {
  71. return values.size();
  72. }
  73. inline const std::string& MenuItem::getValue(std::size_t index) const
  74. {
  75. return values[index];
  76. }
  77. inline std::size_t MenuItem::getValueIndex() const
  78. {
  79. return valueIndex;
  80. }
  81. class Menu
  82. {
  83. public:
  84. Menu();
  85. ~Menu();
  86. void enter();
  87. void exit();
  88. MenuItem* addItem();
  89. void removeItems();
  90. void setEnteredCallback(std::function<void()> callback);
  91. void setExitedCallback(std::function<void()> callback);
  92. void setFont(Font* font);
  93. void setLineSpacing(float spacing);
  94. void setColumnMargin(float margin);
  95. std::size_t getItemCount();
  96. const MenuItem* getItem(std::size_t index) const;
  97. MenuItem* getItem(std::size_t index);
  98. const MenuItem* getSelectedItem() const;
  99. MenuItem* getSelectedItem();
  100. const UIContainer* getUIContainer() const;
  101. UIContainer* getUIContainer();
  102. /**
  103. * Deselects the currently selected item (if any)
  104. */
  105. void deselect();
  106. /**
  107. * Selects the item at the specified index
  108. */
  109. void select(std::size_t index);
  110. /**
  111. * Activates the selected item (if any)
  112. */
  113. void activate();
  114. /**
  115. * Recalculates the dimensions of the UI container according the dimensions of the menu item labels and the line spacing.
  116. */
  117. void resize();
  118. private:
  119. friend class MenuItem;
  120. std::vector<MenuItem*> items;
  121. MenuItem* selectedItem;
  122. std::function<void()> enteredCallback;
  123. std::function<void()> exitedCallback;
  124. Font* font;
  125. float lineSpacing;
  126. float columnMargin;
  127. UIContainer* container;
  128. };
  129. inline std::size_t Menu::getItemCount()
  130. {
  131. return items.size();
  132. }
  133. inline const MenuItem* Menu::getItem(std::size_t index) const
  134. {
  135. return items[index];
  136. }
  137. inline MenuItem* Menu::getItem(std::size_t index)
  138. {
  139. return items[index];
  140. }
  141. inline const MenuItem* Menu::getSelectedItem() const
  142. {
  143. return selectedItem;
  144. }
  145. inline MenuItem* Menu::getSelectedItem()
  146. {
  147. return selectedItem;
  148. }
  149. inline const UIContainer* Menu::getUIContainer() const
  150. {
  151. return container;
  152. }
  153. inline UIContainer* Menu::getUIContainer()
  154. {
  155. return container;
  156. }
  157. #endif // MENU_HPP