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-03 01:01:58 +00:00
|
|
|
mFont(NULL), mColor(0x000000FF), mAutoCalcExtent(true, true)
|
2013-06-14 15:48:13 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-03 07:54:55 +00:00
|
|
|
TextComponent::TextComponent(Window* window, const std::string& text, std::shared_ptr<Font> font, Vector2i pos, Vector2u size) : GuiComponent(window),
|
2013-07-03 01:01:58 +00:00
|
|
|
mFont(NULL), mColor(0x000000FF), mAutoCalcExtent(true, 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-07-03 01:01:58 +00:00
|
|
|
mSize = size;
|
|
|
|
mAutoCalcExtent = Vector2<bool>(size.x == 0, size.y == 0);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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
|
|
|
|
return mWindow->getResourceManager()->getFont(Font::getDefaultPath(), FONT_SIZE_MEDIUM);
|
2013-06-14 15:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextComponent::onRender()
|
|
|
|
{
|
2013-07-03 07:54:55 +00:00
|
|
|
std::shared_ptr<Font> font = getFont();
|
2013-06-14 15:48:13 +00:00
|
|
|
|
2013-07-03 01:01:58 +00:00
|
|
|
//Renderer::pushClipRect(getGlobalOffset(), getSize());
|
2013-06-14 15:48:13 +00:00
|
|
|
|
2013-07-03 07:54:55 +00:00
|
|
|
font->drawWrappedText(mText, 0, 0, mSize.x, mColor >> 8 << 8 | getOpacity());
|
2013-06-14 15:48:13 +00:00
|
|
|
|
2013-07-03 01:01:58 +00:00
|
|
|
//Renderer::popClipRect();
|
2013-06-14 15:48:13 +00:00
|
|
|
|
|
|
|
GuiComponent::onRender();
|
|
|
|
}
|
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-03 01:01:58 +00:00
|
|
|
if(mAutoCalcExtent.x)
|
|
|
|
font->sizeText(mText, (int*)&mSize.x, (int*)&mSize.y);
|
|
|
|
else
|
|
|
|
if(mAutoCalcExtent.y)
|
2013-07-03 07:54:55 +00:00
|
|
|
font->sizeWrappedText(mText, getSize().x, NULL, (int*)&mSize.y);
|
2013-07-02 05:57:31 +00:00
|
|
|
}
|