Changed SliderComponent to use TextComponent instead of using Font facilities directly

This commit is contained in:
Leon Styhre 2024-08-11 15:03:04 +02:00
parent 5b7becf446
commit 8ee7b6f118
2 changed files with 23 additions and 40 deletions

View file

@ -9,7 +9,6 @@
#include "components/SliderComponent.h"
#include "Window.h"
#include "resources/Font.h"
#include "utils/LocalizationUtil.h"
#define MOVE_REPEAT_DELAY 500
@ -19,18 +18,22 @@ SliderComponent::SliderComponent(float min, float max, float increment, const st
: mRenderer {Renderer::getInstance()}
, mMin {min}
, mMax {max}
, mValue {0.0f}
, mSingleIncrement {increment}
, mMoveRate {0.0f}
, mBarLength {0.0f}
, mBarHeight {0.0f}
, mBarPosY {0.0f}
, mSuffix {suffix}
{
assert((min - max) != 0.0f);
mSliderText = std::make_unique<TextComponent>("", Font::get(FONT_SIZE_SMALL, FONT_PATH_LIGHT),
mMenuColorPrimary);
setSize(mWindow->peekGui()->getSize().x * 0.26f,
Font::get(FONT_SIZE_MEDIUM)->getLetterHeight());
// Some sane default value.
// Some reasonable default value.
mValue = (max + min) / 2.0f;
mKnob.setResize(0.0f, std::round(mSize.y * 0.7f));
@ -91,26 +94,11 @@ void SliderComponent::update(int deltaTime)
void SliderComponent::render(const glm::mat4& parentTrans)
{
glm::mat4 trans {parentTrans * getTransform()};
mSliderText->render(trans);
mRenderer->setMatrix(trans);
if (Settings::getInstance()->getBool("DebugText")) {
mRenderer->drawRect(
mSize.x - mTextCache->metrics.size.x, (mSize.y - mTextCache->metrics.size.y) / 2.0f,
mTextCache->metrics.size.x, mTextCache->metrics.size.y, 0x0000FF33, 0x0000FF33);
mRenderer->drawRect(mSize.x - mTextCache->metrics.size.x, 0.0f, mTextCache->metrics.size.x,
mSize.y, 0x00000033, 0x00000033);
}
const float width {
mSize.x - mKnob.getSize().x -
(mTextCache ? mTextCache->metrics.size.x + (4.0f * mRenderer->getScreenWidthModifier()) :
0.0f)};
if (mTextCache)
mFont->renderTextCache(mTextCache.get());
mRenderer->drawRect(
mKnob.getSize().x / 2.0f, mBarPosY, width, mBarHeight,
mKnob.getSize().x / 2.0f, mBarPosY, mBarLength, mBarHeight,
(mMenuColorPrimary & 0xFFFFFF00) | static_cast<unsigned int>(mOpacity * 255.0f),
(mMenuColorPrimary & 0xFFFFFF00) | static_cast<unsigned int>(mOpacity * 255.0f));
@ -135,14 +123,15 @@ void SliderComponent::setValue(float value)
void SliderComponent::onSizeChanged()
{
mFont = Font::get(mSize.y, FONT_PATH_LIGHT);
mSliderText->setFont(Font::get(mSize.y, FONT_PATH_LIGHT));
onValueChanged();
}
void SliderComponent::onValueChanged()
{
// Update suffix textcache.
if (mFont) {
glm::vec2 textSize {0.0f, 0.0f};
{
std::stringstream ss;
ss << std::fixed;
ss.precision(0);
@ -156,12 +145,10 @@ void SliderComponent::onValueChanged()
ss.precision(0);
ss << mMax;
ss << mSuffix;
const std::string max {ss.str()};
glm::vec2 textSize {mFont->sizeText(max)};
mTextCache = std::shared_ptr<TextCache>(mFont->buildTextCache(
val, mSize.x - textSize.x, (mSize.y - textSize.y) / 2.0f, mMenuColorPrimary));
mTextCache->metrics.size.x = textSize.x; // Fudge the width.
mSliderText->setText(val);
textSize = mSliderText->getFont()->sizeText(ss.str());
mSliderText->setPosition(mSize.x - textSize.x, (mSize.y - textSize.y) / 2.0f);
}
mKnob.setResize(0.0f, std::round(mSize.y * 0.7f));
@ -181,12 +168,9 @@ void SliderComponent::onValueChanged()
setSize(getSize().x, getSize().y - 1.0f);
}
float barLength {
mSize.x - mKnob.getSize().x -
(mTextCache ? mTextCache->metrics.size.x + (4.0f * mRenderer->getScreenWidthModifier()) :
0.0f)};
mBarLength =
mSize.x - mKnob.getSize().x - (textSize.x + (4.0f * mRenderer->getScreenWidthModifier()));
// Likewise for the bar.
if (static_cast<int>(mSize.y) % 2 != static_cast<int>(mBarHeight) % 2) {
if (mBarHeight > 1.0f && mSize.y / mBarHeight < 5.0f)
--mBarHeight;
@ -194,12 +178,12 @@ void SliderComponent::onValueChanged()
++mBarHeight;
}
const float val {(mValue - mMin) / (mMax - mMin)};
const float posX {(mValue - mMin) / (mMax - mMin)};
// For smooth outer boundaries.
// const float val {glm::smoothstep(mMin, mMax, mValue)};
// const float posX {glm::smoothstep(mMin, mMax, mValue)};
const float posY {(mSize.y - mKnob.getSize().y) / 2.0f};
mKnob.setPosition(val * barLength + mKnob.getSize().x / 2.0f, posY);
mKnob.setPosition(posX * mBarLength + mKnob.getSize().x / 2.0f, posY);
mKnobDisabled.setResize(mKnob.getSize());
mKnobDisabled.setPosition(mKnob.getPosition());

View file

@ -11,9 +11,8 @@
#include "GuiComponent.h"
#include "components/ImageComponent.h"
#include "resources/Font.h"
#include "components/TextComponent.h"
// Slider to set value in a predefined range.
class SliderComponent : public GuiComponent
{
public:
@ -40,7 +39,7 @@ public:
void setOpacity(float opacity) override
{
mOpacity = opacity;
mTextCache->setOpacity(opacity);
mSliderText->setOpacity(opacity);
}
std::vector<HelpPrompt> getHelpPrompts() override;
@ -53,6 +52,7 @@ private:
float mValue;
float mSingleIncrement;
float mMoveRate;
float mBarLength;
float mBarHeight;
float mBarPosY;
int mMoveAccumulator;
@ -61,8 +61,7 @@ private:
ImageComponent mKnobDisabled;
std::string mSuffix;
std::shared_ptr<Font> mFont;
std::shared_ptr<TextCache> mTextCache;
std::unique_ptr<TextComponent> mSliderText;
std::function<void()> mChangedValueCallback;
};