Add ColorParameter to orx-parameters

This commit is contained in:
Edwin Jakobs
2020-02-02 23:00:53 +01:00
parent 2a118f6146
commit ac76612362
2 changed files with 43 additions and 24 deletions

View File

@@ -6,6 +6,14 @@ import kotlin.reflect.KVisibility
import kotlin.reflect.full.declaredMemberProperties import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.full.findAnnotation import kotlin.reflect.full.findAnnotation
/* In case you are here to add an extra annotation type:
1. Add an annotation class
2. Add an entry to ParameterType
3. Add extra fields (if any) to Parameter
4. Add handling annotation code to listParameters
5. Add a test in TestAnnotations.kt
*/
@Target(AnnotationTarget.CLASS) @Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME) @Retention(AnnotationRetention.RUNTIME)
annotation class Description(val title: String, val description: String = "") annotation class Description(val title: String, val description: String = "")
@@ -43,7 +51,7 @@ annotation class IntParameter(val label: String, val low: Int, val high: Int, va
annotation class BooleanParameter(val label: String, val order: Int = Integer.MAX_VALUE) annotation class BooleanParameter(val label: String, val order: Int = Integer.MAX_VALUE)
/** /**
* Text annotation for a text parameter * TextParameter annotation for a text parameter
* @property label a short description of the parameter * @property label a short description of the parameter
* @property order hint for where to place the parameter in user interfaces * @property order hint for where to place the parameter in user interfaces
*/ */
@@ -51,6 +59,14 @@ annotation class BooleanParameter(val label: String, val order: Int = Integer.MA
@Retention(AnnotationRetention.RUNTIME) @Retention(AnnotationRetention.RUNTIME)
annotation class TextParameter(val label: String, val order: Int = Integer.MAX_VALUE) annotation class TextParameter(val label: String, val order: Int = Integer.MAX_VALUE)
/**
* ColorParameter annotation for a ColorRGBa 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 ColorParameter(val label: String, val order: Int = Integer.MAX_VALUE)
/** /**
* ButtonParameter annotation for button parameter * ButtonParameter annotation for button parameter
@@ -61,25 +77,20 @@ annotation class TextParameter(val label: String, val order: Int = Integer.MAX_V
@Retention(AnnotationRetention.RUNTIME) @Retention(AnnotationRetention.RUNTIME)
annotation class ButtonParameter(val label: String, val order: Int = Integer.MAX_VALUE) annotation class ButtonParameter(val label: String, val order: Int = Integer.MAX_VALUE)
enum class ParameterType(val annotation: KClass<out Annotation>) { enum class ParameterType(val annotationClass: KClass<out Annotation>) {
Double(DoubleParameter::class), Double(DoubleParameter::class),
Int(IntParameter::class), Int(IntParameter::class),
Boolean(BooleanParameter::class), Boolean(BooleanParameter::class),
Button(ButtonParameter::class), Button(ButtonParameter::class),
Text(TextParameter::class) Text(TextParameter::class),
Color(ColorParameter::class)
; ;
companion object { companion object {
fun forParameterAnnotationClass(annotation: Annotation): ParameterType { fun forParameterAnnotationClass(annotation: Annotation): ParameterType =
return when (annotation) { values().find { it.annotationClass == annotation.annotationClass } ?: error("no type for $annotation")
is DoubleParameter -> Double
is IntParameter -> Int val parameterAnnotationClasses get() = values().map { it.annotationClass }
is BooleanParameter -> Boolean
is ButtonParameter -> Button
is TextParameter -> Text
else -> error("no type for $annotation")
}
}
} }
} }
@@ -111,17 +122,9 @@ fun Any.listParameters(): List<Parameter> {
return this::class.declaredMemberProperties.filter { return this::class.declaredMemberProperties.filter {
!it.isConst && !it.isConst &&
it.visibility == KVisibility.PUBLIC && it.visibility == KVisibility.PUBLIC &&
(it.findAnnotation<BooleanParameter>() != null || it.annotations.map { it.annotationClass }.intersect(ParameterType.parameterAnnotationClasses).isNotEmpty()
it.findAnnotation<IntParameter>() != null ||
it.findAnnotation<DoubleParameter>() != null ||
it.findAnnotation<ButtonParameter>() != null) ||
it.findAnnotation<TextParameter>() != null
}.map { }.map {
val annotations = listOfNotNull(it.findAnnotation<BooleanParameter>(), val annotations = it.annotations.filter { it.annotationClass in ParameterType.parameterAnnotationClasses }
it.findAnnotation<IntParameter>(),
it.findAnnotation<DoubleParameter>(),
it.findAnnotation<ButtonParameter>(),
it.findAnnotation<TextParameter>())
var intRange: IntRange? = null var intRange: IntRange? = null
var doubleRange: ClosedRange<Double>? = null var doubleRange: ClosedRange<Double>? = null
var order: Int = Integer.MAX_VALUE var order: Int = Integer.MAX_VALUE
@@ -155,6 +158,10 @@ fun Any.listParameters(): List<Parameter> {
label = it.label label = it.label
order = it.order order = it.order
} }
is ColorParameter -> {
label = it.label
order = it.order
}
} }
} }
Parameter(type Parameter(type

View File

@@ -1,5 +1,6 @@
import org.amshove.kluent.`should be equal to` import org.amshove.kluent.`should be equal to`
import org.amshove.kluent.shouldBeInRange import org.amshove.kluent.shouldBeInRange
import org.openrndr.color.ColorRGBa
import org.openrndr.extra.parameters.* import org.openrndr.extra.parameters.*
import org.spekframework.spek2.Spek import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe import org.spekframework.spek2.style.specification.describe
@@ -19,13 +20,18 @@ val a = object {
@TextParameter("a text parameter", order = 4) @TextParameter("a text parameter", order = 4)
var t = "test" var t = "test"
@ColorParameter("a color parameter", order = 5)
var c = ColorRGBa.WHITE
} }
object TestAnnotations : Spek({ object TestAnnotations : Spek({
describe("an annotated object") { describe("an annotated object") {
it("has listable parameters") { it("has listable parameters") {
val list = a.listParameters() val list = a.listParameters()
list.size `should be equal to` 5 list.size `should be equal to` 6
list[0].property.name `should be equal to` "d" list[0].property.name `should be equal to` "d"
list[0].parameterType `should be equal to` ParameterType.Double list[0].parameterType `should be equal to` ParameterType.Double
list[0].label `should be equal to` "a double scalar" list[0].label `should be equal to` "a double scalar"
@@ -50,9 +56,15 @@ object TestAnnotations : Spek({
list[3].parameterType `should be equal to` ParameterType.Button list[3].parameterType `should be equal to` ParameterType.Button
list[3].property.name `should be equal to` "f" list[3].property.name `should be equal to` "f"
list[3].label `should be equal to` "a button parameter"
list[4].parameterType `should be equal to` ParameterType.Text list[4].parameterType `should be equal to` ParameterType.Text
list[4].property.name `should be equal to` "t" list[4].property.name `should be equal to` "t"
list[4].label `should be equal to` "a text parameter"
list[5].parameterType `should be equal to` ParameterType.Color
list[5].property.name `should be equal to` "c"
list[5].label `should be equal to` "a color parameter"
} }
} }
}) })