[orx-panel] Fix Textfield.bind

This commit is contained in:
Edwin Jakobs
2020-07-16 10:40:23 +02:00
parent c72e4829d5
commit e14c7c3d0b
2 changed files with 18 additions and 23 deletions

View File

@@ -12,7 +12,9 @@ import org.openrndr.KEY_ARROW_UP
import org.openrndr.KEY_ENTER import org.openrndr.KEY_ENTER
import org.openrndr.events.Event import org.openrndr.events.Event
import org.openrndr.launch import org.openrndr.launch
import kotlin.math.max
import kotlin.math.min import kotlin.math.min
import kotlin.math.roundToInt
import kotlin.reflect.KMutableProperty0 import kotlin.reflect.KMutableProperty0
class Item : Element(ElementType("item")) { class Item : Element(ElementType("item")) {
@@ -122,7 +124,7 @@ class DropdownButton : Element(ElementType("dropdown-button")), DisposableElemen
val textHeight = font.ascenderLength val textHeight = font.ascenderLength
val offset = Math.round((layout.screenWidth - textWidth)) val offset = Math.round((layout.screenWidth - textWidth))
val yOffset = Math.round((layout.screenHeight / 2) + textHeight / 2.0) - 2.0 val yOffset = ((layout.screenHeight / 2) + textHeight / 2.0).roundToInt() - 2.0
drawer.fill = ((computedStyle.color as? Color.RGBa)?.color ?: ColorRGBa.WHITE) drawer.fill = ((computedStyle.color as? Color.RGBa)?.color ?: ColorRGBa.WHITE)
@@ -155,8 +157,8 @@ class DropdownButton : Element(ElementType("dropdown-button")), DisposableElemen
it.cancelPropagation() it.cancelPropagation()
val newValue = parent.items()[activeIndex] val newValue = parent.items()[activeIndex]
parent.value?.let { parent.value?.let { item ->
itemButtons[it]?.pseudoClasses?.remove(ElementPseudoClass("selected")) itemButtons[item]?.pseudoClasses?.remove(ElementPseudoClass("selected"))
} }
parent.value?.let { parent.value?.let {
itemButtons[newValue]?.pseudoClasses?.add(ElementPseudoClass("selected")) itemButtons[newValue]?.pseudoClasses?.add(ElementPseudoClass("selected"))
@@ -185,8 +187,8 @@ class DropdownButton : Element(ElementType("dropdown-button")), DisposableElemen
scrollTop -= 24.0 scrollTop -= 24.0
} }
parent.value?.let { parent.value?.let { item ->
itemButtons[it]?.pseudoClasses?.remove(ElementPseudoClass("selected")) itemButtons[item]?.pseudoClasses?.remove(ElementPseudoClass("selected"))
} }
parent.value?.let { parent.value?.let {
itemButtons[newValue]?.pseudoClasses?.add(ElementPseudoClass("selected")) itemButtons[newValue]?.pseudoClasses?.add(ElementPseudoClass("selected"))
@@ -201,7 +203,7 @@ class DropdownButton : Element(ElementType("dropdown-button")), DisposableElemen
mouse.scrolled.listen { mouse.scrolled.listen {
scrollTop -= it.rotation.y scrollTop -= it.rotation.y
scrollTop = Math.max(0.0, scrollTop) scrollTop = max(0.0, scrollTop)
draw.dirty = true draw.dirty = true
it.cancelPropagation() it.cancelPropagation()
} }

View File

@@ -95,7 +95,7 @@ class Textfield : Element(ElementType("textfield")), DisposableElement {
val yOffset = Math.round((layout.screenHeight / 2) + textHeight / 2.0 - 2.0) * 1.0 val yOffset = Math.round((layout.screenHeight / 2) + textHeight / 2.0 - 2.0) * 1.0
drawer.fill = ((computedStyle.color as? Color.RGBa)?.color ?: ColorRGBa.WHITE) drawer.fill = ((computedStyle.color as? Color.RGBa)?.color ?: ColorRGBa.WHITE)
drawer.text("$label", 0.0 + offset, 0.0 + yOffset - textHeight * 1.5) drawer.text(label, 0.0 + offset, 0.0 + yOffset - textHeight * 1.5)
drawer.fill = (((computedStyle.color as? Color.RGBa)?.color ?: ColorRGBa.WHITE).opacify(0.05)) drawer.fill = (((computedStyle.color as? Color.RGBa)?.color ?: ColorRGBa.WHITE).opacify(0.05))
drawer.rectangle(0.0 + offset, 0.0 + yOffset - (textHeight + 2), layout.screenWidth - 10.0, textHeight + 8.0) drawer.rectangle(0.0 + offset, 0.0 + yOffset - (textHeight + 2), layout.screenWidth - 10.0, textHeight + 8.0)
@@ -132,41 +132,34 @@ class Textfield : Element(ElementType("textfield")), DisposableElement {
drawer.stroke = computedStyle.effectiveColor?.shade(0.25) drawer.stroke = computedStyle.effectiveColor?.shade(0.25)
drawer.lineCap = LineCap.ROUND drawer.lineCap = LineCap.ROUND
//drawer.lineSegment(0.0, yOffset + 4.0, layout.screenWidth, yOffset + 4.0)
} }
} }
override var disposed: Boolean = false override var disposed: Boolean = false
} }
fun Textfield.bind(property: KMutableProperty0<String>) { fun Textfield.bind(property: KMutableProperty0<String>) {
var currentValue = property.get()
events.valueChanged.listen {
currentValue = it.newValue
property.set(it.newValue)
}
GlobalScope.launch { GlobalScope.launch {
while (!disposed) { install@ while (!disposed) {
val body = (root() as? Body) val body = (root() as? Body)
if (body != null) { if (body != null) {
events.valueChanged.listen {
property.set(it.newValue)
}
fun update() { fun update() {
val cval = property.get() val propertyValue = property.get()
if (cval != currentValue) { if (propertyValue != value) {
currentValue = cval value = propertyValue
value = cval
} }
} }
update() update()
(root() as Body).controlManager.program.launch { (root() as Body).controlManager.program.launch {
while (true) { while (!disposed) {
update() update()
yield() yield()
} }
} }
break break@install
} }
yield() yield()
} }