Fixed an issue where the cursor would end up outside text fields for long strings.

This commit is contained in:
Leon Styhre 2020-09-18 18:16:12 +02:00
parent 14e2ede448
commit f4e24bdd56
6 changed files with 28 additions and 22 deletions

View file

@ -1,4 +1,6 @@
// SPDX-License-Identifier: MIT
// //
// EmulationStation Desktop Edition
// TextEditComponent.cpp // TextEditComponent.cpp
// //
// Component for editing text fields in menus. // Component for editing text fields in menus.
@ -68,12 +70,12 @@ void TextEditComponent::textInput(const char* text)
if (mCursor > 0) { if (mCursor > 0) {
size_t newCursor = Utils::String::prevCursor(mText, mCursor); size_t newCursor = Utils::String::prevCursor(mText, mCursor);
mText.erase(mText.begin() + newCursor, mText.begin() + mCursor); mText.erase(mText.begin() + newCursor, mText.begin() + mCursor);
mCursor = (unsigned int)newCursor; mCursor = static_cast<unsigned int>(newCursor);
} }
} }
else { else {
mText.insert(mCursor, text); mText.insert(mCursor, text);
mCursor += (unsigned int)strlen(text); mCursor += static_cast<unsigned int>(strlen(text));
} }
} }
@ -83,6 +85,8 @@ void TextEditComponent::textInput(const char* text)
void TextEditComponent::startEditing() void TextEditComponent::startEditing()
{ {
if (!isMultiline())
setCursor(mText.size());
SDL_StartTextInput(); SDL_StartTextInput();
mEditing = true; mEditing = true;
updateHelpPrompts(); updateHelpPrompts();
@ -219,16 +223,16 @@ void TextEditComponent::updateCursorRepeat(int deltaTime)
void TextEditComponent::moveCursor(int amt) void TextEditComponent::moveCursor(int amt)
{ {
mCursor = (unsigned int)Utils::String::moveCursor(mText, mCursor, amt); mCursor = static_cast<unsigned int>(Utils::String::moveCursor(mText, mCursor, amt));
onCursorChanged(); onCursorChanged();
} }
void TextEditComponent::setCursor(size_t pos) void TextEditComponent::setCursor(size_t pos)
{ {
if (pos == std::string::npos) if (pos == std::string::npos)
mCursor = (unsigned int)mText.length(); mCursor = static_cast<unsigned int>(mText.length());
else else
mCursor = (int)pos; mCursor = static_cast<int>(pos);
moveCursor(0); moveCursor(0);
} }
@ -240,8 +244,8 @@ void TextEditComponent::onTextChanged()
mTextCache = std::unique_ptr<TextCache> mTextCache = std::unique_ptr<TextCache>
(mFont->buildTextCache(wrappedText, 0, 0, 0x77777700 | getOpacity())); (mFont->buildTextCache(wrappedText, 0, 0, 0x77777700 | getOpacity()));
if (mCursor > (int)mText.length()) if (mCursor > static_cast<int>(mText.length()))
mCursor = (unsigned int)mText.length(); mCursor = static_cast<unsigned int>(mText.length());
} }
void TextEditComponent::onCursorChanged() void TextEditComponent::onCursorChanged()
@ -275,11 +279,12 @@ void TextEditComponent::render(const Transform4x4f& parentTrans)
// Offset into our "text area" (padding). // Offset into our "text area" (padding).
trans.translation() += Vector3f(getTextAreaPos().x(), getTextAreaPos().y(), 0); trans.translation() += Vector3f(getTextAreaPos().x(), getTextAreaPos().y(), 0);
Vector2i clipPos((int)trans.translation().x(), (int)trans.translation().y()); Vector2i clipPos(static_cast<int>(trans.translation().x()),
static_cast<int>(trans.translation().y()));
// Use "text area" size for clipping. // Use "text area" size for clipping.
Vector3f dimScaled = trans * Vector3f(getTextAreaSize().x(), getTextAreaSize().y(), 0); Vector3f dimScaled = trans * Vector3f(getTextAreaSize().x(), getTextAreaSize().y(), 0);
Vector2i clipDim((int)(dimScaled.x() - trans.translation().x()), (int)(dimScaled.y() - Vector2i clipDim(static_cast<int>((dimScaled.x()) - trans.translation().x()),
trans.translation().y())); static_cast<int>((dimScaled.y()) - trans.translation().y()));
Renderer::pushClipRect(clipPos, clipDim); Renderer::pushClipRect(clipPos, clipDim);
trans.translate(Vector3f(-mScrollOffset.x(), -mScrollOffset.y(), 0)); trans.translate(Vector3f(-mScrollOffset.x(), -mScrollOffset.y(), 0));

View file

@ -1,10 +1,11 @@
// SPDX-License-Identifier: MIT
// //
// EmulationStation Desktop Edition
// TextEditComponent.h // TextEditComponent.h
// //
// Component for editing text fields in menus. // Component for editing text fields in menus.
// //
#pragma once
#ifndef ES_CORE_COMPONENTS_TEXT_EDIT_COMPONENT_H #ifndef ES_CORE_COMPONENTS_TEXT_EDIT_COMPONENT_H
#define ES_CORE_COMPONENTS_TEXT_EDIT_COMPONENT_H #define ES_CORE_COMPONENTS_TEXT_EDIT_COMPONENT_H

View file

