]> git.lizzy.rs Git - dragonfireclient.git/blob - src/irrlicht_changes/static_text.cpp
StaticText/EnrichedString: Styling support (#9187)
[dragonfireclient.git] / src / irrlicht_changes / static_text.cpp
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
2 // Copyright (C) 2016 NathanaĆ«l Courant:
3 //   Modified the functions to use EnrichedText instead of string.
4 // This file is part of the "Irrlicht Engine".
5 // For conditions of distribution and use, see copyright notice in irrlicht.h
6
7 #include "static_text.h"
8 #ifdef _IRR_COMPILE_WITH_GUI_
9
10 #include <IGUIFont.h>
11 #include <IVideoDriver.h>
12 #include <rect.h>
13 #include <SColor.h>
14
15 #if USE_FREETYPE
16         #include "CGUITTFont.h"
17 #endif
18
19 #include "util/string.h"
20
21 namespace irr
22 {
23
24 #if USE_FREETYPE
25
26 namespace gui
27 {
28 //! constructor
29 StaticText::StaticText(const EnrichedString &text, bool border,
30                         IGUIEnvironment* environment, IGUIElement* parent,
31                         s32 id, const core::rect<s32>& rectangle,
32                         bool background)
33 : IGUIStaticText(environment, parent, id, rectangle),
34         HAlign(EGUIA_UPPERLEFT), VAlign(EGUIA_UPPERLEFT),
35         Border(border), WordWrap(false), Background(background),
36         RestrainTextInside(true), RightToLeft(false),
37         OverrideFont(0), LastBreakFont(0)
38 {
39         #ifdef _DEBUG
40         setDebugName("StaticText");
41         #endif
42
43         setText(text);
44 }
45
46
47 //! destructor
48 StaticText::~StaticText()
49 {
50         if (OverrideFont)
51                 OverrideFont->drop();
52 }
53
54 //! draws the element and its children
55 void StaticText::draw()
56 {
57         if (!IsVisible)
58                 return;
59
60         IGUISkin* skin = Environment->getSkin();
61         if (!skin)
62                 return;
63         video::IVideoDriver* driver = Environment->getVideoDriver();
64
65         core::rect<s32> frameRect(AbsoluteRect);
66
67         // draw background
68
69         if (Background)
70                 driver->draw2DRectangle(getBackgroundColor(), frameRect, &AbsoluteClippingRect);
71
72         // draw the border
73
74         if (Border)
75         {
76                 skin->draw3DSunkenPane(this, 0, true, false, frameRect, &AbsoluteClippingRect);
77                 frameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X);
78         }
79
80         // draw the text
81         IGUIFont *font = getActiveFont();
82         if (font && BrokenText.size()) {
83                 if (font != LastBreakFont)
84                         updateText();
85
86                 core::rect<s32> r = frameRect;
87                 s32 height_line = font->getDimension(L"A").Height + font->getKerningHeight();
88                 s32 height_total = height_line * BrokenText.size();
89                 if (VAlign == EGUIA_CENTER && WordWrap)
90                 {
91                         r.UpperLeftCorner.Y = r.getCenter().Y - (height_total / 2);
92                 }
93                 else if (VAlign == EGUIA_LOWERRIGHT)
94                 {
95                         r.UpperLeftCorner.Y = r.LowerRightCorner.Y - height_total;
96                 }
97                 if (HAlign == EGUIA_LOWERRIGHT)
98                 {
99                         r.UpperLeftCorner.X = r.LowerRightCorner.X -
100                                 getTextWidth();
101                 }
102
103                 irr::video::SColor previous_color(255, 255, 255, 255);
104                 for (const EnrichedString &str : BrokenText) {
105                         if (HAlign == EGUIA_LOWERRIGHT)
106                         {
107                                 r.UpperLeftCorner.X = frameRect.LowerRightCorner.X -
108                                         font->getDimension(str.c_str()).Width;
109                         }
110
111                         //str = colorizeText(BrokenText[i].c_str(), colors, previous_color);
112                         //if (!colors.empty())
113                         //      previous_color = colors[colors.size() - 1];
114
115 #if USE_FREETYPE
116                         if (font->getType() == irr::gui::EGFT_CUSTOM) {
117                                 irr::gui::CGUITTFont *tmp = static_cast<irr::gui::CGUITTFont*>(font);
118                                 tmp->draw(str,
119                                         r, previous_color, // FIXME
120                                         HAlign == EGUIA_CENTER, VAlign == EGUIA_CENTER,
121                                         (RestrainTextInside ? &AbsoluteClippingRect : NULL));
122                         } else
123 #endif
124                         {
125                                 // Draw non-colored text
126                                 font->draw(str.c_str(),
127                                         r, str.getDefaultColor(), // TODO: Implement colorization
128                                         HAlign == EGUIA_CENTER, VAlign == EGUIA_CENTER,
129                                         (RestrainTextInside ? &AbsoluteClippingRect : NULL));
130                         }
131
132
133                         r.LowerRightCorner.Y += height_line;
134                         r.UpperLeftCorner.Y += height_line;
135                 }
136         }
137
138         IGUIElement::draw();
139 }
140
141
142 //! Sets another skin independent font.
143 void StaticText::setOverrideFont(IGUIFont* font)
144 {
145         if (OverrideFont == font)
146                 return;
147
148         if (OverrideFont)
149                 OverrideFont->drop();
150
151         OverrideFont = font;
152
153         if (OverrideFont)
154                 OverrideFont->grab();
155
156         updateText();
157 }
158
159 //! Gets the override font (if any)
160 IGUIFont * StaticText::getOverrideFont() const
161 {
162         return OverrideFont;
163 }
164
165 //! Get the font which is used right now for drawing
166 IGUIFont* StaticText::getActiveFont() const
167 {
168         if ( OverrideFont )
169                 return OverrideFont;
170         IGUISkin* skin = Environment->getSkin();
171         if (skin)
172                 return skin->getFont();
173         return 0;
174 }
175
176 //! Sets another color for the text.
177 void StaticText::setOverrideColor(video::SColor color)
178 {
179         ColoredText.setDefaultColor(color);
180         updateText();
181 }
182
183
184 //! Sets another color for the text.
185 void StaticText::setBackgroundColor(video::SColor color)
186 {
187         ColoredText.setBackground(color);
188         Background = true;
189 }
190
191
192 //! Sets whether to draw the background
193 void StaticText::setDrawBackground(bool draw)
194 {
195         Background = draw;
196 }
197
198
199 //! Gets the background color
200 video::SColor StaticText::getBackgroundColor() const
201 {
202         IGUISkin *skin = Environment->getSkin();
203
204         return (ColoredText.hasBackground() || !skin) ?
205                 ColoredText.getBackground() : skin->getColor(gui::EGDC_3D_FACE);
206 }
207
208
209 //! Checks if background drawing is enabled
210 bool StaticText::isDrawBackgroundEnabled() const
211 {
212         return Background;
213 }
214
215
216 //! Sets whether to draw the border
217 void StaticText::setDrawBorder(bool draw)
218 {
219         Border = draw;
220 }
221
222
223 //! Checks if border drawing is enabled
224 bool StaticText::isDrawBorderEnabled() const
225 {
226         return Border;
227 }
228
229
230 void StaticText::setTextRestrainedInside(bool restrainTextInside)
231 {
232         RestrainTextInside = restrainTextInside;
233 }
234
235
236 bool StaticText::isTextRestrainedInside() const
237 {
238         return RestrainTextInside;
239 }
240
241
242 void StaticText::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
243 {
244         HAlign = horizontal;
245         VAlign = vertical;
246 }
247
248
249 #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 7
250 const video::SColor& StaticText::getOverrideColor() const
251 #else
252 video::SColor StaticText::getOverrideColor() const
253 #endif
254 {
255         return ColoredText.getDefaultColor();
256 }
257
258
259 //! Sets if the static text should use the overide color or the
260 //! color in the gui skin.
261 void StaticText::enableOverrideColor(bool enable)
262 {
263         // TODO
264 }
265
266
267 bool StaticText::isOverrideColorEnabled() const
268 {
269         return true;
270 }
271
272
273 //! Enables or disables word wrap for using the static text as
274 //! multiline text control.
275 void StaticText::setWordWrap(bool enable)
276 {
277         WordWrap = enable;
278         updateText();
279 }
280
281
282 bool StaticText::isWordWrapEnabled() const
283 {
284         return WordWrap;
285 }
286
287
288 void StaticText::setRightToLeft(bool rtl)
289 {
290         if (RightToLeft != rtl)
291         {
292                 RightToLeft = rtl;
293                 updateText();
294         }
295 }
296
297
298 bool StaticText::isRightToLeft() const
299 {
300         return RightToLeft;
301 }
302
303
304 //! Breaks the single text line.
305 // Updates the font colors
306 void StaticText::updateText()
307 {
308         const EnrichedString &cText = ColoredText;
309         BrokenText.clear();
310
311         if (cText.hasBackground()) {
312                 setBackgroundColor(cText.getBackground());
313         }
314
315         if (!WordWrap) {
316                 BrokenText.push_back(cText);
317                 return;
318         }
319
320         // Update word wrap
321
322         IGUISkin* skin = Environment->getSkin();
323         IGUIFont* font = getActiveFont();
324         if (!font)
325                 return;
326
327         LastBreakFont = font;
328
329         EnrichedString line;
330         EnrichedString word;
331         EnrichedString whitespace;
332         s32 size = cText.size();
333         s32 length = 0;
334         s32 elWidth = RelativeRect.getWidth();
335         if (Border)
336                 elWidth -= 2*skin->getSize(EGDS_TEXT_DISTANCE_X);
337         wchar_t c;
338
339         //std::vector<irr::video::SColor> colors;
340
341         // We have to deal with right-to-left and left-to-right differently
342         // However, most parts of the following code is the same, it's just
343         // some order and boundaries which change.
344         if (!RightToLeft)
345         {
346                 // regular (left-to-right)
347                 for (s32 i=0; i<size; ++i)
348                 {
349                         c = cText.getString()[i];
350                         bool lineBreak = false;
351
352                         if (c == L'\r') // Mac or Windows breaks
353                         {
354                                 lineBreak = true;
355                                 //if (Text[i+1] == L'\n') // Windows breaks
356                                 //{
357                                 //      Text.erase(i+1);
358                                 //      --size;
359                                 //}
360                                 c = '\0';
361                         }
362                         else if (c == L'\n') // Unix breaks
363                         {
364                                 lineBreak = true;
365                                 c = '\0';
366                         }
367
368                         bool isWhitespace = (c == L' ' || c == 0);
369                         if ( !isWhitespace )
370                         {
371                                 // part of a word
372                                 //word += c;
373                                 word.addChar(cText, i);
374                         }
375
376                         if ( isWhitespace || i == (size-1))
377                         {
378                                 if (word.size())
379                                 {
380                                         // here comes the next whitespace, look if
381                                         // we must break the last word to the next line.
382                                         const s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
383                                         //const std::wstring sanitized = removeEscapes(word.c_str());
384                                         const s32 wordlgth = font->getDimension(word.c_str()).Width;
385
386                                         if (wordlgth > elWidth)
387                                         {
388                                                 // This word is too long to fit in the available space, look for
389                                                 // the Unicode Soft HYphen (SHY / 00AD) character for a place to
390                                                 // break the word at
391                                                 int where = core::stringw(word.c_str()).findFirst( wchar_t(0x00AD) );
392                                                 if (where != -1)
393                                                 {
394                                                         EnrichedString first = word.substr(0, where);
395                                                         EnrichedString second = word.substr(where, word.size() - where);
396                                                         first.addCharNoColor(L'-');
397                                                         BrokenText.push_back(line + first);
398                                                         const s32 secondLength = font->getDimension(second.c_str()).Width;
399
400                                                         length = secondLength;
401                                                         line = second;
402                                                 }
403                                                 else
404                                                 {
405                                                         // No soft hyphen found, so there's nothing more we can do
406                                                         // break to next line
407                                                         if (length)
408                                                                 BrokenText.push_back(line);
409                                                         length = wordlgth;
410                                                         line = word;
411                                                 }
412                                         }
413                                         else if (length && (length + wordlgth + whitelgth > elWidth))
414                                         {
415                                                 // break to next line
416                                                 BrokenText.push_back(line);
417                                                 length = wordlgth;
418                                                 line = word;
419                                         }
420                                         else
421                                         {
422                                                 // add word to line
423                                                 line += whitespace;
424                                                 line += word;
425                                                 length += whitelgth + wordlgth;
426                                         }
427
428                                         word.clear();
429                                         whitespace.clear();
430                                 }
431
432                                 if ( isWhitespace && c != 0)
433                                 {
434                                         whitespace.addChar(cText, i);
435                                 }
436
437                                 // compute line break
438                                 if (lineBreak)
439                                 {
440                                         line += whitespace;
441                                         line += word;
442                                         BrokenText.push_back(line);
443                                         line.clear();
444                                         word.clear();
445                                         whitespace.clear();
446                                         length = 0;
447                                 }
448                         }
449                 }
450
451                 line += whitespace;
452                 line += word;
453                 BrokenText.push_back(line);
454         }
455         else
456         {
457                 // right-to-left
458                 for (s32 i=size; i>=0; --i)
459                 {
460                         c = cText.getString()[i];
461                         bool lineBreak = false;
462
463                         if (c == L'\r') // Mac or Windows breaks
464                         {
465                                 lineBreak = true;
466                                 //if ((i>0) && Text[i-1] == L'\n') // Windows breaks
467                                 //{
468                                 //      Text.erase(i-1);
469                                 //      --size;
470                                 //}
471                                 c = '\0';
472                         }
473                         else if (c == L'\n') // Unix breaks
474                         {
475                                 lineBreak = true;
476                                 c = '\0';
477                         }
478
479                         if (c==L' ' || c==0 || i==0)
480                         {
481                                 if (word.size())
482                                 {
483                                         // here comes the next whitespace, look if
484                                         // we must break the last word to the next line.
485                                         const s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
486                                         const s32 wordlgth = font->getDimension(word.c_str()).Width;
487
488                                         if (length && (length + wordlgth + whitelgth > elWidth))
489                                         {
490                                                 // break to next line
491                                                 BrokenText.push_back(line);
492                                                 length = wordlgth;
493                                                 line = word;
494                                         }
495                                         else
496                                         {
497                                                 // add word to line
498                                                 line = whitespace + line;
499                                                 line = word + line;
500                                                 length += whitelgth + wordlgth;
501                                         }
502
503                                         word.clear();
504                                         whitespace.clear();
505                                 }
506
507                                 if (c != 0)
508                                 //      whitespace = core::stringw(&c, 1) + whitespace;
509                                 whitespace = cText.substr(i, 1) + whitespace;
510
511                                 // compute line break
512                                 if (lineBreak)
513                                 {
514                                         line = whitespace + line;
515                                         line = word + line;
516                                         BrokenText.push_back(line);
517                                         line.clear();
518                                         word.clear();
519                                         whitespace.clear();
520                                         length = 0;
521                                 }
522                         }
523                         else
524                         {
525                                 // yippee this is a word..
526                                 //word = core::stringw(&c, 1) + word;
527                                 word = cText.substr(i, 1) + word;
528                         }
529                 }
530
531                 line = whitespace + line;
532                 line = word + line;
533                 BrokenText.push_back(line);
534         }
535 }
536
537
538 //! Sets the new caption of this element.
539 void StaticText::setText(const wchar_t* text)
540 {
541         setText(EnrichedString(text, getOverrideColor()));
542 }
543
544 void StaticText::setText(const EnrichedString &text)
545 {
546         ColoredText = text;
547         IGUIElement::setText(ColoredText.c_str());
548         updateText();
549 }
550
551 void StaticText::updateAbsolutePosition()
552 {
553         IGUIElement::updateAbsolutePosition();
554         updateText();
555 }
556
557
558 //! Returns the height of the text in pixels when it is drawn.
559 s32 StaticText::getTextHeight() const
560 {
561         IGUIFont* font = getActiveFont();
562         if (!font)
563                 return 0;
564
565         if (WordWrap) {
566                 s32 height = font->getDimension(L"A").Height + font->getKerningHeight();
567                 return height * BrokenText.size();
568         }
569         // There may be intentional new lines without WordWrap
570         return font->getDimension(BrokenText[0].c_str()).Height;
571 }
572
573
574 s32 StaticText::getTextWidth() const
575 {
576         IGUIFont *font = getActiveFont();
577         if (!font)
578                 return 0;
579
580         s32 widest = 0;
581
582         for (const EnrichedString &line : BrokenText) {
583                 s32 width = font->getDimension(line.c_str()).Width;
584
585                 if (width > widest)
586                         widest = width;
587         }
588
589         return widest;
590 }
591
592
593 //! Writes attributes of the element.
594 //! Implement this to expose the attributes of your element for
595 //! scripting languages, editors, debuggers or xml serialization purposes.
596 void StaticText::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
597 {
598         IGUIStaticText::serializeAttributes(out,options);
599
600         out->addBool    ("Border",              Border);
601         out->addBool    ("OverrideColorEnabled",true);
602         out->addBool    ("OverrideBGColorEnabled",ColoredText.hasBackground());
603         out->addBool    ("WordWrap",            WordWrap);
604         out->addBool    ("Background",          Background);
605         out->addBool    ("RightToLeft",         RightToLeft);
606         out->addBool    ("RestrainTextInside",  RestrainTextInside);
607         out->addColor   ("OverrideColor",       ColoredText.getDefaultColor());
608         out->addColor   ("BGColor",             ColoredText.getBackground());
609         out->addEnum    ("HTextAlign",          HAlign, GUIAlignmentNames);
610         out->addEnum    ("VTextAlign",          VAlign, GUIAlignmentNames);
611
612         // out->addFont ("OverrideFont",        OverrideFont);
613 }
614
615
616 //! Reads attributes of the element
617 void StaticText::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
618 {
619         IGUIStaticText::deserializeAttributes(in,options);
620
621         Border = in->getAttributeAsBool("Border");
622         setWordWrap(in->getAttributeAsBool("WordWrap"));
623         Background = in->getAttributeAsBool("Background");
624         RightToLeft = in->getAttributeAsBool("RightToLeft");
625         RestrainTextInside = in->getAttributeAsBool("RestrainTextInside");
626         if (in->getAttributeAsBool("OverrideColorEnabled"))
627                 ColoredText.setDefaultColor(in->getAttributeAsColor("OverrideColor"));
628         if (in->getAttributeAsBool("OverrideBGColorEnabled"))
629                 ColoredText.setBackground(in->getAttributeAsColor("BGColor"));
630
631         setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
632                       (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
633
634         // OverrideFont = in->getAttributeAsFont("OverrideFont");
635 }
636
637 } // end namespace gui
638
639 #endif // USE_FREETYPE
640
641 } // end namespace irr
642
643
644 #endif // _IRR_COMPILE_WITH_GUI_