TGUI  0.7.4
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 
265  virtual void setOpacity(float opacity) override;
266 
267 
273  virtual void setParent(Container* parent) override;
274 
275 
279  virtual bool mouseOnWidget(float x, float y) const override;
280 
284  virtual void leftMousePressed(float x, float y) override;
285 
289  virtual void leftMouseReleased(float x, float y) override;
290 
294  virtual void mouseMoved(float x, float y) override;
295 
299  virtual void mouseNoLongerDown() override;
300 
301 
303  private:
304 
306  // This function is called when the mouse leaves the widget. If requested, a callback will be send.
308  virtual void mouseLeftWidget() override;
309 
310 
312  // Closes any menu that might be open
314  void closeVisibleMenu();
315 
316 
318  protected:
319 
320 
322  // Makes a copy of the widget
324  virtual Widget::Ptr clone() const override
325  {
326  return std::make_shared<MenuBar>(*this);
327  }
328 
329 
342  virtual void reload(const std::string& primary = "", const std::string& secondary = "", bool force = false) override;
343 
344 
346  // Draws the widget on the render target.
348  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
349 
350 
352  protected:
353 
354  struct Menu
355  {
356  Label text;
357  std::vector<Label> menuItems;
358  int selectedMenuItem = -1;
359  };
360 
361  std::vector<Menu> m_menus;
362 
363  int m_visibleMenu = -1;
364 
365  unsigned int m_textSize = 0;
366 
367  float m_minimumSubMenuWidth = 125;
368 
369  friend class MenuBarRenderer;
370 
372  };
373 
374 
376 
377  class TGUI_API MenuBarRenderer : public WidgetRenderer
378  {
379  public:
380 
387  MenuBarRenderer(MenuBar* menuBar) : m_menuBar{menuBar} {}
388 
389 
400  virtual void setProperty(std::string property, const std::string& value) override;
401 
402 
414  virtual void setProperty(std::string property, ObjectConverter&& value) override;
415 
416 
426  virtual ObjectConverter getProperty(std::string property) const override;
427 
428 
435  virtual std::map<std::string, ObjectConverter> getPropertyValuePairs() const override;
436 
437 
444  void setBackgroundColor(const Color& backgroundColor);
445 
446 
453  void setTextColor(const Color& textColor);
454 
455 
462  void setSelectedBackgroundColor(const Color& selectedBackgroundColor);
463 
464 
471  void setSelectedTextColor(const Color& selectedTextColor);
472 
473 
483  void setTextFont(std::shared_ptr<sf::Font> font);
484 
485 
492  void setDistanceToSide(float distanceToSide);
493 
494 
501  void setBorderColor(const Color& color);
502 
503 
512  void setBackgroundTexture(const Texture& texture);
513 
514 
523  void setItemBackgroundTexture(const Texture& texture);
524 
525 
534  void setSelectedItemBackgroundTexture(const Texture& texture);
535 
536 
538  // Draws the widget on the render target.
540  void draw(sf::RenderTarget& target, sf::RenderStates states) const;
541 
542 
544  private:
545 
547  // Makes a copy of the renderer
549  virtual std::shared_ptr<WidgetRenderer> clone(Widget* widget) override;
550 
551 
553  protected:
554 
555  MenuBar* m_menuBar;
556 
557  float m_distanceToSide = 0;
558 
559  sf::Color m_textColor;
560  sf::Color m_selectedTextColor;
561 
562  sf::Color m_backgroundColor;
563  sf::Color m_selectedBackgroundColor;
564 
565  Texture m_backgroundTexture;
566  Texture m_itemBackgroundTexture;
567  Texture m_selectedItemBackgroundTexture;
568 
569  friend class MenuBar;
570 
572  };
573 
575 }
576 
577 
579 
580 #endif // TGUI_MENU_BAR_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
Definition: MenuBar.hpp:377
Menu bar widget.
Definition: MenuBar.hpp:50
Implicit converter for colors.
Definition: Color.hpp:39
Definition: Font.hpp:37
Class to store the position or size of a widget.
Definition: Layout.hpp:262
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:324
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:387
Implicit converter for settable properties.
Definition: ObjectConverter.hpp:42
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
Definition: Texture.hpp:44
unsigned int getTextSize() const
Returns the character size of the text.
Definition: MenuBar.hpp:227
Definition: MenuBar.hpp:354
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