Fix proxy styling

This commit is contained in:
Alexander Nozik 2019-10-06 22:12:17 +03:00
parent 3353d4704f
commit b8b8606371
5 changed files with 36 additions and 40 deletions

View File

@ -58,11 +58,12 @@ abstract class AbstractVisualObject : VisualObject {
private var styleCache: Laminate? = null private var styleCache: Laminate? = null
protected val actualStyles: Laminate /**
get() = styleCache ?: run { * Collect all styles for this object in a laminate
Laminate(style.map { it.toName() }.mapNotNull(::findStyle)) */
.also { styleCache = it } val appliedStyles: Laminate
} get() = styleCache ?: Laminate(style.map { it.toName() }.mapNotNull(::findStyle)).also { styleCache = it }
/** /**
* Helper to reset style cache * Helper to reset style cache
@ -74,9 +75,9 @@ abstract class AbstractVisualObject : VisualObject {
override fun getProperty(name: Name, inherit: Boolean): MetaItem<*>? { override fun getProperty(name: Name, inherit: Boolean): MetaItem<*>? {
return if (inherit) { return if (inherit) {
properties?.get(name) ?: actualStyles[name] ?: parent?.getProperty(name, inherit) properties?.get(name) ?: appliedStyles[name] ?: parent?.getProperty(name, inherit)
} else { } else {
properties?.get(name) ?: actualStyles[name] properties?.get(name) ?: appliedStyles[name]
} }
} }

View File

@ -35,8 +35,9 @@ class Proxy(val templateName: Name) : AbstractVisualObject(), VisualGroup, Visua
/** /**
* Recursively search for defined template in the parent * Recursively search for defined template in the parent
*/ */
val prototype: VisualObject3D val prototype: VisualObject3D get() = getPrototype()
get() = (parent as? VisualGroup3D)?.getTemplate(templateName)
private fun getPrototype(): VisualObject3D = (parent as? VisualGroup3D)?.getTemplate(templateName)
?: error("Template with name $templateName not found in $parent") ?: error("Template with name $templateName not found in $parent")
override fun getStyle(name: Name): Meta? = (parent as VisualGroup?)?.getStyle(name) 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 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 { inner class ProxyChild(val name: Name) : AbstractVisualObject(), VisualGroup {
val prototype: VisualObject by lazy {
prototypeFor(name)
}
override val children: Map<NameToken, VisualObject> override val children: Map<NameToken, VisualObject>
get() = ((prototype as? MutableVisualGroup)?.get(name) as? MutableVisualGroup) get() = (prototype as? VisualGroup)?.children?.mapValues { (key, _) ->
?.children
?.mapValues { (key, _) ->
ProxyChild( ProxyChild(
name + key.asName() name + key.asName()
) )
} } ?: emptyMap()
?: emptyMap()
override fun getStyle(name: Name): Meta? = this@Proxy.getStyle(name) 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) 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? override var properties: Config?
get() = propertyCache[name] get() = propertyCache[name]
set(value) { set(value) {
@ -112,15 +115,16 @@ class Proxy(val templateName: Name) : AbstractVisualObject(), VisualGroup, Visua
override fun getProperty(name: Name, inherit: Boolean): MetaItem<*>? { override fun getProperty(name: Name, inherit: Boolean): MetaItem<*>? {
return if (inherit) { return if (inherit) {
properties?.get(name) properties?.get(name)
?: actualStyles[name] ?: appliedStyles[name]
?: parent?.getProperty(name, inherit) ?: parent?.getProperty(name, inherit)
?: prototype.getProperty(name, inherit) ?: prototype.getProperty(name, inherit)
} else { } else {
properties?.get(name) properties?.get(name)
?: actualStyles[name] ?: appliedStyles[name]
?: prototype.getProperty(name, inherit) ?: prototype.getProperty(name, inherit)
} }
} }
} }
companion object { companion object {

View File

@ -20,7 +20,7 @@ import hep.dataforge.names.NameToken
import hep.dataforge.names.asName import hep.dataforge.names.asName
import hep.dataforge.names.isEmpty import hep.dataforge.names.isEmpty
import hep.dataforge.vis.common.AbstractVisualGroup 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 hep.dataforge.vis.common.VisualObject
import kotlinx.serialization.SerialName import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable 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 * 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 { this.children.values.forEach {
it.parent = this it.parent = this
(it as? MutableVisualGroup)?.attachChildren() (it as? VisualGroup)?.attachChildren()
} }
if (this is VisualGroup3D) { if (this is VisualGroup3D) {
templates?.apply { templates?.also {
parent = this@attachChildren it.parent = this
attachChildren() it.attachChildren()
} }
} }
} }

View File

@ -1,25 +1,16 @@
package hep.dataforge.vis.spatial.three package hep.dataforge.vis.spatial.three
import hep.dataforge.names.toName import hep.dataforge.names.toName
import hep.dataforge.vis.common.VisualObject
import hep.dataforge.vis.spatial.Proxy import hep.dataforge.vis.spatial.Proxy
import hep.dataforge.vis.spatial.Proxy.Companion.PROXY_CHILD_PROPERTY_PREFIX import hep.dataforge.vis.spatial.Proxy.Companion.PROXY_CHILD_PROPERTY_PREFIX
import hep.dataforge.vis.spatial.VisualObject3D 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.core.Object3D
import info.laht.threekt.objects.Mesh
class ThreeProxyFactory(val three: ThreePlugin) : ThreeFactory<Proxy> { class ThreeProxyFactory(val three: ThreePlugin) : ThreeFactory<Proxy> {
private val cache = HashMap<VisualObject3D, Object3D>() private val cache = HashMap<VisualObject3D, Object3D>()
override val type = Proxy::class 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 { override fun invoke(obj: Proxy): Object3D {
val template = obj.prototype val template = obj.prototype
val cachedObject = cache.getOrPut(template) { val cachedObject = cache.getOrPut(template) {

View File

@ -32,7 +32,7 @@ internal fun createInspireTree(block: Config.() -> Unit = {}): InspireTree {
return InspireTree(config) return InspireTree(config)
} }
fun VisualGroup.toTree(onFocus: (VisualObject?, String?) -> Unit = { obj, name -> }): InspireTree { fun VisualGroup.toTree(onFocus: (VisualObject?, String?) -> Unit = { _, _ -> }): InspireTree {
val map = HashMap<String, VisualObject>() val map = HashMap<String, VisualObject>()