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