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 video::SColor StaticText::getOverrideColor() const
251 return ColoredText.getDefaultColor();
254 #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR > 8
255 video::SColor StaticText::getActiveColor() const
257 return getOverrideColor();
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)
269 bool StaticText::isOverrideColorEnabled() const
275 //! Enables or disables word wrap for using the static text as
276 //! multiline text control.
277 void StaticText::setWordWrap(bool enable)
284 bool StaticText::isWordWrapEnabled() const
290 void StaticText::setRightToLeft(bool rtl)
292 if (RightToLeft != rtl)
300 bool StaticText::isRightToLeft() const
306 //! Breaks the single text line.
307 // Updates the font colors
308 void StaticText::updateText()
310 const EnrichedString &cText = ColoredText;
313 if (cText.hasBackground())
314 setBackgroundColor(cText.getBackground());
316 setDrawBackground(false);
319 BrokenText.push_back(cText);
325 IGUISkin* skin = Environment->getSkin();
326 IGUIFont* font = getActiveFont();
330 LastBreakFont = font;
334 EnrichedString whitespace;
335 s32 size = cText.size();
337 s32 elWidth = RelativeRect.getWidth();
339 elWidth -= 2*skin->getSize(EGDS_TEXT_DISTANCE_X);
342 //std::vector<irr::video::SColor> colors;
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.
349 // regular (left-to-right)
350 for (s32 i=0; i<size; ++i)
352 c = cText.getString()[i];
353 bool lineBreak = false;
355 if (c == L'\r') // Mac or Windows breaks
358 //if (Text[i+1] == L'\n') // Windows breaks
365 else if (c == L'\n') // Unix breaks
371 bool isWhitespace = (c == L' ' || c == 0);
376 word.addChar(cText, i);
379 if ( isWhitespace || i == (size-1))
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;
389 if (wordlgth > elWidth)
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
394 int where = core::stringw(word.c_str()).findFirst( wchar_t(0x00AD) );
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;
403 length = secondLength;
408 // No soft hyphen found, so there's nothing more we can do
409 // break to next line
411 BrokenText.push_back(line);
416 else if (length && (length + wordlgth + whitelgth > elWidth))
418 // break to next line
419 BrokenText.push_back(line);
428 length += whitelgth + wordlgth;
435 if ( isWhitespace && c != 0)
437 whitespace.addChar(cText, i);
440 // compute line break
445 BrokenText.push_back(line);
456 BrokenText.push_back(line);
461 for (s32 i=size; i>=0; --i)
463 c = cText.getString()[i];
464 bool lineBreak = false;
466 if (c == L'\r') // Mac or Windows breaks
469 //if ((i>0) && Text[i-1] == L'\n') // Windows breaks
476 else if (c == L'\n') // Unix breaks
482 if (c==L' ' || c==0 || i==0)
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;
491 if (length && (length + wordlgth + whitelgth > elWidth))
493 // break to next line
494 BrokenText.push_back(line);
501 line = whitespace + line;
503 length += whitelgth + wordlgth;
511 // whitespace = core::stringw(&c, 1) + whitespace;
512 whitespace = cText.substr(i, 1) + whitespace;
514 // compute line break
517 line = whitespace + line;
519 BrokenText.push_back(line);
528 // yippee this is a word..
529 //word = core::stringw(&c, 1) + word;
530 word = cText.substr(i, 1) + word;
534 line = whitespace + line;
536 BrokenText.push_back(line);
541 //! Sets the new caption of this element.
542 void StaticText::setText(const wchar_t* text)
544 setText(EnrichedString(text, getOverrideColor()));
547 void StaticText::setText(const EnrichedString &text)
550 IGUIElement::setText(ColoredText.c_str());
554 void StaticText::updateAbsolutePosition()
556 IGUIElement::updateAbsolutePosition();
561 //! Returns the height of the text in pixels when it is drawn.
562 s32 StaticText::getTextHeight() const
564 IGUIFont* font = getActiveFont();
569 s32 height = font->getDimension(L"A").Height + font->getKerningHeight();
570 return height * BrokenText.size();
572 // There may be intentional new lines without WordWrap
573 return font->getDimension(BrokenText[0].c_str()).Height;
577 s32 StaticText::getTextWidth() const
579 IGUIFont *font = getActiveFont();
585 for (const EnrichedString &line : BrokenText) {
586 s32 width = font->getDimension(line.c_str()).Width;
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
601 IGUIStaticText::serializeAttributes(out,options);
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);
615 // out->addFont ("OverrideFont", OverrideFont);
619 //! Reads attributes of the element
620 void StaticText::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
622 IGUIStaticText::deserializeAttributes(in,options);
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"));
634 setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
635 (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
637 // OverrideFont = in->getAttributeAsFont("OverrideFont");
640 } // end namespace gui
642 #endif // USE_FREETYPE
644 } // end namespace irr
647 #endif // _IRR_COMPILE_WITH_GUI_