TGUI  1.1
Loading...
Searching...
No Matches
MenuBar.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2023 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
476 TGUI_NODISCARD std::vector<GetMenusElement> getMenus() const;
477
478
482 void closeMenu();
483
484
491 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
492
496 bool leftMousePressed(Vector2f pos) override;
497
501 void leftMouseReleased(Vector2f pos) override;
502
506 void mouseMoved(Vector2f pos) override;
507
508
516 void draw(BackendRenderTarget& target, RenderStates states) const override;
517
518
520 protected:
521
531 TGUI_NODISCARD Signal& getSignal(String signalName) override;
532
533
540 void rendererChanged(const String& property) override;
541
542
546 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
547
548
552 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
553
554
558 void updateTextSize() override;
559
560
562 // Makes a copy of the widget
564 TGUI_NODISCARD Widget::Ptr clone() const override;
565
566
571 void openMenu(std::size_t menuIndex);
572
573
576 void createMenu(std::vector<Menu>& menus, const String& text);
577
581 TGUI_NODISCARD Menu* findMenu(const std::vector<String>& hierarchy, unsigned int parentIndex, std::vector<Menu>& menus, bool createParents);
582
586 TGUI_NODISCARD const Menu* findMenu(const std::vector<String>& hierarchy, unsigned int parentIndex, const std::vector<Menu>& menus) const;
587
590 TGUI_NODISCARD Menu* findMenuItem(const std::vector<String>& hierarchy);
591
594 TGUI_NODISCARD const Menu* findMenuItem(const std::vector<String>& hierarchy) const;
595
598 void loadMenus(const std::unique_ptr<DataIO::Node>& node, std::vector<Menu>& menus);
599
602 void closeSubMenus(std::vector<Menu>& menus, int& selectedMenu);
603
605 void deselectBottomItem();
606
608 void updateMenuTextColor(Menu& menu, bool selected);
609
611 void updateTextColors(std::vector<Menu>& menus, int selectedMenu);
612
614 void updateTextOpacity(std::vector<Menu>& menus);
615
617 void updateTextFont(std::vector<Menu>& menus);
618
621 TGUI_NODISCARD float calculateMenuWidth(const Menu& menu) const;
622
625 TGUI_NODISCARD float getMenuItemHeight(const Menu& menuItem) const;
626
629 TGUI_NODISCARD float calculateOpenMenuHeight(const std::vector<Menu>& menuItems) const;
630
632 TGUI_NODISCARD Vector2f calculateSubmenuOffset(const Menu& menu, float globalLeftPos, float menuWidth, float subMenuWidth, bool& openSubMenuToRight) const;
633
635 TGUI_NODISCARD bool isMouseOnTopOfMenu(Vector2f menuPos, Vector2f mousePos, bool openSubMenuToRight, const Menu& menu, float menuWidth) const;
636
638 TGUI_NODISCARD bool findMenuItemBelowMouse(Vector2f menuPos, Vector2f mousePos, bool openSubMenuToRight, Menu& menu, float menuWidth, Menu** resultMenu, std::size_t* resultSelectedMenuItem);
639
642 void drawMenusOnBar(BackendRenderTarget& target, RenderStates states) const;
643
646 void drawMenu(BackendRenderTarget& target, RenderStates states, const Menu& menu, float menuWidth, float globalLeftPos, bool openSubMenuToRight) const;
647
648
655 TGUI_NODISCARD bool isMouseOnOpenMenu(Vector2f pos) const;
656
661 void leftMouseReleasedOnMenu();
662
668 void mouseMovedOnMenu(Vector2f pos);
669
676 void drawOpenMenu(BackendRenderTarget& target, RenderStates states) const;
677
678
680 public:
681
686 SignalItemHierarchy onMenuItemClick = {"MenuItemClicked"};
687
688
690 protected:
691
692 std::vector<Menu> m_menus;
693 std::shared_ptr<MenuBarMenuPlaceholder> m_menuWidgetPlaceholder;
694
695 int m_visibleMenu = -1;
696
697 float m_minimumSubMenuWidth = 125;
698
699 bool m_invertedMenuDirection = false;
700
701 Sprite m_spriteBackground;
702 Sprite m_spriteItemBackground;
703 Sprite m_spriteSelectedItemBackground;
704
705 // Cached renderer properties
706 Color m_backgroundColorCached;
707 Color m_selectedBackgroundColorCached;
708 Color m_textColorCached;
709 Color m_selectedTextColorCached;
710 Color m_textColorDisabledCached;
711 Color m_separatorColorCached = Color::Black;
712 float m_separatorThicknessCached = 1;
713 float m_separatorVerticalPaddingCached = 0;
714 float m_separatorSidePaddingCached = 0;
715 float m_distanceToSideCached = 0;
716
718
719 friend class MenuBarMenuPlaceholder;
720 };
721
722
728 {
729 public:
730
731 // Instances of this class can't be copied
733 MenuBarMenuPlaceholder& operator=(const MenuBarMenuPlaceholder&) = delete;
734
735
741
742
750 TGUI_NODISCARD Vector2f getFullSize() const override;
751
752
760 TGUI_NODISCARD Vector2f getWidgetOffset() const override;
761
762
767 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
768
769
776 void draw(BackendRenderTarget& target, RenderStates states) const override;
777
778
782 void leftMouseButtonNoLongerDown() override;
783
787 void mouseMoved(Vector2f pos) override;
788
789
791 // Makes a copy of the widget
793 TGUI_NODISCARD Widget::Ptr clone() const override;
794
795
797 private:
798 MenuBar* m_menuBar;
799 bool m_mouseWasOnMenuBar = true; // When a menu opens then the mouse will be on top of the menu bar
800 };
801
803}
804
805
807
808#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:728
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.
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