TGUI  1.6.1
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/Text.hpp>
31
33
34TGUI_MODULE_EXPORT namespace tgui
35{
36 class MenuBarMenuPlaceholder;
37
41 class TGUI_API MenuBar : public Widget
42 {
43 public:
44
45 using Ptr = std::shared_ptr<MenuBar>;
46 using ConstPtr = std::shared_ptr<const MenuBar>;
47
48 static constexpr const char StaticWidgetType[] = "MenuBar";
49
52 {
53 String text;
54 bool enabled;
55 std::vector<GetMenusElement> menuItems;
56 };
57
59 struct Menu
60 {
61 Text text;
62 bool enabled = true;
63 int selectedMenuItem = -1;
64 std::vector<Menu> menuItems;
65 };
66
74 MenuBar(const char* typeName = StaticWidgetType, bool initRenderer = true);
75
77 // Copy constructor
79 MenuBar(const MenuBar& other);
80
82 // Move constructor
84 MenuBar(MenuBar&& other) noexcept;
85
87 // Copy assignment operator
89 MenuBar& operator=(const MenuBar& other);
90
92 // Move assignment operator
94 MenuBar& operator=(MenuBar&& other) noexcept;
95
101 TGUI_NODISCARD static MenuBar::Ptr create();
102
110 TGUI_NODISCARD static MenuBar::Ptr copy(const MenuBar::ConstPtr& menuBar);
111
116 TGUI_NODISCARD MenuBarRenderer* getSharedRenderer() override;
117 TGUI_NODISCARD const MenuBarRenderer* getSharedRenderer() const override;
118
124 TGUI_NODISCARD MenuBarRenderer* getRenderer() override;
125
139 template <typename Func, typename... Args>
140 unsigned int connectMenuItem(const String& menu, const String& menuItem, Func&& handler, const Args&... args)
141 {
142 return connectMenuItem({menu, menuItem}, std::forward<Func>(handler), args...);
143 }
144
158 template <typename Func, typename... Args>
159 unsigned int connectMenuItem(const std::vector<String>& hierarchy, Func&& handler, const Args&... args)
160 {
161#if defined(__cpp_lib_invoke) && (__cpp_lib_invoke >= 201411L)
162 return onMenuItemClick.connect(
163 [=](const std::vector<String>& clickedMenuItem)
164 {
165 if (clickedMenuItem == hierarchy)
166 std::invoke(handler, args...);
167 }
168 );
169#else
170 return onMenuItemClick.connect(
171 [f=std::function<void(const Args&...)>(handler),args...,hierarchy](const std::vector<String>& clickedMenuItem)
172 {
173 if (clickedMenuItem == hierarchy)
174 f(args...);
175 }
176 );
177#endif
178 }
179
187 void setSize(const Layout2d& size) override;
188 using Widget::setSize;
189
197 void setEnabled(bool enabled) override;
198
204 void addMenu(const String& text);
205
223 bool addMenuItem(const String& text);
224
243 bool addMenuItem(const String& menu, const String& text);
244
261 bool addMenuItem(const std::vector<String>& hierarchy, bool createParents = true);
262
279 bool changeMenuItem(const std::vector<String>& hierarchy, const String& text);
280
285
295 bool removeMenu(const String& menu);
296
305 bool removeMenuItem(const String& menu, const String& menuItem);
306
320 bool removeMenuItem(const std::vector<String>& hierarchy, bool removeParentsWhenEmpty = true);
321
329 bool removeMenuItems(const String& menu);
330
342 bool removeSubMenuItems(const std::vector<String>& hierarchy);
343
350 bool setMenuEnabled(const String& menu, bool enabled);
351
357 TGUI_NODISCARD bool getMenuEnabled(const String& menu) const;
358
366 bool setMenuItemEnabled(const String& menu, const String& menuItem, bool enabled);
367
374 bool setMenuItemEnabled(const std::vector<String>& hierarchy, bool enabled);
375
382 TGUI_NODISCARD bool getMenuItemEnabled(const String& menu, const String& menuItem) const;
383
389 TGUI_NODISCARD bool getMenuItemEnabled(const std::vector<String>& hierarchy) const;
390
399 void setMinimumSubMenuWidth(float minimumWidth);
400
408 TGUI_NODISCARD float getMinimumSubMenuWidth() const;
409
415 void setInvertedMenuDirection(bool invertDirection);
416
422 TGUI_NODISCARD bool getInvertedMenuDirection() const;
423
428 TGUI_NODISCARD std::vector<GetMenusElement> getMenus() const;
429
433 void closeMenu();
434
440 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
441
445 bool leftMousePressed(Vector2f pos) override;
446
450 void leftMouseReleased(Vector2f pos) override;
451
455 void mouseMoved(Vector2f pos) override;
456
463 void draw(BackendRenderTarget& target, RenderStates states) const override;
464
466 protected:
467
477 TGUI_NODISCARD Signal& getSignal(String signalName) override;
478
484 void rendererChanged(const String& property) override;
485
489 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
490
494 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
495
499 void updateTextSize() override;
500
502 // Makes a copy of the widget
504 TGUI_NODISCARD Widget::Ptr clone() const override;
505
510 void openMenu(std::size_t menuIndex);
511
514 void createMenu(std::vector<Menu>& menus, const String& text);
515
519 TGUI_NODISCARD Menu* findMenu(const std::vector<String>& hierarchy, unsigned int parentIndex, std::vector<Menu>& menus, bool createParents);
520
524 TGUI_NODISCARD const Menu* findMenu(const std::vector<String>& hierarchy, unsigned int parentIndex, const std::vector<Menu>& menus) const;
525
528 TGUI_NODISCARD Menu* findMenuItem(const std::vector<String>& hierarchy);
529
532 TGUI_NODISCARD const Menu* findMenuItem(const std::vector<String>& hierarchy) const;
533
536 void loadMenus(const std::unique_ptr<DataIO::Node>& node, std::vector<Menu>& menus);
537
540 void closeSubMenus(std::vector<Menu>& menus, int& selectedMenu);
541
543 void deselectBottomItem();
544
546 void updateMenuTextColor(Menu& menu, bool selected);
547
549 void updateTextColors(std::vector<Menu>& menus, int selectedMenu);
550
552 void updateTextOpacity(std::vector<Menu>& menus);
553
555 void updateTextFont(std::vector<Menu>& menus);
556
559 TGUI_NODISCARD float calculateMenuWidth(const Menu& menu) const;
560
563 TGUI_NODISCARD float getMenuItemHeight(const Menu& menuItem) const;
564
567 TGUI_NODISCARD float calculateOpenMenuHeight(const std::vector<Menu>& menuItems) const;
568
570 TGUI_NODISCARD Vector2f calculateSubmenuOffset(const Menu& menu, float globalLeftPos, float menuWidth, float subMenuWidth, bool& openSubMenuToRight) const;
571
573 TGUI_NODISCARD bool isMouseOnTopOfMenu(Vector2f menuPos, Vector2f mousePos, bool openSubMenuToRight, const Menu& menu, float menuWidth) const;
574
576 TGUI_NODISCARD bool findMenuItemBelowMouse(Vector2f menuPos, Vector2f mousePos, bool openSubMenuToRight, Menu& menu, float menuWidth, Menu** resultMenu, std::size_t* resultSelectedMenuItem);
577
580 void drawMenusOnBar(BackendRenderTarget& target, RenderStates states) const;
581
584 void drawMenu(BackendRenderTarget& target, RenderStates states, const Menu& menu, float menuWidth, float globalLeftPos, bool openSubMenuToRight) const;
585
592 TGUI_NODISCARD bool isMouseOnOpenMenu(Vector2f pos) const;
593
598 void leftMouseReleasedOnMenu();
599
605 void mouseMovedOnMenu(Vector2f pos);
606
613 void drawOpenMenu(BackendRenderTarget& target, RenderStates states) const;
614
616 public:
617
622 SignalItemHierarchy onMenuItemClick = {"MenuItemClicked"};
623
625 protected:
626
627 std::vector<Menu> m_menus;
628 std::shared_ptr<MenuBarMenuPlaceholder> m_menuWidgetPlaceholder;
629
630 int m_visibleMenu = -1;
631
632 float m_minimumSubMenuWidth = 125;
633
634 bool m_invertedMenuDirection = false;
635
636 Sprite m_spriteBackground;
637 Sprite m_spriteItemBackground;
638 Sprite m_spriteSelectedItemBackground;
639
640 // Cached renderer properties
641 Color m_backgroundColorCached;
642 Color m_selectedBackgroundColorCached;
643 Color m_textColorCached;
644 Color m_selectedTextColorCached;
645 Color m_textColorDisabledCached;
646 Color m_separatorColorCached = Color::Black;
647 float m_separatorThicknessCached = 1;
648 float m_separatorVerticalPaddingCached = 0;
649 float m_separatorSidePaddingCached = 0;
650 float m_distanceToSideCached = 0;
651
653
654 friend class MenuBarMenuPlaceholder;
655 };
656
662 {
663 public:
664
665 // Instances of this class can't be copied
667 MenuBarMenuPlaceholder& operator=(const MenuBarMenuPlaceholder&) = delete;
668
674
682 TGUI_NODISCARD Vector2f getFullSize() const override;
683
691 TGUI_NODISCARD Vector2f getWidgetOffset() const override;
692
697 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
698
705 void draw(BackendRenderTarget& target, RenderStates states) const override;
706
710 void leftMouseButtonNoLongerDown() override;
711
715 void mouseMoved(Vector2f pos) override;
716
718 // Makes a copy of the widget
720 TGUI_NODISCARD Widget::Ptr clone() const override;
721
723 private:
724 MenuBar* m_menuBar;
725 bool m_mouseWasOnMenuBar = true; // When a menu opens then the mouse will be on top of the menu bar
726 };
727
729}
730
732
733#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:323
Widget that is added to a container when the user clicks on the menu bar. This widget will be added i...
Definition MenuBar.hpp:662
Vector2f getFullSize() const override
Returns the entire size that the widget is using.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
MenuBarMenuPlaceholder(MenuBar *menuBar)
Constructor.
Vector2f getWidgetOffset() const override
Returns the distance between the position where the widget is drawn and where the widget is placed.
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...
Renderer for the MenuBar widget.
Definition MenuBarRenderer.hpp:38
Menu bar widget.
Definition MenuBar.hpp:42
bool setMenuItemEnabled(const String &menu, const String &menuItem, bool enabled)
Enable or disable a 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:159
bool removeSubMenuItems(const std::vector< String > &hierarchy)
Removes a all menu items below a (sub) menu.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
bool changeMenuItem(const std::vector< String > &hierarchy, const String &text)
Changes the text of an existing menu item.
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.
float getMinimumSubMenuWidth() const
Returns the distance between the text and the side of the menu item.
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.
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.
bool getInvertedMenuDirection() const
Returns whether the menus open above or below the menu bar.
void openMenu(std::size_t menuIndex)
Opens a menu.
std::shared_ptr< const MenuBar > ConstPtr
Shared constant widget pointer.
Definition MenuBar.hpp:46
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 draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
std::vector< GetMenusElement > getMenus() const
Returns the menus and their menu items, including submenus.
void addMenu(const String &text)
Adds a new menu.
MenuBarRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
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.
static MenuBar::Ptr copy(const MenuBar::ConstPtr &menuBar)
Makes a copy of another menu bar.
bool getMenuEnabled(const String &menu) const
Check if an entire menu is enabled or disabled.
void setMinimumSubMenuWidth(float minimumWidth)
Changes the minimum width of the submenus.
std::shared_ptr< MenuBar > Ptr
Shared widget pointer.
Definition MenuBar.hpp:45
static MenuBar::Ptr create()
Creates a new menu bar widget.
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)
bool getMenuItemEnabled(const std::vector< String > &hierarchy) const
Check if a menu item is enabled or disabled.
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:140
void setInvertedMenuDirection(bool invertDirection)
Changes whether the menus open above or below the menu bar.
MenuBarRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setSize(const Layout2d &size) override
Changes the size of the menu bar.
bool getMenuItemEnabled(const String &menu, const String &menuItem) const
Check if a menu item is enabled or disabled.
bool removeMenu(const String &menu)
Removes a menu.
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:1060
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:61
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:52
Definition MenuBar.hpp:60
States used for drawing.
Definition RenderStates.hpp:38