TGUI  1.0-alpha
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 typedef std::shared_ptr<Label> Ptr;
48 typedef std::shared_ptr<const Label> ConstPtr;
49
54 {
55 Left,
56 Center,
57 Right
58 };
59
60
65 {
66 Top ,
67 Center,
68 Bottom
69 };
70
71
79 Label(const char* typeName = "Label", bool initRenderer = true);
80
81
90 static Label::Ptr create(String text = "");
91
92
102
103
109 const LabelRenderer* getSharedRenderer() const;
110
117
118
134 void setSize(const Layout2d& size) override;
135 using Widget::setSize;
136
137
148 void setText(const String& text);
149
150
157 const String& getText() const;
158
159
169
170
178
179
189
190
198
199
205
206
212
213
219 void setScrollbarValue(unsigned int value);
220
221
227 unsigned int getScrollbarValue() const;
228
229
241 void setAutoSize(bool autoSize);
242
243
250 bool getAutoSize() const;
251
252
265 void setMaximumTextWidth(float maximumWidth);
266
267
277 float getMaximumTextWidth() const;
278
279
287 void ignoreMouseEvents(bool ignore = true);
288
289
296
297
303 void setParent(Container* parent) override;
304
305
312 bool canGainFocus() const override;
313
314
319 bool isMouseOnWidget(Vector2f pos) const override;
320
321
323 void leftMousePressed(Vector2f pos) override;
324
326 void leftMouseReleased(Vector2f pos) override;
327
329 void mouseMoved(Vector2f pos) override;
330
332 bool mouseWheelScrolled(float delta, Vector2f pos) override;
333
335 void mouseNoLongerOnWidget() override;
336
338 void leftMouseButtonNoLongerDown() override;
339
340
348 void draw(BackendRenderTarget& target, RenderStates states) const override;
349
350
352 protected:
353
363 Signal& getSignal(String signalName) override;
364
365
372 void rendererChanged(const String& property) override;
373
374
378 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
379
380
384 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
385
386
390 void updateTextSize() override;
391
392
394 // This function is called every frame with the time passed since the last frame.
396 bool updateTime(Duration elapsedTime) override;
397
398
400 // Rearrange the text, making use of the given size of maximum text width.
402 void rearrangeText();
403
404
406 // Makes a copy of the widget
408 Widget::Ptr clone() const override;
409
410
412 public:
413
414 SignalString onDoubleClick = {"DoubleClicked"};
415
416
418 protected:
419
420 String m_string;
421 std::vector<Text> m_lines;
422
423 HorizontalAlignment m_horizontalAlignment = HorizontalAlignment::Left;
424 VerticalAlignment m_verticalAlignment = VerticalAlignment::Top;
425
426 bool m_autoSize = true;
427
428 float m_maximumTextWidth = 0;
429
430 bool m_ignoringMouseEvents = false;
431
432 // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
433 bool m_possibleDoubleClick = false;
434
437
438 Sprite m_spriteBackground;
439
440 // Cached renderer properties
441 Borders m_bordersCached;
442 Padding m_paddingCached;
443 TextStyles m_textStyleCached;
444 Color m_textColorCached;
445 Color m_borderColorCached;
446 Color m_backgroundColorCached;
447 Color m_textOutlineColorCached;
448 float m_textOutlineThicknessCached = 0;
449
451 };
452
454}
455
457
458#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:65
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.
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.
void setVerticalAlignment(VerticalAlignment alignment)
Changes the vertical text alignment.
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.
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.
void setText(const String &text)
Changes the text.
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.
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(String text="")
Creates a new label widget.
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...
void setHorizontalAlignment(HorizontalAlignment alignment)
Changes the horizontal text alignment.
HorizontalAlignment
The horizontal text alignment.
Definition: Label.hpp:54
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:50
@ Automatic
Show the scrollbar only when needed (default)
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
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
States used for drawing.
Definition: RenderStates.hpp:39