TGUI  1.0-beta
Loading...
Searching...
No Matches
Label.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2022 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 using Ptr = std::shared_ptr<Label>;
48 using ConstPtr = std::shared_ptr<const Label>;
49
50 static constexpr const char StaticWidgetType[] = "Label";
51
52
57 {
58 Left,
59 Center,
60 Right
61 };
62
63
68 {
69 Top ,
70 Center,
71 Bottom
72 };
73
74
82 Label(const char* typeName = StaticWidgetType, bool initRenderer = true);
83
84
93 static Label::Ptr create(const String& text = "");
94
95
104 static Label::Ptr copy(const Label::ConstPtr& label);
105
106
112 const LabelRenderer* getSharedRenderer() const;
113
120
121
137 void setSize(const Layout2d& size) override;
138 using Widget::setSize;
139
140
151 void setText(const String& text);
152
153
160 const String& getText() const;
161
162
172
173
181
182
192
193
201
202
208
209
215
216
222 void setScrollbarValue(unsigned int value);
223
224
230 unsigned int getScrollbarValue() const;
231
232
244 void setAutoSize(bool autoSize);
245
246
253 bool getAutoSize() const;
254
255
268 void setMaximumTextWidth(float maximumWidth);
269
270
280 float getMaximumTextWidth() const;
281
282
290 void ignoreMouseEvents(bool ignore = true);
291
292
299
300
306 void setParent(Container* parent) override;
307
308
315 bool canGainFocus() const override;
316
317
322 bool isMouseOnWidget(Vector2f pos) const override;
323
324
326 void leftMousePressed(Vector2f pos) override;
327
329 void leftMouseReleased(Vector2f pos) override;
330
332 void mouseMoved(Vector2f pos) override;
333
335 bool mouseWheelScrolled(float delta, Vector2f pos) override;
336
338 void mouseNoLongerOnWidget() override;
339
341 void leftMouseButtonNoLongerDown() override;
342
343
351 void draw(BackendRenderTarget& target, RenderStates states) const override;
352
353
355 protected:
356
366 Signal& getSignal(String signalName) override;
367
368
375 void rendererChanged(const String& property) override;
376
377
381 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
382
383
387 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
388
389
393 void updateTextSize() override;
394
395
397 // This function is called every frame with the time passed since the last frame.
399 bool updateTime(Duration elapsedTime) override;
400
401
405 virtual void rearrangeText();
406
407
409 // Called at the end of rearrangeText() to position the text pieces that were created during rearrangeText().
411 void updateTextPiecePositions(float maxWidth);
412
413
415 // Makes a copy of the widget
417 Widget::Ptr clone() const override;
418
419
421 public:
422
423 SignalString onDoubleClick = {"DoubleClicked"};
424
425
427 protected:
428
429 String m_string;
430 std::vector<std::vector<Text>> m_lines;
431
432 HorizontalAlignment m_horizontalAlignment = HorizontalAlignment::Left;
433 VerticalAlignment m_verticalAlignment = VerticalAlignment::Top;
434
435 bool m_autoSize = true;
436
437 float m_maximumTextWidth = 0;
438
439 bool m_ignoringMouseEvents = false;
440
441 // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
442 bool m_possibleDoubleClick = false;
443
445 Scrollbar::Policy m_scrollbarPolicy = Scrollbar::Policy::Automatic;
446
447 Sprite m_spriteBackground;
448
449 // Cached renderer properties
450 Borders m_bordersCached;
451 Padding m_paddingCached;
452 TextStyles m_textStyleCached;
453 Color m_textColorCached;
454 Color m_borderColorCached;
455 Color m_backgroundColorCached;
456 Color m_textOutlineColorCached;
457 float m_textOutlineThicknessCached = 0;
458
460 };
461
463}
464
466
467#endif // TGUI_LABEL_HPP
Base class for render targets.
Definition: BackendRenderTarget.hpp:48
Clickable widget.
Definition: ClickableWidget.hpp:40
Wrapper for colors.
Definition: Color.hpp:63
Container widget.
Definition: Container.hpp:47
Definition: CopiedSharedPtr.hpp:40
Wrapper for durations.
Definition: Duration.hpp:52
Definition: LabelRenderer.hpp:37
Label widget.
Definition: Label.hpp:44
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer)
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.
VerticalAlignment
The vertical text alignment.
Definition: Label.hpp:68
void ignoreMouseEvents(bool ignore=true)
Sets whether the widget should completely ignore mouse events and let them pass to the widgets behind...
unsigned int getScrollbarValue() const
Returns the thumb position of the scrollbar.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
virtual void rearrangeText()
Rearrange the text (recreates m_textPieces), making use of the given size of maximum text width.
LabelRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
float getMaximumTextWidth() const
Returns the maximum width that the text will have.
static Label::Ptr create(const String &text="")
Creates a new label widget.
void setVerticalAlignment(VerticalAlignment alignment)
Changes the vertical text alignment.
std::shared_ptr< const Label > ConstPtr
Shared constant widget pointer.
Definition: Label.hpp:48
void setScrollbarValue(unsigned int value)
Changes the thumb position of the scrollbar.
LabelRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
static Label::Ptr copy(const Label::ConstPtr &label)
Makes a copy of another label.
bool isIgnoringMouseEvents() const
Returns whether the widget is ignoring mouse events and letting them pass to the widgets behind it.
void setText(const String &text)
Changes the text.
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.
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.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
const String & getText() const
Returns the text.
bool isMouseOnWidget(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< Label > Ptr
Shared widget pointer.
Definition: Label.hpp:47
void setHorizontalAlignment(HorizontalAlignment alignment)
Changes the horizontal text alignment.
HorizontalAlignment
The horizontal text alignment.
Definition: Label.hpp:57
Class to store the position or size of a widget.
Definition: Layout.hpp:284
Definition: Outline.hpp:39
Policy
Defines when the scrollbar shows up.
Definition: Scrollbar.hpp:52
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:58
Definition: Sprite.hpp:45
Wrapper class to store strings.
Definition: String.hpp:79
Wrapper for text styles.
Definition: TextStyle.hpp:58
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
States used for drawing.
Definition: RenderStates.hpp:39