From dc411e4fd3084ea640133a5992853c37afee8b72 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Sun, 29 Nov 2020 21:26:38 +0100 Subject: [PATCH] [orx-gui] Add `visible` property to Gui --- orx-gui/src/demo/kotlin/DemoHide01.kt | 57 +++++++++++++++++++++++++++ orx-gui/src/main/kotlin/Gui.kt | 31 ++++++++------- 2 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 orx-gui/src/demo/kotlin/DemoHide01.kt diff --git a/orx-gui/src/demo/kotlin/DemoHide01.kt b/orx-gui/src/demo/kotlin/DemoHide01.kt new file mode 100644 index 000000000..85eedf0ff --- /dev/null +++ b/orx-gui/src/demo/kotlin/DemoHide01.kt @@ -0,0 +1,57 @@ +import org.openrndr.application +import org.openrndr.color.ColorRGBa +import org.openrndr.extensions.SingleScreenshot +import org.openrndr.extra.gui.GUI +import org.openrndr.extra.parameters.* +import org.openrndr.math.Vector2 +import org.openrndr.shape.Circle + +/** + * A simple demonstration of a GUI for drawing some circles + */ +fun main() = application { + program { + // -- this block is for automation purposes only + if (System.getProperty("takeScreenshot") == "true") { + extend(SingleScreenshot()) { + this.outputFile = System.getProperty("screenshotPath") + } + } + + val gui = GUI() + gui.compartmentsCollapsedByDefault = false + + + val settings = @Description("Settings") object { + @DoubleParameter("radius", 0.0, 100.0) + var radius = 50.0 + + @Vector2Parameter("position", 0.0, 1.0) + var position = Vector2(0.6, 0.5) + + @ColorParameter("color") + var color = ColorRGBa.PINK + + @DoubleListParameter("radii", 5.0, 30.0) + var radii = mutableListOf(5.0, 6.0, 8.0, 14.0, 20.0, 30.0) + } + gui.add(settings) + extend(gui) + + // note we can only change the visibility after the extend + gui.visible = false + + extend { + // determine visibility through mouse x-coordinate + gui.visible = mouse.position.x < 200.0 + + drawer.fill = settings.color + drawer.circle(settings.position * drawer.bounds.position(1.0, 1.0), settings.radius) + drawer.circles( + settings.radii.mapIndexed { i, radius -> + Circle(width - 50.0, 60.0 + i * 70.0, radius) + } + ) + } + } +} \ No newline at end of file diff --git a/orx-gui/src/main/kotlin/Gui.kt b/orx-gui/src/main/kotlin/Gui.kt index dbc86daa7..533beb9af 100644 --- a/orx-gui/src/main/kotlin/Gui.kt +++ b/orx-gui/src/main/kotlin/Gui.kt @@ -72,12 +72,23 @@ class GUI : Extension { private var onChangeListener: ((name: String, value: Any?) -> Unit)? = null override var enabled = true - private var visible = true + var visible = true + set(value) { + if (field != value) { + field = value + if (field) { + panel?.body?.classes?.remove(collapsed) + } else { + panel?.body?.classes?.add(collapsed) + } + sidebarState().hidden = !field + } + } var compartmentsCollapsedByDefault = true var doubleBind = false - private lateinit var panel: ControlManager + private var panel: ControlManager? = null // Randomize button private var shiftDown = false @@ -95,12 +106,8 @@ class GUI : Extension { println("f11 pressed") visible = !visible - if(visible) { - panel.body!!.classes.remove(collapsed) - } else { - panel.body!!.classes.add(collapsed) - } - sidebarState().hidden = !visible + + } if (it.key == KEY_LEFT_SHIFT) { @@ -341,13 +348,9 @@ class GUI : Extension { } visible = !sidebarState().hidden - if(visible) { - panel.body!!.classes.remove(collapsed) - } else { - panel.body!!.classes.add(collapsed) - } - program.extend(panel) + + program.extend(panel ?: error("no panel")) } /* 2) control creation. create control, set label, set range, setup event-handler, load values */