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
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]
}
}

View File

@ -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<NameToken, VisualObject>
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 {

View File

@ -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()
}
}
}

View File

@ -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<Proxy> {
private val cache = HashMap<VisualObject3D, Object3D>()
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) {

View File

@ -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<String, VisualObject>()