TGUI  1.2.0
Loading...
Searching...
No Matches
MenuBar.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25
26#ifndef TGUI_MENU_BAR_HPP
27#define TGUI_MENU_BAR_HPP
28
29
30#include <TGUI/Widget.hpp>
31#include <TGUI/Renderers/MenuBarRenderer.hpp>
32#include <TGUI/CopiedSharedPtr.hpp>
33#include <TGUI/Text.hpp>
34
36
37TGUI_MODULE_EXPORT namespace tgui
38{
39 class MenuBarMenuPlaceholder;
40
41
45 class TGUI_API MenuBar : public Widget
46 {
47 public:
48
49 using Ptr = std::shared_ptr<MenuBar>;
50 using ConstPtr = std::shared_ptr<const MenuBar>;
51
52 static constexpr const char StaticWidgetType[] = "MenuBar";
53
54
57 {
58 String text;
59 bool enabled;
60 std::vector<GetMenusElement> menuItems;
61 };
62
64 struct Menu
65 {
66 Text text;
67 bool enabled = true;
68 int selectedMenuItem = -1;
69 std::vector<Menu> menuItems;
70 };
71
72
80 MenuBar(const char* typeName = StaticWidgetType, bool initRenderer = true);
81
82
84 // Copy constructor
86 MenuBar(const MenuBar& other);
87
89 // Move constructor
91 MenuBar(MenuBar&& other) noexcept;
92
94 // Copy assignment operator
96 MenuBar& operator=(const MenuBar& other);
97
99 // Move assignment operator
101 MenuBar& operator=(MenuBar&& other) noexcept;
102
103
110 TGUI_NODISCARD static MenuBar::Ptr create();
111
112
121 TGUI_NODISCARD static MenuBar::Ptr copy(const MenuBar::ConstPtr& menuBar);
122
123
128 TGUI_NODISCARD MenuBarRenderer* getSharedRenderer() override;
129 TGUI_NODISCARD const MenuBarRenderer* getSharedRenderer() const override;
130
136 TGUI_NODISCARD MenuBarRenderer* getRenderer() override;
137
138
152 template <typename Func, typename... Args>
153 unsigned int connectMenuItem(const String& menu, const String& menuItem, Func&& handler, const Args&... args)
154 {
155 return connectMenuItem({menu, menuItem}, std::forward<Func>(handler), args...);
156 }
157
158
172 template <typename Func, typename... Args>
173 unsigned int connectMenuItem(const std::vector<String>& hierarchy, Func&& handler, const Args&... args)
174 {
175#if defined(__cpp_lib_invoke) && (__cpp_lib_invoke >= 201411L)
176 return onMenuItemClick.connect(
177 [=](const std::vector<String>& clickedMenuItem)
178 {
179 if (clickedMenuItem == hierarchy)
180 std::invoke(handler, args...);
181 }
182 );
183#else
184 return onMenuItemClick.connect(
185 [f=std::function<void(const Args&...)>(handler),args...,hierarchy](const std::vector<String>& clickedMenuItem)
186 {
187 if (clickedMenuItem == hierarchy)
188 f(args...);
189 }
190 );
191#endif
192 }
193
194
203 void setSize(const Layout2d& size) override;
204 using Widget::setSize;
205
206
214 void setEnabled(bool enabled) override;
215
216
223 void addMenu(const String& text);
224
225
243 bool addMenuItem(const String& text);
244
245
264 bool addMenuItem(const String& menu, const String& text);
265
266
283 bool addMenuItem(const std::vector<String>& hierarchy, bool createParents = true);
284
285
302 bool changeMenuItem(const std::vector<String>& hierarchy, const String& text);
303
304
309
310
321 bool removeMenu(const String& menu);
322
323
333 bool removeMenuItem(const String& menu, const String& menuItem);
334
335
349 bool removeMenuItem(const std::vector<String>& hierarchy, bool removeParentsWhenEmpty = true);
350
351
360 bool removeMenuItems(const String& menu);
361
362
374 bool removeSubMenuItems(const std::vector<String>& hierarchy);
375
376
383 bool setMenuEnabled(const String& menu, bool enabled);
384
385
391 TGUI_NODISCARD bool getMenuEnabled(const String& menu) const;
392
393
401 bool setMenuItemEnabled(const String& menu, const String& menuItem, bool enabled);
402
403
410 bool setMenuItemEnabled(const std::vector<String>& hierarchy, bool enabled);
411
412
419 TGUI_NODISCARD bool getMenuItemEnabled(const String& menu, const String& menuItem) const;
420
421
427 TGUI_NODISCARD bool getMenuItemEnabled(const std::vector<String>& hierarchy) const;
428
429
439 void setMinimumSubMenuWidth(float minimumWidth);
440
441
450 TGUI_NODISCARD float getMinimumSubMenuWidth() const;
451
452
459 void setInvertedMenuDirection(bool invertDirection);
460
461
468 TGUI_NODISCARD bool getInvertedMenuDirection() const;
469
470
475 TGUI_NODISCARD std::vector<GetMenusElement> getMenus() const;
476
477
481 void closeMenu();
482
483
490 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
491
495 bool leftMousePressed(Vector2f pos) override;
496
500 void leftMouseReleased(Vector2f pos) override;
501
505 void mouseMoved(Vector2f pos) override;
506
507
515 void draw(BackendRenderTarget& target, RenderStates states) const override;
516
517
519 protected:
520
530 TGUI_NODISCARD Signal& getSignal(String signalName) override;
531
532
539 void rendererChanged(const String& property) override;
540
541
545 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
546
547
551 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
552
553
557 void updateTextSize() override;
558
559
561 // Makes a copy of the widget
563 TGUI_NODISCARD Widget::Ptr clone() const override;
564
565
570 void openMenu(std::size_t menuIndex);
571
572
575 void createMenu(std::vector<Menu>& menus, const String& text);
576
580 TGUI_NODISCARD Menu* findMenu(const std::vector<String>& hierarchy, unsigned int parentIndex, std::vector<Menu>& menus, bool createParents);
581
585 TGUI_NODISCARD const Menu* findMenu(const std::vector<String>& hierarchy, unsigned int parentIndex, const std::vector<Menu>& menus) const;
586
589 TGUI_NODISCARD Menu* findMenuItem(const std::vector<String>& hierarchy);
590
593 TGUI_NODISCARD const Menu* findMenuItem(const std::vector<String>& hierarchy) const;
594
597 void loadMenus(const std::unique_ptr<DataIO::Node>& node, std::vector<Menu>& menus);
598
601 void closeSubMenus(std::vector<Menu>& menus, int& selectedMenu);
602
604 void deselectBottomItem();
605
607 void updateMenuTextColor(Menu& menu, bool selected);
608
610 void updateTextColors(std::vector<Menu>& menus, int selectedMenu);
611
613 void updateTextOpacity(std::vector<Menu>& menus);
614
616 void updateTextFont(std::vector<Menu>& menus);
617
620 TGUI_NODISCARD float calculateMenuWidth(const Menu& menu) const;
621
624 TGUI_NODISCARD float getMenuItemHeight(const Menu& menuItem) const;
625
628 TGUI_NODISCARD float calculateOpenMenuHeight(const std::vector<Menu>& menuItems) const;
629
631 TGUI_NODISCARD Vector2f calculateSubmenuOffset(const Menu& menu, float globalLeftPos, float menuWidth, float subMenuWidth, bool& openSubMenuToRight) const;
632
634 TGUI_NODISCARD bool isMouseOnTopOfMenu(Vector2f menuPos, Vector2f mousePos, bool openSubMenuToRight, const Menu& menu, float menuWidth) const;
635
637 TGUI_NODISCARD bool findMenuItemBelowMouse(Vector2f menuPos, Vector2f mousePos, bool openSubMenuToRight, Menu& menu, float menuWidth, Menu** resultMenu, std::size_t* resultSelectedMenuItem);
638
641 void drawMenusOnBar(BackendRenderTarget& target, RenderStates states) const;
642
645 void drawMenu(BackendRenderTarget& target, RenderStates states, const Menu& menu, float menuWidth, float globalLeftPos, bool openSubMenuToRight) const;
646
647
654 TGUI_NODISCARD bool isMouseOnOpenMenu(Vector2f pos) const;
655
660 void leftMouseReleasedOnMenu();
661
667 void mouseMovedOnMenu(Vector2f pos);
668
675 void drawOpenMenu(BackendRenderTarget& target, RenderStates states) const;
676
677
679 public:
680
685 SignalItemHierarchy onMenuItemClick = {"MenuItemClicked"};
686
687
689 protected:
690
691 std::vector<Menu> m_menus;
692 std::shared_ptr<MenuBarMenuPlaceholder> m_menuWidgetPlaceholder;
693
694 int m_visibleMenu = -1;
695
696 float m_minimumSubMenuWidth = 125;
697
698 bool m_invertedMenuDirection = false;
699
700 Sprite m_spriteBackground;
701 Sprite m_spriteItemBackground;
702 Sprite m_spriteSelectedItemBackground;
703
704 // Cached renderer properties
705 Color m_backgroundColorCached;
706 Color m_selectedBackgroundColorCached;
707 Color m_textColorCached;
708 Color m_selectedTextColorCached;
709 Color m_textColorDisabledCached;
710 Color m_separatorColorCached = Color::Black;
711 float m_separatorThicknessCached = 1;
712 float m_separatorVerticalPaddingCached = 0;
713 float m_separatorSidePaddingCached = 0;
714 float m_distanceToSideCached = 0;
715
717
718 friend class MenuBarMenuPlaceholder;
719 };
720
721
727 {
728 public:
729
730 // Instances of this class can't be copied
732 MenuBarMenuPlaceholder& operator=(const MenuBarMenuPlaceholder&) = delete;
733
734
740
741
749 TGUI_NODISCARD Vector2f getFullSize() const override;
750
751
759 TGUI_NODISCARD Vector2f getWidgetOffset() const override;
760
761
766 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
767
768
775 void draw(BackendRenderTarget& target, RenderStates states) const override;
776
777
781 void leftMouseButtonNoLongerDown() override;
782
786 void mouseMoved(Vector2f pos) override;
787
788
790 // Makes a copy of the widget
792 TGUI_NODISCARD Widget::Ptr clone() const override;
793
794
796 private:
797 MenuBar* m_menuBar;
798 bool m_mouseWasOnMenuBar = true; // When a menu opens then the mouse will be on top of the menu bar
799 };
800
802}
803
804
806
807#endif // TGUI_MENU_BAR_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:48
Wrapper for colors.
Definition Color.hpp:72
Class to store the position or size of a widget.
Definition Layout.hpp:305
Widget that is added to a container when the user clicks on the menu bar. This widget will be added i...
Definition MenuBar.hpp:727
TGUI_NODISCARD Vector2f getFullSize() const override
Returns the entire size that the widget is using.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of an open me...
MenuBarMenuPlaceholder(MenuBar *menuBar)
Constructor.
TGUI_NODISCARD Vector2f getWidgetOffset() const override
Returns the distance between the position where the widget is drawn and where the widget is placed.
TGUI_NODISCARD Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Renderer for the MenuBar widget.
Definition MenuBarRenderer.hpp:40
Menu bar widget.
Definition MenuBar.hpp:46
TGUI_NODISCARD bool getMenuItemEnabled(const std::vector< String > &hierarchy) const
Check if a menu item is enabled or disabled.
TGUI_NODISCARD bool getMenuEnabled(const String &menu) const
Check if an entire menu is enabled or disabled.
bool setMenuItemEnabled(const String &menu, const String &menuItem, bool enabled)
Enable or disable a menu item.
TGUI_NODISCARD float getMinimumSubMenuWidth() const
Returns the distance between the text and the side of the menu item.
bool removeMenuItems(const String &menu)
Removes all menu items from a menu.
bool setMenuEnabled(const String &menu, bool enabled)
Enable or disable an entire menu.
void setEnabled(bool enabled) override
Enables or disables the widget.
unsigned int connectMenuItem(const std::vector< String > &hierarchy, Func &&handler, const Args &... args)
Connects a signal handler to the "MenuItemClicked" callback that will only be called when a specific ...
Definition MenuBar.hpp:173
bool removeSubMenuItems(const std::vector< String > &hierarchy)
Removes a all menu items below a (sub) menu.
bool changeMenuItem(const std::vector< String > &hierarchy, const String &text)
Changes the text of an existing menu item.
TGUI_NODISCARD Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
void closeMenu()
Closes the open menu when one of the menus is open.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
TGUI_NODISCARD MenuBarRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
TGUI_NODISCARD std::vector< GetMenusElement > getMenus() const
Returns the menus and their menu items, including submenus.
bool addMenuItem(const String &menu, const String &text)
Adds a new menu item to an existing menu.
bool addMenuItem(const std::vector< String > &hierarchy, bool createParents=true)
Adds a new menu item (or sub menu item)
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
TGUI_NODISCARD bool getMenuItemEnabled(const String &menu, const String &menuItem) const
Check if a menu item is enabled or disabled.
void openMenu(std::size_t menuIndex)
Opens a menu.
TGUI_NODISCARD MenuBarRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
std::shared_ptr< const MenuBar > ConstPtr
Shared constant widget pointer.
Definition MenuBar.hpp:50
static TGUI_NODISCARD MenuBar::Ptr copy(const MenuBar::ConstPtr &menuBar)
Makes a copy of another menu bar.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
void addMenu(const String &text)
Adds a new menu.
void removeAllMenus()
Removes all menus.
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer)
bool removeMenuItem(const String &menu, const String &menuItem)
Removes a menu item.
TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
void setMinimumSubMenuWidth(float minimumWidth)
Changes the minimum width of the submenus.
std::shared_ptr< MenuBar > Ptr
Shared widget pointer.
Definition MenuBar.hpp:49
TGUI_NODISCARD Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
bool addMenuItem(const String &text)
Adds a new menu item to the last added menu.
bool setMenuItemEnabled(const std::vector< String > &hierarchy, bool enabled)
Enable or disable a menu item.
bool removeMenuItem(const std::vector< String > &hierarchy, bool removeParentsWhenEmpty=true)
Removes a menu item (or sub menu item)
unsigned int connectMenuItem(const String &menu, const String &menuItem, Func &&handler, const Args &... args)
Connects a signal handler to the "MenuItemClicked" callback that will only be called when a specific ...
Definition MenuBar.hpp:153
void setInvertedMenuDirection(bool invertDirection)
Changes whether the menus open above or below the menu bar.
TGUI_NODISCARD bool getInvertedMenuDirection() const
Returns whether the menus open above or below the menu bar.
TGUI_NODISCARD std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
static TGUI_NODISCARD MenuBar::Ptr create()
Creates a new menu bar widget.
void setSize(const Layout2d &size) override
Changes the size of the menu bar.
bool removeMenu(const String &menu)
Removes a menu.
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:1131
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:62
Definition Sprite.hpp:48
Wrapper class to store strings.
Definition String.hpp:101
Backend-independent wrapper around the backend-specific text class.
Definition Text.hpp:50
The parent class for every widget.
Definition Widget.hpp:84
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:87
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39
Used for return value of getMenus.
Definition MenuBar.hpp:57
Definition MenuBar.hpp:65
States used for drawing.
Definition RenderStates.hpp:39