From 8d21d3cd740ac9808f2292120eee405605b7ea5a Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sat, 7 Aug 2021 11:27:57 +0300 Subject: [PATCH] Adjust property editors --- build.gradle.kts | 11 +- .../visionforge/gdml/GDMLVisionTest.kt | 4 +- .../visionforge/gdml/demo/GdmlFxDemoApp.kt | 11 +- .../kotlin/ru/mipt/npm/muon/monitor/Model.kt | 1 + demo/playground/build.gradle.kts | 1 + .../src/jvmMain/kotlin/markdownDemo.kt | 91 +++++++ .../src/jvmMain/kotlin/serverExtensions.kt | 2 +- .../src/jvmMain/kotlin/simpleCube.kt | 7 +- .../main/kotlin/ru/mipt/npm/sat/satServer.kt | 4 +- .../visionforge/solid/demo}/VisionLayout.kt | 3 +- .../kscience/visionforge/solid/demo/demo.kt | 5 +- .../visionforge/solid/demo/ThreeDemoGrid.kt | 1 - .../visionforge/solid/demo/FXDemoGrid.kt | 1 - .../visionforge/solid/demo/MetaEditorDemo.kt | 33 +-- docs/design.md | 2 +- docs/uml/Vision.puml | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- settings.gradle.kts | 2 +- ui/bootstrap/build.gradle.kts | 2 +- .../visionforge/bootstrap/outputConfig.kt | 5 +- .../visionforge/bootstrap/tabComponent.kt | 4 +- .../bootstrap/visionPropertyEditor.kt | 15 +- .../kscience/visionforge/react/MetaViewer.kt | 137 +++++------ .../visionforge/react/MultiSelectChooser.kt | 10 +- .../visionforge/react/PropertyEditor.kt | 155 ++++++------ .../visionforge/react/RangeValueChooser.kt | 4 +- .../visionforge/react/ThreeCanvasComponent.kt | 2 +- .../kscience/visionforge/react/VisionTree.kt | 2 +- .../visionforge/react/valueChooser.kt | 43 ++-- ui/ring/build.gradle.kts | 9 +- .../ThreeViewWithControls.kt | 17 +- .../ringPropertyEditor.kt | 15 +- .../ringThreeControls.kt | 7 +- .../space/kscience/visionforge/Colors.kt | 33 ++- .../kscience/visionforge/StyleReference.kt | 4 +- .../space/kscience/visionforge/StyleSheet.kt | 21 +- .../space/kscience/visionforge/Vision.kt | 88 ++++--- .../space/kscience/visionforge/VisionBase.kt | 76 +++--- .../kscience/visionforge/VisionChange.kt | 10 +- .../space/kscience/visionforge/VisionGroup.kt | 4 +- .../kscience/visionforge/VisionGroupBase.kt | 6 +- .../kscience/visionforge/VisionManager.kt | 15 +- .../kscience/visionforge/VisionProperties.kt | 38 +++ .../visionforge/VisionPropertyContainer.kt | 33 ++- .../visionforge/html/VisionTagConsumer.kt | 13 +- .../kotlin/space/kscience/visionforge/misc.kt | 24 +- .../kscience/visionforge/schemeDesctiptors.kt | 54 ++--- .../kscience/visionforge/visionDelegates.kt | 93 ++++++++ .../kscience/visionforge/visionDescriptor.kt | 64 ++--- .../kscience/visionforge/html/HtmlTagTest.kt | 2 +- .../kscience/visionforge/VisionClient.kt | 2 +- .../space/kscience/visionforge/FXPlugin.kt | 2 +- .../editor/ComboBoxValueChooser.kt | 4 +- .../visionforge/editor/ConfigEditor.kt | 189 --------------- .../kscience/visionforge/editor/FXMeta.kt | 223 ------------------ .../visionforge/editor/FXMetaModel.kt | 217 +++++++++++++++++ .../kscience/visionforge/editor/MetaViewer.kt | 41 ++-- .../visionforge/editor/MutableMetaEditor.kt | 193 +++++++++++++++ .../visionforge/editor/TextValueChooser.kt | 7 +- .../visionforge/editor/ValueCallback.kt | 4 +- .../visionforge/editor/ValueChooser.kt | 22 +- .../visionforge/editor/ValueChooserBase.kt | 16 +- ...torFragment.kt => VisionEditorFragment.kt} | 39 ++- ...tTreeFragment.kt => VisionTreeFragment.kt} | 11 +- .../kscience/visionforge/solid/FXMaterials.kt | 49 ++-- .../visionforge/solid/FXReferenceFactory.kt | 7 +- .../solid/VisualObjectFXBinding.kt | 38 ++- .../visionforge/gdml/GdmlTransformer.kt | 10 +- .../src/commonTest/kotlin/TestCubes.kt | 6 +- .../visionforge/gdml/TestConvertor.kt | 4 +- .../visionforge/markup/VisionOfMarkup.kt | 4 +- visionforge-plotly/build.gradle.kts | 2 +- .../visionforge/plotly/VisionOfPlotly.kt | 6 +- .../kscience/visionforge/plotly/plotlyJs.kt | 2 +- .../visionforge/three/server/VisionServer.kt | 21 +- .../visionforge/solid/ColorAccessor.kt | 16 +- .../kscience/visionforge/solid/Composite.kt | 2 +- .../kscience/visionforge/solid/Extruded.kt | 7 +- .../kscience/visionforge/solid/PolyLine.kt | 7 +- .../space/kscience/visionforge/solid/Solid.kt | 37 ++- .../kscience/visionforge/solid/SolidBase.kt | 8 +- .../kscience/visionforge/solid/SolidGroup.kt | 8 +- .../visionforge/solid/SolidMaterial.kt | 42 ++-- .../visionforge/solid/SolidReference.kt | 32 +-- .../kscience/visionforge/solid/geometry.kt | 10 +- .../visionforge/solid/specifications/Axes.kt | 6 +- .../solid/specifications/Camera.kt | 6 +- .../solid/specifications/Canvas3DOptions.kt | 39 ++- .../solid/transform/RemoveSingleChild.kt | 13 +- .../kscience/visionforge/solid/ConvexTest.kt | 7 +- .../visionforge/solid/DescriptorTest.kt | 3 +- .../visionforge/solid/PropertyTest.kt | 2 +- .../visionforge/solid/SerializationTest.kt | 15 +- .../visionforge/solid/VisionUpdateTest.kt | 15 +- .../solid/three/MeshThreeFactory.kt | 2 +- .../visionforge/solid/three/ThreeCanvas.kt | 10 +- .../solid/three/ThreeLineFactory.kt | 6 +- .../visionforge/solid/three/ThreeMaterials.kt | 42 ++-- .../visionforge/solid/three/ThreePlugin.kt | 6 +- .../solid/three/ThreeReferenceFactory.kt | 4 +- .../kscience/visionforge/solid/three/three.kt | 4 +- 101 files changed, 1410 insertions(+), 1246 deletions(-) create mode 100644 demo/playground/src/jvmMain/kotlin/markdownDemo.kt rename {visionforge-core/src/commonMain/kotlin/space/kscience/visionforge => demo/solid-showcase/src/commonMain/kotlin/space/kscience/visionforge/solid/demo}/VisionLayout.kt (69%) create mode 100644 visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionProperties.kt create mode 100644 visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/visionDelegates.kt delete mode 100644 visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/ConfigEditor.kt delete mode 100644 visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/FXMeta.kt create mode 100644 visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/FXMetaModel.kt create mode 100644 visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/MutableMetaEditor.kt rename visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/{VisualObjectEditorFragment.kt => VisionEditorFragment.kt} (57%) rename visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/{VisualObjectTreeFragment.kt => VisionTreeFragment.kt} (82%) diff --git a/build.gradle.kts b/build.gradle.kts index c7fbc2d8..22615a5d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,18 +1,13 @@ plugins { id("ru.mipt.npm.gradle.project") - - //Override kotlin version -// val kotlinVersion = "1.5.20-RC" -// kotlin("multiplatform") version(kotlinVersion) apply false -// kotlin("jvm") version(kotlinVersion) apply false -// kotlin("js") version(kotlinVersion) apply false } -val dataforgeVersion by extra("0.5.0-dev-2") +val dataforgeVersion by extra("0.5.0-dev-9") val fxVersion by extra("11") allprojects { repositories { + mavenLocal() mavenCentral() jcenter() maven("https://repo.kotlin.link") @@ -29,7 +24,7 @@ subprojects { } } -ksciencePublish{ +ksciencePublish { github("visionforge") space() sonatype() diff --git a/demo/gdml/src/commonTest/kotlin/space/kscience/visionforge/gdml/GDMLVisionTest.kt b/demo/gdml/src/commonTest/kotlin/space/kscience/visionforge/gdml/GDMLVisionTest.kt index 17bdc014..66332d9d 100644 --- a/demo/gdml/src/commonTest/kotlin/space/kscience/visionforge/gdml/GDMLVisionTest.kt +++ b/demo/gdml/src/commonTest/kotlin/space/kscience/visionforge/gdml/GDMLVisionTest.kt @@ -1,7 +1,7 @@ package space.kscience.visionforge.gdml import space.kscience.dataforge.meta.string -import space.kscience.dataforge.names.toName +import space.kscience.dataforge.names.Name import space.kscience.dataforge.values.asValue import space.kscience.gdml.GdmlShowCase import space.kscience.visionforge.setProperty @@ -24,7 +24,7 @@ class GDMLVisionTest { @Test fun testPrototypeProperty() { val vision = GdmlShowCase.cubes().toVision() - val child = vision["composite-000.segment-0".toName()] + val child = vision[Name.of("composite-000","segment-0")] assertNotNull(child) child.setProperty(SolidMaterial.MATERIAL_COLOR_KEY, "red".asValue()) assertEquals("red", child.getProperty(SolidMaterial.MATERIAL_COLOR_KEY).string) diff --git a/demo/gdml/src/jvmMain/kotlin/space/kscience/visionforge/gdml/demo/GdmlFxDemoApp.kt b/demo/gdml/src/jvmMain/kotlin/space/kscience/visionforge/gdml/demo/GdmlFxDemoApp.kt index 4fd1e2e7..d4cd85b0 100644 --- a/demo/gdml/src/jvmMain/kotlin/space/kscience/visionforge/gdml/demo/GdmlFxDemoApp.kt +++ b/demo/gdml/src/jvmMain/kotlin/space/kscience/visionforge/gdml/demo/GdmlFxDemoApp.kt @@ -8,8 +8,8 @@ import space.kscience.dataforge.context.fetch import space.kscience.gdml.GdmlShowCase import space.kscience.visionforge.VisionManager import space.kscience.visionforge.describedProperties -import space.kscience.visionforge.editor.VisualObjectEditorFragment -import space.kscience.visionforge.editor.VisualObjectTreeFragment +import space.kscience.visionforge.editor.VisionEditorFragment +import space.kscience.visionforge.editor.VisionTreeFragment import space.kscience.visionforge.gdml.toVision import space.kscience.visionforge.solid.FX3DPlugin import space.kscience.visionforge.solid.FXCanvas3D @@ -29,25 +29,24 @@ class GDMLView : View() { private val visionManager = context.fetch(VisionManager) private val canvas = FXCanvas3D(fx3d) - private val treeFragment = VisualObjectTreeFragment().apply { + private val treeFragment = VisionTreeFragment().apply { this.itemProperty.bind(canvas.rootObjectProperty) } - private val propertyEditor = VisualObjectEditorFragment { + private val propertyEditor = VisionEditorFragment { it.describedProperties }.apply { descriptorProperty.set(SolidMaterial.descriptor) itemProperty.bind(treeFragment.selectedProperty) } - override val root: Parent = borderpane { top { buttonbar { button("Load GDML/json") { action { val file = chooseFile("Select a GDML/json file", filters = fileNameFilter).firstOrNull() - if(file!= null) { + if (file != null) { runAsync { visionManager.readFile(file) as Solid } ui { diff --git a/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Model.kt b/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Model.kt index c485a1e3..87065723 100644 --- a/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Model.kt +++ b/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Model.kt @@ -6,6 +6,7 @@ import ru.mipt.npm.muon.monitor.Monitor.UPPER_LAYER_Z import space.kscience.visionforge.VisionManager import space.kscience.visionforge.removeAll import space.kscience.visionforge.root +import space.kscience.visionforge.setProperty import space.kscience.visionforge.solid.* import kotlin.math.PI diff --git a/demo/playground/build.gradle.kts b/demo/playground/build.gradle.kts index 7c99d6fa..73fd2aa7 100644 --- a/demo/playground/build.gradle.kts +++ b/demo/playground/build.gradle.kts @@ -67,6 +67,7 @@ kotlin { val jvmMain by getting{ dependencies { api(project(":visionforge-server")) + api(project(":visionforge-markdown")) api("ch.qos.logback:logback-classic:1.2.3") implementation("com.github.Ricky12Awesome:json-schema-serialization:0.6.6") } diff --git a/demo/playground/src/jvmMain/kotlin/markdownDemo.kt b/demo/playground/src/jvmMain/kotlin/markdownDemo.kt new file mode 100644 index 00000000..cb4b7cf0 --- /dev/null +++ b/demo/playground/src/jvmMain/kotlin/markdownDemo.kt @@ -0,0 +1,91 @@ +package space.kscience.visionforge.examples + +import kotlinx.html.div +import kotlinx.html.h1 +import space.kscience.dataforge.context.Context +import space.kscience.plotly.layout +import space.kscience.plotly.models.ScatterMode +import space.kscience.plotly.models.TextPosition +import space.kscience.plotly.scatter +import space.kscience.visionforge.html.ResourceLocation +import space.kscience.visionforge.markup.markdown +import space.kscience.visionforge.plotly.PlotlyPlugin +import space.kscience.visionforge.plotly.plotly +import space.kscience.visionforge.solid.* + +fun main() { + val context = Context { + plugin(Solids) + plugin(PlotlyPlugin) + } + + context.makeVisionFile(resourceLocation = ResourceLocation.SYSTEM) { + markdown { + //language=markdown + """ + # Section + + **TBD** + + ## Subsection + """.trimIndent() + } + + div { + h1 { +"Canvas" } + vision("canvas") { + solid { + box(100, 100, 100) + material { + emissiveColor("red") + } + } + } + } + + vision("plot") { + plotly { + scatter { + x(1, 2, 3, 4) + y(10, 15, 13, 17) + mode = ScatterMode.markers + name = "Team A" + text("A-1", "A-2", "A-3", "A-4", "A-5") + textposition = TextPosition.`top center` + textfont { + family = "Raleway, sans-serif" + } + marker { size = 12 } + } + + scatter { + x(2, 3, 4, 5) + y(10, 15, 13, 17) + mode = ScatterMode.lines + name = "Team B" + text("B-a", "B-b", "B-c", "B-d", "B-e") + textposition = TextPosition.`bottom center` + textfont { + family = "Times New Roman" + } + marker { size = 12 } + } + + layout { + title = "Data Labels Hover" + xaxis { + range(0.75..5.25) + } + legend { + y = 0.5 + font { + family = "Arial, sans-serif" + size = 20 + color("grey") + } + } + } + } + } + } +} \ No newline at end of file diff --git a/demo/playground/src/jvmMain/kotlin/serverExtensions.kt b/demo/playground/src/jvmMain/kotlin/serverExtensions.kt index b71b1873..e4111afc 100644 --- a/demo/playground/src/jvmMain/kotlin/serverExtensions.kt +++ b/demo/playground/src/jvmMain/kotlin/serverExtensions.kt @@ -26,7 +26,7 @@ public fun Context.makeVisionFile( content: VisionTagConsumer<*>.() -> Unit ): Unit { val actualPath = page(title, content = content).makeFile(path) { actualPath -> - mapOf("threeJs" to scriptHeader("js/visionforge-playground.js", resourceLocation, actualPath)) + mapOf("playground" to scriptHeader("js/visionforge-playground.js", resourceLocation, actualPath)) } if (show) Desktop.getDesktop().browse(actualPath.toFile().toURI()) } diff --git a/demo/playground/src/jvmMain/kotlin/simpleCube.kt b/demo/playground/src/jvmMain/kotlin/simpleCube.kt index e98d1ff1..f804228b 100644 --- a/demo/playground/src/jvmMain/kotlin/simpleCube.kt +++ b/demo/playground/src/jvmMain/kotlin/simpleCube.kt @@ -2,9 +2,7 @@ package space.kscience.visionforge.examples import space.kscience.dataforge.context.Context import space.kscience.visionforge.html.ResourceLocation -import space.kscience.visionforge.solid.Solids -import space.kscience.visionforge.solid.box -import space.kscience.visionforge.solid.solid +import space.kscience.visionforge.solid.* fun main() { val context = Context { @@ -15,6 +13,9 @@ fun main() { vision("canvas") { solid { box(100, 100, 100) + material { + emissiveColor("red") + } } } } diff --git a/demo/sat-demo/src/main/kotlin/ru/mipt/npm/sat/satServer.kt b/demo/sat-demo/src/main/kotlin/ru/mipt/npm/sat/satServer.kt index 6d80c691..8b286e07 100644 --- a/demo/sat-demo/src/main/kotlin/ru/mipt/npm/sat/satServer.kt +++ b/demo/sat-demo/src/main/kotlin/ru/mipt/npm/sat/satServer.kt @@ -8,7 +8,7 @@ import kotlinx.coroutines.launch import kotlinx.html.div import kotlinx.html.h1 import space.kscience.dataforge.context.Global -import space.kscience.dataforge.names.toName +import space.kscience.dataforge.names.Name import space.kscience.visionforge.solid.* import space.kscience.visionforge.three.server.* import space.kscience.visionforge.visionManager @@ -42,7 +42,7 @@ fun main() { val randomLayer = Random.nextInt(1, 11) val randomI = Random.nextInt(1, 4) val randomJ = Random.nextInt(1, 4) - val target = "layer[$randomLayer].segment[$randomI,$randomJ]".toName() + val target = Name.parse("layer[$randomLayer].segment[$randomI,$randomJ]") val targetVision = sat[target] as Solid targetVision.color("red") delay(1000) diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionLayout.kt b/demo/solid-showcase/src/commonMain/kotlin/space/kscience/visionforge/solid/demo/VisionLayout.kt similarity index 69% rename from visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionLayout.kt rename to demo/solid-showcase/src/commonMain/kotlin/space/kscience/visionforge/solid/demo/VisionLayout.kt index 1a0fddfc..016f2ecf 100644 --- a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionLayout.kt +++ b/demo/solid-showcase/src/commonMain/kotlin/space/kscience/visionforge/solid/demo/VisionLayout.kt @@ -1,7 +1,8 @@ -package space.kscience.visionforge +package space.kscience.visionforge.solid.demo import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.names.Name +import space.kscience.visionforge.Vision public interface VisionLayout { public fun render(name: Name, vision: V, meta: Meta = Meta.EMPTY) diff --git a/demo/solid-showcase/src/commonMain/kotlin/space/kscience/visionforge/solid/demo/demo.kt b/demo/solid-showcase/src/commonMain/kotlin/space/kscience/visionforge/solid/demo/demo.kt index 2385fef1..fcf94589 100644 --- a/demo/solid-showcase/src/commonMain/kotlin/space/kscience/visionforge/solid/demo/demo.kt +++ b/demo/solid-showcase/src/commonMain/kotlin/space/kscience/visionforge/solid/demo/demo.kt @@ -6,9 +6,8 @@ import kotlinx.coroutines.isActive import kotlinx.coroutines.launch import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.invoke -import space.kscience.dataforge.names.toName +import space.kscience.dataforge.names.Name import space.kscience.visionforge.Colors -import space.kscience.visionforge.VisionLayout import space.kscience.visionforge.solid.* import space.kscience.visionforge.solid.specifications.Canvas3DOptions import space.kscience.visionforge.visible @@ -23,7 +22,7 @@ fun VisionLayout.demo(name: String, title: String = name, block: SolidGro "title" put title } val vision = SolidGroup(block) - render(name.toName(), vision) + render(Name.parse(name), vision) } val canvasOptions = Canvas3DOptions { diff --git a/demo/solid-showcase/src/jsMain/kotlin/space/kscience/visionforge/solid/demo/ThreeDemoGrid.kt b/demo/solid-showcase/src/jsMain/kotlin/space/kscience/visionforge/solid/demo/ThreeDemoGrid.kt index bef5404f..6dcfa36f 100644 --- a/demo/solid-showcase/src/jsMain/kotlin/space/kscience/visionforge/solid/demo/ThreeDemoGrid.kt +++ b/demo/solid-showcase/src/jsMain/kotlin/space/kscience/visionforge/solid/demo/ThreeDemoGrid.kt @@ -15,7 +15,6 @@ import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.get import space.kscience.dataforge.meta.string import space.kscience.dataforge.names.Name -import space.kscience.visionforge.VisionLayout import space.kscience.visionforge.solid.Solid import space.kscience.visionforge.solid.three.ThreeCanvas import space.kscience.visionforge.solid.three.ThreePlugin diff --git a/demo/solid-showcase/src/jvmMain/kotlin/space/kscience/visionforge/solid/demo/FXDemoGrid.kt b/demo/solid-showcase/src/jvmMain/kotlin/space/kscience/visionforge/solid/demo/FXDemoGrid.kt index 4e727bb6..ef03092a 100644 --- a/demo/solid-showcase/src/jvmMain/kotlin/space/kscience/visionforge/solid/demo/FXDemoGrid.kt +++ b/demo/solid-showcase/src/jvmMain/kotlin/space/kscience/visionforge/solid/demo/FXDemoGrid.kt @@ -7,7 +7,6 @@ import space.kscience.dataforge.context.Global import space.kscience.dataforge.context.fetch import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.names.Name -import space.kscience.visionforge.VisionLayout import space.kscience.visionforge.solid.FX3DPlugin import space.kscience.visionforge.solid.FXCanvas3D import space.kscience.visionforge.solid.Solid diff --git a/demo/solid-showcase/src/jvmMain/kotlin/space/kscience/visionforge/solid/demo/MetaEditorDemo.kt b/demo/solid-showcase/src/jvmMain/kotlin/space/kscience/visionforge/solid/demo/MetaEditorDemo.kt index fc231c38..b00ef055 100644 --- a/demo/solid-showcase/src/jvmMain/kotlin/space/kscience/visionforge/solid/demo/MetaEditorDemo.kt +++ b/demo/solid-showcase/src/jvmMain/kotlin/space/kscience/visionforge/solid/demo/MetaEditorDemo.kt @@ -2,12 +2,14 @@ package space.kscience.visionforge.demo import javafx.geometry.Orientation import space.kscience.dataforge.meta.Meta -import space.kscience.dataforge.meta.asConfig -import space.kscience.dataforge.meta.descriptors.NodeDescriptor +import space.kscience.dataforge.meta.MutableMeta +import space.kscience.dataforge.meta.descriptors.MetaDescriptor +import space.kscience.dataforge.meta.descriptors.node +import space.kscience.dataforge.meta.descriptors.value import space.kscience.dataforge.values.ValueType -import space.kscience.visionforge.editor.ConfigEditor -import space.kscience.visionforge.editor.FXMeta +import space.kscience.visionforge.editor.FXMetaModel import space.kscience.visionforge.editor.MetaViewer +import space.kscience.visionforge.editor.MutableMetaEditor import tornadofx.* @@ -15,7 +17,7 @@ class MetaEditorDemoApp : App(MetaEditorDemo::class) class MetaEditorDemo : View("Meta editor demo") { - val meta = Meta { + val meta = MutableMeta { "aNode" put { "innerNode" put { "innerValue" put true @@ -23,18 +25,16 @@ class MetaEditorDemo : View("Meta editor demo") { "b" put 223 "c" put "StringValue" } - }.asConfig() + } - val descriptor = NodeDescriptor { + val descriptor = MetaDescriptor { node("aNode") { info = "A root demo node" - value("b") { + value("b", ValueType.NUMBER) { info = "b number value" - type(ValueType.NUMBER) } node("otherNode") { - value("otherValue") { - type(ValueType.BOOLEAN) + value("otherValue", ValueType.BOOLEAN) { default(false) info = "default value" } @@ -46,12 +46,13 @@ class MetaEditorDemo : View("Meta editor demo") { } } - private val rootNode = FXMeta.root(meta, descriptor) + private val rootNode = FXMetaModel.root(meta, descriptor) - override val root = - splitpane(Orientation.HORIZONTAL, MetaViewer(rootNode).root, ConfigEditor( - rootNode - ).root) + override val root = splitpane( + Orientation.HORIZONTAL, + MetaViewer(rootNode as Meta).root, + MutableMetaEditor(rootNode as FXMetaModel).root + ) } fun main() { diff --git a/docs/design.md b/docs/design.md index 79014d73..c6b1b64b 100644 --- a/docs/design.md +++ b/docs/design.md @@ -1,5 +1,5 @@ ## Library design -The central point of the library design is the `Vision` interface. The `Vision` stores an optional reference to its parent and is able to store a number of mutable or read-only properties. Each property is represented by its `Name`, and a `MetaItem` value-tree, both following DataForge library specification (discussed in the [Appendix](appendix.md)). The `Vision` objects are organized in a tree using `VisionGroup` as nodes. `VisionGroup` additionally to all `Vision` properties holds a `children` container that holds named references to its direct children `Vision`s. Thus, `Vision`s form a doubly linked tree (a parent stores references to all its children and children store a reference to the parent). +The central point of the library design is the `Vision` interface. The `Vision` stores an optional reference to its parent and is able to store a number of mutable or read-only properties. Each property is represented by its `Name`, and a `Meta` value-tree, both following DataForge library specification (discussed in the [Appendix](appendix.md)). The `Vision` objects are organized in a tree using `VisionGroup` as nodes. `VisionGroup` additionally to all `Vision` properties holds a `children` container that holds named references to its direct children `Vision`s. Thus, `Vision`s form a doubly linked tree (a parent stores references to all its children and children store a reference to the parent). An important concept using in the VisionForge is the property layering mechanism. It means that if the property with a given name is not found in the `Vision` it is requested from, it could be requested from the parent `Vision`, form the style declaration, the prototype for the vision or any other place defined by the component author. For example, let's take a `color` attribute used in 3D visualization. When one draws a group of objects, he usually wants to make the color of all objects in the group to be defined by a single handle in the group common ancestor. So when the parent color changes, all children color must follow suite, but we also want to change children color individually without changing the parent. In this case two property layers are defined: diff --git a/docs/uml/Vision.puml b/docs/uml/Vision.puml index 2028b097..079ca82c 100644 --- a/docs/uml/Vision.puml +++ b/docs/uml/Vision.puml @@ -3,7 +3,7 @@ interface Vision{ val parent: Vision? - fun getProperty(name): MetaItem? + fun getProperty(name): Meta? fun setProperty(name, value) } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index f371643e..05679dc3 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.1.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/settings.gradle.kts b/settings.gradle.kts index 70f3606a..d88c9b81 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,6 +1,6 @@ pluginManagement { - val toolsVersion = "0.10.0" + val toolsVersion = "0.10.2" repositories { maven("https://repo.kotlin.link") diff --git a/ui/bootstrap/build.gradle.kts b/ui/bootstrap/build.gradle.kts index b1b0588e..af4ec727 100644 --- a/ui/bootstrap/build.gradle.kts +++ b/ui/bootstrap/build.gradle.kts @@ -11,5 +11,5 @@ dependencies { implementation(npm("file-saver", "2.0.2")) implementation(npm("bootstrap","4.6.0")) implementation(npm("jquery","3.5.1")) - implementation(npm("popper.js","1.16.1")) + implementation(npm("@popperjs/core","2.9.3")) } \ No newline at end of file diff --git a/ui/bootstrap/src/main/kotlin/space/kscience/visionforge/bootstrap/outputConfig.kt b/ui/bootstrap/src/main/kotlin/space/kscience/visionforge/bootstrap/outputConfig.kt index 3423baf8..f16985c5 100644 --- a/ui/bootstrap/src/main/kotlin/space/kscience/visionforge/bootstrap/outputConfig.kt +++ b/ui/bootstrap/src/main/kotlin/space/kscience/visionforge/bootstrap/outputConfig.kt @@ -12,6 +12,7 @@ import org.w3c.files.BlobPropertyBag import react.* import react.dom.attrs import react.dom.button +import space.kscience.dataforge.meta.descriptors.defaultNode import space.kscience.dataforge.meta.withDefault import space.kscience.visionforge.Vision import space.kscience.visionforge.react.flexColumn @@ -43,7 +44,7 @@ public external interface CanvasControlsProps : RProps { public var vision: Vision? } -public val CanvasControls: FunctionalComponent = functionalComponent("CanvasControls") { props -> +public val CanvasControls: FunctionComponent = functionalComponent("CanvasControls") { props -> flexColumn { flexRow { css { @@ -66,7 +67,7 @@ public val CanvasControls: FunctionalComponent = functional } propertyEditor( ownProperties = props.canvasOptions, - allProperties = props.canvasOptions.withDefault(Canvas3DOptions.descriptor.defaultMeta), + allProperties = props.canvasOptions.meta.withDefault(Canvas3DOptions.descriptor.defaultNode), descriptor = Canvas3DOptions.descriptor, expanded = false ) diff --git a/ui/bootstrap/src/main/kotlin/space/kscience/visionforge/bootstrap/tabComponent.kt b/ui/bootstrap/src/main/kotlin/space/kscience/visionforge/bootstrap/tabComponent.kt index 5ff0d5e8..987dbc0e 100644 --- a/ui/bootstrap/src/main/kotlin/space/kscience/visionforge/bootstrap/tabComponent.kt +++ b/ui/bootstrap/src/main/kotlin/space/kscience/visionforge/bootstrap/tabComponent.kt @@ -18,7 +18,7 @@ public external class TabProps : RProps { } @JsExport -public val Tab: FunctionalComponent = functionalComponent { props -> +public val Tab: FunctionComponent = functionalComponent { props -> props.children() } @@ -27,7 +27,7 @@ public external class TabPaneProps : RProps { } @JsExport -public val TabPane: FunctionalComponent = functionalComponent("TabPane") { props -> +public val TabPane: FunctionComponent = functionalComponent("TabPane") { props -> var activeTab: String? by useState(props.activeTab) val children: Array = Children.map(props.children) { diff --git a/ui/bootstrap/src/main/kotlin/space/kscience/visionforge/bootstrap/visionPropertyEditor.kt b/ui/bootstrap/src/main/kotlin/space/kscience/visionforge/bootstrap/visionPropertyEditor.kt index 7f28473e..9c2206f1 100644 --- a/ui/bootstrap/src/main/kotlin/space/kscience/visionforge/bootstrap/visionPropertyEditor.kt +++ b/ui/bootstrap/src/main/kotlin/space/kscience/visionforge/bootstrap/visionPropertyEditor.kt @@ -3,22 +3,25 @@ package space.kscience.visionforge.bootstrap import org.w3c.dom.Element import react.RBuilder import react.dom.render -import space.kscience.dataforge.meta.descriptors.NodeDescriptor -import space.kscience.visionforge.* +import space.kscience.dataforge.meta.descriptors.MetaDescriptor +import space.kscience.visionforge.Vision +import space.kscience.visionforge.getStyle +import space.kscience.visionforge.meta import space.kscience.visionforge.react.metaViewer import space.kscience.visionforge.react.propertyEditor import space.kscience.visionforge.solid.SolidReference +import space.kscience.visionforge.styles public fun RBuilder.visionPropertyEditor( vision: Vision, - descriptor: NodeDescriptor? = vision.descriptor, + descriptor: MetaDescriptor? = vision.descriptor, key: Any? = null, ) { card("Properties") { propertyEditor( - ownProperties = vision.ownProperties, - allProperties = vision.allProperties(), + ownProperties = vision.meta(false,false,false), + allProperties = vision.meta(), updateFlow = vision.propertyChanges, descriptor = descriptor, key = key @@ -47,7 +50,7 @@ public fun RBuilder.visionPropertyEditor( public fun Element.visionPropertyEditor( item: Vision, - descriptor: NodeDescriptor? = item.descriptor, + descriptor: MetaDescriptor? = item.descriptor, ): Unit = render(this) { visionPropertyEditor(item, descriptor = descriptor) } \ No newline at end of file diff --git a/ui/react/src/main/kotlin/space/kscience/visionforge/react/MetaViewer.kt b/ui/react/src/main/kotlin/space/kscience/visionforge/react/MetaViewer.kt index a3972612..288251e0 100644 --- a/ui/react/src/main/kotlin/space/kscience/visionforge/react/MetaViewer.kt +++ b/ui/react/src/main/kotlin/space/kscience/visionforge/react/MetaViewer.kt @@ -8,12 +8,11 @@ import react.* import react.dom.a import react.dom.attrs import space.kscience.dataforge.meta.Meta -import space.kscience.dataforge.meta.MetaItemNode -import space.kscience.dataforge.meta.MetaItemValue -import space.kscience.dataforge.meta.descriptors.ItemDescriptor -import space.kscience.dataforge.meta.descriptors.NodeDescriptor +import space.kscience.dataforge.meta.descriptors.MetaDescriptor +import space.kscience.dataforge.meta.descriptors.defaultNode import space.kscience.dataforge.meta.descriptors.get import space.kscience.dataforge.meta.get +import space.kscience.dataforge.meta.isLeaf import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.NameToken import space.kscience.dataforge.names.lastOrNull @@ -41,18 +40,19 @@ public external interface MetaViewerProps : RProps { /** * Root descriptor */ - public var descriptor: NodeDescriptor? + public var descriptor: MetaDescriptor? } -private val MetaViewerItem: FunctionalComponent = functionalComponent("MetaViewerItem") { props -> +private val MetaViewerItem: FunctionComponent = functionalComponent("MetaViewerItem") { props -> metaViewerItem(props) } private fun RBuilder.metaViewerItem(props: MetaViewerProps) { var expanded: Boolean by useState { true } val item = props.root[props.name] - val descriptorItem: ItemDescriptor? = props.descriptor?.get(props.name) - val actualItem = item ?: descriptorItem?.defaultValue + val descriptorItem: MetaDescriptor? = props.descriptor?.get(props.name) + val actualValue = item?.value ?: descriptorItem?.defaultValue + val actualMeta = item ?: descriptorItem?.defaultNode val token = props.name.lastOrNull()?.toString() ?: props.rootName ?: "" @@ -60,90 +60,75 @@ private fun RBuilder.metaViewerItem(props: MetaViewerProps) { expanded = !expanded } - when (actualItem) { - is MetaItemNode -> { - flexRow { + flexRow { + css { + alignItems = Align.center + } + if (actualMeta?.isLeaf == false) { + styledSpan { css { - alignItems = Align.center - } - styledSpan { - css { - +TreeStyles.treeCaret - if (expanded) { - +TreeStyles.treeCaredDown - } - } - attrs { - onClickFunction = expanderClick + +TreeStyles.treeCaret + if (expanded) { + +TreeStyles.treeCaredDown } } - styledSpan { - css { - +TreeStyles.treeLabel - if (item == null) { - +TreeStyles.treeLabelInactive - } - } - +token - } - } - if (expanded) { - flexColumn { - css { - +TreeStyles.tree - } - val keys = buildSet { - (descriptorItem as? NodeDescriptor)?.items?.keys?.forEach { - add(NameToken(it)) - } - actualItem.node.items.keys.let { addAll(it) } - } - - keys.filter { !it.body.startsWith("@") }.forEach { token -> - styledDiv { - css { - +TreeStyles.treeItem - } - child(MetaViewerItem) { - attrs { - this.key = props.name.toString() - this.root = props.root - this.name = props.name + token - this.descriptor = props.descriptor - } - } - //configEditor(props.root, props.name + token, props.descriptor, props.default) - } - } + attrs { + onClickFunction = expanderClick } } } - is MetaItemValue -> { - flexRow { - css { - alignItems = Align.center + + styledSpan { + css { + +TreeStyles.treeLabel + if (item == null) { + +TreeStyles.treeLabelInactive } - styledSpan { + } + +token + } + styledDiv { + a { + +actualValue.toString() + } + } + } + if (expanded) { + flexColumn { + css { + +TreeStyles.tree + } + val keys = buildSet { + descriptorItem?.children?.keys?.forEach { + add(NameToken(it)) + } + actualMeta!!.items.keys.let { addAll(it) } + } + + keys.filter { !it.body.startsWith("@") }.forEach { token -> + styledDiv { css { - +TreeStyles.treeLabel - if (item == null) { - +TreeStyles.treeLabelInactive + +TreeStyles.treeItem + } + child(MetaViewerItem) { + attrs { + this.key = props.name.toString() + this.root = props.root + this.name = props.name + token + this.descriptor = props.descriptor } } - +token - } - styledDiv { - a { - +actualItem.value.toString() - } + //configEditor(props.root, props.name + token, props.descriptor, props.default) } } } } + + } @JsExport -public val MetaViewer: FunctionalComponent = +public val MetaViewer: FunctionComponent = functionalComponent("MetaViewer") { props -> child(MetaViewerItem) { attrs { @@ -155,7 +140,7 @@ public val MetaViewer: FunctionalComponent = } } -public fun RBuilder.metaViewer(meta: Meta, descriptor: NodeDescriptor? = null, key: Any? = null) { +public fun RBuilder.metaViewer(meta: Meta, descriptor: MetaDescriptor? = null, key: Any? = null) { child(MetaViewer) { attrs { this.key = key?.toString() ?: "" diff --git a/ui/react/src/main/kotlin/space/kscience/visionforge/react/MultiSelectChooser.kt b/ui/react/src/main/kotlin/space/kscience/visionforge/react/MultiSelectChooser.kt index a363fcd5..9f698df7 100644 --- a/ui/react/src/main/kotlin/space/kscience/visionforge/react/MultiSelectChooser.kt +++ b/ui/react/src/main/kotlin/space/kscience/visionforge/react/MultiSelectChooser.kt @@ -5,23 +5,23 @@ import org.w3c.dom.HTMLOptionElement import org.w3c.dom.HTMLSelectElement import org.w3c.dom.asList import org.w3c.dom.events.Event -import react.FunctionalComponent +import react.FunctionComponent import react.dom.attrs import react.dom.option import react.dom.select import react.functionalComponent import react.useState -import space.kscience.dataforge.meta.value +import space.kscience.dataforge.meta.descriptors.allowedValues import space.kscience.dataforge.values.asValue import space.kscience.dataforge.values.string @JsExport -public val MultiSelectChooser: FunctionalComponent = +public val MultiSelectChooser: FunctionComponent = functionalComponent("MultiSelectChooser") { props -> - var selectedItems by useState { props.item.value?.list ?: emptyList() } + var selectedItems by useState { props.item?.value?.list ?: emptyList() } val onChange: (Event) -> Unit = { event: Event -> - val newSelected= (event.target as HTMLSelectElement).selectedOptions.asList() + val newSelected = (event.target as HTMLSelectElement).selectedOptions.asList() .map { (it as HTMLOptionElement).value.asValue() } props.valueChanged?.invoke(newSelected.asValue()) selectedItems = newSelected diff --git a/ui/react/src/main/kotlin/space/kscience/visionforge/react/PropertyEditor.kt b/ui/react/src/main/kotlin/space/kscience/visionforge/react/PropertyEditor.kt index 6e41555b..cdb1bf30 100644 --- a/ui/react/src/main/kotlin/space/kscience/visionforge/react/PropertyEditor.kt +++ b/ui/react/src/main/kotlin/space/kscience/visionforge/react/PropertyEditor.kt @@ -18,14 +18,10 @@ import react.* import react.dom.attrs import react.dom.render import space.kscience.dataforge.meta.* -import space.kscience.dataforge.meta.descriptors.ItemDescriptor -import space.kscience.dataforge.meta.descriptors.NodeDescriptor -import space.kscience.dataforge.meta.descriptors.ValueDescriptor +import space.kscience.dataforge.meta.descriptors.MetaDescriptor +import space.kscience.dataforge.meta.descriptors.ValueRequirement import space.kscience.dataforge.meta.descriptors.get -import space.kscience.dataforge.names.Name -import space.kscience.dataforge.names.NameToken -import space.kscience.dataforge.names.lastOrNull -import space.kscience.dataforge.names.plus +import space.kscience.dataforge.names.* import space.kscience.dataforge.values.Value import space.kscience.visionforge.hidden import styled.css @@ -38,12 +34,12 @@ public external interface PropertyEditorProps : RProps { /** * Root config object - always non null */ - public var ownProperties: MutableItemProvider + public var ownProperties: MutableMetaProvider /** * Provide default item (greyed out if used) */ - public var allProperties: ItemProvider? + public var allProperties: MetaProvider? /** * Full path to the displayed node in [ownProperties]. Could be empty @@ -53,7 +49,7 @@ public external interface PropertyEditorProps : RProps { /** * Root descriptor */ - public var descriptor: NodeDescriptor? + public var descriptor: MetaDescriptor? /** * A coroutine scope for updates @@ -71,21 +67,21 @@ public external interface PropertyEditorProps : RProps { public var expanded: Boolean? } -private val PropertyEditorItem: FunctionalComponent = +private val PropertyEditorItem: FunctionComponent = functionalComponent("ConfigEditorItem") { props -> propertyEditorItem(props) } private fun RBuilder.propertyEditorItem(props: PropertyEditorProps) { var expanded: Boolean by useState { props.expanded ?: true } - val descriptorItem: ItemDescriptor? = props.descriptor?.get(props.name) - var ownProperty: MetaItem? by useState { props.ownProperties.getItem(props.name) } - val actualItem: MetaItem? = props.allProperties?.getItem(props.name) + val descriptor: MetaDescriptor? = props.descriptor?.get(props.name) + var ownProperty: Meta? by useState { props.ownProperties.getMeta(props.name) } + val actualMeta = props.allProperties?.getMeta(props.name) val token = props.name.lastOrNull()?.toString() ?: "Properties" fun update() { - ownProperty = props.ownProperties.getItem(props.name) + ownProperty = props.ownProperties.getMeta(props.name) } if (props.updateFlow != null) { @@ -109,7 +105,7 @@ private fun RBuilder.propertyEditorItem(props: PropertyEditorProps) { if (it == null) { props.ownProperties.remove(props.name) } else { - props.ownProperties[props.name] = it + props.ownProperties.setValue(props.name, it) } update() } @@ -119,19 +115,20 @@ private fun RBuilder.propertyEditorItem(props: PropertyEditorProps) { update() } - if (actualItem is MetaItemNode) { - val keys = buildSet { - (descriptorItem as? NodeDescriptor)?.items?.filterNot { - it.key.startsWith("@") || it.value.hidden - }?.forEach { - add(NameToken(it.key)) - } - ownProperty?.node?.items?.keys?.filterNot { it.body.startsWith("@") }?.let { addAll(it) } + val keys = buildSet { + descriptor?.children?.filterNot { + it.key.startsWith("@") || it.value.hidden + }?.forEach { + add(NameToken(it.key)) } - // Do not show nodes without visible children - if (keys.isEmpty()) return + ownProperty?.items?.keys?.filterNot { it.body.startsWith("@") }?.let { addAll(it) } + } - flexRow { + flexRow { + css { + alignItems = Align.center + } + if(keys.isNotEmpty()) { styledSpan { css { +TreeStyles.treeCaret @@ -143,55 +140,17 @@ private fun RBuilder.propertyEditorItem(props: PropertyEditorProps) { onClickFunction = expanderClick } } - styledSpan { - css { - +TreeStyles.treeLabel - if (ownProperty == null) { - +TreeStyles.treeLabelInactive - } - } - +token - } } - if (expanded) { - flexColumn { - css { - +TreeStyles.tree - } - keys.forEach { token -> - styledDiv { - css { - +TreeStyles.treeItem - } - child(PropertyEditorItem) { - attrs { - this.key = props.name.toString() - this.ownProperties = props.ownProperties - this.allProperties = props.allProperties - this.name = props.name + token - this.descriptor = props.descriptor - } - } - //configEditor(props.root, props.name + token, props.descriptor, props.default) - } - } - } - } - } else { - flexRow { + styledSpan { css { - alignItems = Align.center - } - styledSpan { - css { - +TreeStyles.treeLabel - if (ownProperty == null) { - +TreeStyles.treeLabelInactive - } + +TreeStyles.treeLabel + if (ownProperty == null) { + +TreeStyles.treeLabelInactive } - +token } - + +token + } + if(!props.name.isEmpty() && descriptor?.valueRequirement != ValueRequirement.ABSENT) { styledDiv { css { //+TreeStyles.resizeableInput @@ -200,8 +159,8 @@ private fun RBuilder.propertyEditorItem(props: PropertyEditorProps) { } valueChooser( props.name, - actualItem, - descriptorItem as? ValueDescriptor, + actualMeta, + descriptor, valueChanged ) } @@ -232,14 +191,38 @@ private fun RBuilder.propertyEditorItem(props: PropertyEditorProps) { } } } - } } + if (expanded) { + flexColumn { + css { + +TreeStyles.tree + } + keys.forEach { token -> + styledDiv { + css { + +TreeStyles.treeItem + } + child(PropertyEditorItem) { + attrs { + this.key = props.name.toString() + this.ownProperties = props.ownProperties + this.allProperties = props.allProperties + this.name = props.name + token + this.descriptor = props.descriptor + } + } + //configEditor(props.root, props.name + token, props.descriptor, props.default) + } + } + } + } + } @JsExport -public val PropertyEditor: FunctionalComponent = functionalComponent("PropertyEditor") { props -> +public val PropertyEditor: FunctionComponent = functionalComponent("PropertyEditor") { props -> child(PropertyEditorItem) { attrs { this.key = "" @@ -254,10 +237,10 @@ public val PropertyEditor: FunctionalComponent = functional } public fun RBuilder.propertyEditor( - ownProperties: MutableItemProvider, - allProperties: ItemProvider? = ownProperties, + ownProperties: MutableMetaProvider, + allProperties: MetaProvider? = ownProperties, updateFlow: Flow? = null, - descriptor: NodeDescriptor? = null, + descriptor: MetaDescriptor? = null, scope: CoroutineScope? = null, key: Any? = null, expanded: Boolean? = null @@ -276,8 +259,8 @@ public fun RBuilder.propertyEditor( } @OptIn(ExperimentalCoroutinesApi::class) -private fun Config.flowUpdates(): Flow = callbackFlow { - onChange(this) { name, _, _ -> +private fun ObservableMutableMeta.flowUpdates(): Flow = callbackFlow { + onChange(this) { name -> launch { send(name) } @@ -289,16 +272,16 @@ private fun Config.flowUpdates(): Flow = callbackFlow { public fun RBuilder.configEditor( - config: Config, - default: ItemProvider? = null, - descriptor: NodeDescriptor? = null, + config: ObservableMutableMeta, + default: MetaProvider? = null, + descriptor: MetaDescriptor? = null, key: Any? = null, scope: CoroutineScope? = null, ): Unit = propertyEditor(config, default, config.flowUpdates(), descriptor, scope, key = key) public fun Element.configEditor( - config: Config, - descriptor: NodeDescriptor? = null, + config: ObservableMutableMeta, + descriptor: MetaDescriptor? = null, default: Meta? = null, key: Any? = null, scope: CoroutineScope? = null, diff --git a/ui/react/src/main/kotlin/space/kscience/visionforge/react/RangeValueChooser.kt b/ui/react/src/main/kotlin/space/kscience/visionforge/react/RangeValueChooser.kt index 2b11143b..28513416 100644 --- a/ui/react/src/main/kotlin/space/kscience/visionforge/react/RangeValueChooser.kt +++ b/ui/react/src/main/kotlin/space/kscience/visionforge/react/RangeValueChooser.kt @@ -6,7 +6,7 @@ import kotlinx.html.InputType import kotlinx.html.js.onChangeFunction import org.w3c.dom.HTMLInputElement import org.w3c.dom.events.Event -import react.FunctionalComponent +import react.FunctionComponent import react.dom.attrs import react.functionalComponent import react.useState @@ -18,7 +18,7 @@ import styled.css import styled.styledInput @JsExport -public val RangeValueChooser: FunctionalComponent = +public val RangeValueChooser: FunctionComponent = functionalComponent("RangeValueChooser") { props -> var innerValue by useState(props.item.double) var rangeDisabled: Boolean by useState(props.item == null) diff --git a/ui/react/src/main/kotlin/space/kscience/visionforge/react/ThreeCanvasComponent.kt b/ui/react/src/main/kotlin/space/kscience/visionforge/react/ThreeCanvasComponent.kt index 6dd563de..41034773 100644 --- a/ui/react/src/main/kotlin/space/kscience/visionforge/react/ThreeCanvasComponent.kt +++ b/ui/react/src/main/kotlin/space/kscience/visionforge/react/ThreeCanvasComponent.kt @@ -21,7 +21,7 @@ public external interface ThreeCanvasProps : RProps { public var selected: Name? } -public val ThreeCanvasComponent: FunctionalComponent = functionalComponent( +public val ThreeCanvasComponent: FunctionComponent = functionalComponent( "ThreeCanvasComponent" ) { props -> val elementRef = useRef(null) diff --git a/ui/react/src/main/kotlin/space/kscience/visionforge/react/VisionTree.kt b/ui/react/src/main/kotlin/space/kscience/visionforge/react/VisionTree.kt index bcb7e84a..6be045d6 100644 --- a/ui/react/src/main/kotlin/space/kscience/visionforge/react/VisionTree.kt +++ b/ui/react/src/main/kotlin/space/kscience/visionforge/react/VisionTree.kt @@ -107,7 +107,7 @@ private fun RBuilder.visionTree(props: ObjectTreeProps): Unit { } @JsExport -public val ObjectTree: FunctionalComponent = functionalComponent("ObjectTree") { props -> +public val ObjectTree: FunctionComponent = functionalComponent("ObjectTree") { props -> visionTree(props) } diff --git a/ui/react/src/main/kotlin/space/kscience/visionforge/react/valueChooser.kt b/ui/react/src/main/kotlin/space/kscience/visionforge/react/valueChooser.kt index 2147b191..2cfcc976 100644 --- a/ui/react/src/main/kotlin/space/kscience/visionforge/react/valueChooser.kt +++ b/ui/react/src/main/kotlin/space/kscience/visionforge/react/valueChooser.kt @@ -13,8 +13,12 @@ import org.w3c.dom.events.Event import react.* import react.dom.attrs import react.dom.option -import space.kscience.dataforge.meta.* -import space.kscience.dataforge.meta.descriptors.ValueDescriptor +import space.kscience.dataforge.meta.Meta +import space.kscience.dataforge.meta.boolean +import space.kscience.dataforge.meta.descriptors.MetaDescriptor +import space.kscience.dataforge.meta.descriptors.allowedValues +import space.kscience.dataforge.meta.get +import space.kscience.dataforge.meta.string import space.kscience.dataforge.names.Name import space.kscience.dataforge.values.* import space.kscience.visionforge.Colors @@ -24,14 +28,15 @@ import styled.styledInput import styled.styledSelect public external interface ValueChooserProps : RProps { - public var item: MetaItem? - public var descriptor: ValueDescriptor? + public var item: Meta? + public var descriptor: MetaDescriptor? + //public var nullable: Boolean? public var valueChanged: ((Value?) -> Unit)? } @JsExport -public val StringValueChooser: FunctionalComponent = +public val StringValueChooser: FunctionComponent = functionalComponent("StringValueChooser") { props -> var value by useState(props.item.string ?: "") val keyDown: (Event) -> Unit = { event -> @@ -46,7 +51,7 @@ public val StringValueChooser: FunctionalComponent = value = (it.target as HTMLInputElement).value } styledInput(type = InputType.text) { - css{ + css { width = 100.pct } attrs { @@ -58,14 +63,14 @@ public val StringValueChooser: FunctionalComponent = } @JsExport -public val BooleanValueChooser: FunctionalComponent = +public val BooleanValueChooser: FunctionComponent = functionalComponent("BooleanValueChooser") { props -> val handleChange: (Event) -> Unit = { val newValue = (it.target as HTMLInputElement).checked props.valueChanged?.invoke(newValue.asValue()) } styledInput(type = InputType.checkBox) { - css{ + css { width = 100.pct } attrs { @@ -77,7 +82,7 @@ public val BooleanValueChooser: FunctionalComponent = } @JsExport -public val NumberValueChooser: FunctionalComponent = +public val NumberValueChooser: FunctionComponent = functionalComponent("NumberValueChooser") { props -> var innerValue by useState(props.item.string ?: "") val keyDown: (Event) -> Unit = { event -> @@ -95,7 +100,7 @@ public val NumberValueChooser: FunctionalComponent = innerValue = (it.target as HTMLInputElement).value } styledInput(type = InputType.number) { - css{ + css { width = 100.pct } attrs { @@ -116,7 +121,7 @@ public val NumberValueChooser: FunctionalComponent = } @JsExport -public val ComboValueChooser: FunctionalComponent = +public val ComboValueChooser: FunctionComponent = functionalComponent("ComboValueChooser") { props -> var selected by useState(props.item.string ?: "") val handleChange: (Event) -> Unit = { @@ -124,7 +129,7 @@ public val ComboValueChooser: FunctionalComponent = props.valueChanged?.invoke(selected.asValue()) } styledSelect { - css{ + css { width = 100.pct } props.descriptor?.allowedValues?.forEach { @@ -141,10 +146,10 @@ public val ComboValueChooser: FunctionalComponent = } @JsExport -public val ColorValueChooser: FunctionalComponent = +public val ColorValueChooser: FunctionComponent = functionalComponent("ColorValueChooser") { props -> var value by useState( - props.item.value?.let { value -> + props.item?.value?.let { value -> if (value.type == ValueType.NUMBER) Colors.rgbToString(value.int) else value.string } ?: "#000000" @@ -154,7 +159,7 @@ public val ColorValueChooser: FunctionalComponent = props.valueChanged?.invoke(value.asValue()) } styledInput(type = InputType.color) { - css{ + css { width = 100.pct margin(0.px) } @@ -166,11 +171,11 @@ public val ColorValueChooser: FunctionalComponent = } @JsExport -public val ValueChooser: FunctionalComponent = functionalComponent("ValueChooser") { props -> +public val ValueChooser: FunctionComponent = functionalComponent("ValueChooser") { props -> val rawInput by useState(false) val descriptor = props.descriptor - val type = descriptor?.type?.firstOrNull() + val type = descriptor?.valueTypes?.firstOrNull() when { rawInput -> child(StringValueChooser, props) @@ -187,8 +192,8 @@ public val ValueChooser: FunctionalComponent = functionalComp internal fun RBuilder.valueChooser( name: Name, - item: MetaItem?, - descriptor: ValueDescriptor? = null, + item: Meta?, + descriptor: MetaDescriptor? = null, callback: (Value?) -> Unit, ) { child(ValueChooser) { diff --git a/ui/ring/build.gradle.kts b/ui/ring/build.gradle.kts index 84d33b43..84b4112f 100644 --- a/ui/ring/build.gradle.kts +++ b/ui/ring/build.gradle.kts @@ -17,14 +17,13 @@ kotlin{ dependencies{ api(project(":ui:react")) - //TODO replace by kotlin-wrappers - api("ru.mipt.npm:ring-ui:0.1.0") + //api("ru.mipt.npm:ring-ui:0.1.0") + api("org.jetbrains.kotlin-wrappers:kotlin-ring-ui") implementation(npm("@jetbrains/icons", "3.14.1")) implementation(npm("@jetbrains/ring-ui", "4.0.7")) implementation(npm("core-js","3.12.1")) implementation(npm("file-saver", "2.0.2")) - compileOnly(npm("url-loader","4.1.1")) - compileOnly(npm("postcss-loader","5.2.0")) - compileOnly(npm("source-map-loader","2.0.1")) +// compileOnly(npm("url-loader","4.1.1")) +// compileOnly(npm("postcss-loader","5.2.0")) } \ 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 7b427fba..d0aa2ed8 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,8 +14,7 @@ import space.kscience.dataforge.names.NameToken import space.kscience.dataforge.names.isEmpty import space.kscience.dataforge.names.length import space.kscience.visionforge.VisionGroup -import space.kscience.visionforge.allProperties -import space.kscience.visionforge.ownProperties +import space.kscience.visionforge.meta import space.kscience.visionforge.react.ThreeCanvasComponent import space.kscience.visionforge.react.flexColumn import space.kscience.visionforge.react.flexRow @@ -70,7 +69,7 @@ public fun RBuilder.nameCrumbs(name: Name?, link: (Name) -> Unit): ReactElement } @JsExport -public val ThreeCanvasWithControls: FunctionalComponent = +public val ThreeCanvasWithControls: FunctionComponent = functionalComponent("ThreeViewWithControls") { props -> var selected by useState { props.selected } @@ -120,23 +119,23 @@ public val ThreeCanvasWithControls: FunctionalComponent styledDiv { - css{ + css { position = Position.absolute top = 5.px right = 5.px width = 450.px } - Island{ - IslandHeader{ + Island { + IslandHeader { attrs { border = true } nameCrumbs(selected) { selected = it } } - IslandContent{ + IslandContent { propertyEditor( - ownProperties = vision.ownProperties, - allProperties = vision.allProperties(), + ownProperties = vision.meta(false, false, false), + allProperties = vision.meta(), updateFlow = vision.propertyChanges, descriptor = vision.descriptor, key = selected diff --git a/ui/ring/src/main/kotlin/space.kscience.visionforge.ring/ringPropertyEditor.kt b/ui/ring/src/main/kotlin/space.kscience.visionforge.ring/ringPropertyEditor.kt index e33dfb2b..f7783898 100644 --- a/ui/ring/src/main/kotlin/space.kscience.visionforge.ring/ringPropertyEditor.kt +++ b/ui/ring/src/main/kotlin/space.kscience.visionforge.ring/ringPropertyEditor.kt @@ -7,16 +7,19 @@ import react.dom.render import ringui.Island import ringui.SmartTabs import ringui.Tab -import space.kscience.dataforge.meta.descriptors.NodeDescriptor -import space.kscience.visionforge.* +import space.kscience.dataforge.meta.descriptors.MetaDescriptor +import space.kscience.visionforge.Vision +import space.kscience.visionforge.getStyle +import space.kscience.visionforge.meta import space.kscience.visionforge.react.flexColumn import space.kscience.visionforge.react.metaViewer import space.kscience.visionforge.react.propertyEditor import space.kscience.visionforge.solid.SolidReference +import space.kscience.visionforge.styles public fun RBuilder.ringPropertyEditor( vision: Vision, - descriptor: NodeDescriptor? = vision.descriptor, + descriptor: MetaDescriptor? = vision.descriptor, key: Any? = null, ) { val styles = if (vision is SolidReference) { @@ -28,8 +31,8 @@ public fun RBuilder.ringPropertyEditor( flexColumn { Island("Properties") { propertyEditor( - ownProperties = vision.ownProperties, - allProperties = vision.allProperties(), + ownProperties = vision.meta(false,false,false), + allProperties = vision.meta(), updateFlow = vision.propertyChanges, descriptor = descriptor, key = key @@ -69,7 +72,7 @@ public fun RBuilder.ringPropertyEditor( public fun Element.ringPropertyEditor( item: Vision, - descriptor: NodeDescriptor? = item.descriptor, + descriptor: MetaDescriptor? = item.descriptor, ): Unit = render(this) { ringPropertyEditor(item, descriptor = descriptor) } \ No newline at end of file diff --git a/ui/ring/src/main/kotlin/space.kscience.visionforge.ring/ringThreeControls.kt b/ui/ring/src/main/kotlin/space.kscience.visionforge.ring/ringThreeControls.kt index b0fc188c..1d9626ed 100644 --- a/ui/ring/src/main/kotlin/space.kscience.visionforge.ring/ringThreeControls.kt +++ b/ui/ring/src/main/kotlin/space.kscience.visionforge.ring/ringThreeControls.kt @@ -15,6 +15,7 @@ import react.dom.button import ringui.Island import ringui.SmartTabs import ringui.Tab +import space.kscience.dataforge.meta.descriptors.defaultNode import space.kscience.dataforge.meta.withDefault import space.kscience.dataforge.names.Name import space.kscience.visionforge.Vision @@ -49,7 +50,7 @@ internal external interface CanvasControlsProps : RProps { public var vision: Vision? } -internal val CanvasControls: FunctionalComponent = functionalComponent("CanvasControls") { props -> +internal val CanvasControls: FunctionComponent = functionalComponent("CanvasControls") { props -> flexColumn { flexRow { css { @@ -73,7 +74,7 @@ internal val CanvasControls: FunctionalComponent = function } propertyEditor( ownProperties = props.options, - allProperties = props.options.withDefault(Canvas3DOptions.descriptor.defaultMeta), + allProperties = props.options.meta.withDefault(Canvas3DOptions.descriptor.defaultNode), descriptor = Canvas3DOptions.descriptor, expanded = false ) @@ -91,7 +92,7 @@ public external interface ThreeControlsProps : RProps { } @JsExport -public val ThreeControls: FunctionalComponent = functionalComponent { props -> +public val ThreeControls: FunctionComponent = functionalComponent { props -> SmartTabs("Tree") { props.vision?.let { Tab("Tree") { diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/Colors.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/Colors.kt index 3f228272..58be399e 100644 --- a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/Colors.kt +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/Colors.kt @@ -1,6 +1,8 @@ package space.kscience.visionforge -import space.kscience.dataforge.meta.* +import space.kscience.dataforge.meta.Meta +import space.kscience.dataforge.meta.get +import space.kscience.dataforge.meta.number import space.kscience.dataforge.values.ValueType import space.kscience.dataforge.values.int import space.kscience.dataforge.values.string @@ -190,25 +192,18 @@ public object Colors { /** * Convert color represented as Meta to string of format #rrggbb */ - fun fromMeta(item: MetaItem): String { - return when (item) { - is MetaItemNode -> { - val node = item.node - rgbToString( - node[RED_KEY].number?.toByte()?.toUByte() ?: 0u, - node[GREEN_KEY].number?.toByte()?.toUByte() ?: 0u, - node[BLUE_KEY].number?.toByte()?.toUByte() ?: 0u - ) - } - is MetaItemValue -> { - if (item.value.type == ValueType.NUMBER) { - rgbToString(item.value.int) - } else { - item.value.string - } - } + fun fromMeta(meta: Meta): String = meta.value?.let { value -> + //if value is present, use it + if (value.type == ValueType.NUMBER) { + rgbToString(value.int) + } else { + value.string } - } + } ?: rgbToString( + meta[RED_KEY].number?.toByte()?.toUByte() ?: 0u, + meta[GREEN_KEY].number?.toByte()?.toUByte() ?: 0u, + meta[BLUE_KEY].number?.toByte()?.toUByte() ?: 0u + ) /** * Convert Int color to string of format #rrggbb diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/StyleReference.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/StyleReference.kt index 961e937a..d5dfac6e 100644 --- a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/StyleReference.kt +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/StyleReference.kt @@ -1,7 +1,7 @@ package space.kscience.visionforge import space.kscience.dataforge.meta.Meta -import space.kscience.dataforge.meta.MetaBuilder +import space.kscience.dataforge.meta.MutableMeta import space.kscience.dataforge.meta.Scheme import space.kscience.dataforge.meta.Specification import kotlin.properties.ReadOnlyProperty @@ -27,7 +27,7 @@ public fun Vision.useStyle(reference: StyleReference) { @VisionBuilder public fun VisionGroup.style( styleKey: String? = null, - builder: MetaBuilder.() -> Unit, + builder: MutableMeta.() -> Unit, ): ReadOnlyProperty = ReadOnlyProperty { _, property -> val styleName = styleKey ?: property.name styleSheet.define(styleName, Meta(builder)) diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/StyleSheet.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/StyleSheet.kt index 655cb0f3..e2869df8 100644 --- a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/StyleSheet.kt +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/StyleSheet.kt @@ -5,6 +5,7 @@ import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.NameToken import space.kscience.dataforge.names.asName import space.kscience.dataforge.names.plus +import space.kscience.dataforge.values.asValue import kotlin.jvm.JvmInline /** @@ -13,9 +14,9 @@ import kotlin.jvm.JvmInline @JvmInline public value class StyleSheet(private val owner: VisionGroup) { - private val styleNode get() = owner.ownProperties[STYLESHEET_KEY].node + private val styleNode: Meta? get() = owner.getOwnProperty(STYLESHEET_KEY) - public val items: Map? get() = styleNode?.items?.mapValues { it.value.node ?: Meta.EMPTY } + public val items: Map? get() = styleNode?.items public operator fun get(key: String): Meta? = owner.getStyle(key) @@ -23,7 +24,7 @@ public value class StyleSheet(private val owner: VisionGroup) { * Define a style without notifying owner */ public fun define(key: String, style: Meta?) { - owner.setProperty(STYLESHEET_KEY + key, style) + owner.setPropertyNode(STYLESHEET_KEY + key, style) } /** @@ -40,7 +41,7 @@ public value class StyleSheet(private val owner: VisionGroup) { /** * Create and set a style */ - public operator fun set(key: String, builder: MetaBuilder.() -> Unit) { + public operator fun set(key: String, builder: MutableMeta.() -> Unit) { val newStyle = get(key)?.toMutableMeta()?.apply(builder) ?: Meta(builder) set(key, newStyle.seal()) } @@ -70,9 +71,9 @@ internal fun Vision.styleChanged(key: String, oldStyle: Meta?, newStyle: Meta?) * List of names of styles applied to this object. Order matters. Not inherited. */ public var Vision.styles: List - get() = ownProperties[Vision.STYLE_KEY]?.stringList ?: emptyList() + get() = getOwnProperty(Vision.STYLE_KEY)?.stringList ?: emptyList() set(value) { - setProperty(Vision.STYLE_KEY, value) + setPropertyValue(Vision.STYLE_KEY, value.map { it.asValue() }.asValue()) } /** @@ -85,7 +86,7 @@ public val VisionGroup.styleSheet: StyleSheet get() = StyleSheet(this) * Add style name to the list of styles to be resolved later. The style with given name does not necessary exist at the moment. */ public fun Vision.useStyle(name: String) { - styles = (ownProperties[Vision.STYLE_KEY]?.stringList ?: emptyList()) + name + styles = (getOwnProperty(Vision.STYLE_KEY)?.stringList ?: emptyList()) + name } @@ -93,13 +94,13 @@ public fun Vision.useStyle(name: String) { * Find a style with given name for given [Vision]. The style is not necessary applied to this [Vision]. */ public tailrec fun Vision.getStyle(name: String): Meta? = - ownProperties[StyleSheet.STYLESHEET_KEY + name].node ?: parent?.getStyle(name) + getOwnProperty(StyleSheet.STYLESHEET_KEY + name) ?: parent?.getStyle(name) /** * Resolve an item in all style layers */ -public fun Vision.getStyleItems(name: Name): List = styles.mapNotNull { - getStyle(it)[name] +public fun Vision.getStyleItems(name: Name): List = styles.mapNotNull { + getStyle(it)?.get(name) } diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/Vision.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/Vision.kt index 07f063ca..daa854a5 100644 --- a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/Vision.kt +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/Vision.kt @@ -4,14 +4,15 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach -import space.kscience.dataforge.meta.* +import space.kscience.dataforge.meta.Meta +import space.kscience.dataforge.meta.MutableMeta import space.kscience.dataforge.meta.descriptors.Described -import space.kscience.dataforge.meta.descriptors.NodeDescriptor -import space.kscience.dataforge.meta.descriptors.get +import space.kscience.dataforge.meta.descriptors.MetaDescriptor +import space.kscience.dataforge.meta.update import space.kscience.dataforge.misc.Type import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.asName -import space.kscience.dataforge.names.toName +import space.kscience.dataforge.values.Value import space.kscience.visionforge.Vision.Companion.TYPE import kotlin.coroutines.CoroutineContext import kotlin.coroutines.EmptyCoroutineContext @@ -45,13 +46,13 @@ public interface Vision : Described, CoroutineScope { inherit: Boolean = false, includeStyles: Boolean = true, includeDefaults: Boolean = true, - ): MetaItem? + ): Meta? /** * Get an intrinsic property of this Vision excluding any inheritance or defaults. In most cases should be the same as * `getProperty(name, false, false, false`. */ - public fun getOwnProperty(name: Name): MetaItem? = getProperty( + public fun getOwnProperty(name: Name): Meta? = getProperty( name, inherit = false, includeStyles = false, @@ -59,9 +60,14 @@ public interface Vision : Described, CoroutineScope { ) /** - * Set the property value + * Replace the property node. If [node] is null remove node and its descendants */ - public fun setProperty(name: Name, item: MetaItem?, notify: Boolean = true) + public fun setPropertyNode(name: Name, node: Meta?, notify: Boolean = true) + + /** + * Set a value of specific property node + */ + public fun setPropertyValue(name: Name, value: Value?, notify: Boolean = true) /** * Flow of property invalidation events. It does not contain property values after invalidation since it is not clear @@ -77,9 +83,9 @@ public interface Vision : Described, CoroutineScope { /** * Update this vision using a dif represented by [VisionChange]. */ - public fun update(change: VisionChange) + public fun change(change: VisionChange) - override val descriptor: NodeDescriptor? + override val descriptor: MetaDescriptor? public companion object { public const val TYPE: String = "vision" @@ -89,11 +95,6 @@ public interface Vision : Described, CoroutineScope { } } -/** - * Root property node - */ -public val Vision.meta: Meta get() = ownProperties[Name.EMPTY]?.node ?: Meta.EMPTY - /** * Subscribe on property updates. The subscription is bound to the given [scope] and canceled when the scope is canceled */ @@ -105,31 +106,26 @@ public fun Vision.onPropertyChange(scope: CoroutineScope, callback: suspend (Nam /** * Own properties, excluding inheritance, styles and descriptor */ -public val Vision.ownProperties: MutableItemProvider - get() = object : MutableItemProvider { - override fun getItem(name: Name): MetaItem? = getOwnProperty(name) - override fun setItem(name: Name, item: MetaItem?): Unit = setProperty(name, item) - } - -/** - * Convenient accessor for all properties of a vision. - * @param inherit - inherit property value from the parent by default. If null, inheritance is inferred from descriptor - */ -public fun Vision.allProperties( - inherit: Boolean? = null, - includeStyles: Boolean? = null, +public fun Vision.meta( + inherit: Boolean = false, + includeStyles: Boolean = true, includeDefaults: Boolean = true, -): MutableItemProvider = object : MutableItemProvider { - override fun getItem(name: Name): MetaItem? = getProperty( - name, - inherit = inherit ?: (descriptor?.get(name)?.inherited == true), - includeStyles = includeStyles ?: (descriptor?.get(name)?.usesStyles != false), - includeDefaults = includeDefaults - ) +): MutableMeta = VisionProperties(this, Name.EMPTY, inherit, includeStyles, includeDefaults) - override fun setItem(name: Name, item: MetaItem?): Unit = setProperty(name, item) +public fun Vision.configure(target: Name = Name.EMPTY, block: MutableMeta.() -> Unit): Unit { + VisionProperties(this, target).apply(block) } +public fun Vision.configure(meta: Meta) { + configure(Name.EMPTY) { + update(meta) + } +} + +public fun Vision.configure(block: MutableMeta.() -> Unit): Unit = configure(Meta(block)) + +public fun Vision.getOwnProperty(key: String): Meta? = getOwnProperty(Name.parse(key)) + /** * Get [Vision] property using key as a String */ @@ -138,18 +134,20 @@ public fun Vision.getProperty( inherit: Boolean = false, includeStyles: Boolean = true, includeDefaults: Boolean = true, -): MetaItem? = getProperty(key.toName(), inherit, includeStyles, includeDefaults) +): Meta? = getProperty(Name.parse(key), inherit, includeStyles, includeDefaults) + /** - * A convenience method to pair [getProperty] + * A convenience method to set property node or value. If Item is null, then node is removed, not a value */ -public fun Vision.setProperty(key: Name, item: Any?) { - setProperty(key, MetaItem.of(item)) +public fun Vision.setProperty(name: Name, item: Any?) { + when (item) { + null -> setPropertyNode(name, null) + is Meta -> setPropertyNode(name, item) + is Value -> setPropertyValue(name, item) + } } -/** - * A convenience method to pair [getProperty] - */ -public fun Vision.setProperty(key: String, item: Any?) { - setProperty(key.toName(), MetaItem.of(item)) +public fun Vision.setPropertyNode(key: String, item: Any?) { + setProperty(Name.parse(key), item) } diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionBase.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionBase.kt index 4dcffbca..46e5f62a 100644 --- a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionBase.kt +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionBase.kt @@ -7,21 +7,18 @@ import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import kotlinx.serialization.Transient import space.kscience.dataforge.meta.* -import space.kscience.dataforge.meta.descriptors.NodeDescriptor +import space.kscience.dataforge.meta.descriptors.MetaDescriptor +import space.kscience.dataforge.meta.descriptors.defaultNode +import space.kscience.dataforge.meta.descriptors.value import space.kscience.dataforge.misc.DFExperimental import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.asName import space.kscience.dataforge.names.plus -import space.kscience.dataforge.values.Null +import space.kscience.dataforge.values.Value import space.kscience.dataforge.values.ValueType import space.kscience.visionforge.Vision.Companion.STYLE_KEY import kotlin.jvm.Synchronized -internal data class PropertyListener( - val owner: Any? = null, - val action: (name: Name) -> Unit, -) - /** * A full base implementation for a [Vision] * @param properties Object own properties excluding styles and inheritance @@ -29,14 +26,17 @@ internal data class PropertyListener( @Serializable @SerialName("vision") public open class VisionBase( - override @Transient var parent: VisionGroup? = null, - protected var properties: MutableItemProvider? = null + @Transient override var parent: VisionGroup? = null, + @Serializable(MutableMetaSerializer::class) + protected var properties: MutableMeta? = null ) : Vision { + //protected val observableProperties: ObservableMutableMeta by lazy { properties.asObservable() } + @Synchronized - protected fun getOrCreateProperties(): MutableItemProvider { + protected fun getOrCreateProperties(): MutableMeta { if (properties == null) { - val newProperties = MetaBuilder() + val newProperties = MutableMeta() properties = newProperties } return properties!! @@ -45,18 +45,14 @@ public open class VisionBase( /** * A fast accessor method to get own property (no inheritance or styles */ - override fun getOwnProperty(name: Name): MetaItem? = if (name == Name.EMPTY) { - properties?.rootItem - } else { - properties?.getItem(name) - } + override fun getOwnProperty(name: Name): Meta? = properties?.getMeta(name) override fun getProperty( name: Name, inherit: Boolean, includeStyles: Boolean, includeDefaults: Boolean, - ): MetaItem? = if (!inherit && !includeStyles && !includeDefaults) { + ): Meta? = if (!inherit && !includeStyles && !includeDefaults) { getOwnProperty(name) } else { buildList { @@ -68,22 +64,32 @@ public open class VisionBase( add(parent?.getProperty(name, inherit, includeStyles, includeDefaults)) } if (includeDefaults) { - add(descriptor?.defaultMeta?.get(name)) + add(descriptor?.defaultNode?.get(name)) } }.merge() } - override fun setProperty(name: Name, item: MetaItem?, notify: Boolean) { - val oldItem = properties?.getItem(name) - if(oldItem!= item) { - getOrCreateProperties().setItem(name, item) + override fun setPropertyNode(name: Name, node: Meta?, notify: Boolean) { + val oldItem = properties?.get(name) + if (oldItem != node) { + getOrCreateProperties().setMeta(name, node) if (notify) { invalidateProperty(name) } } } - override val descriptor: NodeDescriptor? get() = null + override fun setPropertyValue(name: Name, value: Value?, notify: Boolean) { + val oldItem = properties?.get(name)?.value + if (oldItem != value) { + getOrCreateProperties()[name] = value + if (notify) { + invalidateProperty(name) + } + } + } + + override val descriptor: MetaDescriptor? get() = null private suspend fun updateStyles(names: List) { names.mapNotNull { getStyle(it) }.asSequence() @@ -111,31 +117,23 @@ public open class VisionBase( } } - override fun update(change: VisionChange) { + override fun change(change: VisionChange) { change.properties?.let { - updateProperties(Name.EMPTY, it.asMetaItem()) + updateProperties(Name.EMPTY, it) } } public companion object { - public val descriptor: NodeDescriptor = NodeDescriptor { - value(STYLE_KEY) { - type(ValueType.STRING) + public val descriptor: MetaDescriptor = MetaDescriptor { + value(STYLE_KEY, ValueType.STRING) { multiple = true } } - public fun Vision.updateProperties(at: Name, item: MetaItem) { - when (item) { - is MetaItemValue -> { - if (item.value == Null) { - setProperty(at, null) - } else - setProperty(at, item) - } - is MetaItemNode -> item.node.items.forEach { (token, childItem) -> - updateProperties(at + token, childItem) - } + public fun Vision.updateProperties(at: Name, item: Meta) { + setPropertyValue(at, item.value) + item.items.forEach { (token, item) -> + updateProperties(at + token, item) } } diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionChange.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionChange.kt index aa50001a..e538a89c 100644 --- a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionChange.kt +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionChange.kt @@ -21,7 +21,7 @@ public class VisionChangeBuilder : VisionContainerBuilder { private var reset: Boolean = false private var vision: Vision? = null - private val propertyChange = MetaBuilder() + private val propertyChange = MutableMeta() private val children: HashMap = HashMap() public fun isEmpty(): Boolean = propertyChange.isEmpty() && propertyChange.isEmpty() && children.isEmpty() @@ -30,17 +30,17 @@ public class VisionChangeBuilder : VisionContainerBuilder { private fun getOrPutChild(visionName: Name): VisionChangeBuilder = children.getOrPut(visionName) { VisionChangeBuilder() } - public fun propertyChanged(visionName: Name, propertyName: Name, item: MetaItem?) { + public fun propertyChanged(visionName: Name, propertyName: Name, item: Meta?) { if (visionName == Name.EMPTY) { //Write property removal as [Null] - propertyChange[propertyName] = (item ?: Null.asMetaItem()) + propertyChange[propertyName] = (item ?: Meta(Null)) } else { getOrPutChild(visionName).propertyChanged(Name.EMPTY, propertyName, item) } } override fun set(name: Name?, child: Vision?) { - if(name == null) error("Static children are not allowed in VisionChange") + if (name == null) error("Static children are not allowed in VisionChange") getOrPutChild(name).apply { vision = child reset = vision == null @@ -90,7 +90,7 @@ private fun CoroutineScope.collectChange( //Collect properties change source.onPropertyChange(this) { propertyName -> - val newItem = source.ownProperties[propertyName] + val newItem = source.getOwnProperty(propertyName) collector().propertyChanged(name, propertyName, newItem) } diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionGroup.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionGroup.kt index 6b35d971..c7c47901 100644 --- a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionGroup.kt +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionGroup.kt @@ -75,12 +75,12 @@ public interface MutableVisionGroup : VisionGroup, VisionContainerBuilder } -public operator fun VisionContainer.get(str: String): V? = get(str.toName()) +public operator fun VisionContainer.get(str: String): V? = get(Name.parse(str)) public operator fun VisionContainerBuilder.set(token: NameToken, child: V?): Unit = set(token.asName(), child) public operator fun VisionContainerBuilder.set(key: String?, child: V?): Unit = - set(key?.toName(), child) + set(key?.let(Name::parse), child) public fun MutableVisionGroup.removeAll(): Unit = children.keys.map { it.asName() }.forEach { this[it] = null } \ No newline at end of file diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionGroupBase.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionGroupBase.kt index 933b55cd..aab27bc5 100644 --- a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionGroupBase.kt +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionGroupBase.kt @@ -131,15 +131,15 @@ public open class VisionGroupBase( } } - override fun update(change: VisionChange) { + override fun change(change: VisionChange) { change.children?.forEach { (name, change) -> when { change.delete -> set(name, null) change.vision != null -> set(name, change.vision) - else -> get(name)?.update(change) + else -> get(name)?.change(change) } } - super.update(change) + super.change(change) } } diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionManager.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionManager.kt index 4f01e839..3f9bfaa8 100644 --- a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionManager.kt +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionManager.kt @@ -8,12 +8,10 @@ import kotlinx.serialization.modules.polymorphic import kotlinx.serialization.modules.subclass import space.kscience.dataforge.context.* import space.kscience.dataforge.meta.Meta -import space.kscience.dataforge.meta.descriptors.NodeDescriptor -import space.kscience.dataforge.meta.node +import space.kscience.dataforge.meta.descriptors.MetaDescriptor import space.kscience.dataforge.meta.toJson -import space.kscience.dataforge.meta.toMetaItem +import space.kscience.dataforge.meta.toMeta import space.kscience.dataforge.names.Name -import space.kscience.dataforge.names.toName import kotlin.reflect.KClass public class VisionManager(meta: Meta) : AbstractPlugin(meta) { @@ -48,12 +46,11 @@ public class VisionManager(meta: Meta) : AbstractPlugin(meta) { jsonFormat.encodeToJsonElement(visionSerializer, vision) //TODO remove double transformation with dedicated Meta serial format - public fun decodeFromMeta(meta: Meta, descriptor: NodeDescriptor? = null): Vision = + public fun decodeFromMeta(meta: Meta, descriptor: MetaDescriptor? = null): Vision = decodeFromJson(meta.toJson(descriptor)) - public fun encodeToMeta(vision: Vision, descriptor: NodeDescriptor? = null): Meta = - encodeToJsonElement(vision).toMetaItem(descriptor).node - ?: error("Expected node, but value found. Check your serializer!") + public fun encodeToMeta(vision: Vision, descriptor: MetaDescriptor? = null): Meta = + encodeToJsonElement(vision).toMeta(descriptor) public companion object : PluginFactory { override val tag: PluginTag = PluginTag(name = "vision", group = PluginTag.DATAFORGE_GROUP) @@ -89,7 +86,7 @@ public abstract class VisionPlugin(meta: Meta = Meta.EMPTY) : AbstractPlugin(met protected abstract val visionSerializersModule: SerializersModule override fun content(target: String): Map = when (target) { - VisionManager.VISION_SERIALIZER_MODULE_TARGET -> mapOf(tag.toString().toName() to visionSerializersModule) + VisionManager.VISION_SERIALIZER_MODULE_TARGET -> mapOf(Name.parse(tag.toString()) to visionSerializersModule) else -> super.content(target) } } diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionProperties.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionProperties.kt new file mode 100644 index 00000000..363ad729 --- /dev/null +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionProperties.kt @@ -0,0 +1,38 @@ +package space.kscience.visionforge + +import space.kscience.dataforge.meta.Meta +import space.kscience.dataforge.meta.MutableMeta +import space.kscience.dataforge.names.Name +import space.kscience.dataforge.names.NameToken +import space.kscience.dataforge.names.plus +import space.kscience.dataforge.values.Value + +internal class VisionProperties( + val vision: Vision, + val rootName: Name, + val inherit: Boolean = false, + val includeStyles: Boolean = true, + val includeDefaults: Boolean = true, +) : MutableMeta { + + override val items: Map + get() = vision.getProperty(rootName, inherit, includeStyles, includeDefaults)?.items?.mapValues { + VisionProperties(vision, rootName + it.key, inherit, includeStyles, includeDefaults) + } ?: emptyMap() + + override var value: Value? + get() = vision.getProperty(rootName, inherit, includeStyles, includeDefaults)?.value + set(value) { + vision.setPropertyValue(rootName, value) + } + + override fun getOrCreate(name: Name): MutableMeta = VisionProperties(vision, rootName + name) + + override fun setMeta(name: Name, node: Meta?) { + vision.setPropertyNode(rootName + name, node) + } + + override fun equals(other: Any?): Boolean = Meta.equals(this, other as? Meta) + override fun hashCode(): Int = Meta.hashCode(this) + override fun toString(): String = Meta.toString(this) +} \ No newline at end of file diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionPropertyContainer.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionPropertyContainer.kt index 3710175c..f6f822b7 100644 --- a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionPropertyContainer.kt +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionPropertyContainer.kt @@ -1,32 +1,49 @@ package space.kscience.visionforge -import space.kscience.dataforge.meta.* +import space.kscience.dataforge.meta.Configurable +import space.kscience.dataforge.meta.Meta +import space.kscience.dataforge.meta.ObservableMutableMeta +import space.kscience.dataforge.meta.get import space.kscience.dataforge.names.Name +import space.kscience.dataforge.values.Value /** * Property containers are used to create a symmetric behaviors for vision properties and style builders */ -public interface VisionPropertyContainer { +public interface VisionPropertyContainer { public fun getProperty( name: Name, inherit: Boolean = false, includeStyles: Boolean = true, includeDefaults: Boolean = true, - ): MetaItem? + ): Meta? - public fun setProperty(name: Name, item: MetaItem?, notify: Boolean = true) + /** + * Replace the property node. If [node] is null remove node and its descendants + */ + public fun setPropertyNode(name: Name, node: Meta?, notify: Boolean = true) + + /** + * Set a value of specific property node + */ + public fun setPropertyValue(name: Name, value: Value?, notify: Boolean = true) } -public open class SimpleVisionPropertyContainer(protected val config: ObservableMeta): VisionPropertyContainer{ +public open class SimpleVisionPropertyContainer( + override val meta: ObservableMutableMeta, +) : VisionPropertyContainer, Configurable { override fun getProperty( name: Name, inherit: Boolean, includeStyles: Boolean, includeDefaults: Boolean - ): MetaItem? = config[name] + ): Meta? = meta[name] - override fun setProperty(name: Name, item: MetaItem?, notify: Boolean) { - config[name] = item + override fun setPropertyNode(name: Name, node: Meta?, notify: Boolean) { + this.meta.setMeta(name, node) } + override fun setPropertyValue(name: Name, value: Value?, notify: Boolean) { + this.meta.setValue(name, value) + } } \ No newline at end of file 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 287f32bc..a0b64d8b 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 @@ -2,12 +2,13 @@ package space.kscience.visionforge.html import kotlinx.html.* import space.kscience.dataforge.meta.Meta -import space.kscience.dataforge.meta.MetaBuilder import space.kscience.dataforge.meta.MetaSerializer +import space.kscience.dataforge.meta.MutableMeta import space.kscience.dataforge.meta.isEmpty import space.kscience.dataforge.misc.DFExperimental import space.kscience.dataforge.names.Name -import space.kscience.dataforge.names.toName +import space.kscience.dataforge.names.NameToken +import space.kscience.dataforge.names.asName import space.kscience.visionforge.Vision import space.kscience.visionforge.VisionManager import kotlin.collections.set @@ -25,7 +26,7 @@ public class VisionOutput @PublishedApi internal constructor(public val manager: //TODO expose a way to define required plugins. - public inline fun meta(block: MetaBuilder.() -> Unit) { + public inline fun meta(block: MutableMeta.() -> Unit) { this.meta = Meta(block) } } @@ -36,7 +37,7 @@ public class VisionOutput @PublishedApi internal constructor(public val manager: @VisionDSL public abstract class VisionTagConsumer( private val root: TagConsumer, - public val manager:VisionManager, + public val manager: VisionManager, private val idPrefix: String? = null, ) : TagConsumer by root { @@ -94,11 +95,11 @@ public abstract class VisionTagConsumer( public inline fun TagConsumer.vision( name: String = DEFAULT_VISION_NAME, visionProvider: VisionOutput.() -> Vision, - ): T = vision(name.toName(), visionProvider) + ): T = vision(Name.parse(name), visionProvider) public fun TagConsumer.vision( vision: Vision, - ): T = vision("vision[${vision.hashCode()}]".toName(), vision) + ): T = vision(NameToken("vision", vision.hashCode().toString()).asName(), vision) /** * Process the resulting object produced by [TagConsumer] diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/misc.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/misc.kt index f471b234..900f6f1c 100644 --- a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/misc.kt +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/misc.kt @@ -1,18 +1,20 @@ package space.kscience.visionforge -import space.kscience.dataforge.meta.* +import space.kscience.dataforge.meta.Laminate +import space.kscience.dataforge.meta.Meta +import space.kscience.dataforge.meta.boolean +import space.kscience.dataforge.meta.isLeaf import space.kscience.dataforge.values.asValue @DslMarker public annotation class VisionBuilder -public fun List.merge(): MetaItem? = when (val first = firstOrNull { it != null }) { - null -> null - is MetaItemValue -> first //fast search for first entry if it is value - is MetaItemNode -> { - //merge nodes if first encountered node is meta - val laminate: Laminate = Laminate(mapNotNull { it.node }) - MetaItemNode(laminate) +public fun List.merge(): Meta? { + val first = firstOrNull { it != null } + return when { + first == null -> null + first.isLeaf -> first //fast search for first entry if it is value + else -> Laminate(filterNotNull()) //merge nodes if first encountered node is meta } } @@ -21,8 +23,4 @@ public fun List.merge(): MetaItem? = when (val first = firstOrNull { */ public var Vision.visible: Boolean? get() = getProperty(Vision.VISIBLE_KEY).boolean - set(value) = setProperty(Vision.VISIBLE_KEY, value?.asValue()) - -public fun Vision.configure(meta: Meta?): Unit = update(VisionChange(properties = meta)) - -public fun Vision.configure(block: MetaBuilder.() -> Unit): Unit = configure(Meta(block)) \ No newline at end of file + set(value) = setPropertyValue(Vision.VISIBLE_KEY, value?.asValue()) \ No newline at end of file diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/schemeDesctiptors.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/schemeDesctiptors.kt index 4187843f..403312ea 100644 --- a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/schemeDesctiptors.kt +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/schemeDesctiptors.kt @@ -2,11 +2,11 @@ package space.kscience.visionforge import space.kscience.dataforge.meta.Scheme import space.kscience.dataforge.meta.SchemeSpec -import space.kscience.dataforge.meta.asObservable -import space.kscience.dataforge.meta.descriptors.NodeDescriptor -import space.kscience.dataforge.meta.descriptors.NodeDescriptorBuilder -import space.kscience.dataforge.meta.descriptors.ValueDescriptorBuilder -import space.kscience.dataforge.meta.toMutableMeta +import space.kscience.dataforge.meta.descriptors.Described +import space.kscience.dataforge.meta.descriptors.MetaDescriptorBuilder +import space.kscience.dataforge.meta.descriptors.item +import space.kscience.dataforge.meta.descriptors.value +import space.kscience.dataforge.names.Name import space.kscience.dataforge.values.ValueType import kotlin.reflect.KProperty1 import kotlin.reflect.typeOf @@ -14,55 +14,53 @@ import kotlin.reflect.typeOf /** * TODO to be moved into the core */ -public inline fun NodeDescriptorBuilder.value( +public inline fun MetaDescriptorBuilder.value( property: KProperty1, - noinline block: ValueDescriptorBuilder.() -> Unit = {}, + noinline block: MetaDescriptorBuilder.() -> Unit = {}, ) { when (typeOf()) { typeOf(), typeOf(), typeOf(), typeOf(), typeOf(), typeOf() -> - value(property.name) { - type(ValueType.NUMBER) + value(property.name, ValueType.NUMBER) { block() } typeOf(), typeOf(), typeOf(), typeOf(), typeOf(), typeOf() -> - value(property.name) { - type(ValueType.NUMBER) + value(property.name, ValueType.NUMBER) { block() } - typeOf() -> value(property.name) { - type(ValueType.BOOLEAN) + typeOf() -> value(property.name, ValueType.BOOLEAN) { block() } typeOf>(), typeOf>(), typeOf>(), typeOf>(), typeOf>(), typeOf>(), typeOf(), typeOf(), typeOf(), typeOf(), typeOf(), - -> value(property.name) { - type(ValueType.NUMBER) + -> value(property.name, ValueType.NUMBER) { multiple = true block() } - typeOf() -> value(property.name) { - type(ValueType.STRING) + typeOf() -> value(property.name, ValueType.STRING) { block() } - typeOf>(), typeOf>() -> value(property.name) { - type(ValueType.STRING) + typeOf>(), typeOf>() -> value(property.name, ValueType.STRING) { multiple = true block() } - else -> value(property.name, block) + else -> item(property.name, block) } } -public fun NodeDescriptor.copy(block: NodeDescriptorBuilder.() -> Unit = {}): NodeDescriptor { - return NodeDescriptorBuilder(toMeta().toMutableMeta().asObservable()).apply(block) +public fun MetaDescriptorBuilder.item( + key: String, + described: Described, + block: MetaDescriptorBuilder.() -> Unit = {}, +) { + described.descriptor?.let { + item(Name.parse(key), it, block) + } } -public inline fun NodeDescriptorBuilder.scheme( +public inline fun MetaDescriptorBuilder.scheme( property: KProperty1, spec: SchemeSpec, - noinline block: NodeDescriptorBuilder.() -> Unit = {}, + noinline block: MetaDescriptorBuilder.() -> Unit = {}, ) { - spec.descriptor?.let { descriptor -> - item(property.name, descriptor.copy(block)) - } -} \ No newline at end of file + item(property.name, spec, block) +} diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/visionDelegates.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/visionDelegates.kt new file mode 100644 index 00000000..524363d7 --- /dev/null +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/visionDelegates.kt @@ -0,0 +1,93 @@ +package space.kscience.visionforge + +import space.kscience.dataforge.meta.Meta +import space.kscience.dataforge.meta.transformations.MetaConverter +import space.kscience.dataforge.names.Name +import space.kscience.dataforge.values.Value +import space.kscience.dataforge.values.number +import kotlin.properties.ReadWriteProperty +import kotlin.reflect.KProperty + +public fun Vision.propertyNode( + name: Name? = null, + inherit: Boolean = false, + includeStyles: Boolean = true, + includeDefaults: Boolean = true, +): ReadWriteProperty = object : ReadWriteProperty { + override fun getValue(thisRef: Any?, property: KProperty<*>): Meta? = + getProperty(name ?: Name.parse(property.name), inherit, includeStyles, includeDefaults) + + override fun setValue(thisRef: Any?, property: KProperty<*>, value: Meta?) { + setPropertyNode(name ?: Name.parse(property.name), value) + } +} + +public fun Vision.propertyNode( + converter: MetaConverter, + name: Name? = null, + inherit: Boolean = false, + includeStyles: Boolean = true, + includeDefaults: Boolean = true, +): ReadWriteProperty = object : ReadWriteProperty { + override fun getValue(thisRef: Any?, property: KProperty<*>): T? = getProperty( + name ?: Name.parse(property.name), + inherit, + includeStyles, + includeDefaults + )?.let(converter::metaToObject) + + override fun setValue(thisRef: Any?, property: KProperty<*>, value: T?) { + setPropertyNode(name ?: Name.parse(property.name), value?.let(converter::objectToMeta)) + } +} + +public fun Vision.propertyValue( + name: Name? = null, + inherit: Boolean = false, + includeStyles: Boolean = true, + includeDefaults: Boolean = true, +): ReadWriteProperty = object : ReadWriteProperty { + override fun getValue(thisRef: Any?, property: KProperty<*>): Value? = + getProperty(name ?: Name.parse(property.name), inherit, includeStyles, includeDefaults)?.value + + override fun setValue(thisRef: Any?, property: KProperty<*>, value: Value?) { + setPropertyValue(name ?: Name.parse(property.name), value) + } +} + +public fun Vision.propertyValue( + name: Name? = null, + inherit: Boolean = false, + includeStyles: Boolean = true, + includeDefaults: Boolean = true, + setter: (T) -> Value? = { it?.let(Value::of) }, + getter: (Value?) -> T, +): ReadWriteProperty = object : ReadWriteProperty { + override fun getValue(thisRef: Any?, property: KProperty<*>): T = getProperty( + name ?: Name.parse(property.name), + inherit, + includeStyles, + includeDefaults + )?.value.let(getter) + + override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { + setPropertyValue(name ?: Name.parse(property.name), value?.let(setter)) + } +} + +public fun Vision.numberProperty( + name: Name? = null, + inherit: Boolean = false, + includeStyles: Boolean = true, + includeDefaults: Boolean = true +): ReadWriteProperty = propertyValue(name, inherit, includeStyles, includeDefaults) { it?.number } + +public fun Vision.numberProperty( + name: Name? = null, + inherit: Boolean = false, + includeStyles: Boolean = true, + includeDefaults: Boolean = true, + default: () -> Number +): ReadWriteProperty = propertyValue(name, inherit, includeStyles, includeDefaults) { + it?.number ?: default() +} \ No newline at end of file diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/visionDescriptor.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/visionDescriptor.kt index c56b495e..de505c50 100644 --- a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/visionDescriptor.kt +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/visionDescriptor.kt @@ -2,87 +2,63 @@ package space.kscience.visionforge import space.kscience.dataforge.meta.* import space.kscience.dataforge.meta.descriptors.* -import space.kscience.dataforge.names.Name -import space.kscience.dataforge.values.ValueType +import space.kscience.dataforge.names.asName import space.kscience.dataforge.values.asValue private const val INHERITED_DESCRIPTOR_ATTRIBUTE = "inherited" private const val STYLE_DESCRIPTOR_ATTRIBUTE = "useStyles" -public val ItemDescriptor.inherited: Boolean +public val MetaDescriptor.inherited: Boolean get() = attributes[INHERITED_DESCRIPTOR_ATTRIBUTE].boolean ?: false -public var ItemDescriptorBuilder.inherited: Boolean +public var MetaDescriptorBuilder.inherited: Boolean get() = attributes[INHERITED_DESCRIPTOR_ATTRIBUTE].boolean ?: false - set(value) = attributes { - set(INHERITED_DESCRIPTOR_ATTRIBUTE, value) - } + set(value) = attributes.set(INHERITED_DESCRIPTOR_ATTRIBUTE, value) -public val ItemDescriptor.usesStyles: Boolean + +public val MetaDescriptor.usesStyles: Boolean get() = attributes[STYLE_DESCRIPTOR_ATTRIBUTE].boolean ?: true -public var ItemDescriptorBuilder.usesStyles: Boolean +public var MetaDescriptorBuilder.usesStyles: Boolean get() = attributes[STYLE_DESCRIPTOR_ATTRIBUTE].boolean ?: true - set(value) = attributes { - set(STYLE_DESCRIPTOR_ATTRIBUTE, value) - } + set(value) = attributes.set(STYLE_DESCRIPTOR_ATTRIBUTE, value) public val Vision.describedProperties: Meta get() = Meta { - descriptor?.items?.forEach { (key, descriptor) -> - key put getProperty(key, inherit = descriptor.inherited) + descriptor?.children?.forEach { (key, descriptor) -> + this.setMeta(key.asName(), getProperty(key, inherit = descriptor.inherited)) } } -public val ValueDescriptor.widget: Meta - get() = attributes["widget"].node ?: Meta.EMPTY +public val MetaDescriptor.widget: Meta + get() = attributes["widget"] ?: Meta.EMPTY /** * Extension property to access the "widget" key of [ValueDescriptor] */ -public var ValueDescriptorBuilder.widget: Meta - get() = attributes["widget"].node ?: Meta.EMPTY +public var MetaDescriptorBuilder.widget: Meta + get() = attributes["widget"] ?: Meta.EMPTY set(value) { - attributes { - set("widget", value) - } + attributes["widget"] = value } -public val ValueDescriptor.widgetType: String? +public val MetaDescriptor.widgetType: String? get() = attributes["widget.type"].string /** * Extension property to access the "widget.type" key of [ValueDescriptor] */ -public var ValueDescriptorBuilder.widgetType: String? +public var MetaDescriptorBuilder.widgetType: String? get() = attributes["widget.type"].string set(value) { - attributes { - set("widget.type", value) - } + attributes["widget.type"] = value?.asValue() } /** * If true, this item is hidden in property editor. Default is false */ -public val ItemDescriptor.hidden: Boolean +public val MetaDescriptor.hidden: Boolean get() = attributes["widget.hide"].boolean ?: false -public fun ItemDescriptorBuilder.hide(): Unit = attributes { - set("widget.hide", true) -} - - -public inline fun > NodeDescriptorBuilder.enum( - key: Name, - default: E?, - crossinline modifier: ValueDescriptorBuilder.() -> Unit = {}, -): Unit = value(key) { - type(ValueType.STRING) - default?.let { - default(default) - } - allowedValues = enumValues().map { it.asValue() } - modifier() -} \ No newline at end of file +public fun MetaDescriptorBuilder.hide(): Unit = attributes.set("widget.hide", true) \ No newline at end of file diff --git a/visionforge-core/src/commonTest/kotlin/space/kscience/visionforge/html/HtmlTagTest.kt b/visionforge-core/src/commonTest/kotlin/space/kscience/visionforge/html/HtmlTagTest.kt index 1199b89b..2a7c6d28 100644 --- a/visionforge-core/src/commonTest/kotlin/space/kscience/visionforge/html/HtmlTagTest.kt +++ b/visionforge-core/src/commonTest/kotlin/space/kscience/visionforge/html/HtmlTagTest.kt @@ -57,7 +57,7 @@ class HtmlTagTest { div { h2 { +"Properties" } ul { - (vision as? VisionBase)?.meta?.items?.forEach { + (vision as? VisionBase)?.meta()?.items?.forEach { li { a { +it.key.toString() } p { +it.value.toString() } diff --git a/visionforge-core/src/jsMain/kotlin/space/kscience/visionforge/VisionClient.kt b/visionforge-core/src/jsMain/kotlin/space/kscience/visionforge/VisionClient.kt index 017c615d..ae691d4b 100644 --- a/visionforge-core/src/jsMain/kotlin/space/kscience/visionforge/VisionClient.kt +++ b/visionforge-core/src/jsMain/kotlin/space/kscience/visionforge/VisionClient.kt @@ -92,7 +92,7 @@ public class VisionClient : AbstractPlugin() { } logger.debug { "Got update $change for output with name $name" } - vision.update(change) + vision.change(change) } else { console.error("WebSocket message data is not a string") } diff --git a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/FXPlugin.kt b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/FXPlugin.kt index 5e7994ef..d8210a13 100644 --- a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/FXPlugin.kt +++ b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/FXPlugin.kt @@ -115,5 +115,5 @@ public class ApplicationSurrogate : App() { } public fun Context.display(width: Double = 800.0, height: Double = 600.0, component: () -> UIComponent) { - plugins.fetch(FXPlugin).display(component(), width, height) + fetch(FXPlugin).display(component(), width, height) } \ No newline at end of file diff --git a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/ComboBoxValueChooser.kt b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/ComboBoxValueChooser.kt index a3d5c942..a99599ee 100644 --- a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/ComboBoxValueChooser.kt +++ b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/ComboBoxValueChooser.kt @@ -9,8 +9,8 @@ import javafx.collections.FXCollections import javafx.scene.control.ComboBox import javafx.util.StringConverter import space.kscience.dataforge.meta.Meta +import space.kscience.dataforge.meta.descriptors.allowedValues import space.kscience.dataforge.meta.get -import space.kscience.dataforge.meta.value import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.asName import space.kscience.dataforge.values.Value @@ -56,7 +56,7 @@ public class ComboBoxValueChooser(public val values: Collection? = null) override val name: Name = "combo".asName() override fun invoke(meta: Meta): ValueChooser = - ComboBoxValueChooser(meta["values"].value?.list) + ComboBoxValueChooser(meta["values"]?.value?.list) } } diff --git a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/ConfigEditor.kt b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/ConfigEditor.kt deleted file mode 100644 index 76d32f57..00000000 --- a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/ConfigEditor.kt +++ /dev/null @@ -1,189 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package space.kscience.visionforge.editor - -import de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon -import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView -import javafx.scene.Node -import javafx.scene.control.* -import javafx.scene.control.cell.TextFieldTreeTableCell -import javafx.scene.layout.BorderPane -import javafx.scene.layout.HBox -import javafx.scene.layout.Priority -import javafx.scene.paint.Color -import javafx.scene.text.Text -import space.kscience.dataforge.context.Global -import space.kscience.dataforge.meta.Config -import space.kscience.dataforge.meta.descriptors.NodeDescriptor -import space.kscience.dataforge.names.NameToken -import space.kscience.visionforge.dfIconView -import tornadofx.* - -/** - * A configuration editor fragment - * - * @author Alexander Nozik - */ -public class ConfigEditor( - public val rootNode: FXMetaNode, - public val allowNew: Boolean = true, - title: String = "Configuration editor" -) : Fragment(title = title, icon = dfIconView) { - //TODO replace parameters by properties - - public constructor(config: Config, descriptor: NodeDescriptor?, title: String = "Configuration editor") : - this(FXMeta.root(config, descriptor = descriptor), title = title) - - override val root: BorderPane = borderpane { - center = treetableview> { - root = TreeItem(rootNode) - root.isExpanded = true - sortMode = TreeSortMode.ALL_DESCENDANTS - columnResizePolicy = TreeTableView.CONSTRAINED_RESIZE_POLICY - populate { - when (val fxMeta = it.value) { - is FXMetaNode -> { - fxMeta.children - } - is FXMetaValue -> null - } - } - column("Name", FXMeta::name) { - setCellFactory { - object : TextFieldTreeTableCell, NameToken>() { - override fun updateItem(item: NameToken?, empty: Boolean) { - super.updateItem(item, empty) - contextMenu?.items?.removeIf { it.text == "Remove" } - if (!empty) { - if (treeTableRow.item != null) { - textFillProperty().bind(treeTableRow.item.hasValue.objectBinding { - if (it == true) { - Color.BLACK - } else { - Color.GRAY - } - }) - if (treeTableRow.treeItem.value.parent != null && treeTableRow.treeItem.value.hasValue.get()) { - contextmenu { - item("Remove") { - action { - treeTableRow.item.remove() - } - } - } - } - } - } - } - } - } - } - - column("Value") { param: TreeTableColumn.CellDataFeatures, FXMeta> -> - param.value.valueProperty() - }.setCellFactory { - ValueCell() - } - - column("Description") { param: TreeTableColumn.CellDataFeatures, String> -> param.value.value.descriptionProperty } - .setCellFactory { param: TreeTableColumn, String> -> - val cell = TreeTableCell, String>() - val text = Text() - cell.graphic = text - cell.prefHeight = Control.USE_COMPUTED_SIZE - text.wrappingWidthProperty().bind(param.widthProperty()) - text.textProperty().bind(cell.itemProperty()) - cell - } - } - } - - private fun showNodeDialog(): String? { - val dialog = TextInputDialog() - dialog.title = "Node name selection" - dialog.contentText = "Enter a name for new node: " - dialog.headerText = null - - val result = dialog.showAndWait() - return result.orElse(null) - } - - private fun showValueDialog(): String? { - val dialog = TextInputDialog() - dialog.title = "Value name selection" - dialog.contentText = "Enter a name for new value: " - dialog.headerText = null - - val result = dialog.showAndWait() - return result.orElse(null) - } - - private inner class ValueCell : TreeTableCell, FXMeta?>() { - - public override fun updateItem(item: FXMeta?, empty: Boolean) { - if (!empty) { - if (item != null) { - when (item) { - is FXMetaValue -> { - text = null - val chooser = ValueChooser.build( - Global, - item.valueProperty, - item.descriptor - ) { - item.set(it) - } - graphic = chooser.node - } - is FXMetaNode -> { - if (allowNew) { - text = null - graphic = HBox().apply { - val glyph: Node = FontAwesomeIconView(FontAwesomeIcon.PLUS_CIRCLE) - button("node", graphic = glyph) { - hgrow = Priority.ALWAYS - maxWidth = Double.POSITIVE_INFINITY - action { - showNodeDialog()?.let { - item.addNode(it) - } - } - } - button("value", graphic = FontAwesomeIconView(FontAwesomeIcon.PLUS_SQUARE)) { - hgrow = Priority.ALWAYS - maxWidth = Double.POSITIVE_INFINITY - action { - showValueDialog()?.let { - item.addValue(it) - } - } - } - } - } else { - text = "" - } - } - } - - } else { - text = null - graphic = null - } - } else { - text = null - graphic = null - } - } - - } - - companion object { - /** - * The tag not to display node or value in configurator - */ - const val NO_CONFIGURATOR_TAG = "nocfg" - } -} diff --git a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/FXMeta.kt b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/FXMeta.kt deleted file mode 100644 index 3e2cfc8d..00000000 --- a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/FXMeta.kt +++ /dev/null @@ -1,223 +0,0 @@ -package space.kscience.visionforge.editor - -import javafx.beans.binding.ListBinding -import javafx.beans.binding.ObjectBinding -import javafx.beans.property.SimpleObjectProperty -import javafx.beans.value.ObservableBooleanValue -import javafx.beans.value.ObservableStringValue -import javafx.collections.ObservableList -import space.kscience.dataforge.meta.* -import space.kscience.dataforge.meta.descriptors.ItemDescriptor -import space.kscience.dataforge.meta.descriptors.NodeDescriptor -import space.kscience.dataforge.meta.descriptors.ValueDescriptor -import space.kscience.dataforge.names.* -import space.kscience.dataforge.values.Null -import space.kscience.dataforge.values.Value -import tornadofx.* - -/** - * A display for meta and descriptor - */ -sealed class FXMeta> : Comparable> { - abstract val name: NameToken - abstract val parent: FXMetaNode? - abstract val descriptionProperty: ObservableStringValue - abstract val descriptor: ItemDescriptor? - - abstract val hasValue: ObservableBooleanValue - - override fun compareTo(other: FXMeta<*>): Int { - return if (this.hasValue.get() == other.hasValue.get()) { - this.name.toString().compareTo(other.name.toString()) - } else { - this.hasValue.get().compareTo(other.hasValue.get()) - } - } - - companion object { - fun > root( - node: M, - descriptor: NodeDescriptor? = null, - rootName: String = "root" - ): FXMetaNode = - FXMetaNode(NameToken(rootName), null, node, descriptor) - - fun root(node: Meta, descriptor: NodeDescriptor? = null, rootName: String = "root"): FXMetaNode = - root(node.seal(), descriptor, rootName) - } -} - -class FXMetaNode>( - override val name: NameToken, - override val parent: FXMetaNode?, - nodeValue: M? = null, - descriptorValue: NodeDescriptor? = null -) : FXMeta() { - - /** - * A descriptor that could be manually set to the node - */ - private val innerDescriptorProperty = SimpleObjectProperty(descriptorValue) - - /** - * Actual descriptor which holds value inferred from parrent - */ - val descriptorProperty = objectBinding(innerDescriptorProperty) { - value ?: parent?.descriptor?.nodes?.get(this@FXMetaNode.name.body) - } - - override val descriptor: NodeDescriptor? by descriptorProperty - - private val innerNodeProperty = SimpleObjectProperty(nodeValue) - - val nodeProperty: ObjectBinding = objectBinding(innerNodeProperty) { - value ?: parent?.node?.get(this@FXMetaNode.name).node - } - - val node: M? by nodeProperty - - override val descriptionProperty = innerDescriptorProperty.stringBinding { it?.info ?: "" } - - override val hasValue: ObservableBooleanValue = nodeProperty.booleanBinding { it != null } - - private val filter: (FXMeta) -> Boolean = { cfg -> - !(cfg.descriptor?.attributes?.get(ConfigEditor.NO_CONFIGURATOR_TAG)?.boolean ?: false) - } - - val children = object : ListBinding>() { - - init { - bind(nodeProperty, descriptorProperty) - - val listener: (Name, MetaItem?, MetaItem?) -> Unit = { name, _, _ -> - if (name.length == 1) invalidate() - } - - (node as? Config)?.onChange(this, listener) - - nodeProperty.addListener { _, oldValue, newValue -> - if (newValue == null) { - (oldValue as? Config)?.removeListener(this) - } - - if (newValue is Config) { - newValue.onChange(this, listener) - } - } - } - - override fun computeValue(): ObservableList> { - val nodeKeys = node?.items?.keys?.toSet() ?: emptySet() - val descriptorKeys = descriptor?.items?.keys?.map { NameToken(it) } ?: emptyList() - val keys: Set = nodeKeys + descriptorKeys - - val items = keys.map { token -> - val actualItem = node?.items?.get(token) - val actualDescriptor = descriptor?.items?.get(token.body) - - if (actualItem is MetaItemNode || actualDescriptor is NodeDescriptor) { - FXMetaNode(token, this@FXMetaNode) - } else { - FXMetaValue(token, this@FXMetaNode) - } - } - - return items.filter(filter).asObservable() - } - } - - init { - if (parent != null) { - parent.descriptorProperty.onChange { descriptorProperty.invalidate() } - parent.nodeProperty.onChange { nodeProperty.invalidate() } - } - } -} - -public class FXMetaValue>( - override val name: NameToken, - override val parent: FXMetaNode -) : FXMeta() { - - public val descriptorProperty = parent.descriptorProperty.objectBinding { - it?.values?.get(name.body) - } - - /** - * A descriptor that could be manually set to the node - */ - override val descriptor: ValueDescriptor? by descriptorProperty - - //private val innerValueProperty = SimpleObjectProperty(value) - - public val valueProperty = descriptorProperty.objectBinding { descriptor -> - parent.node?.get(name).value ?: descriptor?.default - } - - override val hasValue: ObservableBooleanValue = parent.nodeProperty.booleanBinding { it?.get(name) != null } - - public val value by valueProperty - - override val descriptionProperty = descriptorProperty.stringBinding { it?.info ?: "" } -} - -public fun > FXMetaNode.remove(name: NameToken) { - node?.remove(name.asName()) - children.invalidate() -} - -private fun > M.createEmptyNode(token: NameToken, append: Boolean): M { - return if (append && token.hasIndex()) { - val name = token.asName() - val index = (getIndexed(name).keys.mapNotNull { it?.toIntOrNull() }.maxOrNull() ?: -1) + 1 - val newName = name.withIndex(index.toString()) - set(newName, Meta.EMPTY) - get(newName).node!! - } else { - this.set(token.asName(), Meta.EMPTY) - //FIXME possible concurrency bug - get(token).node!! - } -} - -fun > FXMetaNode.getOrCreateNode(): M { - val node = node - return when { - node != null -> node - parent != null -> parent.getOrCreateNode().createEmptyNode(this.name, descriptor?.multiple == true).also { - parent.children.invalidate() - } - else -> kotlin.error("Orphan empty node is not allowed") - } - -} - -fun > FXMeta.remove() { - parent?.node?.remove(name.asName()) -} - -fun > FXMetaNode.addValue(key: String) { - val parent = getOrCreateNode() - if (descriptor?.multiple == true) { - parent.append(key, Null) - } else { - parent[key] = Null - } -} - -fun > FXMetaNode.addNode(key: String) { - val parent = getOrCreateNode() - if (descriptor?.multiple == true) { - parent.append(key, Meta.EMPTY) - } else { - parent[key] = Meta.EMPTY - } -} - -fun > FXMetaValue.set(value: Value?) { - if (descriptor?.multiple == true) { - parent.getOrCreateNode().append(this.name.body, value) - } else { - parent.getOrCreateNode()[this.name] = value - } -} \ No newline at end of file diff --git a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/FXMetaModel.kt b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/FXMetaModel.kt new file mode 100644 index 00000000..a3509221 --- /dev/null +++ b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/FXMetaModel.kt @@ -0,0 +1,217 @@ +package space.kscience.visionforge.editor + +import javafx.beans.binding.Binding +import javafx.beans.binding.BooleanBinding +import javafx.beans.binding.ListBinding +import javafx.beans.property.SimpleObjectProperty +import javafx.collections.ObservableList +import space.kscience.dataforge.meta.* +import space.kscience.dataforge.meta.descriptors.MetaDescriptor +import space.kscience.dataforge.meta.descriptors.get +import space.kscience.dataforge.names.* +import space.kscience.dataforge.values.Value +import tornadofx.* + +/** + * A display for meta and descriptor + */ +public class FXMetaModel( + public val root: M, + public val rootDescriptor: MetaDescriptor?, + public val nodeName: Name, + public val title: String = nodeName.lastOrNull()?.toString() ?: "Meta" +) : Comparable> { + + private val existingNode = SimpleObjectProperty(root[nodeName]) + + public val children: ListBinding> = object : ListBinding>() { + override fun computeValue(): ObservableList> { + val nodeKeys = existingNode.get().items.keys + val descriptorKeys = descriptor?.children?.keys?.map { NameToken(it) } ?: emptySet() + return (nodeKeys + descriptorKeys).map { + FXMetaModel( + root, + rootDescriptor, + nodeName + it + ) + }.filter(filter).asObservable() + } + } + + init { + //add listener to the root node if possible + if (root is ObservableMeta) { + root.onChange(this) { changed -> + if (changed.startsWith(nodeName)) { + if (nodeName.length == changed.length) existingNode.set(root[nodeName]) + else if (changed.length == nodeName.length + 1) children.invalidate() + } + } + } + } + + public val descriptor: MetaDescriptor? = rootDescriptor?.get(nodeName) + + public val existsProperty: BooleanBinding = existingNode.isNotNull + + public val exists: Boolean by existsProperty + + public val valueProperty: Binding = existingNode.objectBinding { + existingNode.get().value ?: descriptor?.defaultValue + } + + override fun compareTo(other: FXMetaModel<*>): Int = if (this.exists == other.exists) { + this.nodeName.toString().compareTo(other.nodeName.toString()) + } else { + this.exists.compareTo(other.exists) + } + + public companion object { + private val filter: (FXMetaModel<*>) -> Boolean = { cfg -> + !(cfg.descriptor?.attributes?.get(MutableMetaEditor.NO_CONFIGURATOR_TAG)?.boolean ?: false) + } + + public fun root( + node: M, + descriptor: MetaDescriptor? = null, + rootName: String = "root" + ): FXMetaModel = FXMetaModel(node, descriptor, Name.EMPTY, title = rootName) + } + +// /** +// * A descriptor that could be manually set to the node +// */ +// private val innerDescriptorProperty = SimpleObjectProperty(descriptorValue) +// +// /** +// * Actual descriptor which holds value inferred from parrent +// */ +// val descriptorProperty: ObjectBinding = objectBinding(innerDescriptorProperty) { +// value ?: parent?.descriptor?.get(this@FXMeta.name.body) +// } +// +// val descriptor: MetaDescriptor? by descriptorProperty +// +// private val innerNodeProperty = SimpleObjectProperty(nodeValue) +// +// val nodeProperty: ObjectBinding = objectBinding(innerNodeProperty) { +// value ?: parent?.node?.get(this@FXMeta.name) +// } +// +// val node by nodeProperty +// +// val hasValue: ObservableBooleanValue = nodeProperty.booleanBinding { it != null } +// +// private val filter: (FXMeta) -> Boolean = { cfg -> +// !(cfg.descriptor?.attributes?.get(MutableMetaEditor.NO_CONFIGURATOR_TAG)?.boolean ?: false) +// } +// +// val children: ListBinding> = object : ListBinding>() { +// +// init { +// bind(nodeProperty, descriptorProperty) +// +// val listener: Meta.(Name) -> Unit = { name -> +// if (name.length == 1) invalidate() +// } +// +// (node as? ObservableMeta)?.onChange(this, listener) +// +// nodeProperty.addListener { _, oldValue, newValue -> +// if (newValue == null) { +// (oldValue as? ObservableMeta)?.removeListener(this) +// } +// +// if (newValue is ObservableMeta) { +// newValue.onChange(this, listener) +// } +// } +// } +// +// override fun computeValue(): ObservableList> { +// val nodeKeys = node?.items?.keys?.toSet() ?: emptySet() +// val descriptorKeys = descriptor?.children?.keys?.map { NameToken(it) } ?: emptyList() +// val keys: Set = nodeKeys + descriptorKeys +// +// val items = keys.map { token -> +// val actualItem = node?.items?.get(token) +// val actualDescriptor = descriptor?.children?.get(token.body) +// +// if (actualItem is MetaNode) { +// FXMetaNode(token, this@FXMetaNode) +// } else { +// FXMetaValue(token, this@FXMetaNode) +// } +// } +// +// return items.filter(filter).asObservable() +// } +// } +// +// init { +// if (parent != null) { +// parent.descriptorProperty.onChange { descriptorProperty.invalidate() } +// parent.nodeProperty.onChange { nodeProperty.invalidate() } +// } +// } +// +} + +// +//internal fun FXMeta.remove(name: NameToken) { +// node?.remove(name.asName()) +// children.invalidate() +//} +// +//private fun M.createEmptyNode(token: NameToken, append: Boolean): M { +// return if (append && token.hasIndex()) { +// val name = token.asName() +// val index = (getIndexed(name).keys.mapNotNull { it?.toIntOrNull() }.maxOrNull() ?: -1) + 1 +// val newName = name.withIndex(index.toString()) +// set(newName, Meta.EMPTY) +// get(newName).node +// } else { +// this.set(token.asName(), Meta.EMPTY) +// //FIXME possible concurrency bug +// get(token).node +// } +//} +// +//internal fun FXMeta.getOrCreateNode(): M { +// val node = node +// return when { +// node != null -> node +// parent != null -> parent.getOrCreateNode().createEmptyNode(this.name, descriptor?.multiple == true).also { +// parent.children.invalidate() +// } +// else -> kotlin.error("Orphan empty node is not allowed") +// } +// +//} + +internal fun FXMetaModel.remove() { + root.remove(nodeName) +} + +// +//internal fun FXMeta.addValue(key: String) { +// val parent = getOrCreateNode() +// if (descriptor?.multiple == true) { +// parent.append(key, Null) +// } else { +// parent[key] = Null +// } +//} +// +//internal fun FXMeta.addNode(key: String) { +// val parent = getOrCreateNode() +// if (descriptor?.multiple == true) { +// parent.append(key, Meta.EMPTY) +// } else { +// parent[key] = Meta.EMPTY +// } +//} +// +internal fun FXMetaModel.setValue(value: Value?) { + root.setValue(nodeName, value) +} \ No newline at end of file diff --git a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/MetaViewer.kt b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/MetaViewer.kt index 1a822793..92c1222f 100644 --- a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/MetaViewer.kt +++ b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/MetaViewer.kt @@ -16,45 +16,42 @@ package space.kscience.visionforge.editor -import javafx.beans.property.SimpleStringProperty import javafx.scene.control.TreeItem import javafx.scene.control.TreeSortMode import javafx.scene.control.TreeTableView +import javafx.scene.layout.BorderPane import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.values.string import space.kscience.visionforge.dfIconView import tornadofx.* -class MetaViewer(val rootNode: FXMetaNode<*>, title: String = "Meta viewer") : Fragment(title, - dfIconView -) { - constructor(meta: Meta, title: String = "Meta viewer"): this( - FXMeta.root( - meta - ),title = title) +public class MetaViewer( + private val rootNode: FXMetaModel, + title: String = "Meta viewer" +) : Fragment(title, dfIconView) { - override val root = borderpane { + public constructor(meta: Meta, title: String = "Meta viewer") : this( + FXMetaModel.root( + meta + ), title = title + ) + + override val root: BorderPane = borderpane { center { - treetableview> { + treetableview> { isShowRoot = false root = TreeItem(rootNode) populate { - when (val fxMeta = it.value) { - is FXMetaNode -> { - fxMeta.children - } - is FXMetaValue -> null - } + val fxMeta = it.value + fxMeta.children } root.isExpanded = true sortMode = TreeSortMode.ALL_DESCENDANTS columnResizePolicy = TreeTableView.CONSTRAINED_RESIZE_POLICY - column("Name", FXMeta<*>::name) - column, String>("Value") { cellDataFeatures -> - when (val item = cellDataFeatures.value.value) { - is FXMetaValue -> item.valueProperty.stringBinding { it?.string ?: "" } - is FXMetaNode -> SimpleStringProperty("[node]") - } + column("Name", FXMetaModel<*>::title) + column, String>("Value") { cellDataFeatures -> + val item = cellDataFeatures.value.value + item.valueProperty.stringBinding { it?.string ?: "" } } } } diff --git a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/MutableMetaEditor.kt b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/MutableMetaEditor.kt new file mode 100644 index 00000000..c7238b44 --- /dev/null +++ b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/MutableMetaEditor.kt @@ -0,0 +1,193 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package space.kscience.visionforge.editor + +import javafx.scene.control.* +import javafx.scene.control.cell.TextFieldTreeTableCell +import javafx.scene.layout.BorderPane +import javafx.scene.paint.Color +import javafx.scene.text.Text +import space.kscience.dataforge.context.Global +import space.kscience.dataforge.meta.MutableMeta +import space.kscience.dataforge.meta.descriptors.MetaDescriptor +import space.kscience.visionforge.dfIconView +import tornadofx.* + +/** + * A Configuration editor fragment + * + * @author Alexander Nozik + */ +public class MutableMetaEditor( + public val rootNode: FXMetaModel, + public val allowNew: Boolean = true, + title: String = "Configuration editor" +) : Fragment(title = title, icon = dfIconView) { + //TODO replace parameters by properties + + public constructor( + MutableMeta: MutableMeta, + descriptor: MetaDescriptor?, + title: String = "Configuration editor" + ) : + this(FXMetaModel.root(MutableMeta, descriptor = descriptor), title = title) + + override val root: BorderPane = borderpane { + center = treetableview> { + root = TreeItem(rootNode) + root.isExpanded = true + sortMode = TreeSortMode.ALL_DESCENDANTS + columnResizePolicy = TreeTableView.CONSTRAINED_RESIZE_POLICY + populate { + it.value.children + } + column("Name", FXMetaModel::title) { + setCellFactory { + object : TextFieldTreeTableCell, String>() { + override fun updateItem(item: String?, empty: Boolean) { + super.updateItem(item, empty) + contextMenu?.items?.removeIf { it.text == "Remove" } + val content = treeTableRow.item + if (!empty) { + if (treeTableRow.item != null) { + textFillProperty().bind(content.existsProperty.objectBinding { + if (it == true) { + Color.BLACK + } else { + Color.GRAY + } + }) + if (content.exists) { + contextmenu { + item("Remove") { + action { + content.remove() + } + } + } + } + } + } + } + } + } + } + + column("Value") { param: TreeTableColumn.CellDataFeatures, FXMetaModel> -> + param.value.valueProperty() + }.setCellFactory { + ValueCell() + } + + column("Description") { param: TreeTableColumn.CellDataFeatures, String> -> + (param.value.value.descriptor?.info ?: "").observable() + }.setCellFactory { param: TreeTableColumn, String> -> + val cell = TreeTableCell, String>() + val text = Text() + cell.graphic = text + cell.prefHeight = Control.USE_COMPUTED_SIZE + text.wrappingWidthProperty().bind(param.widthProperty()) + text.textProperty().bind(cell.itemProperty()) + cell + } + } + } + + private fun showNodeDialog(): String? { + val dialog = TextInputDialog() + dialog.title = "Node name selection" + dialog.contentText = "Enter a name for new node: " + dialog.headerText = null + + val result = dialog.showAndWait() + return result.orElse(null) + } + + private fun showValueDialog(): String? { + val dialog = TextInputDialog() + dialog.title = "Value name selection" + dialog.contentText = "Enter a name for new value: " + dialog.headerText = null + + val result = dialog.showAndWait() + return result.orElse(null) + } + + private inner class ValueCell : TreeTableCell, FXMetaModel?>() { + + public override fun updateItem(item: FXMetaModel?, empty: Boolean) { + if (!empty) { + if (item != null) { + text = null + val chooser = ValueChooser.build( + Global, + item.valueProperty, + item.descriptor + ) { + item.setValue(it) + } + graphic = chooser.node +// when (item) { +// is FXMetaValue -> { +// text = null +// val chooser = ValueChooser.build( +// Global, +// item.valueProperty, +// item.descriptor +// ) { +// item.set(it) +// } +// graphic = chooser.node +// } +// is FXMetaNode -> { +// if (allowNew) { +// text = null +// graphic = HBox().apply { +// val glyph: Node = FontAwesomeIconView(FontAwesomeIcon.PLUS_CIRCLE) +// button("node", graphic = glyph) { +// hgrow = Priority.ALWAYS +// maxWidth = Double.POSITIVE_INFINITY +// action { +// showNodeDialog()?.let { +// item.addNode(it) +// } +// } +// } +// button("value", graphic = FontAwesomeIconView(FontAwesomeIcon.PLUS_SQUARE)) { +// hgrow = Priority.ALWAYS +// maxWidth = Double.POSITIVE_INFINITY +// action { +// showValueDialog()?.let { +// item.addValue(it) +// } +// } +// } +// } +// } else { +// text = "" +// } +// } +// } + + } else { + text = null + graphic = null + } + } else { + text = null + graphic = null + } + } + + } + + public companion object { + /** + * The tag not to display node or value in MutableMetaurator + */ + public const val NO_CONFIGURATOR_TAG: String = "nocfg" + } +} diff --git a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/TextValueChooser.kt b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/TextValueChooser.kt index ea55fc83..9a1840ce 100644 --- a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/TextValueChooser.kt +++ b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/TextValueChooser.kt @@ -10,12 +10,13 @@ import javafx.scene.control.TextField import javafx.scene.input.KeyCode import javafx.scene.input.KeyEvent import space.kscience.dataforge.meta.Meta +import space.kscience.dataforge.meta.descriptors.validate import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.asName import space.kscience.dataforge.values.* import tornadofx.* -class TextValueChooser : ValueChooserBase() { +public class TextValueChooser : ValueChooserBase() { private val displayText: String get() = currentValue().let { @@ -85,7 +86,7 @@ class TextValueChooser : ValueChooserBase() { } private fun validate(value: Value): Boolean { - return descriptor?.isAllowedValue(value) ?: true + return descriptor?.validate(value) ?: true } // @Override @@ -101,7 +102,7 @@ class TextValueChooser : ValueChooserBase() { } } - companion object : ValueChooser.Factory { + public companion object : ValueChooser.Factory { override val name: Name = "text".asName() override fun invoke(meta: Meta): ValueChooser = TextValueChooser() diff --git a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/ValueCallback.kt b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/ValueCallback.kt index 32a0d741..a4e72871 100644 --- a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/ValueCallback.kt +++ b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/ValueCallback.kt @@ -13,11 +13,11 @@ import space.kscience.dataforge.values.Value * @param value Value after change * @param message Message on unsuccessful change */ -class ValueCallbackResponse(val success: Boolean, val value: Value, val message: String) +public class ValueCallbackResponse(public val success: Boolean, public val value: Value, public val message: String) /** * A callback for some visual object trying to change some value * @author [Alexander Nozik](mailto:altavir@gmail.com) */ -typealias ValueCallback = (Value) -> ValueCallbackResponse +public typealias ValueCallback = (Value) -> ValueCallbackResponse diff --git a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/ValueChooser.kt b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/ValueChooser.kt index c3796fa0..99616b61 100644 --- a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/ValueChooser.kt +++ b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/ValueChooser.kt @@ -10,10 +10,12 @@ import javafx.beans.value.ObservableValue import javafx.scene.Node import space.kscience.dataforge.context.Context import space.kscience.dataforge.meta.Meta -import space.kscience.dataforge.meta.descriptors.ValueDescriptor +import space.kscience.dataforge.meta.descriptors.MetaDescriptor +import space.kscience.dataforge.meta.descriptors.allowedValues +import space.kscience.dataforge.meta.descriptors.validate import space.kscience.dataforge.misc.Named import space.kscience.dataforge.misc.Type -import space.kscience.dataforge.names.toName +import space.kscience.dataforge.names.Name import space.kscience.dataforge.provider.provideByType import space.kscience.dataforge.values.Null import space.kscience.dataforge.values.Value @@ -42,8 +44,8 @@ public interface ValueChooser { * * @return */ - public val descriptorProperty: ObjectProperty - public var descriptor: ValueDescriptor? + public val descriptorProperty: ObjectProperty + public var descriptor: MetaDescriptor? public val valueProperty: ObjectProperty public var value: Value? @@ -71,7 +73,7 @@ public interface ValueChooser { public companion object { private fun findWidgetByType(context: Context, type: String): Factory? { - return when (type.toName()) { + return when (Name.parse(type)) { TextValueChooser.name -> TextValueChooser ColorValueChooser.name -> ColorValueChooser ComboBoxValueChooser.name -> ComboBoxValueChooser @@ -79,7 +81,7 @@ public interface ValueChooser { } } - private fun build(context: Context, descriptor: ValueDescriptor?): ValueChooser { + private fun build(context: Context, descriptor: MetaDescriptor?): ValueChooser { return if (descriptor == null) { TextValueChooser(); } else { @@ -93,7 +95,7 @@ public interface ValueChooser { descriptor.widget ) ?: TextValueChooser() } - descriptor.allowedValues.isNotEmpty() -> ComboBoxValueChooser() + !descriptor.allowedValues.isNullOrEmpty() -> ComboBoxValueChooser() else -> TextValueChooser() } chooser.descriptor = descriptor @@ -101,10 +103,10 @@ public interface ValueChooser { } } - fun build( + public fun build( context: Context, value: ObservableValue, - descriptor: ValueDescriptor? = null, + descriptor: MetaDescriptor? = null, setter: (Value) -> Unit, ): ValueChooser { val chooser = build(context, descriptor) @@ -113,7 +115,7 @@ public interface ValueChooser { chooser.setDisplayValue(it ?: Null) } chooser.setCallback { result -> - if (descriptor?.isAllowedValue(result) != false) { + if (descriptor?.validate(result) != false) { setter(result) ValueCallbackResponse(true, result, "OK") } else { diff --git a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/ValueChooserBase.kt b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/ValueChooserBase.kt index 81dc42ac..e9b61886 100644 --- a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/ValueChooserBase.kt +++ b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/ValueChooserBase.kt @@ -8,7 +8,7 @@ package space.kscience.visionforge.editor import javafx.beans.property.SimpleObjectProperty import javafx.scene.Node import org.slf4j.LoggerFactory -import space.kscience.dataforge.meta.descriptors.ValueDescriptor +import space.kscience.dataforge.meta.descriptors.MetaDescriptor import space.kscience.dataforge.values.Null import space.kscience.dataforge.values.Value import tornadofx.* @@ -18,16 +18,16 @@ import tornadofx.* * * @author Alexander Nozik */ -abstract class ValueChooserBase : ValueChooser { +public abstract class ValueChooserBase : ValueChooser { - override val node by lazy { buildNode() } - final override val valueProperty = SimpleObjectProperty(Null) - final override val descriptorProperty = SimpleObjectProperty() + override val node: T by lazy { buildNode() } + final override val valueProperty: SimpleObjectProperty = SimpleObjectProperty(Null) + final override val descriptorProperty: SimpleObjectProperty = SimpleObjectProperty() - override var descriptor: ValueDescriptor? by descriptorProperty + override var descriptor: MetaDescriptor? by descriptorProperty override var value: Value? by valueProperty - fun resetValue() { + public fun resetValue() { setDisplayValue(currentValue()) } @@ -36,7 +36,7 @@ abstract class ValueChooserBase : ValueChooser { * @return */ protected fun currentValue(): Value { - return value ?: descriptor?.default ?: Null + return value ?: descriptor?.defaultValue ?: Null } /** diff --git a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/VisualObjectEditorFragment.kt b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/VisionEditorFragment.kt similarity index 57% rename from visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/VisualObjectEditorFragment.kt rename to visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/VisionEditorFragment.kt index 929c7e85..3ce3b2de 100644 --- a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/VisualObjectEditorFragment.kt +++ b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/VisionEditorFragment.kt @@ -5,39 +5,36 @@ import javafx.beans.property.SimpleObjectProperty import javafx.scene.Node import javafx.scene.Parent import javafx.scene.layout.VBox -import space.kscience.dataforge.meta.Config -import space.kscience.dataforge.meta.Meta -import space.kscience.dataforge.meta.MutableItemProvider -import space.kscience.dataforge.meta.descriptors.NodeDescriptor -import space.kscience.dataforge.meta.update +import space.kscience.dataforge.meta.* +import space.kscience.dataforge.meta.descriptors.MetaDescriptor import space.kscience.visionforge.* import tornadofx.* -class VisualObjectEditorFragment(val selector: (Vision) -> Meta) : Fragment() { +public class VisionEditorFragment(public val selector: (Vision) -> Meta) : Fragment() { - val itemProperty = SimpleObjectProperty() - var item: Vision? by itemProperty - val descriptorProperty = SimpleObjectProperty() + public val itemProperty: SimpleObjectProperty = SimpleObjectProperty() + public var item: Vision? by itemProperty + public val descriptorProperty: SimpleObjectProperty = SimpleObjectProperty() - constructor( + public constructor( item: Vision?, - descriptor: NodeDescriptor?, - selector: (Vision) -> MutableItemProvider = { it.allProperties() }, + descriptor: MetaDescriptor?, + selector: (Vision) -> MutableMetaProvider = { it.meta() }, ) : this({ it.describedProperties }) { this.item = item this.descriptorProperty.set(descriptor) } - private var currentConfig: Config? = null + private var currentConfig: ObservableMutableMeta? = null - private val configProperty: Binding = itemProperty.objectBinding { visualObject -> - if (visualObject == null) return@objectBinding null - val meta = selector(visualObject) - val config = Config().apply { + private val configProperty: Binding = itemProperty.objectBinding { vision -> + if (vision == null) return@objectBinding null + val meta = selector(vision) + val config = MutableMeta { update(meta) - onChange(this@VisualObjectEditorFragment) { key, _, after -> - visualObject.setProperty(key, after) - } + } + config.onChange(this@VisionEditorFragment) { key -> + vision.setPropertyNode(key, config[key]) } //remember old config reference to cleanup listeners currentConfig?.removeListener(this) @@ -47,7 +44,7 @@ class VisualObjectEditorFragment(val selector: (Vision) -> Meta) : Fragment() { private val configEditorProperty: Binding = configProperty.objectBinding(descriptorProperty) { it?.let { - ConfigEditor(it, descriptorProperty.get()).root + MutableMetaEditor(it, descriptorProperty.get()).root } } diff --git a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/VisualObjectTreeFragment.kt b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/VisionTreeFragment.kt similarity index 82% rename from visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/VisualObjectTreeFragment.kt rename to visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/VisionTreeFragment.kt index f1faaf1a..65aa1b9c 100644 --- a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/VisualObjectTreeFragment.kt +++ b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/editor/VisionTreeFragment.kt @@ -3,6 +3,7 @@ package space.kscience.visionforge.editor import javafx.beans.property.SimpleObjectProperty import javafx.scene.control.SelectionMode import javafx.scene.control.TreeItem +import javafx.scene.layout.VBox import space.kscience.visionforge.Vision import space.kscience.visionforge.VisionGroup import tornadofx.* @@ -29,13 +30,13 @@ private fun toTreeItem(vision: Vision, title: String): TreeItem() - var item: Vision? by itemProperty +public class VisionTreeFragment : Fragment() { + public val itemProperty: SimpleObjectProperty = SimpleObjectProperty() + public var item: Vision? by itemProperty - val selectedProperty = SimpleObjectProperty() + public val selectedProperty: SimpleObjectProperty = SimpleObjectProperty() - override val root = vbox { + override val root: VBox = vbox { titledpane("Object tree", collapsible = false) { treeview> { cellFormat { diff --git a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/FXMaterials.kt b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/FXMaterials.kt index aa02d1ca..0b205626 100644 --- a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/FXMaterials.kt +++ b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/FXMaterials.kt @@ -3,11 +3,15 @@ package space.kscience.visionforge.solid import javafx.scene.paint.Color import javafx.scene.paint.Material import javafx.scene.paint.PhongMaterial -import space.kscience.dataforge.meta.* +import space.kscience.dataforge.meta.Meta +import space.kscience.dataforge.meta.double +import space.kscience.dataforge.meta.get +import space.kscience.dataforge.meta.int import space.kscience.dataforge.values.ValueType import space.kscience.dataforge.values.int import space.kscience.dataforge.values.string import space.kscience.visionforge.Colors +import space.kscience.visionforge.solid.FXMaterials.GREY public object FXMaterials { public val RED: PhongMaterial = PhongMaterial().apply { @@ -26,46 +30,43 @@ public object FXMaterials { } public val BLUE: PhongMaterial = PhongMaterial(Color.BLUE) + } /** * Infer color based on meta item * @param opacity default opacity */ -public fun MetaItem.color(opacity: Double = 1.0): Color { - return when (this) { - is MetaItemValue -> if (this.value.type == ValueType.NUMBER) { - val int = value.int +public fun Meta.color(opacity: Double = 1.0): Color { + return value?.let { + if (it.type == ValueType.NUMBER) { + val int = it.int val red = int and 0x00ff0000 shr 16 val green = int and 0x0000ff00 shr 8 val blue = int and 0x000000ff Color.rgb(red, green, blue, opacity) } else { - Color.web(this.value.string) + Color.web(it.string) } - is MetaItemNode -> { - Color.rgb( - node[Colors.RED_KEY]?.int ?: 0, - node[Colors.GREEN_KEY]?.int ?: 0, - node[Colors.BLUE_KEY]?.int ?: 0, - node[SolidMaterial.OPACITY_KEY]?.double ?: opacity - ) - } - } + } ?: Color.rgb( + this[Colors.RED_KEY]?.int ?: 0, + this[Colors.GREEN_KEY]?.int ?: 0, + this[Colors.BLUE_KEY]?.int ?: 0, + this[SolidMaterial.OPACITY_KEY]?.double ?: opacity + ) } /** * Infer FX material based on meta item */ -public fun MetaItem?.material(): Material { - return when (this) { - null -> FXMaterials.GREY - is MetaItemValue -> PhongMaterial(color()) - is MetaItemNode -> PhongMaterial().apply { - val opacity = node[SolidMaterial.OPACITY_KEY].double ?: 1.0 - diffuseColor = node[SolidMaterial.COLOR_KEY]?.color(opacity) ?: Color.DARKGREY - specularColor = node[SolidMaterial.SPECULAR_COLOR_KEY]?.color(opacity) ?: Color.WHITE - } +public fun Meta?.material(): Material { + if (this == null) return GREY + return value?.let { + PhongMaterial(color()) + } ?: PhongMaterial().apply { + val opacity = get(SolidMaterial.OPACITY_KEY).double ?: 1.0 + diffuseColor = get(SolidMaterial.COLOR_KEY)?.color(opacity) ?: Color.DARKGREY + specularColor = get(SolidMaterial.SPECULAR_COLOR_KEY)?.color(opacity) ?: Color.WHITE } } diff --git a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/FXReferenceFactory.kt b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/FXReferenceFactory.kt index dc72f7f1..d2863cea 100644 --- a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/FXReferenceFactory.kt +++ b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/FXReferenceFactory.kt @@ -2,7 +2,10 @@ package space.kscience.visionforge.solid import javafx.scene.Group import javafx.scene.Node -import space.kscience.dataforge.names.* +import space.kscience.dataforge.names.Name +import space.kscience.dataforge.names.cutFirst +import space.kscience.dataforge.names.firstOrNull +import space.kscience.dataforge.names.isEmpty import space.kscience.visionforge.Vision import space.kscience.visionforge.onPropertyChange import kotlin.reflect.KClass @@ -16,7 +19,7 @@ class FXReferenceFactory(val plugin: FX3DPlugin) : FX3DFactory if (name.firstOrNull()?.body == SolidReferenceGroup.REFERENCE_CHILD_PROPERTY_PREFIX) { - val childName = name.firstOrNull()?.index?.toName() ?: error("Wrong syntax for reference child property: '$name'") + val childName = name.firstOrNull()?.index?.let(Name::parse) ?: error("Wrong syntax for reference child property: '$name'") val propertyName = name.cutFirst() val referenceChild = obj[childName] ?: error("Reference child with name '$childName' not found") val child = node.findChild(childName) ?: error("Object child with name '$childName' not found") diff --git a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/VisualObjectFXBinding.kt b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/VisualObjectFXBinding.kt index 5045a375..492356ea 100644 --- a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/VisualObjectFXBinding.kt +++ b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/VisualObjectFXBinding.kt @@ -5,7 +5,6 @@ import javafx.beans.binding.* import space.kscience.dataforge.meta.* import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.startsWith -import space.kscience.dataforge.names.toName import space.kscience.dataforge.values.Value import space.kscience.visionforge.Vision import space.kscience.visionforge.onPropertyChange @@ -15,7 +14,7 @@ import tornadofx.* * A caching binding collection for [Vision] properties */ public class VisualObjectFXBinding(public val fx: FX3DPlugin, public val obj: Vision) { - private val bindings = HashMap>() + private val bindings = HashMap>() init { obj.onPropertyChange(fx.context) { name -> @@ -33,30 +32,29 @@ public class VisualObjectFXBinding(public val fx: FX3DPlugin, public val obj: Vi } } - public operator fun get(key: Name): ObjectBinding { + public operator fun get(key: Name): ObjectBinding { return bindings.getOrPut(key) { - object : ObjectBinding() { - override fun computeValue(): MetaItem? = obj.getProperty(key) + object : ObjectBinding() { + override fun computeValue(): Meta? = obj.getProperty(key) } } } - public operator fun get(key: String) = get(key.toName()) + public operator fun get(key: String): ObjectBinding = get(Name.parse(key)) } -public fun ObjectBinding.value(): Binding = objectBinding { it.value } -public fun ObjectBinding.string(): StringBinding = stringBinding { it.string } -public fun ObjectBinding.number(): Binding = objectBinding { it.number } -public fun ObjectBinding.double(): Binding = objectBinding { it.double } -public fun ObjectBinding.float(): Binding = objectBinding { it.float } -public fun ObjectBinding.int(): Binding = objectBinding { it.int } -public fun ObjectBinding.long(): Binding = objectBinding { it.long } -public fun ObjectBinding.node(): Binding = objectBinding { it.node } +public fun ObjectBinding.value(): Binding = objectBinding { it?.value } +public fun ObjectBinding.string(): StringBinding = stringBinding { it.string } +public fun ObjectBinding.number(): Binding = objectBinding { it.number } +public fun ObjectBinding.double(): Binding = objectBinding { it.double } +public fun ObjectBinding.float(): Binding = objectBinding { it.float } +public fun ObjectBinding.int(): Binding = objectBinding { it.int } +public fun ObjectBinding.long(): Binding = objectBinding { it.long } -public fun ObjectBinding.string(default: String): StringBinding = stringBinding { it.string ?: default } -public fun ObjectBinding.double(default: Double): DoubleBinding = doubleBinding { it.double ?: default } -public fun ObjectBinding.float(default: Float): FloatBinding = floatBinding { it.float ?: default } -public fun ObjectBinding.int(default: Int): IntegerBinding = integerBinding { it.int ?: default } -public fun ObjectBinding.long(default: Long): LongBinding = longBinding { it.long ?: default } +public fun ObjectBinding.string(default: String): StringBinding = stringBinding { it.string ?: default } +public fun ObjectBinding.double(default: Double): DoubleBinding = doubleBinding { it.double ?: default } +public fun ObjectBinding.float(default: Float): FloatBinding = floatBinding { it.float ?: default } +public fun ObjectBinding.int(default: Int): IntegerBinding = integerBinding { it.int ?: default } +public fun ObjectBinding.long(default: Long): LongBinding = longBinding { it.long ?: default } -public fun ObjectBinding.transform(transform: (MetaItem) -> T): Binding = objectBinding { it?.let(transform) } +public fun ObjectBinding.transform(transform: (Meta) -> T): Binding = objectBinding { it?.let(transform) } diff --git a/visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/GdmlTransformer.kt b/visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/GdmlTransformer.kt index 98b4509e..0a837919 100644 --- a/visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/GdmlTransformer.kt +++ b/visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/GdmlTransformer.kt @@ -1,12 +1,12 @@ package space.kscience.visionforge.gdml import space.kscience.dataforge.meta.Meta -import space.kscience.dataforge.meta.MetaBuilder +import space.kscience.dataforge.meta.MutableMeta import space.kscience.dataforge.misc.DFExperimental import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.asName import space.kscience.dataforge.names.plus -import space.kscience.dataforge.names.toName + import space.kscience.gdml.* import space.kscience.visionforge.* import space.kscience.visionforge.html.VisionOutput @@ -41,8 +41,8 @@ public class GdmlTransformer { internal val styleCache = HashMap() - public fun Solid.registerAndUseStyle(name: String, builder: MetaBuilder.() -> Unit) { - styleCache.getOrPut(name.toName()) { + public fun Solid.registerAndUseStyle(name: String, builder: MutableMeta.() -> Unit) { + styleCache.getOrPut(Name.parse(name)) { Meta(builder) } useStyle(name) @@ -118,7 +118,7 @@ private class GdmlTransformerEnv(val settings: GdmlTransformer) { private val proto = SolidGroup() private val solids = proto.group(solidsName) { - setProperty("edges.enabled", false) + setPropertyNode("edges.enabled", false) } private val referenceStore = HashMap>() diff --git a/visionforge-gdml/src/commonTest/kotlin/TestCubes.kt b/visionforge-gdml/src/commonTest/kotlin/TestCubes.kt index 9fb80095..0ca76fd1 100644 --- a/visionforge-gdml/src/commonTest/kotlin/TestCubes.kt +++ b/visionforge-gdml/src/commonTest/kotlin/TestCubes.kt @@ -1,7 +1,7 @@ package space.kscience.visionforge.gdml import space.kscience.dataforge.context.Context -import space.kscience.dataforge.names.toName +import space.kscience.dataforge.names.Name import space.kscience.gdml.* import space.kscience.visionforge.Vision import space.kscience.visionforge.get @@ -23,7 +23,7 @@ class TestCubes { fun testCubesDirect() { val vision = cubes.toVision() // println(Solids.encodeToString(vision)) - val smallBoxPrototype = vision.getPrototype("solids.smallBox".toName()) as? Box + val smallBoxPrototype = vision.getPrototype(Name.parse("solids.smallBox")) as? Box assertNotNull(smallBoxPrototype) assertEquals(30.0, smallBoxPrototype.xSize.toDouble()) val smallBoxVision = vision["composite-111.smallBox"]?.unref as? Box @@ -46,7 +46,7 @@ class TestCubes { val vision = cubes.toVision() val serialized = Solids.encodeToString(vision) val deserialized = testContext.visionManager.decodeFromString(serialized) as SolidGroup - val smallBox = deserialized.getPrototype("solids.smallBox".toName()) as? Box + val smallBox = deserialized.getPrototype(Name.parse("solids.smallBox")) as? Box assertNotNull(smallBox) assertEquals(30.0, smallBox.xSize.toDouble()) //println(testContext.visionManager.encodeToString(deserialized)) diff --git a/visionforge-gdml/src/jvmTest/kotlin/space/kscience/visionforge/gdml/TestConvertor.kt b/visionforge-gdml/src/jvmTest/kotlin/space/kscience/visionforge/gdml/TestConvertor.kt index 730adfa9..a8a078a8 100644 --- a/visionforge-gdml/src/jvmTest/kotlin/space/kscience/visionforge/gdml/TestConvertor.kt +++ b/visionforge-gdml/src/jvmTest/kotlin/space/kscience/visionforge/gdml/TestConvertor.kt @@ -1,7 +1,7 @@ package space.kscience.visionforge.gdml import org.junit.jupiter.api.Test -import space.kscience.dataforge.names.toName +import space.kscience.dataforge.names.Name import space.kscience.gdml.Gdml import space.kscience.gdml.decodeFromStream import space.kscience.visionforge.solid.Solids @@ -23,7 +23,7 @@ class TestConvertor { val stream = javaClass.getResourceAsStream("/gdml/cubes.gdml")!! val gdml = Gdml.decodeFromStream(stream) val vision = gdml.toVision() - assertNotNull(vision.getPrototype("solids.box".toName())) + assertNotNull(vision.getPrototype(Name.parse("solids.box"))) println(Solids.encodeToString(vision)) } diff --git a/visionforge-markdown/src/commonMain/kotlin/space/kscience/visionforge/markup/VisionOfMarkup.kt b/visionforge-markdown/src/commonMain/kotlin/space/kscience/visionforge/markup/VisionOfMarkup.kt index 689ed221..2103078d 100644 --- a/visionforge-markdown/src/commonMain/kotlin/space/kscience/visionforge/markup/VisionOfMarkup.kt +++ b/visionforge-markdown/src/commonMain/kotlin/space/kscience/visionforge/markup/VisionOfMarkup.kt @@ -7,7 +7,7 @@ import kotlinx.serialization.modules.polymorphic import kotlinx.serialization.modules.subclass import space.kscience.dataforge.meta.string import space.kscience.dataforge.names.Name -import space.kscience.dataforge.names.toName +import space.kscience.dataforge.names.asName import space.kscience.visionforge.Vision import space.kscience.visionforge.VisionBase import space.kscience.visionforge.setProperty @@ -27,7 +27,7 @@ public class VisionOfMarkup( } public companion object { - public val CONTENT_PROPERTY_KEY: Name = "content".toName() + public val CONTENT_PROPERTY_KEY: Name = "content".asName() public const val COMMONMARK_FORMAT: String = "markdown.commonmark" public const val GFM_FORMAT: String = "markdown.gfm" } diff --git a/visionforge-plotly/build.gradle.kts b/visionforge-plotly/build.gradle.kts index b62876ab..41e541db 100644 --- a/visionforge-plotly/build.gradle.kts +++ b/visionforge-plotly/build.gradle.kts @@ -2,7 +2,7 @@ plugins { id("ru.mipt.npm.gradle.mpp") } -val plotlyVersion = "0.4.3" +val plotlyVersion = "0.5.0-dev-1" kscience { useSerialization() diff --git a/visionforge-plotly/src/commonMain/kotlin/space/kscience/visionforge/plotly/VisionOfPlotly.kt b/visionforge-plotly/src/commonMain/kotlin/space/kscience/visionforge/plotly/VisionOfPlotly.kt index 8603971e..7b2f1c25 100644 --- a/visionforge-plotly/src/commonMain/kotlin/space/kscience/visionforge/plotly/VisionOfPlotly.kt +++ b/visionforge-plotly/src/commonMain/kotlin/space/kscience/visionforge/plotly/VisionOfPlotly.kt @@ -2,6 +2,8 @@ package space.kscience.visionforge.plotly import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +import space.kscience.dataforge.meta.MutableMeta +import space.kscience.dataforge.meta.asObservable import space.kscience.dataforge.misc.DFExperimental import space.kscience.plotly.Plot import space.kscience.plotly.Plotly @@ -13,10 +15,10 @@ import space.kscience.visionforge.root @SerialName("vision.plotly") public class VisionOfPlotly private constructor() : VisionBase() { public constructor(plot: Plot) : this() { - properties = plot.config + properties = plot.meta } - public val plot: Plot get() = Plot(properties ?: Config()) + public val plot: Plot get() = Plot(properties?.asObservable() ?: MutableMeta()) } public fun Plot.asVision(): VisionOfPlotly = VisionOfPlotly(this) diff --git a/visionforge-plotly/src/jsMain/kotlin/space/kscience/visionforge/plotly/plotlyJs.kt b/visionforge-plotly/src/jsMain/kotlin/space/kscience/visionforge/plotly/plotlyJs.kt index be9e1e08..21b447ef 100644 --- a/visionforge-plotly/src/jsMain/kotlin/space/kscience/visionforge/plotly/plotlyJs.kt +++ b/visionforge-plotly/src/jsMain/kotlin/space/kscience/visionforge/plotly/plotlyJs.kt @@ -31,7 +31,7 @@ public actual class PlotlyPlugin : VisionPlugin(), ElementVisionRenderer { override fun render(element: Element, vision: Vision, meta: Meta) { val plot = (vision as? VisionOfPlotly)?.plot ?: error("VisionOfPlotly expected but ${vision::class} found") val config = PlotlyConfig.read(meta) - println(plot.config) + println(plot.meta) println(plot.data[0].toMeta()) element.plot(plot, config) } diff --git a/visionforge-server/src/main/kotlin/space/kscience/visionforge/three/server/VisionServer.kt b/visionforge-server/src/main/kotlin/space/kscience/visionforge/three/server/VisionServer.kt index fabb0675..0a9c60e3 100644 --- a/visionforge-server/src/main/kotlin/space/kscience/visionforge/three/server/VisionServer.kt +++ b/visionforge-server/src/main/kotlin/space/kscience/visionforge/three/server/VisionServer.kt @@ -30,7 +30,6 @@ import space.kscience.dataforge.context.fetch import space.kscience.dataforge.meta.* import space.kscience.dataforge.misc.DFExperimental import space.kscience.dataforge.names.Name -import space.kscience.dataforge.names.toName import space.kscience.visionforge.Vision import space.kscience.visionforge.VisionChange import space.kscience.visionforge.VisionManager @@ -54,12 +53,12 @@ public class VisionServer internal constructor( private val application: Application, private val rootRoute: String, ) : Configurable, CoroutineScope by application { - override val config: ObservableMeta = ObservableMeta() - public var updateInterval: Long by config.long(300, key = UPDATE_INTERVAL_KEY) - public var cacheFragments: Boolean by config.boolean(true) - public var dataEmbed: Boolean by config.boolean(true, "data.embed".toName()) - public var dataFetch: Boolean by config.boolean(false, "data.fetch".toName()) - public var dataConnect: Boolean by config.boolean(true, "data.connect".toName()) + override val meta: ObservableMutableMeta = MutableMeta() + public var updateInterval: Long by meta.long(300, key = UPDATE_INTERVAL_KEY) + public var cacheFragments: Boolean by meta.boolean(true) + public var dataEmbed: Boolean by meta.boolean(true, Name.parse("data.embed")) + public var dataFetch: Boolean by meta.boolean(false, Name.parse("data.fetch")) + public var dataConnect: Boolean by meta.boolean(true, Name.parse("data.connect")) /** * a list of headers that should be applied to all pages @@ -131,14 +130,14 @@ public class VisionServer internal constructor( ?: error("Vision name is not defined in parameters") application.log.debug("Opened server socket for $name") - val vision: Vision = visions[name.toName()] ?: error("Plot with id='$name' not registered") + val vision: Vision = visions[Name.parse(name)] ?: error("Plot with id='$name' not registered") launch { incoming.consumeEach { val change = visionManager.jsonFormat.decodeFromString( VisionChange.serializer(), it.data.decodeToString() ) - vision.update(change) + vision.change(change) } } @@ -161,7 +160,7 @@ public class VisionServer internal constructor( val name: String = call.request.queryParameters["name"] ?: error("Vision name is not defined in parameters") - val vision: Vision? = visions[name.toName()] + val vision: Vision? = visions[Name.parse(name)] if (vision == null) { call.respond(HttpStatusCode.NotFound, "Vision with name '$name' not found") } else { @@ -232,7 +231,7 @@ public class VisionServer internal constructor( public companion object { public const val DEFAULT_PAGE: String = "/" - public val UPDATE_INTERVAL_KEY: Name = "update.interval".toName() + public val UPDATE_INTERVAL_KEY: Name = Name.parse("update.interval") } } diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/ColorAccessor.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/ColorAccessor.kt index 9232874c..66efe69b 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/ColorAccessor.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/ColorAccessor.kt @@ -1,8 +1,6 @@ package space.kscience.visionforge.solid -import space.kscience.dataforge.meta.MutableItemProvider -import space.kscience.dataforge.meta.set -import space.kscience.dataforge.meta.value +import space.kscience.dataforge.meta.* import space.kscience.dataforge.names.Name import space.kscience.dataforge.values.Value import space.kscience.dataforge.values.asValue @@ -11,11 +9,17 @@ import space.kscience.visionforge.Colors import space.kscience.visionforge.VisionBuilder @VisionBuilder -public class ColorAccessor(private val parent: MutableItemProvider, private val colorKey: Name) { +public class ColorAccessor(private val parent: MutableMetaProvider, private val colorKey: Name) { public var value: Value? - get() = parent.getItem(colorKey).value + get() = parent.getMeta(colorKey)?.value set(value) { - parent[colorKey] = value + parent.setValue(colorKey,value) + } + + public var item: Meta? + get() = parent.getMeta(colorKey) + set(value) { + parent.setMeta(colorKey,value) } } diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Composite.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Composite.kt index d1d41d09..4017b11b 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Composite.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Composite.kt @@ -31,7 +31,7 @@ public inline fun VisionContainerBuilder.composite( if (children.size != 2) error("Composite requires exactly two children") return Composite(type, children[0], children[1]).also { composite -> composite.configure { - update(group.meta) + update(group.meta()) } if (group.position != null) { composite.position = group.position diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Extruded.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Extruded.kt index d94062d8..0ecc02b0 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Extruded.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Extruded.kt @@ -2,7 +2,8 @@ package space.kscience.visionforge.solid import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import space.kscience.dataforge.meta.ObservableMeta +import space.kscience.dataforge.meta.MutableMeta +import space.kscience.dataforge.meta.ObservableMutableMeta import space.kscience.visionforge.* import kotlin.math.PI import kotlin.math.cos @@ -95,7 +96,7 @@ public class Extruded( public class ExtrudeBuilder( public var shape: List = emptyList(), public var layers: ArrayList = ArrayList(), - config: ObservableMeta = ObservableMeta() + config: ObservableMutableMeta = MutableMeta() ) : SimpleVisionPropertyContainer(config) { public fun shape(block: Shape2DBuilder.() -> Unit) { this.shape = Shape2DBuilder().apply(block).build() @@ -105,7 +106,7 @@ public class ExtrudeBuilder( layers.add(Layer(x.toFloat(), y.toFloat(), z.toFloat(), scale.toFloat())) } - internal fun build(): Extruded = Extruded(shape, layers).apply { configure(config) } + internal fun build(): Extruded = Extruded(shape, layers).apply { configure(meta()) } } @VisionBuilder diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/PolyLine.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/PolyLine.kt index cd9f2cbd..d8aa937c 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/PolyLine.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/PolyLine.kt @@ -2,13 +2,12 @@ package space.kscience.visionforge.solid import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import space.kscience.dataforge.meta.number import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.asName import space.kscience.dataforge.names.plus import space.kscience.visionforge.VisionBuilder import space.kscience.visionforge.VisionContainerBuilder -import space.kscience.visionforge.allProperties +import space.kscience.visionforge.numberProperty import space.kscience.visionforge.set @Serializable @@ -16,8 +15,8 @@ import space.kscience.visionforge.set public class PolyLine(public val points: List) : SolidBase(), Solid { //var lineType by string() - public var thickness: Number by allProperties(inherit = false).number(1.0, - key = SolidMaterial.MATERIAL_KEY + THICKNESS_KEY) + public var thickness: Number by numberProperty(name = SolidMaterial.MATERIAL_KEY + THICKNESS_KEY) { 1.0 } + public companion object { public val THICKNESS_KEY: Name = "thickness".asName() diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Solid.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Solid.kt index a4c8e69c..f2ad38fd 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Solid.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Solid.kt @@ -1,7 +1,7 @@ package space.kscience.visionforge.solid import space.kscience.dataforge.meta.* -import space.kscience.dataforge.meta.descriptors.NodeDescriptor +import space.kscience.dataforge.meta.descriptors.* import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.asName import space.kscience.dataforge.names.plus @@ -35,7 +35,7 @@ import kotlin.reflect.KProperty */ public interface Solid : Vision { - override val descriptor: NodeDescriptor get() = Companion.descriptor + override val descriptor: MetaDescriptor get() = Companion.descriptor public companion object { // val SELECTED_KEY = "selected".asName() @@ -69,39 +69,38 @@ public interface Solid : Vision { public val Y_SCALE_KEY: Name = SCALE_KEY + Y_KEY public val Z_SCALE_KEY: Name = SCALE_KEY + Z_KEY - public val descriptor: NodeDescriptor by lazy { - NodeDescriptor { - value(VISIBLE_KEY) { + public val descriptor: MetaDescriptor by lazy { + MetaDescriptor { + value(VISIBLE_KEY, ValueType.BOOLEAN) { inherited = false - type(ValueType.BOOLEAN) default(true) } //TODO replace by descriptor merge - value(Vision.STYLE_KEY) { - type(ValueType.STRING) + value(Vision.STYLE_KEY, ValueType.STRING) { multiple = true hide() } - node(POSITION_KEY){ + node(POSITION_KEY) { hide() } - node(ROTATION_KEY){ + node(ROTATION_KEY) { hide() } - node(SCALE_KEY){ + node(SCALE_KEY) { hide() } - value(DETAIL_KEY) { - type(ValueType.NUMBER) + value(DETAIL_KEY, ValueType.NUMBER) { hide() } - item(SolidMaterial.MATERIAL_KEY.toString(), SolidMaterial.descriptor) + item(SolidMaterial.MATERIAL_KEY.toString(), SolidMaterial){ + valueRequirement = ValueRequirement.ABSENT + } enum(ROTATION_ORDER_KEY, default = RotationOrder.XYZ) { hide() @@ -115,7 +114,7 @@ public interface Solid : Vision { * Get the layer number this solid belongs to. Return 0 if layer is not defined. */ public var Solid.layer: Int - get() = allProperties().getItem(LAYER_KEY).int ?: 0 + get() = getProperty(LAYER_KEY, inherit = true).int ?: 0 set(value) { setProperty(LAYER_KEY, value) } @@ -136,7 +135,7 @@ public enum class RotationOrder { */ public var Solid.rotationOrder: RotationOrder get() = getProperty(Solid.ROTATION_ORDER_KEY).enum() ?: RotationOrder.XYZ - set(value) = setProperty(Solid.ROTATION_ORDER_KEY, value.name.asValue()) + set(value) = setPropertyValue(Solid.ROTATION_ORDER_KEY, value.name.asValue()) /** @@ -144,7 +143,7 @@ public var Solid.rotationOrder: RotationOrder */ public var Solid.detail: Int? get() = getProperty(DETAIL_KEY, false).int - set(value) = setProperty(DETAIL_KEY, value?.asValue()) + set(value) = setPropertyValue(DETAIL_KEY, value?.asValue()) /** * If this property is true, the object will be ignored on render. @@ -152,7 +151,7 @@ public var Solid.detail: Int? */ public var Vision.ignore: Boolean? get() = getProperty(IGNORE_KEY, false).boolean - set(value) = setProperty(IGNORE_KEY, value?.asValue()) + set(value) = setPropertyValue(IGNORE_KEY, value?.asValue()) //var VisualObject.selected: Boolean? // get() = getProperty(SELECTED_KEY).boolean @@ -182,7 +181,7 @@ internal fun point(name: Name, default: Float): ReadWriteProperty, value: Point3D?) { if (value == null) { - thisRef.setProperty(name, null) + thisRef.setPropertyNode(name, null) } else { thisRef.setProperty(name + X_KEY, value.x) thisRef.setProperty(name + Y_KEY, value.y) diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidBase.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidBase.kt index d2fee33a..2beee1ed 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidBase.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidBase.kt @@ -2,17 +2,17 @@ package space.kscience.visionforge.solid import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import space.kscience.dataforge.meta.descriptors.NodeDescriptor +import space.kscience.dataforge.meta.descriptors.MetaDescriptor import space.kscience.visionforge.VisionBase import space.kscience.visionforge.VisionChange @Serializable @SerialName("solid") public open class SolidBase : VisionBase(), Solid { - override val descriptor: NodeDescriptor get() = Solid.descriptor + override val descriptor: MetaDescriptor get() = Solid.descriptor - override fun update(change: VisionChange) { + override fun change(change: VisionChange) { updatePosition(change.properties) - super.update(change) + super.change(change) } } diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidGroup.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidGroup.kt index b78ec4a5..bc6676f6 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidGroup.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidGroup.kt @@ -2,7 +2,7 @@ package space.kscience.visionforge.solid import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import space.kscience.dataforge.meta.descriptors.NodeDescriptor +import space.kscience.dataforge.meta.descriptors.MetaDescriptor import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.NameToken import space.kscience.visionforge.* @@ -40,7 +40,7 @@ public class SolidGroup : VisionGroupBase(), Solid, PrototypeHolder { } - override val descriptor: NodeDescriptor get() = Solid.descriptor + override val descriptor: MetaDescriptor get() = Solid.descriptor /** * Get a prototype redirecting the request to the parent if prototype is not found. @@ -60,9 +60,9 @@ public class SolidGroup : VisionGroupBase(), Solid, PrototypeHolder { override fun createGroup(): SolidGroup = SolidGroup() - override fun update(change: VisionChange) { + override fun change(change: VisionChange) { updatePosition(change.properties) - super.update(change) + super.change(change) } public companion object { diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidMaterial.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidMaterial.kt index aa6c9301..3d9843a3 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidMaterial.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidMaterial.kt @@ -1,8 +1,8 @@ package space.kscience.visionforge.solid import space.kscience.dataforge.meta.* -import space.kscience.dataforge.meta.descriptors.NodeDescriptor -import space.kscience.dataforge.meta.descriptors.attributes +import space.kscience.dataforge.meta.descriptors.MetaDescriptor +import space.kscience.dataforge.meta.descriptors.value import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.asName import space.kscience.dataforge.names.plus @@ -26,6 +26,8 @@ public class SolidMaterial : Scheme() { */ public val specularColor: ColorAccessor = ColorAccessor(this, SPECULAR_COLOR_KEY) + public val emissiveColor: ColorAccessor = ColorAccessor(this, "emissiveColor".asName()) + /** * Opacity */ @@ -48,43 +50,37 @@ public class SolidMaterial : Scheme() { public val WIREFRAME_KEY: Name = "wireframe".asName() public val MATERIAL_WIREFRAME_KEY: Name = MATERIAL_KEY + WIREFRAME_KEY - public override val descriptor: NodeDescriptor by lazy { + public override val descriptor: MetaDescriptor by lazy { //must be lazy to avoid initialization bug - NodeDescriptor { + MetaDescriptor { inherited = true usesStyles = true - value(COLOR_KEY) { + value(COLOR_KEY, ValueType.STRING, ValueType.NUMBER) { inherited = true usesStyles = true - type(ValueType.STRING, ValueType.NUMBER) widgetType = "color" } - value(SPECULAR_COLOR_KEY) { + value(SPECULAR_COLOR_KEY, ValueType.STRING, ValueType.NUMBER) { inherited = true usesStyles = true - type(ValueType.STRING, ValueType.NUMBER) widgetType = "color" hide() } - value(OPACITY_KEY) { + value(OPACITY_KEY, ValueType.NUMBER) { inherited = true usesStyles = true - type(ValueType.NUMBER) default(1.0) - attributes { - this["min"] = 0.0 - this["max"] = 1.0 - this["step"] = 0.1 - } + attributes["min"] = 0.0 + attributes["max"] = 1.0 + attributes["step"] = 0.1 widgetType = "slider" } - value(WIREFRAME_KEY) { + value(WIREFRAME_KEY, ValueType.BOOLEAN) { inherited = true usesStyles = true - type(ValueType.BOOLEAN) default(false) } } @@ -94,21 +90,23 @@ public class SolidMaterial : Scheme() { public val Solid.color: ColorAccessor get() = ColorAccessor( - allProperties(inherit = true), + meta(inherit = true), MATERIAL_COLOR_KEY ) public var Solid.material: SolidMaterial? - get() = getProperty(MATERIAL_KEY, inherit = true).node?.let { SolidMaterial.read(it) } - set(value) = setProperty(MATERIAL_KEY, value?.rootNode) + get() = getProperty(MATERIAL_KEY, inherit = true)?.let { SolidMaterial.read(it) } + set(value) = setPropertyNode(MATERIAL_KEY, value?.meta) @VisionBuilder public fun Solid.material(builder: SolidMaterial.() -> Unit) { - ownProperties.getChild(MATERIAL_KEY).update(SolidMaterial, builder) + configure(MATERIAL_KEY){ + updateWith(SolidMaterial,builder) + } } public var Solid.opacity: Number? get() = getProperty(MATERIAL_OPACITY_KEY, inherit = true).number set(value) { - setProperty(MATERIAL_OPACITY_KEY, value?.asValue()) + setPropertyValue(MATERIAL_OPACITY_KEY, value?.asValue()) } \ No newline at end of file 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 1cb5eac8..0149fc88 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 @@ -4,11 +4,11 @@ import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.mapNotNull import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import space.kscience.dataforge.meta.MetaItem -import space.kscience.dataforge.meta.asMetaItem -import space.kscience.dataforge.meta.descriptors.NodeDescriptor +import space.kscience.dataforge.meta.Meta +import space.kscience.dataforge.meta.descriptors.MetaDescriptor import space.kscience.dataforge.misc.DFExperimental import space.kscience.dataforge.names.* +import space.kscience.dataforge.values.Value import space.kscience.visionforge.* @@ -37,7 +37,7 @@ private fun SolidReference.getRefProperty( inherit: Boolean, includeStyles: Boolean, includeDefaults: Boolean, -): MetaItem? = if (!inherit && !includeStyles && !includeDefaults) { +): Meta? = if (!inherit && !includeStyles && !includeDefaults) { getOwnProperty(name) } else { buildList { @@ -88,9 +88,9 @@ public class SolidReferenceGroup( inherit: Boolean, includeStyles: Boolean, includeDefaults: Boolean, - ): MetaItem? = getRefProperty(name, inherit, includeStyles, includeDefaults) + ): Meta? = getRefProperty(name, inherit, includeStyles, includeDefaults) - override val descriptor: NodeDescriptor get() = prototype.descriptor + override val descriptor: MetaDescriptor get() = prototype.descriptor /** @@ -118,11 +118,15 @@ public class SolidReferenceGroup( ReferenceChild(owner, refName + key.asName()) } ?: emptyMap() - override fun getOwnProperty(name: Name): MetaItem? = + override fun getOwnProperty(name: Name): Meta? = owner.getOwnProperty(childPropertyName(refName, name)) - override fun setProperty(name: Name, item: MetaItem?, notify: Boolean) { - owner.setProperty(childPropertyName(refName, name), item, notify) + override fun setPropertyNode(name: Name, node: Meta?, notify: Boolean) { + owner.setPropertyNode(childPropertyName(refName, name), node, notify) + } + + override fun setPropertyValue(name: Name, value: Value?, notify: Boolean) { + owner.setPropertyValue(childPropertyName(refName, name), value, notify) } override fun getProperty( @@ -130,7 +134,7 @@ public class SolidReferenceGroup( inherit: Boolean, includeStyles: Boolean, includeDefaults: Boolean, - ): MetaItem? = getRefProperty(name, inherit, includeStyles, includeDefaults) + ): Meta? = getRefProperty(name, inherit, includeStyles, includeDefaults) override var parent: VisionGroup? get() { @@ -155,13 +159,13 @@ public class SolidReferenceGroup( owner.invalidateProperty(childPropertyName(refName, propertyName)) } - override fun update(change: VisionChange) { + override fun change(change: VisionChange) { change.properties?.let { - updateProperties(Name.EMPTY, it.asMetaItem()) + updateProperties(Name.EMPTY, it) } } - override val descriptor: NodeDescriptor get() = prototype.descriptor + override val descriptor: MetaDescriptor get() = prototype.descriptor } @@ -184,7 +188,7 @@ public fun SolidGroup.ref( public fun SolidGroup.ref( name: String, obj: Solid, - templateName: Name = name.toName(), + templateName: Name = Name.parse(name), ): SolidReferenceGroup { val existing = getPrototype(templateName) if (existing == null) { diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/geometry.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/geometry.kt index f5aa2d4a..31611dc9 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/geometry.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/geometry.kt @@ -95,13 +95,13 @@ public fun MutablePoint3D.normalizeInPlace() { z /= norm } -internal fun ItemProvider.point3D(default: Float = 0f) = object : Point3D { +internal fun MetaProvider.point3D(default: Float = 0f) = object : Point3D { override val x: Float by float(default) override val y: Float by float(default) override val z: Float by float(default) } -public fun Point3D.toMeta(): MetaBuilder = Meta { +public fun Point3D.toMeta(): Meta = Meta { X_KEY put x Y_KEY put y Z_KEY put z @@ -115,7 +115,7 @@ internal fun Meta.toVector(default: Float = 0f) = Point3D( ) internal fun Solid.updatePosition(meta: Meta?) { - meta[Solid.POSITION_KEY].node?.toVector()?.let { position = it } - meta[Solid.ROTATION_KEY].node?.toVector()?.let { rotation = it } - meta[Solid.SCALE_KEY].node?.toVector(1f)?.let { scale = it } + meta?.get(Solid.POSITION_KEY)?.toVector()?.let { position = it } + meta?.get(Solid.ROTATION_KEY)?.toVector()?.let { rotation = it } + meta?.get(Solid.SCALE_KEY)?.toVector(1f)?.let { scale = it } } \ No newline at end of file diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/specifications/Axes.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/specifications/Axes.kt index 2f3e1645..8f980dad 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/specifications/Axes.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/specifications/Axes.kt @@ -3,7 +3,7 @@ package space.kscience.visionforge.solid.specifications import space.kscience.dataforge.meta.Scheme import space.kscience.dataforge.meta.SchemeSpec import space.kscience.dataforge.meta.boolean -import space.kscience.dataforge.meta.descriptors.NodeDescriptor +import space.kscience.dataforge.meta.descriptors.MetaDescriptor import space.kscience.dataforge.meta.double import space.kscience.visionforge.value @@ -16,8 +16,8 @@ public class Axes : Scheme() { public const val AXIS_SIZE: Double = 1000.0 public const val AXIS_WIDTH: Double = 3.0 - override val descriptor: NodeDescriptor by lazy { - NodeDescriptor { + override val descriptor: MetaDescriptor by lazy { + MetaDescriptor { value(Axes::visible){ default(false) } diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/specifications/Camera.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/specifications/Camera.kt index 905fa234..2319fa69 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/specifications/Camera.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/specifications/Camera.kt @@ -2,7 +2,7 @@ package space.kscience.visionforge.solid.specifications import space.kscience.dataforge.meta.Scheme import space.kscience.dataforge.meta.SchemeSpec -import space.kscience.dataforge.meta.descriptors.NodeDescriptor +import space.kscience.dataforge.meta.descriptors.MetaDescriptor import space.kscience.dataforge.meta.double import space.kscience.dataforge.meta.int import space.kscience.visionforge.value @@ -27,8 +27,8 @@ public class Camera : Scheme() { public const val FAR_CLIP: Double = 10000.0 public const val FIELD_OF_VIEW: Int = 75 - override val descriptor: NodeDescriptor by lazy { - NodeDescriptor { + override val descriptor: MetaDescriptor by lazy { + MetaDescriptor { value(Camera::fov){ default(FIELD_OF_VIEW) } diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/specifications/Canvas3DOptions.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/specifications/Canvas3DOptions.kt index 11dd5800..ea546821 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/specifications/Canvas3DOptions.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/specifications/Canvas3DOptions.kt @@ -1,8 +1,7 @@ package space.kscience.visionforge.solid.specifications import space.kscience.dataforge.meta.* -import space.kscience.dataforge.meta.descriptors.NodeDescriptor -import space.kscience.dataforge.meta.descriptors.attributes +import space.kscience.dataforge.meta.descriptors.MetaDescriptor import space.kscience.dataforge.names.Name import space.kscience.dataforge.values.ValueType import space.kscience.visionforge.hide @@ -16,30 +15,26 @@ public class Clipping : Scheme() { public var z: Double? by double() public companion object : SchemeSpec(::Clipping) { - override val descriptor: NodeDescriptor = NodeDescriptor { + override val descriptor: MetaDescriptor = MetaDescriptor { value(Clipping::x) { widgetType = "range" - attributes { - set("min", 0.0) - set("max", 1.0) - set("step", 0.01) - } + attributes["min"] = 0.0 + attributes["max"] = 1.0 + attributes["step"] = 0.01 } value(Clipping::y) { widgetType = "range" - attributes { - set("min", 0.0) - set("max", 1.0) - set("step", 0.01) - } + attributes["min"] = 0.0 + attributes["max"] = 1.0 + attributes["step"] = 0.01 + } value(Clipping::z) { widgetType = "range" - attributes { - set("min", 0.0) - set("max", 1.0) - set("step", 0.01) - } + attributes["min"] = 0.0 + attributes["max"] = 1.0 + attributes["step"] = 0.01 + } } } @@ -55,7 +50,7 @@ public class CanvasSize : Scheme() { public var maxHeight: Number by number { maxSize } public companion object : SchemeSpec(::CanvasSize) { - override val descriptor: NodeDescriptor = NodeDescriptor { + override val descriptor: MetaDescriptor = MetaDescriptor { value(CanvasSize::minSize) value(CanvasSize::minWith) value(CanvasSize::minHeight) @@ -82,8 +77,8 @@ public class Canvas3DOptions : Scheme() { public companion object : SchemeSpec(::Canvas3DOptions) { - override val descriptor: NodeDescriptor by lazy { - NodeDescriptor { + override val descriptor: MetaDescriptor by lazy { + MetaDescriptor { scheme(Canvas3DOptions::axes, Axes) scheme(Canvas3DOptions::light, Light) @@ -104,7 +99,7 @@ public class Canvas3DOptions : Scheme() { multiple = true default(listOf(0)) widgetType = "multiSelect" - allow(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) + allowedValues(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) } scheme(Canvas3DOptions::clipping, Clipping) } diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/transform/RemoveSingleChild.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/transform/RemoveSingleChild.kt index a51aaa25..e623ef46 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/transform/RemoveSingleChild.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/transform/RemoveSingleChild.kt @@ -1,12 +1,9 @@ package space.kscience.visionforge.solid.transform -import space.kscience.dataforge.meta.itemSequence +import space.kscience.dataforge.meta.update import space.kscience.dataforge.misc.DFExperimental import space.kscience.dataforge.names.asName -import space.kscience.visionforge.MutableVisionGroup -import space.kscience.visionforge.Vision -import space.kscience.visionforge.VisionGroup -import space.kscience.visionforge.meta +import space.kscience.visionforge.* import space.kscience.visionforge.solid.* private operator fun Number.plus(other: Number) = toFloat() + other.toFloat() @@ -24,10 +21,8 @@ internal fun Vision.updateFrom(other: Vision): Vision { scaleX *= other.scaleX scaleY *= other.scaleY scaleZ *= other.scaleZ - other.meta.itemSequence().forEach { (name, item) -> - if (getProperty(name) == null) { - setProperty(name, item) - } + configure{ + update(other.meta()) } } return this diff --git a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/ConvexTest.kt b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/ConvexTest.kt index 58cde48e..a7133006 100644 --- a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/ConvexTest.kt +++ b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/ConvexTest.kt @@ -1,9 +1,8 @@ package space.kscience.visionforge.solid -import space.kscience.dataforge.meta.MetaItemNode import space.kscience.dataforge.meta.getIndexed import space.kscience.dataforge.meta.node -import space.kscience.dataforge.meta.toMetaItem +import space.kscience.dataforge.meta.toMeta import space.kscience.dataforge.misc.DFExperimental import kotlin.test.Test import kotlin.test.assertEquals @@ -29,9 +28,9 @@ class ConvexTest { val convex = group.children.values.first() as Convex val json = Solids.jsonForSolids.encodeToJsonElement(Convex.serializer(), convex) - val meta = json.toMetaItem().node!! + val meta = json.toMeta() - val points = meta.getIndexed("points").values.map { (it as MetaItemNode<*>).node.point3D() } + val points = meta.getIndexed("points").values.map { it.point3D() } assertEquals(8, points.count()) assertEquals(8, convex.points.size) diff --git a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/DescriptorTest.kt b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/DescriptorTest.kt index 95f6b23e..6f004f5c 100644 --- a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/DescriptorTest.kt +++ b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/DescriptorTest.kt @@ -1,6 +1,5 @@ package space.kscience.visionforge.solid -import space.kscience.dataforge.meta.descriptors.ValueDescriptor import space.kscience.dataforge.meta.descriptors.get import space.kscience.dataforge.values.ValueType import space.kscience.visionforge.solid.specifications.Canvas3DOptions @@ -16,7 +15,7 @@ class DescriptorTest { val axesSize = descriptor["axes.size"] assertNotNull(axesSize) assertTrue { - ValueType.NUMBER in (axesSize as ValueDescriptor).type!! + ValueType.NUMBER in axesSize.valueTypes!! } } } \ No newline at end of file diff --git a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/PropertyTest.kt b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/PropertyTest.kt index 1a9aa45b..a31ed9f8 100644 --- a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/PropertyTest.kt +++ b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/PropertyTest.kt @@ -12,7 +12,7 @@ class PropertyTest { fun testInheritedProperty() { var box: Box? = null val group = SolidGroup().apply { - setProperty("test", 22) + setPropertyNode("test", 22) group { box = box(100, 100, 100) } diff --git a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/SerializationTest.kt b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/SerializationTest.kt index 6df7b285..d878881f 100644 --- a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/SerializationTest.kt +++ b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/SerializationTest.kt @@ -1,7 +1,6 @@ package space.kscience.visionforge.solid import space.kscience.dataforge.names.Name -import space.kscience.dataforge.names.toName import space.kscience.visionforge.MutableVisionGroup import space.kscience.visionforge.get import space.kscience.visionforge.meta @@ -14,7 +13,7 @@ import kotlin.test.assertEquals */ fun SolidGroup.refGroup( name: String, - templateName: Name = name.toName(), + templateName: Name = Name.parse(name), block: MutableVisionGroup.() -> Unit ): SolidReferenceGroup { val group = SolidGroup().apply(block) @@ -30,10 +29,10 @@ class SerializationTest { x = 100 z = -100 } - val string = Solids.encodeToString(cube) + val string = Solids.encodeToString(cube) println(string) val newCube = Solids.decodeFromString(string) - assertEquals(cube.meta, newCube.meta) + assertEquals(cube.meta(), newCube.meta()) } @Test @@ -43,10 +42,10 @@ class SerializationTest { x = 100 z = -100 } - val group = SolidGroup{ + val group = SolidGroup { ref("cube", cube) - refGroup("pg", "pg.content".toName()){ - sphere(50){ + refGroup("pg", Name.parse("pg.content")) { + sphere(50) { x = -100 } } @@ -54,7 +53,7 @@ class SerializationTest { val string = Solids.encodeToString(group) println(string) val reconstructed = Solids.decodeFromString(string) as SolidGroup - assertEquals(group["cube"]?.meta, reconstructed["cube"]?.meta) + assertEquals(group["cube"]?.meta(), reconstructed["cube"]?.meta()) } } \ No newline at end of file diff --git a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/VisionUpdateTest.kt b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/VisionUpdateTest.kt index 2e87adf8..821e7f04 100644 --- a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/VisionUpdateTest.kt +++ b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/VisionUpdateTest.kt @@ -2,8 +2,9 @@ package space.kscience.visionforge.solid import space.kscience.dataforge.context.Global import space.kscience.dataforge.context.fetch -import space.kscience.dataforge.meta.MetaItem -import space.kscience.dataforge.names.toName +import space.kscience.dataforge.meta.Meta +import space.kscience.dataforge.names.asName +import space.kscience.dataforge.values.asValue import space.kscience.visionforge.VisionChange import space.kscience.visionforge.get import kotlin.test.Test @@ -24,10 +25,10 @@ class VisionUpdateTest { color(123) box(100,100,100) } - propertyChanged("top".toName(), SolidMaterial.MATERIAL_COLOR_KEY, MetaItem.of("red")) - propertyChanged("origin".toName(), SolidMaterial.MATERIAL_COLOR_KEY, MetaItem.of("red")) + propertyChanged("top".asName(), SolidMaterial.MATERIAL_COLOR_KEY, Meta("red".asValue())) + propertyChanged("origin".asName(), SolidMaterial.MATERIAL_COLOR_KEY, Meta("red".asValue())) } - targetVision.update(dif) + targetVision.change(dif) assertTrue { targetVision["top"] is SolidGroup } assertEquals("red", (targetVision["origin"] as Solid).color.string) // Should work assertEquals("#00007b", (targetVision["top"] as Solid).color.string) // new item always takes precedence @@ -40,8 +41,8 @@ class VisionUpdateTest { color(123) box(100,100,100) } - propertyChanged("top".toName(), SolidMaterial.MATERIAL_COLOR_KEY, MetaItem.of("red")) - propertyChanged("origin".toName(), SolidMaterial.MATERIAL_COLOR_KEY, MetaItem.of("red")) + propertyChanged("top".asName(), SolidMaterial.MATERIAL_COLOR_KEY, Meta("red".asValue())) + propertyChanged("origin".asName(), SolidMaterial.MATERIAL_COLOR_KEY, Meta("red".asValue())) } val serialized = visionManager.jsonFormat.encodeToString(VisionChange.serializer(), change) println(serialized) diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/MeshThreeFactory.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/MeshThreeFactory.kt index 7cbd4d3d..7e90a657 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/MeshThreeFactory.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/MeshThreeFactory.kt @@ -90,7 +90,7 @@ public fun Mesh.applyEdges(obj: Solid) { MeshThreeFactory.EDGES_MATERIAL_KEY, inherit = true, includeStyles = true - ).node, + ), true ) if (edges == null) { 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 3afb6776..2cfcadf5 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 @@ -168,10 +168,10 @@ public class ThreeCanvas( } //Clipping planes - options.onChange(this@ThreeCanvas) { name, _, _ -> + options.meta.onChange(this@ThreeCanvas) { name-> if (name.startsWith(Canvas3DOptions::clipping.name.asName())) { val clipping = options.clipping - if (!clipping.isEmpty()) { + if (!clipping.meta.isEmpty()) { renderer.localClippingEnabled = true boundingBox?.let { boundingBox -> val xClippingPlane = clipping.x?.let { @@ -212,9 +212,9 @@ public class ThreeCanvas( private fun Object3D.fullName(): Name { if (root == null) error("Can't resolve element name without the root") return if (parent == root) { - name.toName() + Name.parse(name) } else { - (parent?.fullName() ?: Name.EMPTY) + name.toName() + (parent?.fullName() ?: Name.EMPTY) + Name.parse(name) } } @@ -237,7 +237,7 @@ public class ThreeCanvas( private fun buildLight(spec: Light?): info.laht.threekt.lights.Light = AmbientLight(0x404040) private fun addControls(element: Node, controls: Controls) { - when (controls["type"].string) { + when (controls.meta["type"].string) { "trackball" -> TrackballControls(camera, element) else -> OrbitControls(camera, element) } diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeLineFactory.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeLineFactory.kt index b2858fdb..bc369086 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeLineFactory.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeLineFactory.kt @@ -4,7 +4,6 @@ import info.laht.threekt.core.BufferGeometry import info.laht.threekt.core.Object3D import info.laht.threekt.math.Color import info.laht.threekt.objects.LineSegments -import space.kscience.dataforge.meta.node import space.kscience.visionforge.onPropertyChange import space.kscience.visionforge.solid.PolyLine import space.kscience.visionforge.solid.color @@ -20,7 +19,10 @@ public object ThreeLineFactory : ThreeFactory { setFromPoints(Array(obj.points.size) { obj.points[it].toVector() }) } - val material = ThreeMaterials.getLineMaterial(obj.getProperty(MeshThreeFactory.EDGES_MATERIAL_KEY).node, true) + val material = ThreeMaterials.getLineMaterial( + obj.getProperty(MeshThreeFactory.EDGES_MATERIAL_KEY), + true + ) material.linewidth = obj.thickness.toDouble() material.color = obj.color.string?.let { Color(it) } ?: DEFAULT_LINE_COLOR diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeMaterials.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeMaterials.kt index 5f7ef051..258df5ff 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeMaterials.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeMaterials.kt @@ -13,7 +13,7 @@ import space.kscience.dataforge.values.int import space.kscience.dataforge.values.string import space.kscience.visionforge.Colors import space.kscience.visionforge.Vision -import space.kscience.visionforge.ownProperties +import space.kscience.visionforge.meta import space.kscience.visionforge.solid.SolidMaterial @@ -41,7 +41,7 @@ public object ThreeMaterials { private val lineMaterialCache = HashMap() private fun buildLineMaterial(meta: Meta): LineBasicMaterial = LineBasicMaterial().apply { - color = meta[SolidMaterial.COLOR_KEY]?.getColor() ?: DEFAULT_LINE_COLOR + color = meta[SolidMaterial.COLOR_KEY]?.threeColor() ?: DEFAULT_LINE_COLOR opacity = meta[SolidMaterial.OPACITY_KEY].double ?: 1.0 transparent = opacity < 1.0 linewidth = meta["thickness"].double ?: 1.0 @@ -59,11 +59,12 @@ public object ThreeMaterials { private val materialCache = HashMap() internal fun buildMaterial(meta: Meta): Material { + val material = SolidMaterial.read(meta) return meta[SolidMaterial.SPECULAR_COLOR_KEY]?.let { specularColor -> MeshPhongMaterial().apply { - color = meta[SolidMaterial.COLOR_KEY]?.getColor() ?: DEFAULT_COLOR - specular = specularColor.getColor() - emissive = specular + color = meta[SolidMaterial.COLOR_KEY]?.threeColor() ?: DEFAULT_COLOR + specular = specularColor.threeColor() + emissive = material.emissiveColor.item?.threeColor() ?: specular reflectivity = 0.5 refractionRatio = 1.0 shininess = 100.0 @@ -73,7 +74,7 @@ public object ThreeMaterials { needsUpdate = true } } ?: MeshBasicMaterial().apply { - color = meta[SolidMaterial.COLOR_KEY]?.getColor() ?: DEFAULT_COLOR + color = meta[SolidMaterial.COLOR_KEY]?.threeColor() ?: DEFAULT_COLOR opacity = meta[SolidMaterial.OPACITY_KEY]?.double ?: 1.0 transparent = opacity < 1.0 wireframe = meta[SolidMaterial.WIREFRAME_KEY].boolean ?: false @@ -93,22 +94,19 @@ public object ThreeMaterials { /** * Infer color based on meta item */ -public fun MetaItem.getColor(): Color { - return when (this) { - is MetaItemValue -> if (this.value.type == ValueType.NUMBER) { +public fun Meta.threeColor(): Color { + return value?.let { value -> + if (value.type == ValueType.NUMBER) { val int = value.int Color(int) } else { - Color(this.value.string) + Color(value.string) } - is MetaItemNode -> { - Color( - node[Colors.RED_KEY]?.int ?: 0, - node[Colors.GREEN_KEY]?.int ?: 0, - node[Colors.BLUE_KEY]?.int ?: 0 - ) - } - } + } ?: Color( + this[Colors.RED_KEY]?.int ?: 0, + this[Colors.GREEN_KEY]?.int ?: 0, + this[Colors.BLUE_KEY]?.int ?: 0 + ) } private var Material.cached: Boolean @@ -119,7 +117,7 @@ private var Material.cached: Boolean public fun Mesh.updateMaterial(vision: Vision) { //val meta = vision.getProperty(SolidMaterial.MATERIAL_KEY, inherit = true).node - val ownMaterialMeta = vision.ownProperties[SolidMaterial.MATERIAL_KEY] + val ownMaterialMeta = vision.meta()[SolidMaterial.MATERIAL_KEY] val parentMaterialMeta = vision.parent?.getProperty( SolidMaterial.MATERIAL_KEY, inherit = true, @@ -135,7 +133,7 @@ public fun Mesh.updateMaterial(vision: Vision) { inherit = false, includeStyles = true, includeDefaults = false - ).node?.let { + )?.let { ThreeMaterials.cacheMaterial(it) } ?: ThreeMaterials.DEFAULT } @@ -143,7 +141,7 @@ public fun Mesh.updateMaterial(vision: Vision) { vision.getProperty( SolidMaterial.MATERIAL_KEY, inherit = true - ).node?.let { + )?.let { ThreeMaterials.buildMaterial(it) } ?: ThreeMaterials.DEFAULT } @@ -162,7 +160,7 @@ public fun Mesh.updateMaterialProperty(vision: Vision, propertyName: Name) { inherit = true, includeStyles = true, includeDefaults = false - )?.getColor() ?: ThreeMaterials.DEFAULT_COLOR + )?.threeColor() ?: ThreeMaterials.DEFAULT_COLOR material.needsUpdate = true } SolidMaterial.MATERIAL_OPACITY_KEY -> { 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 e6d85dc5..9c7c1f6b 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 @@ -143,7 +143,7 @@ public class ThreePlugin : AbstractPlugin(), ElementVisionRenderer { element, vision as? Solid ?: error("Solid expected but ${vision::class} found"), ).apply { - options.update(meta) + options.meta.update(meta) } } @@ -195,8 +195,4 @@ internal fun Object3D.findChild(name: Name): Object3D? { name.length == 1 -> this.children.find { it.name == name.tokens.first().toString() } else -> findChild(name.tokens.first().asName())?.findChild(name.cutFirst()) } -} - -public fun Context.withThreeJs(): Context = apply { - plugins.fetch(ThreePlugin) } \ No newline at end of file diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeReferenceFactory.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeReferenceFactory.kt index 33fd3ccb..4dca0eeb 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeReferenceFactory.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeReferenceFactory.kt @@ -3,9 +3,9 @@ package space.kscience.visionforge.solid.three import info.laht.threekt.core.BufferGeometry import info.laht.threekt.core.Object3D import info.laht.threekt.objects.Mesh +import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.cutFirst import space.kscience.dataforge.names.firstOrNull -import space.kscience.dataforge.names.toName import space.kscience.visionforge.onPropertyChange import space.kscience.visionforge.solid.Solid import space.kscience.visionforge.solid.SolidReferenceGroup @@ -49,7 +49,7 @@ public object ThreeReferenceFactory : ThreeFactory { obj.onPropertyChange(three.updateScope) { name-> if (name.firstOrNull()?.body == REFERENCE_CHILD_PROPERTY_PREFIX) { - val childName = name.firstOrNull()?.index?.toName() ?: error("Wrong syntax for reference child property: '$name'") + val childName = name.firstOrNull()?.index?.let(Name::parse) ?: error("Wrong syntax for reference child property: '$name'") val propertyName = name.cutFirst() val referenceChild = obj[childName] ?: error("Reference child with name '$childName' not found") val child = object3D.findChild(childName) ?: error("Object child with name '$childName' not found") diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/three.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/three.kt index 9e78800b..ea609e58 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/three.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/three.kt @@ -8,7 +8,7 @@ import info.laht.threekt.math.Euler import info.laht.threekt.math.Vector3 import info.laht.threekt.objects.Mesh import info.laht.threekt.textures.Texture -import space.kscience.dataforge.meta.MetaItem +import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.float import space.kscience.dataforge.meta.get import space.kscience.dataforge.meta.node @@ -17,7 +17,7 @@ import kotlin.math.PI public val Solid.euler: Euler get() = Euler(rotationX, rotationY, rotationZ, rotationOrder.name) -public val MetaItem.vector: Vector3 get() = Vector3(node["x"].float ?: 0f, node["y"].float ?: 0f, node["z"].float ?: 0f) +public val Meta.vector: Vector3 get() = Vector3(this["x"].float ?: 0f, this["y"].float ?: 0f, this["z"].float ?: 0f) internal fun Double.toRadians() = this * PI / 180