2013-06-14 15:48:13 +00:00
|
|
|
#include "TextComponent.h"
|
|
|
|
#include "../Renderer.h"
|
|
|
|
#include "../Log.h"
|
|
|
|
|
2013-06-19 01:12:30 +00:00
|
|
|
TextComponent::TextComponent(Window* window) : GuiComponent(window), mFont(NULL), mColor(0x000000FF), mAutoCalcExtent(true)
|
2013-06-14 15:48:13 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TextComponent::TextComponent(Window* window, const std::string& text, Font* font, Vector2i pos, Vector2u size) : GuiComponent(window),
|
2013-06-19 01:12:30 +00:00
|
|
|
mFont(NULL), mColor(0x000000FF), mAutoCalcExtent(true)
|
2013-06-14 15:48:13 +00:00
|
|
|
{
|
|
|
|
setText(text);
|
|
|
|
setFont(font);
|
|
|
|
setBox(pos, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextComponent::setBox(Vector2i pos, Vector2u size)
|
|
|
|
{
|
|
|
|
setOffset(pos);
|
|
|
|
setExtent(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextComponent::setExtent(Vector2u size)
|
|
|
|
{
|
2013-06-19 01:12:30 +00:00
|
|
|
if(size == Vector2u(0, 0))
|
|
|
|
{
|
|
|
|
mAutoCalcExtent = true;
|
|
|
|
calculateExtent();
|
|
|
|
}else{
|
|
|
|
mAutoCalcExtent = false;
|
|
|
|
mSize = size;
|
|
|
|
}
|
2013-06-14 15:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextComponent::setFont(Font* font)
|
|
|
|
{
|
|
|
|
mFont = font;
|
2013-06-19 01:12:30 +00:00
|
|
|
|
|
|
|
if(mAutoCalcExtent)
|
|
|
|
calculateExtent();
|
2013-06-14 15:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextComponent::setColor(unsigned int color)
|
|
|
|
{
|
|
|
|
mColor = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextComponent::setText(const std::string& text)
|
|
|
|
{
|
|
|
|
mText = text;
|
2013-06-19 01:12:30 +00:00
|
|
|
|
|
|
|
if(mAutoCalcExtent)
|
|
|
|
calculateExtent();
|
2013-06-14 15:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextComponent::onRender()
|
|
|
|
{
|
|
|
|
Font* font = (mFont ? mFont : Renderer::getDefaultFont(Renderer::MEDIUM));
|
|
|
|
if(font == NULL)
|
|
|
|
{
|
|
|
|
LOG(LogError) << "TextComponent can't get a valid font!";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-19 01:12:30 +00:00
|
|
|
Renderer::pushClipRect(getGlobalOffset(), getSize());
|
2013-06-14 15:48:13 +00:00
|
|
|
|
|
|
|
Renderer::drawWrappedText(mText, 0, 0, mSize.x, mColor, font);
|
|
|
|
|
|
|
|
Renderer::popClipRect();
|
|
|
|
|
|
|
|
GuiComponent::onRender();
|
|
|
|
}
|
2013-06-19 01:12:30 +00:00
|
|
|
|
|
|
|
void TextComponent::calculateExtent()
|
|
|
|
{
|
|
|
|
Font* font = (mFont ? mFont : Renderer::getDefaultFont(Renderer::MEDIUM));
|
|
|
|
if(font == NULL)
|
|
|
|
{
|
|
|
|
LOG(LogError) << "TextComponent can't get a valid font!";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
font->sizeText(mText, (int*)&mSize.x, (int*)&mSize.y);
|
|
|
|
}
|