2014-06-20 01:30:09 +00:00
|
|
|
#include "components/TextComponent.h"
|
2017-03-13 21:11:07 +00:00
|
|
|
|
2017-11-15 15:59:39 +00:00
|
|
|
#include "utils/StringUtil.h"
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "Log.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
#include "Renderer.h"
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "Settings.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
#include "Util.h"
|
2013-06-14 15:48:13 +00:00
|
|
|
|
2013-07-02 05:57:31 +00:00
|
|
|
TextComponent::TextComponent(Window* window) : GuiComponent(window),
|
2017-08-15 02:34:34 +00:00
|
|
|
mFont(Font::get(FONT_SIZE_MEDIUM)), mUppercase(false), mColor(0x000000FF), mAutoCalcExtent(true, true),
|
|
|
|
mHorizontalAlignment(ALIGN_LEFT), mVerticalAlignment(ALIGN_CENTER), mLineSpacing(1.5f), mBgColor(0),
|
|
|
|
mRenderBackground(false)
|
2013-06-14 15:48:13 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-03-21 19:51:25 +00:00
|
|
|
TextComponent::TextComponent(Window* window, const std::string& text, const std::shared_ptr<Font>& font, unsigned int color, Alignment align,
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector3f pos, Vector2f size, unsigned int bgcolor) : GuiComponent(window),
|
2017-08-15 02:34:34 +00:00
|
|
|
mFont(NULL), mUppercase(false), mColor(0x000000FF), mAutoCalcExtent(true, true),
|
|
|
|
mHorizontalAlignment(align), mVerticalAlignment(ALIGN_CENTER), mLineSpacing(1.5f), mBgColor(0),
|
|
|
|
mRenderBackground(false)
|
2013-06-14 15:48:13 +00:00
|
|
|
{
|
2014-03-08 01:35:16 +00:00
|
|
|
setFont(font);
|
2014-03-01 21:02:44 +00:00
|
|
|
setColor(color);
|
2017-03-13 21:11:07 +00:00
|
|
|
setBackgroundColor(bgcolor);
|
2013-06-14 15:48:13 +00:00
|
|
|
setText(text);
|
2013-07-10 11:29:43 +00:00
|
|
|
setPosition(pos);
|
|
|
|
setSize(size);
|
2013-06-14 15:48:13 +00:00
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
void TextComponent::onSizeChanged()
|
2013-06-14 15:48:13 +00:00
|
|
|
{
|
2017-10-28 20:24:35 +00:00
|
|
|
mAutoCalcExtent = Vector2i((getSize().x() == 0), (getSize().y() == 0));
|
2013-08-22 01:08:36 +00:00
|
|
|
onTextChanged();
|
2013-06-14 15:48:13 +00:00
|
|
|
}
|
|
|
|
|
2014-03-08 01:35:16 +00:00
|
|
|
void TextComponent::setFont(const std::shared_ptr<Font>& font)
|
2013-06-14 15:48:13 +00:00
|
|
|
{
|
|
|
|
mFont = font;
|
2013-08-22 01:08:36 +00:00
|
|
|
onTextChanged();
|
2013-06-14 15:48:13 +00:00
|
|
|
}
|
|
|
|
|
2017-03-13 21:11:07 +00:00
|
|
|
// Set the color of the font/text
|
2013-06-14 15:48:13 +00:00
|
|
|
void TextComponent::setColor(unsigned int color)
|
|
|
|
{
|
|
|
|
mColor = color;
|
2017-03-13 21:11:07 +00:00
|
|
|
mColorOpacity = mColor & 0x000000FF;
|
2013-10-10 21:14:33 +00:00
|
|
|
onColorChanged();
|
|
|
|
}
|
|
|
|
|
2017-03-13 21:11:07 +00:00
|
|
|
// Set the color of the background box
|
|
|
|
void TextComponent::setBackgroundColor(unsigned int color)
|
|
|
|
{
|
|
|
|
mBgColor = color;
|
|
|
|
mBgColorOpacity = mBgColor & 0x000000FF;
|
|
|
|
}
|
|
|
|
|
2017-05-01 02:54:27 +00:00
|
|
|
void TextComponent::setRenderBackground(bool render)
|
|
|
|
{
|
|
|
|
mRenderBackground = render;
|
|
|
|
}
|
|
|
|
|
2017-03-13 21:11:07 +00:00
|
|
|
// Scale the opacity
|
2013-10-10 21:14:33 +00:00
|
|
|
void TextComponent::setOpacity(unsigned char opacity)
|
|
|
|
{
|
2017-03-13 21:11:07 +00:00
|
|
|
// This method is mostly called to do fading in-out of the Text component element.
|
|
|
|
// Therefore, we assume here that opacity is a fractional value (expressed as an int 0-255),
|
|
|
|
// of the opacity originally set with setColor() or setBackgroundColor().
|
|
|
|
|
|
|
|
unsigned char o = (unsigned char)((float)opacity / 255.f * (float) mColorOpacity);
|
|
|
|
mColor = (mColor & 0xFFFFFF00) | (unsigned char) o;
|
2017-05-01 02:54:27 +00:00
|
|
|
|
2017-03-13 21:11:07 +00:00
|
|
|
unsigned char bgo = (unsigned char)((float)opacity / 255.f * (float)mBgColorOpacity);
|
|
|
|
mBgColor = (mBgColor & 0xFFFFFF00) | (unsigned char)bgo;
|
2017-05-01 02:54:27 +00:00
|
|
|
|
2013-10-10 21:14:33 +00:00
|
|
|
onColorChanged();
|
|
|
|
|
|
|
|
GuiComponent::setOpacity(opacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char TextComponent::getOpacity() const
|
|
|
|
{
|
|
|
|
return mColor & 0x000000FF;
|
2013-06-14 15:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextComponent::setText(const std::string& text)
|
|
|
|
{
|
|
|
|
mText = text;
|
2013-08-22 01:08:36 +00:00
|
|
|
onTextChanged();
|
2013-07-02 05:57:31 +00:00
|
|
|
}
|
|
|
|
|
2014-05-03 19:51:50 +00:00
|
|
|
void TextComponent::setUppercase(bool uppercase)
|
|
|
|
{
|
|
|
|
mUppercase = uppercase;
|
|
|
|
onTextChanged();
|
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
void TextComponent::render(const Transform4x4f& parentTrans)
|
2013-06-14 15:48:13 +00:00
|
|
|
{
|
2017-10-28 20:24:35 +00:00
|
|
|
Transform4x4f trans = parentTrans * getTransform();
|
2013-07-23 06:27:28 +00:00
|
|
|
|
2017-05-01 02:54:27 +00:00
|
|
|
if (mRenderBackground)
|
2017-03-13 21:11:07 +00:00
|
|
|
{
|
2017-05-01 02:54:27 +00:00
|
|
|
Renderer::setMatrix(trans);
|
|
|
|
Renderer::drawRect(0.f, 0.f, mSize.x(), mSize.y(), mBgColor);
|
2017-03-13 21:11:07 +00:00
|
|
|
}
|
2014-03-12 03:00:08 +00:00
|
|
|
|
2014-03-08 01:35:16 +00:00
|
|
|
if(mTextCache)
|
2013-07-23 06:27:28 +00:00
|
|
|
{
|
2017-10-28 20:24:35 +00:00
|
|
|
const Vector2f& textSize = mTextCache->metrics.size;
|
2017-11-13 22:16:38 +00:00
|
|
|
float yOff = 0;
|
2017-08-15 02:34:34 +00:00
|
|
|
switch(mVerticalAlignment)
|
|
|
|
{
|
|
|
|
case ALIGN_TOP:
|
|
|
|
yOff = 0;
|
|
|
|
break;
|
|
|
|
case ALIGN_BOTTOM:
|
|
|
|
yOff = (getSize().y() - textSize.y());
|
|
|
|
break;
|
|
|
|
case ALIGN_CENTER:
|
|
|
|
yOff = (getSize().y() - textSize.y()) / 2.0f;
|
|
|
|
break;
|
|
|
|
}
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector3f off(0, yOff, 0);
|
2013-08-22 20:29:50 +00:00
|
|
|
|
2014-04-05 17:48:38 +00:00
|
|
|
if(Settings::getInstance()->getBool("DebugText"))
|
|
|
|
{
|
|
|
|
// draw the "textbox" area, what we are aligned within
|
|
|
|
Renderer::setMatrix(trans);
|
|
|
|
Renderer::drawRect(0.f, 0.f, mSize.x(), mSize.y(), 0xFF000033);
|
|
|
|
}
|
2017-03-13 21:11:07 +00:00
|
|
|
|
2014-03-21 19:51:25 +00:00
|
|
|
trans.translate(off);
|
2017-10-28 20:24:35 +00:00
|
|
|
trans.round();
|
2014-03-21 19:51:25 +00:00
|
|
|
Renderer::setMatrix(trans);
|
2014-04-05 17:48:38 +00:00
|
|
|
|
|
|
|
// draw the text area, where the text actually is going
|
|
|
|
if(Settings::getInstance()->getBool("DebugText"))
|
2014-05-13 01:03:02 +00:00
|
|
|
{
|
2017-08-15 02:34:34 +00:00
|
|
|
switch(mHorizontalAlignment)
|
2014-05-13 01:03:02 +00:00
|
|
|
{
|
|
|
|
case ALIGN_LEFT:
|
|
|
|
Renderer::drawRect(0.0f, 0.0f, mTextCache->metrics.size.x(), mTextCache->metrics.size.y(), 0x00000033);
|
|
|
|
break;
|
|
|
|
case ALIGN_CENTER:
|
|
|
|
Renderer::drawRect((mSize.x() - mTextCache->metrics.size.x()) / 2.0f, 0.0f, mTextCache->metrics.size.x(), mTextCache->metrics.size.y(), 0x00000033);
|
|
|
|
break;
|
|
|
|
case ALIGN_RIGHT:
|
|
|
|
Renderer::drawRect(mSize.x() - mTextCache->metrics.size.x(), 0.0f, mTextCache->metrics.size.x(), mTextCache->metrics.size.y(), 0x00000033);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 01:35:16 +00:00
|
|
|
mFont->renderTextCache(mTextCache.get());
|
2013-07-23 06:27:28 +00:00
|
|
|
}
|
2013-06-14 15:48:13 +00:00
|
|
|
}
|
2013-06-19 01:12:30 +00:00
|
|
|
|
|
|
|
void TextComponent::calculateExtent()
|
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
if(mAutoCalcExtent.x())
|
|
|
|
{
|
2014-05-16 23:13:19 +00:00
|
|
|
mSize = mFont->sizeText(mUppercase ? strToUpper(mText) : mText, mLineSpacing);
|
2013-07-10 11:29:43 +00:00
|
|
|
}else{
|
|
|
|
if(mAutoCalcExtent.y())
|
|
|
|
{
|
2014-05-16 23:13:19 +00:00
|
|
|
mSize[1] = mFont->sizeWrappedText(mUppercase ? strToUpper(mText) : mText, getSize().x(), mLineSpacing).y();
|
2013-07-10 11:29:43 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-02 05:57:31 +00:00
|
|
|
}
|
2013-08-14 12:16:49 +00:00
|
|
|
|
2013-08-22 01:08:36 +00:00
|
|
|
void TextComponent::onTextChanged()
|
|
|
|
{
|
|
|
|
calculateExtent();
|
|
|
|
|
2014-05-21 18:10:39 +00:00
|
|
|
if(!mFont || mText.empty())
|
|
|
|
{
|
|
|
|
mTextCache.reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-03 19:51:50 +00:00
|
|
|
std::string text = mUppercase ? strToUpper(mText) : mText;
|
|
|
|
|
2014-05-21 18:10:39 +00:00
|
|
|
std::shared_ptr<Font> f = mFont;
|
|
|
|
const bool isMultiline = (mSize.y() == 0 || mSize.y() > f->getHeight()*1.2f);
|
|
|
|
|
|
|
|
bool addAbbrev = false;
|
|
|
|
if(!isMultiline)
|
|
|
|
{
|
|
|
|
size_t newline = text.find('\n');
|
|
|
|
text = text.substr(0, newline); // single line of text - stop at the first newline since it'll mess everything up
|
|
|
|
addAbbrev = newline != std::string::npos;
|
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2f size = f->sizeText(text);
|
2014-05-21 18:10:39 +00:00
|
|
|
if(!isMultiline && mSize.x() && text.size() && (size.x() > mSize.x() || addAbbrev))
|
2014-01-19 23:22:26 +00:00
|
|
|
{
|
|
|
|
// abbreviate text
|
2014-01-23 21:30:32 +00:00
|
|
|
const std::string abbrev = "...";
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2f abbrevSize = f->sizeText(abbrev);
|
2014-01-19 23:22:26 +00:00
|
|
|
|
|
|
|
while(text.size() && size.x() + abbrevSize.x() > mSize.x())
|
|
|
|
{
|
2017-11-15 15:59:39 +00:00
|
|
|
size_t newSize = Utils::String::prevCursor(text, text.size());
|
2014-07-27 21:44:02 +00:00
|
|
|
text.erase(newSize, text.size() - newSize);
|
2014-01-19 23:22:26 +00:00
|
|
|
size = f->sizeText(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
text.append(abbrev);
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
mTextCache = std::shared_ptr<TextCache>(f->buildTextCache(text, Vector2f(0, 0), (mColor >> 8 << 8) | mOpacity, mSize.x(), mHorizontalAlignment, mLineSpacing));
|
2014-01-19 23:22:26 +00:00
|
|
|
}else{
|
2017-10-28 20:24:35 +00:00
|
|
|
mTextCache = std::shared_ptr<TextCache>(f->buildTextCache(f->wrapText(text, mSize.x()), Vector2f(0, 0), (mColor >> 8 << 8) | mOpacity, mSize.x(), mHorizontalAlignment, mLineSpacing));
|
2014-01-19 23:22:26 +00:00
|
|
|
}
|
2013-08-22 01:08:36 +00:00
|
|
|
}
|
|
|
|
|
2013-10-10 21:14:33 +00:00
|
|
|
void TextComponent::onColorChanged()
|
|
|
|
{
|
|
|
|
if(mTextCache)
|
|
|
|
{
|
|
|
|
mTextCache->setColor(mColor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 02:34:34 +00:00
|
|
|
void TextComponent::setHorizontalAlignment(Alignment align)
|
2014-05-14 22:01:40 +00:00
|
|
|
{
|
2017-08-15 02:34:34 +00:00
|
|
|
mHorizontalAlignment = align;
|
2014-05-14 22:01:40 +00:00
|
|
|
onTextChanged();
|
|
|
|
}
|
|
|
|
|
2017-08-15 02:34:34 +00:00
|
|
|
void TextComponent::setVerticalAlignment(Alignment align)
|
|
|
|
{
|
|
|
|
mVerticalAlignment = align;
|
|
|
|
}
|
|
|
|
|
2014-05-14 22:01:40 +00:00
|
|
|
void TextComponent::setLineSpacing(float spacing)
|
|
|
|
{
|
|
|
|
mLineSpacing = spacing;
|
|
|
|
onTextChanged();
|
|
|
|
}
|
|
|
|
|
2013-08-14 12:16:49 +00:00
|
|
|
void TextComponent::setValue(const std::string& value)
|
|
|
|
{
|
|
|
|
setText(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TextComponent::getValue() const
|
|
|
|
{
|
|
|
|
return mText;
|
|
|
|
}
|
2014-01-01 05:39:22 +00:00
|
|
|
|
|
|
|
void TextComponent::applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties)
|
|
|
|
{
|
|
|
|
GuiComponent::applyTheme(theme, view, element, properties);
|
|
|
|
|
|
|
|
using namespace ThemeFlags;
|
|
|
|
|
|
|
|
const ThemeData::ThemeElement* elem = theme->getElement(view, element, "text");
|
|
|
|
if(!elem)
|
|
|
|
return;
|
|
|
|
|
2017-03-13 21:11:07 +00:00
|
|
|
if (properties & COLOR && elem->has("color"))
|
|
|
|
setColor(elem->get<unsigned int>("color"));
|
|
|
|
|
2017-05-01 02:54:27 +00:00
|
|
|
setRenderBackground(false);
|
|
|
|
if (properties & COLOR && elem->has("backgroundColor")) {
|
2017-03-13 21:11:07 +00:00
|
|
|
setBackgroundColor(elem->get<unsigned int>("backgroundColor"));
|
2017-05-01 02:54:27 +00:00
|
|
|
setRenderBackground(true);
|
|
|
|
}
|
2014-01-01 05:39:22 +00:00
|
|
|
|
2014-03-21 19:51:25 +00:00
|
|
|
if(properties & ALIGNMENT && elem->has("alignment"))
|
|
|
|
{
|
|
|
|
std::string str = elem->get<std::string>("alignment");
|
|
|
|
if(str == "left")
|
2017-08-15 02:34:34 +00:00
|
|
|
setHorizontalAlignment(ALIGN_LEFT);
|
2014-03-21 19:51:25 +00:00
|
|
|
else if(str == "center")
|
2017-08-15 02:34:34 +00:00
|
|
|
setHorizontalAlignment(ALIGN_CENTER);
|
2014-04-05 17:48:38 +00:00
|
|
|
else if(str == "right")
|
2017-08-15 02:34:34 +00:00
|
|
|
setHorizontalAlignment(ALIGN_RIGHT);
|
2014-03-21 19:51:25 +00:00
|
|
|
else
|
|
|
|
LOG(LogError) << "Unknown text alignment string: " << str;
|
|
|
|
}
|
2014-01-01 05:39:22 +00:00
|
|
|
|
|
|
|
if(properties & TEXT && elem->has("text"))
|
|
|
|
setText(elem->get<std::string>("text"));
|
|
|
|
|
2014-05-03 19:51:50 +00:00
|
|
|
if(properties & FORCE_UPPERCASE && elem->has("forceUppercase"))
|
|
|
|
setUppercase(elem->get<bool>("forceUppercase"));
|
|
|
|
|
2014-05-14 22:01:40 +00:00
|
|
|
if(properties & LINE_SPACING && elem->has("lineSpacing"))
|
|
|
|
setLineSpacing(elem->get<float>("lineSpacing"));
|
|
|
|
|
2014-01-01 05:39:22 +00:00
|
|
|
setFont(Font::getFromTheme(elem, properties, mFont));
|
|
|
|
}
|