diff --git a/orx-osc/src/main/kotlin/OSC.kt b/orx-osc/src/main/kotlin/OSC.kt index 6dd393de..cd0a4fc3 100644 --- a/orx-osc/src/main/kotlin/OSC.kt +++ b/orx-osc/src/main/kotlin/OSC.kt @@ -9,11 +9,13 @@ import com.illposed.osc.transport.udp.OSCPortOut import mu.KotlinLogging import java.net.InetAddress import java.net.PortUnreachableException +import kotlin.reflect.KMutableProperty0 private typealias OSCListener = Pair private val logger = KotlinLogging.logger {} +@Suppress("unused") class OSC ( val address: InetAddress = InetAddress.getLocalHost(), val portIn: Int = OSCPort.DEFAULT_SC_OSC_PORT, @@ -51,6 +53,19 @@ class OSC ( if (!receiver.isListening) this.startListening() } + infix fun String.bind(prop: KMutableProperty0) { + val channel = this + + listen(channel) { + when (val message = it.first()) { + is Double -> prop.set(message) + is Float -> prop.set(message.toDouble()) + } + } + } + + fun listen(function: OSC.() -> Unit) = function() + // Cannot be called inside a listener's callback fun removeListener(channel: String?) { val listener = listeners[channel]