All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.tracking.PropertyTracker.kt Maven / Gradle / Ivy

@file:Suppress("PackageDirectoryMismatch")

package org.openrndr.extra.delegatemagic.tracking

import org.openrndr.Clock
import kotlin.reflect.KProperty
import kotlin.reflect.KProperty0

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 = clock.seconds - lastTime!!
            if (dt > 1E-10) {
                track.add(this.property.get())
            }
        } else {
            track.add(this.property.get())
        }
        if (track.size > length) {
            track.removeAt(0)
        }
        lastTime = clock.seconds
        return track
    }
}

/**
 * Create a property tracker
 * @param property the property to track
 * @param length the maximum length of the tracked history
 * @return a property tracker
 * @since 0.4.3
 */
fun  Clock.tracking(property: KProperty0, length: Int = 30): PropertyTracker {
    return PropertyTracker(this, property, length)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy