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-06-14 15:48:13 +00:00
|
|
|
|
2013-07-02 05:57:31 +00:00
|
|
|
TextComponent::TextComponent(Window* window) : GuiComponent(window),
|
2013-07-23 06:27:28 +00:00
|
|
|
mFont(NULL), mColor(0x000000FF), mAutoCalcExtent(true, true), mCentered(false)
|
2013-06-14 15:48:13 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
TextComponent::TextComponent(Window* window, const std::string& text, std::shared_ptr<Font> font, Eigen::Vector3f pos, Eigen::Vector2f size) : GuiComponent(window),
|
2013-07-23 06:27:28 +00:00
|
|
|
mFont(NULL), mColor(0x000000FF), mAutoCalcExtent(true, true), mCentered(false)
|
2013-06-14 15:48:13 +00:00
|
|
|
{
|
|
|
|
setText(text);
|
|
|
|
setFont(font);
|
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
|
|
|
}
|
|
|
|
|
2013-07-03 07:54:55 +00:00
|
|
|
void TextComponent::setFont(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-03 07:54:55 +00:00
|
|
|
std::shared_ptr<Font> TextComponent::getFont() const
|
2013-07-02 05:57:31 +00:00
|
|
|
{
|
2013-07-03 07:54:55 +00:00
|
|
|
if(mFont)
|
|
|
|
return mFont;
|
|
|
|
else
|
2013-10-04 23:09:54 +00:00
|
|
|
return Font::get(FONT_SIZE_MEDIUM);
|
2013-06-14 15:48:13 +00:00
|
|
|
}
|
|
|
|
|
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-03 07:54:55 +00:00
|
|
|
std::shared_ptr<Font> font = getFont();
|
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
|
|
|
|
|
|
|
if(font && !mText.empty())
|
|
|
|
{
|
|
|
|
Renderer::setMatrix(trans);
|
|
|
|
|
|
|
|
if(mCentered)
|
|
|
|
{
|
2013-11-21 22:39:02 +00:00
|
|
|
const Eigen::Vector2f& textSize = mTextCache->metrics.size;
|
2013-07-23 06:27:28 +00:00
|
|
|
Eigen::Vector2f pos((getSize().x() - textSize.x()) / 2, 0);
|
2013-08-22 01:08:36 +00:00
|
|
|
|
|
|
|
Eigen::Affine3f centeredTrans = trans;
|
|
|
|
centeredTrans = centeredTrans.translate(Eigen::Vector3f(pos.x(), pos.y(), 0));
|
|
|
|
Renderer::setMatrix(centeredTrans);
|
2013-07-23 06:27:28 +00:00
|
|
|
}
|
2013-08-22 20:29:50 +00:00
|
|
|
|
|
|
|
font->renderTextCache(mTextCache.get());
|
2013-07-23 06:27:28 +00:00
|
|
|
}
|
2013-06-14 15:48:13 +00:00
|
|
|
|
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-03 07:54:55 +00:00
|
|
|
std::shared_ptr<Font> font = getFont();
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
if(mAutoCalcExtent.x())
|
|
|
|
{
|
|
|
|
mSize = font->sizeText(mText);
|
|
|
|
}else{
|
|
|
|
if(mAutoCalcExtent.y())
|
|
|
|
{
|
|
|
|
mSize[1] = font->sizeWrappedText(mText, getSize().x()).y();
|
|
|
|
}
|
|
|
|
}
|
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();
|
2013-09-19 23:41:14 +00:00
|
|
|
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;
|
|
|
|
}
|