TGUI  0.8.1
MenuBar.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2018 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  sf::String text;
52  bool enabled;
53  std::vector<std::unique_ptr<GetAllMenusElement>> menuItems;
54  };
55 
57  struct Menu
58  {
59  Text text;
60  bool enabled = true;
61  int selectedMenuItem = -1;
62  std::vector<aurora::CopiedPtr<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 
119  template <typename Func, typename... Args>
120  unsigned int connectMenuItem(const sf::String& menu, const sf::String& menuItem, Func&& handler, const Args&... args)
121  {
122  return connectMenuItem({menu, menuItem}, std::forward<Func>(handler), args...);
123  }
124 
125 
137  template <typename Func, typename... Args>
138  unsigned int connectMenuItem(const std::vector<sf::String>& hierarchy, Func&& handler, const Args&... args)
139  {
140  #ifdef TGUI_USE_CPP17
141  return connect("MenuItemClicked",
142  [=](const std::vector<sf::String>& clickedMenuItem)
143  {
144  if (clickedMenuItem == hierarchy)
145  std::invoke(handler, args...);
146  }
147  );
148  #else
149  return connect("MenuItemClicked",
150  [f=std::function<void(const Args&...)>(handler),args...,hierarchy](const std::vector<sf::String>& clickedMenuItem)
151  {
152  if (clickedMenuItem == hierarchy)
153  f(args...);
154  }
155  );
156  #endif
157  }
158 
159 
168  void setSize(const Layout2d& size) override;
169  using Widget::setSize;
170 
171 
179  void setEnabled(bool enabled) override;
180 
181 
188  void addMenu(const sf::String& text);
189 
190 
206  bool addMenuItem(const sf::String& text);
207 
208 
225  bool addMenuItem(const sf::String& menu, const sf::String& text);
226 
227 
242  bool addMenuItem(const std::vector<sf::String>& hierarchy, bool createParents = true);
243 
244 
248  void removeAllMenus();
249 
250 
261  bool removeMenu(const sf::String& menu);
262 
263 
273  bool removeMenuItem(const sf::String& menu, const sf::String& menuItem);
274 
275 
289  bool removeMenuItem(const std::vector<sf::String>& hierarchy, bool removeParentsWhenEmpty = true);
290 
291 
298  bool setMenuEnabled(const sf::String& menu, bool enabled);
299 
300 
306  bool getMenuEnabled(const sf::String& menu) const;
307 
308 
316  bool setMenuItemEnabled(const sf::String& menu, const sf::String& menuItem, bool enabled);
317 
318 
325  bool setMenuItemEnabled(const std::vector<sf::String>& hierarchy, bool enabled);
326 
327 
334  bool getMenuItemEnabled(const sf::String& menu, const sf::String& menuItem) const;
335 
336 
342  bool getMenuItemEnabled(const std::vector<sf::String>& hierarchy) const;
343 
344 
349  void setTextSize(unsigned int size);
350 
351 
358  unsigned int getTextSize() const;
359 
360 
370  void setMinimumSubMenuWidth(float minimumWidth);
371 
372 
381  float getMinimumSubMenuWidth() const;
382 
383 
390  void setInvertedMenuDirection(bool invertDirection);
391 
392 
399  bool getInvertedMenuDirection() const;
400 
401 
402  #ifndef TGUI_REMOVE_DEPRECATED_CODE
403  TGUI_DEPRECATED("This function doesn't work with submenus, use getAllMenus instead")
409  std::vector<std::pair<sf::String, std::vector<sf::String>>> getMenus() const;
410  #endif
411 
417  std::vector<std::unique_ptr<GetAllMenusElement>> getAllMenus() const;
418 
419 
424  void closeMenu();
425 
426 
433  bool mouseOnWidget(Vector2f pos) const override;
434 
438  void leftMousePressed(Vector2f pos) override;
439 
443  void leftMouseReleased(Vector2f pos) override;
444 
448  void mouseMoved(Vector2f pos) override;
449 
453  void mouseNoLongerDown() override;
454 
455 
457  private:
458 
460  // This function is called when the mouse leaves the widget. If requested, a callback will be send.
462  void mouseLeftWidget() override;
463 
464 
472  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
473 
474 
476  protected:
477 
487  Signal& getSignal(std::string signalName) override;
488 
489 
496  void rendererChanged(const std::string& property) override;
497 
498 
502  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
503 
504 
508  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
509 
510 
512  // Makes a copy of the widget
514  Widget::Ptr clone() const override
515  {
516  return std::make_shared<MenuBar>(*this);
517  }
518 
521  void createMenu(std::vector<aurora::CopiedPtr<Menu>>& menus, const sf::String& text);
522 
526  aurora::CopiedPtr<Menu>* findMenu(const std::vector<sf::String>& hierarchy, unsigned int parentIndex, std::vector<aurora::CopiedPtr<Menu>>& menus, bool createParents);
527 
531  const aurora::CopiedPtr<Menu>* findMenu(const std::vector<sf::String>& hierarchy, unsigned int parentIndex, const std::vector<aurora::CopiedPtr<Menu>>& menus) const;
532 
535  const aurora::CopiedPtr<Menu>* findMenuItem(const std::vector<sf::String>& hierarchy) const;
536 
539  void loadMenus(const std::unique_ptr<DataIO::Node>& node, std::vector<aurora::CopiedPtr<Menu>>& menus);
540 
543  void closeSubMenus(std::vector<aurora::CopiedPtr<Menu>>& menus, int& selectedMenu);
544 
546  void deselectBottomItem();
547 
549  void updateMenuTextColor(aurora::CopiedPtr<Menu>& menu, bool selected);
550 
552  void updateTextColors(std::vector<aurora::CopiedPtr<Menu>>& menus, int selectedMenu);
553 
555  void updateTextOpacity(std::vector<aurora::CopiedPtr<Menu>>& menus);
556 
558  void updateTextFont(std::vector<aurora::CopiedPtr<Menu>>& menus);
559 
562  float calculateMenuWidth(const aurora::CopiedPtr<MenuBar::Menu>& menu) const;
563 
565  Vector2f calculateSubmenuOffset(const aurora::CopiedPtr<Menu>& menu, float globalLeftPos, float menuWidth, float subMenuWidth, bool& openSubMenuToRight) const;
566 
568  bool isMouseOnTopOfMenu(Vector2f menuPos, Vector2f mousePos, bool openSubMenuToRight, const aurora::CopiedPtr<Menu>& menu, float menuWidth) const;
569 
571  bool findMenuItemBelowMouse(Vector2f menuPos, Vector2f mousePos, bool openSubMenuToRight, aurora::CopiedPtr<Menu>& menu, float menuWidth, aurora::CopiedPtr<Menu>** resultMenu, int* resultSelectedMenuItem);
572 
575  void drawMenusOnBar(sf::RenderTarget& target, sf::RenderStates states, Sprite& backgroundSprite) const;
576 
579  void drawMenu(sf::RenderTarget& target, sf::RenderStates states, const aurora::CopiedPtr<Menu>& menu, float menuWidth, Sprite& backgroundSprite, float globalLeftPos, bool openSubMenuToRight) const;
580 
581 
583  public:
584 
589  SignalMenuItem onMenuItemClick = {"MenuItemClicked"};
590 
591 
593  protected:
594 
595  std::vector<aurora::CopiedPtr<Menu>> m_menus;
596 
597  int m_visibleMenu = -1;
598 
599  unsigned int m_textSize = 0;
600 
601  float m_minimumSubMenuWidth = 125;
602 
603  bool m_invertedMenuDirection = false;
604 
605  Sprite m_spriteBackground;
606  Sprite m_spriteItemBackground;
607  Sprite m_spriteSelectedItemBackground;
608 
609  // Cached renderer properties
610  Color m_backgroundColorCached;
611  Color m_selectedBackgroundColorCached;
612  Color m_textColorCached;
613  Color m_selectedTextColorCached;
614  Color m_textColorDisabledCached;
615  float m_distanceToSideCached = 0;
616 
618  };
619 
621 }
622 
623 
625 
626 #endif // TGUI_MENU_BAR_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Sprite.hpp:43
Renderer for the MenuBar widget.
Definition: MenuBarRenderer.hpp:39
Menu bar widget.
Definition: MenuBar.hpp:41
Wrapper for colors.
Definition: Color.hpp:47
Class to store the position or size of a widget.
Definition: Layout.hpp:245
The parent class for every widget.
Definition: Widget.hpp:72
Definition: Vector2f.hpp:37
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.
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:580
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
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:138
Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: MenuBar.hpp:514
Definition: MenuBar.hpp:57
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:120
Used for return value of getAllMenus.
Definition: MenuBar.hpp:49
std::shared_ptr< const MenuBar > ConstPtr
Shared constant widget pointer.
Definition: MenuBar.hpp:46