BsGUIMenuBar.h 8.2 KB

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