2023-01-06 14:15:06 +03:00
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.material.CursorDropdownMenu
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
2023-02-22 14:57:25 +03:00
import androidx.compose.material.TextButton
2023-01-06 14:15:06 +03:00
import androidx.compose.runtime.*
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.input.pointer.isSecondaryPressed
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import center.sciprog.maps.features.*
import center.sciprog.maps.scheme.SchemeView
import center.sciprog.maps.scheme.XY
import center.sciprog.maps.scheme.XYCoordinateSpace
import center.sciprog.maps.scheme.XYViewScope
@Composable
@Preview
fun App ( ) {
MaterialTheme {
var clickPoint by remember { mutableStateOf < XY ? > ( null ) }
val myPolygon : SnapshotStateList < XY > = remember { mutableStateListOf < XY > ( ) }
val featureState : FeatureGroup < XY > = FeatureGroup . remember ( XYCoordinateSpace ) {
2023-02-13 16:49:36 +03:00
multiLine (
2023-01-06 14:15:06 +03:00
listOf ( XY ( 0f , 0f ) , XY ( 0f , 1f ) , XY ( 1f , 1f ) , XY ( 1f , 0f ) , XY ( 0f , 0f ) ) ,
id = " frame "
)
}
val mapState : XYViewScope = XYViewScope . remember (
config = ViewConfig < XY > (
onClick = { event , point ->
if ( event . buttons . isSecondaryPressed ) {
clickPoint = point . focus
}
}
) ,
initialRectangle = featureState . getBoundingBox ( 1f ) ,
)
CursorDropdownMenu ( clickPoint != null , { clickPoint = null } ) {
clickPoint ?. let { point ->
2023-02-22 14:57:25 +03:00
TextButton ( {
2023-01-06 14:15:06 +03:00
myPolygon . add ( point )
2023-02-22 14:57:25 +03:00
if ( myPolygon . isNotEmpty ( ) ) {
featureState . group ( id = " polygon " ) {
val pointRefs = myPolygon . mapIndexed { index , xy ->
circle ( xy , id = " point[ $index ] " ) . draggable { _ , to ->
myPolygon [ index ] = to . focus
}
}
draggableMultiLine (
pointRefs + pointRefs . first ( ) ,
)
}
}
2023-01-06 14:15:06 +03:00
clickPoint = null
} ) {
Text ( " Create node " )
}
}
}
SchemeView (
mapState ,
featureState ,
)
}
}
fun main ( ) = application {
Window ( title = " Polygon editor demo " , onCloseRequest = :: exitApplication ) {
App ( )
}
}