Changed TextEditComponent to use TextComponent instead of using Font facilities directly

This commit is contained in:
Leon Styhre 2024-08-11 19:01:47 +02:00
parent 75fdd94d45
commit aba1163c44
2 changed files with 24 additions and 28 deletions

View file

@ -3,7 +3,7 @@
// ES-DE Frontend // ES-DE Frontend
// TextEditComponent.cpp // TextEditComponent.cpp
// //
// Component for editing text fields in menus. // Component for editing text fields.
// //
#include "components/TextEditComponent.h" #include "components/TextEditComponent.h"
@ -34,12 +34,14 @@ TextEditComponent::TextEditComponent()
, mScrollOffset {0.0f, 0.0f} , mScrollOffset {0.0f, 0.0f}
, mCursorPos {0.0f, 0.0f} , mCursorPos {0.0f, 0.0f}
, mBox {":/graphics/textinput.svg"} , mBox {":/graphics/textinput.svg"}
, mFont {Font::get(FONT_SIZE_MEDIUM, FONT_PATH_LIGHT)}
{ {
mEditText = std::make_unique<TextComponent>("", Font::get(FONT_SIZE_MEDIUM, FONT_PATH_LIGHT));
mBox.setSharpCorners(true); mBox.setSharpCorners(true);
addChild(&mBox); addChild(&mBox);
onFocusLost(); onFocusLost();
setSize(4096, mFont->getHeight() + (TEXT_PADDING_VERT * mRenderer->getScreenHeightModifier())); setSize(4096,
getFont()->getHeight() + (TEXT_PADDING_VERT * mRenderer->getScreenHeightModifier()));
} }
TextEditComponent::~TextEditComponent() TextEditComponent::~TextEditComponent()
@ -310,10 +312,11 @@ void TextEditComponent::setCursor(size_t pos)
void TextEditComponent::onTextChanged() void TextEditComponent::onTextChanged()
{ {
mWrappedText = mWrappedText =
(isMultiline() ? mFont->wrapText(mText, getTextAreaSize().x, 0.0f, 1.5f, true) : mText); (isMultiline() ? getFont()->wrapText(mText, getTextAreaSize().x, 0.0f, 1.5f, true) : mText);
mTextCache = std::unique_ptr<TextCache>(mFont->buildTextCache( mEditText->setText(mWrappedText);
mWrappedText, 0.0f, 0.0f, // Setting the Y size to zero makes the text area expand vertically as needed.
mMenuColorKeyboardText | static_cast<unsigned char>(mOpacity * 255.0f))); mEditText->setSize(mEditText->getSize().x, 0.0f);
mEditText->setColor(mMenuColorKeyboardText | static_cast<unsigned char>(mOpacity * 255.0f));
if (mCursor > static_cast<int>(mText.length())) if (mCursor > static_cast<int>(mText.length()))
mCursor = static_cast<int>(mText.length()); mCursor = static_cast<int>(mText.length());
@ -322,17 +325,17 @@ void TextEditComponent::onTextChanged()
void TextEditComponent::onCursorChanged() void TextEditComponent::onCursorChanged()
{ {
if (isMultiline()) { if (isMultiline()) {
mCursorPos = mFont->getWrappedTextCursorOffset(mWrappedText, mCursor); mCursorPos = getFont()->getWrappedTextCursorOffset(mWrappedText, mCursor);
// Need to scroll down? // Need to scroll down?
if (mScrollOffset.y + getTextAreaSize().y < mCursorPos.y + mFont->getHeight()) if (mScrollOffset.y + getTextAreaSize().y < mCursorPos.y + getFont()->getHeight())
mScrollOffset.y = mCursorPos.y - getTextAreaSize().y + mFont->getHeight(); mScrollOffset.y = mCursorPos.y - getTextAreaSize().y + getFont()->getHeight();
// Need to scroll up? // Need to scroll up?
else if (mScrollOffset.y > mCursorPos.y) else if (mScrollOffset.y > mCursorPos.y)
mScrollOffset.y = mCursorPos.y; mScrollOffset.y = mCursorPos.y;
} }
else { else {
mCursorPos = mFont->sizeText(mText.substr(0, mCursor)); mCursorPos = getFont()->sizeText(mText.substr(0, mCursor));
mCursorPos.y = 0.0f; mCursorPos.y = 0.0f;
if (mScrollOffset.x + getTextAreaSize().x < mCursorPos.x) 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})); trans = glm::translate(trans, glm::round(glm::vec3 {-mScrollOffset.x, -mScrollOffset.y, 0.0f}));
mRenderer->setMatrix(trans); mRenderer->setMatrix(trans);
mEditText->render(trans);
if (mTextCache)
mFont->renderTextCache(mTextCache.get());
// Pop the clip early to allow the cursor to be drawn outside of the "text area". // Pop the clip early to allow the cursor to be drawn outside of the "text area".
mRenderer->popClipRect(); mRenderer->popClipRect();
// Draw cursor. // Draw cursor.
const float cursorHeight {mFont->getHeight() * 0.8f}; const float textHeight {getFont()->getHeight()};
const float cursorHeight {textHeight * 0.8f};
if (!mEditing) { 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, 2.0f * mRenderer->getScreenResolutionModifier(), cursorHeight,
mMenuColorKeyboardCursorUnfocused, mMenuColorKeyboardCursorUnfocused); mMenuColorKeyboardCursorUnfocused, mMenuColorKeyboardCursorUnfocused);
} }
if (mEditing && mBlinkTime < BLINKTIME / 2) { 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, 2.0f * mRenderer->getScreenResolutionModifier(), cursorHeight,
mMenuColorKeyboardCursorFocused, mMenuColorKeyboardCursorFocused); mMenuColorKeyboardCursorFocused, mMenuColorKeyboardCursorFocused);
} }

