TGUI  0.8-dev
MenuBar.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2017 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 {
50  class TGUI_API MenuBar : public Widget
51  {
52  public:
53 
54  typedef std::shared_ptr<MenuBar> Ptr;
55  typedef std::shared_ptr<const MenuBar> ConstPtr;
56 
57 
59  // Default constructor
61  MenuBar();
62 
63 
70  static MenuBar::Ptr create();
71 
72 
81  static MenuBar::Ptr copy(MenuBar::ConstPtr menuBar);
82 
83 
91  {
92  return aurora::downcast<MenuBarRenderer*>(m_renderer.get());
93  }
94 
95 
104  virtual void setSize(const Layout2d& size) override;
106 
107 
114  void addMenu(const sf::String& text);
115 
116 
134  bool addMenuItem(const sf::String& menu, const sf::String& text);
135 
136 
153  bool addMenuItem(const sf::String& text);
154 
155 
166  bool removeMenu(const sf::String& menu);
167 
168 
173  void removeAllMenus();
174 
175 
185  bool removeMenuItem(const sf::String& menu, const sf::String& menuItem);
186 
187 
195  void setTextSize(unsigned int size);
196 
197 
204  unsigned int getTextSize() const;
205 
206 
216  void setMinimumSubMenuWidth(float minimumWidth);
217 
218 
227  float getMinimumSubMenuWidth() const;
228 
229 
236  void setInvertedMenuDirection(bool invertDirection);
237 
238 
245  bool getInvertedMenuDirection() const;
246 
247 
254  std::map<sf::String, std::vector<sf::String>> getMenus() const;
255 
256 
262  virtual void setParent(Container* parent) override;
263 
264 
268  virtual bool mouseOnWidget(sf::Vector2f pos) const override;
269 
273  virtual void leftMousePressed(sf::Vector2f pos) override;
274 
278  virtual void leftMouseReleased(sf::Vector2f pos) override;
279 
283  virtual void mouseMoved(sf::Vector2f pos) override;
284 
288  virtual void mouseNoLongerDown() override;
289 
290 
292  private:
293 
295  // This function is called when the mouse leaves the widget. If requested, a callback will be send.
297  virtual void mouseLeftWidget() override;
298 
299 
301  // Closes any menu that might be open
303  void closeVisibleMenu();
304 
305 
313  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
314 
315 
317  protected:
318 
325  virtual void rendererChanged(const std::string& property) override;
326 
327 
329  // Makes a copy of the widget
331  virtual Widget::Ptr clone() const override
332  {
333  return std::make_shared<MenuBar>(*this);
334  }
335 
336 
338  protected:
339 
340  struct Menu
341  {
342  Text text;
343  std::vector<Text> menuItems;
344  int selectedMenuItem = -1;
345  };
346 
347  std::vector<Menu> m_menus;
348 
349  int m_visibleMenu = -1;
350 
351  unsigned int m_textSize = 0;
352 
353  float m_minimumSubMenuWidth = 125;
354 
355  bool m_invertedMenuDirection = false;
356 
357  Sprite m_spriteBackground;
358  Sprite m_spriteItemBackground;
359  Sprite m_spriteSelectedItemBackground;
360 
361  // Cached renderer properties
362  Color m_backgroundColorCached;
363  Color m_selectedBackgroundColorCached;
364  Color m_textColorCached;
365  Color m_selectedTextColorCached;
366  float m_distanceToSideCached = 0;
367 
369  };
370 
372 }
373 
374 
376 
377 #endif // TGUI_MENU_BAR_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
Definition: Sprite.hpp:40
Definition: MenuBarRenderer.hpp:36
Menu bar widget.
Definition: MenuBar.hpp:50
Wrapper for colors.
Definition: Color.hpp:46
Class to store the position or size of a widget.
Definition: Layout.hpp:263
The parent class for every widget.
Definition: Widget.hpp:72
MenuBarRenderer * getRenderer() const
Returns the renderer, which gives access to functions that determine how the widget is displayed...
Definition: MenuBar.hpp:90
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: MenuBar.hpp:331
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Definition: Text.hpp:41
std::shared_ptr< MenuBar > Ptr
Shared widget pointer.
Definition: MenuBar.hpp:54
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
Container widget.
Definition: Container.hpp:47
Definition: MenuBar.hpp:340
std::shared_ptr< const MenuBar > ConstPtr
Shared constant widget pointer.
Definition: MenuBar.hpp:55