TGUI  1.3-dev
Loading...
Searching...
No Matches
Label.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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
38TGUI_MODULE_EXPORT namespace 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 TGUI_NODISCARD static Label::Ptr create(const String& text = "");
94
95
104 TGUI_NODISCARD static Label::Ptr copy(const Label::ConstPtr& label);
105
106
111 TGUI_NODISCARD LabelRenderer* getSharedRenderer() override;
112 TGUI_NODISCARD const LabelRenderer* getSharedRenderer() const override;
113
119 TGUI_NODISCARD LabelRenderer* getRenderer() override;
120
121
137 void setSize(const Layout2d& size) override;
138 using Widget::setSize;
139
140
151 void setText(const String& text);
152
153
160 TGUI_NODISCARD const String& getText() const;
161
162
172
173
181
182
192
193
201
202
208
209
214 TGUI_NODISCARD Scrollbar::Policy getScrollbarPolicy() const;
215
216
222 void setScrollbarValue(unsigned int value);
223
224
230 TGUI_NODISCARD unsigned int getScrollbarValue() const;
231
232
244 void setAutoSize(bool autoSize);
245
246
253 TGUI_NODISCARD bool getAutoSize() const;
254
255
268 void setMaximumTextWidth(float maximumWidth);
269
270
280 TGUI_NODISCARD float getMaximumTextWidth() const;
281
282
290 void ignoreMouseEvents(bool ignore = true);
291
292
298 TGUI_NODISCARD bool isIgnoringMouseEvents() const;
299
300
306 void setParent(Container* parent) override;
307
308
315 TGUI_NODISCARD bool canGainFocus() const override;
316
317
322 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
323
324
326 bool leftMousePressed(Vector2f pos) override;
327
329 void leftMouseReleased(Vector2f pos) override;
330
332 void mouseMoved(Vector2f pos) override;
333
335 bool scrolled(float delta, Vector2f pos, bool touch) 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 TGUI_NODISCARD Signal& getSignal(String signalName) override;
367
368
375 void rendererChanged(const String& property) override;
376
377
381 TGUI_NODISCARD 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 TGUI_NODISCARD 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:72
Container widget.
Definition Container.hpp:49
Definition CopiedSharedPtr.hpp:45
Wrapper for durations.
Definition Duration.hpp:56
Definition LabelRenderer.hpp:37
Label widget.
Definition Label.hpp:44
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer)
TGUI_NODISCARD bool isIgnoringMouseEvents() const
Returns whether the widget is ignoring mouse events and letting them pass to the widgets behind it.
void setAutoSize(bool autoSize)
Changes whether the label is auto-sized or not.
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.
static TGUI_NODISCARD Label::Ptr create(const String &text="")
Creates a new label widget.
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...
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.
bool scrolled(float delta, Vector2f pos, bool touch) override
Called by the parent on scroll event (either from mouse wheel of from two finger scrolling on a touch...
TGUI_NODISCARD Scrollbar::Policy getScrollbarPolicy() const
Returns when the vertical scrollbar should be displayed.
TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
void setVerticalAlignment(VerticalAlignment alignment)
Changes the vertical text alignment.
std::shared_ptr< const Label > ConstPtr
Shared constant widget pointer.
Definition Label.hpp:48
TGUI_NODISCARD VerticalAlignment getVerticalAlignment() const
Gets the current vertical text alignment.
TGUI_NODISCARD Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
void setScrollbarValue(unsigned int value)
Changes the thumb position of the scrollbar.
TGUI_NODISCARD bool canGainFocus() const override
Returns whether the widget can gain focus.
void setText(const String &text)
Changes the text.
TGUI_NODISCARD unsigned int getScrollbarValue() const
Returns the thumb position of the scrollbar.
TGUI_NODISCARD LabelRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
TGUI_NODISCARD HorizontalAlignment getHorizontalAlignment() const
Gets the current horizontal text alignment.
TGUI_NODISCARD const String & getText() const
Returns the text.
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
TGUI_NODISCARD 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.
TGUI_NODISCARD bool getAutoSize() const
Returns whether the label is auto-sized or not.
void setSize(const Layout2d &size) override
Changes the area of the text that will be drawn.
TGUI_NODISCARD LabelRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
TGUI_NODISCARD float getMaximumTextWidth() const
Returns the maximum width that the text will have.
TGUI_NODISCARD Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
static TGUI_NODISCARD Label::Ptr copy(const Label::ConstPtr &label)
Makes a copy of another label.
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:305
Definition Outline.hpp:39
Policy
Defines when the scrollbar shows up.
Definition Scrollbar.hpp:56
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:62
Definition Sprite.hpp:48
Wrapper class to store strings.
Definition String.hpp:101
Wrapper for text styles.
Definition TextStyle.hpp:57
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:87
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39
States used for drawing.
Definition RenderStates.hpp:39