TGUI  0.7.8
Label.hpp
1
2//
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
34namespace 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
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
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
250
251
259
260
270
271
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
Definition: Borders.hpp:38
Clickable widget.
Definition: ClickableWidget.hpp:56
Implicit converter for colors.
Definition: Color.hpp:40
Container widget.
Definition: Container.hpp:48
Definition: Font.hpp:38
Definition: Label.hpp:459
void setPadding(const Padding &padding) override
Changes the size of the padding.
virtual void setProperty(std::string property, const std::string &value) override
Change a property of the renderer.
void setBorderColor(const Color &color)
Changes the color of the borders.
virtual void setProperty(std::string property, ObjectConverter &&value) override
Change a property of the renderer.
void setTextColor(const Color &color)
Changes the color of the text.
virtual ObjectConverter getProperty(std::string property) const override
Retrieve the value of a certain property.
LabelRenderer(Label *label)
Constructor.
Definition: Label.hpp:468
void setBackgroundColor(const Color &color)
Changes the background color of the label.
virtual std::map< std::string, ObjectConverter > getPropertyValuePairs() const override
Get a map with all properties and their values.
Label widget.
Definition: Label.hpp:50
virtual void reload(const std::string &primary="", const std::string &secondary="", bool force=false) override
Reload the widget.
HorizontalAlignment getHorizontalAlignment() const
Get the current horizontal text alignment.
void setMaximumTextWidth(float maximumWidth)
Changes the maximum width that the text will have when auto-sizing.
const sf::Color & getTextColor() const
Returns the color of the text.
VerticalAlignment
The vertical text alignment.
Definition: Label.hpp:72
void setTextColor(const Color &color)
Changes the color of the text.
void setText(const sf::String &text)
Changes the text.
void setTextStyle(sf::Uint32 style)
Changes the style of the text.
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: Label.hpp:406
virtual void setSize(const Layout2d &size) override
Changes the area of the text that will be drawn.
virtual void setOpacity(float opacity) override
Changes the opacity of the widget.
float getMaximumTextWidth() const
Returns the maximum width that the text will have.
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
unsigned int getTextSize() const
Returns the character size of the text.
virtual sf::Vector2f getWidgetOffset() const override
Returns the distance between the position where the widget is drawn and where the widget is placed.
void setVerticalAlignment(VerticalAlignment alignment)
Change the vertical text alignment.
virtual void setFont(const Font &font) override
Changes the font of the text in the widget.
void setTextSize(unsigned int size)
Changes the character size of the text.
std::shared_ptr< const Label > ConstPtr
Shared constant widget pointer.
Definition: Label.hpp:54
static Label::Ptr copy(Label::ConstPtr label)
Makes a copy of another label.
virtual void setPosition(const Layout2d &position) override
Set the position of the widget.
std::shared_ptr< Label > Ptr
Shared widget pointer.
Definition: Label.hpp:53
virtual bool getAutoSize() const
Returns whether the label is auto-sized or not.
Definition: Label.hpp:319
VerticalAlignment getVerticalAlignment() const
Get the current vertical text alignment.
const sf::String & getText() const
Returns the text.
Definition: Label.hpp:195
sf::Uint32 getTextStyle() const
Returns the style of the text.
static Label::Ptr create(sf::String text="")
Creates a new label widget.
virtual sf::Vector2f getFullSize() const override
Returns the full size of the label.
virtual void setAutoSize(bool autoSize)
Changes whether the label is auto-sized or not.
void setHorizontalAlignment(HorizontalAlignment alignment)
Change the horizontal text alignment.
HorizontalAlignment
The horizontal text alignment.
Definition: Label.hpp:61
Class to store the position or size of a widget.
Definition: Layout.hpp:255
Implicit converter for settable properties.
Definition: ObjectConverter.hpp:43
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
virtual void setPosition(const Layout2d &position)
set the position of the widget
Parent class for every widget that has borders.
Definition: Borders.hpp:137
Parent class for every widget that has padding.
Definition: Borders.hpp:211
virtual void setPadding(const Padding &padding)
Changes the size of the padding.
Definition: Borders.hpp:223
Base class for all renderer classes.
Definition: Widget.hpp:683
The parent class for every widget.
Definition: Widget.hpp:72
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:75
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:34