2013-08-18 14:16:11 +00:00
|
|
|
#include "TextEditComponent.h"
|
|
|
|
#include "../Log.h"
|
2013-08-19 15:36:48 +00:00
|
|
|
#include "../Font.h"
|
|
|
|
#include "../Window.h"
|
|
|
|
#include "../Renderer.h"
|
2013-08-21 19:49:33 +00:00
|
|
|
#include "ComponentListComponent.h"
|
2013-08-18 14:16:11 +00:00
|
|
|
|
|
|
|
TextEditComponent::TextEditComponent(Window* window) : GuiComponent(window),
|
2013-08-21 19:49:33 +00:00
|
|
|
mBox(window, 0, 0, 0, 0), mFocused(false), mAllowResize(true)
|
2013-08-18 14:16:11 +00:00
|
|
|
{
|
|
|
|
addChild(&mBox);
|
|
|
|
|
|
|
|
onFocusLost();
|
|
|
|
|
2013-08-21 17:40:39 +00:00
|
|
|
setSize(256, (float)getFont()->getHeight());
|
2013-08-18 14:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextEditComponent::onFocusGained()
|
|
|
|
{
|
|
|
|
mBox.setHorizontalImage(":/glow_hor.png");
|
|
|
|
mBox.setVerticalImage(":/glow_vert.png");
|
2013-08-19 15:36:48 +00:00
|
|
|
mBox.setBorderColor(0x51CCFF00 | getOpacity());
|
2013-08-19 14:05:30 +00:00
|
|
|
|
|
|
|
SDL_StartTextInput();
|
2013-08-19 15:36:48 +00:00
|
|
|
mFocused = true;
|
2013-08-18 14:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextEditComponent::onFocusLost()
|
|
|
|
{
|
|
|
|
mBox.setHorizontalImage(":/glow_off_hor.png");
|
|
|
|
mBox.setVerticalImage(":/glow_off_vert.png");
|
2013-08-19 15:36:48 +00:00
|
|
|
mBox.setBorderColor(0xFFFFFF00 | getOpacity());
|
2013-08-19 14:05:30 +00:00
|
|
|
|
|
|
|
SDL_StopTextInput();
|
2013-08-19 15:36:48 +00:00
|
|
|
mFocused = false;
|
2013-08-18 14:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextEditComponent::onSizeChanged()
|
|
|
|
{
|
|
|
|
mBox.setSize(mSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEditComponent::setValue(const std::string& val)
|
|
|
|
{
|
|
|
|
mText = val;
|
2013-08-21 19:49:33 +00:00
|
|
|
onTextChanged();
|
2013-08-18 14:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string TextEditComponent::getValue() const
|
|
|
|
{
|
|
|
|
return mText;
|
|
|
|
}
|
|
|
|
|
2013-08-19 15:36:48 +00:00
|
|
|
void TextEditComponent::textInput(const char* text)
|
2013-08-18 14:16:11 +00:00
|
|
|
{
|
2013-08-19 15:36:48 +00:00
|
|
|
if(mFocused)
|
|
|
|
{
|
|
|
|
if(text[0] == '\b')
|
|
|
|
{
|
|
|
|
if(mText.length() > 0)
|
|
|
|
mText.erase(mText.end() - 1, mText.end());
|
|
|
|
}else{
|
|
|
|
mText += text;
|
|
|
|
}
|
|
|
|
}
|
2013-08-21 19:49:33 +00:00
|
|
|
|
|
|
|
onTextChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEditComponent::onTextChanged()
|
|
|
|
{
|
2013-08-22 01:08:36 +00:00
|
|
|
std::shared_ptr<Font> f = getFont();
|
|
|
|
|
|
|
|
std::string wrappedText = f->wrapText(mText, mSize.x());
|
|
|
|
mTextCache = std::unique_ptr<TextCache>(f->buildTextCache(wrappedText, 0, 0, 0x00000000 | getOpacity()));
|
|
|
|
|
2013-08-21 19:49:33 +00:00
|
|
|
if(mAllowResize)
|
|
|
|
{
|
2013-08-22 01:08:36 +00:00
|
|
|
float y = f->sizeText(wrappedText).y();
|
2013-08-21 19:49:33 +00:00
|
|
|
if(y == 0)
|
2013-08-22 01:08:36 +00:00
|
|
|
y = (float)f->getHeight();
|
2013-08-21 19:49:33 +00:00
|
|
|
|
|
|
|
setSize(mSize.x(), y);
|
|
|
|
}
|
|
|
|
|
|
|
|
ComponentListComponent* cmp = dynamic_cast<ComponentListComponent*>(getParent());
|
|
|
|
if(cmp)
|
|
|
|
cmp->updateComponent(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEditComponent::setAllowResize(bool allow)
|
|
|
|
{
|
|
|
|
mAllowResize = allow;
|
|
|
|
onTextChanged();
|
2013-08-19 15:36:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextEditComponent::render(const Eigen::Affine3f& parentTrans)
|
|
|
|
{
|
|
|
|
Eigen::Affine3f trans = getTransform() * parentTrans;
|
|
|
|
renderChildren(trans);
|
|
|
|
|
|
|
|
Renderer::setMatrix(trans);
|
|
|
|
|
2013-08-22 01:08:36 +00:00
|
|
|
if(mTextCache != NULL)
|
|
|
|
{
|
|
|
|
std::shared_ptr<Font> f = getFont();
|
|
|
|
f->renderTextCache(mTextCache.get());
|
|
|
|
}
|
2013-08-19 15:36:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Font> TextEditComponent::getFont()
|
|
|
|
{
|
|
|
|
return Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_SMALL);
|
2013-08-18 14:16:11 +00:00
|
|
|
}
|