@ -1,4 +1,6 @@
// SPDX-License-Identifier: MIT
// //
// EmulationStation Desktop Edition
// GuiComplexTextEditPopup.cpp // GuiComplexTextEditPopup.cpp
// //
// Text edit popup with a title, two text strings, a text input box and buttons // Text edit popup with a title, two text strings, a text input box and buttons
@ -7,11 +9,11 @@
// //
#include "guis/GuiComplexTextEditPopup.h" #include "guis/GuiComplexTextEditPopup.h"
#include "guis/GuiMsgBox.h"
#include "components/ButtonComponent.h" #include "components/ButtonComponent.h"
#include "components/MenuComponent.h" #include "components/MenuComponent.h"
#include "components/TextEditComponent.h" #include "components/TextEditComponent.h"
#include "guis/GuiMsgBox.h"
#include "Window.h" #include "Window.h"
GuiComplexTextEditPopup::GuiComplexTextEditPopup( GuiComplexTextEditPopup::GuiComplexTextEditPopup(
@ -53,15 +55,12 @@ GuiComplexTextEditPopup::GuiComplexTextEditPopup(
mText = std::make_shared<TextEditComponent>(mWindow); mText = std::make_shared<TextEditComponent>(mWindow);
mText->setValue(initValue); mText->setValue(initValue);
if (!multiLine)
mText->setCursor(initValue.size());
std::vector< std::shared_ptr<ButtonComponent> > buttons; std::vector< std::shared_ptr<ButtonComponent> > buttons;
buttons.push_back(std::make_shared<ButtonComponent>(mWindow, acceptBtnText, acceptBtnText, buttons.push_back(std::make_shared<ButtonComponent>(mWindow, acceptBtnText, acceptBtnText,
[this, okCallback] { okCallback(mText->getValue()); delete this; })); [this, okCallback] { okCallback(mText->getValue()); delete this; }));
buttons.push_back(std::make_shared<ButtonComponent>(mWindow, loadBtnText, loadBtnHelpText, buttons.push_back(std::make_shared<ButtonComponent>(mWindow, loadBtnText, loadBtnHelpText,
[this, infoString2] { [this, infoString2] {
mText->setValue(infoString2); mText->setCursor(infoString2.size()); })); mText->setValue(infoString2); mText->setCursor(infoString2.size()); }));
buttons.push_back(std::make_shared<ButtonComponent>(mWindow, clearBtnText, clearBtnHelpText, buttons.push_back(std::make_shared<ButtonComponent>(mWindow, clearBtnText, clearBtnHelpText,
[this] { mText->setValue(""); })); [this] { mText->setValue(""); }));
if (!mHideCancelButton) if (!mHideCancelButton)

View file

@ -1,4 +1,6 @@
// SPDX-License-Identifier: MIT
// //
// EmulationStation Desktop Edition
// GuiComplexTextEditPopup.h // GuiComplexTextEditPopup.h
// //
// Text edit popup with a title, two text strings, a text input box and buttons // Text edit popup with a title, two text strings, a text input box and buttons
@ -6,7 +8,6 @@
// Intended for updating settings for configuration files and similar. // Intended for updating settings for configuration files and similar.
// //
#pragma once
#ifndef ES_CORE_GUIS_GUI_COMPLEX_TEXT_EDIT_POPUP_H #ifndef ES_CORE_GUIS_GUI_COMPLEX_TEXT_EDIT_POPUP_H
#define ES_CORE_GUIS_GUI_COMPLEX_TEXT_EDIT_POPUP_H #define ES_CORE_GUIS_GUI_COMPLEX_TEXT_EDIT_POPUP_H

View file

@ -1,15 +1,17 @@
// SPDX-License-Identifier: MIT
// //
// EmulationStation Desktop Edition
// GuiTextEditPopup.cpp // GuiTextEditPopup.cpp
// //
// Simple text edit popup with a title, a text input box and OK and Cancel buttons. // Simple text edit popup with a title, a text input box and OK and Cancel buttons.
// //
#include "guis/GuiTextEditPopup.h" #include "guis/GuiTextEditPopup.h"
#include "guis/GuiMsgBox.h"
#include "components/ButtonComponent.h" #include "components/ButtonComponent.h"
#include "components/MenuComponent.h" #include "components/MenuComponent.h"
#include "components/TextEditComponent.h" #include "components/TextEditComponent.h"
#include "guis/GuiMsgBox.h"
#include "Window.h" #include "Window.h"
GuiTextEditPopup::GuiTextEditPopup( GuiTextEditPopup::GuiTextEditPopup(
@ -39,9 +41,6 @@ GuiTextEditPopup::GuiTextEditPopup(
mText = std::make_shared<TextEditComponent>(mWindow); mText = std::make_shared<TextEditComponent>(mWindow);
mText->setValue(initValue); mText->setValue(initValue);
if (!multiLine)
mText->setCursor(initValue.size());
std::vector< std::shared_ptr<ButtonComponent> > buttons; std::vector< std::shared_ptr<ButtonComponent> > buttons;
buttons.push_back(std::make_shared<ButtonComponent>(mWindow, acceptBtnText, acceptBtnText, buttons.push_back(std::make_shared<ButtonComponent>(mWindow, acceptBtnText, acceptBtnText,
[this, okCallback] { okCallback(mText->getValue()); delete this; })); [this, okCallback] { okCallback(mText->getValue()); delete this; }));

View file

@ -1,10 +1,11 @@
// SPDX-License-Identifier: MIT
// //
// EmulationStation Desktop Edition
// GuiTextEditPopup.h // GuiTextEditPopup.h
// //
// Simple text edit popup with a title, a text input box and OK and Cancel buttons. // Simple text edit popup with a title, a text input box and OK and Cancel buttons.
// //
#pragma once
#ifndef ES_CORE_GUIS_GUI_TEXT_EDIT_POPUP_H #ifndef ES_CORE_GUIS_GUI_TEXT_EDIT_POPUP_H
#define ES_CORE_GUIS_GUI_TEXT_EDIT_POPUP_H #define ES_CORE_GUIS_GUI_TEXT_EDIT_POPUP_H