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

View file

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