2021-05-09 15:49:18 +03:00
|
|
|
package space.kscience.visionforge.ring
|
|
|
|
|
2021-06-13 21:24:02 +03:00
|
|
|
import kotlinx.css.BorderStyle
|
|
|
|
import kotlinx.css.Color
|
|
|
|
import kotlinx.css.padding
|
2021-05-09 15:49:18 +03:00
|
|
|
import kotlinx.css.properties.border
|
2021-06-13 21:24:02 +03:00
|
|
|
import kotlinx.css.px
|
2021-05-09 15:49:18 +03:00
|
|
|
import kotlinx.html.js.onClickFunction
|
|
|
|
import org.w3c.dom.events.Event
|
|
|
|
import org.w3c.files.Blob
|
|
|
|
import org.w3c.files.BlobPropertyBag
|
|
|
|
import react.*
|
2021-06-04 20:01:50 +03:00
|
|
|
import react.dom.attrs
|
2021-05-09 15:49:18 +03:00
|
|
|
import react.dom.button
|
|
|
|
import ringui.island.ringIsland
|
|
|
|
import ringui.tabs.ringSmartTabs
|
|
|
|
import ringui.tabs.ringTab
|
|
|
|
import space.kscience.dataforge.meta.withDefault
|
|
|
|
import space.kscience.dataforge.names.Name
|
|
|
|
import space.kscience.dataforge.names.isEmpty
|
|
|
|
import space.kscience.visionforge.Vision
|
|
|
|
import space.kscience.visionforge.VisionGroup
|
2021-06-06 20:57:39 +03:00
|
|
|
import space.kscience.visionforge.encodeToString
|
2021-05-09 15:49:18 +03:00
|
|
|
import space.kscience.visionforge.react.flexColumn
|
|
|
|
import space.kscience.visionforge.react.flexRow
|
|
|
|
import space.kscience.visionforge.react.propertyEditor
|
2021-06-13 21:24:02 +03:00
|
|
|
import space.kscience.visionforge.react.visionTree
|
2021-05-09 15:49:18 +03:00
|
|
|
import space.kscience.visionforge.solid.specifications.Canvas3DOptions
|
|
|
|
import styled.css
|
|
|
|
|
|
|
|
internal fun saveData(event: Event, fileName: String, mimeType: String = "text/plain", dataBuilder: () -> String) {
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
val fileSaver = kotlinext.js.require("file-saver")
|
|
|
|
val blob = Blob(arrayOf(dataBuilder()), BlobPropertyBag("$mimeType;charset=utf-8"))
|
|
|
|
fileSaver.saveAs(blob, fileName)
|
|
|
|
}
|
|
|
|
|
2021-06-06 20:57:39 +03:00
|
|
|
internal fun RBuilder.canvasControls(options: Canvas3DOptions, vision: Vision?): ReactElement {
|
2021-05-09 15:49:18 +03:00
|
|
|
return child(CanvasControls) {
|
|
|
|
attrs {
|
2021-06-06 20:57:39 +03:00
|
|
|
this.options = options
|
|
|
|
this.vision = vision
|
2021-05-09 15:49:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal external interface CanvasControlsProps : RProps {
|
2021-06-06 20:57:39 +03:00
|
|
|
public var options: Canvas3DOptions
|
|
|
|
public var vision: Vision?
|
2021-05-09 15:49:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
internal val CanvasControls: FunctionalComponent<CanvasControlsProps> = functionalComponent("CanvasControls") { props ->
|
|
|
|
flexColumn {
|
|
|
|
flexRow {
|
|
|
|
css {
|
|
|
|
border(1.px, BorderStyle.solid, Color.blue)
|
|
|
|
padding(4.px)
|
|
|
|
}
|
2021-06-06 20:57:39 +03:00
|
|
|
props.vision?.let { vision ->
|
|
|
|
button {
|
|
|
|
+"Export"
|
|
|
|
attrs {
|
|
|
|
onClickFunction = {
|
|
|
|
val json = vision.encodeToString()
|
2021-05-09 15:49:18 +03:00
|
|
|
saveData(it, "object.json", "text/json") {
|
|
|
|
json
|
|
|
|
}
|
2021-06-06 20:57:39 +03:00
|
|
|
|
2021-05-09 15:49:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
propertyEditor(
|
2021-06-06 20:57:39 +03:00
|
|
|
ownProperties = props.options,
|
|
|
|
allProperties = props.options.withDefault(Canvas3DOptions.descriptor.defaultMeta),
|
2021-05-09 15:49:18 +03:00
|
|
|
descriptor = Canvas3DOptions.descriptor,
|
|
|
|
expanded = false
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public external interface ThreeControlsProps : RProps {
|
2021-06-06 20:57:39 +03:00
|
|
|
public var canvasOptions: Canvas3DOptions
|
|
|
|
public var vision: Vision?
|
2021-05-09 15:49:18 +03:00
|
|
|
public var selected: Name?
|
2021-06-13 21:24:02 +03:00
|
|
|
public var onSelect: (Name?) -> Unit
|
2021-05-09 15:49:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@JsExport
|
|
|
|
public val ThreeControls: FunctionalComponent<ThreeControlsProps> = functionalComponent { props ->
|
2021-06-08 18:45:03 +03:00
|
|
|
ringSmartTabs(if (props.selected != null) "Properties" else "Tree") {
|
2021-05-09 15:49:18 +03:00
|
|
|
ringTab("Canvas") {
|
|
|
|
ringIsland("Canvas configuration") {
|
2021-06-06 20:57:39 +03:00
|
|
|
canvasControls(props.canvasOptions, props.vision)
|
2021-05-09 15:49:18 +03:00
|
|
|
}
|
|
|
|
}
|
2021-06-13 21:24:02 +03:00
|
|
|
props.vision?.let {
|
|
|
|
ringTab("Tree") {
|
|
|
|
ringIsland("Vision tree") {
|
|
|
|
visionTree(it, props.selected, props.onSelect)
|
2021-05-09 15:49:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-08 18:45:03 +03:00
|
|
|
if (props.selected != null) {
|
|
|
|
ringTab("Properties") {
|
|
|
|
props.selected.let { selected ->
|
|
|
|
val selectedObject: Vision? = when {
|
|
|
|
selected == null -> null
|
|
|
|
selected.isEmpty() -> props.vision
|
|
|
|
else -> (props.vision as? VisionGroup)?.get(selected)
|
|
|
|
}
|
|
|
|
if (selectedObject != null) {
|
|
|
|
ringPropertyEditor(selectedObject, key = selected)
|
|
|
|
}
|
2021-05-09 15:49:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
props.children()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public fun RBuilder.ringThreeControls(
|
2021-06-06 20:57:39 +03:00
|
|
|
canvasOptions: Canvas3DOptions,
|
|
|
|
vision: Vision?,
|
2021-05-09 15:49:18 +03:00
|
|
|
selected: Name?,
|
2021-06-13 21:24:02 +03:00
|
|
|
onSelect: (Name?) -> Unit = {},
|
2021-05-09 15:49:18 +03:00
|
|
|
builder: RBuilder.() -> Unit = {},
|
|
|
|
): ReactElement = child(ThreeControls) {
|
|
|
|
attrs {
|
2021-06-06 20:57:39 +03:00
|
|
|
this.canvasOptions = canvasOptions
|
|
|
|
this.vision = vision
|
2021-05-09 15:49:18 +03:00
|
|
|
this.selected = selected
|
|
|
|
this.onSelect = onSelect
|
|
|
|
}
|
|
|
|
builder()
|
|
|
|
}
|