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
7 #include "static_text.h"
8 #ifdef _IRR_COMPILE_WITH_GUI_
11 #include <IVideoDriver.h>
16 #include "CGUITTFont.h"
19 #include "util/string.h"
29 StaticText::StaticText(const EnrichedString &text, bool border,
30 IGUIEnvironment* environment, IGUIElement* parent,
31 s32 id, const core::rect<s32>& rectangle,
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)
40 setDebugName("StaticText");
48 StaticText::~StaticText()
54 //! draws the element and its children
55 void StaticText::draw()
60 IGUISkin* skin = Environment->getSkin();
63 video::IVideoDriver* driver = Environment->getVideoDriver();
65 core::rect<s32> frameRect(AbsoluteRect);
70 driver->draw2DRectangle(getBackgroundColor(), frameRect, &AbsoluteClippingRect);
76 skin->draw3DSunkenPane(this, 0, true, false, frameRect, &AbsoluteClippingRect);
77 frameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X);
81 IGUIFont *font = getActiveFont();
82 if (font && BrokenText.size()) {
83 if (font != LastBreakFont)
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)
91 r.UpperLeftCorner.Y = r.getCenter().Y - (height_total / 2);
93 else if (VAlign == EGUIA_LOWERRIGHT)
95 r.UpperLeftCorner.Y = r.LowerRightCorner.Y - height_total;
97 if (HAlign == EGUIA_LOWERRIGHT)
99 r.UpperLeftCorner.X = r.LowerRightCorner.X -
103 irr::video::SColor previous_color(255, 255, 255, 255);
104 for (const EnrichedString &str : BrokenText) {
105 if (HAlign == EGUIA_LOWERRIGHT)
107 r.UpperLeftCorner.X = frameRect.LowerRightCorner.X -
108 font->getDimension(str.c_str()).Width;
111 //str = colorizeText(BrokenText[i].c_str(), colors, previous_color);
112 //if (!colors.empty())
113 // previous_color = colors[colors.size() - 1];
116 if (font->getType() == irr::gui::EGFT_CUSTOM) {
117 irr::gui::CGUITTFont *tmp = static_cast<irr::gui::CGUITTFont*>(font);
119 r, previous_color, // FIXME
120 HAlign == EGUIA_CENTER, VAlign == EGUIA_CENTER,
121 (RestrainTextInside ? &AbsoluteClippingRect : NULL));
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));
133 r.LowerRightCorner.Y += height_line;
134 r.UpperLeftCorner.Y += height_line;
142 //! Sets another skin independent font.
143 void StaticText::setOverrideFont(IGUIFont* font)
145 if (OverrideFont == font)
149 OverrideFont->drop();
154 OverrideFont->grab();
159 //! Gets the override font (if any)
160 IGUIFont * StaticText::getOverrideFont() const
165 //! Get the font which is used right now for drawing
166 IGUIFont* StaticText::getActiveFont() const
170 IGUISkin* skin = Environment->getSkin();
172 return skin->getFont();
176 //! Sets another color for the text.
177 void StaticText::setOverrideColor(video::SColor color)
179 ColoredText.setDefaultColor(color);
184 //! Sets another color for the text.
185 void StaticText::setBackgroundColor(video::SColor color)
187 ColoredText.setBackground(color);
192 //! Sets whether to draw the background
193 void StaticText::setDrawBackground(bool draw)
199 //! Gets the background color
200 video::SColor StaticText::getBackgroundColor() const
202 IGUISkin *skin = Environment->getSkin();
204 return (ColoredText.hasBackground() || !skin) ?
205 ColoredText.getBackground() : skin->getColor(gui::EGDC_3D_FACE);
209 //! Checks if background drawing is enabled
210 bool StaticText::isDrawBackgroundEnabled() const
216 //! Sets whether to draw the border
217 void StaticText::setDrawBorder(bool draw)
223 //! Checks if border drawing is enabled
224 bool StaticText::isDrawBorderEnabled() const
230 void StaticText::setTextRestrainedInside(bool restrainTextInside)
232 RestrainTextInside = restrainTextInside;
236 bool StaticText::isTextRestrainedInside() const
238 return RestrainTextInside;
242 void StaticText::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
249 #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 7
250 const video::SColor& StaticText::getOverrideColor() const
252 video::SColor StaticText::getOverrideColor() const
255 return ColoredText.getDefaultColor();
258 #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR > 8
259 video::SColor StaticText::getActiveColor() const
261 return getOverrideColor();
265 //! Sets if the static text should use the overide color or the
266 //! color in the gui skin.
267 void StaticText::enableOverrideColor(bool enable)
273 bool StaticText::isOverrideColorEnabled() const
279 //! Enables or disables word wrap for using the static text as
280 //! multiline text control.
281 void StaticText::setWordWrap(bool enable)
288 bool StaticText::isWordWrapEnabled() const
294 void StaticText::setRightToLeft(bool rtl)
296 if (RightToLeft != rtl)
304 bool StaticText::isRightToLeft() const
310 //! Breaks the single text line.
311 // Updates the font colors
312 void StaticText::updateText()
314 const EnrichedString &cText = ColoredText;
317 if (cText.hasBackground())
318 setBackgroundColor(cText.getBackground());
320 setDrawBackground(false);
323 BrokenText.push_back(cText);
329 IGUISkin* skin = Environment->getSkin();
330 IGUIFont* font = getActiveFont();
334 LastBreakFont = font;
338 EnrichedString whitespace;
339 s32 size = cText.size();
341 s32 elWidth = RelativeRect.getWidth();
343 elWidth -= 2*skin->getSize(EGDS_TEXT_DISTANCE_X);
346 //std::vector<irr::video::SColor> colors;
348 // We have to deal with right-to-left and left-to-right differently
349 // However, most parts of the following code is the same, it's just
350 // some order and boundaries which change.
353 // regular (left-to-right)
354 for (s32 i=0; i<size; ++i)
356 c = cText.getString()[i];
357 bool lineBreak = false;
359 if (c == L'\r') // Mac or Windows breaks
362 //if (Text[i+1] == L'\n') // Windows breaks
369 else if (c == L'\n') // Unix breaks
375 bool isWhitespace = (c == L' ' || c == 0);
380 word.addChar(cText, i);
383 if ( isWhitespace || i == (size-1))
387 // here comes the next whitespace, look if
388 // we must break the last word to the next line.
389 const s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
390 //const std::wstring sanitized = removeEscapes(word.c_str());
391 const s32 wordlgth = font->getDimension(word.c_str()).Width;
393 if (wordlgth > elWidth)
395 // This word is too long to fit in the available space, look for
396 // the Unicode Soft HYphen (SHY / 00AD) character for a place to
398 int where = core::stringw(word.c_str()).findFirst( wchar_t(0x00AD) );
401 EnrichedString first = word.substr(0, where);
402 EnrichedString second = word.substr(where, word.size() - where);
403 first.addCharNoColor(L'-');
404 BrokenText.push_back(line + first);
405 const s32 secondLength = font->getDimension(second.c_str()).Width;
407 length = secondLength;
412 // No soft hyphen found, so there's nothing more we can do
413 // break to next line
415 BrokenText.push_back(line);
420 else if (length && (length + wordlgth + whitelgth > elWidth))
422 // break to next line
423 BrokenText.push_back(line);
432 length += whitelgth + wordlgth;
439 if ( isWhitespace && c != 0)
441 whitespace.addChar(cText, i);
444 // compute line break
449 BrokenText.push_back(line);
460 BrokenText.push_back(line);
465 for (s32 i=size; i>=0; --i)
467 c = cText.getString()[i];
468 bool lineBreak = false;
470 if (c == L'\r') // Mac or Windows breaks
473 //if ((i>0) && Text[i-1] == L'\n') // Windows breaks
480 else if (c == L'\n') // Unix breaks
486 if (c==L' ' || c==0 || i==0)
490 // here comes the next whitespace, look if
491 // we must break the last word to the next line.
492 const s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
493 const s32 wordlgth = font->getDimension(word.c_str()).Width;
495 if (length && (length + wordlgth + whitelgth > elWidth))
497 // break to next line
498 BrokenText.push_back(line);
505 line = whitespace + line;
507 length += whitelgth + wordlgth;
515 // whitespace = core::stringw(&c, 1) + whitespace;
516 whitespace = cText.substr(i, 1) + whitespace;
518 // compute line break
521 line = whitespace + line;
523 BrokenText.push_back(line);
532 // yippee this is a word..
533 //word = core::stringw(&c, 1) + word;
534 word = cText.substr(i, 1) + word;
538 line = whitespace + line;
540 BrokenText.push_back(line);
545 //! Sets the new caption of this element.
546 void StaticText::setText(const wchar_t* text)
548 setText(EnrichedString(text, getOverrideColor()));
551 void StaticText::setText(const EnrichedString &text)
554 IGUIElement::setText(ColoredText.c_str());
558 void StaticText::updateAbsolutePosition()
560 IGUIElement::updateAbsolutePosition();
565 //! Returns the height of the text in pixels when it is drawn.
566 s32 StaticText::getTextHeight() const
568 IGUIFont* font = getActiveFont();
573 s32 height = font->getDimension(L"A").Height + font->getKerningHeight();
574 return height * BrokenText.size();
576 // There may be intentional new lines without WordWrap
577 return font->getDimension(BrokenText[0].c_str()).Height;
581 s32 StaticText::getTextWidth() const
583 IGUIFont *font = getActiveFont();
589 for (const EnrichedString &line : BrokenText) {
590 s32 width = font->getDimension(line.c_str()).Width;
600 //! Writes attributes of the element.
601 //! Implement this to expose the attributes of your element for
602 //! scripting languages, editors, debuggers or xml serialization purposes.
603 void StaticText::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
605 IGUIStaticText::serializeAttributes(out,options);
607 out->addBool ("Border", Border);
608 out->addBool ("OverrideColorEnabled",true);
609 out->addBool ("OverrideBGColorEnabled",ColoredText.hasBackground());
610 out->addBool ("WordWrap", WordWrap);
611 out->addBool ("Background", Background);
612 out->addBool ("RightToLeft", RightToLeft);
613 out->addBool ("RestrainTextInside", RestrainTextInside);
614 out->addColor ("OverrideColor", ColoredText.getDefaultColor());
615 out->addColor ("BGColor", ColoredText.getBackground());
616 out->addEnum ("HTextAlign", HAlign, GUIAlignmentNames);
617 out->addEnum ("VTextAlign", VAlign, GUIAlignmentNames);
619 // out->addFont ("OverrideFont", OverrideFont);
623 //! Reads attributes of the element
624 void StaticText::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
626 IGUIStaticText::deserializeAttributes(in,options);
628 Border = in->getAttributeAsBool("Border");
629 setWordWrap(in->getAttributeAsBool("WordWrap"));
630 Background = in->getAttributeAsBool("Background");
631 RightToLeft = in->getAttributeAsBool("RightToLeft");
632 RestrainTextInside = in->getAttributeAsBool("RestrainTextInside");
633 if (in->getAttributeAsBool("OverrideColorEnabled"))
634 ColoredText.setDefaultColor(in->getAttributeAsColor("OverrideColor"));
635 if (in->getAttributeAsBool("OverrideBGColorEnabled"))
636 ColoredText.setBackground(in->getAttributeAsColor("BGColor"));
638 setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
639 (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
641 // OverrideFont = in->getAttributeAsFont("OverrideFont");
644 } // end namespace gui
646 #endif // USE_FREETYPE
648 } // end namespace irr
651 #endif // _IRR_COMPILE_WITH_GUI_