Skip to content

Commit

Permalink
Merge pull request #86 from JD557/fix-slider-position
Browse files Browse the repository at this point in the history
Fix slider position
  • Loading branch information
JD557 authored Nov 21, 2023
2 parents 490efa5 + 3124ec7 commit 5f81916
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, _, _) =>
Expand Down

0 comments on commit 5f81916

Please sign in to comment.