[orx-delegate-magic] Use Clock interface instead of Program interface

This commit is contained in:
Edwin Jakobs
2023-04-23 19:05:32 +02:00
parent f21a910a0f
commit 05584f9d08
3 changed files with 27 additions and 27 deletions

View File

@@ -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<Double>,
private val k: Double,
private val kProperty: KProperty0<Double>?,
@@ -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<T : LinearType<T>>(
private val program: Program,
private val clock: Clock,
private val property: KProperty0<T>,
private val k: Double,
private val kProperty: KProperty0<Double>?,
@@ -56,7 +56,7 @@ class LinearTypePropertySpringForcer<T : LinearType<T>>(
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<T : LinearType<T>>(
} else {
output = this.property.get()
}
lastTime = program.seconds
lastTime = clock.seconds
return output ?: error("no value")
}
}
@@ -84,7 +84,7 @@ class LinearTypePropertySpringForcer<T : LinearType<T>>(
* @param decayProperty velocity decay property, overrides [decay]
* @since 0.4.3
*/
fun Program.springForcing(
fun Clock.springForcing(
property: KProperty0<Double>,
k: Double = 1.0,
kProperty: KProperty0<Double>? = null,
@@ -92,7 +92,7 @@ fun Program.springForcing(
decayProperty: KProperty0<Double>? = 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 <T : LinearType<T>> Program.springForcing(
fun <T : LinearType<T>> Clock.springForcing(
property: KProperty0<T>,
k: Double = 1.0,
kProperty: KProperty0<Double>? = null,
@@ -118,7 +118,7 @@ fun <T : LinearType<T>> Program.springForcing(
decayProperty: KProperty0<Double>? = null
): LinearTypePropertySpringForcer<T> {
return LinearTypePropertySpringForcer(
program = this,
clock = this,
property = property,
k = k,
kProperty = kProperty,

View File

@@ -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<Double>,
private val factor: Double = 0.99,
private val factorProperty: KProperty0<Double>?
@@ -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<T : LinearType<T>>(
private val program: Program,
private val clock: Clock,
private val property: KProperty0<T>,
private val factor: Double = 0.99,
private val factorProperty: KProperty0<Double>?
@@ -42,7 +42,7 @@ class PropertySmoother<T : LinearType<T>>(
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<T : LinearType<T>>(
} else {
output = this.property.get()
}
lastTime = program.seconds
lastTime = clock.seconds
return output ?: error("no value")
}
}
@@ -64,7 +64,7 @@ class PropertySmoother<T : LinearType<T>>(
* @param factor the smoothing factor
* @since 0.4.3
*/
fun Program.smoothing(property: KProperty0<Double>, factor: Double = 0.99): DoublePropertySmoother {
fun Clock.smoothing(property: KProperty0<Double>, factor: Double = 0.99): DoublePropertySmoother {
return DoublePropertySmoother(this, property, factor, null)
}
@@ -74,7 +74,7 @@ fun Program.smoothing(property: KProperty0<Double>, factor: Double = 0.99): Doub
* @param factor the smoothing factor property
* @since 0.4.3
*/
fun Program.smoothing(
fun Clock.smoothing(
property: KProperty0<Double>,
factor: KProperty0<Double>
): DoublePropertySmoother {
@@ -87,7 +87,7 @@ fun Program.smoothing(
* @param factor the smoothing factor
* @since 0.4.3
*/
fun <T : LinearType<T>> Program.smoothing(property: KProperty0<T>, factor: Double = 0.99): PropertySmoother<T> {
fun <T : LinearType<T>> Clock.smoothing(property: KProperty0<T>, factor: Double = 0.99): PropertySmoother<T> {
return PropertySmoother(this, property, factor, null)
}
@@ -97,6 +97,6 @@ fun <T : LinearType<T>> Program.smoothing(property: KProperty0<T>, factor: Doubl
* @param factor the smoothing factor property
* @since 0.4.3
*/
fun <T : LinearType<T>> Program.smoothing(property: KProperty0<T>, factor: KProperty0<Double>): PropertySmoother<T> {
fun <T : LinearType<T>> Clock.smoothing(property: KProperty0<T>, factor: KProperty0<Double>): PropertySmoother<T> {
return PropertySmoother(this, property, 1E10, factor)
}

View File

@@ -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<T>(private val program: Program, private val property: KProperty0<T>, val length: Int = 30) {
class PropertyTracker<T>(private val clock: Clock, private val property: KProperty0<T>, val length: Int = 30) {
private val track = mutableListOf<T>()
private var lastTime: Double? = null
operator fun getValue(any: Any?, property: KProperty<*>): List<T> {
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<T>(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<T>(private val program: Program, private val property: KPr
* @return a property tracker
* @since 0.4.3
*/
fun <T> Program.tracking(property: KProperty0<T>, length: Int = 30): PropertyTracker<T> {
fun <T> Clock.tracking(property: KProperty0<T>, length: Int = 30): PropertyTracker<T> {
return PropertyTracker(this, property, length)
}