From b4cacfc0fbb571c4f78adabc4963f2be366581a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Costa?= Date: Tue, 21 Nov 2023 23:06:52 +0100 Subject: [PATCH] Fix slider position --- .../eu/joaocosta/interim/api/Components.scala | 15 ++++-------- .../joaocosta/interim/skins/SliderSkin.scala | 24 +++++++------------ 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/core/shared/src/main/scala/eu/joaocosta/interim/api/Components.scala b/core/shared/src/main/scala/eu/joaocosta/interim/api/Components.scala index d162891..d766441 100644 --- a/core/shared/src/main/scala/eu/joaocosta/interim/api/Components.scala +++ b/core/shared/src/main/scala/eu/joaocosta/interim/api/Components.scala @@ -111,21 +111,16 @@ trait Components: new ComponentWithValue[Int]: def render(value: Ref[Int]): Component[Unit] = val sliderArea = skin.sliderArea(area) - val sliderSize = skin.sliderSize(area, min, max) - val range = max - min + val steps = max - min + 1 val itemStatus = UiContext.registerItem(id, sliderArea) val clampedValue = math.max(min, math.min(value.get, max)) skin.renderSlider(area, min, clampedValue, max, itemStatus) if (itemStatus.active) summon[InputState].mouseInput.position.foreach: (mouseX, mouseY) => - if (area.w > area.h) - val mousePos = mouseX - sliderArea.x - sliderSize / 2 - val maxPos = sliderArea.w - sliderSize - value := math.max(min, math.min(min + (mousePos * range) / maxPos, max)) - else - val mousePos = mouseY - sliderArea.y - sliderSize / 2 - val maxPos = sliderArea.h - sliderSize - value := math.max(min, math.min((mousePos * range) / maxPos, max)) + val intPosition = + if (area.w > area.h) steps * (mouseX - sliderArea.x) / sliderArea.w + else steps * (mouseY - sliderArea.y) / sliderArea.h + value := math.max(min, math.min(min + intPosition, max)) /** Text input component. Returns the current string inputed. */ diff --git a/core/shared/src/main/scala/eu/joaocosta/interim/skins/SliderSkin.scala b/core/shared/src/main/scala/eu/joaocosta/interim/skins/SliderSkin.scala index 03d4a54..33143f7 100644 --- a/core/shared/src/main/scala/eu/joaocosta/interim/skins/SliderSkin.scala +++ b/core/shared/src/main/scala/eu/joaocosta/interim/skins/SliderSkin.scala @@ -4,7 +4,6 @@ import eu.joaocosta.interim.* import eu.joaocosta.interim.api.Primitives.* trait SliderSkin: - def sliderSize(area: Rect, min: Int, max: Int): Int def sliderArea(area: Rect): Rect def renderSlider(area: Rect, min: Int, value: Int, max: Int, itemStatus: UiContext.ItemStatus)(using uiContext: UiContext @@ -21,30 +20,25 @@ object SliderSkin extends DefaultSkin: activeColor: Color ) extends SliderSkin: - def sliderSize(area: Rect, min: Int, max: Int): Int = - val steps = (max - min) + 1 - math.max(minSliderSize, math.max(area.w, area.h) / steps) - def sliderArea(area: Rect): Rect = area.shrink(padding) def renderSlider(area: Rect, min: Int, value: Int, max: Int, itemStatus: UiContext.ItemStatus)(using uiContext: UiContext ): Unit = val sliderArea = this.sliderArea(area) - val sliderSize = this.sliderSize(area, min, max) val delta = value - min - val maxDelta = max - min + val steps = (max - min) + 1 val sliderRect = if (area.w > area.h) - val sliderFill = sliderArea.h - val lastX = math.max(0, (sliderArea.w - sliderSize)) - val deltaX = if (lastX == 0) 0 else delta * lastX / maxDelta - Rect(area.x + padding + deltaX, area.y + padding, sliderSize, sliderFill) + val deltaX = delta * sliderArea.w / steps + Rect(0, 0, math.max(minSliderSize, sliderArea.w / steps), sliderArea.h) + .centerAt(0, sliderArea.centerY) + .copy(x = sliderArea.x + deltaX) else - val sliderFill = sliderArea.w - val lastY = math.max(0, (sliderArea.h - sliderSize)) - val deltaY = if (lastY == 0) 0 else delta * lastY / maxDelta - Rect(area.x + padding, area.y + padding + deltaY, sliderFill, sliderSize) + val deltaY = delta * sliderArea.h / steps + Rect(0, 0, sliderArea.w, math.max(minSliderSize, sliderArea.h / steps)) + .centerAt(sliderArea.centerX, 0) + .copy(y = sliderArea.y + deltaY) rectangle(area, scrollbarColor) // Scrollbar itemStatus match case UiContext.ItemStatus(false, false, _, _) =>