From cba59e6a89bc76137a3826aa5e164585eded58b5 Mon Sep 17 00:00:00 2001 From: Rein van der Woerd Date: Sat, 14 Mar 2020 16:17:45 +0100 Subject: [PATCH] Vector control in GUI Also using snapshot of panel --- build.gradle | 5 ++- orx-gui/src/main/kotlin/Gui.kt | 37 ++++++++++++++++++- orx-parameters/src/main/kotlin/Annotations.kt | 32 +++++++++++++++- 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index fc280aba..65f663d2 100644 --- a/build.gradle +++ b/build.gradle @@ -16,7 +16,7 @@ apply plugin: 'org.jetbrains.dokka' project.ext { openrndrVersion = "0.3.39" - panelVersion = "0.3.21" + panelVersion = "0.4.0-SNAPSHOT" kotlinVersion = "1.3.70" spekVersion = "2.0.9" libfreenectVersion = "0.5.7-1.5.2" @@ -49,6 +49,7 @@ allprojects { repositories { mavenCentral() + mavenLocal() jcenter() maven { url = "https://dl.bintray.com/openrndr/openrndr" @@ -112,4 +113,4 @@ allprojects { includeEngines 'spek2' } } -} \ 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 093a2b2e..bf57ff6c 100644 --- a/orx-gui/src/main/kotlin/Gui.kt +++ b/orx-gui/src/main/kotlin/Gui.kt @@ -13,6 +13,7 @@ import org.openrndr.dialogs.saveFileDialog import org.openrndr.draw.Drawer import org.openrndr.extra.parameters.* import org.openrndr.internal.Driver +import org.openrndr.math.Vector2 import org.openrndr.panel.ControlManager import org.openrndr.panel.controlManager import org.openrndr.panel.elements.* @@ -175,6 +176,11 @@ class GUI : Extension { descendant(has type "toggle") { this.width = 175.px } + + descendant(has type "vector2") { + this.width = 175.px + this.height = 175.px + } } styleSheet(has class_ "randomize-strong") { @@ -406,6 +412,25 @@ class GUI : Extension { } } } + + ParameterType.Vector2 -> { + vector2 { + minX = parameter.vectorRange!!.first.x + minY = parameter.vectorRange!!.first.y + maxX = parameter.vectorRange!!.second.x + maxY = parameter.vectorRange!!.second.y + + events.valueChanged.subscribe { + setAndPersist( + compartment.label, + parameter.property as KMutableProperty1, + obj, + it.newValue + ) + onChangeListener?.invoke(parameter.property!!.name, it.newValue) + } + } + } } } @@ -423,6 +448,7 @@ class GUI : Extension { var intValue: Int? = null, var booleanValue: Boolean? = null, var colorValue: ColorRGBa? = null, + var vectorValue: Vector2? = null, var textValue: String? = null) @@ -442,6 +468,7 @@ class GUI : Extension { ParameterType.Color -> ParameterValue(colorValue = k.property.qget(lo.obj) as ColorRGBa) ParameterType.Text -> ParameterValue(textValue = k.property.qget(lo.obj) as String) ParameterType.Boolean -> ParameterValue(booleanValue = k.property.qget(lo.obj) as Boolean) + ParameterType.Vector2 -> ParameterValue(vectorValue = k.property.qget(lo.obj) as Vector2) }) }) } @@ -476,6 +503,9 @@ class GUI : Extension { ParameterType.Color -> parameterValue.colorValue?.let { parameter.property.qset(lo.obj, it) } + ParameterType.Vector2 -> parameterValue.vectorValue?.let { + parameter.property.qset(lo.obj, it) + } ParameterType.Boolean -> parameterValue.booleanValue?.let { parameter.property.qset(lo.obj, it) } @@ -505,6 +535,11 @@ class GUI : Extension { ParameterType.Color -> { (control as ColorpickerButton).color = (parameter.property as KMutableProperty1).get(labeledObject.obj) } + +// ParameterType.Vector2 -> { +// (control as Vector2Control).value = (parameter.property as KMutableProperty1).get(labeledObject.obj) +// } + ParameterType.Boolean -> { (control as Toggle).value = (parameter.property as KMutableProperty1).get(labeledObject.obj) } @@ -614,4 +649,4 @@ class GUI : Extension { fun T.addTo(gui: GUI, label:String? = this.title()): T { gui.add(this, label) return this -} \ No newline at end of file +} diff --git a/orx-parameters/src/main/kotlin/Annotations.kt b/orx-parameters/src/main/kotlin/Annotations.kt index 5353647e..dac0cab0 100644 --- a/orx-parameters/src/main/kotlin/Annotations.kt +++ b/orx-parameters/src/main/kotlin/Annotations.kt @@ -1,5 +1,6 @@ package org.openrndr.extra.parameters +import org.openrndr.math.Vector2 import kotlin.reflect.KCallable import kotlin.reflect.KClass import kotlin.reflect.KMutableProperty1 @@ -71,6 +72,25 @@ annotation class TextParameter(val label: String, val order: Int = Integer.MAX_V @Retention(AnnotationRetention.RUNTIME) annotation class ColorParameter(val label: String, val order: Int = Integer.MAX_VALUE) + +/** + * Vector2 annotation for a vector 2 parameter + * @property label a short description of the parameter + * @property order hint for where to place the parameter in user interfaces + */ +@Target(AnnotationTarget.PROPERTY) +@Retention(AnnotationRetention.RUNTIME) +annotation class Vector2Parameter( + val label: String, + val minX: Double = -1.0, + val minY: Double = -1.0, + val maxX: Double = 1.0, + val maxY: Double = 1.0, + val order: Int = Integer.MAX_VALUE +) + + + /** * ActionParameter annotation for functions without arguments * @property label a short description of the parameter @@ -86,7 +106,8 @@ enum class ParameterType(val annotationClass: KClass) { Boolean(BooleanParameter::class), Action(ActionParameter::class), Text(TextParameter::class), - Color(ColorParameter::class) + Color(ColorParameter::class), + Vector2(Vector2Parameter::class) ; companion object { @@ -115,6 +136,7 @@ class Parameter( val function: KCallable?, val label: String, val doubleRange: ClosedRange?, + val vectorRange: Pair?, val intRange: IntRange?, val precision: Int?, val order: Int) @@ -136,6 +158,7 @@ fun Any.listParameters(): List { var label = "" var precision: Int? = null var type: ParameterType? = null + var vectorRange = Pair(Vector2(-1.0, -1.0), Vector2(1.0, 1.0)) annotations.forEach { type = ParameterType.forParameterAnnotationClass(it) @@ -163,6 +186,11 @@ fun Any.listParameters(): List { label = it.label order = it.order } + is Vector2Parameter -> { + label = it.label + order = it.order + vectorRange = Pair(Vector2(it.minX, it.minY), Vector2(it.maxX, it.maxY)) + } } } Parameter( @@ -171,6 +199,7 @@ fun Any.listParameters(): List { function = null, label = label, doubleRange = doubleRange, + vectorRange = vectorRange, intRange = intRange, precision = precision, order = order @@ -189,6 +218,7 @@ fun Any.listParameters(): List { label = label, doubleRange = null, intRange = null, + vectorRange = null, precision = null, order = order )