2014-06-20 01:30:09 +00:00
|
|
|
#include "components/TextEditComponent.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "resources/Font.h"
|
2017-11-15 15:59:39 +00:00
|
|
|
#include "utils/StringUtil.h"
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "Renderer.h"
|
2014-03-21 02:47:45 +00:00
|
|
|
|
|
|
|
#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-25 23:10:35 +00:00
|
|
|
mBox(window, ":/textinput_ninepatch.png"), mFocused(false),
|
2014-10-18 20:46:14 +00:00
|
|
|
mScrollOffset(0.0f, 0.0f), mCursor(0), mEditing(false), mFont(Font::get(FONT_SIZE_MEDIUM, FONT_PATH_LIGHT)),
|
|
|
|
mCursorRepeatDir(0)
|
2013-08-18 14:16:11 +00:00
|
|
|
{
|
|
|
|
addChild(&mBox);
|
|
|
|
|
|
|
|
onFocusLost();
|
|
|
|
|
2017-02-18 22:35:12 +00:00
|
|
|
setSize(4096, 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;
|
2014-04-19 21:58:01 +00:00
|
|
|
mBox.setImagePath(":/textinput_ninepatch_active.png");
|
2013-08-18 14:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextEditComponent::onFocusLost()
|
|
|
|
{
|
2013-08-19 15:36:48 +00:00
|
|
|
mFocused = false;
|
2014-04-19 21:58:01 +00:00
|
|
|
mBox.setImagePath(":/textinput_ninepatch.png");
|
2013-08-18 14:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextEditComponent::onSizeChanged()
|
|
|
|
{
|
2017-10-28 20:24:35 +00:00
|
|
|
mBox.fitTo(mSize, Vector3f::Zero(), Vector2f(-34, -32 - TEXT_PADDING_VERT));
|
2014-03-21 02:47:45 +00:00
|
|
|
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)
|
|
|
|
{
|
2017-11-15 15:59:39 +00:00
|
|
|
size_t newCursor = Utils::String::prevCursor(mText, mCursor);
|
2014-08-30 20:37:51 +00:00
|
|
|
mText.erase(mText.begin() + newCursor, mText.begin() + mCursor);
|
2017-11-17 14:58:52 +00:00
|
|
|
mCursor = (unsigned int)newCursor;
|
2013-09-07 22:43:36 +00:00
|
|
|
}
|
2013-08-19 15:36:48 +00:00
|
|
|
}else{
|
2013-09-07 22:43:36 +00:00
|
|
|
mText.insert(mCursor, text);
|
2017-11-17 14:58:52 +00:00
|
|
|
mCursor += (unsigned int)strlen(text);
|
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)
|
|
|
|
{
|
2017-02-18 22:35:12 +00:00
|
|
|
bool const cursor_left = (config->getDeviceId() != DEVICE_KEYBOARD && config->isMappedTo("left", input)) ||
|
|
|
|
(config->getDeviceId() == DEVICE_KEYBOARD && input.id == SDLK_LEFT);
|
|
|
|
bool const cursor_right = (config->getDeviceId() != DEVICE_KEYBOARD && config->isMappedTo("right", input)) ||
|
|
|
|
(config->getDeviceId() == DEVICE_KEYBOARD && input.id == SDLK_RIGHT);
|
|
|
|
|
2013-09-07 22:43:36 +00:00
|
|
|
if(input.value == 0)
|
2014-03-21 02:47:45 +00:00
|
|
|
{
|
2017-02-18 22:35:12 +00:00
|
|
|
if(cursor_left || cursor_right)
|
2014-03-21 02:47:45 +00:00
|
|
|
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
|
|
|
|
2017-02-18 22:35:12 +00:00
|
|
|
if((config->isMappedTo("a", input) || (config->getDeviceId() == DEVICE_KEYBOARD && input.id == SDLK_RETURN)) && mFocused && !mEditing)
|
2013-09-07 22:43:36 +00:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2017-02-18 22:35:12 +00:00
|
|
|
if(config->getDeviceId() != DEVICE_KEYBOARD && config->isMappedTo("up", input))
|
2013-09-07 22:43:36 +00:00
|
|
|
{
|
2014-03-21 02:47:45 +00:00
|
|
|
// TODO
|
2017-02-18 22:35:12 +00:00
|
|
|
}else if(config->getDeviceId() != DEVICE_KEYBOARD && config->isMappedTo("down", input))
|
2013-09-07 22:43:36 +00:00
|
|
|
{
|
2014-03-21 02:47:45 +00:00
|
|
|
// TODO
|
2017-02-18 22:35:12 +00:00
|
|
|
}else if(cursor_left || cursor_right)
|
2013-09-07 22:43:36 +00:00
|
|
|
{
|
2017-02-18 22:35:12 +00:00
|
|
|
mCursorRepeatDir = cursor_left ? -1 : 1;
|
2014-03-21 02:47:45 +00:00
|
|
|
mCursorRepeatTimer = -(CURSOR_REPEAT_START_DELAY - CURSOR_REPEAT_SPEED);
|
|
|
|
moveCursor(mCursorRepeatDir);
|
2017-02-18 22:35:12 +00:00
|
|
|
} else if(config->getDeviceId() == DEVICE_KEYBOARD)
|
|
|
|
{
|
|
|
|
switch(input.id)
|
|
|
|
{
|
|
|
|
case SDLK_HOME:
|
|
|
|
setCursor(0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDLK_END:
|
|
|
|
setCursor(std::string::npos);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDLK_DELETE:
|
|
|
|
if(mCursor < mText.length())
|
|
|
|
{
|
|
|
|
// Fake as Backspace one char to the right
|
|
|
|
moveCursor(1);
|
|
|
|
textInput("\b");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
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)
|
|
|
|
{
|
2017-11-17 14:58:52 +00:00
|
|
|
mCursor = (unsigned int)Utils::String::moveCursor(mText, mCursor, amt);
|
2014-03-21 02:47:45 +00:00
|
|
|
onCursorChanged();
|
|
|
|
}
|
2013-08-22 01:08:36 +00:00
|
|
|
|
2014-06-15 17:55:30 +00:00
|
|
|
void TextEditComponent::setCursor(size_t pos)
|
|
|
|
{
|
|
|
|
if(pos == std::string::npos)
|
2017-11-17 14:58:52 +00:00
|
|
|
mCursor = (unsigned int)mText.length();
|
2014-06-15 17:55:30 +00:00
|
|
|
else
|
|
|
|
mCursor = (int)pos;
|
|
|
|
|
|
|
|
moveCursor(0);
|
|
|
|
}
|
|
|
|
|
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())
|
2017-11-17 14:58:52 +00:00
|
|
|
mCursor = (unsigned int)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
|
|
|
{
|
2017-10-28 20:24:35 +00:00
|
|
|
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{
|
2017-10-28 20:24:35 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
void TextEditComponent::render(const Transform4x4f& parentTrans)
|
2013-08-19 15:36:48 +00:00
|
|
|
{
|
2017-10-28 20:24:35 +00:00
|
|
|
Transform4x4f trans = getTransform() * parentTrans;
|
2013-08-19 15:36:48 +00:00
|
|
|
renderChildren(trans);
|
|
|
|
|
2014-03-21 02:47:45 +00:00
|
|
|
// text + cursor rendering
|
|
|
|
// offset into our "text area" (padding)
|
2017-10-28 20:24:35 +00:00
|
|
|
trans.translation() += Vector3f(getTextAreaPos().x(), getTextAreaPos().y(), 0);
|
2014-03-21 02:47:45 +00:00
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2i clipPos((int)trans.translation().x(), (int)trans.translation().y());
|
|
|
|
Vector3f dimScaled = trans * Vector3f(getTextAreaSize().x(), getTextAreaSize().y(), 0); // use "text area" size for clipping
|
2017-11-17 14:58:52 +00:00
|
|
|
Vector2i clipDim((int)(dimScaled.x() - trans.translation().x()), (int)(dimScaled.y() - trans.translation().y()));
|
2013-09-07 22:43:36 +00:00
|
|
|
Renderer::pushClipRect(clipPos, clipDim);
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
trans.translate(Vector3f(-mScrollOffset.x(), -mScrollOffset.y(), 0));
|
|
|
|
trans.round();
|
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)
|
|
|
|
{
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2f cursorPos;
|
2013-09-12 21:35:44 +00:00
|
|
|
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
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2f TextEditComponent::getTextAreaPos() const
|
2013-09-12 21:35:44 +00:00
|
|
|
{
|
2017-10-28 20:24:35 +00:00
|
|
|
return Vector2f(TEXT_PADDING_HORIZ / 2.0f, TEXT_PADDING_VERT / 2.0f);
|
2013-09-12 21:35:44 +00:00
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2f TextEditComponent::getTextAreaSize() const
|
2013-09-20 23:55:05 +00:00
|
|
|
{
|
2017-10-28 20:24:35 +00:00
|
|
|
return 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;
|
|
|
|
}
|