BsGUIMenuBar.h 8.3 KB

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