TGUI  0.8.9
Label.hpp
1
2//
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_LABEL_HPP
27#define TGUI_LABEL_HPP
28
29
30#include <TGUI/Widgets/ClickableWidget.hpp>
31#include <TGUI/Renderers/LabelRenderer.hpp>
32#include <TGUI/CopiedSharedPtr.hpp>
33#include <TGUI/Widgets/Scrollbar.hpp>
34#include <TGUI/Text.hpp>
35
37
38namespace tgui
39{
43 class TGUI_API Label : public ClickableWidget
44 {
45 public:
46
47 typedef std::shared_ptr<Label> Ptr;
48 typedef std::shared_ptr<const Label> ConstPtr;
49
50#ifndef TGUI_REMOVE_DEPRECATED_CODE
52 using ScrollbarPolicy TGUI_DEPRECATED("Use tgui::Scrollbar::Policy instead") = Scrollbar::Policy;
53#endif
54
59 {
60 Left,
61 Center,
62 Right
63 };
64
65
70 {
71 Top ,
72 Center,
73 Bottom
74 };
75
76
78 // Default constructor
80 Label();
81
82
91 static Label::Ptr create(sf::String text = "");
92
93
103
104
110 const LabelRenderer* getSharedRenderer() const;
111
118 const LabelRenderer* getRenderer() const;
119
120
136 void setSize(const Layout2d& size) override;
137 using Widget::setSize;
138
139
150 void setText(const sf::String& text);
151
152
159 const sf::String& getText() const;
160
161
168 void setTextSize(unsigned int size) override;
169
170
180
181
189
190
200
201
209
210
216
217
223
224
236 void setAutoSize(bool autoSize);
237
238
245 bool getAutoSize() const;
246
247
260 void setMaximumTextWidth(float maximumWidth);
261
262
272 float getMaximumTextWidth() const;
273
274
282 void ignoreMouseEvents(bool ignore = true);
283
284
291
292
298 void setParent(Container* parent) override;
299
300
307 bool canGainFocus() const override;
308
309
314 bool mouseOnWidget(Vector2f pos) const override;
315
316
318 void leftMousePressed(Vector2f pos) override;
319
321 void leftMouseReleased(Vector2f pos) override;
322
324 void mouseMoved(Vector2f pos) override;
325
327 bool mouseWheelScrolled(float delta, Vector2f pos) override;
328
330 void mouseNoLongerOnWidget() override;
331
333 void leftMouseButtonNoLongerDown() override;
334
335
343 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
344
345
347 protected:
348
358 Signal& getSignal(std::string signalName) override;
359
360
367 void rendererChanged(const std::string& property) override;
368
369
373 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
374
375
379 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
380
381
383 // This function is called every frame with the time passed since the last frame.
385 bool update(sf::Time elapsedTime) override;
386
387
389 // Rearrange the text, making use of the given size of maximum text width.
391 void rearrangeText();
392
393
395 // Makes a copy of the widget
397 Widget::Ptr clone() const override
398 {
399 return std::make_shared<Label>(*this);
400 }
401
402
404 public:
405
406 SignalString onDoubleClick = {"DoubleClicked"};
407
408
410 protected:
411
412 sf::String m_string;
413 std::vector<Text> m_lines;
414
415 HorizontalAlignment m_horizontalAlignment = HorizontalAlignment::Left;
416 VerticalAlignment m_verticalAlignment = VerticalAlignment::Top;
417
418 bool m_autoSize = true;
419
420 float m_maximumTextWidth = 0;
421
422 bool m_ignoringMouseEvents = false;
423
424 // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
425 bool m_possibleDoubleClick = false;
426
428#ifdef TGUI_NEXT
430#else
432#endif
433
434 Sprite m_spriteBackground;
435
436 // Cached renderer properties
437 Borders m_bordersCached;
438 Padding m_paddingCached;
439 TextStyle m_textStyleCached;
440 Color m_textColorCached;
441 Color m_borderColorCached;
442 Color m_backgroundColorCached;
443 Color m_textOutlineColorCached;
444 float m_textOutlineThicknessCached = 0;
445
447 };
448
450}
451
453
454#endif // TGUI_LABEL_HPP
Clickable widget.
Definition: ClickableWidget.hpp:40
Wrapper for colors.
Definition: Color.hpp:49
Container widget.
Definition: Container.hpp:48
Definition: CopiedSharedPtr.hpp:40
Definition: LabelRenderer.hpp:37
Label widget.
Definition: Label.hpp:44
bool mouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
Draw the widget to a render target.
void rendererChanged(const std::string &property) override
Function called when one of the properties of the renderer is changed.
void setTextSize(unsigned int size) override
Changes the character size of the text.
bool canGainFocus() const override
Returns whether the widget can gain focus.
void setAutoSize(bool autoSize)
Changes whether the label is auto-sized or not.
HorizontalAlignment getHorizontalAlignment() const
Gets the current horizontal text alignment.
void setMaximumTextWidth(float maximumWidth)
Changes the maximum width that the text will have when auto-sizing.
void setScrollbarPolicy(Scrollbar::Policy policy)
Changes when the vertical scrollbar should be displayed.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: Label.hpp:397
VerticalAlignment
The vertical text alignment.
Definition: Label.hpp:70
void ignoreMouseEvents(bool ignore=true)
Sets whether the widget should completely ignore mouse events and let them pass to the widgets behind...
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
LabelRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setText(const sf::String &text)
Changes the text.
float getMaximumTextWidth() const
Returns the maximum width that the text will have.
void setVerticalAlignment(VerticalAlignment alignment)
Changes the vertical text alignment.
Signal & getSignal(std::string signalName) override
Retrieves a signal based on its name.
LabelRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
bool isIgnoringMouseEvents() const
Returns whether the widget is ignoring mouse events and letting them pass to the widgets behind it.
std::shared_ptr< const Label > ConstPtr
Shared constant widget pointer.
Definition: Label.hpp:48
static Label::Ptr copy(Label::ConstPtr label)
Makes a copy of another label.
std::shared_ptr< Label > Ptr
Shared widget pointer.
Definition: Label.hpp:47
bool getAutoSize() const
Returns whether the label is auto-sized or not.
VerticalAlignment getVerticalAlignment() const
Gets the current vertical text alignment.
Scrollbar::Policy getScrollbarPolicy() const
Returns when the vertical scrollbar should be displayed.
const sf::String & getText() const
Returns the text.
void setSize(const Layout2d &size) override
Changes the area of the text that will be drawn.
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.
static Label::Ptr create(sf::String text="")
Creates a new label widget.
void setHorizontalAlignment(HorizontalAlignment alignment)
Changes the horizontal text alignment.
HorizontalAlignment
The horizontal text alignment.
Definition: Label.hpp:59
Class to store the position or size of a widget.
Definition: Layout.hpp:260
Definition: Outline.hpp:39
Policy
Defines when the scrollbar shows up.
Definition: Scrollbar.hpp:50
@ Automatic
Show the scrollbar only when needed (default)
@ Never
Never show the scrollbar, even if the contents does not fit.
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:231
Definition: Sprite.hpp:46
Wrapper for text styles.
Definition: TextStyle.hpp:47
Definition: Vector2f.hpp:39
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37