2014-06-20 01:30:09 +00:00
|
|
|
#include "components/TextComponent.h"
|
2017-03-13 21:11:07 +00:00
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "Renderer.h"
|
|
|
|
#include "Log.h"
|
|
|
|
#include "Window.h"
|
|
|
|
#include "ThemeData.h"
|
|
|
|
#include "Util.h"
|
|
|
|
#include "Settings.h"
|
2013-06-14 15:48:13 +00:00
|
|
|
|
2013-07-02 05:57:31 +00:00
|
|
|
TextComponent::TextComponent(Window* window) : GuiComponent(window),
|
2017-04-03 18:41:21 +00:00
|
|
|
mFont(Font::get(FONT_SIZE_MEDIUM)), mUppercase(false), mColor(0x000000FF), mAutoCalcExtent(true, true), mAlignment(ALIGN_LEFT), mLineSpacing(1.5f), mBgColor(0)
|
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-03-13 21:11:07 +00:00
|
|
|
Eigen::Vector3f pos, Eigen::Vector2f size, unsigned int bgcolor) : GuiComponent(window),
|
2017-04-03 18:41:21 +00:00
|
|
|
mFont(NULL), mUppercase(false), mColor(0x000000FF), mAutoCalcExtent(true, true), mAlignment(align), mLineSpacing(1.5f), mBgColor(0)
|
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
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
mAutoCalcExtent << (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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
unsigned char bgo = (unsigned char)((float)opacity / 255.f * (float)mBgColorOpacity);
|
|
|
|
mBgColor = (mBgColor & 0xFFFFFF00) | (unsigned char)bgo;
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
void TextComponent::render(const Eigen::Affine3f& parentTrans)
|
2013-06-14 15:48:13 +00:00
|
|
|
{
|
2014-03-22 18:04:14 +00:00
|
|
|
Eigen::Affine3f trans = parentTrans * getTransform();
|
2013-07-23 06:27:28 +00:00
|
|
|
|
2017-03-13 21:11:07 +00:00
|
|
|
if (mBgColor)
|
|
|
|
{
|
|
|
|
Renderer::drawRect(getPosition().x(), getPosition().y() - 1,
|
|
|
|
getSize().x(), getSize().y(), mBgColor);
|
|
|
|
}
|
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
|
|
|
{
|
2014-03-21 19:51:25 +00:00
|
|
|
const Eigen::Vector2f& textSize = mTextCache->metrics.size;
|
2014-05-13 01:03:02 +00:00
|
|
|
Eigen::Vector3f off(0, (getSize().y() - textSize.y()) / 2.0f, 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);
|
2014-03-22 18:04:14 +00:00
|
|
|
trans = roundMatrix(trans);
|
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
|
|
|
{
|
|
|
|
switch(mAlignment)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-05-03 19:51:50 +00:00
|
|
|
Eigen::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 = "...";
|
2014-01-19 23:22:26 +00:00
|
|
|
Eigen::Vector2f abbrevSize = f->sizeText(abbrev);
|
|
|
|
|
|
|
|
while(text.size() && size.x() + abbrevSize.x() > mSize.x())
|
|
|
|
{
|
2014-07-27 21:44:02 +00:00
|
|
|
size_t newSize = Font::getPrevCursor(text, text.size());
|
|
|
|
text.erase(newSize, text.size() - newSize);
|
2014-01-19 23:22:26 +00:00
|
|
|
size = f->sizeText(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
text.append(abbrev);
|
|
|
|
|
2014-05-14 22:01:40 +00:00
|
|
|
mTextCache = std::shared_ptr<TextCache>(f->buildTextCache(text, Eigen::Vector2f(0, 0), (mColor >> 8 << 8) | mOpacity, mSize.x(), mAlignment, mLineSpacing));
|
2014-01-19 23:22:26 +00:00
|
|
|
}else{
|
2014-05-14 22:01:40 +00:00
|
|
|
mTextCache = std::shared_ptr<TextCache>(f->buildTextCache(f->wrapText(text, mSize.x()), Eigen::Vector2f(0, 0), (mColor >> 8 << 8) | mOpacity, mSize.x(), mAlignment, 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-14 22:01:40 +00:00
|
|
|
void TextComponent::setAlignment(Alignment align)
|
|
|
|
{
|
|
|
|
mAlignment = align;
|
|
|
|
onTextChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
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"));
|
|
|
|
|
|
|
|
if (properties & COLOR && elem->has("backgroundColor"))
|
|
|
|
setBackgroundColor(elem->get<unsigned int>("backgroundColor"));
|
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")
|
|
|
|
setAlignment(ALIGN_LEFT);
|
|
|
|
else if(str == "center")
|
|
|
|
setAlignment(ALIGN_CENTER);
|
2014-04-05 17:48:38 +00:00
|
|
|
else if(str == "right")
|
2014-03-21 19:51:25 +00:00
|
|
|
setAlignment(ALIGN_RIGHT);
|
|
|
|
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));
|
|
|
|
}
|