#include "CGUIStaticText.h"
#ifdef _IRR_COMPILE_WITH_GUI_
#include "IGUISkin.h"
#include "IGUIEnvironment.h"
#include "IGUIFont.h"
#include "IVideoDriver.h"
#include "rect.h"
namespace irr
{
namespace gui
{
CGUIStaticText::CGUIStaticText(const wchar_t* text, bool border,
IGUIEnvironment* environment, IGUIElement* parent,
s32 id, const core::rect<s32>& rectangle,
bool background)
: IGUIStaticText(environment, parent, id, rectangle),
HAlign(EGUIA_UPPERLEFT), VAlign(EGUIA_UPPERLEFT),
Border(border), OverrideColorEnabled(false), OverrideBGColorEnabled(false), WordWrap(false), Background(background),
OverrideColor(video::SColor(101,255,255,255)), BGColor(video::SColor(101,210,210,210)),
OverrideFont(0), LastBreakFont(0)
{
#ifdef _DEBUG
setDebugName("CGUIStaticText");
#endif
Text = text;
RestrainTextInside = true;
if (environment && environment->getSkin())
{
BGColor = environment->getSkin()->getColor(gui::EGDC_3D_FACE);
}
}
CGUIStaticText::~CGUIStaticText()
{
if (OverrideFont)
OverrideFont->drop();
}
void CGUIStaticText::draw()
{
if (!IsVisible)
return;
IGUISkin* skin = Environment->getSkin();
if (!skin)
return;
video::IVideoDriver* driver = Environment->getVideoDriver();
core::rect<s32> frameRect(AbsoluteRect);
if (Background)
{
if ( !OverrideBGColorEnabled )
BGColor = skin->getColor(gui::EGDC_3D_FACE);
driver->draw2DRectangle(BGColor, frameRect, &AbsoluteClippingRect);
}
if (Border)
{
skin->draw3DSunkenPane(this, 0, true, false, frameRect, &AbsoluteClippingRect);
frameRect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X);
}
if (Text.size())
{
IGUIFont* font = OverrideFont;
if (!OverrideFont)
font = skin->getFont();
if (font)
{
if (!WordWrap)
{
if (VAlign == EGUIA_LOWERRIGHT)
{
frameRect.UpperLeftCorner.Y = frameRect.LowerRightCorner.Y -
font->getDimension(L"A").Height - font->getKerningHeight();
}
if (HAlign == EGUIA_LOWERRIGHT)
{
frameRect.UpperLeftCorner.X = frameRect.LowerRightCorner.X -
font->getDimension(Text.c_str()).Width;
}
font->draw(Text.c_str(), frameRect,
OverrideColorEnabled ? OverrideColor : skin->getColor(isEnabled() ? EGDC_BUTTON_TEXT : EGDC_GRAY_TEXT),
HAlign == EGUIA_CENTER, VAlign == EGUIA_CENTER, (RestrainTextInside ? &AbsoluteClippingRect : NULL));
}
else
{
if (font != LastBreakFont)
breakText();
core::rect<s32> r = frameRect;
s32 height = font->getDimension(L"A").Height + font->getKerningHeight();
s32 totalHeight = height * BrokenText.size();
if (VAlign == EGUIA_CENTER)
{
r.UpperLeftCorner.Y = r.getCenter().Y - (totalHeight / 2);
}
else if (VAlign == EGUIA_LOWERRIGHT)
{
r.UpperLeftCorner.Y = r.LowerRightCorner.Y - totalHeight;
}
for (u32 i=0; i<BrokenText.size(); ++i)
{
if (HAlign == EGUIA_LOWERRIGHT)
{
r.UpperLeftCorner.X = frameRect.LowerRightCorner.X -
font->getDimension(BrokenText[i].c_str()).Width;
}
font->draw(BrokenText[i].c_str(), r,
OverrideColorEnabled ? OverrideColor : skin->getColor(isEnabled() ? EGDC_BUTTON_TEXT : EGDC_GRAY_TEXT),
HAlign == EGUIA_CENTER, false, (RestrainTextInside ? &AbsoluteClippingRect : NULL));
r.LowerRightCorner.Y += height;
r.UpperLeftCorner.Y += height;
}
}
}
}
IGUIElement::draw();
}
void CGUIStaticText::setOverrideFont(IGUIFont* font)
{
if (OverrideFont == font)
return;
if (OverrideFont)
OverrideFont->drop();
OverrideFont = font;
if (OverrideFont)
OverrideFont->grab();
breakText();
}
IGUIFont * CGUIStaticText::getOverrideFont() const
{
return OverrideFont;
}
void CGUIStaticText::setOverrideColor(video::SColor color)
{
OverrideColor = color;
OverrideColorEnabled = true;
}
void CGUIStaticText::setBackgroundColor(video::SColor color)
{
BGColor = color;
OverrideBGColorEnabled = true;
Background = true;
}
void CGUIStaticText::setDrawBackground(bool draw)
{
Background = draw;
}
void CGUIStaticText::setDrawBorder(bool draw)
{
Border = draw;
}
void CGUIStaticText::setTextRestrainedInside(bool restrainTextInside)
{
RestrainTextInside = restrainTextInside;
}
bool CGUIStaticText::isTextRestrainedInside() const
{
return RestrainTextInside;
}
void CGUIStaticText::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
{
HAlign = horizontal;
VAlign = vertical;
}
video::SColor const& CGUIStaticText::getOverrideColor() const
{
return OverrideColor;
}
void CGUIStaticText::enableOverrideColor(bool enable)
{
OverrideColorEnabled = enable;
}
bool CGUIStaticText::isOverrideColorEnabled() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return OverrideColorEnabled;
}
void CGUIStaticText::setWordWrap(bool enable)
{
WordWrap = enable;
breakText();
}
bool CGUIStaticText::isWordWrapEnabled() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return WordWrap;
}
void CGUIStaticText::breakText()
{
IGUISkin* skin = Environment->getSkin();
if (!WordWrap || !skin)
return;
BrokenText.clear();
IGUIFont* font = OverrideFont;
if (!OverrideFont)
font = skin->getFont();
if (!font)
return;
LastBreakFont = font;
core::stringw line;
core::stringw word;
core::stringw whitespace;
s32 size = Text.size();
s32 length = 0;
s32 elWidth = RelativeRect.getWidth() - 6;
wchar_t c;
for (s32 i=0; i<size; ++i)
{
c = Text[i];
bool lineBreak = false;
if (c == L'\r')
{
lineBreak = true;
if (Text[i+1] == L'\n')
{
Text.erase(i+1);
--size;
}
c = '\0';
}
else if (c == L'\n')
{
lineBreak = true;
c = '\0';
}
if (c == L' ' || c == 0 || i == (size-1))
{
if (word.size())
{
const s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
const s32 wordlgth = font->getDimension(word.c_str()).Width;
if (length && (length + wordlgth + whitelgth > elWidth))
{
BrokenText.push_back(line);
length = wordlgth;
line = word;
}
else
{
line += whitespace;
line += word;
length += whitelgth + wordlgth;
}
word = L"";
whitespace = L"";
}
whitespace += c;
if (lineBreak)
{
line += whitespace;
line += word;
BrokenText.push_back(line);
line = L"";
word = L"";
whitespace = L"";
length = 0;
}
}
else
{
word += c;
}
}
line += whitespace;
line += word;
BrokenText.push_back(line);
}
void CGUIStaticText::setText(const wchar_t* text)
{
IGUIElement::setText(text);
breakText();
}
void CGUIStaticText::updateAbsolutePosition()
{
IGUIElement::updateAbsolutePosition();
breakText();
}
s32 CGUIStaticText::getTextHeight() const
{
IGUISkin* skin = Environment->getSkin();
if (!skin)
return 0;
IGUIFont* font = OverrideFont;
if (!OverrideFont)
font = skin->getFont();
if (!font)
return 0;
s32 height = font->getDimension(L"A").Height + font->getKerningHeight();
if (WordWrap)
height *= BrokenText.size();
return height;
}
s32 CGUIStaticText::getTextWidth() const
{
IGUIFont * font = OverrideFont;
if(!OverrideFont)
{
IGUISkin * skin = Environment->getSkin();
if(skin)
font = skin->getFont();
}
if(!font)
return 0;
if(WordWrap)
{
s32 widest = 0;
for(u32 line = 0; line < BrokenText.size(); ++line)
{
s32 width = font->getDimension(BrokenText[line].c_str()).Width;
if(width > widest)
widest = width;
}
return widest;
}
else
{
return font->getDimension(Text.c_str()).Width;
}
}
void CGUIStaticText::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
{
IGUIStaticText::serializeAttributes(out,options);
out->addBool ("Border", Border);
out->addBool ("OverrideColorEnabled",OverrideColorEnabled);
out->addBool ("OverrideBGColorEnabled",OverrideBGColorEnabled);
out->addBool ("WordWrap", WordWrap);
out->addBool ("Background", Background);
out->addBool ("RestrainTextInside", RestrainTextInside);
out->addColor ("OverrideColor", OverrideColor);
out->addColor ("BGColor", BGColor);
out->addEnum ("HTextAlign", HAlign, GUIAlignmentNames);
out->addEnum ("VTextAlign", VAlign, GUIAlignmentNames);
}
void CGUIStaticText::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
{
IGUIStaticText::deserializeAttributes(in,options);
Border = in->getAttributeAsBool("Border");
enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
OverrideBGColorEnabled = in->getAttributeAsBool("OverrideBGColorEnabled");
setWordWrap(in->getAttributeAsBool("WordWrap"));
Background = in->getAttributeAsBool("Background");
RestrainTextInside = in->getAttributeAsBool("RestrainTextInside");
OverrideColor = in->getAttributeAsColor("OverrideColor");
BGColor = in->getAttributeAsColor("BGColor");
setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
(EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
}
}
}
#endif