TGUI  0.7.4
Label.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_LABEL_HPP
27 #define TGUI_LABEL_HPP
28 
29 
30 #include <TGUI/Widgets/ClickableWidget.hpp>
31 
33 
34 namespace tgui
35 {
36  class LabelRenderer;
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 
113  std::shared_ptr<LabelRenderer> getRenderer() const
114  {
115  return std::static_pointer_cast<LabelRenderer>(m_renderer);
116  }
117 
118 
131  virtual void setPosition(const Layout2d& position) override;
133 
134 
150  virtual void setSize(const Layout2d& size) override;
152 
153 
162  virtual sf::Vector2f getFullSize() const override;
163 
164 
173  virtual void setFont(const Font& font) override;
174 
175 
186  void setText(const sf::String& text);
187 
188 
195  const sf::String& getText() const
196  {
197  return m_string;
198  }
199 
200 
211  void setTextStyle(sf::Uint32 style);
212 
213 
220  sf::Uint32 getTextStyle() const;
221 
222 
229  void setTextSize(unsigned int size);
230 
231 
238  unsigned int getTextSize() const;
239 
240 
249  void setHorizontalAlignment(HorizontalAlignment alignment);
250 
251 
258  HorizontalAlignment getHorizontalAlignment() const;
259 
260 
269  void setVerticalAlignment(VerticalAlignment alignment);
270 
271 
278  VerticalAlignment getVerticalAlignment() const;
279 
280 
287  void setTextColor(const Color& color);
288 
289 
296  const sf::Color& getTextColor() const;
297 
298 
310  virtual void setAutoSize(bool autoSize);
311 
312 
319  virtual bool getAutoSize() const
320  {
321  return m_autoSize;
322  }
323 
324 
336  void setMaximumTextWidth(float maximumWidth);
337 
338 
348  float getMaximumTextWidth() const;
349 
350 
357  virtual void setOpacity(float opacity) override;
358 
359 
368  virtual sf::Vector2f getWidgetOffset() const override;
369 
370 
376  virtual void setParent(Container* parent) override;
377 
378 
382  virtual void leftMouseReleased(float x, float y) override;
383 
384 
386  protected:
387 
400  virtual void reload(const std::string& primary = "", const std::string& secondary = "", bool force = false) override;
401 
402 
404  // Makes a copy of the widget
406  virtual Widget::Ptr clone() const override
407  {
408  return std::make_shared<Label>(*this);
409  }
410 
411 
413  // This function is called every frame with the time passed since the last frame.
415  virtual void update(sf::Time elapsedTime) override;
416 
417 
419  // Rearrange the text, making use of the given size of maximum text width.
421  void rearrangeText();
422 
423 
425  // Draws the widget on the render target.
427  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
428 
429 
431  protected:
432 
433  sf::RectangleShape m_background;
434 
435  sf::String m_string;
436  std::vector<sf::Text> m_lines;
437 
438  unsigned int m_textSize = 18;
439  sf::Uint32 m_textStyle = sf::Text::Style::Regular;
440  HorizontalAlignment m_horizontalAlignment = HorizontalAlignment::Left;
441  VerticalAlignment m_verticalAlignment = VerticalAlignment::Top;
442 
443  bool m_autoSize = true;
444 
445  float m_maximumTextWidth = 0;
446 
447  // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
448  bool m_possibleDoubleClick = false;
449 
450  friend LabelRenderer;
451 
453  };
454 
455 
457 
458  class TGUI_API LabelRenderer : public WidgetRenderer, public WidgetBorders, public WidgetPadding
459  {
460  public:
461 
468  LabelRenderer(Label* label) : m_label{label} {}
469 
470 
480  virtual void setProperty(std::string property, const std::string& value) override;
481 
482 
493  virtual void setProperty(std::string property, ObjectConverter&& value) override;
494 
495 
505  virtual ObjectConverter getProperty(std::string property) const override;
506 
507 
514  virtual std::map<std::string, ObjectConverter> getPropertyValuePairs() const override;
515 
516 
525  void setPadding(const Padding& padding) override;
527 
528 
535  void setTextColor(const Color& color);
536 
537 
549  void setBackgroundColor(const Color& color);
550 
551 
558  void setBorderColor(const Color& color);
559 
560 
562  // Draws the widget on the render target.
564  void draw(sf::RenderTarget& target, sf::RenderStates states) const;
565 
566 
568  private:
569 
571  // Makes a copy of the renderer
573  virtual std::shared_ptr<WidgetRenderer> clone(Widget* widget) override;
574 
575 
577  protected:
578 
579  Label* m_label;
580 
581  sf::Color m_textColor;
582  sf::Color m_backgroundColor;
583  sf::Color m_borderColor;
584 
585  friend class Label;
586 
588  };
589 
591 }
592 
594 
595 #endif // TGUI_LABEL_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
virtual void setPadding(const Padding &padding)
Changes the size of the padding.
Definition: Borders.hpp:223
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 bool getAutoSize() const
Returns whether the label is auto-sized or not.
Definition: Label.hpp:319
std::shared_ptr< LabelRenderer > getRenderer() const
Returns the renderer, which gives access to functions that determine how the widget is displayed...
Definition: Label.hpp:113
std::shared_ptr< Label > Ptr
Shared widget pointer.
Definition: Label.hpp:53
const sf::String & getText() const
Returns the text.
Definition: Label.hpp:195
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Parent class for every widget that has padding.
Definition: Borders.hpp:210
LabelRenderer(Label *label)
Constructor.
Definition: Label.hpp:468
virtual void setPosition(const Layout2d &position)
set the position of the widget
Clickable widget.
Definition: ClickableWidget.hpp:55
Definition: Label.hpp:458
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
Container widget.
Definition: Container.hpp:47
Definition: Borders.hpp:37
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: Label.hpp:406
VerticalAlignment
The vertical text alignment.
Definition: Label.hpp:71
HorizontalAlignment
The horizontal text alignment.
Definition: Label.hpp:60
Base class for all renderer classes.
Definition: Widget.hpp:682
std::shared_ptr< const Label > ConstPtr
Shared constant widget pointer.
Definition: Label.hpp:54