From aba1163c44e8e04fb61f595bf626f93bf946f912 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Sun, 11 Aug 2024 19:01:47 +0200 Subject: [PATCH] Changed TextEditComponent to use TextComponent instead of using Font facilities directly --- es-core/src/components/TextEditComponent.cpp | 36 +++++++++++--------- es-core/src/components/TextEditComponent.h | 16 +++------ 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/es-core/src/components/TextEditComponent.cpp b/es-core/src/components/TextEditComponent.cpp index 6105a6b12..4d122e998 100644 --- a/es-core/src/components/TextEditComponent.cpp +++ b/es-core/src/components/TextEditComponent.cpp @@ -3,7 +3,7 @@ // ES-DE Frontend // TextEditComponent.cpp // -// Component for editing text fields in menus. +// Component for editing text fields. // #include "components/TextEditComponent.h" @@ -34,12 +34,14 @@ TextEditComponent::TextEditComponent() , mScrollOffset {0.0f, 0.0f} , mCursorPos {0.0f, 0.0f} , mBox {":/graphics/textinput.svg"} - , mFont {Font::get(FONT_SIZE_MEDIUM, FONT_PATH_LIGHT)} { + mEditText = std::make_unique("", Font::get(FONT_SIZE_MEDIUM, FONT_PATH_LIGHT)); + mBox.setSharpCorners(true); addChild(&mBox); onFocusLost(); - setSize(4096, mFont->getHeight() + (TEXT_PADDING_VERT * mRenderer->getScreenHeightModifier())); + setSize(4096, + getFont()->getHeight() + (TEXT_PADDING_VERT * mRenderer->getScreenHeightModifier())); } TextEditComponent::~TextEditComponent() @@ -310,10 +312,11 @@ void TextEditComponent::setCursor(size_t pos) void TextEditComponent::onTextChanged() { mWrappedText = - (isMultiline() ? mFont->wrapText(mText, getTextAreaSize().x, 0.0f, 1.5f, true) : mText); - mTextCache = std::unique_ptr(mFont->buildTextCache( - mWrappedText, 0.0f, 0.0f, - mMenuColorKeyboardText | static_cast(mOpacity * 255.0f))); + (isMultiline() ? getFont()->wrapText(mText, getTextAreaSize().x, 0.0f, 1.5f, true) : mText); + mEditText->setText(mWrappedText); + // Setting the Y size to zero makes the text area expand vertically as needed. + mEditText->setSize(mEditText->getSize().x, 0.0f); + mEditText->setColor(mMenuColorKeyboardText | static_cast(mOpacity * 255.0f)); if (mCursor > static_cast(mText.length())) mCursor = static_cast(mText.length()); @@ -322,17 +325,17 @@ void TextEditComponent::onTextChanged() void TextEditComponent::onCursorChanged() { if (isMultiline()) { - mCursorPos = mFont->getWrappedTextCursorOffset(mWrappedText, mCursor); + mCursorPos = getFont()->getWrappedTextCursorOffset(mWrappedText, mCursor); // Need to scroll down? - if (mScrollOffset.y + getTextAreaSize().y < mCursorPos.y + mFont->getHeight()) - mScrollOffset.y = mCursorPos.y - getTextAreaSize().y + mFont->getHeight(); + if (mScrollOffset.y + getTextAreaSize().y < mCursorPos.y + getFont()->getHeight()) + mScrollOffset.y = mCursorPos.y - getTextAreaSize().y + getFont()->getHeight(); // Need to scroll up? else if (mScrollOffset.y > mCursorPos.y) mScrollOffset.y = mCursorPos.y; } else { - mCursorPos = mFont->sizeText(mText.substr(0, mCursor)); + mCursorPos = getFont()->sizeText(mText.substr(0, mCursor)); mCursorPos.y = 0.0f; if (mScrollOffset.x + getTextAreaSize().x < mCursorPos.x) @@ -364,24 +367,23 @@ void TextEditComponent::render(const glm::mat4& parentTrans) trans = glm::translate(trans, glm::round(glm::vec3 {-mScrollOffset.x, -mScrollOffset.y, 0.0f})); mRenderer->setMatrix(trans); - - if (mTextCache) - mFont->renderTextCache(mTextCache.get()); + mEditText->render(trans); // Pop the clip early to allow the cursor to be drawn outside of the "text area". mRenderer->popClipRect(); // Draw cursor. - const float cursorHeight {mFont->getHeight() * 0.8f}; + const float textHeight {getFont()->getHeight()}; + const float cursorHeight {textHeight * 0.8f}; if (!mEditing) { - mRenderer->drawRect(mCursorPos.x, mCursorPos.y + (mFont->getHeight() - cursorHeight) / 2.0f, + mRenderer->drawRect(mCursorPos.x, mCursorPos.y + (textHeight - cursorHeight) / 2.0f, 2.0f * mRenderer->getScreenResolutionModifier(), cursorHeight, mMenuColorKeyboardCursorUnfocused, mMenuColorKeyboardCursorUnfocused); } if (mEditing && mBlinkTime < BLINKTIME / 2) { - mRenderer->drawRect(mCursorPos.x, mCursorPos.y + (mFont->getHeight() - cursorHeight) / 2.0f, + mRenderer->drawRect(mCursorPos.x, mCursorPos.y + (textHeight - cursorHeight) / 2.0f, 2.0f * mRenderer->getScreenResolutionModifier(), cursorHeight, mMenuColorKeyboardCursorFocused, mMenuColorKeyboardCursorFocused); } diff --git a/es-core/src/components/TextEditComponent.h b/es-core/src/components/TextEditComponent.h index 086a18d2d..6efb7d33a 100644 --- a/es-core/src/components/TextEditComponent.h +++ b/es-core/src/components/TextEditComponent.h @@ -3,7 +3,7 @@ // ES-DE Frontend // TextEditComponent.h // -// Component for editing text fields in menus. +// Component for editing text fields. // #ifndef ES_CORE_COMPONENTS_TEXT_EDIT_COMPONENT_H @@ -11,12 +11,8 @@ #include "GuiComponent.h" #include "components/NinePatchComponent.h" -#include "resources/Font.h" +#include "components/TextComponent.h" -class Font; -class TextCache; - -// Used to enter text. class TextEditComponent : public GuiComponent { public: @@ -40,7 +36,7 @@ public: void stopEditing(); bool isEditing() const { return mEditing; } - std::shared_ptr getFont() const override { return mFont; } + std::shared_ptr getFont() const override { return mEditText->getFont(); } void setCursor(size_t pos); void setMaskInput(bool state) { mMaskInput = state; } @@ -54,7 +50,7 @@ private: void updateCursorRepeat(int deltaTime); void moveCursor(int amt); - bool isMultiline() { return (getSize().y > mFont->getHeight() * 1.25f); } + bool isMultiline() { return (getSize().y > getFont()->getHeight() * 1.25f); } glm::vec2 getTextAreaPos() const; glm::vec2 getTextAreaSize() const; @@ -75,9 +71,7 @@ private: glm::vec2 mCursorPos; NinePatchComponent mBox; - - std::shared_ptr mFont; - std::unique_ptr mTextCache; + std::unique_ptr mEditText; }; #endif // ES_CORE_COMPONENTS_TEXT_EDIT_COMPONENT_H