TGUI  0.8-alpha
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 
48 
50  // Default constructor
52  MenuBar();
53 
54 
61  static MenuBar::Ptr create();
62 
63 
72  static MenuBar::Ptr copy(MenuBar::ConstPtr menuBar);
73 
74 
79  MenuBarRenderer* getSharedRenderer();
80  const MenuBarRenderer* getSharedRenderer() const;
81 
87  MenuBarRenderer* getRenderer();
88  const MenuBarRenderer* getRenderer() const;
89 
90 
99  void setSize(const Layout2d& size) override;
100  using Widget::setSize;
101 
102 
109  void addMenu(const sf::String& text);
110 
111 
129  bool addMenuItem(const sf::String& menu, const sf::String& text);
130 
131 
148  bool addMenuItem(const sf::String& text);
149 
150 
161  bool removeMenu(const sf::String& menu);
162 
163 
168  void removeAllMenus();
169 
170 
180  bool removeMenuItem(const sf::String& menu, const sf::String& menuItem);
181 
182 
190  void setTextSize(unsigned int size);
191 
192 
199  unsigned int getTextSize() const;
200 
201 
211  void setMinimumSubMenuWidth(float minimumWidth);
212 
213 
222  float getMinimumSubMenuWidth() const;
223 
224 
231  void setInvertedMenuDirection(bool invertDirection);
232 
233 
240  bool getInvertedMenuDirection() const;
241 
242 
249  std::vector<std::pair<sf::String, std::vector<sf::String>>> getMenus() const;
250 
251 
256  void closeMenu();
257 
258 
265  bool mouseOnWidget(Vector2f pos) const override;
266 
270  void leftMousePressed(Vector2f pos) override;
271 
275  void leftMouseReleased(Vector2f pos) override;
276 
280  void mouseMoved(Vector2f pos) override;
281 
285  void mouseNoLongerDown() override;
286 
287 
289  private:
290 
292  // This function is called when the mouse leaves the widget. If requested, a callback will be send.
294  void mouseLeftWidget() override;
295 
296 
304  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
305 
306 
308  protected:
309 
319  Signal& getSignal(std::string signalName) override;
320 
321 
328  void rendererChanged(const std::string& property) override;
329 
330 
334  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
335 
336 
340  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
341 
342 
344  // Makes a copy of the widget
346  Widget::Ptr clone() const override
347  {
348  return std::make_shared<MenuBar>(*this);
349  }
350 
351 
353  public:
354 
359  SignalMenuItem onMenuItemClick = {"MenuItemClicked"};
360 
361 
363  protected:
364 
365  struct Menu
366  {
367  Text text;
368  std::vector<Text> menuItems;
369  int selectedMenuItem = -1;
370  };
371 
372  std::vector<Menu> m_menus;
373 
374  int m_visibleMenu = -1;
375 
376  unsigned int m_textSize = 0;
377 
378  float m_minimumSubMenuWidth = 125;
379 
380  bool m_invertedMenuDirection = false;
381 
382  Sprite m_spriteBackground;
383  Sprite m_spriteItemBackground;
384  Sprite m_spriteSelectedItemBackground;
385 
386  // Cached renderer properties
387  Color m_backgroundColorCached;
388  Color m_selectedBackgroundColorCached;
389  Color m_textColorCached;
390  Color m_selectedTextColorCached;
391  float m_distanceToSideCached = 0;
392 
394  };
395 
397 }
398 
399 
401 
402 #endif // TGUI_MENU_BAR_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Sprite.hpp:43
Definition: MenuBarRenderer.hpp:36
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:243
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:486
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:58
Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: MenuBar.hpp:346
Definition: MenuBar.hpp:365
std::shared_ptr< const MenuBar > ConstPtr
Shared constant widget pointer.
Definition: MenuBar.hpp:46