TGUI  0.7.8
MenuBar.hpp
1 //
3 // TGUI - Texus's 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/Widgets/Label.hpp>
31 
33 
34 namespace tgui
35 {
36  class MenuBarRenderer;
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 
90  std::shared_ptr<MenuBarRenderer> getRenderer() const
91  {
92  return std::static_pointer_cast<MenuBarRenderer>(m_renderer);
93  }
94 
95 
108  virtual void setPosition(const Layout2d& position) override;
110 
111 
120  virtual void setSize(const Layout2d& size) override;
122 
123 
132  virtual void setFont(const Font& font) override;
133 
134 
141  void addMenu(const sf::String& text);
142 
143 
159  bool addMenuItem(const sf::String& menu, const sf::String& text);
160 
161 
176  bool addMenuItem(const sf::String& text);
177 
178 
189  bool removeMenu(const sf::String& menu);
190 
191 
196  void removeAllMenus();
197 
198 
208  bool removeMenuItem(const sf::String& menu, const sf::String& menuItem);
209 
210 
218  void setTextSize(unsigned int size);
219 
220 
227  unsigned int getTextSize() const
228  {
229  return m_textSize;
230  }
231 
232 
242  void setMinimumSubMenuWidth(float minimumWidth);
243 
244 
254  {
255  return m_minimumSubMenuWidth;
256  }
257 
258 
263  void closeMenu();
264 
265 
272  virtual void setOpacity(float opacity) override;
273 
274 
280  virtual void setParent(Container* parent) override;
281 
282 
286  virtual bool mouseOnWidget(float x, float y) const override;
287 
291  virtual void leftMousePressed(float x, float y) override;
292 
296  virtual void leftMouseReleased(float x, float y) override;
297 
301  virtual void mouseMoved(float x, float y) override;
302 
306  virtual void mouseNoLongerDown() override;
307 
308 
310  private:
311 
313  // This function is called when the mouse leaves the widget. If requested, a callback will be send.
315  virtual void mouseLeftWidget() override;
316 
317 
319  protected:
320 
321 
323  // Makes a copy of the widget
325  virtual Widget::Ptr clone() const override
326  {
327  return std::make_shared<MenuBar>(*this);
328  }
329 
330 
343  virtual void reload(const std::string& primary = "", const std::string& secondary = "", bool force = false) override;
344 
345 
347  // Draws the widget on the render target.
349  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
350 
351 
353  protected:
354 
355  struct Menu
356  {
357  Label text;
358  std::vector<Label> menuItems;
359  int selectedMenuItem = -1;
360  };
361 
362  std::vector<Menu> m_menus;
363 
364  int m_visibleMenu = -1;
365 
366  unsigned int m_textSize = 0;
367 
368  float m_minimumSubMenuWidth = 125;
369 
370  friend class MenuBarRenderer;
371 
373  };
374 
375 
377 
378  class TGUI_API MenuBarRenderer : public WidgetRenderer
379  {
380  public:
381 
388  MenuBarRenderer(MenuBar* menuBar) : m_menuBar{menuBar} {}
389 
390 
401  virtual void setProperty(std::string property, const std::string& value) override;
402 
403 
415  virtual void setProperty(std::string property, ObjectConverter&& value) override;
416 
417 
427  virtual ObjectConverter getProperty(std::string property) const override;
428 
429 
436  virtual std::map<std::string, ObjectConverter> getPropertyValuePairs() const override;
437 
438 
445  void setBackgroundColor(const Color& backgroundColor);
446 
447 
454  void setTextColor(const Color& textColor);
455 
456 
463  void setSelectedBackgroundColor(const Color& selectedBackgroundColor);
464 
465 
472  void setSelectedTextColor(const Color& selectedTextColor);
473 
474 
481  void setDistanceToSide(float distanceToSide);
482 
483 
492  void setBackgroundTexture(const Texture& texture);
493 
494 
503  void setItemBackgroundTexture(const Texture& texture);
504 
505 
514  void setSelectedItemBackgroundTexture(const Texture& texture);
515 
516 
518  // Draws the widget on the render target.
520  void draw(sf::RenderTarget& target, sf::RenderStates states) const;
521 
522 
524  private:
525 
527  // Makes a copy of the renderer
529  virtual std::shared_ptr<WidgetRenderer> clone(Widget* widget) override;
530 
531 
533  protected:
534 
535  MenuBar* m_menuBar;
536 
537  float m_distanceToSide = 0;
538 
539  sf::Color m_textColor;
540  sf::Color m_selectedTextColor;
541 
542  sf::Color m_backgroundColor;
543  sf::Color m_selectedBackgroundColor;
544 
545  Texture m_backgroundTexture;
546  Texture m_itemBackgroundTexture;
547  Texture m_selectedItemBackgroundTexture;
548 
549  friend class MenuBar;
550 
552  };
553 
555 }
556 
557 
559 
560 #endif // TGUI_MENU_BAR_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
Definition: MenuBar.hpp:378
Menu bar widget.
Definition: MenuBar.hpp:50
Definition: Font.hpp:37
Class to store the position or size of a widget.
Definition: Layout.hpp:254
Label widget.
Definition: Label.hpp:49
The parent class for every widget.
Definition: Widget.hpp:71
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: MenuBar.hpp:325
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
virtual void setPosition(const Layout2d &position)
set the position of the widget
std::shared_ptr< MenuBar > Ptr
Shared widget pointer.
Definition: MenuBar.hpp:54
MenuBarRenderer(MenuBar *menuBar)
Constructor.
Definition: MenuBar.hpp:388
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:75
Container widget.
Definition: Container.hpp:47
float getMinimumSubMenuWidth() const
Returns the distance between the text and the side of the menu item.
Definition: MenuBar.hpp:253
unsigned int getTextSize() const
Returns the character size of the text.
Definition: MenuBar.hpp:227
Definition: MenuBar.hpp:355
std::shared_ptr< MenuBarRenderer > getRenderer() const
Returns the renderer, which gives access to functions that determine how the widget is displayed...
Definition: MenuBar.hpp:90
std::shared_ptr< const MenuBar > ConstPtr
Shared constant widget pointer.
Definition: MenuBar.hpp:55
Base class for all renderer classes.
Definition: Widget.hpp:682