Changed ButtonComponent to use TextComponent instead of using Font facilities directly

This commit is contained in:
Leon Styhre 2024-08-11 14:00:40 +02:00
parent ccf680b433
commit 5b7becf446
2 changed files with 18 additions and 34 deletions

View file

@ -3,7 +3,7 @@
// ES-DE Frontend
// ButtonComponent.cpp
//
// Basic on/off button.
// Basic button, used as a GUI element and for the virtual keyboard buttons.
//
#include "components/ButtonComponent.h"
@ -19,7 +19,6 @@ ButtonComponent::ButtonComponent(const std::string& text,
bool flatStyle)
: mRenderer {Renderer::getInstance()}
, mBox {":/graphics/button.svg"}
, mFont {Font::get(FONT_SIZE_MEDIUM)}
, mPadding {0.0f, 0.0f, 0.0f, 0.0f}
, mFocused {false}
, mEnabled {true}
@ -30,6 +29,9 @@ ButtonComponent::ButtonComponent(const std::string& text,
, mFlatColorUnfocused {mMenuColorButtonFlatUnfocused}
{
mButtonText =
std::make_unique<TextComponent>("", Font::get(FONT_SIZE_MEDIUM), 0xFFFFFFFF, ALIGN_CENTER);
mBox.setSharpCorners(true);
setPressedFunc(func);
setText(text, helpText, upperCase);
@ -71,17 +73,15 @@ void ButtonComponent::setText(const std::string& text,
{
mText = upperCase ? Utils::String::toUpper(text) : text;
mHelpText = helpText;
mButtonText->setText(mText);
mTextCache =
std::unique_ptr<TextCache>(mFont->buildTextCache(mText, 0.0f, 0.0f, getCurTextColor()));
const float minWidth {mFont->sizeText("DELETE").x +
const float minWidth {mButtonText->getFont()->sizeText("DELETE").x +
(12.0f * mRenderer->getScreenResolutionModifier())};
if (resize) {
setSize(std::max(mTextCache->metrics.size.x +
(12.0f * mRenderer->getScreenResolutionModifier()),
minWidth),
mTextCache->metrics.size.y);
setSize(
std::max(mButtonText->getSize().x + (12.0f * mRenderer->getScreenResolutionModifier()),
minWidth),
mButtonText->getSize().y);
}
updateHelpPrompts();
@ -136,26 +136,12 @@ void ButtonComponent::render(const glm::mat4& parentTrans)
mBox.render(trans);
}
if (mTextCache) {
glm::vec3 centerOffset {(mSize.x - mTextCache->metrics.size.x) / 2.0f,
(mSize.y - mTextCache->metrics.size.y) / 2.0f, 0.0f};
trans = glm::translate(trans, glm::round(centerOffset));
const glm::vec3 centerOffset {(mSize.x - mButtonText->getSize().x) / 2.0f,
(mSize.y - mButtonText->getSize().y) / 2.0f, 0.0f};
trans = glm::translate(trans, glm::round(centerOffset));
if (Settings::getInstance()->getBool("DebugText")) {
mRenderer->drawRect(centerOffset.x, 0.0f, mTextCache->metrics.size.x, mSize.y,
0x00000033, 0x00000033);
mRenderer->drawRect(mBox.getPosition().x, 0.0f, mBox.getSize().x, mSize.y, 0x0000FF33,
0x0000FF33);
}
mRenderer->setMatrix(trans);
mTextCache->setColor(getCurTextColor());
mFont->renderTextCache(mTextCache.get());
trans = glm::translate(trans, glm::round(-centerOffset));
}
renderChildren(trans);
mButtonText->setColor(getCurTextColor());
mButtonText->render(trans);
}
std::vector<HelpPrompt> ButtonComponent::getHelpPrompts()

View file

@ -3,7 +3,7 @@
// ES-DE Frontend
// ButtonComponent.h
//
// Basic on/off button.
// Basic button, used as a GUI element and for the virtual keyboard buttons.
//
#ifndef ES_CORE_COMPONENTS_BUTTON_COMPONENT_H
@ -11,8 +11,7 @@
#include "GuiComponent.h"
#include "components/NinePatchComponent.h"
class TextCache;
#include "components/TextComponent.h"
class ButtonComponent : public GuiComponent
{
@ -56,8 +55,7 @@ private:
Renderer* mRenderer;
NinePatchComponent mBox;
std::shared_ptr<Font> mFont;
std::unique_ptr<TextCache> mTextCache;
std::unique_ptr<TextComponent> mButtonText;
std::function<void()> mPressedFunc;
glm::vec4 mPadding;