Skip to content

Commit

Permalink
Trigger requestDraw when changing TextElement text (#89)
Browse files Browse the repository at this point in the history
* Fix #85 - orx-panel requestDraw() call

* Clean up imports
  • Loading branch information
hamoid authored May 6, 2020
1 parent 58d444b commit 8fddac2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
44 changes: 21 additions & 23 deletions orx-panel/src/demo/kotlin/DemoHorizontalLayout01.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import org.openrndr.panel.controlManager
import org.openrndr.panel.elements.button
import org.openrndr.panel.elements.div
import org.openrndr.panel.elements.h1
import org.openrndr.panel.elements.requestRedraw
import org.openrndr.panel.style.*

fun main() = application {
Expand Down Expand Up @@ -45,31 +44,30 @@ fun main() = application {
"twist", "swim", "roll", "fly", "dance")
.forEachIndexed { i, word ->

// A fun way of generating a set of colors
// of similar brightness:
// Grab a point on the surface of a sphere
// and treat its coordinates as an rgb color.
val pos = Vector3.fromSpherical(
Spherical(i * 19.0, i * 17.0, 0.4))
val rgb = ColorRGBa.fromVector(pos + 0.4)
// A fun way of generating a set of colors
// of similar brightness:
// Grab a point on the surface of a sphere
// and treat its coordinates as an rgb color.
val pos = Vector3.fromSpherical(
Spherical(i * 19.0, i * 17.0, 0.4))
val rgb = ColorRGBa.fromVector(pos + 0.4)

button {
label = word
style = styleSheet {
// Use Color.RGBa() to convert a ColorRGBa
// color (the standard color datatype)
// into "CSS" format:
background = Color.RGBa(rgb)
}
button {
label = word
style = styleSheet {
// Use Color.RGBa() to convert a ColorRGBa
// color (the standard color datatype)
// into "CSS" format:
background = Color.RGBa(rgb)
}

// When the button is clicked replace
// the header text with the button's label
events.clicked.listen {
header.replaceText(it.source.label)
header.requestRedraw()
// When the button is clicked replace
// the header text with the button's label
events.clicked.listen {
header.replaceText(it.source.label)
}
}
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ class P : TextElement(ElementType("p"))
abstract class TextElement(et: ElementType) : Element(et) {
fun text(text: String) {
append(TextNode(text))
requestRedraw()
}
fun replaceText(text : String) {
if (children.isEmpty()) {
text(text)
} else {
(children.first() as? TextNode)?.text = text
requestRedraw()
}
}
}
Expand Down

0 comments on commit 8fddac2

Please sign in to comment.