]> git.lizzy.rs Git - dragonfireclient.git/blob - src/irrlicht_changes/static_text.cpp
Irrlicht support code maintenance
[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 video::SColor StaticText::getOverrideColor() const
250 {
251         return ColoredText.getDefaultColor();
252 }
253
254 #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR > 8
255 video::SColor StaticText::getActiveColor() const
256 {
257         return getOverrideColor();
258 }
259 #endif
260
261 //! Sets if the static text should use the overide color or the
262 //! color in the gui skin.
263 void StaticText::enableOverrideColor(bool enable)
264 {
265         // TODO
266 }
267
268
269 bool StaticText::isOverrideColorEnabled() const
270 {
271         return true;
272 }
273
274
275 //! Enables or disables word wrap for using the static text as
276 //! multiline text control.
277 void StaticText::setWordWrap(bool enable)
278 {
279         WordWrap = enable;
280         updateText();
281 }
282
283
284 bool StaticText::isWordWrapEnabled() const
285 {
286         return WordWrap;
287 }
288
289
290 void StaticText::setRightToLeft(bool rtl)
291 {
292         if (RightToLeft != rtl)
293         {
294                 RightToLeft = rtl;
295                 updateText();
296         }
297 }
298
299
300 bool StaticText::isRightToLeft() const
301 {
302         return RightToLeft;
303 }
304
305
306 //! Breaks the single text line.
307 // Updates the font colors
308 void StaticText::updateText()
309 {
310         const EnrichedString &cText = ColoredText;
311         BrokenText.clear();
312
313         if (cText.hasBackground())
314                 setBackgroundColor(cText.getBackground());
315         else
316                 setDrawBackground(false);
317
318         if (!WordWrap) {
319                 BrokenText.push_back(cText);
320                 return;
321         }
322
323         // Update word wrap
324
325         IGUISkin* skin = Environment->getSkin();
326         IGUIFont* font = getActiveFont();
327         if (!font)
328                 return;
329
330         LastBreakFont = font;
331
332         EnrichedString line;
333         EnrichedString word;
334         EnrichedString whitespace;
335         s32 size = cText.size();
336         s32 length = 0;
337         s32 elWidth = RelativeRect.getWidth();
338         if (Border)
339                 elWidth -= 2*skin->getSize(EGDS_TEXT_DISTANCE_X);
340         wchar_t c;
341
342         //std::vector<irr::video::SColor> colors;
343
344         // We have to deal with right-to-left and left-to-right differently
345         // However, most parts of the following code is the same, it's just
346         // some order and boundaries which change.
347         if (!RightToLeft)
348         {
349                 // regular (left-to-right)
350                 for (s32 i=0; i<size; ++i)
351                 {
352                         c = cText.getString()[i];
353                         bool lineBreak = false;
354
355                         if (c == L'\r') // Mac or Windows breaks
356                         {
357                                 lineBreak = true;
358                                 //if (Text[i+1] == L'\n') // Windows breaks
359                                 //{
360                                 //      Text.erase(i+1);
361                                 //      --size;
362                                 //}
363                                 c = '\0';
364                         }
365                         else if (c == L'\n') // Unix breaks
366                         {
367                                 lineBreak = true;
368                                 c = '\0';
369                         }
370
371                         bool isWhitespace = (c == L' ' || c == 0);
372                         if ( !isWhitespace )
373                         {
374                                 // part of a word
375                                 //word += c;
376                                 word.addChar(cText, i);
377                         }
378
379                         if ( isWhitespace || i == (size-1))
380                         {
381                                 if (word.size())
382                                 {
383                                         // here comes the next whitespace, look if
384                                         // we must break the last word to the next line.
385                                         const s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
386                                         //const std::wstring sanitized = removeEscapes(word.c_str());
387                                         const s32 wordlgth = font->getDimension(word.c_str()).Width;
388
389                                         if (wordlgth > elWidth)
390                                         {
391                                                 // This word is too long to fit in the available space, look for
392                                                 // the Unicode Soft HYphen (SHY / 00AD) character for a place to
393                                                 // break the word at
394                                                 int where = core::stringw(word.c_str()).findFirst( wchar_t(0x00AD) );
395                                                 if (where != -1)
396                                                 {
397                                                         EnrichedString first = word.substr(0, where);
398                                                         EnrichedString second = word.substr(where, word.size() - where);
399                                                         first.addCharNoColor(L'-');
400                                                         BrokenText.push_back(line + first);
401                                                         const s32 secondLength = font->getDimension(second.c_str()).Width;
402
403                                                         length = secondLength;
404                                                         line = second;
405                                                 }
406                                                 else
407                                                 {
408                                                         // No soft hyphen found, so there's nothing more we can do
409                                                         // break to next line
410                                                         if (length)
411                                                                 BrokenText.push_back(line);
412                                                         length = wordlgth;
413                                                         line = word;
414                                                 }
415                                         }
416                                         else if (length && (length + wordlgth + whitelgth > elWidth))
417                                         {
418                                                 // break to next line
419                                                 BrokenText.push_back(line);
420                                                 length = wordlgth;
421                                                 line = word;
422                                         }
423                                         else
424                                         {
425                                                 // add word to line
426                                                 line += whitespace;
427                                                 line += word;
428                                                 length += whitelgth + wordlgth;
429                                         }
430
431                                         word.clear();
432                                         whitespace.clear();
433                                 }
434
435                                 if ( isWhitespace && c != 0)
436                                 {
437                                         whitespace.addChar(cText, i);
438                                 }
439
440                                 // compute line break
441                                 if (lineBreak)
442                                 {
443                                         line += whitespace;
444                                         line += word;
445                                         BrokenText.push_back(line);
446                                         line.clear();
447                                         word.clear();
448                                         whitespace.clear();
449                                         length = 0;
450                                 }
451                         }
452                 }
453
454                 line += whitespace;
455                 line += word;
456                 BrokenText.push_back(line);
457         }
458         else
459         {
460                 // right-to-left
461                 for (s32 i=size; i>=0; --i)
462                 {
463                         c = cText.getString()[i];
464                         bool lineBreak = false;
465
466                         if (c == L'\r') // Mac or Windows breaks
467                         {
468                                 lineBreak = true;
469                                 //if ((i>0) && Text[i-1] == L'\n') // Windows breaks
470                                 //{
471                                 //      Text.erase(i-1);
472                                 //      --size;
473                                 //}
474                                 c = '\0';
475                         }
476                         else if (c == L'\n') // Unix breaks
477                         {
478                                 lineBreak = true;
479                                 c = '\0';
480                         }
481
482                         if (c==L' ' || c==0 || i==0)
483                         {
484                                 if (word.size())
485                                 {
486                                         // here comes the next whitespace, look if
487                                         // we must break the last word to the next line.
488                                         const s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
489                                         const s32 wordlgth = font->getDimension(word.c_str()).Width;
490
491                                         if (length && (length + wordlgth + whitelgth > elWidth))
492                                         {
493                                                 // break to next line
494                                                 BrokenText.push_back(line);
495                                                 length = wordlgth;
496                                                 line = word;
497                                         }
498                                         else
499                                         {
500                                                 // add word to line
501                                                 line = whitespace + line;
502                                                 line = word + line;
503                                                 length += whitelgth + wordlgth;
504                                         }
505
506                                         word.clear();
507                                         whitespace.clear();
508                                 }
509
510                                 if (c != 0)
511                                 //      whitespace = core::stringw(&c, 1) + whitespace;
512                                 whitespace = cText.substr(i, 1) + whitespace;
513
514                                 // compute line break
515                                 if (lineBreak)
516                                 {
517                                         line = whitespace + line;
518                                         line = word + line;
519                                         BrokenText.push_back(line);
520                                         line.clear();
521                                         word.clear();
522                                         whitespace.clear();
523                                         length = 0;
524                                 }
525                         }
526                         else
527                         {
528                                 // yippee this is a word..
529                                 //word = core::stringw(&c, 1) + word;
530                                 word = cText.substr(i, 1) + word;
531                         }
532                 }
533
534                 line = whitespace + line;
535                 line = word + line;
536                 BrokenText.push_back(line);
537         }
538 }
539
540
541 //! Sets the new caption of this element.
542 void StaticText::setText(const wchar_t* text)
543 {
544         setText(EnrichedString(text, getOverrideColor()));
545 }
546
547 void StaticText::setText(const EnrichedString &text)
548 {
549         ColoredText = text;
550         IGUIElement::setText(ColoredText.c_str());
551         updateText();
552 }
553
554 void StaticText::updateAbsolutePosition()
555 {
556         IGUIElement::updateAbsolutePosition();
557         updateText();
558 }
559
560
561 //! Returns the height of the text in pixels when it is drawn.
562 s32 StaticText::getTextHeight() const
563 {
564         IGUIFont* font = getActiveFont();
565         if (!font)
566                 return 0;
567
568         if (WordWrap) {
569                 s32 height = font->getDimension(L"A").Height + font->getKerningHeight();
570                 return height * BrokenText.size();
571         }
572         // There may be intentional new lines without WordWrap
573         return font->getDimension(BrokenText[0].c_str()).Height;
574 }
575
576
577 s32 StaticText::getTextWidth() const
578 {
579         IGUIFont *font = getActiveFont();
580         if (!font)
581                 return 0;
582
583         s32 widest = 0;
584
585         for (const EnrichedString &line : BrokenText) {
586                 s32 width = font->getDimension(line.c_str()).Width;
587
588                 if (width > widest)
589                         widest = width;
590         }
591
592         return widest;
593 }
594
595
596 //! Writes attributes of the element.
597 //! Implement this to expose the attributes of your element for
598 //! scripting languages, editors, debuggers or xml serialization purposes.
599 void StaticText::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
600 {
601         IGUIStaticText::serializeAttributes(out,options);
602
603         out->addBool    ("Border",              Border);
604         out->addBool    ("OverrideColorEnabled",true);
605         out->addBool    ("OverrideBGColorEnabled",ColoredText.hasBackground());
606         out->addBool    ("WordWrap",            WordWrap);
607         out->addBool    ("Background",          Background);
608         out->addBool    ("RightToLeft",         RightToLeft);
609         out->addBool    ("RestrainTextInside",  RestrainTextInside);
610         out->addColor   ("OverrideColor",       ColoredText.getDefaultColor());
611         out->addColor   ("BGColor",             ColoredText.getBackground());
612         out->addEnum    ("HTextAlign",          HAlign, GUIAlignmentNames);
613         out->addEnum    ("VTextAlign",          VAlign, GUIAlignmentNames);
614
615         // out->addFont ("OverrideFont",        OverrideFont);
616 }
617
618
619 //! Reads attributes of the element
620 void StaticText::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
621 {
622         IGUIStaticText::deserializeAttributes(in,options);
623
624         Border = in->getAttributeAsBool("Border");
625         setWordWrap(in->getAttributeAsBool("WordWrap"));
626         Background = in->getAttributeAsBool("Background");
627         RightToLeft = in->getAttributeAsBool("RightToLeft");
628         RestrainTextInside = in->getAttributeAsBool("RestrainTextInside");
629         if (in->getAttributeAsBool("OverrideColorEnabled"))
630                 ColoredText.setDefaultColor(in->getAttributeAsColor("OverrideColor"));
631         if (in->getAttributeAsBool("OverrideBGColorEnabled"))
632                 ColoredText.setBackground(in->getAttributeAsColor("BGColor"));
633
634         setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
635                       (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
636
637         // OverrideFont = in->getAttributeAsFont("OverrideFont");
638 }
639
640 } // end namespace gui
641
642 #endif // USE_FREETYPE
643
644 } // end namespace irr
645
646
647 #endif // _IRR_COMPILE_WITH_GUI_