View file

@ -3,7 +3,7 @@
// ES-DE Frontend // ES-DE Frontend
// TextEditComponent.h // TextEditComponent.h
// //
// Component for editing text fields in menus. // Component for editing text fields.
// //
#ifndef ES_CORE_COMPONENTS_TEXT_EDIT_COMPONENT_H #ifndef ES_CORE_COMPONENTS_TEXT_EDIT_COMPONENT_H
@ -11,12 +11,8 @@
#include "GuiComponent.h" #include "GuiComponent.h"
#include "components/NinePatchComponent.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 class TextEditComponent : public GuiComponent
{ {
public: public:
@ -40,7 +36,7 @@ public:
void stopEditing(); void stopEditing();
bool isEditing() const { return mEditing; } bool isEditing() const { return mEditing; }
std::shared_ptr<Font> getFont() const override { return mFont; } std::shared_ptr<Font> getFont() const override { return mEditText->getFont(); }
void setCursor(size_t pos); void setCursor(size_t pos);
void setMaskInput(bool state) { mMaskInput = state; } void setMaskInput(bool state) { mMaskInput = state; }
@ -54,7 +50,7 @@ private:
void updateCursorRepeat(int deltaTime); void updateCursorRepeat(int deltaTime);
void moveCursor(int amt); 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 getTextAreaPos() const;
glm::vec2 getTextAreaSize() const; glm::vec2 getTextAreaSize() const;
@ -75,9 +71,7 @@ private:
glm::vec2 mCursorPos; glm::vec2 mCursorPos;
NinePatchComponent mBox; NinePatchComponent mBox;
std::unique_ptr<TextComponent> mEditText;
std::shared_ptr<Font> mFont;
std::unique_ptr<TextCache> mTextCache;
}; };
#endif // ES_CORE_COMPONENTS_TEXT_EDIT_COMPONENT_H #endif // ES_CORE_COMPONENTS_TEXT_EDIT_COMPONENT_H