--- --- TGUI: include/TGUI/Widgets/MenuBar.hpp Source File
TGUI  1.x-dev
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#ifndef TGUI_MENU_BAR_HPP
26#define TGUI_MENU_BAR_HPP
27
28#include <TGUI/Widget.hpp>
29#include <TGUI/Renderers/MenuBarRenderer.hpp>
30#include <TGUI/CopiedSharedPtr.hpp>
31#include <TGUI/Text.hpp>
32
34
35TGUI_MODULE_EXPORT namespace tgui
36{
37 class MenuBarMenuPlaceholder;
38
42 class TGUI_API MenuBar : public Widget
43 {
44 public:
45
46 using Ptr = std::shared_ptr<MenuBar>;
47 using ConstPtr = std::shared_ptr<const MenuBar>;
48
49 static constexpr const char StaticWidgetType[] = "MenuBar";
50
53 {
54 String text;
55 bool enabled;
56 std::vector<GetMenusElement> menuItems;
57 };
58
60 struct Menu
61 {
62 Text text;
63 bool enabled = true;
64 int selectedMenuItem = -1;
65 std::vector<Menu> menuItems;
66 };
67
75 MenuBar(const char* typeName = StaticWidgetType, bool initRenderer = true);
76
78 // Copy constructor
80 MenuBar(const MenuBar& other);
81
83 // Move constructor
85 MenuBar(MenuBar&& other) noexcept;
86
88 // Copy assignment operator
90 MenuBar& operator=(const MenuBar& other);
91
93 // Move assignment operator
95 MenuBar& operator=(MenuBar&& other) noexcept;
96
102 TGUI_NODISCARD static MenuBar::Ptr create();
103
111 TGUI_NODISCARD static MenuBar::Ptr copy(const MenuBar::ConstPtr& menuBar);
112
117 TGUI_NODISCARD MenuBarRenderer* getSharedRenderer() override;
118 TGUI_NODISCARD const MenuBarRenderer* getSharedRenderer() const override;
119
125 TGUI_NODISCARD MenuBarRenderer* getRenderer() override;
126
140 template <typename Func, typename... Args>
141 unsigned int connectMenuItem(const String& menu, const String& menuItem, Func&& handler, const Args&... args)
142 {
143 return connectMenuItem({menu, menuItem}, std::forward<Func>(handler), args...);
144 }
145
159 template <typename Func, typename... Args>
160 unsigned int connectMenuItem(const std::vector<String>& hierarchy, Func&& handler, const Args&... args)
161 {
162#if defined(__cpp_lib_invoke) && (__cpp_lib_invoke >= 201411L)
163 return onMenuItemClick.connect(
164 [=](const std::vector<String>& clickedMenuItem)
165 {
166 if (clickedMenuItem == hierarchy)
167 std::invoke(handler, args...);
168 }
169 );
170#else
171 return onMenuItemClick.connect(
172 [f=std::function<void(const Args&...)>(handler),args...,hierarchy](const std::vector<String>& clickedMenuItem)
173 {
174 if (clickedMenuItem == hierarchy)
175 f(args...);
176 }
177 );
178#endif
179 }
180
188 void setSize(const Layout2d& size) override;
189 using Widget::setSize;
190
198 void setEnabled(bool enabled) override;
199
205 void addMenu(const String& text);
206
224 bool addMenuItem(const String& text);
225
244 bool addMenuItem(const String& menu, const String& text);
245
262 bool addMenuItem(const std::vector<String>& hierarchy, bool createParents = true);
263
280 bool changeMenuItem(const std::vector<String>& hierarchy, const String& text);
281
286
296 bool removeMenu(const String& menu);
297
306 bool removeMenuItem(const String& menu, const String& menuItem);
307
321 bool removeMenuItem(const std::vector<String>& hierarchy, bool removeParentsWhenEmpty = true);
322
330 bool removeMenuItems(const String& menu);
331
343 bool removeSubMenuItems(const std::vector<String>& hierarchy);
344
351 bool setMenuEnabled(const String& menu, bool enabled);
352
358 TGUI_NODISCARD bool getMenuEnabled(const String& menu) const;
359
367 bool setMenuItemEnabled(const String& menu, const String& menuItem, bool enabled);
368
375 bool setMenuItemEnabled(const std::vector<String>& hierarchy, bool enabled);
376
383 TGUI_NODISCARD bool getMenuItemEnabled(const String& menu, const String& menuItem) const;
384
390 TGUI_NODISCARD bool getMenuItemEnabled(const std::vector<String>& hierarchy) const;
391
400 void setMinimumSubMenuWidth(float minimumWidth);
401
409 TGUI_NODISCARD float getMinimumSubMenuWidth() const;
410
416 void setInvertedMenuDirection(bool invertDirection);
417
423 TGUI_NODISCARD bool getInvertedMenuDirection() const;
424
429 TGUI_NODISCARD std::vector<GetMenusElement> getMenus() const;
430
434 void closeMenu();
435
441 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
442
446 bool leftMousePressed(Vector2f pos) override;
447
451 void leftMouseReleased(Vector2f pos) override;
452
456 void mouseMoved(Vector2f pos) override;
457
464 void draw(BackendRenderTarget& target, RenderStates states) const override;
465
467 protected:
468
478 TGUI_NODISCARD Signal& getSignal(String signalName) override;
479
485 void rendererChanged(const String& property) override;
486
490 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
491
495 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
496
500 void updateTextSize() override;
501
503 // Makes a copy of the widget
505 TGUI_NODISCARD Widget::Ptr clone() const override;
506
511 void openMenu(std::size_t menuIndex);
512
515 void createMenu(std::vector<Menu>& menus, const String& text);
516
520 TGUI_NODISCARD Menu* findMenu(const std::vector<String>& hierarchy, unsigned int parentIndex, std::vector<Menu>& menus, bool createParents);
521
525 TGUI_NODISCARD const Menu* findMenu(const std::vector<String>& hierarchy, unsigned int parentIndex, const std::vector<Menu>& menus) const;
526
529 TGUI_NODISCARD Menu* findMenuItem(const std::vector<String>& hierarchy);
530
533 TGUI_NODISCARD const Menu* findMenuItem(const std::vector<String>& hierarchy) const;
534
537 void loadMenus(const std::unique_ptr<DataIO::Node>& node, std::vector<Menu>& menus);
538
541 void closeSubMenus(std::vector<Menu>& menus, int& selectedMenu);
542
544 void deselectBottomItem();
545
547 void updateMenuTextColor(Menu& menu, bool selected);
548
550 void updateTextColors(std::vector<Menu>& menus, int selectedMenu);
551
553 void updateTextOpacity(std::vector<Menu>& menus);
554
556 void updateTextFont(std::vector<Menu>& menus);
557
560 TGUI_NODISCARD float calculateMenuWidth(const Menu& menu) const;
561
564 TGUI_NODISCARD float getMenuItemHeight(const Menu& menuItem) const;
565
568 TGUI_NODISCARD float calculateOpenMenuHeight(const std::vector<Menu>& menuItems) const;
569
571 TGUI_NODISCARD Vector2f calculateSubmenuOffset(const Menu& menu, float globalLeftPos, float menuWidth, float subMenuWidth, bool& openSubMenuToRight) const;
572
574 TGUI_NODISCARD bool isMouseOnTopOfMenu(Vector2f menuPos, Vector2f mousePos, bool openSubMenuToRight, const Menu& menu, float menuWidth) const;
575
577 TGUI_NODISCARD bool findMenuItemBelowMouse(Vector2f menuPos, Vector2f mousePos, bool openSubMenuToRight, Menu& menu, float menuWidth, Menu** resultMenu, std::size_t* resultSelectedMenuItem);
578
581 void drawMenusOnBar(BackendRenderTarget& target, RenderStates states) const;
582
585 void drawMenu(BackendRenderTarget& target, RenderStates states, const Menu& menu, float menuWidth, float globalLeftPos, bool openSubMenuToRight) const;
586
593 TGUI_NODISCARD bool isMouseOnOpenMenu(Vector2f pos) const;
594
599 void leftMouseReleasedOnMenu();
600
606 void mouseMovedOnMenu(Vector2f pos);
607
614 void drawOpenMenu(BackendRenderTarget& target, RenderStates states) const;
615
617 public:
618
623 SignalItemHierarchy onMenuItemClick = {"MenuItemClicked"};
624
626 protected:
627
628 std::vector<Menu> m_menus;
629 std::shared_ptr<MenuBarMenuPlaceholder> m_menuWidgetPlaceholder;
630
631 int m_visibleMenu = -1;
632
633 float m_minimumSubMenuWidth = 125;
634
635 bool m_invertedMenuDirection = false;
636
637 Sprite m_spriteBackground;
638 Sprite m_spriteItemBackground;
639 Sprite m_spriteSelectedItemBackground;
640
641 // Cached renderer properties
642 Color m_backgroundColorCached;
643 Color m_selectedBackgroundColorCached;
644 Color m_textColorCached;
645 Color m_selectedTextColorCached;
646 Color m_textColorDisabledCached;
647 Color m_separatorColorCached = Color::Black;
648 float m_separatorThicknessCached = 1;
649 float m_separatorVerticalPaddingCached = 0;
650 float m_separatorSidePaddingCached = 0;
651 float m_distanceToSideCached = 0;
652
654
655 friend class MenuBarMenuPlaceholder;
656 };
657
663 {
664 public:
665
666 // Instances of this class can't be copied
668 MenuBarMenuPlaceholder& operator=(const MenuBarMenuPlaceholder&) = delete;
669
675
683 TGUI_NODISCARD Vector2f getFullSize() const override;
684
692 TGUI_NODISCARD Vector2f getWidgetOffset() const override;
693
698 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
699
706 void draw(BackendRenderTarget& target, RenderStates states) const override;
707
711 void leftMouseButtonNoLongerDown() override;
712
716 void mouseMoved(Vector2f pos) override;
717
719 // Makes a copy of the widget
721 TGUI_NODISCARD Widget::Ptr clone() const override;
722
724 private:
725 MenuBar* m_menuBar;
726 bool m_mouseWasOnMenuBar = true; // When a menu opens then the mouse will be on top of the menu bar
727 };
728
730}
731
733
734#endif // TGUI_MENU_BAR_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Wrapper for colors.
Definition Color.hpp:73
Class to store the position or size of a widget.
Definition Layout.hpp:313
Menu bar widget.
Definition MenuBar.hpp:43
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:160
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:47
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:46
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:141
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.
Widget that is added to a container when the user clicks on the menu bar. This widget will be added i...
Definition MenuBar.hpp:663
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:38
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:61
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:1060
Definition Sprite.hpp:47
Wrapper class to store strings.
Definition String.hpp:96
Backend-independent wrapper around the backend-specific text class.
Definition Text.hpp:48
The parent class for every widget.
Definition Widget.hpp:83
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:86
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
Used for return value of getMenus.
Definition MenuBar.hpp:53
Definition MenuBar.hpp:61
States used for drawing.
Definition RenderStates.hpp:38