[orx-parameters, orx-property-watchers, orx-file-watcher, orx-gui] Add @PathParameter, file watcher delegates and property delegates

This commit is contained in:
Edwin Jakobs
2023-03-18 20:32:43 +01:00
parent bab525cd92
commit 84e623c3e8
20 changed files with 553 additions and 143 deletions

View File

@@ -3,14 +3,7 @@ plugins {
}
kotlin {
jvm {
testRuns["test"].executionTask {
useJUnitPlatform {
includeEngines("spek2")
}
}
}
sourceSets {
sourceSets {
@Suppress("UNUSED_VARIABLE")
val commonMain by getting {
dependencies {
@@ -25,8 +18,6 @@ kotlin {
val jvmTest by getting {
dependencies {
implementation(libs.kluent)
implementation(libs.spek.dsl)
runtimeOnly(libs.spek.junit5)
runtimeOnly(libs.kotlin.reflect)
}
}

View File

@@ -159,6 +159,29 @@ annotation class Vector4Parameter(
@Retention(AnnotationRetention.RUNTIME)
annotation class ActionParameter(val label: String, val order: Int = Int.MAX_VALUE)
/**
* ActionParameter annotation for functions without arguments
* @property label a short description of the parameter
* @property absolute should the path be stored as an absolute path
* @property context which dialog context to use
* @property extensions an array of supported extensions
* @property directory the path points to a directory
* @property order hint for where to place the parameter in user interfaces
*/
@Target(AnnotationTarget.PROPERTY)
@Retention(AnnotationRetention.RUNTIME)
annotation class PathParameter(
val label: String,
val absolute: Boolean = false,
val context: String = "null",
val extensions: Array<String> = [],
val directory: Boolean = false,
val order: Int = Int.MAX_VALUE
)
//</editor-fold>
//<editor-fold desc="2. Add an entry to ParameterType" id="add-parameter-type" defaultstate="collapsed">
enum class ParameterType(val annotationClass: KClass<out Annotation>) {
@@ -173,7 +196,8 @@ enum class ParameterType(val annotationClass: KClass<out Annotation>) {
Vector2(Vector2Parameter::class),
Vector3(Vector3Parameter::class),
Vector4(Vector4Parameter::class),
Option(OptionParameter::class)
Option(OptionParameter::class),
Path(PathParameter::class)
;
companion object {
@@ -209,7 +233,12 @@ class Parameter(
val invertY: Boolean?,
val showVector: Boolean?,
// val optionEnum: Enum<*>,
val order: Int)
val absolutePath: Boolean?,
val pathContext: String?,
val pathExtensions: Array<String>?,
val pathIsDirectory: Boolean?,
val order: Int,
)
//</editor-fold>
//<editor-fold desc="4. Add handling annotation code to listParameters" defaultstate="collapsed">
/**

View File

@@ -31,6 +31,10 @@ fun Any.listParameters(): List<Parameter> {
var vectorRange = Pair(Vector2(-1.0, -1.0), Vector2(1.0, 1.0))
var invertY: Boolean? = null
var showVector: Boolean? = null
var absolutePath: Boolean? = null
var pathContext: String? = null
var pathExtensions: Array<String>? = null
var pathIsDirectory: Boolean? = null
for (it in annotations) {
type = ParameterType.forParameterAnnotationClass(it)
@@ -95,6 +99,13 @@ fun Any.listParameters(): List<Parameter> {
label = it.label
order = it.order
}
is PathParameter -> {
label = it.label
absolutePath = it.absolute
pathContext = it.context
pathExtensions = it.extensions
pathIsDirectory = it.directory
}
}
}
Parameter(
@@ -109,7 +120,12 @@ fun Any.listParameters(): List<Parameter> {
precision = precision,
showVector = showVector,
invertY = invertY,
order = order
absolutePath = absolutePath,
pathContext = pathContext,
pathExtensions = pathExtensions,
pathIsDirectory = pathIsDirectory,
order = order,
)
} + this::class.declaredMemberFunctions.filter {
it.findAnnotation<ActionParameter>() != null
@@ -130,6 +146,10 @@ fun Any.listParameters(): List<Parameter> {
precision = null,
showVector = null,
invertY = null,
absolutePath = null,
pathContext = null,
pathExtensions = null,
pathIsDirectory = null,
order = order
)
}).sortedBy { it.order }

View File

@@ -1,4 +1,5 @@
import org.amshove.kluent.*
import org.junit.jupiter.api.Assertions.assertEquals
import org.openrndr.color.ColorRGBa
import org.openrndr.extra.parameters.*
import org.openrndr.math.Vector2
@@ -46,6 +47,9 @@ val a = object {
@OptionParameter("an option parameter", order = 11)
var o = ParameterType.Option
@PathParameter("a path parameter", order = 12)
var p = "bla.png"
}
object TestAnnotations : Spek({
@@ -124,6 +128,12 @@ object TestAnnotations : Spek({
list[11].property?.name `should be equal to` "o"
list[11].label `should be equal to` "an option parameter"
assertEquals(list[12].parameterType, ParameterType.Path)
assertEquals(list[12].property?.name, "p")
assertEquals(list[12].label, "a path parameter")
assertEquals(list[12].absolutePath, false)
assertEquals(list[12].pathContext, "null")
}
}
})