--- --- 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
76 TGUI_NODISCARD static Label::Ptr create(const String& text = "");
77
85 TGUI_NODISCARD static Label::Ptr copy(const Label::ConstPtr& label);
86
91 TGUI_NODISCARD LabelRenderer* getSharedRenderer() override;
92 TGUI_NODISCARD const LabelRenderer* getSharedRenderer() const override;
93
99 TGUI_NODISCARD LabelRenderer* getRenderer() override;
100
115 void setSize(const Layout2d& size) override;
116 using Widget::setSize;
117
127 void setText(const String& text);
128
134 TGUI_NODISCARD const String& getText() const;
135
143 void setHorizontalAlignment(tgui::HorizontalAlignment alignment); // TGUI_NEXT: Remove "tgui::" prefix
144
150 TGUI_NODISCARD tgui::HorizontalAlignment getHorizontalAlignment() const; // TGUI_NEXT: Remove "tgui::" prefix
151
159 void setVerticalAlignment(tgui::VerticalAlignment alignment); // TGUI_NEXT: Remove "tgui::" prefix
160
166 TGUI_NODISCARD tgui::VerticalAlignment getVerticalAlignment() const; // TGUI_NEXT: Remove "tgui::" prefix
167
173
178 TGUI_NODISCARD Scrollbar::Policy getScrollbarPolicy() const;
179
185 void setScrollbarValue(unsigned int value);
186
192 TGUI_NODISCARD unsigned int getScrollbarValue() const;
193
201 TGUI_NODISCARD unsigned int getScrollbarMaxValue() const;
202
213 void setAutoSize(bool autoSize);
214
220 TGUI_NODISCARD bool getAutoSize() const;
221
233 void setMaximumTextWidth(float maximumWidth);
234
243 TGUI_NODISCARD float getMaximumTextWidth() const;
244
252 TGUI_DEPRECATED("Use setIgnoreMouseEvents instead") void ignoreMouseEvents(bool ignore = true);
253
259 TGUI_DEPRECATED("Use getIgnoreMouseEvents instead") TGUI_NODISCARD bool isIgnoringMouseEvents() const;
260
266 void setParent(Container* parent) override;
267
274 TGUI_NODISCARD bool canGainFocus() const override;
275
280 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
281
283 bool leftMousePressed(Vector2f pos) override;
284
286 void leftMouseReleased(Vector2f pos) override;
287
289 void mouseMoved(Vector2f pos) override;
290
292 bool scrolled(float delta, Vector2f pos, bool touch) override;
293
295 void mouseNoLongerOnWidget() override;
296
298 void leftMouseButtonNoLongerDown() override;
299
306 void draw(BackendRenderTarget& target, RenderStates states) const override;
307
309 protected:
310
320 TGUI_NODISCARD Signal& getSignal(String signalName) override;
321
327 void rendererChanged(const String& property) override;
328
332 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
333
337 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
338
342 void updateTextSize() override;
343
345 // This function is called every frame with the time passed since the last frame.
347 bool updateTime(Duration elapsedTime) override;
348
352 virtual void rearrangeText();
353
355 // Called at the end of rearrangeText() to position the text pieces that were created during rearrangeText().
357 void updateTextPiecePositions(float maxWidth);
358
360 // Makes a copy of the widget
362 TGUI_NODISCARD Widget::Ptr clone() const override;
363
365 public:
366
367 SignalString onDoubleClick = {"DoubleClicked"};
368
370 protected:
371
372 String m_string;
373 std::vector<std::vector<Text>> m_lines;
374
375 // TGUI_NEXT: Remove "tgui::" prefixes
378
379 bool m_autoSize = true;
380
381 float m_maximumTextWidth = 0;
382
383 bool m_ignoringMouseEvents = false; // TGUI_NEXT: Remove this property
384
385 // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
386 bool m_possibleDoubleClick = false;
387
389 Scrollbar::Policy m_scrollbarPolicy = Scrollbar::Policy::Automatic;
390
391 Sprite m_spriteBackground;
392
393 // Cached renderer properties
394 Borders m_bordersCached;
395 Padding m_paddingCached;
396 TextStyles m_textStyleCached;
397 Color m_textColorCached;
398 Color m_borderColorCached;
399 Color m_backgroundColorCached;
400 Color m_textOutlineColorCached;
401 float m_textOutlineThicknessCached = 0;
402
404 };
405
407}
408
410
411#endif // TGUI_LABEL_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Clickable widget.
Definition ClickableWidget.hpp:38
Wrapper for colors.
Definition Color.hpp:73
Container widget.
Definition Container.hpp:48
Definition CopiedSharedPtr.hpp:42
Wrapper for durations.
Definition Duration.hpp:55
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.
TGUI_NODISCARD unsigned int getScrollbarMaxValue() const
Returns the maximum thumb position of the scrollbar.
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:35
Class to store the position or size of a widget.
Definition Layout.hpp:313
Definition Outline.hpp:38
Policy
Defines when the scrollbar shows up.
Definition Scrollbar.hpp:53
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:61
Definition Sprite.hpp:47
Wrapper class to store strings.
Definition String.hpp:96
Wrapper for text styles.
Definition TextStyle.hpp:55
The parent class for every widget.
Definition Widget.hpp:83
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
HorizontalAlignment
The horizontal alignment.
Definition Layout.hpp:61
@ Left
Align to the left side.
VerticalAlignment
The vertical alignment.
Definition Layout.hpp:72
@ Top
Align to the top.
States used for drawing.
Definition RenderStates.hpp:38