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();
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)
267 bool StaticText::isOverrideColorEnabled() const
273 //! Enables or disables word wrap for using the static text as
274 //! multiline text control.
275 void StaticText::setWordWrap(bool enable)
282 bool StaticText::isWordWrapEnabled() const
288 void StaticText::setRightToLeft(bool rtl)
290 if (RightToLeft != rtl)
298 bool StaticText::isRightToLeft() const
304 //! Breaks the single text line.
305 // Updates the font colors
306 void StaticText::updateText()
308 const EnrichedString &cText = ColoredText;
311 if (cText.hasBackground()) {
312 setBackgroundColor(cText.getBackground());
316 BrokenText.push_back(cText);
322 IGUISkin* skin = Environment->getSkin();
323 IGUIFont* font = getActiveFont();
327 LastBreakFont = font;
331 EnrichedString whitespace;
332 s32 size = cText.size();
334 s32 elWidth = RelativeRect.getWidth();
336 elWidth -= 2*skin->getSize(EGDS_TEXT_DISTANCE_X);
339 //std::vector<irr::video::SColor> colors;
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.
346 // regular (left-to-right)
347 for (s32 i=0; i<size; ++i)
349 c = cText.getString()[i];
350 bool lineBreak = false;
352 if (c == L'\r') // Mac or Windows breaks
355 //if (Text[i+1] == L'\n') // Windows breaks
362 else if (c == L'\n') // Unix breaks
368 bool isWhitespace = (c == L' ' || c == 0);
373 word.addChar(cText, i);
376 if ( isWhitespace || i == (size-1))
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;
386 if (wordlgth > elWidth)
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
391 int where = core::stringw(word.c_str()).findFirst( wchar_t(0x00AD) );
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;
400 length = secondLength;
405 // No soft hyphen found, so there's nothing more we can do
406 // break to next line
408 BrokenText.push_back(line);
413 else if (length && (length + wordlgth + whitelgth > elWidth))
415 // break to next line
416 BrokenText.push_back(line);
425 length += whitelgth + wordlgth;
432 if ( isWhitespace && c != 0)
434 whitespace.addChar(cText, i);
437 // compute line break
442 BrokenText.push_back(line);
453 BrokenText.push_back(line);
458 for (s32 i=size; i>=0; --i)
460 c = cText.getString()[i];
461 bool lineBreak = false;
463 if (c == L'\r') // Mac or Windows breaks
466 //if ((i>0) && Text[i-1] == L'\n') // Windows breaks
473 else if (c == L'\n') // Unix breaks
479 if (c==L' ' || c==0 || i==0)
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;
488 if (length && (length + wordlgth + whitelgth > elWidth))
490 // break to next line
491 BrokenText.push_back(line);
498 line = whitespace + line;
500 length += whitelgth + wordlgth;
508 // whitespace = core::stringw(&c, 1) + whitespace;
509 whitespace = cText.substr(i, 1) + whitespace;
511 // compute line break
514 line = whitespace + line;
516 BrokenText.push_back(line);
525 // yippee this is a word..
526 //word = core::stringw(&c, 1) + word;
527 word = cText.substr(i, 1) + word;
531 line = whitespace + line;
533 BrokenText.push_back(line);
538 //! Sets the new caption of this element.
539 void StaticText::setText(const wchar_t* text)
541 setText(EnrichedString(text, getOverrideColor()));
544 void StaticText::setText(const EnrichedString &text)
547 IGUIElement::setText(ColoredText.c_str());
551 void StaticText::updateAbsolutePosition()
553 IGUIElement::updateAbsolutePosition();
558 //! Returns the height of the text in pixels when it is drawn.
559 s32 StaticText::getTextHeight() const
561 IGUIFont* font = getActiveFont();
566 s32 height = font->getDimension(L"A").Height + font->getKerningHeight();
567 return height * BrokenText.size();
569 // There may be intentional new lines without WordWrap
570 return font->getDimension(BrokenText[0].c_str()).Height;
574 s32 StaticText::getTextWidth() const
576 IGUIFont *font = getActiveFont();
582 for (const EnrichedString &line : BrokenText) {
583 s32 width = font->getDimension(line.c_str()).Width;
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
598 IGUIStaticText::serializeAttributes(out,options);
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);
612 // out->addFont ("OverrideFont", OverrideFont);
616 //! Reads attributes of the element
617 void StaticText::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
619 IGUIStaticText::deserializeAttributes(in,options);
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"));
631 setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
632 (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
634 // OverrideFont = in->getAttributeAsFont("OverrideFont");
637 } // end namespace gui
639 #endif // USE_FREETYPE
641 } // end namespace irr
644 #endif // _IRR_COMPILE_WITH_GUI_