PolyUIMenu.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. Copyright (C) 2012 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "polycode/modules/ui/PolyUIElement.h"
  21. #include "polycode/core/PolySceneLabel.h"
  22. #include "polycode/modules/ui/PolyUIBox.h"
  23. namespace Polycode {
  24. class SceneLine;
  25. /**
  26. * An single selectable entry in an UIMenu
  27. */
  28. class _PolyExport UIMenuItem : public UIElement {
  29. public:
  30. UIMenuItem(Core *core, ResourcePool *pool, String label, String _id, void *data, Number comboWidth, Number comboHeight);
  31. virtual ~UIMenuItem();
  32. virtual bool isSelectable();
  33. String getMenuItemID();
  34. /**
  35. * The user-data associated with this entry, as set in UIMenu::addOption()
  36. */
  37. void *data;
  38. /**
  39. * The display text of this entry, as set in UIMenu::addOption()
  40. */
  41. String label;
  42. /**
  43. * The internal ID of this entry, as set in UIMenu::addOption()
  44. *
  45. * Use this instead of label to identify an option, as the label
  46. * may be changed by e.g. translations.
  47. */
  48. String _id;
  49. protected:
  50. UIMenuItem(Core *core);
  51. SceneLabel *itemLabel;
  52. friend class UIMenu;
  53. };
  54. class _PolyExport UIMenuDivider : public UIMenuItem {
  55. public:
  56. UIMenuDivider(Core *core, ResourcePool *pool, Number comboWidth, Number comboHeight);
  57. ~UIMenuDivider();
  58. bool isSelectable();
  59. protected:
  60. SceneLine* line;
  61. };
  62. /**
  63. * A dropdown menu.
  64. *
  65. * Displays a vertical list of text entries that can be clicked.
  66. * When clicked, an UIEvent::OK_EVENT event will be dispatched.
  67. * You can then retrieve the selected entry with getSelectedItem()
  68. */
  69. class _PolyExport UIMenu : public UIElement {
  70. public:
  71. /**
  72. * Create an empty dropdown menu with the given width.
  73. *
  74. * Use addOption() to populate the menu.
  75. */
  76. UIMenu(Core *core, ResourcePool *pool, Number menuWidth);
  77. ~UIMenu();
  78. /**
  79. * Add a selectable entry to the dropdown.
  80. *
  81. * @param label The text string that will be displayed to the user for this entry.
  82. * @param _id The internal ID of this entry.
  83. * @param data A pointer to arbitrary user-data associated with this menu entry.
  84. *
  85. * @see UIMenuItem
  86. */
  87. UIMenuItem *addOption(String label, String _id, void *data = NULL);
  88. /**
  89. * Add a non-selectable entry to the dropdown
  90. * used to separate sections of the menu
  91. *
  92. */
  93. UIMenuItem *addDivider();
  94. /**
  95. * Returns the currently selected item.
  96. *
  97. * This can be used both for retrieving the currently selected item while the menu
  98. * is open, as well as retrieving the last selected item after an UIEvent::OK_EVENT
  99. * was dispatched.
  100. */
  101. UIMenuItem *getSelectedItem();
  102. void Resize(Number width, Number height);
  103. void Update(Number elapsed);
  104. void handleEvent(Event *event);
  105. void fitToScreenVertical();
  106. protected:
  107. Vector2 initialMouse;
  108. Number menuItemHeight;
  109. Number menuWidth;
  110. Number selectorPadding;
  111. Number nextItemHeight;
  112. int selectedOffset;
  113. UIBox *dropDownBox;
  114. UIBox *selectorBox;
  115. Number paddingX;
  116. Number paddingY;
  117. bool ignoreMouse;
  118. ResourcePool *resourcePool;
  119. std::vector<UIMenuItem*> items;
  120. };
  121. class _PolyExport UIGlobalMenu : public UIElement {
  122. public:
  123. UIGlobalMenu(Core *core, ResourcePool *pool);
  124. ~UIGlobalMenu();
  125. void handleEvent(Event *event);
  126. void Update(Number elapsed);
  127. void hideMenu();
  128. UIMenu *showMenu(Number x, Number y, Number width);
  129. UIMenu *showMenuAtMouse(Number width);
  130. Number defaultMenuWidth;
  131. protected:
  132. ResourcePool *resourcePool;
  133. bool willHideMenu;
  134. UIMenu *currentMenu;
  135. };
  136. }