TGUI  0.10-beta
MenuBar.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2022 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
37namespace tgui
38{
39 class MenuBarMenuPlaceholder;
40
41
45 class TGUI_API MenuBar : public Widget
46 {
47 public:
48
49 typedef std::shared_ptr<MenuBar> Ptr;
50 typedef std::shared_ptr<const MenuBar> ConstPtr;
51
54 {
55 String text;
56 bool enabled;
57 std::vector<GetMenusElement> menuItems;
58 };
59
61 struct Menu
62 {
63 Text text;
64 bool enabled = true;
65 int selectedMenuItem = -1;
66 std::vector<Menu> menuItems;
67 };
68
69
77 MenuBar(const char* typeName = "MenuBar", bool initRenderer = true);
78
79
81 // Copy constructor
83 MenuBar(const MenuBar& other);
84
86 // Move constructor
88 MenuBar(MenuBar&& other);
89
91 // Copy assignment operator
93 MenuBar& operator=(const MenuBar& other);
94
96 // Move assignment operator
98 MenuBar& operator=(MenuBar&& other);
99
100
108
109
119
120
126 const MenuBarRenderer* getSharedRenderer() const;
127
134 const MenuBarRenderer* getRenderer() const;
135
136
150 template <typename Func, typename... Args>
151 unsigned int connectMenuItem(const String& menu, const String& menuItem, Func&& handler, const Args&... args)
152 {
153 return connectMenuItem({menu, menuItem}, std::forward<Func>(handler), args...);
154 }
155
156
170 template <typename Func, typename... Args>
171 unsigned int connectMenuItem(const std::vector<String>& hierarchy, Func&& handler, const Args&... args)
172 {
173#if defined(__cpp_lib_invoke) && (__cpp_lib_invoke >= 201411L)
174 return onMenuItemClick.connect(
175 [=](const std::vector<String>& clickedMenuItem)
176 {
177 if (clickedMenuItem == hierarchy)
178 std::invoke(handler, args...);
179 }
180 );
181#else
182 return onMenuItemClick.connect(
183 [f=std::function<void(const Args&...)>(handler),args...,hierarchy](const std::vector<String>& clickedMenuItem)
184 {
185 if (clickedMenuItem == hierarchy)
186 f(args...);
187 }
188 );
189#endif
190 }
191
192
201 void setSize(const Layout2d& size) override;
202 using Widget::setSize;
203
204
212 void setEnabled(bool enabled) override;
213
214
221 void addMenu(const String& text);
222
223
241 bool addMenuItem(const String& text);
242
243
262 bool addMenuItem(const String& menu, const String& text);
263
264
281 bool addMenuItem(const std::vector<String>& hierarchy, bool createParents = true);
282
283
288
289
300 bool removeMenu(const String& menu);
301
302
312 bool removeMenuItem(const String& menu, const String& menuItem);
313
314
328 bool removeMenuItem(const std::vector<String>& hierarchy, bool removeParentsWhenEmpty = true);
329
330
339 bool removeMenuItems(const String& menu);
340
341
353 bool removeSubMenuItems(const std::vector<String>& hierarchy);
354
355
362 bool setMenuEnabled(const String& menu, bool enabled);
363
364
370 bool getMenuEnabled(const String& menu) const;
371
372
380 bool setMenuItemEnabled(const String& menu, const String& menuItem, bool enabled);
381
382
389 bool setMenuItemEnabled(const std::vector<String>& hierarchy, bool enabled);
390
391
398 bool getMenuItemEnabled(const String& menu, const String& menuItem) const;
399
400
406 bool getMenuItemEnabled(const std::vector<String>& hierarchy) const;
407
408
418 void setMinimumSubMenuWidth(float minimumWidth);
419
420
430
431
438 void setInvertedMenuDirection(bool invertDirection);
439
440
448
449
455 std::vector<GetMenusElement> getMenus() const;
456
457
461 void closeMenu();
462
463
470 bool isMouseOnWidget(Vector2f pos) const override;
471
475 void leftMousePressed(Vector2f pos) override;
476
480 void leftMouseReleased(Vector2f pos) override;
481
485 void mouseMoved(Vector2f pos) override;
486
487
495 void draw(BackendRenderTarget& target, RenderStates states) const override;
496
497
499 protected:
500
510 Signal& getSignal(String signalName) override;
511
512
519 void rendererChanged(const String& property) override;
520
521
525 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
526
527
531 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
532
533
537 void updateTextSize() override;
538
539
541 // Makes a copy of the widget
543 Widget::Ptr clone() const override;
544
545
550 void openMenu(std::size_t menuIndex);
551
552
555 void createMenu(std::vector<Menu>& menus, const String& text);
556
560 Menu* findMenu(const std::vector<String>& hierarchy, unsigned int parentIndex, std::vector<Menu>& menus, bool createParents);
561
565 const Menu* findMenu(const std::vector<String>& hierarchy, unsigned int parentIndex, const std::vector<Menu>& menus) const;
566
569 const Menu* findMenuItem(const std::vector<String>& hierarchy) const;
570
573 void loadMenus(const std::unique_ptr<DataIO::Node>& node, std::vector<Menu>& menus);
574
577 void closeSubMenus(std::vector<Menu>& menus, int& selectedMenu);
578
580 void deselectBottomItem();
581
583 void updateMenuTextColor(Menu& menu, bool selected);
584
586 void updateTextColors(std::vector<Menu>& menus, int selectedMenu);
587
589 void updateTextOpacity(std::vector<Menu>& menus);
590
592 void updateTextFont(std::vector<Menu>& menus);
593
596 float calculateMenuWidth(const Menu& menu) const;
597
600 float getMenuItemHeight(const Menu& menuItem) const;
601
604 float calculateOpenMenuHeight(const std::vector<Menu>& menuItems) const;
605
607 Vector2f calculateSubmenuOffset(const Menu& menu, float globalLeftPos, float menuWidth, float subMenuWidth, bool& openSubMenuToRight) const;
608
610 bool isMouseOnTopOfMenu(Vector2f menuPos, Vector2f mousePos, bool openSubMenuToRight, const Menu& menu, float menuWidth) const;
611
613 bool findMenuItemBelowMouse(Vector2f menuPos, Vector2f mousePos, bool openSubMenuToRight, Menu& menu, float menuWidth, Menu** resultMenu, int* resultSelectedMenuItem);
614
617 void drawMenusOnBar(BackendRenderTarget& target, RenderStates states) const;
618
621 void drawMenu(BackendRenderTarget& target, RenderStates states, const Menu& menu, float menuWidth, float globalLeftPos, bool openSubMenuToRight) const;
622
623
630 bool isMouseOnOpenMenu(Vector2f pos) const;
631
636 void leftMouseReleasedOnMenu();
637
643 void mouseMovedOnMenu(Vector2f pos);
644
651 void drawOpenMenu(BackendRenderTarget& target, RenderStates states) const;
652
653
655 public:
656
661 SignalItemHierarchy onMenuItemClick = {"MenuItemClicked"};
662
663
665 protected:
666
667 std::vector<Menu> m_menus;
668 std::shared_ptr<MenuBarMenuPlaceholder> m_menuWidgetPlaceholder;
669
670 int m_visibleMenu = -1;
671
672 float m_minimumSubMenuWidth = 125;
673
674 bool m_invertedMenuDirection = false;
675
676 Sprite m_spriteBackground;
677 Sprite m_spriteItemBackground;
678 Sprite m_spriteSelectedItemBackground;
679
680 // Cached renderer properties
681 Color m_backgroundColorCached;
682 Color m_selectedBackgroundColorCached;
683 Color m_textColorCached;
684 Color m_selectedTextColorCached;
685 Color m_textColorDisabledCached;
686 Color m_separatorColorCached = Color::Black;
687 float m_separatorThicknessCached = 1;
688 float m_separatorVerticalPaddingCached = 0;
689 float m_separatorSidePaddingCached = 0;
690 float m_distanceToSideCached = 0;
691
693
694 friend class MenuBarMenuPlaceholder;
695 };
696
697
703 {
704 public:
705
706 // Instances of this class can't be copied
708 MenuBarMenuPlaceholder& operator=(const MenuBarMenuPlaceholder&) = delete;
709
710
716
717
725 Vector2f getFullSize() const override;
726
727
735 Vector2f getWidgetOffset() const override;
736
737
742 bool isMouseOnWidget(Vector2f pos) const override;
743
744
751 void draw(BackendRenderTarget& target, RenderStates states) const override;
752
753
757 void leftMouseButtonNoLongerDown() override;
758
762 void mouseMoved(Vector2f pos) override;
763
764
766 // Makes a copy of the widget
768 Widget::Ptr clone() const override;
769
770
772 private:
773 MenuBar* m_menuBar;
774 bool m_mouseWasOnMenuBar = true; // When a menu opens then the mouse will be on top of the menu bar
775 };
776
778}
779
780
782
783#endif // TGUI_MENU_BAR_HPP
Base class for render targets.
Definition: BackendRenderTarget.hpp:48
Wrapper for colors.
Definition: Color.hpp:63
static const Color Black
Black predefined color.
Definition: Color.hpp:253
Class to store the position or size of a widget.
Definition: Layout.hpp:284
Widget that is added to a container when the user clicks on the menu bar. This widget will be added i...
Definition: MenuBar.hpp:703
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:40
Menu bar widget.
Definition: MenuBar.hpp:46
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:171
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.
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.
MenuBarRenderer * getSharedRenderer()
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)
std::shared_ptr< const MenuBar > ConstPtr
Shared constant widget pointer.
Definition: MenuBar.hpp:50
bool getInvertedMenuDirection() const
Returns whether the menus open above or below the menu bar.
void openMenu(std::size_t menuIndex)
Opens a menu.
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.
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.
static MenuBar::Ptr copy(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.
std::shared_ptr< MenuBar > Ptr
Shared widget pointer.
Definition: MenuBar.hpp:49
void setMinimumSubMenuWidth(float minimumWidth)
Changes the minimum width of the submenus.
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:151
void setInvertedMenuDirection(bool invertDirection)
Changes whether the menus open above or below the menu bar.
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.
MenuBarRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:859
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:58
Definition: Sprite.hpp:45
Wrapper class to store strings.
Definition: String.hpp:79
Definition: Text.hpp:48
The parent class for every widget.
Definition: Widget.hpp:70
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Used for return value of getMenus.
Definition: MenuBar.hpp:54
Definition: MenuBar.hpp:62
States used for drawing.
Definition: RenderStates.hpp:39