2013-06-14 15:48:13 +00:00
|
|
|
#include "TextComponent.h"
|
|
|
|
#include "../Renderer.h"
|
|
|
|
#include "../Log.h"
|
2013-07-03 07:54:55 +00:00
|
|
|
#include "../Window.h"
|
2013-11-25 20:49:02 +00:00
|
|
|
#include "../ThemeData.h"
|
2013-06-14 15:48:13 +00:00
|
|
|
|
2013-07-02 05:57:31 +00:00
|
|
|
TextComponent::TextComponent(Window* window) : GuiComponent(window),
|
2014-03-08 01:35:16 +00:00
|
|
|
mFont(Font::get(FONT_SIZE_MEDIUM)), mColor(0x000000FF), mAutoCalcExtent(true, true), mCentered(false)
|
2013-06-14 15:48:13 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
TextComponent::TextComponent(Window* window, const std::string& text, const std::shared_ptr<Font>& font, unsigned int color, bool center,
|
|
|
|
Eigen::Vector3f pos, Eigen::Vector2f size) : GuiComponent(window),
|
|
|
|
mFont(NULL), mColor(0x000000FF), mAutoCalcExtent(true, true), mCentered(center)
|
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);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void TextComponent::setColor(unsigned int color)
|
|
|
|
{
|
|
|
|
mColor = color;
|
2013-10-10 21:14:33 +00:00
|
|
|
|
|
|
|
unsigned char opacity = mColor & 0x000000FF;
|
|
|
|
GuiComponent::setOpacity(opacity);
|
|
|
|
|
|
|
|
onColorChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextComponent::setOpacity(unsigned char opacity)
|
|
|
|
{
|
|
|
|
mColor = (mColor & 0xFFFFFF00) | opacity;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-07-23 06:27:28 +00:00
|
|
|
void TextComponent::setCentered(bool center)
|
|
|
|
{
|
|
|
|
mCentered = center;
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
void TextComponent::render(const Eigen::Affine3f& parentTrans)
|
2013-06-14 15:48:13 +00:00
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
Eigen::Affine3f trans = parentTrans * getTransform();
|
2013-07-23 06:27:28 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
Eigen::Vector3f dim(mSize.x(), mSize.y(), 0);
|
|
|
|
dim = trans * dim - trans.translation();
|
|
|
|
Renderer::pushClipRect(Eigen::Vector2i((int)trans.translation().x(), (int)trans.translation().y()), Eigen::Vector2i((int)dim.x(), (int)dim.y()));
|
|
|
|
|
2014-03-08 01:35:16 +00:00
|
|
|
if(mTextCache)
|
2013-07-23 06:27:28 +00:00
|
|
|
{
|
|
|
|
if(mCentered)
|
|
|
|
{
|
2013-11-21 22:39:02 +00:00
|
|
|
const Eigen::Vector2f& textSize = mTextCache->metrics.size;
|
2014-03-19 18:10:30 +00:00
|
|
|
Eigen::Vector3f off((getSize().x() - textSize.x()) / 2, (getSize().y() - textSize.y()) / 2, 0);
|
2013-08-22 01:08:36 +00:00
|
|
|
|
2014-02-27 21:25:30 +00:00
|
|
|
trans.translate(off);
|
|
|
|
Renderer::setMatrix(trans);
|
|
|
|
trans.translate(-off);
|
2014-01-19 23:22:26 +00:00
|
|
|
}else{
|
|
|
|
Renderer::setMatrix(trans);
|
2013-07-23 06:27:28 +00:00
|
|
|
}
|
2013-08-22 20:29:50 +00:00
|
|
|
|
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
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
Renderer::popClipRect();
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
GuiComponent::renderChildren(trans);
|
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-03-08 01:35:16 +00:00
|
|
|
mSize = mFont->sizeText(mText);
|
2013-07-10 11:29:43 +00:00
|
|
|
}else{
|
|
|
|
if(mAutoCalcExtent.y())
|
|
|
|
{
|
2014-03-08 01:35:16 +00:00
|
|
|
mSize[1] = mFont->sizeWrappedText(mText, getSize().x()).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();
|
|
|
|
|
|
|
|
std::shared_ptr<Font> f = getFont();
|
2014-01-19 23:22:26 +00:00
|
|
|
const bool wrap = (mSize.y() == 0 || (int)mSize.y() > f->getHeight());
|
|
|
|
Eigen::Vector2f size = f->sizeText(mText);
|
|
|
|
if(!wrap && mSize.x() && mText.size() && size.x() > mSize.x())
|
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
std::string text = mText;
|
|
|
|
while(text.size() && size.x() + abbrevSize.x() > mSize.x())
|
|
|
|
{
|
|
|
|
text.erase(text.size() - 1, 1);
|
|
|
|
size = f->sizeText(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
text.append(abbrev);
|
|
|
|
|
|
|
|
mTextCache = std::shared_ptr<TextCache>(f->buildTextCache(text, 0, 0, (mColor >> 8 << 8) | mOpacity));
|
|
|
|
}else{
|
|
|
|
mTextCache = std::shared_ptr<TextCache>(f->buildTextCache(f->wrapText(mText, mSize.x()), 0, 0, (mColor >> 8 << 8) | mOpacity));
|
|
|
|
}
|
2013-08-22 01:08:36 +00:00
|
|
|
}
|
|
|
|
|
2013-10-10 21:14:33 +00:00
|
|
|
void TextComponent::onColorChanged()
|
|
|
|
{
|
|
|
|
if(mTextCache)
|
|
|
|
{
|
|
|
|
mTextCache->setColor(mColor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if(properties & COLOR && elem->has("color"))
|
|
|
|
setColor(elem->get<unsigned int>("color"));
|
|
|
|
|
2014-01-22 02:43:33 +00:00
|
|
|
if(properties & ALIGNMENT && elem->has("center"))
|
2014-01-01 05:39:22 +00:00
|
|
|
setCentered(elem->get<bool>("center"));
|
|
|
|
|
|
|
|
if(properties & TEXT && elem->has("text"))
|
|
|
|
setText(elem->get<std::string>("text"));
|
|
|
|
|
|
|
|
setFont(Font::getFromTheme(elem, properties, mFont));
|
|
|
|
}
|