TGUI  1.0-alpha
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) noexcept;
89
91 // Copy assignment operator
93 MenuBar& operator=(const MenuBar& other);
94
96 // Move assignment operator
98 MenuBar& operator=(MenuBar&& other) noexcept;
99
100
108
109
119
120
126 const MenuBarRenderer* getSharedRenderer() const;
127
134
135
149 template <typename Func, typename... Args>
150 unsigned int connectMenuItem(const String& menu, const String& menuItem, Func&& handler, const Args&... args)
151 {
152 return connectMenuItem({menu, menuItem}, std::forward<Func>(handler), args...);
153 }
154
155
169 template <typename Func, typename... Args>
170 unsigned int connectMenuItem(const std::vector<String>& hierarchy, Func&& handler, const Args&... args)
171 {
172#if defined(__cpp_lib_invoke) && (__cpp_lib_invoke >= 201411L)
173 return onMenuItemClick.connect(
174 [=](const std::vector<String>& clickedMenuItem)
175 {
176 if (clickedMenuItem == hierarchy)
177 std::invoke(handler, args...);
178 }
179 );
180#else
181 return onMenuItemClick.connect(
182 [f=std::function<void(const Args&...)>(handler),args...,hierarchy](const std::vector<String>& clickedMenuItem)
183 {
184 if (clickedMenuItem == hierarchy)
185 f(args...);
186 }
187 );
188#endif
189 }
190
191
200 void setSize(const Layout2d& size) override;
201 using Widget::setSize;
202
203
211 void setEnabled(bool enabled) override;
212
213
220 void addMenu(const String& text);
221
222
240 bool addMenuItem(const String& text);
241
242
261 bool addMenuItem(const String& menu, const String& text);
262
263
280 bool addMenuItem(const std::vector<String>& hierarchy, bool createParents = true);
281
282
299 bool changeMenuItem(const std::vector<String>& hierarchy, const String& text);
300
301
306
307
318 bool removeMenu(const String& menu);
319
320
330 bool removeMenuItem(const String& menu, const String& menuItem);
331
332
346 bool removeMenuItem(const std::vector<String>& hierarchy, bool removeParentsWhenEmpty = true);
347
348
357 bool removeMenuItems(const String& menu);
358
359
371 bool removeSubMenuItems(const std::vector<String>& hierarchy);
372
373
380 bool setMenuEnabled(const String& menu, bool enabled);
381
382
388 bool getMenuEnabled(const String& menu) const;
389
390
398 bool setMenuItemEnabled(const String& menu, const String& menuItem, bool enabled);
399
400
407 bool setMenuItemEnabled(const std::vector<String>& hierarchy, bool enabled);
408
409
416 bool getMenuItemEnabled(const String& menu, const String& menuItem) const;
417
418
424 bool getMenuItemEnabled(const std::vector<String>& hierarchy) const;
425
426
436 void setMinimumSubMenuWidth(float minimumWidth);
437
438
448
449
456 void setInvertedMenuDirection(bool invertDirection);
457
458
466
467
473 std::vector<GetMenusElement> getMenus() const;
474
475
479 void closeMenu();
480
481
488 bool isMouseOnWidget(Vector2f pos) const override;
489
493 void leftMousePressed(Vector2f pos) override;
494
498 void leftMouseReleased(Vector2f pos) override;
499
503 void mouseMoved(Vector2f pos) override;
504
505
513 void draw(BackendRenderTarget& target, RenderStates states) const override;
514
515
517 protected:
518
528 Signal& getSignal(String signalName) override;
529
530
537 void rendererChanged(const String& property) override;
538
539
543 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
544
545
549 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
550
551
555 void updateTextSize() override;
556
557
559 // Makes a copy of the widget
561 Widget::Ptr clone() const override;
562
563
568 void openMenu(std::size_t menuIndex);
569
570
573 void createMenu(std::vector<Menu>& menus, const String& text);
574
578 Menu* findMenu(const std::vector<String>& hierarchy, unsigned int parentIndex, std::vector<Menu>& menus, bool createParents);
579
583 const Menu* findMenu(const std::vector<String>& hierarchy, unsigned int parentIndex, const std::vector<Menu>& menus) const;
584
587 Menu* findMenuItem(const std::vector<String>& hierarchy);
588
591 const Menu* findMenuItem(const std::vector<String>& hierarchy) const;
592
595 void loadMenus(const std::unique_ptr<DataIO::Node>& node, std::vector<Menu>& menus);
596
599 void closeSubMenus(std::vector<Menu>& menus, int& selectedMenu);
600
602 void deselectBottomItem();
603
605 void updateMenuTextColor(Menu& menu, bool selected);
606
608 void updateTextColors(std::vector<Menu>& menus, int selectedMenu);
609
611 void updateTextOpacity(std::vector<Menu>& menus);
612
614 void updateTextFont(std::vector<Menu>& menus);
615
618 float calculateMenuWidth(const Menu& menu) const;
619
622 float getMenuItemHeight(const Menu& menuItem) const;
623
626 float calculateOpenMenuHeight(const std::vector<Menu>& menuItems) const;
627
629 Vector2f calculateSubmenuOffset(const Menu& menu, float globalLeftPos, float menuWidth, float subMenuWidth, bool& openSubMenuToRight) const;
630
632 bool isMouseOnTopOfMenu(Vector2f menuPos, Vector2f mousePos, bool openSubMenuToRight, const Menu& menu, float menuWidth) const;
633
635 bool findMenuItemBelowMouse(Vector2f menuPos, Vector2f mousePos, bool openSubMenuToRight, Menu& menu, float menuWidth, Menu** resultMenu, int* resultSelectedMenuItem);
636
639 void drawMenusOnBar(BackendRenderTarget& target, RenderStates states) const;
640
643 void drawMenu(BackendRenderTarget& target, RenderStates states, const Menu& menu, float menuWidth, float globalLeftPos, bool openSubMenuToRight) const;
644
645
652 bool isMouseOnOpenMenu(Vector2f pos) const;
653
658 void leftMouseReleasedOnMenu();
659
665 void mouseMovedOnMenu(Vector2f pos);
666
673 void drawOpenMenu(BackendRenderTarget& target, RenderStates states) const;
674
675
677 public:
678
683 SignalItemHierarchy onMenuItemClick = {"MenuItemClicked"};
684
685
687 protected:
688
689 std::vector<Menu> m_menus;
690 std::shared_ptr<MenuBarMenuPlaceholder> m_menuWidgetPlaceholder;
691
692 int m_visibleMenu = -1;
693
694 float m_minimumSubMenuWidth = 125;
695
696 bool m_invertedMenuDirection = false;
697
698 Sprite m_spriteBackground;
699 Sprite m_spriteItemBackground;
700 Sprite m_spriteSelectedItemBackground;
701
702 // Cached renderer properties
703 Color m_backgroundColorCached;
704 Color m_selectedBackgroundColorCached;
705 Color m_textColorCached;
706 Color m_selectedTextColorCached;
707 Color m_textColorDisabledCached;
708 Color m_separatorColorCached = Color::Black;
709 float m_separatorThicknessCached = 1;
710 float m_separatorVerticalPaddingCached = 0;
711 float m_separatorSidePaddingCached = 0;
712 float m_distanceToSideCached = 0;
713
715
716 friend class MenuBarMenuPlaceholder;
717 };
718
719
725 {
726 public:
727
728 // Instances of this class can't be copied
730 MenuBarMenuPlaceholder& operator=(const MenuBarMenuPlaceholder&) = delete;
731
732
738
739
747 Vector2f getFullSize() const override;
748
749
757 Vector2f getWidgetOffset() const override;
758
759
764 bool isMouseOnWidget(Vector2f pos) const override;
765
766
773 void draw(BackendRenderTarget& target, RenderStates states) const override;
774
775
779 void leftMouseButtonNoLongerDown() override;
780
784 void mouseMoved(Vector2f pos) override;
785
786
788 // Makes a copy of the widget
790 Widget::Ptr clone() const override;
791
792
794 private:
795 MenuBar* m_menuBar;
796 bool m_mouseWasOnMenuBar = true; // When a menu opens then the mouse will be on top of the menu bar
797 };
798
800}
801
802
804
805#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:259
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:725
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:170
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.
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:150
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