BsGUIMenuBar.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #pragma once
  2. #include "BsEditorPrerequisites.h"
  3. #include "BsShortcutKey.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief A menu bar GUI element that contains a horizontal list of elements that can each be expanded into a
  8. * hierarchical sub-menus, as well as a list of tool bar buttons.
  9. *
  10. * Contents of the menu and tool bar are customizable.
  11. *
  12. * The menu bar also displays the minimize, maximize and close buttons for the window.
  13. */
  14. class BS_ED_EXPORT GUIMenuBar
  15. {
  16. /**
  17. * @brief Contains data about the top level menu elements.
  18. */
  19. struct GUIMenuBarData
  20. {
  21. WString name;
  22. GUIMenu* menu;
  23. GUIButton* button;
  24. GUIFixedSpace* space;
  25. INT32 priority;
  26. };
  27. /**
  28. * @brief Contains data about a single tool bar element.
  29. */
  30. struct GUIToolBarData
  31. {
  32. String name;
  33. INT32 priority;
  34. GUIButton* button;
  35. GUITexture* separator;
  36. GUIFixedSpace* space;
  37. };
  38. public:
  39. /**
  40. * @brief Returns the style type for the menu bar background.
  41. */
  42. static const String& getBackgroundStyleType();
  43. /**
  44. * @brief Returns the style type for the menu bar line draw under the menu items.
  45. */
  46. static const String& getLineStyleType();
  47. /**
  48. * @brief Returns the style type for the menu bar logo.
  49. */
  50. static const String& getLogoStyleType();
  51. /**
  52. * @brief Returns the style type for the menu bar menu item buttons.
  53. */
  54. static const String& getMenuItemButtonStyleType();
  55. /**
  56. * @brief Returns the style type for tool bar buttons.
  57. */
  58. static const String& getToolBarButtonStyleType();
  59. /**
  60. * @brief Returns the style type for the tool bar button separator.
  61. */
  62. static const String& getToolBarSeparatorStyleType();
  63. /**
  64. * @brief Constructs a new menu bar.
  65. *
  66. * @param parent Parent GUI widget the menu bar will be docked in.
  67. * @param parentWindow Window to trigger the min/max/close events on.
  68. */
  69. GUIMenuBar(CGUIWidget* parent, RenderWindow* parentWindow);
  70. virtual ~GUIMenuBar();
  71. /**
  72. * @brief Sets the area of the menu bar, in pixels relative
  73. * to the parent GUI widget.
  74. */
  75. void setArea(INT32 x, INT32 y, UINT32 width, UINT32 height);
  76. /**
  77. * @brief Adds a new menu item to the menu bar.
  78. *
  79. * @param path Path to the menu item. Each element of the path must be separated using "/".
  80. * First element of the path will create the top level menu, and any further element
  81. * will create a new sub-menu. Last element will be the interactable element.
  82. * @param callback Callback to trigger when user click on the interactable element (last element in the provided path).
  83. * Can be null.
  84. * @param priority Determines where is the element positioned compared to other elements in the same sub-menu.
  85. * Higher priority elements get placed higher up in the sub-menu. This only applies to the last
  86. * element. If you need to customize its parent element priority call this method with
  87. * with their specific paths.
  88. * @param shortcut Keyboard shortcut key to display next to the interactable element, and register with the
  89. * global shortcut manager.
  90. */
  91. GUIMenuItem* addMenuItem(const WString& path, std::function<void()> callback, INT32 priority = 0, const ShortcutKey& shortcut = ShortcutKey::NONE);
  92. /**
  93. * @brief Adds a menu item separator element at the specified path. The separator is added as a child of the path.
  94. *
  95. * @param path Parent path of the sub-menu to add the separator.
  96. * @param priority Determines where is the separator positioned compared to other elements in the same sub-menu.
  97. * Higher priority elements get placed higher up in the sub-menu.
  98. */
  99. GUIMenuItem* addMenuItemSeparator(const WString& path, INT32 priority = 0);
  100. /**
  101. * @brief Returns an existing menu item at the specified path, or null if one cannot be found.
  102. */
  103. GUIMenuItem* getMenuItem(const WString& path);
  104. /**
  105. * @brief Removes a menu item from the specified path. If this path points to a sub-menu entire sub-menu will be removed.
  106. */
  107. void removeMenuItem(const WString& path);
  108. /**
  109. * @brief Removes the specified menu item.
  110. */
  111. void removeMenuItem(GUIMenuItem* item);
  112. /**
  113. * @brief Adds a new button to the tool bar.
  114. *
  115. * @param name Unique name of the button that can be used for identifiying it.
  116. * @param content Content to display on the button.
  117. * @param callback Callback to trigger when the button is pressed.
  118. * @param priority Determines where is the button positioned compared to other elements on the tool bar.
  119. * Higher priority elements get placed before lower priority ones.
  120. */
  121. void addToolBarButton(const String& name, const GUIContent& content, std::function<void()> callback, INT32 priority = 0);
  122. /**
  123. * @brief Adds a new separator element to the tool bar.
  124. *
  125. * @param name Unique name of the separator that can be used for identifiying it.
  126. * @param priority Determines where is the separator positioned compared to other elements on the tool bar.
  127. * Higher priority elements get placed before lower priority ones.
  128. */
  129. void addToolBarSeparator(const String& name, INT32 priority = 0);
  130. /**
  131. * @brief Removes an element from the tool bar.
  132. *
  133. * @param name Unique name of the element to remove.
  134. */
  135. void removeToolBarButton(const String& name);
  136. private:
  137. /**
  138. * @brief Finds a top level sub-menu with the specified name.
  139. */
  140. const GUIMenuBarData* getSubMenu(const WString& name) const;
  141. /**
  142. * @brief Adds a new top level sub-menu button.
  143. */
  144. GUIMenuBarData* addNewButton(const WString& name, INT32 priority);
  145. /**
  146. * @brief Attempts to remove the first element from the specified path. First element
  147. * returned in specified in "pathRoot", and original "path" parameter is modified so
  148. * it no longer includes the first element.
  149. *
  150. * @return False if first element doesn't exist, true otherwise.
  151. */
  152. bool stripPath(WString& path, WString& pathRoot) const;
  153. /**
  154. * @brief Registers a shortcut with the global shortcut manager. Pressing the shortcut will trigger
  155. * the provided callback.
  156. */
  157. void registerShortcut(const WString& path, const ShortcutKey& shortcut, std::function<void()> callback);
  158. /**
  159. * @brief Unregisters a shortcut assigned to the provided path from the global shortcut manager.
  160. */
  161. void unregisterShortcut(const WString& path);
  162. /**
  163. * @brief Opens a top level sub-menu with the provided name.
  164. */
  165. void openSubMenu(const WString& name);
  166. /**
  167. * @brief Closes any currently active sub-menu.
  168. */
  169. void closeSubMenu();
  170. /**
  171. * @brief Triggered when a sub-menu is open and a user hovers over another
  172. * top level sub-menu button
  173. *
  174. * @param name Name of the sub-menu the user is hovering over.
  175. */
  176. void onSubMenuHover(const WString& name);
  177. /**
  178. * @brief Triggered when a sub-menu is closed.
  179. */
  180. void onSubMenuClosed();
  181. /**
  182. * @brief Triggered when the minimize button is clicked.
  183. * Minimizes the attached window.
  184. */
  185. void onMinimizeClicked();
  186. /**
  187. * @brief Triggered when the maximize button is clicked.
  188. * Maximizes the attached window.
  189. */
  190. void onMaximizeClicked();
  191. /**
  192. * @brief Triggered when the close button is clicked.
  193. * Closes the attached window.
  194. */
  195. void onCloseClicked();
  196. /**
  197. * @brief Refreshes the OS client area that allow the window to be dragged
  198. * by dragging the empty areas on the menu bar. Should be called when top
  199. * level button configuration changes or menu bar area changes.
  200. */
  201. void refreshNonClientAreas();
  202. static const UINT32 NUM_ELEMENTS_AFTER_CONTENT;
  203. static const UINT32 ELEMENT_SPACING;
  204. RenderWindow* mParentWindow;
  205. CGUIWidget* mParentWidget;
  206. GUIPanel* mMainPanel;
  207. GUIPanel* mBgPanel;
  208. GUILayout* mMenuItemLayout;
  209. GUILayout* mToolBarLayout;
  210. GUITexture* mBgTexture;
  211. GUITexture* mLogoTexture;
  212. GUITexture* mSplitterLine;
  213. GUIButton* mMinBtn;
  214. GUIButton* mMaxBtn;
  215. GUIButton* mCloseBtn;
  216. Vector<GUIMenuBarData> mChildMenus;
  217. UnorderedMap<WString, ShortcutKey> mEntryShortcuts;
  218. Vector<GUIToolBarData> mToolbarElements;
  219. GUIButton* mSubMenuButton;
  220. bool mSubMenuOpen;
  221. };
  222. }