Skylicht Engine
Loading...
Searching...
No Matches
CGUIText.h
1/*
2!@
3MIT License
4
5Copyright (c) 2019 Skylicht Technology CO., LTD
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
8(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
9merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
10subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20This file is part of the "Skylicht Engine".
21https://github.com/skylicht-lab/skylicht-engine
22!#
23*/
24
25#pragma once
26
27#include "CGUIElement.h"
28#include "Graphics2D/SpriteFrame/IFont.h"
29
30#define MAX_FORMATCOLOR 32
31
32#define HAVE_CARET
33
34namespace Skylicht
35{
36 class CFontSource;
37
59 class SKYLICHT_API CGUIText : public CGUIElement
60 {
61 friend class CCanvas;
62 public:
63 typedef std::vector<int> ArrayInt;
64 typedef std::vector<SModuleOffset*> ArrayModuleOffset;
65
66 protected:
67 IFont* m_font;
68 IFont* m_customfont;
69
70 int m_charPadding;
71 int m_charSpacePadding;
72 int m_linePadding;
73
74 bool m_strim;
75
76 std::string m_defaultText;
77
78 std::string m_text;
79 std::wstring m_textw;
80 ArrayInt m_textFormat;
81
82 bool m_enableTextFormat;
83 bool m_password;
84
85 std::string m_textId;
86 std::wstring m_textwId;
87
88 EGUIVerticalAlign TextVertical;
89 EGUIHorizontalAlign TextHorizontal;
90
91 int m_textHeight;
92 int m_textOffsetY;
93
94 bool m_multiLine;
95 bool m_centerRotate;
96
97 SColor m_colorFormat[MAX_FORMATCOLOR];
98
99 bool m_updateTextRender;
100
101 std::vector<ArrayModuleOffset> m_arrayCharRender;
102 std::vector<ArrayInt> m_arrayCharFormat;
103 std::vector<ArrayInt> m_arrayCharId;
104
105 std::string m_fontSource;
106 std::string m_fontGUID;
107
108 CFontSource* m_fontData;
109 int m_fontChanged;
110
111 float m_lastWidth;
112 float m_lastHeight;
113
114#ifdef HAVE_CARET
115 bool m_showCaret;
116 int m_caretShader;
117 int m_setCaret;
118 float m_caretBlink;
119 float m_caretBlinkSpeed;
120 core::vector2di m_caret;
121#endif
122
123 protected:
124 CGUIText(CCanvas* canvas, CGUIElement* parent, IFont* font);
125 CGUIText(CCanvas* canvas, CGUIElement* parent, const core::rectf& rect, IFont* font);
126
127 virtual void renderText(ArrayModuleOffset& string, ArrayInt& format, int posX, int posY, int line);
128
129 void init();
130
131 public:
132 virtual ~CGUIText();
133
134 virtual void render(CCamera* camera);
135
136 void setTextAlign(EGUIHorizontalAlign h, EGUIVerticalAlign v)
137 {
138 TextVertical = v;
139 TextHorizontal = h;
140 }
141
142 /*
143 * Example:
144 * <1>PLAYERA <0>gives <2>a heart <0>to <1>PLAYERB
145 * With format:
146 * <1> -> RED
147 * <0> -> WHITE
148 * <2> -> YELLOW
149 */
150 void setFormatText(const char* formatText);
151
152 // setColorFormat
153 // set color for function setFormatText
154 void setColorFormat(int id, const SColor& c)
155 {
156 if (id < MAX_FORMATCOLOR)
157 m_colorFormat[id] = c;
158 }
159
160 void setText(const char* text);
161 void setTextStrim(const char* text);
162 void setText(const wchar_t* text);
163 void setTextStrim(const wchar_t* text);
164
165 void setTextId(const char* textId);
166 void setTextId(const wchar_t* textId);
167
168 int getStringWidth(const char* text);
169 int getStringWidth(const wchar_t* text);
170 int getCharWidth(wchar_t c);
171
172 const char* getText()
173 {
174 return m_text.c_str();
175 }
176
177 const char* getDefaultText()
178 {
179 return m_defaultText.c_str();
180 }
181
182 void setDefaultText(const char* text);
183
184 void setDefaultText(const wchar_t* text);
185
186 const wchar_t* getTextW()
187 {
188 return m_textw.c_str();
189 }
190
191 const char* getTextId()
192 {
193 return m_textId.c_str();
194 }
195
196 const wchar_t* getTextwId()
197 {
198 return m_textwId.c_str();
199 }
200
201 inline int getTextLength()
202 {
203 return (int)m_textw.size();
204 }
205
206 inline bool isStrim()
207 {
208 return m_strim;
209 }
210
211 inline void setEnableTextFormnat(bool b)
212 {
213 m_enableTextFormat = b;
214 }
215
216 inline bool isEnableTextFormat()
217 {
218 return m_enableTextFormat;
219 }
220
221 void setPassword(bool b);
222
223 inline bool isPassword()
224 {
225 return m_password;
226 }
227
228 inline void setCharPadding(int charPadding)
229 {
230 m_charPadding = charPadding;
231 m_charSpacePadding = charPadding;
232 }
233
234 inline void setLinePadding(int linePadding)
235 {
236 m_linePadding = linePadding;
237 }
238
239 inline void setMultiLine(bool b)
240 {
241 m_multiLine = b;
242 }
243
244 inline void setCenterRotate(bool b)
245 {
246 m_centerRotate = b;
247 }
248
249 void setFontSource(const char* fontSource);
250
251 void setFontSource(CFontSource* fontData);
252
253 void setCustomFont(IFont* font);
254
255 inline IFont* getCustomFont()
256 {
257 return m_customfont;
258 }
259
260 inline CFontSource* getFontData()
261 {
262 return m_fontData;
263 }
264
265 const std::string& getFontSource()
266 {
267 return m_fontSource;
268 }
269
270 const std::string& getFontId()
271 {
272 return m_fontGUID;
273 }
274
275 DECLARE_GETTYPENAME(CGUIText)
276
277 virtual CObjectSerializable* createSerializable();
278
279 virtual void loadSerializable(CObjectSerializable* object);
280
281 void getClosestCharacter(float posX, float posY, int& line, int& character);
282
283#ifdef HAVE_CARET
284 inline void showCaret(bool b)
285 {
286 m_showCaret = b;
287 }
288
289 inline void setCaret(int line, int character)
290 {
291 m_caret.set(character, line);
292 m_caretBlink = 0.0f;
293 }
294
295 void setCaret(int charPos);
296
297 inline void getCaretPosition(int& line, int& character)
298 {
299 character = m_caret.X;
300 line = m_caret.Y;
301 }
302
303 inline int getNumLine()
304 {
305 return (int)m_arrayCharRender.size();
306 }
307
308 int getNumCharacter(int line);
309
310 bool getWordAtPosition(int line, int charPosition, int& from, int& to);
311
312 bool isCharacter(wchar_t c);
313
314 void doBackspace();
315
316 void doDelete();
317
318 void insert(wchar_t c);
319#endif
320
321 protected:
322
323#ifdef HAVE_CARET
324 void updateCaret();
325
326 void updateSetCaret();
327#endif
328
329 void updateSplitText();
330
331 void splitText(std::vector<ArrayModuleOffset>& split, std::vector<ArrayInt>& format, std::vector<ArrayInt>& id, int width);
332
333 void getClosestCharacter(float posX, int x, int y, int line, const core::matrix4& world, int& character);
334
335 wchar_t getDisplayCharacter(wchar_t c) const;
336
337 void initFont(IFont* font);
338
339 IFont* getCurrentFont();
340 };
341}
This is an object class used to set up the camera, including its position, viewing angle,...
Definition CCamera.h:70
This is the object class that describes .font file information.
Definition CFontSource.h:77
Definition CObjectSerializable.h:36
Definition IFont.h:32
Class representing a 32 bit ARGB color.
Definition SColor.h:285
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29
CMatrix4< f32 > matrix4
Typedef for f32 matrix.
Definition matrix4.h:2241
vector2d< s32 > vector2di
Typedef for integer 2d vector.
Definition vector2d.h:326
rect< f32 > rectf
Rectangle with float values.
Definition rect.h:271