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