TGUI  0.8-dev
Label.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_LABEL_HPP
27 #define TGUI_LABEL_HPP
28 
29 
30 #include <TGUI/Widgets/ClickableWidget.hpp>
31 #include <TGUI/Renderers/LabelRenderer.hpp>
32 #include <TGUI/Text.hpp>
33 
35 
36 namespace tgui
37 {
49  class TGUI_API Label : public ClickableWidget
50  {
51  public:
52 
53  typedef std::shared_ptr<Label> Ptr;
54  typedef std::shared_ptr<const Label> ConstPtr;
55 
56 
61  {
62  Left,
63  Center,
64  Right
65  };
66 
67 
71  enum class VerticalAlignment
72  {
73  Top ,
74  Center,
75  Bottom
76  };
77 
78 
80  // Default constructor
82  Label();
83 
84 
93  static Label::Ptr create(sf::String text = "");
94 
95 
104  static Label::Ptr copy(Label::ConstPtr label);
105 
106 
114  {
115  return aurora::downcast<LabelRenderer*>(m_renderer.get());
116  }
117 
118 
134  virtual void setSize(const Layout2d& size) override;
136 
137 
148  void setText(const sf::String& text);
149 
150 
157  const sf::String& getText() const;
158 
159 
166  void setTextSize(unsigned int size);
167 
168 
175  unsigned int getTextSize() const;
176 
177 
186  void setHorizontalAlignment(HorizontalAlignment alignment);
187 
188 
195  HorizontalAlignment getHorizontalAlignment() const;
196 
197 
206  void setVerticalAlignment(VerticalAlignment alignment);
207 
208 
215  VerticalAlignment getVerticalAlignment() const;
216 
217 
229  void setAutoSize(bool autoSize);
230 
231 
238  bool getAutoSize() const;
239 
240 
253  void setMaximumTextWidth(float maximumWidth);
254 
255 
265  float getMaximumTextWidth() const;
266 
267 
273  virtual void setParent(Container* parent) override;
274 
275 
279  virtual void leftMouseReleased(sf::Vector2f pos) override;
280 
281 
289  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
290 
291 
293  protected:
294 
301  virtual void rendererChanged(const std::string& property) override;
302 
303 
305  // This function is called every frame with the time passed since the last frame.
307  virtual void update(sf::Time elapsedTime) override;
308 
309 
311  // Rearrange the text, making use of the given size of maximum text width.
313  void rearrangeText();
314 
315 
317  // Makes a copy of the widget
319  virtual Widget::Ptr clone() const override
320  {
321  return std::make_shared<Label>(*this);
322  }
323 
324 
326  protected:
327 
328  sf::String m_string;
329  std::vector<Text> m_lines;
330 
331  unsigned int m_textSize = 18;
332  HorizontalAlignment m_horizontalAlignment = HorizontalAlignment::Left;
333  VerticalAlignment m_verticalAlignment = VerticalAlignment::Top;
334 
335  bool m_autoSize = true;
336 
337  float m_maximumTextWidth = 0;
338 
339  // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
340  bool m_possibleDoubleClick = false;
341 
342  // Cached renderer properties
343  Borders m_bordersCached;
344  Padding m_paddingCached;
345  TextStyle m_textStyleCached;
346  Color m_textColorCached;
347  Color m_borderColorCached;
348  Color m_backgroundColorCached;
349 
351  };
352 
354 }
355 
357 
358 #endif // TGUI_LABEL_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
Wrapper for colors.
Definition: Color.hpp:46
Wrapper for text styles.
Definition: TextStyle.hpp:46
Class to store the position or size of a widget.
Definition: Layout.hpp:263
Label widget.
Definition: Label.hpp:49
std::shared_ptr< Label > Ptr
Shared widget pointer.
Definition: Label.hpp:53
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Clickable widget.
Definition: ClickableWidget.hpp:55
Definition: LabelRenderer.hpp:36
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
Container widget.
Definition: Container.hpp:47
LabelRenderer * getRenderer() const
Returns the renderer, which gives access to functions that determine how the widget is displayed...
Definition: Label.hpp:113
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: Label.hpp:319
Definition: Outline.hpp:37
VerticalAlignment
The vertical text alignment.
Definition: Label.hpp:71
HorizontalAlignment
The horizontal text alignment.
Definition: Label.hpp:60
std::shared_ptr< const Label > ConstPtr
Shared constant widget pointer.
Definition: Label.hpp:54