Fixed an issue where the SliderComponent knob was not always correctly positioned vertically.

This commit is contained in:
Leon Styhre 2023-01-24 18:32:28 +01:00
parent b17b7194a6
commit 6135021c26
2 changed files with 27 additions and 25 deletions

View file

@ -21,6 +21,7 @@ SliderComponent::SliderComponent(float min, float max, float increment, const st
, mSingleIncrement {increment} , mSingleIncrement {increment}
, mMoveRate {0.0f} , mMoveRate {0.0f}
, mBarHeight {0.0f} , mBarHeight {0.0f}
, mBarPosY {0.0f}
, mSuffix {suffix} , mSuffix {suffix}
{ {
assert((min - max) != 0.0f); assert((min - max) != 0.0f);
@ -88,20 +89,17 @@ void SliderComponent::render(const glm::mat4& parentTrans)
mSize.y, 0x00000033, 0x00000033); mSize.y, 0x00000033, 0x00000033);
} }
float width {mSize.x - mKnob.getSize().x - const float width {
(mTextCache ? mSize.x - mKnob.getSize().x -
mTextCache->metrics.size.x + (4.0f * mRenderer->getScreenWidthModifier()) : (mTextCache ? mTextCache->metrics.size.x + (4.0f * mRenderer->getScreenWidthModifier()) :
0.0f)}; 0.0f)};
// Render suffix.
if (mTextCache) if (mTextCache)
mFont->renderTextCache(mTextCache.get()); mFont->renderTextCache(mTextCache.get());
// Render bar. mRenderer->drawRect(mKnob.getSize().x / 2.0f, mBarPosY, width, mBarHeight, 0x777777FF,
mRenderer->drawRect(mKnob.getSize().x / 2.0f, mSize.y / 2.0f, width, mBarHeight, 0x777777FF,
0x777777FF); 0x777777FF);
// Render knob.
mKnob.render(trans); mKnob.render(trans);
GuiComponent::renderChildren(trans); GuiComponent::renderChildren(trans);
@ -151,42 +149,45 @@ void SliderComponent::onValueChanged()
mTextCache->metrics.size.x = textSize.x; // Fudge the width. mTextCache->metrics.size.x = textSize.x; // Fudge the width.
} }
mKnob.setResize(0.0f, mSize.y * 0.7f); mKnob.setResize(0.0f, std::round(mSize.y * 0.7f));
float barLength { float barLength {
mSize.x - mKnob.getSize().x - mSize.x - mKnob.getSize().x -
(mTextCache ? mTextCache->metrics.size.x + (4.0f * mRenderer->getScreenWidthModifier()) : (mTextCache ? mTextCache->metrics.size.x + (4.0f * mRenderer->getScreenWidthModifier()) :
0.0f)}; 0.0f)};
int barHeight {0}; if (mRenderer->getScreenWidth() > mRenderer->getScreenHeight())
if (mRenderer->getScreenWidth() > mRenderer->getScreenHeight()) { mBarHeight = std::round(2.0f * mRenderer->getScreenHeightModifier());
barHeight = static_cast<int>(std::round(2.0f * mRenderer->getScreenHeightModifier())); else
} mBarHeight = std::round(2.0f * std::round(mRenderer->getScreenWidthModifier()));
else {
barHeight =
static_cast<int>(std::round(2.0f * std::round(mRenderer->getScreenWidthModifier())));
}
// For very low resolutions, make sure the bar height is not rounded to zero. // For very low resolutions, make sure the bar height is not rounded to zero.
if (barHeight == 0) if (mBarHeight < 1.0f)
barHeight = 1; mBarHeight = 1.0f;
// Resize the knob one pixel if necessary to keep the bar centered. // Always make both mSize and the knob odd or even for correct positioning.
if (barHeight % 2 == 0 && static_cast<int>(std::round(mKnob.getSize().y)) % 2 != 0) { if (static_cast<int>(mSize.y) % 2 != static_cast<int>(mKnob.getSize().y) % 2) {
mKnob.setResize(mKnob.getSize().x - 1.0f, mKnob.getSize().y - 1.0f); mKnob.setResize(mKnob.getSize().x - 1.0f, mKnob.getSize().y - 1.0f);
setSize(getSize().x, getSize().y - 1.0f); setSize(getSize().x, getSize().y - 1.0f);
} }
else if (barHeight == 1 && static_cast<int>(std::round(mKnob.getSize().y)) % 2 == 0) {
mKnob.setResize(mKnob.getSize().x - 1.0f, mKnob.getSize().y - 1); // Likewise for the bar.
setSize(getSize().x, getSize().y - 1.0f); if (static_cast<int>(mSize.y) % 2 != static_cast<int>(mBarHeight) % 2) {
if (mBarHeight > 1.0f && mSize.y / mBarHeight < 5.0f)
--mBarHeight;
else
++mBarHeight;
} }
mBarHeight = static_cast<float>(barHeight);
const float val {(mValue - mMin) / (mMax - mMin)}; const float val {(mValue - mMin) / (mMax - mMin)};
// For smooth outer boundaries. // For smooth outer boundaries.
// const float val {glm::smoothstep(mMin, mMax, mValue)}; // const float val {glm::smoothstep(mMin, mMax, mValue)};
mKnob.setPosition(val * barLength + mKnob.getSize().x / 2.0f, mSize.y / 2.0f); mKnob.setOrigin(glm::vec2 {0.5f, 0.0f});
const float posY {(mSize.y - mKnob.getSize().y) / 2.0f};
mKnob.setPosition(val * barLength + mKnob.getSize().x / 2.0f, posY);
mBarPosY = (mSize.y - mBarHeight) / 2.0f;
} }
std::vector<HelpPrompt> SliderComponent::getHelpPrompts() std::vector<HelpPrompt> SliderComponent::getHelpPrompts()

View file

@ -46,6 +46,7 @@ private:
float mSingleIncrement; float mSingleIncrement;
float mMoveRate; float mMoveRate;
float mBarHeight; float mBarHeight;
float mBarPosY;
int mMoveAccumulator; int mMoveAccumulator;
ImageComponent mKnob; ImageComponent mKnob;