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