TGUI  0.8.9
MenuBar.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2020 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/Text.hpp>
33
35
36namespace tgui
37{
41 class TGUI_API MenuBar : public Widget
42 {
43 public:
44
45 typedef std::shared_ptr<MenuBar> Ptr;
46 typedef std::shared_ptr<const MenuBar> ConstPtr;
47
48 #ifndef TGUI_REMOVE_DEPRECATED_CODE
52 {
53 sf::String text;
54 bool enabled;
55 std::vector<std::unique_ptr<GetAllMenusElement>> menuItems;
56 };
57 #endif
58
61 {
62 sf::String text;
63 bool enabled;
64 std::vector<GetMenuListElement> menuItems;
65 };
66
68 struct Menu
69 {
70 Text text;
71 bool enabled = true;
72 int selectedMenuItem = -1;
73 std::vector<Menu> menuItems;
74 };
75
76
78 // Default constructor
80 MenuBar();
81
82
90
91
101
102
108 const MenuBarRenderer* getSharedRenderer() const;
109
116 const MenuBarRenderer* getRenderer() const;
117
118
132 template <typename Func, typename... Args>
133 unsigned int connectMenuItem(const sf::String& menu, const sf::String& menuItem, Func&& handler, const Args&... args)
134 {
135 return connectMenuItem({menu, menuItem}, std::forward<Func>(handler), args...);
136 }
137
138
152 template <typename Func, typename... Args>
153 unsigned int connectMenuItem(const std::vector<sf::String>& hierarchy, Func&& handler, const Args&... args)
154 {
155#if defined(__cpp_lib_invoke) && (__cpp_lib_invoke >= 201411L)
156 return connect("MenuItemClicked",
157 [=](const std::vector<sf::String>& clickedMenuItem)
158 {
159 if (clickedMenuItem == hierarchy)
160 std::invoke(handler, args...);
161 }
162 );
163#else
164 return connect("MenuItemClicked",
165 [f=std::function<void(const Args&...)>(handler),args...,hierarchy](const std::vector<sf::String>& clickedMenuItem)
166 {
167 if (clickedMenuItem == hierarchy)
168 f(args...);
169 }
170 );
171#endif
172 }
173
174
183 void setSize(const Layout2d& size) override;
184 using Widget::setSize;
185
186
194 void setEnabled(bool enabled) override;
195
196
203 void addMenu(const sf::String& text);
204
205
221 bool addMenuItem(const sf::String& text);
222
223
240 bool addMenuItem(const sf::String& menu, const sf::String& text);
241
242
257 bool addMenuItem(const std::vector<sf::String>& hierarchy, bool createParents = true);
258
259
264
265
276 bool removeMenu(const sf::String& menu);
277
278
288 bool removeMenuItem(const sf::String& menu, const sf::String& menuItem);
289
290
304 bool removeMenuItem(const std::vector<sf::String>& hierarchy, bool removeParentsWhenEmpty = true);
305
306
315 bool removeMenuItems(const sf::String& menu);
316
317
329 bool removeSubMenuItems(const std::vector<sf::String>& hierarchy);
330
331
338 bool setMenuEnabled(const sf::String& menu, bool enabled);
339
340
346 bool getMenuEnabled(const sf::String& menu) const;
347
348
356 bool setMenuItemEnabled(const sf::String& menu, const sf::String& menuItem, bool enabled);
357
358
365 bool setMenuItemEnabled(const std::vector<sf::String>& hierarchy, bool enabled);
366
367
374 bool getMenuItemEnabled(const sf::String& menu, const sf::String& menuItem) const;
375
376
382 bool getMenuItemEnabled(const std::vector<sf::String>& hierarchy) const;
383
384
389 void setTextSize(unsigned int size) override;
390
391
401 void setMinimumSubMenuWidth(float minimumWidth);
402
403
413
414
421 void setInvertedMenuDirection(bool invertDirection);
422
423
431
432
433 #ifndef TGUI_REMOVE_DEPRECATED_CODE
439 TGUI_DEPRECATED("This function doesn't work with submenus, use getMenuList instead")
440 std::vector<std::pair<sf::String, std::vector<sf::String>>> getMenus() const;
441
442
448 TGUI_DEPRECATED("This function is deprecated, use getMenuList instead")
449 std::vector<std::unique_ptr<GetAllMenusElement>> getAllMenus() const;
450 #endif
451
457 std::vector<GetMenuListElement> getMenuList() const;
458
459
464 void closeMenu();
465
466
473 bool mouseOnWidget(Vector2f pos) const override;
474
478 void leftMousePressed(Vector2f pos) override;
479
483 void leftMouseReleased(Vector2f pos) override;
484
488 void mouseMoved(Vector2f pos) override;
489
493 void leftMouseButtonNoLongerDown() override;
494
495
497 private:
498
500 // This function is called when the mouse leaves the widget. If requested, a callback will be send.
502 void mouseLeftWidget() override;
503
504
512 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
513
514
516 protected:
517
527 Signal& getSignal(std::string signalName) override;
528
529
536 void rendererChanged(const std::string& property) override;
537
538
542 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
543
544
548 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
549
550
552 // Makes a copy of the widget
554 Widget::Ptr clone() const override
555 {
556 return std::make_shared<MenuBar>(*this);
557 }
558
561 void createMenu(std::vector<Menu>& menus, const sf::String& text);
562
566 Menu* findMenu(const std::vector<sf::String>& hierarchy, unsigned int parentIndex, std::vector<Menu>& menus, bool createParents);
567
571 const Menu* findMenu(const std::vector<sf::String>& hierarchy, unsigned int parentIndex, const std::vector<Menu>& menus) const;
572
575 const Menu* findMenuItem(const std::vector<sf::String>& hierarchy) const;
576
579 void loadMenus(const std::unique_ptr<DataIO::Node>& node, std::vector<Menu>& menus);
580
583 void closeSubMenus(std::vector<Menu>& menus, int& selectedMenu);
584
586 void deselectBottomItem();
587
589 void updateMenuTextColor(Menu& menu, bool selected);
590
592 void updateTextColors(std::vector<Menu>& menus, int selectedMenu);
593
595 void updateTextOpacity(std::vector<Menu>& menus);
596
598 void updateTextFont(std::vector<Menu>& menus);
599
602 float calculateMenuWidth(const Menu& menu) const;
603
605 Vector2f calculateSubmenuOffset(const Menu& menu, float globalLeftPos, float menuWidth, float subMenuWidth, bool& openSubMenuToRight) const;
606
608 bool isMouseOnTopOfMenu(Vector2f menuPos, Vector2f mousePos, bool openSubMenuToRight, const Menu& menu, float menuWidth) const;
609
611 bool findMenuItemBelowMouse(Vector2f menuPos, Vector2f mousePos, bool openSubMenuToRight, Menu& menu, float menuWidth, Menu** resultMenu, int* resultSelectedMenuItem);
612
615 void drawMenusOnBar(sf::RenderTarget& target, sf::RenderStates states, Sprite& backgroundSprite) const;
616
619 void drawMenu(sf::RenderTarget& target, sf::RenderStates states, const Menu& menu, float menuWidth, Sprite& backgroundSprite, float globalLeftPos, bool openSubMenuToRight) const;
620
621
623 public:
624
629 SignalItemHierarchy onMenuItemClick = {"MenuItemClicked"};
630
631
633 protected:
634
635 std::vector<Menu> m_menus;
636
637 int m_visibleMenu = -1;
638
639 float m_minimumSubMenuWidth = 125;
640
641 bool m_invertedMenuDirection = false;
642
643 Sprite m_spriteBackground;
644 Sprite m_spriteItemBackground;
645 Sprite m_spriteSelectedItemBackground;
646
647 // Cached renderer properties
648 Color m_backgroundColorCached;
649 Color m_selectedBackgroundColorCached;
650 Color m_textColorCached;
651 Color m_selectedTextColorCached;
652 Color m_textColorDisabledCached;
653 float m_distanceToSideCached = 0;
654
656 };
657
659}
660
661
663
664#endif // TGUI_MENU_BAR_HPP
Wrapper for colors.
Definition: Color.hpp:49
Class to store the position or size of a widget.
Definition: Layout.hpp:260
Renderer for the MenuBar widget.
Definition: MenuBarRenderer.hpp:40
Menu bar widget.
Definition: MenuBar.hpp:42
void addMenu(const sf::String &text)
Adds a new menu.
bool addMenuItem(const std::vector< sf::String > &hierarchy, bool createParents=true)
Adds a new menu item (or sub menu item)
unsigned int connectMenuItem(const sf::String &menu, const sf::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:133
bool removeMenuItems(const sf::String &menu)
Removes all menu items from a menu.
void setEnabled(bool enabled) override
Enables or disables the widget.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: MenuBar.hpp:554
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.
Signal & getSignal(std::string signalName) override
Retrieves a signal based on its name.
void closeMenu()
Closes the open menu when one of the menus is open.
bool removeSubMenuItems(const std::vector< sf::String > &hierarchy)
Removes a all menu items below a (sub) menu.
bool getMenuItemEnabled(const std::vector< sf::String > &hierarchy) const
Check if a menu item is enabled or disabled.
MenuBarRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setTextSize(unsigned int size) override
Changes the character size of the text.
bool getMenuItemEnabled(const sf::String &menu, const sf::String &menuItem) const
Check if a menu item is enabled or disabled.
std::shared_ptr< const MenuBar > ConstPtr
Shared constant widget pointer.
Definition: MenuBar.hpp:46
void rendererChanged(const std::string &property) override
Function called when one of the properties of the renderer is changed.
bool getInvertedMenuDirection() const
Returns whether the menus open above or below the menu bar.
bool mouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
bool addMenuItem(const sf::String &menu, const sf::String &text)
Adds a new menu item to an existing menu.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
bool setMenuEnabled(const sf::String &menu, bool enabled)
Enable or disable an entire menu.
bool removeMenuItem(const sf::String &menu, const sf::String &menuItem)
Removes a menu item.
void removeAllMenus()
Removes all menus.
bool removeMenu(const sf::String &menu)
Removes a menu.
static MenuBar::Ptr copy(MenuBar::ConstPtr menuBar)
Makes a copy of another menu bar.
std::shared_ptr< MenuBar > Ptr
Shared widget pointer.
Definition: MenuBar.hpp:45
bool getMenuEnabled(const sf::String &menu) const
Check if an entire menu is enabled or disabled.
bool setMenuItemEnabled(const std::vector< sf::String > &hierarchy, bool enabled)
Enable or disable a menu item.
void setMinimumSubMenuWidth(float minimumWidth)
Changes the minimum width of the submenus.
unsigned int connectMenuItem(const std::vector< sf::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:153
static MenuBar::Ptr create()
Creates a new menu bar widget.
bool setMenuItemEnabled(const sf::String &menu, const sf::String &menuItem, bool enabled)
Enable or disable a menu item.
bool addMenuItem(const sf::String &text)
Adds a new menu item to the last added menu.
void setInvertedMenuDirection(bool invertDirection)
Changes whether the menus open above or below the menu bar.
bool removeMenuItem(const std::vector< sf::String > &hierarchy, bool removeParentsWhenEmpty=true)
Removes a menu item (or sub menu item)
void setSize(const Layout2d &size) override
Changes the size of the menu bar.
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:739
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:231
Definition: Sprite.hpp:46
Wrapper class to store strings.
Definition: String.hpp:119
Definition: Text.hpp:43
Definition: Vector2f.hpp:39
The parent class for every widget.
Definition: Widget.hpp:74
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37
Used for return value of getAllMenus.
Definition: MenuBar.hpp:52
Used for return value of getMenuList.
Definition: MenuBar.hpp:61
Definition: MenuBar.hpp:69