]> git.lizzy.rs Git - dragonfireclient.git/blob - src/irrlicht_changes/static_text.h
Make HUD status messages translatable (#5795)
[dragonfireclient.git] / src / irrlicht_changes / static_text.h
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
2 // Copyright (C) 2016 NathanaĆ«l 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 "IrrCompileConfig.h"
10 #ifdef _IRR_COMPILE_WITH_GUI_
11
12 #include "IGUIStaticText.h"
13 #include "irrArray.h"
14
15 #include "log.h"
16
17 #include <vector>
18
19 #include "util/enriched_string.h"
20 #include "config.h"
21 #include <IGUIEnvironment.h>
22
23 #if USE_FREETYPE
24
25 namespace irr
26 {
27
28 namespace gui
29 {
30
31         const EGUI_ELEMENT_TYPE EGUIET_ENRICHED_STATIC_TEXT = (EGUI_ELEMENT_TYPE)(0x1000);
32
33         class StaticText : public IGUIStaticText
34         {
35         public:
36
37                 //! constructor
38                 StaticText(const EnrichedString &text, bool border, IGUIEnvironment* environment,
39                         IGUIElement* parent, s32 id, const core::rect<s32>& rectangle,
40                         bool background = false);
41
42                 //! destructor
43                 virtual ~StaticText();
44
45                 //! draws the element and its children
46                 virtual void draw();
47
48                 //! Sets another skin independent font.
49                 virtual void setOverrideFont(IGUIFont* font=0);
50
51                 //! Gets the override font (if any)
52                 virtual IGUIFont* getOverrideFont() const;
53
54                 //! Get the font which is used right now for drawing
55                 virtual IGUIFont* getActiveFont() const;
56
57                 //! Sets another color for the text.
58                 virtual void setOverrideColor(video::SColor color);
59
60                 //! Sets another color for the background.
61                 virtual void setBackgroundColor(video::SColor color);
62
63                 //! Sets whether to draw the background
64                 virtual void setDrawBackground(bool draw);
65
66                 //! Gets the background color
67                 virtual video::SColor getBackgroundColor() const;
68
69                 //! Checks if background drawing is enabled
70                 virtual bool isDrawBackgroundEnabled() const;
71
72                 //! Sets whether to draw the border
73                 virtual void setDrawBorder(bool draw);
74
75                 //! Checks if border drawing is enabled
76                 virtual bool isDrawBorderEnabled() const;
77
78                 //! Sets alignment mode for text
79                 virtual void setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical);
80
81                 //! Gets the override color
82                 #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 7
83                 virtual const video::SColor& getOverrideColor() const;
84                 #else
85                 virtual video::SColor getOverrideColor() const;
86                 #endif
87
88                 //! Sets if the static text should use the overide color or the
89                 //! color in the gui skin.
90                 virtual void enableOverrideColor(bool enable);
91
92                 //! Checks if an override color is enabled
93                 virtual bool isOverrideColorEnabled() const;
94
95                 //! Set whether the text in this label should be clipped if it goes outside bounds
96                 virtual void setTextRestrainedInside(bool restrainedInside);
97
98                 //! Checks if the text in this label should be clipped if it goes outside bounds
99                 virtual bool isTextRestrainedInside() const;
100
101                 //! Enables or disables word wrap for using the static text as
102                 //! multiline text control.
103                 virtual void setWordWrap(bool enable);
104
105                 //! Checks if word wrap is enabled
106                 virtual bool isWordWrapEnabled() const;
107
108                 //! Sets the new caption of this element.
109                 virtual void setText(const wchar_t* text);
110
111                 //! Returns the height of the text in pixels when it is drawn.
112                 virtual s32 getTextHeight() const;
113
114                 //! Returns the width of the current text, in the current font
115                 virtual s32 getTextWidth() const;
116
117                 //! Updates the absolute position, splits text if word wrap is enabled
118                 virtual void updateAbsolutePosition();
119
120                 //! Set whether the string should be interpreted as right-to-left (RTL) text
121                 /** \note This component does not implement the Unicode bidi standard, the
122                 text of the component should be already RTL if you call this. The
123                 main difference when RTL is enabled is that the linebreaks for multiline
124                 elements are performed starting from the end.
125                 */
126                 virtual void setRightToLeft(bool rtl);
127
128                 //! Checks if the text should be interpreted as right-to-left text
129                 virtual bool isRightToLeft() const;
130
131                 //! Writes attributes of the element.
132                 virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const;
133
134                 //! Reads attributes of the element
135                 virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options);
136
137                 virtual bool hasType(EGUI_ELEMENT_TYPE t) const {
138                         return (t == EGUIET_ENRICHED_STATIC_TEXT) || (t == EGUIET_STATIC_TEXT);
139                 };
140
141                 virtual bool hasType(EGUI_ELEMENT_TYPE t) {
142                         return (t == EGUIET_ENRICHED_STATIC_TEXT) || (t == EGUIET_STATIC_TEXT);
143                 };
144
145                 void setText(const EnrichedString &text);
146
147         private:
148
149                 //! Breaks the single text line.
150                 void breakText();
151
152                 EGUI_ALIGNMENT HAlign, VAlign;
153                 bool Border;
154                 bool OverrideColorEnabled;
155                 bool OverrideBGColorEnabled;
156                 bool WordWrap;
157                 bool Background;
158                 bool RestrainTextInside;
159                 bool RightToLeft;
160
161                 video::SColor OverrideColor, BGColor;
162                 gui::IGUIFont* OverrideFont;
163                 gui::IGUIFont* LastBreakFont; // stored because: if skin changes, line break must be recalculated.
164
165                 EnrichedString cText;
166                 core::array< EnrichedString > BrokenText;
167         };
168
169
170 } // end namespace gui
171
172 } // end namespace irr
173
174 inline irr::gui::IGUIStaticText *addStaticText(
175                 irr::gui::IGUIEnvironment *guienv,
176                 const EnrichedString &text,
177                 const core::rect< s32 > &rectangle,
178                 bool border = false,
179                 bool wordWrap = true,
180                 irr::gui::IGUIElement *parent = NULL,
181                 s32 id = -1,
182                 bool fillBackground = false)
183 {
184         if (parent == NULL) {
185                 // parent is NULL, so we must find one, or we need not to drop
186                 // result, but then there will be a memory leak.
187                 //
188                 // What Irrlicht does is to use guienv as a parent, but the problem
189                 // is that guienv is here only an IGUIEnvironment, while it is a
190                 // CGUIEnvironment in Irrlicht, which inherits from both IGUIElement
191                 // and IGUIEnvironment.
192                 //
193                 // A solution would be to dynamic_cast guienv to a
194                 // IGUIElement*, but Irrlicht is shipped without rtti support
195                 // in some distributions, causing the dymanic_cast to segfault.
196                 //
197                 // Thus, to find the parent, we create a dummy StaticText and ask
198                 // for its parent, and then remove it.
199                 irr::gui::IGUIStaticText *dummy_text =
200                         guienv->addStaticText(L"", rectangle, border, wordWrap,
201                         parent, id, fillBackground);
202                 parent = dummy_text->getParent();
203                 dummy_text->remove();
204         }
205         irr::gui::IGUIStaticText *result = new irr::gui::StaticText(
206                 text, border, guienv, parent,
207                 id, rectangle, fillBackground);
208
209         result->setWordWrap(wordWrap);
210         result->drop();
211         return result;
212 }
213
214 inline void setStaticText(irr::gui::IGUIStaticText *static_text, const EnrichedString &text)
215 {
216         // dynamic_cast not possible due to some distributions shipped
217         // without rtti support in irrlicht
218         if (static_text->hasType(irr::gui::EGUIET_ENRICHED_STATIC_TEXT)) {
219                 irr::gui::StaticText* stext = static_cast<irr::gui::StaticText*>(static_text);
220                 stext->setText(text);
221         } else {
222                 static_text->setText(text.c_str());
223         }
224 }
225
226 #else // USE_FREETYPE
227
228 inline irr::gui::IGUIStaticText *addStaticText(
229                 irr::gui::IGUIEnvironment *guienv,
230                 const EnrichedString &text,
231                 const core::rect< s32 > &rectangle,
232                 bool border = false,
233                 bool wordWrap = true,
234                 irr::gui::IGUIElement *parent = NULL,
235                 s32 id = -1,
236                 bool fillBackground = false)
237 {
238         return guienv->addStaticText(text.c_str(), rectangle, border, wordWrap, parent, id, fillBackground);
239 }
240
241 inline void setStaticText(irr::gui::IGUIStaticText *static_text, const EnrichedString &text)
242 {
243         static_text->setText(text.c_str());
244 }
245
246 #endif
247
248 inline irr::gui::IGUIStaticText *addStaticText(
249                 irr::gui::IGUIEnvironment *guienv,
250                 const wchar_t *text,
251                 const core::rect< s32 > &rectangle,
252                 bool border = false,
253                 bool wordWrap = true,
254                 irr::gui::IGUIElement *parent = NULL,
255                 s32 id = -1,
256                 bool fillBackground = false) {
257         return addStaticText(guienv, EnrichedString(text), rectangle, border, wordWrap, parent, id, fillBackground);
258 }
259
260 inline void setStaticText(irr::gui::IGUIStaticText *static_text, const wchar_t *text)
261 {
262         setStaticText(static_text, EnrichedString(text));
263 }
264
265 #endif // _IRR_COMPILE_WITH_GUI_