From f4e24bdd5618e20c72c80ef3f1c873c053c35aa5 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Fri, 18 Sep 2020 18:16:12 +0200 Subject: [PATCH] Fixed an issue where the cursor would end up outside text fields for long strings. --- es-core/src/components/TextEditComponent.cpp | 25 ++++++++++++-------- es-core/src/components/TextEditComponent.h | 3 ++- es-core/src/guis/GuiComplexTextEditPopup.cpp | 9 ++++--- es-core/src/guis/GuiComplexTextEditPopup.h | 3 ++- es-core/src/guis/GuiTextEditPopup.cpp | 7 +++--- es-core/src/guis/GuiTextEditPopup.h | 3 ++- 6 files changed, 28 insertions(+), 22 deletions(-) diff --git a/es-core/src/components/TextEditComponent.cpp b/es-core/src/components/TextEditComponent.cpp index eebc423ce..ec8b0b6bb 100644 --- a/es-core/src/components/TextEditComponent.cpp +++ b/es-core/src/components/TextEditComponent.cpp @@ -1,4 +1,6 @@ +// SPDX-License-Identifier: MIT // +// EmulationStation Desktop Edition // TextEditComponent.cpp // // Component for editing text fields in menus. @@ -68,12 +70,12 @@ void TextEditComponent::textInput(const char* text) if (mCursor > 0) { size_t newCursor = Utils::String::prevCursor(mText, mCursor); mText.erase(mText.begin() + newCursor, mText.begin() + mCursor); - mCursor = (unsigned int)newCursor; + mCursor = static_cast(newCursor); } } else { mText.insert(mCursor, text); - mCursor += (unsigned int)strlen(text); + mCursor += static_cast(strlen(text)); } } @@ -83,6 +85,8 @@ void TextEditComponent::textInput(const char* text) void TextEditComponent::startEditing() { + if (!isMultiline()) + setCursor(mText.size()); SDL_StartTextInput(); mEditing = true; updateHelpPrompts(); @@ -219,16 +223,16 @@ void TextEditComponent::updateCursorRepeat(int deltaTime) void TextEditComponent::moveCursor(int amt) { - mCursor = (unsigned int)Utils::String::moveCursor(mText, mCursor, amt); + mCursor = static_cast(Utils::String::moveCursor(mText, mCursor, amt)); onCursorChanged(); } void TextEditComponent::setCursor(size_t pos) { if (pos == std::string::npos) - mCursor = (unsigned int)mText.length(); + mCursor = static_cast(mText.length()); else - mCursor = (int)pos; + mCursor = static_cast(pos); moveCursor(0); } @@ -240,8 +244,8 @@ void TextEditComponent::onTextChanged() mTextCache = std::unique_ptr (mFont->buildTextCache(wrappedText, 0, 0, 0x77777700 | getOpacity())); - if (mCursor > (int)mText.length()) - mCursor = (unsigned int)mText.length(); + if (mCursor > static_cast(mText.length())) + mCursor = static_cast(mText.length()); } void TextEditComponent::onCursorChanged() @@ -275,11 +279,12 @@ void TextEditComponent::render(const Transform4x4f& parentTrans) // Offset into our "text area" (padding). trans.translation() += Vector3f(getTextAreaPos().x(), getTextAreaPos().y(), 0); - Vector2i clipPos((int)trans.translation().x(), (int)trans.translation().y()); + Vector2i clipPos(static_cast(trans.translation().x()), + static_cast(trans.translation().y())); // Use "text area" size for clipping. Vector3f dimScaled = trans * Vector3f(getTextAreaSize().x(), getTextAreaSize().y(), 0); - Vector2i clipDim((int)(dimScaled.x() - trans.translation().x()), (int)(dimScaled.y() - - trans.translation().y())); + Vector2i clipDim(static_cast((dimScaled.x()) - trans.translation().x()), + static_cast((dimScaled.y()) - trans.translation().y())); Renderer::pushClipRect(clipPos, clipDim); trans.translate(Vector3f(-mScrollOffset.x(), -mScrollOffset.y(), 0)); diff --git a/es-core/src/components/TextEditComponent.h b/es-core/src/components/TextEditComponent.h index d94e431e9..e764a6915 100644 --- a/es-core/src/components/TextEditComponent.h +++ b/es-core/src/components/TextEditComponent.h @@ -1,10 +1,11 @@ +// SPDX-License-Identifier: MIT // +// EmulationStation Desktop Edition // TextEditComponent.h // // Component for editing text fields in menus. // -#pragma once #ifndef ES_CORE_COMPONENTS_TEXT_EDIT_COMPONENT_H #define ES_CORE_COMPONENTS_TEXT_EDIT_COMPONENT_H diff --git a/es-core/src/guis/GuiComplexTextEditPopup.cpp b/es-core/src/guis/GuiComplexTextEditPopup.cpp index 41d4bfc0a..1cfb1a982 100644 --- a/es-core/src/guis/GuiComplexTextEditPopup.cpp +++ b/es-core/src/guis/GuiComplexTextEditPopup.cpp @@ -1,4 +1,6 @@ +// SPDX-License-Identifier: MIT // +// EmulationStation Desktop Edition // GuiComplexTextEditPopup.cpp // // 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/GuiMsgBox.h" #include "components/ButtonComponent.h" #include "components/MenuComponent.h" #include "components/TextEditComponent.h" +#include "guis/GuiMsgBox.h" #include "Window.h" GuiComplexTextEditPopup::GuiComplexTextEditPopup( @@ -53,15 +55,12 @@ GuiComplexTextEditPopup::GuiComplexTextEditPopup( mText = std::make_shared(mWindow); mText->setValue(initValue); - if (!multiLine) - mText->setCursor(initValue.size()); - std::vector< std::shared_ptr > buttons; buttons.push_back(std::make_shared(mWindow, acceptBtnText, acceptBtnText, [this, okCallback] { okCallback(mText->getValue()); delete this; })); buttons.push_back(std::make_shared(mWindow, loadBtnText, loadBtnHelpText, [this, infoString2] { - mText->setValue(infoString2); mText->setCursor(infoString2.size()); })); + mText->setValue(infoString2); mText->setCursor(infoString2.size()); })); buttons.push_back(std::make_shared(mWindow, clearBtnText, clearBtnHelpText, [this] { mText->setValue(""); })); if (!mHideCancelButton) diff --git a/es-core/src/guis/GuiComplexTextEditPopup.h b/es-core/src/guis/GuiComplexTextEditPopup.h index 09ef600b2..5eb562eec 100644 --- a/es-core/src/guis/GuiComplexTextEditPopup.h +++ b/es-core/src/guis/GuiComplexTextEditPopup.h @@ -1,4 +1,6 @@ +// SPDX-License-Identifier: MIT // +// EmulationStation Desktop Edition // GuiComplexTextEditPopup.h // // 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. // -#pragma once #ifndef ES_CORE_GUIS_GUI_COMPLEX_TEXT_EDIT_POPUP_H #define ES_CORE_GUIS_GUI_COMPLEX_TEXT_EDIT_POPUP_H diff --git a/es-core/src/guis/GuiTextEditPopup.cpp b/es-core/src/guis/GuiTextEditPopup.cpp index 0e90dd1f0..d20752fec 100644 --- a/es-core/src/guis/GuiTextEditPopup.cpp +++ b/es-core/src/guis/GuiTextEditPopup.cpp @@ -1,15 +1,17 @@ +// SPDX-License-Identifier: MIT // +// EmulationStation Desktop Edition // GuiTextEditPopup.cpp // // Simple text edit popup with a title, a text input box and OK and Cancel buttons. // #include "guis/GuiTextEditPopup.h" -#include "guis/GuiMsgBox.h" #include "components/ButtonComponent.h" #include "components/MenuComponent.h" #include "components/TextEditComponent.h" +#include "guis/GuiMsgBox.h" #include "Window.h" GuiTextEditPopup::GuiTextEditPopup( @@ -39,9 +41,6 @@ GuiTextEditPopup::GuiTextEditPopup( mText = std::make_shared(mWindow); mText->setValue(initValue); - if (!multiLine) - mText->setCursor(initValue.size()); - std::vector< std::shared_ptr > buttons; buttons.push_back(std::make_shared(mWindow, acceptBtnText, acceptBtnText, [this, okCallback] { okCallback(mText->getValue()); delete this; })); diff --git a/es-core/src/guis/GuiTextEditPopup.h b/es-core/src/guis/GuiTextEditPopup.h index ec0f97689..0853c78ae 100644 --- a/es-core/src/guis/GuiTextEditPopup.h +++ b/es-core/src/guis/GuiTextEditPopup.h @@ -1,10 +1,11 @@ +// SPDX-License-Identifier: MIT // +// EmulationStation Desktop Edition // GuiTextEditPopup.h // // 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 #define ES_CORE_GUIS_GUI_TEXT_EDIT_POPUP_H