TGUI  0.9-dev
MenuBar.hpp
1 //
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 
36 namespace 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 
50  {
51  String text;
52  bool enabled;
53  std::vector<GetMenusElement> menuItems;
54  };
55 
57  struct Menu
58  {
59  Text text;
60  bool enabled = true;
61  int selectedMenuItem = -1;
62  std::vector<Menu> menuItems;
63  };
64 
65 
67  // Default constructor
69  MenuBar();
70 
71 
78  static MenuBar::Ptr create();
79 
80 
89  static MenuBar::Ptr copy(MenuBar::ConstPtr menuBar);
90 
91 
96  MenuBarRenderer* getSharedRenderer();
97  const MenuBarRenderer* getSharedRenderer() const;
98 
104  MenuBarRenderer* getRenderer();
105  const MenuBarRenderer* getRenderer() const;
106 
107 
121  template <typename Func, typename... Args>
122  unsigned int connectMenuItem(const String& menu, const String& menuItem, Func&& handler, const Args&... args)
123  {
124  return connectMenuItem({menu, menuItem}, std::forward<Func>(handler), args...);
125  }
126 
127 
141  template <typename Func, typename... Args>
142  unsigned int connectMenuItem(const std::vector<String>& hierarchy, Func&& handler, const Args&... args)
143  {
144 #if defined(__cpp_lib_invoke) && (__cpp_lib_invoke >= 201411L)
145  return onMenuItemClick.connect(
146  [=](const std::vector<String>& clickedMenuItem)
147  {
148  if (clickedMenuItem == hierarchy)
149  std::invoke(handler, args...);
150  }
151  );
152 #else
153  return onMenuItemClick.connect(
154  [f=std::function<void(const Args&...)>(handler),args...,hierarchy](const std::vector<String>& clickedMenuItem)
155  {
156  if (clickedMenuItem == hierarchy)
157  f(args...);
158  }
159  );
160 #endif
161  }
162 
163 
172  void setSize(const Layout2d& size) override;
173  using Widget::setSize;
174 
175 
183  void setEnabled(bool enabled) override;
184 
185 
192  void addMenu(const String& text);
193 
194 
210  bool addMenuItem(const String& text);
211 
212 
229  bool addMenuItem(const String& menu, const String& text);
230 
231 
246  bool addMenuItem(const std::vector<String>& hierarchy, bool createParents = true);
247 
248 
252  void removeAllMenus();
253 
254 
265  bool removeMenu(const String& menu);
266 
267 
277  bool removeMenuItem(const String& menu, const String& menuItem);
278 
279 
293  bool removeMenuItem(const std::vector<String>& hierarchy, bool removeParentsWhenEmpty = true);
294 
295 
304  bool removeMenuItems(const String& menu);
305 
306 
318  bool removeSubMenuItems(const std::vector<String>& hierarchy);
319 
320 
327  bool setMenuEnabled(const String& menu, bool enabled);
328 
329 
335  bool getMenuEnabled(const String& menu) const;
336 
337 
345  bool setMenuItemEnabled(const String& menu, const String& menuItem, bool enabled);
346 
347 
354  bool setMenuItemEnabled(const std::vector<String>& hierarchy, bool enabled);
355 
356 
363  bool getMenuItemEnabled(const String& menu, const String& menuItem) const;
364 
365 
371  bool getMenuItemEnabled(const std::vector<String>& hierarchy) const;
372 
373 
378  void setTextSize(unsigned int size) override;
379 
380 
390  void setMinimumSubMenuWidth(float minimumWidth);
391 
392 
401  float getMinimumSubMenuWidth() const;
402 
403 
410  void setInvertedMenuDirection(bool invertDirection);
411 
412 
419  bool getInvertedMenuDirection() const;
420 
421 
427  std::vector<GetMenusElement> getMenus() const;
428 
429 
434  void closeMenu();
435 
436 
443  bool mouseOnWidget(Vector2f pos) const override;
444 
448  void leftMousePressed(Vector2f pos) override;
449 
453  void leftMouseReleased(Vector2f pos) override;
454 
458  void mouseMoved(Vector2f pos) override;
459 
463  void leftMouseButtonNoLongerDown() override;
464 
465 
467  private:
468 
470  // This function is called when the mouse leaves the widget. If requested, a callback will be send.
472  void mouseLeftWidget() override;
473 
474 
482  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
483 
484 
486  protected:
487 
497  Signal& getSignal(String signalName) override;
498 
499 
506  void rendererChanged(const String& property) override;
507 
508 
512  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
513 
514 
518  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
519 
520 
522  // Makes a copy of the widget
524  Widget::Ptr clone() const override
525  {
526  return std::make_shared<MenuBar>(*this);
527  }
528 
531  void createMenu(std::vector<Menu>& menus, const String& text);
532 
536  Menu* findMenu(const std::vector<String>& hierarchy, unsigned int parentIndex, std::vector<Menu>& menus, bool createParents);
537 
541  const Menu* findMenu(const std::vector<String>& hierarchy, unsigned int parentIndex, const std::vector<Menu>& menus) const;
542 
545  const Menu* findMenuItem(const std::vector<String>& hierarchy) const;
546 
549  void loadMenus(const std::unique_ptr<DataIO::Node>& node, std::vector<Menu>& menus);
550 
553  void closeSubMenus(std::vector<Menu>& menus, int& selectedMenu);
554 
556  void deselectBottomItem();
557 
559  void updateMenuTextColor(Menu& menu, bool selected);
560 
562  void updateTextColors(std::vector<Menu>& menus, int selectedMenu);
563 
565  void updateTextOpacity(std::vector<Menu>& menus);
566 
568  void updateTextFont(std::vector<Menu>& menus);
569 
572  float calculateMenuWidth(const Menu& menu) const;
573 
575  Vector2f calculateSubmenuOffset(const Menu& menu, float globalLeftPos, float menuWidth, float subMenuWidth, bool& openSubMenuToRight) const;
576 
578  bool isMouseOnTopOfMenu(Vector2f menuPos, Vector2f mousePos, bool openSubMenuToRight, const Menu& menu, float menuWidth) const;
579 
581  bool findMenuItemBelowMouse(Vector2f menuPos, Vector2f mousePos, bool openSubMenuToRight, Menu& menu, float menuWidth, Menu** resultMenu, int* resultSelectedMenuItem);
582 
585  void drawMenusOnBar(sf::RenderTarget& target, sf::RenderStates states, Sprite& backgroundSprite) const;
586 
589  void drawMenu(sf::RenderTarget& target, sf::RenderStates states, const Menu& menu, float menuWidth, Sprite& backgroundSprite, float globalLeftPos, bool openSubMenuToRight) const;
590 
591 
593  public:
594 
599  SignalItemHierarchy onMenuItemClick = {"MenuItemClicked"};
600 
601 
603  protected:
604 
605  std::vector<Menu> m_menus;
606 
607  int m_visibleMenu = -1;
608 
609  float m_minimumSubMenuWidth = 125;
610 
611  bool m_invertedMenuDirection = false;
612 
613  Sprite m_spriteBackground;
614  Sprite m_spriteItemBackground;
615  Sprite m_spriteSelectedItemBackground;
616 
617  // Cached renderer properties
618  Color m_backgroundColorCached;
619  Color m_selectedBackgroundColorCached;
620  Color m_textColorCached;
621  Color m_selectedTextColorCached;
622  Color m_textColorDisabledCached;
623  float m_distanceToSideCached = 0;
624 
626  };
627 
629 }
630 
631 
633 
634 #endif // TGUI_MENU_BAR_HPP
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:216
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:142
Definition: MenuBar.hpp:57
std::shared_ptr< MenuBar > Ptr
Shared widget pointer.
Definition: MenuBar.hpp:45
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:122
std::shared_ptr< const MenuBar > ConstPtr
Shared constant widget pointer.
Definition: MenuBar.hpp:46
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:72
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:35
Definition: Text.hpp:42
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:934
Class to store the position or size of a widget.
Definition: Layout.hpp:258
Wrapper for colors.
Definition: Color.hpp:47
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: MenuBar.hpp:524
Renderer for the MenuBar widget.
Definition: MenuBarRenderer.hpp:39
Definition: Sprite.hpp:45
Wrapper class to store strings.
Definition: String.hpp:70
The parent class for every widget.
Definition: Widget.hpp:68
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Used for return value of getMenus.
Definition: MenuBar.hpp:49
Menu bar widget.
Definition: MenuBar.hpp:41