From 05584f9d08dec769477b1eb4866941fa57c0d560 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Sun, 23 Apr 2023 19:05:32 +0200 Subject: [PATCH] [orx-delegate-magic] Use Clock interface instead of Program interface --- .../kotlin/dynamics/PropertySpringForcer.kt | 22 +++++++++---------- .../kotlin/smoothing/PropertySmoother.kt | 22 +++++++++---------- .../kotlin/tracking/PropertyTracker.kt | 10 ++++----- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/orx-delegate-magic/src/commonMain/kotlin/dynamics/PropertySpringForcer.kt b/orx-delegate-magic/src/commonMain/kotlin/dynamics/PropertySpringForcer.kt index 18a0f4c3..44a7e579 100644 --- a/orx-delegate-magic/src/commonMain/kotlin/dynamics/PropertySpringForcer.kt +++ b/orx-delegate-magic/src/commonMain/kotlin/dynamics/PropertySpringForcer.kt @@ -2,13 +2,13 @@ package org.openrndr.extra.delegatemagic.dynamics -import org.openrndr.Program +import org.openrndr.Clock import org.openrndr.math.LinearType import kotlin.reflect.KProperty import kotlin.reflect.KProperty0 class DoublePropertySpringForcer( - private val program: Program, + private val clock: Clock, private val property: KProperty0, private val k: Double, private val kProperty: KProperty0?, @@ -25,7 +25,7 @@ class DoublePropertySpringForcer( val anchor = this.property.get() if (lastTime != null) { - val dt = program.seconds - lastTime!! + val dt = clock.seconds - lastTime!! if (dt > 0.0) { val sfY = -k * (output!! - anchor) velocity = velocity * decay + sfY * dt * 10.0 @@ -34,13 +34,13 @@ class DoublePropertySpringForcer( } else { output = this.property.get() } - lastTime = program.seconds + lastTime = clock.seconds return output ?: error("no value") } } class LinearTypePropertySpringForcer>( - private val program: Program, + private val clock: Clock, private val property: KProperty0, private val k: Double, private val kProperty: KProperty0?, @@ -56,7 +56,7 @@ class LinearTypePropertySpringForcer>( val anchor = this.property.get() if (lastTime != null) { - val dt = program.seconds - lastTime!! + val dt = clock.seconds - lastTime!! if (dt > 0.0) { val sfY = (output!! - anchor) * -k @@ -70,7 +70,7 @@ class LinearTypePropertySpringForcer>( } else { output = this.property.get() } - lastTime = program.seconds + lastTime = clock.seconds return output ?: error("no value") } } @@ -84,7 +84,7 @@ class LinearTypePropertySpringForcer>( * @param decayProperty velocity decay property, overrides [decay] * @since 0.4.3 */ -fun Program.springForcing( +fun Clock.springForcing( property: KProperty0, k: Double = 1.0, kProperty: KProperty0? = null, @@ -92,7 +92,7 @@ fun Program.springForcing( decayProperty: KProperty0? = null ): DoublePropertySpringForcer { return DoublePropertySpringForcer( - program = this, + clock = this, property = property, k = k, kProperty = kProperty, @@ -110,7 +110,7 @@ fun Program.springForcing( * @param decayProperty velocity decay property, overrides [decay] * @since 0.4.3 */ -fun > Program.springForcing( +fun > Clock.springForcing( property: KProperty0, k: Double = 1.0, kProperty: KProperty0? = null, @@ -118,7 +118,7 @@ fun > Program.springForcing( decayProperty: KProperty0? = null ): LinearTypePropertySpringForcer { return LinearTypePropertySpringForcer( - program = this, + clock = this, property = property, k = k, kProperty = kProperty, diff --git a/orx-delegate-magic/src/commonMain/kotlin/smoothing/PropertySmoother.kt b/orx-delegate-magic/src/commonMain/kotlin/smoothing/PropertySmoother.kt index f191bc6f..b5c67533 100644 --- a/orx-delegate-magic/src/commonMain/kotlin/smoothing/PropertySmoother.kt +++ b/orx-delegate-magic/src/commonMain/kotlin/smoothing/PropertySmoother.kt @@ -2,14 +2,14 @@ package org.openrndr.extra.delegatemagic.smoothing -import org.openrndr.Program +import org.openrndr.Clock import org.openrndr.math.LinearType import kotlin.math.pow import kotlin.reflect.KProperty import kotlin.reflect.KProperty0 class DoublePropertySmoother( - private val program: Program, + private val clock: Clock, private val property: KProperty0, private val factor: Double = 0.99, private val factorProperty: KProperty0? @@ -18,7 +18,7 @@ class DoublePropertySmoother( private var lastTime: Double? = null operator fun getValue(any: Any?, property: KProperty<*>): Double { if (lastTime != null) { - val dt = program.seconds - lastTime!! + val dt = clock.seconds - lastTime!! if (dt > 1E-10) { val steps = dt * 60.0 val ef = (factorProperty?.get() ?: factor).pow(steps) @@ -27,13 +27,13 @@ class DoublePropertySmoother( } else { output = this.property.get() } - lastTime = program.seconds + lastTime = clock.seconds return output ?: error("no value") } } class PropertySmoother>( - private val program: Program, + private val clock: Clock, private val property: KProperty0, private val factor: Double = 0.99, private val factorProperty: KProperty0? @@ -42,7 +42,7 @@ class PropertySmoother>( private var lastTime: Double? = null operator fun getValue(any: Any?, property: KProperty<*>): T { if (lastTime != null) { - val dt = program.seconds - lastTime!! + val dt = clock.seconds - lastTime!! if (dt > 1E-10) { val steps = dt * 60.0 val ef = (factorProperty?.get() ?: factor).pow(steps) @@ -53,7 +53,7 @@ class PropertySmoother>( } else { output = this.property.get() } - lastTime = program.seconds + lastTime = clock.seconds return output ?: error("no value") } } @@ -64,7 +64,7 @@ class PropertySmoother>( * @param factor the smoothing factor * @since 0.4.3 */ -fun Program.smoothing(property: KProperty0, factor: Double = 0.99): DoublePropertySmoother { +fun Clock.smoothing(property: KProperty0, factor: Double = 0.99): DoublePropertySmoother { return DoublePropertySmoother(this, property, factor, null) } @@ -74,7 +74,7 @@ fun Program.smoothing(property: KProperty0, factor: Double = 0.99): Doub * @param factor the smoothing factor property * @since 0.4.3 */ -fun Program.smoothing( +fun Clock.smoothing( property: KProperty0, factor: KProperty0 ): DoublePropertySmoother { @@ -87,7 +87,7 @@ fun Program.smoothing( * @param factor the smoothing factor * @since 0.4.3 */ -fun > Program.smoothing(property: KProperty0, factor: Double = 0.99): PropertySmoother { +fun > Clock.smoothing(property: KProperty0, factor: Double = 0.99): PropertySmoother { return PropertySmoother(this, property, factor, null) } @@ -97,6 +97,6 @@ fun > Program.smoothing(property: KProperty0, factor: Doubl * @param factor the smoothing factor property * @since 0.4.3 */ -fun > Program.smoothing(property: KProperty0, factor: KProperty0): PropertySmoother { +fun > Clock.smoothing(property: KProperty0, factor: KProperty0): PropertySmoother { return PropertySmoother(this, property, 1E10, factor) } \ No newline at end of file diff --git a/orx-delegate-magic/src/commonMain/kotlin/tracking/PropertyTracker.kt b/orx-delegate-magic/src/commonMain/kotlin/tracking/PropertyTracker.kt index e19ee610..0c012c11 100644 --- a/orx-delegate-magic/src/commonMain/kotlin/tracking/PropertyTracker.kt +++ b/orx-delegate-magic/src/commonMain/kotlin/tracking/PropertyTracker.kt @@ -2,17 +2,17 @@ package org.openrndr.extra.delegatemagic.tracking -import org.openrndr.Program +import org.openrndr.Clock import kotlin.reflect.KProperty import kotlin.reflect.KProperty0 -class PropertyTracker(private val program: Program, private val property: KProperty0, val length: Int = 30) { +class PropertyTracker(private val clock: Clock, private val property: KProperty0, val length: Int = 30) { private val track = mutableListOf() private var lastTime: Double? = null operator fun getValue(any: Any?, property: KProperty<*>): List { if (lastTime != null) { - val dt = program.seconds - lastTime!! + val dt = clock.seconds - lastTime!! if (dt > 1E-10) { track.add(this.property.get()) } @@ -22,7 +22,7 @@ class PropertyTracker(private val program: Program, private val property: KPr if (track.size > length) { track.removeAt(0) } - lastTime = program.seconds + lastTime = clock.seconds return track } } @@ -34,6 +34,6 @@ class PropertyTracker(private val program: Program, private val property: KPr * @return a property tracker * @since 0.4.3 */ -fun Program.tracking(property: KProperty0, length: Int = 30): PropertyTracker { +fun Clock.tracking(property: KProperty0, length: Int = 30): PropertyTracker { return PropertyTracker(this, property, length) } \ No newline at end of file