[orx-panel] Add code that waits for a body ancestor to be connected in Slider/Toggle.bind

This commit is contained in:
Edwin Jakobs
2020-07-07 16:00:42 +02:00
parent c371e9dd27
commit f0ddc9339f
2 changed files with 63 additions and 44 deletions

View File

@@ -1,5 +1,7 @@
package org.openrndr.panel.elements package org.openrndr.panel.elements
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.yield import kotlinx.coroutines.yield
import mu.KotlinLogging import mu.KotlinLogging
import org.openrndr.* import org.openrndr.*
@@ -296,22 +298,26 @@ fun Slider.bind(property: KMutableProperty0<Double>) {
currentValue = it.newValue currentValue = it.newValue
property.set(it.newValue) property.set(it.newValue)
} }
if (root() as? Body == null) { GlobalScope.launch {
throw RuntimeException("no body") while(!disposed) {
} val body = (root() as? Body)
if (body != null) {
fun update() { fun update() {
if (property.get() != currentValue) { if (property.get() != currentValue) {
val lcur = property.get() val lcur = property.get()
currentValue = lcur currentValue = lcur
value = lcur value = lcur.toDouble()
} }
} }
update() update()
body.controlManager.program.launch {
(root() as? Body)?.controlManager?.program?.launch { while (!disposed) {
while (!disposed) { update()
update() yield()
}
}
break
}
yield() yield()
} }
} }
@@ -324,20 +330,26 @@ fun Slider.bind(property: KMutableProperty0<Int>) {
currentValue = it.newValue.toInt() currentValue = it.newValue.toInt()
property.set(it.newValue.toInt()) property.set(it.newValue.toInt())
} }
if (root() as? Body == null) { GlobalScope.launch {
throw RuntimeException("no body") while(!disposed) {
} val body = (root() as? Body)
fun update() { if (body != null) {
if (property.get() != currentValue) { fun update() {
val lcur = property.get() if (property.get() != currentValue) {
currentValue = lcur val lcur = property.get()
value = lcur.toDouble() currentValue = lcur
} value = lcur.toDouble()
} }
update() }
(root() as? Body)?.controlManager?.program?.launch { update()
while (!disposed) { body.controlManager.program.launch {
update() while (!disposed) {
update()
yield()
}
}
break
}
yield() yield()
} }
} }

View File

@@ -1,5 +1,7 @@
package org.openrndr.panel.elements package org.openrndr.panel.elements
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.openrndr.draw.Drawer import org.openrndr.draw.Drawer
import org.openrndr.draw.FontImageMap import org.openrndr.draw.FontImageMap
import org.openrndr.draw.LineCap import org.openrndr.draw.LineCap
@@ -106,21 +108,26 @@ fun Toggle.bind(property: KMutableProperty0<Boolean>) {
currentValue = it.newValue currentValue = it.newValue
property.set(it.newValue) property.set(it.newValue)
} }
if (root() as? Body == null) { GlobalScope.launch {
throw RuntimeException("no body")
}
fun update() {
if (property.get() != currentValue) {
val lcur = property.get()
currentValue = lcur
value = lcur
}
}
update()
(root() as Body).controlManager.program.launch {
while (!disposed) { while (!disposed) {
update() val body = (root() as? Body)
yield() if (body != null) {
fun update() {
if (property.get() != currentValue) {
val lcur = property.get()
currentValue = lcur
value = lcur
}
}
update()
(root() as Body).controlManager.program.launch {
while (!disposed) {
update()
yield()
}
}
break
}
} }
} }
} }