2013-06-14 15:48:13 +00:00
|
|
|
#include "TextComponent.h"
|
|
|
|
#include "../Renderer.h"
|
|
|
|
#include "../Log.h"
|
|
|
|
|
2013-07-02 05:57:31 +00:00
|
|
|
TextComponent::TextComponent(Window* window) : GuiComponent(window),
|
|
|
|
mFont(NULL), mColor(0x000000FF), mAutoCalcExtent(true), mAutoScrollDelay(0), mAutoScrollSpeed(0), mAutoScrollTimer(0)
|
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-07-02 05:57:31 +00:00
|
|
|
mFont(NULL), mColor(0x000000FF), mAutoCalcExtent(true), mAutoScrollDelay(0), mAutoScrollSpeed(0), mAutoScrollTimer(0)
|
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-07-02 05:57:31 +00:00
|
|
|
|
|
|
|
mScrollPos = Vector2d(0, 0);
|
|
|
|
mScrollDir = Vector2d(0, 0);
|
|
|
|
resetAutoScrollTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
Font* TextComponent::getFont() const
|
|
|
|
{
|
|
|
|
return (mFont ? mFont : Renderer::getDefaultFont(Renderer::MEDIUM));;
|
2013-06-14 15:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextComponent::onRender()
|
|
|
|
{
|
2013-07-02 05:57:31 +00:00
|
|
|
Font* font = getFont();
|
2013-06-14 15:48:13 +00:00
|
|
|
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
|
|
|
|
2013-07-02 05:57:31 +00:00
|
|
|
Renderer::drawWrappedText(mText, (int)-mScrollPos.x, (int)-mScrollPos.y, mSize.x, mColor, font);
|
2013-06-14 15:48:13 +00:00
|
|
|
|
|
|
|
Renderer::popClipRect();
|
|
|
|
|
|
|
|
GuiComponent::onRender();
|
|
|
|
}
|
2013-06-19 01:12:30 +00:00
|
|
|
|
|
|
|
void TextComponent::calculateExtent()
|
|
|
|
{
|
2013-07-02 05:57:31 +00:00
|
|
|
Font* font = getFont();
|
2013-06-19 01:12:30 +00:00
|
|
|
if(font == NULL)
|
|
|
|
{
|
|
|
|
LOG(LogError) << "TextComponent can't get a valid font!";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
font->sizeText(mText, (int*)&mSize.x, (int*)&mSize.y);
|
|
|
|
}
|
2013-07-02 05:57:31 +00:00
|
|
|
|
|
|
|
void TextComponent::setAutoScroll(int delay, double speed)
|
|
|
|
{
|
|
|
|
mAutoScrollDelay = delay;
|
|
|
|
mAutoScrollSpeed = speed;
|
|
|
|
resetAutoScrollTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextComponent::resetAutoScrollTimer()
|
|
|
|
{
|
|
|
|
mAutoScrollTimer = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2d TextComponent::getScrollPos() const
|
|
|
|
{
|
|
|
|
return mScrollPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextComponent::setScrollPos(const Vector2d& pos)
|
|
|
|
{
|
|
|
|
mScrollPos = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextComponent::update(int deltaTime)
|
|
|
|
{
|
|
|
|
double scrollAmt = (double)deltaTime;
|
|
|
|
|
|
|
|
if(mAutoScrollSpeed != 0)
|
|
|
|
{
|
|
|
|
mAutoScrollTimer += deltaTime;
|
|
|
|
|
|
|
|
scrollAmt = (float)(mAutoScrollTimer - mAutoScrollDelay);
|
|
|
|
|
|
|
|
if(scrollAmt > 0)
|
|
|
|
{
|
|
|
|
//scroll the amount of time left over from the delay
|
|
|
|
mAutoScrollTimer = mAutoScrollDelay;
|
|
|
|
|
|
|
|
//scale speed by our width! more text per line = slower scrolling
|
|
|
|
const double widthMod = (680.0 / getSize().x);
|
|
|
|
mScrollDir = Vector2d(0, mAutoScrollSpeed * widthMod);
|
|
|
|
}else{
|
|
|
|
//not enough to pass the delay, do nothing
|
|
|
|
scrollAmt = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2d scroll = mScrollDir * scrollAmt;
|
|
|
|
mScrollPos += scroll;
|
|
|
|
|
|
|
|
//clip scrolling within bounds
|
|
|
|
if(mScrollPos.x < 0)
|
|
|
|
mScrollPos.x = 0;
|
|
|
|
if(mScrollPos.y < 0)
|
|
|
|
mScrollPos.y = 0;
|
|
|
|
|
|
|
|
Font* font = getFont();
|
|
|
|
if(font != NULL)
|
|
|
|
{
|
|
|
|
Vector2i textSize;
|
|
|
|
Renderer::sizeWrappedText(mText, getSize().x, getFont(), &textSize.x, &textSize.y);
|
|
|
|
|
|
|
|
if(mScrollPos.x + getSize().x > textSize.x)
|
|
|
|
mScrollPos.x = (double)textSize.x - getSize().x;
|
|
|
|
if(mScrollPos.y + getSize().y > textSize.y)
|
|
|
|
mScrollPos.y = (double)textSize.y - getSize().y;
|
|
|
|
}
|
|
|
|
|
|
|
|
GuiComponent::update(deltaTime);
|
|
|
|
}
|