From bd1f7d75fcdcea10d0a609ed50a71508c391d229 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 6 Jun 2021 22:42:50 +0300 Subject: [PATCH] Fix reference child bug --- .../visionforge/gdml/demo/GDMLAppComponent.kt | 2 +- demo/playground/src/jvmMain/kotlin/gdmlIaxo.kt | 3 ++- .../ThreeViewWithControls.kt | 6 ++++-- .../visionforge/html/VisionTagConsumer.kt | 5 +++++ .../kscience/visionforge/solid/SolidReference.kt | 7 ++++++- .../visionforge/solid/three/ThreeCanvas.kt | 16 ++++++++++------ .../visionforge/solid/three/ThreePlugin.kt | 4 +--- 7 files changed, 29 insertions(+), 14 deletions(-) diff --git a/demo/gdml/src/jsMain/kotlin/space/kscience/visionforge/gdml/demo/GDMLAppComponent.kt b/demo/gdml/src/jsMain/kotlin/space/kscience/visionforge/gdml/demo/GDMLAppComponent.kt index c9afca38..0a0e83b9 100644 --- a/demo/gdml/src/jsMain/kotlin/space/kscience/visionforge/gdml/demo/GDMLAppComponent.kt +++ b/demo/gdml/src/jsMain/kotlin/space/kscience/visionforge/gdml/demo/GDMLAppComponent.kt @@ -109,7 +109,7 @@ val GDMLApp = functionalComponent("GDMLApp") { props -> } } } - ringThreeControls(options, props.vision, selected, onSelect) + ringThreeControls(options, vision, selected, onSelect) } } } diff --git a/demo/playground/src/jvmMain/kotlin/gdmlIaxo.kt b/demo/playground/src/jvmMain/kotlin/gdmlIaxo.kt index a9070af7..344c5c97 100644 --- a/demo/playground/src/jvmMain/kotlin/gdmlIaxo.kt +++ b/demo/playground/src/jvmMain/kotlin/gdmlIaxo.kt @@ -3,6 +3,7 @@ package space.kscience.visionforge.examples import space.kscience.dataforge.context.Context import space.kscience.gdml.GdmlShowCase import space.kscience.visionforge.gdml.toVision +import space.kscience.visionforge.html.ResourceLocation import space.kscience.visionforge.solid.Solids fun main() { @@ -10,7 +11,7 @@ fun main() { plugin(Solids) } - context.makeVisionFile { + context.makeVisionFile( resourceLocation = ResourceLocation.EMBED) { vision("canvas") { GdmlShowCase.babyIaxo().toVision() } } } \ No newline at end of file diff --git a/ui/ring/src/main/kotlin/space.kscience.visionforge.ring/ThreeViewWithControls.kt b/ui/ring/src/main/kotlin/space.kscience.visionforge.ring/ThreeViewWithControls.kt index 59099f60..10d5fdfb 100644 --- a/ui/ring/src/main/kotlin/space.kscience.visionforge.ring/ThreeViewWithControls.kt +++ b/ui/ring/src/main/kotlin/space.kscience.visionforge.ring/ThreeViewWithControls.kt @@ -14,14 +14,14 @@ import space.kscience.visionforge.solid.specifications.Canvas3DOptions import styled.css import styled.styledDiv -public external interface ThreeWithControlsProps : RProps { +public external interface ThreeViewWithControlsProps : RProps { public var context: Context public var vision: Vision? public var selected: Name? } @JsExport -public val ThreeViewWithControls: (props: ThreeWithControlsProps) -> dynamic = +public val ThreeViewWithControls: (props: ThreeViewWithControlsProps) -> dynamic = functionalComponent("ThreeViewWithControls") { props -> var selected by useState { props.selected } val onSelect: (Name?) -> Unit = { @@ -36,6 +36,8 @@ public val ThreeViewWithControls: (props: ThreeWithControlsProps) -> dynamic = styledDiv { css { height = 100.pct + maxHeight = 100.vh + maxWidth = 100.vw } ringGrid { ringRow { diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/html/VisionTagConsumer.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/html/VisionTagConsumer.kt index 18c53e4f..287f32bc 100644 --- a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/html/VisionTagConsumer.kt +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/html/VisionTagConsumer.kt @@ -12,11 +12,14 @@ import space.kscience.visionforge.Vision import space.kscience.visionforge.VisionManager import kotlin.collections.set +@DslMarker +public annotation class VisionDSL /** * A placeholder object to attach inline vision builders. */ @DFExperimental +@VisionDSL public class VisionOutput @PublishedApi internal constructor(public val manager: VisionManager) { public var meta: Meta = Meta.EMPTY @@ -30,6 +33,7 @@ public class VisionOutput @PublishedApi internal constructor(public val manager: /** * Modified [TagConsumer] that allows rendering output fragments and visions in them */ +@VisionDSL public abstract class VisionTagConsumer( private val root: TagConsumer, public val manager:VisionManager, @@ -86,6 +90,7 @@ public abstract class VisionTagConsumer( * TODO to be replaced by multi-receiver */ @OptIn(DFExperimental::class) + @VisionDSL public inline fun TagConsumer.vision( name: String = DEFAULT_VISION_NAME, visionProvider: VisionOutput.() -> Vision, diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidReference.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidReference.kt index d5d43100..066de405 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidReference.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidReference.kt @@ -74,8 +74,13 @@ public class SolidReferenceGroup( private fun prototypeFor(name: Name): Solid { return if (name.isEmpty()) prototype else { - (prototype as? SolidGroup)?.get(name) as? Solid + val proto = (prototype as? SolidGroup)?.get(name) ?: error("Prototype with name $name not found in SolidReferenceGroup $refName") + when (proto) { + is Solid -> proto + is SolidReference -> proto.prototype + else -> error("Prototype with name $name is ${proto::class} but expected Solid") + } } } diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeCanvas.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeCanvas.kt index 6a978e9e..0d937565 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeCanvas.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeCanvas.kt @@ -39,8 +39,11 @@ import kotlin.math.sin */ public class ThreeCanvas( public val three: ThreePlugin, + public val element: Element, public val options: Canvas3DOptions = Canvas3DOptions() ) { + + private var boundingBox: Box3? = null private var root: Object3D? = null set(value) { @@ -136,6 +139,13 @@ public class ThreeCanvas( } } + init { + check(element.getElementsByClassName("three-canvas").length == 0) { + "Three canvas already created in this element" + } + element.appendChild(canvas) + updateSize() + } /** * Force camera aspect ration and renderer size recalculation @@ -191,12 +201,6 @@ public class ThreeCanvas( } } - internal fun attach(element: Element) { - check(element.getElementsByClassName("three-canvas").length == 0) { "Three canvas already created in this element" } - element.appendChild(canvas) - updateSize() - } - /** * Resolve full name of the object relative to the global root */ diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreePlugin.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreePlugin.kt index 4d1f63b3..95c3a322 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreePlugin.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreePlugin.kt @@ -118,9 +118,7 @@ public class ThreePlugin : AbstractPlugin(), ElementVisionRenderer { element: Element, options: Canvas3DOptions = Canvas3DOptions(), ): ThreeCanvas = canvasCache.getOrPut(element) { - ThreeCanvas(this, options).apply { - attach(element) - } + ThreeCanvas(this, element, options) } override fun content(target: String): Map {