TGUI  0.8.8
Tabs.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 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/Widget.hpp>
31 #include <TGUI/Renderers/TabsRenderer.hpp>
32 #include <TGUI/Text.hpp>
33 
35 
36 namespace tgui
37 {
41  class TGUI_API Tabs : public Widget
42  {
43  public:
44 
45  typedef std::shared_ptr<Tabs> Ptr;
46  typedef std::shared_ptr<const Tabs> ConstPtr;
47 
48 
50  // Default constructor
52  Tabs();
53 
54 
61  static Tabs::Ptr create();
62 
63 
73 
74 
80  const TabsRenderer* getSharedRenderer() const;
81 
88  const TabsRenderer* getRenderer() const;
89 
90 
102  void setSize(const Layout2d& size) override;
103  using Widget::setSize;
104 
105 
113  void setEnabled(bool enabled) override;
114 
115 
127  void setAutoSize(bool autoSize);
128 
129 
136  bool getAutoSize() const;
137 
138 
150  std::size_t add(const sf::String& text, bool select = true);
151 
152 
161  void insert(std::size_t index, const sf::String& text, bool select = true);
162 
163 
172  sf::String getText(std::size_t index) const;
173 
174 
184  bool changeText(std::size_t index, const sf::String& text);
185 
186 
196  bool select(const sf::String& text);
197 
198 
207  bool select(std::size_t index);
208 
209 
214  void deselect();
215 
216 
223  bool remove(const sf::String& text);
224 
225 
233  bool remove(std::size_t index);
234 
235 
240  void removeAll();
241 
242 
250  sf::String getSelected() const;
251 
252 
262  int getSelectedIndex() const;
263 
264 
270  void setTabVisible(std::size_t index, bool visible);
271 
272 
278  bool getTabVisible(std::size_t index) const;
279 
280 
286  void setTabEnabled(std::size_t index, bool enabled);
287 
288 
294  bool getTabEnabled(std::size_t index) const;
295 
296 
304  void setTextSize(unsigned int size) override;
305 
306 
316  void setTabHeight(float height);
317 
318 
330  void setMaximumTabWidth(float maximumWidth);
331 
332 
344  float getMaximumTabWidth() const;
345 
346 
357  void setMinimumTabWidth(float minimumWidth);
358 
359 
370  float getMinimumTabWidth() const;
371 
372 
379  std::size_t getTabsCount() const;
380 
381 
388  bool mouseOnWidget(Vector2f pos) const override;
389 
393  void leftMousePressed(Vector2f pos) override;
394 
398  void mouseMoved(Vector2f pos) override;
399 
403  void mouseNoLongerOnWidget() override;
404 
405 
413  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
414 
415 
417  protected:
418 
428  Signal& getSignal(std::string signalName) override;
429 
430 
432  // Recalculates the size of each tab image.
434  void recalculateTabsWidth();
435 
436 
438  // Update the colors of the text that is drawn on the tabs
440  void updateTextColors();
441 
442 
449  void rendererChanged(const std::string& property) override;
450 
451 
455  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
456 
457 
461  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
462 
463 
465  // Makes a copy of the widget
467  Widget::Ptr clone() const override
468  {
469  return std::make_shared<Tabs>(*this);
470  }
471 
472 
474  public:
475 
476  SignalString onTabSelect = {"TabSelected"};
477 
478 
480  protected:
481  unsigned int m_requestedTextSize = 0;
482  float m_maximumTabWidth = 0;
483  float m_minimumTabWidth = 0;
484  int m_selectedTab = -1;
485  int m_hoveringTab = -1;
486  bool m_autoSize = true;
487 
488  struct Tab
489  {
490  bool visible;
491  bool enabled;
492  float width;
493  Text text;
494  };
495  std::vector<Tab> m_tabs;
496 
497  Sprite m_spriteTab;
498  Sprite m_spriteTabHover;
499  Sprite m_spriteSelectedTab;
500  Sprite m_spriteSelectedTabHover;
501  Sprite m_spriteDisabledTab;
502 
503  // Cached renderer properties
504  Borders m_bordersCached;
505  Color m_borderColorCached;
506  Color m_backgroundColorCached;
507  Color m_backgroundColorHoverCached;
508  Color m_backgroundColorDisabledCached;
509  Color m_selectedBackgroundColorCached;
510  Color m_selectedBackgroundColorHoverCached;
511  Color m_textColorCached;
512  Color m_textColorHoverCached;
513  Color m_textColorDisabledCached;
514  Color m_selectedTextColorCached;
515  Color m_selectedTextColorHoverCached;
516  float m_distanceToSideCached = 0;
517 
519  };
520 
522 }
523 
524 
526 
527 #endif // TGUI_TAB_HPP
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:224
static Tabs::Ptr create()
Creates a new tabs widget.
std::shared_ptr< Tabs > Ptr
Shared widget pointer.
Definition: Tabs.hpp:45
std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
TabsRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
float getMaximumTabWidth() const
Returns the maximum tab width of the tabs.
sf::String getText(std::size_t index) const
Gets the text of one of the tabs.
void deselect()
Deselects the selected tab.
bool mouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37
Definition: Text.hpp:43
bool remove(std::size_t index)
Removes a tab with a given index.
static Tabs::Ptr copy(Tabs::ConstPtr tab)
Makes a copy of another tab.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
int getSelectedIndex() const
Gets the index of the currently selected tab.
void setMaximumTabWidth(float maximumWidth)
Changes the maximum tab width of the tabs.
Class to store the position or size of a widget.
Definition: Layout.hpp:260
Definition: TabsRenderer.hpp:37
Wrapper for colors.
Definition: Color.hpp:49
void setTabVisible(std::size_t index, bool visible)
Changes whether a tab is visible.
void insert(std::size_t index, const sf::String &text, bool select=true)
Inserts a new tab somewhere between the other tabs.
bool getTabEnabled(std::size_t index) const
Returns whether the tab is enabled or disabled.
std::shared_ptr< const Tabs > ConstPtr
Shared constant widget pointer.
Definition: Tabs.hpp:46
Definition: Sprite.hpp:46
bool changeText(std::size_t index, const sf::String &text)
Changes the text of one of the tabs.
bool select(std::size_t index)
Selects the tab with a given index.
void setAutoSize(bool autoSize)
Changes whether the tabs are auto-sized or not.
TabsRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
Definition: Vector2f.hpp:39
void setTabEnabled(std::size_t index, bool enabled)
Changes whether a tab is enabled.
bool getTabVisible(std::size_t index) const
Returns whether the tab is shown or hidden.
Definition: Outline.hpp:39
sf::String getSelected() const
Gets the text that is drawn on the currently selected tab.
std::size_t add(const sf::String &text, bool select=true)
Adds a new tab.
The parent class for every widget.
Definition: Widget.hpp:74
void setTextSize(unsigned int size) override
Changes the character size of the text.
Definition: Tabs.hpp:489
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Tabs widget.
Definition: Tabs.hpp:42
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
Draw the widget to a render target.
Signal & getSignal(std::string signalName) override
Retrieves a signal based on its name.
bool remove(const sf::String &text)
Removes a tab with a given text.
void setTabHeight(float height)
Changes the height of the tabs.
void rendererChanged(const std::string &property) override
Function called when one of the properties of the renderer is changed.
std::size_t getTabsCount() const
Returns the amount of tabs.
void setEnabled(bool enabled) override
Enables or disables the widget.
void setMinimumTabWidth(float minimumWidth)
Changes the minimum tab width of the tabs.
void removeAll()
Removes all tabs.
float getMinimumTabWidth() const
Returns the minimum tab width of the tabs.
void setSize(const Layout2d &size) override
Changes the area used by the tabs.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: Tabs.hpp:467
bool getAutoSize() const
Returns whether the tabs are auto-sized or not.
bool select(const sf::String &text)
Selects the tab with a given text.