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

191 lines
4.1 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. void addValue(const std::string& text);
  38. void removeValues();
  39. void setValueIndex(std::size_t index);
  40. std::size_t getValueCount() const;
  41. const std::string& getValue(std::size_t index) const;
  42. std::size_t getValueIndex() const;
  43. std::size_t getItemIndex() const;
  44. bool isSelected() const;
  45. private:
  46. friend class Menu;
  47. MenuItem(Menu* parent, std::size_t index);
  48. ~MenuItem();
  49. void select();
  50. void deselect();
  51. void activate();
  52. Menu* parent;
  53. std::size_t index;
  54. std::function<void()> selectedCallback;
  55. std::function<void()> deselectedCallback;
  56. std::function<void()> activatedCallback;
  57. std::function<void(std::size_t)> valueChangedCallback;
  58. std::vector<std::string> values;
  59. std::size_t valueIndex;
  60. UILabel* nameLabel;
  61. UILabel* valueLabel;
  62. UIContainer* rowContainer;
  63. };
  64. inline std::size_t MenuItem::getItemIndex() const
  65. {
  66. return index;
  67. }
  68. inline std::size_t MenuItem::getValueCount() const
  69. {
  70. return values.size();
  71. }
  72. inline const std::string& MenuItem::getValue(std::size_t index) const
  73. {
  74. return values[index];
  75. }
  76. inline std::size_t MenuItem::getValueIndex() const
  77. {
  78. return valueIndex;
  79. }
  80. class Menu
  81. {
  82. public:
  83. Menu();
  84. ~Menu();
  85. void enter();
  86. void exit();
  87. MenuItem* addItem();
  88. void removeItems();
  89. void setEnteredCallback(std::function<void()> callback);
  90. void setExitedCallback(std::function<void()> callback);
  91. void setFont(Font* font);
  92. void setLineSpacing(float spacing);
  93. void setColumnMargin(float margin);
  94. std::size_t getItemCount();
  95. const MenuItem* getItem(std::size_t index) const;
  96. MenuItem* getItem(std::size_t index);
  97. const MenuItem* getSelectedItem() const;
  98. MenuItem* getSelectedItem();
  99. const UIContainer* getUIContainer() const;
  100. UIContainer* getUIContainer();
  101. /**
  102. * Deselects the currently selected item (if any)
  103. */
  104. void deselect();
  105. /**
  106. * Selects the item at the specified index
  107. */
  108. void select(std::size_t index);
  109. /**
  110. * Activates the selected item (if any)
  111. */
  112. void activate();
  113. /**
  114. * Recalculates the dimensions of the UI container according the dimensions of the menu item labels and the line spacing.
  115. */
  116. void resize();
  117. private:
  118. friend class MenuItem;
  119. std::vector<MenuItem*> items;
  120. MenuItem* selectedItem;
  121. std::function<void()> enteredCallback;
  122. std::function<void()> exitedCallback;
  123. Font* font;
  124. float lineSpacing;
  125. float columnMargin;
  126. UIContainer* container;
  127. };
  128. inline std::size_t Menu::getItemCount()
  129. {
  130. return items.size();
  131. }
  132. inline const MenuItem* Menu::getItem(std::size_t index) const
  133. {
  134. return items[index];
  135. }
  136. inline MenuItem* Menu::getItem(std::size_t index)
  137. {
  138. return items[index];
  139. }
  140. inline const MenuItem* Menu::getSelectedItem() const
  141. {
  142. return selectedItem;
  143. }
  144. inline MenuItem* Menu::getSelectedItem()
  145. {
  146. return selectedItem;
  147. }
  148. inline const UIContainer* Menu::getUIContainer() const
  149. {
  150. return container;
  151. }
  152. inline UIContainer* Menu::getUIContainer()
  153. {
  154. return container;
  155. }
  156. #endif // MENU_HPP