From edb92005db4fe398a55e43864860f0b66c1dce4d Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Fri, 22 May 2020 20:13:42 +0200 Subject: [PATCH] Add two display modes to orx-panel Slider --- .../org/openrndr/panel/elements/Slider.kt | 65 +++++++++++++++++-- 1 file changed, 60 insertions(+), 5 deletions(-) diff --git a/orx-panel/src/main/kotlin/org/openrndr/panel/elements/Slider.kt b/orx-panel/src/main/kotlin/org/openrndr/panel/elements/Slider.kt index 12265f98..6cb6956b 100644 --- a/orx-panel/src/main/kotlin/org/openrndr/panel/elements/Slider.kt +++ b/orx-panel/src/main/kotlin/org/openrndr/panel/elements/Slider.kt @@ -23,13 +23,20 @@ data class Range(val min: Double, val max: Double) { val span: Double get() = max - min } +enum class SliderMode { + RANGE, + POINT, + SEGMENT +} + class Slider : Element(ElementType("slider")), DisposableElement { override var disposed = false - override val handlesKeyboardFocus = true var label = "" var precision = 3 + var mode = SliderMode.RANGE + var value: Double set(v) { val oldV = realValue @@ -204,11 +211,59 @@ class Slider : Element(ElementType("slider")), DisposableElement { drawer.stroke = ((computedStyle.color as Color.RGBa).color.opacify(0.25)) drawer.lineSegment(margin + 0.0, 2.0, margin + layout.screenWidth - 2 * margin, 2.0) - drawer.stroke = ((computedStyle.color as Color.RGBa).color.opacify(1.0)) - drawer.lineSegment(margin, 2.0, margin + x, 2.0) + if (mode == SliderMode.RANGE) { + drawer.stroke = ((computedStyle.color as Color.RGBa).color.opacify(1.0)) + drawer.lineSegment(margin, 2.0, margin + x, 2.0) + + drawer.fill = ((computedStyle.color as Color.RGBa).color.opacify(1.0)) + drawer.stroke = null + drawer.strokeWeight = 0.0 + drawer.circle(margin + x, 2.0, 5.0) + } + + if (mode == SliderMode.POINT && precision == 0) { + val lineSegments = mutableListOf() + for (i in range.min.toInt()..range.max.toInt()) { + val lx = ((i - range.min) / range.span) * (layout.screenWidth - 2 * margin) + drawer.strokeWeight = 1.0 + drawer.stroke = ((computedStyle.color as Color.RGBa).color.opacify(0.5)) + lineSegments.add(Vector2(margin + lx, -2.0)) + lineSegments.add(Vector2(margin + lx, 4.0)) + } + drawer.lineSegments(lineSegments) + } + + if (mode == SliderMode.SEGMENT) { + drawer.stroke = ((computedStyle.color as Color.RGBa).color.opacify(1.0)) + + val sx = ((value - range.min) / (range.span+1.0)) * (layout.screenWidth - 2 * margin) + margin + val ex = (((value+1) - range.min) / (range.span+1.0)) * (layout.screenWidth - 2 * margin) + margin + + drawer.strokeWeight = 8.0 + drawer.lineSegment(sx, 2.0, ex, 2.0) + + drawer.stroke = null + drawer.strokeWeight = 0.0 + + + val lineSegments = mutableListOf() + for (i in range.min.toInt()..(range.max.toInt()+1)) { + val lx = ((i - range.min) / (range.span+1.0)) * (layout.screenWidth - 2 * margin) + drawer.strokeWeight = 1.0 + drawer.stroke = ((computedStyle.color as Color.RGBa).color.opacify(0.5)) + lineSegments.add(Vector2(margin + lx, -2.0)) + lineSegments.add(Vector2(margin + lx, 4.0)) + } + drawer.lineSegments(lineSegments) + } + + + if (mode == SliderMode.POINT) { + drawer.fill = ((computedStyle.color as Color.RGBa).color.opacify(1.0)) + drawer.stroke = null + drawer.circle(margin + x, 2.0, 8.0) + } - drawer.stroke = null - drawer.circle(Vector2(margin + x, 2.0), 5.0) (root() as? Body)?.controlManager?.fontManager?.let { val font = it.font(computedStyle)