Luanti 5.17.0-dev
Loading...
Searching...
No Matches
static_text.h
Go to the documentation of this file.
1// Copyright (C) 2002-2012 Nikolaus Gebhardt
2// Copyright (C) 2016 Nathanaƫlle Courant
3// Modified this class to work with EnrichedStrings too
4// This file is part of the "Irrlicht Engine".
5// For conditions of distribution and use, see copyright notice in irrlicht.h
6
7#pragma once
8
9#include "IGUIStaticText.h"
10
11#include "dimension2d.h"
13#include <IGUIEnvironment.h>
14
15
16namespace gui
17{
18 class StaticText final : public IGUIStaticText
19 {
20 public:
21
22 // StaticText is translated by EnrichedString.
23 // No need to use translate_string()
24 StaticText(const EnrichedString &text, bool border, IGUIEnvironment* environment,
25 IGUIElement* parent, s32 id, const core::rect<s32>& rectangle,
26 bool background = false);
27
29 virtual ~StaticText();
30
31 static gui::IGUIStaticText *add(
32 gui::IGUIEnvironment *guienv,
33 const EnrichedString &text,
34 const core::rect< s32 > &rectangle,
35 bool border = false,
36 bool wordWrap = true,
37 gui::IGUIElement *parent = NULL,
38 s32 id = -1,
39 bool fillBackground = false)
40 {
41 parent = parent ? parent : guienv->getRootGUIElement();
42 gui::IGUIStaticText *result = new gui::StaticText(
43 text, border, guienv, parent,
44 id, rectangle, fillBackground);
45
46 result->setWordWrap(wordWrap);
47 result->drop();
48 return result;
49 }
50
51 static gui::IGUIStaticText *add(
52 gui::IGUIEnvironment *guienv,
53 std::wstring_view text,
54 const core::rect< s32 > &rectangle,
55 bool border = false,
56 bool wordWrap = true,
57 gui::IGUIElement *parent = NULL,
58 s32 id = -1,
59 bool fillBackground = false)
60 {
61 return add(guienv, EnrichedString(text), rectangle, border, wordWrap, parent,
62 id, fillBackground);
63 }
64
66 virtual void draw();
67
69 virtual void setOverrideFont(IGUIFont* font=0);
70
72 virtual IGUIFont* getOverrideFont() const;
73
75 virtual IGUIFont* getActiveFont() const;
76
78 virtual void setOverrideColor(video::SColor color);
79
81 virtual void setBackgroundColor(video::SColor color);
82
84 virtual void setDrawBackground(bool draw);
85
87 virtual video::SColor getBackgroundColor() const;
88
90 virtual bool isDrawBackgroundEnabled() const;
91
93 virtual void setDrawBorder(bool draw);
94
96 virtual bool isDrawBorderEnabled() const;
97
99 virtual void setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical);
100
102 virtual video::SColor getOverrideColor() const;
103
105 virtual video::SColor getActiveColor() const;
106
109 virtual void enableOverrideColor(bool enable);
110
112 virtual bool isOverrideColorEnabled() const;
113
115 virtual void setTextRestrainedInside(bool restrainedInside);
116
118 virtual bool isTextRestrainedInside() const;
119
122 virtual void setWordWrap(bool enable);
123
125 virtual bool isWordWrapEnabled() const;
126
128 virtual void setText(const wchar_t* text);
129
131 core::dimension2du getTextDimensions() const;
132
134 virtual s32 getTextHeight() const
135 { return getTextDimensions().Height; }
136
138 virtual s32 getTextWidth() const
139 { return getTextDimensions().Width; }
140
142 virtual void updateAbsolutePosition();
143
145
150 virtual void setRightToLeft(bool rtl);
151
153 virtual bool isRightToLeft() const;
154
155 virtual bool hasType(EGUI_ELEMENT_TYPE t) const {
156 return (t == EGUIET_ENRICHED_STATIC_TEXT) || (t == EGUIET_STATIC_TEXT);
157 };
158
159 void setText(const EnrichedString &text);
160
161 private:
162
164 void updateText();
165
166 EGUI_ALIGNMENT HAlign, VAlign;
167 bool Border;
172
173 gui::IGUIFont* OverrideFont;
174 gui::IGUIFont* LastBreakFont; // stored because: if skin changes, line break must be recalculated.
175
178 };
179
180
181} // end namespace gui
182
183inline void setStaticText(gui::IGUIStaticText *static_text, const EnrichedString &text)
184{
185 // dynamic_cast not possible due to some distributions shipped
186 // without rtti support in irrlicht
187 if (static_text->hasType(gui::EGUIET_ENRICHED_STATIC_TEXT)) {
188 gui::StaticText* stext = static_cast<gui::StaticText*>(static_text);
189 stext->setText(text);
190 } else {
191 static_text->setText(text.c_str());
192 }
193}
194
195inline void setStaticText(gui::IGUIStaticText *static_text, std::wstring_view text)
196{
197 setStaticText(static_text, EnrichedString(text, static_text->getOverrideColor()));
198}
Definition enriched_string.h:27
const wchar_t * c_str() const
Definition enriched_string.h:59
Definition static_text.h:19
virtual s32 getTextHeight() const
Returns the height of the text in pixels when it is drawn.
Definition static_text.h:134
void updateText()
Breaks the single text line.
Definition static_text.cpp:263
virtual void draw()
draws the element and its children
Definition static_text.cpp:45
virtual bool isRightToLeft() const
Checks if the text should be interpreted as right-to-left text.
Definition static_text.cpp:255
virtual void setDrawBorder(bool draw)
Sets whether to draw the border.
Definition static_text.cpp:174
bool Border
Definition static_text.h:167
virtual void updateAbsolutePosition()
Updates the absolute position, splits text if word wrap is enabled.
Definition static_text.cpp:502
gui::IGUIFont * LastBreakFont
Definition static_text.h:174
bool RightToLeft
Definition static_text.h:171
virtual bool isOverrideColorEnabled() const
Checks if an override color is enabled.
Definition static_text.cpp:224
virtual void setDrawBackground(bool draw)
Sets whether to draw the background.
Definition static_text.cpp:150
virtual video::SColor getBackgroundColor() const
Gets the background color.
Definition static_text.cpp:157
core::dimension2du getTextDimensions() const
Returns the text dimensions when drawn.
Definition static_text.cpp:508
bool RestrainTextInside
Definition static_text.h:170
virtual s32 getTextWidth() const
Returns the width of the current text, in the current font.
Definition static_text.h:138
EnrichedString ColoredText
Definition static_text.h:176
virtual void setTextRestrainedInside(bool restrainedInside)
Set whether the text in this label should be clipped if it goes outside bounds.
Definition static_text.cpp:187
virtual ~StaticText()
destructor
Definition static_text.cpp:38
virtual void setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
Sets alignment mode for text.
Definition static_text.cpp:199
virtual bool isWordWrapEnabled() const
Checks if word wrap is enabled.
Definition static_text.cpp:239
gui::IGUIFont * OverrideFont
Definition static_text.h:173
virtual IGUIFont * getActiveFont() const
Get the font which is used right now for drawing.
Definition static_text.cpp:123
virtual bool hasType(EGUI_ELEMENT_TYPE t) const
Definition static_text.h:155
virtual void setRightToLeft(bool rtl)
Set whether the string should be interpreted as right-to-left (RTL) text.
Definition static_text.cpp:245
virtual bool isDrawBorderEnabled() const
Checks if border drawing is enabled.
Definition static_text.cpp:181
EGUI_ALIGNMENT VAlign
Definition static_text.h:166
virtual bool isTextRestrainedInside() const
Checks if the text in this label should be clipped if it goes outside bounds.
Definition static_text.cpp:193
virtual video::SColor getOverrideColor() const
Gets the override color.
Definition static_text.cpp:206
static gui::IGUIStaticText * add(gui::IGUIEnvironment *guienv, const EnrichedString &text, const core::rect< s32 > &rectangle, bool border=false, bool wordWrap=true, gui::IGUIElement *parent=NULL, s32 id=-1, bool fillBackground=false)
Definition static_text.h:31
virtual void setBackgroundColor(video::SColor color)
Sets another color for the background.
Definition static_text.cpp:142
virtual void setText(const wchar_t *text)
Sets the new caption of this element.
Definition static_text.cpp:490
bool WordWrap
Definition static_text.h:168
virtual video::SColor getActiveColor() const
Gets the currently used text color.
Definition static_text.cpp:211
EnrichedString BrokenText
Definition static_text.h:177
virtual void setOverrideFont(IGUIFont *font=0)
Sets another skin independent font.
Definition static_text.cpp:100
static gui::IGUIStaticText * add(gui::IGUIEnvironment *guienv, std::wstring_view text, const core::rect< s32 > &rectangle, bool border=false, bool wordWrap=true, gui::IGUIElement *parent=NULL, s32 id=-1, bool fillBackground=false)
Definition static_text.h:51
StaticText(const EnrichedString &text, bool border, IGUIEnvironment *environment, IGUIElement *parent, s32 id, const core::rect< s32 > &rectangle, bool background=false)
constructor
Definition static_text.cpp:23
virtual IGUIFont * getOverrideFont() const
Gets the override font (if any).
Definition static_text.cpp:117
bool Background
Definition static_text.h:169
virtual void setWordWrap(bool enable)
Definition static_text.cpp:232
EGUI_ALIGNMENT HAlign
Definition static_text.h:166
virtual void enableOverrideColor(bool enable)
Definition static_text.cpp:218
virtual void setOverrideColor(video::SColor color)
Sets another color for the text.
Definition static_text.cpp:134
virtual bool isDrawBackgroundEnabled() const
Checks if background drawing is enabled.
Definition static_text.cpp:167
gui::IGUIEnvironment * guienv
Definition clientlauncher.cpp:36
Definition fontengine.h:15
void setStaticText(gui::IGUIStaticText *static_text, const EnrichedString &text)
Definition static_text.h:183