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-07-03 01:01:58 +00:00
|
|
|
calculateExtent();
|
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-06-19 01:12:30 +00:00
|
|
|
|
2013-07-03 01:01:58 +00:00
|
|
|
calculateExtent();
|
2013-06-14 15:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextComponent::setColor(unsigned int color)
|
|
|
|
{
|
|
|
|
mColor = color;
|
2013-07-10 11:29:43 +00:00
|
|
|
mOpacity = mColor & 0x000000FF;
|
2013-06-14 15:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextComponent::setText(const std::string& text)
|
|
|
|
{
|
|
|
|
mText = text;
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2013-07-03 01:01:58 +00:00
|
|
|
calculateExtent();
|
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-07-09 05:44:24 +00:00
|
|
|
return Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), 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)
|
|
|
|
{
|
|
|
|
Eigen::Vector2f textSize = font->sizeWrappedText(mText, getSize().x());
|
|
|
|
Eigen::Vector2f pos((getSize().x() - textSize.x()) / 2, 0);
|
|
|
|
font->drawWrappedText(mText, pos, getSize().x(), (mColor >> 8 << 8) | getOpacity());
|
|
|
|
}else{
|
|
|
|
font->drawWrappedText(mText, Eigen::Vector2f(0, 0), getSize().x(), mColor >> 8 << 8 | getOpacity());
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|