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), OverrideColorEnabled(false), OverrideBGColorEnabled(false), WordWrap(false), Background(background),
36 RestrainTextInside(true), RightToLeft(false),
37 OverrideColor(video::SColor(101,255,255,255)), BGColor(video::SColor(101,210,210,210)),
38 OverrideFont(0), LastBreakFont(0)
41 setDebugName("StaticText");
46 if (environment && environment->getSkin())
48 BGColor = environment->getSkin()->getColor(gui::EGDC_3D_FACE);
54 StaticText::~StaticText()
60 //! draws the element and its children
61 void StaticText::draw()
66 IGUISkin* skin = Environment->getSkin();
69 video::IVideoDriver* driver = Environment->getVideoDriver();
71 core::rect<s32> frameRect(AbsoluteRect);
77 if ( !OverrideBGColorEnabled ) // skin-colors can change
78 BGColor = skin->getColor(gui::EGDC_3D_FACE);
80 driver->draw2DRectangle(BGColor, frameRect, &AbsoluteClippingRect);
87 skin->draw3DSunkenPane(this, 0, true, false, frameRect, &AbsoluteClippingRect);
88 frameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X);
94 IGUIFont* font = getActiveFont();
100 // TODO: add colors here
101 if (VAlign == EGUIA_LOWERRIGHT)
103 frameRect.UpperLeftCorner.Y = frameRect.LowerRightCorner.Y -
104 font->getDimension(L"A").Height - font->getKerningHeight();
106 if (HAlign == EGUIA_LOWERRIGHT)
108 frameRect.UpperLeftCorner.X = frameRect.LowerRightCorner.X -
109 font->getDimension(cText.c_str()).Width;
112 irr::gui::CGUITTFont *tmp = static_cast<irr::gui::CGUITTFont*>(font);
113 tmp->draw(cText, frameRect,
114 OverrideColorEnabled ? OverrideColor : skin->getColor(isEnabled() ? EGDC_BUTTON_TEXT : EGDC_GRAY_TEXT),
115 HAlign == EGUIA_CENTER, VAlign == EGUIA_CENTER, (RestrainTextInside ? &AbsoluteClippingRect : NULL));
119 if (font != LastBreakFont)
122 core::rect<s32> r = frameRect;
123 s32 height = font->getDimension(L"A").Height + font->getKerningHeight();
124 s32 totalHeight = height * BrokenText.size();
125 if (VAlign == EGUIA_CENTER)
127 r.UpperLeftCorner.Y = r.getCenter().Y - (totalHeight / 2);
129 else if (VAlign == EGUIA_LOWERRIGHT)
131 r.UpperLeftCorner.Y = r.LowerRightCorner.Y - totalHeight;
134 irr::video::SColor previous_color(255, 255, 255, 255);
135 for (u32 i=0; i<BrokenText.size(); ++i)
137 if (HAlign == EGUIA_LOWERRIGHT)
139 r.UpperLeftCorner.X = frameRect.LowerRightCorner.X -
140 font->getDimension(BrokenText[i].c_str()).Width;
143 //std::vector<irr::video::SColor> colors;
145 EnrichedString str = BrokenText[i];
147 //str = colorizeText(BrokenText[i].c_str(), colors, previous_color);
148 //if (!colors.empty())
149 // previous_color = colors[colors.size() - 1];
151 irr::gui::CGUITTFont *tmp = static_cast<irr::gui::CGUITTFont*>(font);
153 previous_color, // FIXME
154 HAlign == EGUIA_CENTER, false, (RestrainTextInside ? &AbsoluteClippingRect : NULL));
156 r.LowerRightCorner.Y += height;
157 r.UpperLeftCorner.Y += height;
167 //! Sets another skin independent font.
168 void StaticText::setOverrideFont(IGUIFont* font)
170 if (OverrideFont == font)
174 OverrideFont->drop();
179 OverrideFont->grab();
184 //! Gets the override font (if any)
185 IGUIFont * StaticText::getOverrideFont() const
190 //! Get the font which is used right now for drawing
191 IGUIFont* StaticText::getActiveFont() const
195 IGUISkin* skin = Environment->getSkin();
197 return skin->getFont();
201 //! Sets another color for the text.
202 void StaticText::setOverrideColor(video::SColor color)
204 OverrideColor = color;
205 OverrideColorEnabled = true;
209 //! Sets another color for the text.
210 void StaticText::setBackgroundColor(video::SColor color)
213 OverrideBGColorEnabled = true;
218 //! Sets whether to draw the background
219 void StaticText::setDrawBackground(bool draw)
225 //! Gets the background color
226 video::SColor StaticText::getBackgroundColor() const
232 //! Checks if background drawing is enabled
233 bool StaticText::isDrawBackgroundEnabled() const
239 //! Sets whether to draw the border
240 void StaticText::setDrawBorder(bool draw)
246 //! Checks if border drawing is enabled
247 bool StaticText::isDrawBorderEnabled() const
253 void StaticText::setTextRestrainedInside(bool restrainTextInside)
255 RestrainTextInside = restrainTextInside;
259 bool StaticText::isTextRestrainedInside() const
261 return RestrainTextInside;
265 void StaticText::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
272 #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 7
273 const video::SColor& StaticText::getOverrideColor() const
275 video::SColor StaticText::getOverrideColor() const
278 return OverrideColor;
282 //! Sets if the static text should use the overide color or the
283 //! color in the gui skin.
284 void StaticText::enableOverrideColor(bool enable)
286 OverrideColorEnabled = enable;
290 bool StaticText::isOverrideColorEnabled() const
292 return OverrideColorEnabled;
296 //! Enables or disables word wrap for using the static text as
297 //! multiline text control.
298 void StaticText::setWordWrap(bool enable)
305 bool StaticText::isWordWrapEnabled() const
311 void StaticText::setRightToLeft(bool rtl)
313 if (RightToLeft != rtl)
321 bool StaticText::isRightToLeft() const
327 //! Breaks the single text line.
328 void StaticText::breakText()
335 IGUISkin* skin = Environment->getSkin();
336 IGUIFont* font = getActiveFont();
340 LastBreakFont = font;
344 EnrichedString whitespace;
345 s32 size = cText.size();
347 s32 elWidth = RelativeRect.getWidth();
349 elWidth -= 2*skin->getSize(EGDS_TEXT_DISTANCE_X);
352 //std::vector<irr::video::SColor> colors;
354 // We have to deal with right-to-left and left-to-right differently
355 // However, most parts of the following code is the same, it's just
356 // some order and boundaries which change.
359 // regular (left-to-right)
360 for (s32 i=0; i<size; ++i)
362 c = cText.getString()[i];
363 bool lineBreak = false;
365 if (c == L'\r') // Mac or Windows breaks
368 //if (Text[i+1] == L'\n') // Windows breaks
375 else if (c == L'\n') // Unix breaks
381 bool isWhitespace = (c == L' ' || c == 0);
386 word.addChar(cText, i);
389 if ( isWhitespace || i == (size-1))
393 // here comes the next whitespace, look if
394 // we must break the last word to the next line.
395 const s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
396 //const std::wstring sanitized = removeEscapes(word.c_str());
397 const s32 wordlgth = font->getDimension(word.c_str()).Width;
399 if (wordlgth > elWidth)
401 // This word is too long to fit in the available space, look for
402 // the Unicode Soft HYphen (SHY / 00AD) character for a place to
404 int where = core::stringw(word.c_str()).findFirst( wchar_t(0x00AD) );
407 EnrichedString first = word.substr(0, where);
408 EnrichedString second = word.substr(where, word.size() - where);
409 first.addCharNoColor(L'-');
410 BrokenText.push_back(line + first);
411 const s32 secondLength = font->getDimension(second.c_str()).Width;
413 length = secondLength;
418 // No soft hyphen found, so there's nothing more we can do
419 // break to next line
421 BrokenText.push_back(line);
426 else if (length && (length + wordlgth + whitelgth > elWidth))
428 // break to next line
429 BrokenText.push_back(line);
438 length += whitelgth + wordlgth;
445 if ( isWhitespace && c != 0)
447 whitespace.addChar(cText, i);
450 // compute line break
455 BrokenText.push_back(line);
466 BrokenText.push_back(line);
471 for (s32 i=size; i>=0; --i)
473 c = cText.getString()[i];
474 bool lineBreak = false;
476 if (c == L'\r') // Mac or Windows breaks
479 //if ((i>0) && Text[i-1] == L'\n') // Windows breaks
486 else if (c == L'\n') // Unix breaks
492 if (c==L' ' || c==0 || i==0)
496 // here comes the next whitespace, look if
497 // we must break the last word to the next line.
498 const s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
499 const s32 wordlgth = font->getDimension(word.c_str()).Width;
501 if (length && (length + wordlgth + whitelgth > elWidth))
503 // break to next line
504 BrokenText.push_back(line);
511 line = whitespace + line;
513 length += whitelgth + wordlgth;
521 // whitespace = core::stringw(&c, 1) + whitespace;
522 whitespace = cText.substr(i, 1) + whitespace;
524 // compute line break
527 line = whitespace + line;
529 BrokenText.push_back(line);
538 // yippee this is a word..
539 //word = core::stringw(&c, 1) + word;
540 word = cText.substr(i, 1) + word;
544 line = whitespace + line;
546 BrokenText.push_back(line);
551 //! Sets the new caption of this element.
552 void StaticText::setText(const wchar_t* text)
554 setText(EnrichedString(text));
557 //! Sets the new caption of this element.
558 void StaticText::setText(const EnrichedString &text)
560 IGUIElement::setText(text.c_str());
562 if (text.hasBackground()) {
563 setBackgroundColor(text.getBackground());
569 void StaticText::updateAbsolutePosition()
571 IGUIElement::updateAbsolutePosition();
576 //! Returns the height of the text in pixels when it is drawn.
577 s32 StaticText::getTextHeight() const
579 IGUIFont* font = getActiveFont();
583 s32 height = font->getDimension(L"A").Height + font->getKerningHeight();
586 height *= BrokenText.size();
592 s32 StaticText::getTextWidth() const
594 IGUIFont * font = getActiveFont();
602 for(u32 line = 0; line < BrokenText.size(); ++line)
604 s32 width = font->getDimension(BrokenText[line].c_str()).Width;
614 return font->getDimension(cText.c_str()).Width;
619 //! Writes attributes of the element.
620 //! Implement this to expose the attributes of your element for
621 //! scripting languages, editors, debuggers or xml serialization purposes.
622 void StaticText::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
624 IGUIStaticText::serializeAttributes(out,options);
626 out->addBool ("Border", Border);
627 out->addBool ("OverrideColorEnabled",OverrideColorEnabled);
628 out->addBool ("OverrideBGColorEnabled",OverrideBGColorEnabled);
629 out->addBool ("WordWrap", WordWrap);
630 out->addBool ("Background", Background);
631 out->addBool ("RightToLeft", RightToLeft);
632 out->addBool ("RestrainTextInside", RestrainTextInside);
633 out->addColor ("OverrideColor", OverrideColor);
634 out->addColor ("BGColor", BGColor);
635 out->addEnum ("HTextAlign", HAlign, GUIAlignmentNames);
636 out->addEnum ("VTextAlign", VAlign, GUIAlignmentNames);
638 // out->addFont ("OverrideFont", OverrideFont);
642 //! Reads attributes of the element
643 void StaticText::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
645 IGUIStaticText::deserializeAttributes(in,options);
647 Border = in->getAttributeAsBool("Border");
648 enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
649 OverrideBGColorEnabled = in->getAttributeAsBool("OverrideBGColorEnabled");
650 setWordWrap(in->getAttributeAsBool("WordWrap"));
651 Background = in->getAttributeAsBool("Background");
652 RightToLeft = in->getAttributeAsBool("RightToLeft");
653 RestrainTextInside = in->getAttributeAsBool("RestrainTextInside");
654 OverrideColor = in->getAttributeAsColor("OverrideColor");
655 BGColor = in->getAttributeAsColor("BGColor");
657 setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
658 (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
660 // OverrideFont = in->getAttributeAsFont("OverrideFont");
663 } // end namespace gui
665 #endif // USE_FREETYPE
667 } // end namespace irr
670 #endif // _IRR_COMPILE_WITH_GUI_