BsGUIMenuBar.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 Toggles an existing toolbar button into an on or off state which changes the visuals of the button.
  124. *
  125. * @param name Name of the existing button to toggle.
  126. * @param on True to toggle on, false to toggle off (default).
  127. */
  128. void toggleToolbarButton(const String& name, bool on);
  129. /**
  130. * @brief Adds a new separator element to the tool bar.
  131. *
  132. * @param name Unique name of the separator that can be used for identifiying it.
  133. * @param priority Determines where is the separator positioned compared to other elements on the tool bar.
  134. * Higher priority elements get placed before lower priority ones.
  135. */
  136. void addToolBarSeparator(const String& name, INT32 priority = 0);
  137. /**
  138. * @brief Removes an element from the tool bar.
  139. *
  140. * @param name Unique name of the element to remove.
  141. */
  142. void removeToolBarButton(const String& name);
  143. private:
  144. /**
  145. * @brief Finds a top level sub-menu with the specified name.
  146. */
  147. const GUIMenuBarData* getSubMenu(const WString& name) const;
  148. /**
  149. * @brief Adds a new top level sub-menu button.
  150. */
  151. GUIMenuBarData* addNewButton(const WString& name, INT32 priority);
  152. /**
  153. * @brief Attempts to remove the first element from the specified path. First element
  154. * returned in specified in "pathRoot", and original "path" parameter is modified so
  155. * it no longer includes the first element.
  156. *
  157. * @return False if first element doesn't exist, true otherwise.
  158. */
  159. bool stripPath(WString& path, WString& pathRoot) const;
  160. /**
  161. * @brief Registers a shortcut with the global shortcut manager. Pressing the shortcut will trigger
  162. * the provided callback.
  163. */
  164. void registerShortcut(const WString& path, const ShortcutKey& shortcut, std::function<void()> callback);
  165. /**
  166. * @brief Unregisters a shortcut assigned to the provided path from the global shortcut manager.
  167. */
  168. void unregisterShortcut(const WString& path);
  169. /**
  170. * @brief Opens a top level sub-menu with the provided name.
  171. */
  172. void openSubMenu(const WString& name);
  173. /**
  174. * @brief Closes any currently active sub-menu.
  175. */
  176. void closeSubMenu();
  177. /**
  178. * @brief Triggered when a sub-menu is open and a user hovers over another
  179. * top level sub-menu button
  180. *
  181. * @param name Name of the sub-menu the user is hovering over.
  182. */
  183. void onSubMenuHover(const WString& name);
  184. /**
  185. * @brief Triggered when a sub-menu is closed.
  186. */
  187. void onSubMenuClosed();
  188. /**
  189. * @brief Triggered when the minimize button is clicked.
  190. * Minimizes the attached window.
  191. */
  192. void onMinimizeClicked();
  193. /**
  194. * @brief Triggered when the maximize button is clicked.
  195. * Maximizes the attached window.
  196. */
  197. void onMaximizeClicked();
  198. /**
  199. * @brief Triggered when the close button is clicked.
  200. * Closes the attached window.
  201. */
  202. void onCloseClicked();
  203. /**
  204. * @brief Refreshes the OS client area that allow the window to be dragged
  205. * by dragging the empty areas on the menu bar. Should be called when top
  206. * level button configuration changes or menu bar area changes.
  207. */
  208. void refreshNonClientAreas();
  209. static const UINT32 NUM_ELEMENTS_AFTER_CONTENT;
  210. static const UINT32 ELEMENT_SPACING;
  211. RenderWindow* mParentWindow;
  212. CGUIWidget* mParentWidget;
  213. GUIPanel* mMainPanel;
  214. GUIPanel* mBgPanel;
  215. GUILayout* mMenuItemLayout;
  216. GUILayout* mToolBarLayout;
  217. GUITexture* mBgTexture;
  218. GUITexture* mLogoTexture;
  219. GUITexture* mSplitterLine;
  220. GUIButton* mMinBtn;
  221. GUIButton* mMaxBtn;
  222. GUIButton* mCloseBtn;
  223. Vector<GUIMenuBarData> mChildMenus;
  224. UnorderedMap<WString, ShortcutKey> mEntryShortcuts;
  225. Vector<GUIToolBarData> mToolbarElements;
  226. GUIButton* mSubMenuButton;
  227. bool mSubMenuOpen;
  228. };
  229. }