diff --git a/es-core/src/components/SliderComponent.cpp b/es-core/src/components/SliderComponent.cpp index 4402d9277..23c5ae7d8 100644 --- a/es-core/src/components/SliderComponent.cpp +++ b/es-core/src/components/SliderComponent.cpp @@ -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(std::round(2.0f * mRenderer->getScreenHeightModifier())); - } - else { - barHeight = - static_cast(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(std::round(mKnob.getSize().y)) % 2 != 0) { + // Always make both mSize and the knob odd or even for correct positioning. + if (static_cast(mSize.y) % 2 != static_cast(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(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(mSize.y) % 2 != static_cast(mBarHeight) % 2) { + if (mBarHeight > 1.0f && mSize.y / mBarHeight < 5.0f) + --mBarHeight; + else + ++mBarHeight; } - mBarHeight = static_cast(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 SliderComponent::getHelpPrompts() diff --git a/es-core/src/components/SliderComponent.h b/es-core/src/components/SliderComponent.h index a0204b2d1..533c511f0 100644 --- a/es-core/src/components/SliderComponent.h +++ b/es-core/src/components/SliderComponent.h @@ -46,6 +46,7 @@ private: float mSingleIncrement; float mMoveRate; float mBarHeight; + float mBarPosY; int mMoveAccumulator; ImageComponent mKnob;