mirror of
https://github.com/RetroDECK/ES-DE.git
synced 2025-02-25 07:45:40 +00:00
Changed SliderComponent to use TextComponent instead of using Font facilities directly
This commit is contained in:
parent
5b7becf446
commit
8ee7b6f118
es-core/src/components
|
@ -9,7 +9,6 @@
|
||||||
#include "components/SliderComponent.h"
|
#include "components/SliderComponent.h"
|
||||||
|
|
||||||
#include "Window.h"
|
#include "Window.h"
|
||||||
#include "resources/Font.h"
|
|
||||||
#include "utils/LocalizationUtil.h"
|
#include "utils/LocalizationUtil.h"
|
||||||
|
|
||||||
#define MOVE_REPEAT_DELAY 500
|
#define MOVE_REPEAT_DELAY 500
|
||||||
|
@ -19,18 +18,22 @@ SliderComponent::SliderComponent(float min, float max, float increment, const st
|
||||||
: mRenderer {Renderer::getInstance()}
|
: mRenderer {Renderer::getInstance()}
|
||||||
, mMin {min}
|
, mMin {min}
|
||||||
, mMax {max}
|
, mMax {max}
|
||||||
|
, mValue {0.0f}
|
||||||
, mSingleIncrement {increment}
|
, mSingleIncrement {increment}
|
||||||
, mMoveRate {0.0f}
|
, mMoveRate {0.0f}
|
||||||
|
, mBarLength {0.0f}
|
||||||
, mBarHeight {0.0f}
|
, mBarHeight {0.0f}
|
||||||
, mBarPosY {0.0f}
|
, mBarPosY {0.0f}
|
||||||
, mSuffix {suffix}
|
, mSuffix {suffix}
|
||||||
{
|
{
|
||||||
assert((min - max) != 0.0f);
|
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,
|
setSize(mWindow->peekGui()->getSize().x * 0.26f,
|
||||||
Font::get(FONT_SIZE_MEDIUM)->getLetterHeight());
|
Font::get(FONT_SIZE_MEDIUM)->getLetterHeight());
|
||||||
|
|
||||||
// Some sane default value.
|
// Some reasonable default value.
|
||||||
mValue = (max + min) / 2.0f;
|
mValue = (max + min) / 2.0f;
|
||||||
|
|
||||||
mKnob.setResize(0.0f, std::round(mSize.y * 0.7f));
|
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)
|
void SliderComponent::render(const glm::mat4& parentTrans)
|
||||||
{
|
{
|
||||||
glm::mat4 trans {parentTrans * getTransform()};
|
glm::mat4 trans {parentTrans * getTransform()};
|
||||||
|
mSliderText->render(trans);
|
||||||
mRenderer->setMatrix(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(
|
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),
|
||||||
(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()
|
void SliderComponent::onSizeChanged()
|
||||||
{
|
{
|
||||||
mFont = Font::get(mSize.y, FONT_PATH_LIGHT);
|
mSliderText->setFont(Font::get(mSize.y, FONT_PATH_LIGHT));
|
||||||
onValueChanged();
|
onValueChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SliderComponent::onValueChanged()
|
void SliderComponent::onValueChanged()
|
||||||
{
|
{
|
||||||
// Update suffix textcache.
|
glm::vec2 textSize {0.0f, 0.0f};
|
||||||
if (mFont) {
|
|
||||||
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << std::fixed;
|
ss << std::fixed;
|
||||||
ss.precision(0);
|
ss.precision(0);
|
||||||
|
@ -156,12 +145,10 @@ void SliderComponent::onValueChanged()
|
||||||
ss.precision(0);
|
ss.precision(0);
|
||||||
ss << mMax;
|
ss << mMax;
|
||||||
ss << mSuffix;
|
ss << mSuffix;
|
||||||
const std::string max {ss.str()};
|
|
||||||
|
|
||||||
glm::vec2 textSize {mFont->sizeText(max)};
|
mSliderText->setText(val);
|
||||||
mTextCache = std::shared_ptr<TextCache>(mFont->buildTextCache(
|
textSize = mSliderText->getFont()->sizeText(ss.str());
|
||||||
val, mSize.x - textSize.x, (mSize.y - textSize.y) / 2.0f, mMenuColorPrimary));
|
mSliderText->setPosition(mSize.x - textSize.x, (mSize.y - textSize.y) / 2.0f);
|
||||||
mTextCache->metrics.size.x = textSize.x; // Fudge the width.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mKnob.setResize(0.0f, std::round(mSize.y * 0.7f));
|
mKnob.setResize(0.0f, std::round(mSize.y * 0.7f));
|
||||||
|
@ -181,12 +168,9 @@ void SliderComponent::onValueChanged()
|
||||||
setSize(getSize().x, getSize().y - 1.0f);
|
setSize(getSize().x, getSize().y - 1.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
float barLength {
|
mBarLength =
|
||||||
mSize.x - mKnob.getSize().x -
|
mSize.x - mKnob.getSize().x - (textSize.x + (4.0f * mRenderer->getScreenWidthModifier()));
|
||||||
(mTextCache ? mTextCache->metrics.size.x + (4.0f * mRenderer->getScreenWidthModifier()) :
|
|
||||||
0.0f)};
|
|
||||||
|
|
||||||
// Likewise for the bar.
|
|
||||||
if (static_cast<int>(mSize.y) % 2 != static_cast<int>(mBarHeight) % 2) {
|
if (static_cast<int>(mSize.y) % 2 != static_cast<int>(mBarHeight) % 2) {
|
||||||
if (mBarHeight > 1.0f && mSize.y / mBarHeight < 5.0f)
|
if (mBarHeight > 1.0f && mSize.y / mBarHeight < 5.0f)
|
||||||
--mBarHeight;
|
--mBarHeight;
|
||||||
|
@ -194,12 +178,12 @@ void SliderComponent::onValueChanged()
|
||||||
++mBarHeight;
|
++mBarHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
const float val {(mValue - mMin) / (mMax - mMin)};
|
const float posX {(mValue - mMin) / (mMax - mMin)};
|
||||||
// For smooth outer boundaries.
|
// 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};
|
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.setResize(mKnob.getSize());
|
||||||
mKnobDisabled.setPosition(mKnob.getPosition());
|
mKnobDisabled.setPosition(mKnob.getPosition());
|
||||||
|
|
|
@ -11,9 +11,8 @@
|
||||||
|
|
||||||
#include "GuiComponent.h"
|
#include "GuiComponent.h"
|
||||||
#include "components/ImageComponent.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
|
class SliderComponent : public GuiComponent
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -40,7 +39,7 @@ public:
|
||||||
void setOpacity(float opacity) override
|
void setOpacity(float opacity) override
|
||||||
{
|
{
|
||||||
mOpacity = opacity;
|
mOpacity = opacity;
|
||||||
mTextCache->setOpacity(opacity);
|
mSliderText->setOpacity(opacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<HelpPrompt> getHelpPrompts() override;
|
std::vector<HelpPrompt> getHelpPrompts() override;
|
||||||
|
@ -53,6 +52,7 @@ private:
|
||||||
float mValue;
|
float mValue;
|
||||||
float mSingleIncrement;
|
float mSingleIncrement;
|
||||||
float mMoveRate;
|
float mMoveRate;
|
||||||
|
float mBarLength;
|
||||||
float mBarHeight;
|
float mBarHeight;
|
||||||
float mBarPosY;
|
float mBarPosY;
|
||||||
int mMoveAccumulator;
|
int mMoveAccumulator;
|
||||||
|
@ -61,8 +61,7 @@ private:
|
||||||
ImageComponent mKnobDisabled;
|
ImageComponent mKnobDisabled;
|
||||||
|
|
||||||
std::string mSuffix;
|
std::string mSuffix;
|
||||||
std::shared_ptr<Font> mFont;
|
std::unique_ptr<TextComponent> mSliderText;
|
||||||
std::shared_ptr<TextCache> mTextCache;
|
|
||||||
std::function<void()> mChangedValueCallback;
|
std::function<void()> mChangedValueCallback;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue