--- --- TGUI: include/TGUI/Widgets/Label.hpp Source File
TGUI  1.x-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#ifndef TGUI_LABEL_HPP
26#define TGUI_LABEL_HPP
27
28#include <TGUI/Widgets/ClickableWidget.hpp>
29#include <TGUI/Renderers/LabelRenderer.hpp>
30#include <TGUI/CopiedSharedPtr.hpp>
31#include <TGUI/Widgets/Scrollbar.hpp>
32#include <TGUI/Text.hpp>
33
35
36TGUI_MODULE_EXPORT namespace tgui
37{
41 class TGUI_API Label : public ClickableWidget
42 {
43 public:
44
45 using Ptr = std::shared_ptr<Label>;
46 using ConstPtr = std::shared_ptr<const Label>;
47
48 static constexpr const char StaticWidgetType[] = "Label";
49
53 using HorizontalAlignment TGUI_DEPRECATED("Use tgui::HorizontalAlignment instead") = tgui::HorizontalAlignment;
54
58 using VerticalAlignment TGUI_DEPRECATED("Use tgui::VerticalAlignment instead") = tgui::VerticalAlignment;
59
67 Label(const char* typeName = StaticWidgetType, bool initRenderer = true);
68
77 TGUI_NODISCARD static Label::Ptr create(const String& text = "");
78
87 TGUI_NODISCARD static Label::Ptr copy(const Label::ConstPtr& label);
88
93 TGUI_NODISCARD LabelRenderer* getSharedRenderer() override;
94 TGUI_NODISCARD const LabelRenderer* getSharedRenderer() const override;
95
101 TGUI_NODISCARD LabelRenderer* getRenderer() override;
102
118 void setSize(const Layout2d& size) override;
119 using Widget::setSize;
120
131 void setText(const String& text);
132
139 TGUI_NODISCARD const String& getText() const;
140
149 void setHorizontalAlignment(tgui::HorizontalAlignment alignment); // TGUI_NEXT: Remove "tgui::" prefix
150
157 TGUI_NODISCARD tgui::HorizontalAlignment getHorizontalAlignment() const; // TGUI_NEXT: Remove "tgui::" prefix
158
167 void setVerticalAlignment(tgui::VerticalAlignment alignment); // TGUI_NEXT: Remove "tgui::" prefix
168
175 TGUI_NODISCARD tgui::VerticalAlignment getVerticalAlignment() const; // TGUI_NEXT: Remove "tgui::" prefix
176
182
187 TGUI_NODISCARD Scrollbar::Policy getScrollbarPolicy() const;
188
194 void setScrollbarValue(unsigned int value);
195
201 TGUI_NODISCARD unsigned int getScrollbarValue() const;
202
214 void setAutoSize(bool autoSize);
215
222 TGUI_NODISCARD bool getAutoSize() const;
223
236 void setMaximumTextWidth(float maximumWidth);
237
247 TGUI_NODISCARD float getMaximumTextWidth() const;
248
256 TGUI_DEPRECATED("Use setIgnoreMouseEvents instead") void ignoreMouseEvents(bool ignore = true);
257
263 TGUI_DEPRECATED("Use getIgnoreMouseEvents instead") TGUI_NODISCARD bool isIgnoringMouseEvents() const;
264
270 void setParent(Container* parent) override;
271
278 TGUI_NODISCARD bool canGainFocus() const override;
279
284 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
285
287 bool leftMousePressed(Vector2f pos) override;
288
290 void leftMouseReleased(Vector2f pos) override;
291
293 void mouseMoved(Vector2f pos) override;
294
296 bool scrolled(float delta, Vector2f pos, bool touch) override;
297
299 void mouseNoLongerOnWidget() override;
300
302 void leftMouseButtonNoLongerDown() override;
303
311 void draw(BackendRenderTarget& target, RenderStates states) const override;
312
314 protected:
315
325 TGUI_NODISCARD Signal& getSignal(String signalName) override;
326
333 void rendererChanged(const String& property) override;
334
338 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
339
343 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
344
348 void updateTextSize() override;
349
351 // This function is called every frame with the time passed since the last frame.
353 bool updateTime(Duration elapsedTime) override;
354
358 virtual void rearrangeText();
359
361 // Called at the end of rearrangeText() to position the text pieces that were created during rearrangeText().
363 void updateTextPiecePositions(float maxWidth);
364
366 // Makes a copy of the widget
368 TGUI_NODISCARD Widget::Ptr clone() const override;
369
371 public:
372
373 SignalString onDoubleClick = {"DoubleClicked"};
374
376 protected:
377
378 String m_string;
379 std::vector<std::vector<Text>> m_lines;
380
381 // TGUI_NEXT: Remove "tgui::" prefixes
384
385 bool m_autoSize = true;
386
387 float m_maximumTextWidth = 0;
388
389 bool m_ignoringMouseEvents = false; // TGUI_NEXT: Remove this property
390
391 // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
392 bool m_possibleDoubleClick = false;
393
395 Scrollbar::Policy m_scrollbarPolicy = Scrollbar::Policy::Automatic;
396
397 Sprite m_spriteBackground;
398
399 // Cached renderer properties
400 Borders m_bordersCached;
401 Padding m_paddingCached;
402 TextStyles m_textStyleCached;
403 Color m_textColorCached;
404 Color m_borderColorCached;
405 Color m_backgroundColorCached;
406 Color m_textOutlineColorCached;
407 float m_textOutlineThicknessCached = 0;
408
410 };
411
413}
414
416
417#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:74
Container widget.
Definition Container.hpp:49
Definition CopiedSharedPtr.hpp:45
Wrapper for durations.
Definition Duration.hpp:56
Label widget.
Definition Label.hpp:42
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.
TGUI_NODISCARD tgui::HorizontalAlignment getHorizontalAlignment() const
Gets the current horizontal text alignment.
TGUI_NODISCARD Scrollbar::Policy getScrollbarPolicy() const
Returns when the vertical scrollbar should be displayed.
TGUI_NODISCARD tgui::VerticalAlignment getVerticalAlignment() const
Gets the current vertical text alignment.
std::shared_ptr< const Label > ConstPtr
Shared constant widget pointer.
Definition Label.hpp:46
void setScrollbarValue(unsigned int value)
Changes the thumb position of the scrollbar.
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 const String & getText() const
Returns the text.
void setHorizontalAlignment(tgui::HorizontalAlignment alignment)
Changes the horizontal text alignment.
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.
void setVerticalAlignment(tgui::VerticalAlignment alignment)
Changes the vertical text alignment.
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:45
Definition LabelRenderer.hpp:37
Class to store the position or size of a widget.
Definition Layout.hpp:328
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
The parent class for every widget.
Definition Widget.hpp:84
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39
HorizontalAlignment
The horizontal alignment.
Definition Layout.hpp:52
@ Left
Align to the left side.
VerticalAlignment
The vertical alignment.
Definition Layout.hpp:64
@ Top
Align to the top.
States used for drawing.
Definition RenderStates.hpp:39