2013-08-18 14:16:11 +00:00
|
|
|
#include "TextEditComponent.h"
|
|
|
|
#include "../Log.h"
|
2013-10-04 23:24:41 +00:00
|
|
|
#include "../resources/Font.h"
|
2013-08-19 15:36:48 +00:00
|
|
|
#include "../Window.h"
|
|
|
|
#include "../Renderer.h"
|
2014-03-21 02:47:45 +00:00
|
|
|
#include "../Util.h"
|
|
|
|
|
|
|
|
#define TEXT_PADDING_HORIZ 10
|
|
|
|
#define TEXT_PADDING_VERT 2
|
|
|
|
|
|
|
|
#define CURSOR_REPEAT_START_DELAY 500
|
|
|
|
#define CURSOR_REPEAT_SPEED 28 // lower is faster
|
2013-08-18 14:16:11 +00:00
|
|
|
|
|
|
|
TextEditComponent::TextEditComponent(Window* window) : GuiComponent(window),
|
2014-03-21 02:47:45 +00:00
|
|
|
mBox(window, ":/button.png"), mFocused(false),
|
|
|
|
mScrollOffset(0.0f, 0.0f), mCursor(0), mEditing(false), mFont(Font::get(FONT_SIZE_MEDIUM, FONT_PATH_LIGHT))
|
2013-08-18 14:16:11 +00:00
|
|
|
{
|
|
|
|
addChild(&mBox);
|
|
|
|
|
|
|
|
onFocusLost();
|
|
|
|
|
2014-03-21 02:47:45 +00:00
|
|
|
setSize(256, mFont->getHeight() + TEXT_PADDING_VERT);
|
2013-08-18 14:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextEditComponent::onFocusGained()
|
|
|
|
{
|
2013-08-19 15:36:48 +00:00
|
|
|
mFocused = true;
|
2013-08-18 14:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextEditComponent::onFocusLost()
|
|
|
|
{
|
2013-08-19 15:36:48 +00:00
|
|
|
mFocused = false;
|
2013-08-18 14:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextEditComponent::onSizeChanged()
|
|
|
|
{
|
2014-03-21 02:47:45 +00:00
|
|
|
mBox.fitTo(mSize, Eigen::Vector3f::Zero(), Eigen::Vector2f(-34, -32 - TEXT_PADDING_VERT));
|
|
|
|
onTextChanged(); // wrap point probably changed
|
2013-08-18 14:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-07 22:43:36 +00:00
|
|
|
if(mEditing)
|
2013-08-19 15:36:48 +00:00
|
|
|
{
|
2014-03-21 02:47:45 +00:00
|
|
|
mCursorRepeatDir = 0;
|
2013-08-19 15:36:48 +00:00
|
|
|
if(text[0] == '\b')
|
|
|
|
{
|
2013-09-07 22:43:36 +00:00
|
|
|
if(mCursor > 0)
|
|
|
|
{
|
|
|
|
mText.erase(mText.begin() + mCursor - 1, mText.begin() + mCursor);
|
|
|
|
mCursor--;
|
|
|
|
}
|
2013-08-19 15:36:48 +00:00
|
|
|
}else{
|
2013-09-07 22:43:36 +00:00
|
|
|
mText.insert(mCursor, text);
|
|
|
|
mCursor++;
|
2013-08-19 15:36:48 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-21 19:49:33 +00:00
|
|
|
|
|
|
|
onTextChanged();
|
2013-09-07 22:43:36 +00:00
|
|
|
onCursorChanged();
|
|
|
|
}
|
|
|
|
|
2014-03-21 02:47:45 +00:00
|
|
|
void TextEditComponent::startEditing()
|
|
|
|
{
|
|
|
|
SDL_StartTextInput();
|
|
|
|
mEditing = true;
|
|
|
|
updateHelpPrompts();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEditComponent::stopEditing()
|
|
|
|
{
|
|
|
|
SDL_StopTextInput();
|
|
|
|
mEditing = false;
|
|
|
|
updateHelpPrompts();
|
|
|
|
}
|
|
|
|
|
2013-09-07 22:43:36 +00:00
|
|
|
bool TextEditComponent::input(InputConfig* config, Input input)
|
|
|
|
{
|
|
|
|
if(input.value == 0)
|
2014-03-21 02:47:45 +00:00
|
|
|
{
|
|
|
|
if(config->isMappedTo("left", input) || config->isMappedTo("right", input))
|
|
|
|
mCursorRepeatDir = 0;
|
|
|
|
|
2013-09-07 22:43:36 +00:00
|
|
|
return false;
|
2014-03-21 02:47:45 +00:00
|
|
|
}
|
2013-09-07 22:43:36 +00:00
|
|
|
|
|
|
|
if(config->isMappedTo("a", input) && mFocused && !mEditing)
|
|
|
|
{
|
2014-03-21 02:47:45 +00:00
|
|
|
startEditing();
|
2013-09-07 22:43:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mEditing)
|
|
|
|
{
|
|
|
|
if(config->getDeviceId() == DEVICE_KEYBOARD && input.id == SDLK_RETURN)
|
|
|
|
{
|
2013-09-12 21:35:44 +00:00
|
|
|
if(isMultiline())
|
2014-01-25 23:34:29 +00:00
|
|
|
{
|
2013-09-12 21:35:44 +00:00
|
|
|
textInput("\n");
|
2014-01-25 23:34:29 +00:00
|
|
|
}else{
|
2014-03-21 02:47:45 +00:00
|
|
|
stopEditing();
|
2014-01-25 23:34:29 +00:00
|
|
|
}
|
2013-09-12 21:35:44 +00:00
|
|
|
|
2013-09-07 22:43:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-14 15:58:34 +00:00
|
|
|
if((config->getDeviceId() == DEVICE_KEYBOARD && input.id == SDLK_ESCAPE) || (config->getDeviceId() != DEVICE_KEYBOARD && config->isMappedTo("b", input)))
|
2013-09-07 22:43:36 +00:00
|
|
|
{
|
2014-03-21 02:47:45 +00:00
|
|
|
stopEditing();
|
2013-09-07 22:43:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(config->isMappedTo("up", input))
|
|
|
|
{
|
2014-03-21 02:47:45 +00:00
|
|
|
// TODO
|
2013-09-07 22:43:36 +00:00
|
|
|
}else if(config->isMappedTo("down", input))
|
|
|
|
{
|
2014-03-21 02:47:45 +00:00
|
|
|
// TODO
|
|
|
|
}else if(config->isMappedTo("left", input) || config->isMappedTo("right", input))
|
2013-09-07 22:43:36 +00:00
|
|
|
{
|
2014-03-21 02:47:45 +00:00
|
|
|
mCursorRepeatDir = config->isMappedTo("left", input) ? -1 : 1;
|
|
|
|
mCursorRepeatTimer = -(CURSOR_REPEAT_START_DELAY - CURSOR_REPEAT_SPEED);
|
|
|
|
moveCursor(mCursorRepeatDir);
|
2013-09-07 22:43:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//consume all input when editing text
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-08-21 19:49:33 +00:00
|
|
|
}
|
|
|
|
|
2014-03-21 02:47:45 +00:00
|
|
|
void TextEditComponent::update(int deltaTime)
|
2013-08-21 19:49:33 +00:00
|
|
|
{
|
2014-03-21 02:47:45 +00:00
|
|
|
updateCursorRepeat(deltaTime);
|
|
|
|
GuiComponent::update(deltaTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEditComponent::updateCursorRepeat(int deltaTime)
|
|
|
|
{
|
|
|
|
if(mCursorRepeatDir == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mCursorRepeatTimer += deltaTime;
|
|
|
|
while(mCursorRepeatTimer >= CURSOR_REPEAT_SPEED)
|
|
|
|
{
|
|
|
|
moveCursor(mCursorRepeatDir);
|
|
|
|
mCursorRepeatTimer -= CURSOR_REPEAT_SPEED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEditComponent::moveCursor(int amt)
|
|
|
|
{
|
|
|
|
mCursor += amt;
|
|
|
|
|
|
|
|
if(mCursor < 0)
|
|
|
|
mCursor = 0;
|
|
|
|
if(mCursor >= (int)mText.length())
|
|
|
|
mCursor = mText.length();
|
|
|
|
|
|
|
|
onCursorChanged();
|
|
|
|
}
|
2013-08-22 01:08:36 +00:00
|
|
|
|
2014-03-21 02:47:45 +00:00
|
|
|
void TextEditComponent::onTextChanged()
|
|
|
|
{
|
|
|
|
std::string wrappedText = (isMultiline() ? mFont->wrapText(mText, getTextAreaSize().x()) : mText);
|
|
|
|
mTextCache = std::unique_ptr<TextCache>(mFont->buildTextCache(wrappedText, 0, 0, 0x77777700 | getOpacity()));
|
2013-09-20 23:55:05 +00:00
|
|
|
|
|
|
|
if(mCursor > (int)mText.length())
|
|
|
|
mCursor = mText.length();
|
2013-08-21 19:49:33 +00:00
|
|
|
}
|
|
|
|
|
2013-09-07 22:43:36 +00:00
|
|
|
void TextEditComponent::onCursorChanged()
|
2013-08-21 19:49:33 +00:00
|
|
|
{
|
2013-09-12 21:35:44 +00:00
|
|
|
if(isMultiline())
|
2013-09-07 22:43:36 +00:00
|
|
|
{
|
2014-03-21 02:47:45 +00:00
|
|
|
Eigen::Vector2f textSize = mFont->getWrappedTextCursorOffset(mText, getTextAreaSize().x(), mCursor);
|
2013-09-12 21:35:44 +00:00
|
|
|
|
2014-03-21 02:47:45 +00:00
|
|
|
if(mScrollOffset.y() + getTextAreaSize().y() < textSize.y() + mFont->getHeight()) //need to scroll down?
|
2013-09-12 21:35:44 +00:00
|
|
|
{
|
2014-03-21 02:47:45 +00:00
|
|
|
mScrollOffset[1] = textSize.y() - getTextAreaSize().y() + mFont->getHeight();
|
2013-09-12 21:35:44 +00:00
|
|
|
}else if(mScrollOffset.y() > textSize.y()) //need to scroll up?
|
|
|
|
{
|
|
|
|
mScrollOffset[1] = textSize.y();
|
|
|
|
}
|
|
|
|
}else{
|
2014-03-21 02:47:45 +00:00
|
|
|
Eigen::Vector2f cursorPos = mFont->sizeText(mText.substr(0, mCursor));
|
2013-09-12 21:35:44 +00:00
|
|
|
|
2014-03-21 02:47:45 +00:00
|
|
|
if(mScrollOffset.x() + getTextAreaSize().x() < cursorPos.x())
|
2013-09-12 21:35:44 +00:00
|
|
|
{
|
2014-03-21 02:47:45 +00:00
|
|
|
mScrollOffset[0] = cursorPos.x() - getTextAreaSize().x();
|
2013-09-12 21:35:44 +00:00
|
|
|
}else if(mScrollOffset.x() > cursorPos.x())
|
|
|
|
{
|
|
|
|
mScrollOffset[0] = cursorPos.x();
|
|
|
|
}
|
2013-09-07 22:43:36 +00:00
|
|
|
}
|
2013-08-19 15:36:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextEditComponent::render(const Eigen::Affine3f& parentTrans)
|
|
|
|
{
|
|
|
|
Eigen::Affine3f trans = getTransform() * parentTrans;
|
|
|
|
renderChildren(trans);
|
|
|
|
|
2014-03-21 02:47:45 +00:00
|
|
|
// text + cursor rendering
|
|
|
|
// offset into our "text area" (padding)
|
|
|
|
trans.translation() += Eigen::Vector3f(getTextAreaPos().x(), getTextAreaPos().y(), 0);
|
|
|
|
|
2013-09-07 22:43:36 +00:00
|
|
|
Eigen::Vector2i clipPos((int)trans.translation().x(), (int)trans.translation().y());
|
2014-03-21 02:47:45 +00:00
|
|
|
Eigen::Vector3f dimScaled = trans * Eigen::Vector3f(getTextAreaSize().x(), getTextAreaSize().y(), 0); // use "text area" size for clipping
|
2013-09-07 22:43:36 +00:00
|
|
|
Eigen::Vector2i clipDim((int)dimScaled.x() - trans.translation().x(), (int)dimScaled.y() - trans.translation().y());
|
|
|
|
Renderer::pushClipRect(clipPos, clipDim);
|
|
|
|
|
2013-09-12 21:35:44 +00:00
|
|
|
trans.translate(Eigen::Vector3f(-mScrollOffset.x(), -mScrollOffset.y(), 0));
|
2014-03-21 02:47:45 +00:00
|
|
|
trans = roundMatrix(trans);
|
2013-09-07 22:43:36 +00:00
|
|
|
|
2013-08-19 15:36:48 +00:00
|
|
|
Renderer::setMatrix(trans);
|
2013-09-07 22:43:36 +00:00
|
|
|
|
2014-03-21 02:47:45 +00:00
|
|
|
if(mTextCache)
|
2013-08-22 01:08:36 +00:00
|
|
|
{
|
2014-03-21 02:47:45 +00:00
|
|
|
mFont->renderTextCache(mTextCache.get());
|
2013-08-22 01:08:36 +00:00
|
|
|
}
|
2013-09-07 22:43:36 +00:00
|
|
|
|
2014-03-21 02:47:45 +00:00
|
|
|
// pop the clip early to allow the cursor to be drawn outside of the "text area"
|
|
|
|
Renderer::popClipRect();
|
|
|
|
|
|
|
|
// draw cursor
|
2013-09-07 22:43:36 +00:00
|
|
|
if(mEditing)
|
|
|
|
{
|
2013-09-12 21:35:44 +00:00
|
|
|
Eigen::Vector2f cursorPos;
|
|
|
|
if(isMultiline())
|
|
|
|
{
|
2014-03-21 02:47:45 +00:00
|
|
|
cursorPos = mFont->getWrappedTextCursorOffset(mText, getTextAreaSize().x(), mCursor);
|
2013-09-12 21:35:44 +00:00
|
|
|
}else{
|
2014-03-21 02:47:45 +00:00
|
|
|
cursorPos = mFont->sizeText(mText.substr(0, mCursor));
|
2013-09-12 21:35:44 +00:00
|
|
|
cursorPos[1] = 0;
|
|
|
|
}
|
|
|
|
|
2014-03-21 02:47:45 +00:00
|
|
|
float cursorHeight = mFont->getHeight() * 0.8f;
|
|
|
|
Renderer::drawRect(cursorPos.x(), cursorPos.y() + (mFont->getHeight() - cursorHeight) / 2, 2.0f, cursorHeight, 0x000000FF);
|
2013-09-07 22:43:36 +00:00
|
|
|
}
|
2013-08-19 15:36:48 +00:00
|
|
|
}
|
|
|
|
|
2014-03-21 02:47:45 +00:00
|
|
|
bool TextEditComponent::isMultiline()
|
2013-08-19 15:36:48 +00:00
|
|
|
{
|
2014-03-21 02:47:45 +00:00
|
|
|
return (getSize().y() > mFont->getHeight() * 1.25f);
|
2013-08-18 14:16:11 +00:00
|
|
|
}
|
2013-09-12 21:35:44 +00:00
|
|
|
|
2014-03-21 02:47:45 +00:00
|
|
|
Eigen::Vector2f TextEditComponent::getTextAreaPos() const
|
2013-09-12 21:35:44 +00:00
|
|
|
{
|
2014-03-21 02:47:45 +00:00
|
|
|
return Eigen::Vector2f(TEXT_PADDING_HORIZ / 2.0f, TEXT_PADDING_VERT / 2.0f);
|
2013-09-12 21:35:44 +00:00
|
|
|
}
|
|
|
|
|
2014-03-21 02:47:45 +00:00
|
|
|
Eigen::Vector2f TextEditComponent::getTextAreaSize() const
|
2013-09-20 23:55:05 +00:00
|
|
|
{
|
2014-03-21 02:47:45 +00:00
|
|
|
return Eigen::Vector2f(mSize.x() - TEXT_PADDING_HORIZ, mSize.y() - TEXT_PADDING_VERT);
|
2013-09-20 23:55:05 +00:00
|
|
|
}
|
2014-01-25 23:34:29 +00:00
|
|
|
|
|
|
|
std::vector<HelpPrompt> TextEditComponent::getHelpPrompts()
|
|
|
|
{
|
|
|
|
std::vector<HelpPrompt> prompts;
|
|
|
|
if(mEditing)
|
|
|
|
{
|
|
|
|
prompts.push_back(HelpPrompt("up/down/left/right", "move cursor"));
|
|
|
|
prompts.push_back(HelpPrompt("b", "stop editing"));
|
|
|
|
}else{
|
|
|
|
prompts.push_back(HelpPrompt("a", "edit"));
|
|
|
|
}
|
|
|
|
return prompts;
|
|
|
|
}
|