Fix incorrect assumptions in getPersistedOrDefault() in orx-gui

This commit is contained in:
Edwin Jakobs
2020-02-08 18:22:17 +01:00
parent 96b2ba36fe
commit 7785103510

View File

@@ -32,7 +32,8 @@ private fun <T : Any> getPersistedOrDefault(compartmentLabel: String, property:
if (state == null) { if (state == null) {
return property.get(obj) return property.get(obj)
} else { } else {
return state.parameterValues[property.name] as? T? @Suppress("UNCHECKED_CAST")
return (state.parameterValues[property.name] as? T?) ?: return property.get(obj)
} }
} }
@@ -71,7 +72,7 @@ class GUI : Extension {
} }
styleSheet(has class_ "toolbar") { styleSheet(has class_ "toolbar") {
this.height = 30.px this.height = 50.px
this.width = 100.percent this.width = 100.percent
this.display = Display.FLEX this.display = Display.FLEX
this.flexDirection = FlexDirection.Row this.flexDirection = FlexDirection.Row
@@ -150,7 +151,7 @@ class GUI : Extension {
div("sidebar") { div("sidebar") {
id = "sidebar" id = "sidebar"
for ((labeledObject, binding) in trackedObjects) { for ((labeledObject, binding) in trackedObjects) {
val (label, obj) = labeledObject val (label, _) = labeledObject
val header = h3 { label } val header = h3 { label }
val collapsible = div("compartment") { val collapsible = div("compartment") {
@@ -322,18 +323,19 @@ class GUI : Extension {
} }
private fun loadParameters(file: File) { private fun loadParameters(file: File) {
fun <T> KMutableProperty1<out Any, Any?>?.qset(obj: Any, value:T) { fun <T> KMutableProperty1<out Any, Any?>?.qset(obj: Any, value: T) {
return (this as KMutableProperty1<Any, T>).set(obj, value) return (this as KMutableProperty1<Any, T>).set(obj, value)
} }
val json = file.readText() val json = file.readText()
val tt = object : TypeToken<Map<String, Map<String, ParameterValue>>>() {} val tt = object : TypeToken<Map<String, Map<String, ParameterValue>>>() {}
val labeledValues: Map<String, Map<String, ParameterValue>> = Gson().fromJson(json, tt.type) val labeledValues: Map<String, Map<String, ParameterValue>> = Gson().fromJson(json, tt.type)
labeledValues.forEach { label, ps -> labeledValues.forEach { (label, ps) ->
trackedObjects.keys.find { it.label == label }?.let { lo -> trackedObjects.keys.find { it.label == label }?.let { lo ->
val binding = trackedObjects[lo]!! val binding = trackedObjects[lo]!!
ps.forEach { (parameterName, parameterValue) -> ps.forEach { (parameterName, parameterValue) ->
binding.parameters.find { it.property?.name == parameterName}?.let { parameter -> binding.parameters.find { it.property?.name == parameterName }?.let { parameter ->
when (parameter.parameterType) { when (parameter.parameterType) {
ParameterType.Double -> parameterValue.doubleValue?.let { ParameterType.Double -> parameterValue.doubleValue?.let {
parameter.property.qset(lo.obj, it) parameter.property.qset(lo.obj, it)
@@ -407,7 +409,7 @@ class GUI : Extension {
(parameter.property as KMutableProperty1<Any, ColorRGBa>).set(labeledObject.obj, c) (parameter.property as KMutableProperty1<Any, ColorRGBa>).set(labeledObject.obj, c)
} }
else -> { else -> {
// do nothing // intentionally do nothing
} }
} }
} }