TGUI  0.8.5
MenuBar.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2019 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 
48  #ifndef TGUI_REMOVE_DEPRECATED_CODE
49  struct GetAllMenusElement
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 
89  static MenuBar::Ptr create();
90 
91 
100  static MenuBar::Ptr copy(MenuBar::ConstPtr menuBar);
101 
102 
107  MenuBarRenderer* getSharedRenderer();
108  const MenuBarRenderer* getSharedRenderer() const;
109 
115  MenuBarRenderer* getRenderer();
116  const MenuBarRenderer* getRenderer() const;
117 
118 
130  template <typename Func, typename... Args>
131  unsigned int connectMenuItem(const sf::String& menu, const sf::String& menuItem, Func&& handler, const Args&... args)
132  {
133  return connectMenuItem({menu, menuItem}, std::forward<Func>(handler), args...);
134  }
135 
136 
148  template <typename Func, typename... Args>
149  unsigned int connectMenuItem(const std::vector<sf::String>& hierarchy, Func&& handler, const Args&... args)
150  {
151  #ifdef TGUI_USE_CPP17
152  return connect("MenuItemClicked",
153  [=](const std::vector<sf::String>& clickedMenuItem)
154  {
155  if (clickedMenuItem == hierarchy)
156  std::invoke(handler, args...);
157  }
158  );
159  #else
160  return connect("MenuItemClicked",
161  [f=std::function<void(const Args&...)>(handler),args...,hierarchy](const std::vector<sf::String>& clickedMenuItem)
162  {
163  if (clickedMenuItem == hierarchy)
164  f(args...);
165  }
166  );
167  #endif
168  }
169 
170 
179  void setSize(const Layout2d& size) override;
180  using Widget::setSize;
181 
182 
190  void setEnabled(bool enabled) override;
191 
192 
199  void addMenu(const sf::String& text);
200 
201 
217  bool addMenuItem(const sf::String& text);
218 
219 
236  bool addMenuItem(const sf::String& menu, const sf::String& text);
237 
238 
253  bool addMenuItem(const std::vector<sf::String>& hierarchy, bool createParents = true);
254 
255 
259  void removeAllMenus();
260 
261 
272  bool removeMenu(const sf::String& menu);
273 
274 
284  bool removeMenuItem(const sf::String& menu, const sf::String& menuItem);
285 
286 
300  bool removeMenuItem(const std::vector<sf::String>& hierarchy, bool removeParentsWhenEmpty = true);
301 
302 
311  bool removeMenuItems(const sf::String& menu);
312 
313 
325  bool removeSubMenuItems(const std::vector<sf::String>& hierarchy);
326 
327 
334  bool setMenuEnabled(const sf::String& menu, bool enabled);
335 
336 
342  bool getMenuEnabled(const sf::String& menu) const;
343 
344 
352  bool setMenuItemEnabled(const sf::String& menu, const sf::String& menuItem, bool enabled);
353 
354 
361  bool setMenuItemEnabled(const std::vector<sf::String>& hierarchy, bool enabled);
362 
363 
370  bool getMenuItemEnabled(const sf::String& menu, const sf::String& menuItem) const;
371 
372 
378  bool getMenuItemEnabled(const std::vector<sf::String>& hierarchy) const;
379 
380 
385  void setTextSize(unsigned int size);
386 
387 
394  unsigned int getTextSize() const;
395 
396 
406  void setMinimumSubMenuWidth(float minimumWidth);
407 
408 
417  float getMinimumSubMenuWidth() const;
418 
419 
426  void setInvertedMenuDirection(bool invertDirection);
427 
428 
435  bool getInvertedMenuDirection() const;
436 
437 
438  #ifndef TGUI_REMOVE_DEPRECATED_CODE
439  TGUI_DEPRECATED("This function doesn't work with submenus, use getMenuList instead")
445  std::vector<std::pair<sf::String, std::vector<sf::String>>> getMenus() const;
446 
447 
453  TGUI_DEPRECATED("This function is deprecated, use getMenuList instead")
454  std::vector<std::unique_ptr<GetAllMenusElement>> getAllMenus() const;
455  #endif
456 
462  std::vector<GetMenuListElement> getMenuList() const;
463 
464 
469  void closeMenu();
470 
471 
478  bool mouseOnWidget(Vector2f pos) const override;
479 
483  void leftMousePressed(Vector2f pos) override;
484 
488  void leftMouseReleased(Vector2f pos) override;
489 
493  void mouseMoved(Vector2f pos) override;
494 
498  void mouseNoLongerDown() override;
499 
500 
502  private:
503 
505  // This function is called when the mouse leaves the widget. If requested, a callback will be send.
507  void mouseLeftWidget() override;
508 
509 
517  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
518 
519 
521  protected:
522 
532  Signal& getSignal(std::string signalName) override;
533 
534 
541  void rendererChanged(const std::string& property) override;
542 
543 
547  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
548 
549 
553  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
554 
555 
557  // Makes a copy of the widget
559  Widget::Ptr clone() const override
560  {
561  return std::make_shared<MenuBar>(*this);
562  }
563 
566  void createMenu(std::vector<Menu>& menus, const sf::String& text);
567 
571  Menu* findMenu(const std::vector<sf::String>& hierarchy, unsigned int parentIndex, std::vector<Menu>& menus, bool createParents);
572 
576  const Menu* findMenu(const std::vector<sf::String>& hierarchy, unsigned int parentIndex, const std::vector<Menu>& menus) const;
577 
580  const Menu* findMenuItem(const std::vector<sf::String>& hierarchy) const;
581 
584  void loadMenus(const std::unique_ptr<DataIO::Node>& node, std::vector<Menu>& menus);
585 
588  void closeSubMenus(std::vector<Menu>& menus, int& selectedMenu);
589 
591  void deselectBottomItem();
592 
594  void updateMenuTextColor(Menu& menu, bool selected);
595 
597  void updateTextColors(std::vector<Menu>& menus, int selectedMenu);
598 
600  void updateTextOpacity(std::vector<Menu>& menus);
601 
603  void updateTextFont(std::vector<Menu>& menus);
604 
607  float calculateMenuWidth(const Menu& menu) const;
608 
610  Vector2f calculateSubmenuOffset(const Menu& menu, float globalLeftPos, float menuWidth, float subMenuWidth, bool& openSubMenuToRight) const;
611 
613  bool isMouseOnTopOfMenu(Vector2f menuPos, Vector2f mousePos, bool openSubMenuToRight, const Menu& menu, float menuWidth) const;
614 
616  bool findMenuItemBelowMouse(Vector2f menuPos, Vector2f mousePos, bool openSubMenuToRight, Menu& menu, float menuWidth, Menu** resultMenu, int* resultSelectedMenuItem);
617 
620  void drawMenusOnBar(sf::RenderTarget& target, sf::RenderStates states, Sprite& backgroundSprite) const;
621 
624  void drawMenu(sf::RenderTarget& target, sf::RenderStates states, const Menu& menu, float menuWidth, Sprite& backgroundSprite, float globalLeftPos, bool openSubMenuToRight) const;
625 
626 
628  public:
629 
634  SignalItemHierarchy onMenuItemClick = {"MenuItemClicked"};
635 
636 
638  protected:
639 
640  std::vector<Menu> m_menus;
641 
642  int m_visibleMenu = -1;
643 
644  unsigned int m_textSize = 0;
645 
646  float m_minimumSubMenuWidth = 125;
647 
648  bool m_invertedMenuDirection = false;
649 
650  Sprite m_spriteBackground;
651  Sprite m_spriteItemBackground;
652  Sprite m_spriteSelectedItemBackground;
653 
654  // Cached renderer properties
655  Color m_backgroundColorCached;
656  Color m_selectedBackgroundColorCached;
657  Color m_textColorCached;
658  Color m_selectedTextColorCached;
659  Color m_textColorDisabledCached;
660  float m_distanceToSideCached = 0;
661 
663  };
664 
666 }
667 
668 
670 
671 #endif // TGUI_MENU_BAR_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Sprite.hpp:45
Renderer for the MenuBar widget.
Definition: MenuBarRenderer.hpp:39
Menu bar widget.
Definition: MenuBar.hpp:41
Wrapper for colors.
Definition: Color.hpp:48
Class to store the position or size of a widget.
Definition: Layout.hpp:258
The parent class for every widget.
Definition: Widget.hpp:72
Definition: Vector2f.hpp:37
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:609
Definition: Text.hpp:42
std::shared_ptr< MenuBar > Ptr
Shared widget pointer.
Definition: MenuBar.hpp:45
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
Used for return value of getMenuList.
Definition: MenuBar.hpp:60
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:59
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:149
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: MenuBar.hpp:559
Definition: MenuBar.hpp:68
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:131
Used for return value of getAllMenus.
Definition: MenuBar.hpp:51
std::shared_ptr< const MenuBar > ConstPtr
Shared constant widget pointer.
Definition: MenuBar.hpp:46