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

160 lines
3.4 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 setLabel(const std::string& text);
  37. std::size_t getIndex() const;
  38. bool isSelected() const;
  39. private:
  40. friend class Menu;
  41. MenuItem(Menu* parent, std::size_t index);
  42. ~MenuItem();
  43. void select();
  44. void deselect();
  45. void activate();
  46. Menu* parent;
  47. std::size_t index;
  48. std::function<void()> selectedCallback;
  49. std::function<void()> deselectedCallback;
  50. std::function<void()> activatedCallback;
  51. UILabel* label;
  52. };
  53. inline std::size_t MenuItem::getIndex() const
  54. {
  55. return index;
  56. }
  57. class Menu
  58. {
  59. public:
  60. Menu();
  61. ~Menu();
  62. void enter();
  63. void exit();
  64. MenuItem* addItem();
  65. void removeItems();
  66. void setEnteredCallback(std::function<void()> callback);
  67. void setExitedCallback(std::function<void()> callback);
  68. void setFont(Font* font);
  69. void setLineSpacing(float spacing);
  70. std::size_t getItemCount();
  71. const MenuItem* getItem(std::size_t index) const;
  72. MenuItem* getItem(std::size_t index);
  73. const MenuItem* getSelectedItem() const;
  74. MenuItem* getSelectedItem();
  75. const UIContainer* getUIContainer() const;
  76. UIContainer* getUIContainer();
  77. /**
  78. * Deselects the currently selected item (if any)
  79. */
  80. void deselect();
  81. /**
  82. * Selects the item at the specified index
  83. */
  84. void select(std::size_t index);
  85. /**
  86. * Activates the selected item (if any)
  87. */
  88. void activate();
  89. /**
  90. * Recalculates the dimensions of the UI container according the dimensions of the menu item labels and the line spacing.
  91. */
  92. void resize();
  93. private:
  94. friend class MenuItem;
  95. std::vector<MenuItem*> items;
  96. MenuItem* selectedItem;
  97. std::function<void()> enteredCallback;
  98. std::function<void()> exitedCallback;
  99. Font* font;
  100. float lineSpacing;
  101. UIContainer* container;
  102. };
  103. inline std::size_t Menu::getItemCount()
  104. {
  105. return items.size();
  106. }
  107. inline const MenuItem* Menu::getItem(std::size_t index) const
  108. {
  109. return items[index];
  110. }
  111. inline MenuItem* Menu::getItem(std::size_t index)
  112. {
  113. return items[index];
  114. }
  115. inline const MenuItem* Menu::getSelectedItem() const
  116. {
  117. return selectedItem;
  118. }
  119. inline MenuItem* Menu::getSelectedItem()
  120. {
  121. return selectedItem;
  122. }
  123. inline const UIContainer* Menu::getUIContainer() const
  124. {
  125. return container;
  126. }
  127. inline UIContainer* Menu::getUIContainer()
  128. {
  129. return container;
  130. }
  131. #endif // MENU_HPP