BsGUIMenu.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisites.h"
  5. #include "BsGUIDropDownMenu.h"
  6. #include "BsShortcutKey.h"
  7. namespace BansheeEngine
  8. {
  9. /** @addtogroup GUI-Internal
  10. * @{
  11. */
  12. class GUIMenuItem;
  13. /** Used for comparing GUI menu items in order to determine the order in which they are presented. */
  14. struct GUIMenuItemComparer
  15. {
  16. bool operator() (const GUIMenuItem* const& a, const GUIMenuItem* const& b);
  17. };
  18. /** Holds information about a single element in a GUI menu. */
  19. class BS_EXPORT GUIMenuItem
  20. {
  21. public:
  22. /**
  23. * Constructs a new non-separator menu item.
  24. *
  25. * @param[in] parent Parent item, if any.
  26. * @param[in] name Name of the item to be displayed.
  27. * @param[in] callback Callback to be triggered when menu items is selected.
  28. * @param[in] priority Priority that determines the order of this element compared to its siblings.
  29. * @param[in] seqIdx Sequential index of the menu item that specifies in what order was it added to the menu
  30. * compared to other items.
  31. * @param[in] key Keyboard shortcut that can be used for triggering the menu item.
  32. */
  33. GUIMenuItem(GUIMenuItem* parent, const WString& name, std::function<void()> callback,
  34. INT32 priority, UINT32 seqIdx, const ShortcutKey& key);
  35. /**
  36. * Constructs a new separator menu item.
  37. *
  38. * @param[in] parent Parent item, if any.
  39. * @param[in] priority Priority that determines the order of this element compared to its siblings.
  40. * @param[in] seqIdx Sequential index of the menu item that specifies in what order was it added to the menu
  41. * compared to other items.
  42. */
  43. GUIMenuItem(GUIMenuItem* parent, INT32 priority, UINT32 seqIdx);
  44. ~GUIMenuItem();
  45. /** Registers a new child with the item. */
  46. void addChild(GUIMenuItem* child) { mChildren.insert(child); }
  47. /** Returns number of child menu items. */
  48. UINT32 getNumChildren() const { return (UINT32)mChildren.size(); }
  49. /** Returns the parent menu item, or null if none. */
  50. GUIMenuItem* getParent() const { return mParent; }
  51. /** Returns name of the menu item. Empty if separator. */
  52. const WString& getName() const { return mName; }
  53. /** Returns callback that will trigger when menu item is selected. Null for separators. */
  54. std::function<void()> getCallback() const { return mCallback; }
  55. /** Returns a keyboard shortcut that may be used for triggering the menu item callback. */
  56. const ShortcutKey& getShortcut() const { return mShortcut; }
  57. /** Checks is the menu item a separator or a normal named menu item. */
  58. bool isSeparator() const { return mIsSeparator; }
  59. /** Attempts to find a child menu item with the specified name. Only direct descendants are searched. */
  60. const GUIMenuItem* findChild(const WString& name) const;
  61. /** Removes the first child with the specified name. */
  62. void removeChild(const WString& name);
  63. /** Removes the specified child. */
  64. void removeChild(const GUIMenuItem* item);
  65. private:
  66. friend class GUIMenu;
  67. friend struct GUIMenuItemComparer;
  68. /** @copydoc GUIMenuitem::findChild(const WString& name) const */
  69. GUIMenuItem* findChild(const WString& name);
  70. GUIMenuItem* mParent;
  71. bool mIsSeparator;
  72. WString mName;
  73. std::function<void()> mCallback;
  74. INT32 mPriority;
  75. ShortcutKey mShortcut;
  76. UINT32 mSeqIdx;
  77. Set<GUIMenuItem*, GUIMenuItemComparer> mChildren;
  78. };
  79. /**
  80. * Class that allows creation of menus with drop down functionality.
  81. * Menu consists out of a number of top level elements, each of which opens
  82. * a drop down menu which may internally hold a deeper hierarchy of menus.
  83. *
  84. * @note
  85. * When specifying menu items you must provide a path. Path must be formated in a certain way. All path elements must
  86. * be separated by /, for example "View/Toolbars/Find". "View" would be the top level path element, "Toolbars" a child
  87. * in its menu that opens up its own submenu, and "Find" a child in the "Toolbars" sub-menu with an optional callback.
  88. * @note
  89. * This is an abstract class and you should provide specialized implementations for specific menu types.
  90. */
  91. class BS_EXPORT GUIMenu
  92. {
  93. public:
  94. GUIMenu();
  95. virtual ~GUIMenu();
  96. /**
  97. * Adds a new menu item with the specified callback.
  98. *
  99. * @param[in] path Path that determines where to add the element. See class information on how to specify
  100. * paths. All sub-elements of a path will be added automatically.
  101. * @param[in] callback Callback that triggers when the path element is selected.
  102. * @param[in] priority Priority determines the position of the menu item relative to its siblings. Higher
  103. * priority means it will be placed earlier in the menu.
  104. * @param[in] key Keyboard shortcut that can be used for triggering the menu item.
  105. * @return A menu item object that you may use for removing the menu item later. Its lifetime is
  106. * managed internally.
  107. */
  108. GUIMenuItem* addMenuItem(const WString& path, std::function<void()> callback, INT32 priority, const ShortcutKey& key = ShortcutKey::NONE);
  109. /**
  110. * Adds a new separator menu item with the specified callback.
  111. *
  112. * @param[in] path Path that determines where to add the element. See class information on how to specify
  113. * paths. All sub-elements of a path will be added automatically.
  114. * @param[in] priority Priority determines the position of the menu item relative to its siblings. Higher
  115. * priority means it will be placed earlier in the menu.
  116. * @return A menu item object that you may use for removing the menu item later. Its lifetime is
  117. * managed internally.
  118. */
  119. GUIMenuItem* addSeparator(const WString& path, INT32 priority);
  120. /** Returns a menu item at the specified path, or null if one is not found. */
  121. GUIMenuItem* getMenuItem(const WString& path);
  122. /** Removes the specified menu item from the path. If the menu item has any sub-menus they will also be removed. */
  123. void removeMenuItem(const GUIMenuItem* item);
  124. /**
  125. * Normally menu items use values from their paths as their names. However path labels don't provide a way of
  126. * localizing the menu item. This method allows you to set specific names (different from path labels) to each menu
  127. * item. All the values are localized so they will also be updated according to the string table.
  128. *
  129. * @param[in] menuItemLabel The menu item label. (for example if you have a menu like "View/Toolbars/Find, this
  130. * parameter would be either "View", "Toolbars" or "Find" depending which entry you
  131. * want to localize)
  132. * @param[in] localizedName Localized string with the name.
  133. */
  134. void setLocalizedName(const WString& menuItemLabel, const HString& localizedName);
  135. /** Returns data used for initializing a drop down list, for all elements. */
  136. GUIDropDownData getDropDownData() const;
  137. protected:
  138. /** Adds a menu item at the specified path, as a normal button or as a separator. */
  139. GUIMenuItem* addMenuItemInternal(const WString& path, std::function<void()> callback, bool isSeparator,
  140. INT32 priority, const ShortcutKey& key);
  141. /** Return drop down data for the specified menu. */
  142. GUIDropDownData getDropDownDataInternal(const GUIMenuItem& menu) const;
  143. GUIMenuItem mRootElement;
  144. UnorderedMap<WString, HString> mLocalizedEntryNames;
  145. UINT32 mNextIdx;
  146. };
  147. /** @} */
  148. }