TGUI  0.7.4
Tab.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_TAB_HPP
27 #define TGUI_TAB_HPP
28 
29 
30 #include <TGUI/Widgets/Label.hpp>
31 
33 
34 namespace tgui
35 {
36  class TabRenderer;
37 
49  class TGUI_API Tab : public Widget
50  {
51  public:
52 
53  typedef std::shared_ptr<Tab> Ptr;
54  typedef std::shared_ptr<const Tab> ConstPtr;
55 
56 
58  // Default constructor
60  Tab();
61 
62 
69  static Tab::Ptr create();
70 
71 
80  static Tab::Ptr copy(Tab::ConstPtr tab);
81 
82 
89  std::shared_ptr<TabRenderer> getRenderer() const
90  {
91  return std::static_pointer_cast<TabRenderer>(m_renderer);
92  }
93 
94 
107  virtual void setPosition(const Layout2d& position) override;
109 
110 
123  virtual void setSize(const Layout2d& size) override;
125 
126 
133  virtual sf::Vector2f getSize() const override
134  {
135  return {m_width, m_tabHeight};
136  }
137 
138 
147  virtual void setFont(const Font& font) override;
148 
149 
161  std::size_t add(const sf::String& text, bool select = true);
162 
163 
172  void insert(std::size_t index, const sf::String& text, bool select = true);
173 
174 
183  sf::String getText(std::size_t index) const;
184 
185 
195  bool changeText(std::size_t index, const sf::String& text);
196 
197 
209  void select(const sf::String& text);
210 
211 
222  void select(std::size_t index);
223 
224 
229  void deselect();
230 
231 
242  void remove(const sf::String& text);
243 
244 
255  void remove(std::size_t index);
256 
257 
262  void removeAll();
263 
264 
272  sf::String getSelected() const
273  {
274  return (m_selectedTab >= 0) ? m_tabTexts[m_selectedTab].getText() : "";
275  }
276 
277 
287  int getSelectedIndex() const
288  {
289  return m_selectedTab;
290  }
291 
292 
300  void setTextSize(unsigned int size);
301 
302 
309  unsigned int getTextSize() const;
310 
311 
320  void setTabHeight(float height);
321 
322 
329  float getTabHeight() const
330  {
331  return m_tabHeight;
332  }
333 
334 
344  void setMaximumTabWidth(float maximumWidth);
345 
346 
356  float getMaximumTabWidth() const
357  {
358  return m_maximumTabWidth;
359  }
360 
361 
368  std::size_t getTabsCount() const
369  {
370  return m_tabTexts.size();
371  }
372 
373 
380  virtual void setOpacity(float opacity) override;
381 
382 
391  virtual sf::Vector2f getWidgetOffset() const override;
392 
393 
397  virtual bool mouseOnWidget(float x, float y) const override;
398 
402  virtual void leftMousePressed(float x, float y) override;
403 
404 
406  protected:
407 
408 
410  // Recalculates the size of each tab image.
412  void recalculateTabsWidth();
413 
414 
427  virtual void reload(const std::string& primary = "", const std::string& secondary = "", bool force = false) override;
428 
429 
431  // Makes a copy of the widget
433  virtual Widget::Ptr clone() const override
434  {
435  return std::make_shared<Tab>(*this);
436  }
437 
438 
440  // Draws the widget on the render target.
442  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
443 
444 
446  protected:
447  unsigned int m_requestedTextSize = 0;
448  unsigned int m_textSize = 22;
449 
450  float m_maximumTabWidth = 0;
451  int m_selectedTab = -1;
452 
453  float m_width = 0;
454  float m_tabHeight = 0;
455  std::vector<float> m_tabWidth;
456 
457  std::vector<Label> m_tabTexts;
458 
459  friend class TabRenderer;
460 
462  };
463 
464 
466 
467  class TGUI_API TabRenderer : public WidgetRenderer, public WidgetBorders
468  {
469  public:
470 
477  TabRenderer(Tab* tab) : m_tab{tab} {}
478 
479 
490  virtual void setProperty(std::string property, const std::string& value) override;
491 
492 
504  virtual void setProperty(std::string property, ObjectConverter&& value) override;
505 
506 
516  virtual ObjectConverter getProperty(std::string property) const override;
517 
518 
525  virtual std::map<std::string, ObjectConverter> getPropertyValuePairs() const override;
526 
527 
534  void setTextColor(const Color& color);
535 
536 
543  void setSelectedTextColor(const Color& color);
544 
545 
552  void setDistanceToSide(float distanceToSide);
553 
554 
563  void setBackgroundColor(const Color& color);
564 
565 
574  void setSelectedBackgroundColor(const Color& color);
575 
576 
583  void setBorderColor(const Color& color);
584 
585 
595  void setNormalTexture(const Texture& texture);
596 
597 
607  void setSelectedTexture(const Texture& texture);
608 
609 
611  // Draws the widget on the render target.
613  void draw(sf::RenderTarget& target, sf::RenderStates states) const;
614 
615 
617  private:
618 
620  // Makes a copy of the renderer
622  virtual std::shared_ptr<WidgetRenderer> clone(Widget* widget) override;
623 
624 
626  protected:
627 
628  Tab* m_tab;
629 
630  Texture m_textureNormal;
631  Texture m_textureSelected;
632  std::vector<Texture> m_texturesNormal;
633  std::vector<Texture> m_texturesSelected;
634 
635  sf::Color m_textColor;
636  sf::Color m_selectedTextColor;
637 
638  sf::Color m_backgroundColor;
639  sf::Color m_selectedBackgroundColor;
640 
641  sf::Color m_borderColor;
642 
643  // The distance between the side of the tab and the text that is drawn on top of the tab.
644  float m_distanceToSide = 5;
645 
646  friend class Tab;
647 
649  };
650 
652 }
653 
654 
656 
657 #endif // TGUI_TAB_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
Implicit converter for colors.
Definition: Color.hpp:39
virtual sf::Vector2f getSize() const override
Returns the size of the tabs.
Definition: Tab.hpp:133
Definition: Font.hpp:37
Class to store the position or size of a widget.
Definition: Layout.hpp:262
The parent class for every widget.
Definition: Widget.hpp:71
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: Tab.hpp:433
virtual void setPosition(const Layout2d &position)
set the position of the widget
Parent class for every widget that has borders.
Definition: Borders.hpp:136
Implicit converter for settable properties.
Definition: ObjectConverter.hpp:42
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:75
float getMaximumTabWidth() const
Returns the maximum tab width of the tabs.
Definition: Tab.hpp:356
std::shared_ptr< const Tab > ConstPtr
Shared constant widget pointer.
Definition: Tab.hpp:54
Tab widget.
Definition: Tab.hpp:49
Definition: Texture.hpp:44
std::size_t getTabsCount() const
Returns the amount of tabs.
Definition: Tab.hpp:368
TabRenderer(Tab *tab)
Constructor.
Definition: Tab.hpp:477
Definition: Tab.hpp:467
std::shared_ptr< TabRenderer > getRenderer() const
Returns the renderer, which gives access to functions that determine how the widget is displayed...
Definition: Tab.hpp:89
std::shared_ptr< Tab > Ptr
Shared widget pointer.
Definition: Tab.hpp:53
sf::String getSelected() const
Get the text that is drawn on the currently selected tab.
Definition: Tab.hpp:272
int getSelectedIndex() const
Get the index of the currently selected tab.
Definition: Tab.hpp:287
Base class for all renderer classes.
Definition: Widget.hpp:682
float getTabHeight() const
Returns the height of the tabs.
Definition: Tab.hpp:329