💿🐜 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.

197 lines
4.3 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::u32string& text);
  37. std::size_t addValue();
  38. void removeValues();
  39. void setValueName(std::size_t index, const std::u32string& text);
  40. void setValueIndex(std::size_t index);
  41. std::size_t getValueCount() const;
  42. const std::u32string& 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::u32string> 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::u32string& 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. void update(float dt);
  103. /**
  104. * Deselects the currently selected item (if any)
  105. */
  106. void deselect();
  107. /**
  108. * Selects the item at the specified index
  109. */
  110. void select(std::size_t index);
  111. /**
  112. * Activates the selected item (if any)
  113. */
  114. void activate();
  115. /**
  116. * Recalculates the dimensions of the UI container according the dimensions of the menu item labels and the line spacing.
  117. */
  118. void resize();
  119. private:
  120. friend class MenuItem;
  121. std::vector<MenuItem*> items;
  122. MenuItem* selectedItem;
  123. std::function<void()> enteredCallback;
  124. std::function<void()> exitedCallback;
  125. Font* font;
  126. float lineSpacing;
  127. float columnMargin;
  128. UIContainer* container;
  129. // Prevents activation of multiple menu items in the same frame
  130. int activationDelay;
  131. };
  132. inline std::size_t Menu::getItemCount()
  133. {
  134. return items.size();
  135. }
  136. inline const MenuItem* Menu::getItem(std::size_t index) const
  137. {
  138. return items[index];
  139. }
  140. inline MenuItem* Menu::getItem(std::size_t index)
  141. {
  142. return items[index];
  143. }
  144. inline const MenuItem* Menu::getSelectedItem() const
  145. {
  146. return selectedItem;
  147. }
  148. inline MenuItem* Menu::getSelectedItem()
  149. {
  150. return selectedItem;
  151. }
  152. inline const UIContainer* Menu::getUIContainer() const
  153. {
  154. return container;
  155. }
  156. inline UIContainer* Menu::getUIContainer()
  157. {
  158. return container;
  159. }
  160. #endif // MENU_HPP