From b8b8606371358b87679b0cd972a4403d70bf0881 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 6 Oct 2019 22:12:17 +0300 Subject: [PATCH] Fix proxy styling --- .../vis/common/AbstractVisualObject.kt | 15 ++++---- .../kotlin/hep/dataforge/vis/spatial/Proxy.kt | 38 ++++++++++--------- .../dataforge/vis/spatial/VisualGroup3D.kt | 12 +++--- .../vis/spatial/three/ThreeProxyFactory.kt | 9 ----- .../vis/spatial/tree/jsVisualTree.kt | 2 +- 5 files changed, 36 insertions(+), 40 deletions(-) diff --git a/dataforge-vis-common/src/commonMain/kotlin/hep/dataforge/vis/common/AbstractVisualObject.kt b/dataforge-vis-common/src/commonMain/kotlin/hep/dataforge/vis/common/AbstractVisualObject.kt index effe38d8..c4ce41ae 100644 --- a/dataforge-vis-common/src/commonMain/kotlin/hep/dataforge/vis/common/AbstractVisualObject.kt +++ b/dataforge-vis-common/src/commonMain/kotlin/hep/dataforge/vis/common/AbstractVisualObject.kt @@ -58,11 +58,12 @@ abstract class AbstractVisualObject : VisualObject { private var styleCache: Laminate? = null - protected val actualStyles: Laminate - get() = styleCache ?: run { - Laminate(style.map { it.toName() }.mapNotNull(::findStyle)) - .also { styleCache = it } - } + /** + * Collect all styles for this object in a laminate + */ + val appliedStyles: Laminate + get() = styleCache ?: Laminate(style.map { it.toName() }.mapNotNull(::findStyle)).also { styleCache = it } + /** * Helper to reset style cache @@ -74,9 +75,9 @@ abstract class AbstractVisualObject : VisualObject { override fun getProperty(name: Name, inherit: Boolean): MetaItem<*>? { return if (inherit) { - properties?.get(name) ?: actualStyles[name] ?: parent?.getProperty(name, inherit) + properties?.get(name) ?: appliedStyles[name] ?: parent?.getProperty(name, inherit) } else { - properties?.get(name) ?: actualStyles[name] + properties?.get(name) ?: appliedStyles[name] } } diff --git a/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/Proxy.kt b/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/Proxy.kt index ef284f22..111490aa 100644 --- a/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/Proxy.kt +++ b/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/Proxy.kt @@ -35,9 +35,10 @@ class Proxy(val templateName: Name) : AbstractVisualObject(), VisualGroup, Visua /** * Recursively search for defined template in the parent */ - val prototype: VisualObject3D - get() = (parent as? VisualGroup3D)?.getTemplate(templateName) - ?: error("Template with name $templateName not found in $parent") + val prototype: VisualObject3D get() = getPrototype() + + private fun getPrototype(): VisualObject3D = (parent as? VisualGroup3D)?.getTemplate(templateName) + ?: error("Template with name $templateName not found in $parent") override fun getStyle(name: Name): Meta? = (parent as VisualGroup?)?.getStyle(name) @@ -70,17 +71,23 @@ class Proxy(val templateName: Name) : AbstractVisualObject(), VisualGroup, Visua return NameToken(PROXY_CHILD_PROPERTY_PREFIX, childName.toString()) + propertyName } + private fun prototypeFor(name: Name): VisualObject = + (prototype as? VisualGroup)?.get(name) + ?: error("Prototype with name $name not found in ${this@Proxy}") + + inner class ProxyChild(val name: Name) : AbstractVisualObject(), VisualGroup { + val prototype: VisualObject by lazy { + prototypeFor(name) + } + override val children: Map - get() = ((prototype as? MutableVisualGroup)?.get(name) as? MutableVisualGroup) - ?.children - ?.mapValues { (key, _) -> - ProxyChild( - name + key.asName() - ) - } - ?: emptyMap() + get() = (prototype as? VisualGroup)?.children?.mapValues { (key, _) -> + ProxyChild( + name + key.asName() + ) + } ?: emptyMap() override fun getStyle(name: Name): Meta? = this@Proxy.getStyle(name) @@ -88,10 +95,6 @@ class Proxy(val templateName: Name) : AbstractVisualObject(), VisualGroup, Visua this@Proxy.setStyle(name, meta) } - val prototype: VisualObject - get() = (this@Proxy.prototype as? VisualGroup)?.get(name) - ?: error("Prototype with name $name not found in ${this@Proxy}") - override var properties: Config? get() = propertyCache[name] set(value) { @@ -112,15 +115,16 @@ class Proxy(val templateName: Name) : AbstractVisualObject(), VisualGroup, Visua override fun getProperty(name: Name, inherit: Boolean): MetaItem<*>? { return if (inherit) { properties?.get(name) - ?: actualStyles[name] + ?: appliedStyles[name] ?: parent?.getProperty(name, inherit) ?: prototype.getProperty(name, inherit) } else { properties?.get(name) - ?: actualStyles[name] + ?: appliedStyles[name] ?: prototype.getProperty(name, inherit) } } + } companion object { diff --git a/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/VisualGroup3D.kt b/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/VisualGroup3D.kt index e2ccbd25..d960cfc7 100644 --- a/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/VisualGroup3D.kt +++ b/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/VisualGroup3D.kt @@ -20,7 +20,7 @@ import hep.dataforge.names.NameToken import hep.dataforge.names.asName import hep.dataforge.names.isEmpty import hep.dataforge.vis.common.AbstractVisualGroup -import hep.dataforge.vis.common.MutableVisualGroup +import hep.dataforge.vis.common.VisualGroup import hep.dataforge.vis.common.VisualObject import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @@ -103,15 +103,15 @@ class VisualGroup3D : AbstractVisualGroup(), VisualObject3D { /** * A fix for serialization bug that writes all proper parents inside the tree after deserialization */ -fun MutableVisualGroup.attachChildren() { +fun VisualGroup.attachChildren() { this.children.values.forEach { it.parent = this - (it as? MutableVisualGroup)?.attachChildren() + (it as? VisualGroup)?.attachChildren() } if (this is VisualGroup3D) { - templates?.apply { - parent = this@attachChildren - attachChildren() + templates?.also { + it.parent = this + it.attachChildren() } } } diff --git a/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/three/ThreeProxyFactory.kt b/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/three/ThreeProxyFactory.kt index 91effe00..05bdd9e1 100644 --- a/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/three/ThreeProxyFactory.kt +++ b/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/three/ThreeProxyFactory.kt @@ -1,25 +1,16 @@ package hep.dataforge.vis.spatial.three import hep.dataforge.names.toName -import hep.dataforge.vis.common.VisualObject import hep.dataforge.vis.spatial.Proxy import hep.dataforge.vis.spatial.Proxy.Companion.PROXY_CHILD_PROPERTY_PREFIX import hep.dataforge.vis.spatial.VisualObject3D -import hep.dataforge.vis.spatial.material -import hep.dataforge.vis.spatial.visible import info.laht.threekt.core.Object3D -import info.laht.threekt.objects.Mesh class ThreeProxyFactory(val three: ThreePlugin) : ThreeFactory { private val cache = HashMap() override val type = Proxy::class - private fun Mesh.updateProperties(obj: VisualObject?) { - material = obj?.material.jsMaterial() - visible = obj?.visible ?: true - } - override fun invoke(obj: Proxy): Object3D { val template = obj.prototype val cachedObject = cache.getOrPut(template) { diff --git a/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/tree/jsVisualTree.kt b/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/tree/jsVisualTree.kt index 2d86b7f4..84bd067d 100644 --- a/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/tree/jsVisualTree.kt +++ b/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/tree/jsVisualTree.kt @@ -32,7 +32,7 @@ internal fun createInspireTree(block: Config.() -> Unit = {}): InspireTree { return InspireTree(config) } -fun VisualGroup.toTree(onFocus: (VisualObject?, String?) -> Unit = { obj, name -> }): InspireTree { +fun VisualGroup.toTree(onFocus: (VisualObject?, String?) -> Unit = { _, _ -> }): InspireTree { val map = HashMap()