Luanti 5.15.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 <vector>
12
14#include <IGUIEnvironment.h>
15
16
17namespace gui
18{
19 class StaticText final : public IGUIStaticText
20 {
21 public:
22
23 // StaticText is translated by EnrichedString.
24 // No need to use translate_string()
25 StaticText(const EnrichedString &text, bool border, IGUIEnvironment* environment,
26 IGUIElement* parent, s32 id, const core::rect<s32>& rectangle,
27 bool background = false);
28
30 virtual ~StaticText();
31
32 static gui::IGUIStaticText *add(
33 gui::IGUIEnvironment *guienv,
34 const EnrichedString &text,
35 const core::rect< s32 > &rectangle,
36 bool border = false,
37 bool wordWrap = true,
38 gui::IGUIElement *parent = NULL,
39 s32 id = -1,
40 bool fillBackground = false)
41 {
42 parent = parent ? parent : guienv->getRootGUIElement();
43 gui::IGUIStaticText *result = new gui::StaticText(
44 text, border, guienv, parent,
45 id, rectangle, fillBackground);
46
47 result->setWordWrap(wordWrap);
48 result->drop();
49 return result;
50 }
51
52 static gui::IGUIStaticText *add(
53 gui::IGUIEnvironment *guienv,
54 std::wstring_view text,
55 const core::rect< s32 > &rectangle,
56 bool border = false,
57 bool wordWrap = true,
58 gui::IGUIElement *parent = NULL,
59 s32 id = -1,
60 bool fillBackground = false)
61 {
62 return add(guienv, EnrichedString(text), rectangle, border, wordWrap, parent,
63 id, fillBackground);
64 }
65
67 virtual void draw();
68
70 virtual void setOverrideFont(IGUIFont* font=0);
71
73 virtual IGUIFont* getOverrideFont() const;
74
76 virtual IGUIFont* getActiveFont() const;
77
79 virtual void setOverrideColor(video::SColor color);
80
82 virtual void setBackgroundColor(video::SColor color);
83
85 virtual void setDrawBackground(bool draw);
86
88 virtual video::SColor getBackgroundColor() const;
89
91 virtual bool isDrawBackgroundEnabled() const;
92
94 virtual void setDrawBorder(bool draw);
95
97 virtual bool isDrawBorderEnabled() const;
98
100 virtual void setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical);
101
103 virtual video::SColor getOverrideColor() const;
104
106 virtual video::SColor getActiveColor() const;
107
110 virtual void enableOverrideColor(bool enable);
111
113 virtual bool isOverrideColorEnabled() const;
114
116 virtual void setTextRestrainedInside(bool restrainedInside);
117
119 virtual bool isTextRestrainedInside() const;
120
123 virtual void setWordWrap(bool enable);
124
126 virtual bool isWordWrapEnabled() const;
127
129 virtual void setText(const wchar_t* text);
130
132 virtual s32 getTextHeight() const;
133
135 virtual s32 getTextWidth() const;
136
138 virtual void updateAbsolutePosition();
139
141
146 virtual void setRightToLeft(bool rtl);
147
149 virtual bool isRightToLeft() const;
150
151 virtual bool hasType(EGUI_ELEMENT_TYPE t) const {
152 return (t == EGUIET_ENRICHED_STATIC_TEXT) || (t == EGUIET_STATIC_TEXT);
153 };
154
155 void setText(const EnrichedString &text);
156
157 private:
158
160 void updateText();
161
162 EGUI_ALIGNMENT HAlign, VAlign;
163 bool Border;
168
169 gui::IGUIFont* OverrideFont;
170 gui::IGUIFont* LastBreakFont; // stored because: if skin changes, line break must be recalculated.
171
173 std::vector<EnrichedString> BrokenText;
174 };
175
176
177} // end namespace gui
178
179inline void setStaticText(gui::IGUIStaticText *static_text, const EnrichedString &text)
180{
181 // dynamic_cast not possible due to some distributions shipped
182 // without rtti support in irrlicht
183 if (static_text->hasType(gui::EGUIET_ENRICHED_STATIC_TEXT)) {
184 gui::StaticText* stext = static_cast<gui::StaticText*>(static_text);
185 stext->setText(text);
186 } else {
187 static_text->setText(text.c_str());
188 }
189}
190
191inline void setStaticText(gui::IGUIStaticText *static_text, std::wstring_view text)
192{
193 setStaticText(static_text, EnrichedString(text, static_text->getOverrideColor()));
194}
Definition enriched_string.h:28
const wchar_t * c_str() const
Definition enriched_string.h:60
Definition static_text.h:20
void updateText()
Breaks the single text line.
Definition static_text.cpp:285
virtual void draw()
draws the element and its children
Definition static_text.cpp:42
virtual bool isRightToLeft() const
Checks if the text should be interpreted as right-to-left text.
Definition static_text.cpp:277
virtual void setDrawBorder(bool draw)
Sets whether to draw the border.
Definition static_text.cpp:196
bool Border
Definition static_text.h:163
virtual void updateAbsolutePosition()
Updates the absolute position, splits text if word wrap is enabled.
Definition static_text.cpp:531
gui::IGUIFont * LastBreakFont
Definition static_text.h:170
std::vector< EnrichedString > BrokenText
Definition static_text.h:173
bool RightToLeft
Definition static_text.h:167
virtual bool isOverrideColorEnabled() const
Checks if an override color is enabled.
Definition static_text.cpp:246
virtual void setDrawBackground(bool draw)
Sets whether to draw the background.
Definition static_text.cpp:172
virtual video::SColor getBackgroundColor() const
Gets the background color.
Definition static_text.cpp:179
bool RestrainTextInside
Definition static_text.h:166
EnrichedString ColoredText
Definition static_text.h:172
virtual void setTextRestrainedInside(bool restrainedInside)
Set whether the text in this label should be clipped if it goes outside bounds.
Definition static_text.cpp:209
virtual ~StaticText()
destructor
Definition static_text.cpp:35
virtual void setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
Sets alignment mode for text.
Definition static_text.cpp:221
virtual bool isWordWrapEnabled() const
Checks if word wrap is enabled.
Definition static_text.cpp:261
gui::IGUIFont * OverrideFont
Definition static_text.h:169
virtual IGUIFont * getActiveFont() const
Get the font which is used right now for drawing.
Definition static_text.cpp:145
virtual bool hasType(EGUI_ELEMENT_TYPE t) const
Definition static_text.h:151
virtual void setRightToLeft(bool rtl)
Set whether the string should be interpreted as right-to-left (RTL) text.
Definition static_text.cpp:267
virtual bool isDrawBorderEnabled() const
Checks if border drawing is enabled.
Definition static_text.cpp:203
EGUI_ALIGNMENT VAlign
Definition static_text.h:162
virtual bool isTextRestrainedInside() const
Checks if the text in this label should be clipped if it goes outside bounds.
Definition static_text.cpp:215
virtual video::SColor getOverrideColor() const
Gets the override color.
Definition static_text.cpp:228
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:32
virtual void setBackgroundColor(video::SColor color)
Sets another color for the background.
Definition static_text.cpp:164
virtual s32 getTextWidth() const
Returns the width of the current text, in the current font.
Definition static_text.cpp:554
virtual void setText(const wchar_t *text)
Sets the new caption of this element.
Definition static_text.cpp:519
bool WordWrap
Definition static_text.h:164
virtual video::SColor getActiveColor() const
Gets the currently used text color.
Definition static_text.cpp:233
virtual void setOverrideFont(IGUIFont *font=0)
Sets another skin independent font.
Definition static_text.cpp:122
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:52
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:20
virtual IGUIFont * getOverrideFont() const
Gets the override font (if any)
Definition static_text.cpp:139
bool Background
Definition static_text.h:165
virtual void setWordWrap(bool enable)
Enables or disables word wrap for using the static text as multiline text control.
Definition static_text.cpp:254
EGUI_ALIGNMENT HAlign
Definition static_text.h:162
virtual void enableOverrideColor(bool enable)
Sets if the static text should use the overide color or the color in the gui skin.
Definition static_text.cpp:240
virtual void setOverrideColor(video::SColor color)
Sets another color for the text.
Definition static_text.cpp:156
virtual s32 getTextHeight() const
Returns the height of the text in pixels when it is drawn.
Definition static_text.cpp:539
virtual bool isDrawBackgroundEnabled() const
Checks if background drawing is enabled.
Definition static_text.cpp:189
gui::IGUIEnvironment * guienv
Definition clientlauncher.cpp:34
Definition fontengine.h:16
void setStaticText(gui::IGUIStaticText *static_text, const EnrichedString &text)
Definition static_text.h:179