From 2ed9d72029eaa5d35dc4f06b12d1880afbb02546 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 22 Feb 2023 14:57:25 +0300 Subject: [PATCH] Fix circle clickable radius --- .../polygon-editor/src/jvmMain/kotlin/Main.kt | 31 +++++++++---------- .../center/sciprog/maps/features/Feature.kt | 8 +++-- .../maps/features/compositeFeatures.kt | 12 +++++++ .../sciprog/maps/features/drawFeature.kt | 2 +- .../center/sciprog/maps/svg/exportToSvg.kt | 2 +- 5 files changed, 34 insertions(+), 21 deletions(-) diff --git a/demo/polygon-editor/src/jvmMain/kotlin/Main.kt b/demo/polygon-editor/src/jvmMain/kotlin/Main.kt index 4cf447c..c6746aa 100644 --- a/demo/polygon-editor/src/jvmMain/kotlin/Main.kt +++ b/demo/polygon-editor/src/jvmMain/kotlin/Main.kt @@ -1,9 +1,9 @@ // 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.Button import androidx.compose.material.CursorDropdownMenu import androidx.compose.material.MaterialTheme import androidx.compose.material.Text +import androidx.compose.material.TextButton import androidx.compose.runtime.* import androidx.compose.runtime.snapshots.SnapshotStateList import androidx.compose.ui.input.pointer.isSecondaryPressed @@ -31,21 +31,6 @@ fun App() { ) } - - if(myPolygon.isNotEmpty()) { - featureState.group("polygon") { - multiLine( - myPolygon + myPolygon.first(), - ) - myPolygon.forEachIndexed { index, xy -> - circle(xy, id = "point[$index]").draggable { _, to -> - myPolygon[index] = to.focus - } - } - } - } - - val mapState: XYViewScope = XYViewScope.remember( config = ViewConfig( onClick = { event, point -> @@ -59,8 +44,20 @@ fun App() { CursorDropdownMenu(clickPoint != null, { clickPoint = null }) { clickPoint?.let { point -> - Button({ + TextButton({ myPolygon.add(point) + 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(), + ) + } + } clickPoint = null }) { Text("Create node") diff --git a/maps-kt-features/src/commonMain/kotlin/center/sciprog/maps/features/Feature.kt b/maps-kt-features/src/commonMain/kotlin/center/sciprog/maps/features/Feature.kt index 9c3b289..7fe2631 100644 --- a/maps-kt-features/src/commonMain/kotlin/center/sciprog/maps/features/Feature.kt +++ b/maps-kt-features/src/commonMain/kotlin/center/sciprog/maps/features/Feature.kt @@ -203,11 +203,15 @@ public data class PolygonFeature( public data class CircleFeature( override val space: CoordinateSpace, override val center: T, - public val size: Dp = 5.dp, + public val radius: Dp = 5.dp, override val attributes: Attributes = Attributes.EMPTY, ) : MarkerFeature { override fun getBoundingBox(zoom: Float): Rectangle = - space.Rectangle(center, zoom, DpSize(size, size)) + space.Rectangle(center, zoom, DpSize(radius * 2, radius * 2)) + + override fun contains(viewPoint: ViewPoint): Boolean = with(space) { + viewPoint.focus.distanceTo(center, viewPoint.zoom) < radius + } override fun withCoordinates(newCoordinates: T): Feature = copy(center = newCoordinates) diff --git a/maps-kt-features/src/commonMain/kotlin/center/sciprog/maps/features/compositeFeatures.kt b/maps-kt-features/src/commonMain/kotlin/center/sciprog/maps/features/compositeFeatures.kt index a608a5d..ac360e6 100644 --- a/maps-kt-features/src/commonMain/kotlin/center/sciprog/maps/features/compositeFeatures.kt +++ b/maps-kt-features/src/commonMain/kotlin/center/sciprog/maps/features/compositeFeatures.kt @@ -1,6 +1,7 @@ package center.sciprog.maps.features import center.sciprog.attributes.Attributes +import kotlin.jvm.JvmName public fun FeatureGroup.draggableLine( @@ -67,4 +68,15 @@ public fun FeatureGroup.draggableMultiLine( } return drawLines() +} + +@JvmName("draggableMultiLineFromPoints") +public fun FeatureGroup.draggableMultiLine( + points: List, + id: String? = null, +): FeatureRef> { + val pointRefs = points.map { + circle(it) + } + return draggableMultiLine(pointRefs, id) } \ No newline at end of file diff --git a/maps-kt-features/src/jvmMain/kotlin/center/sciprog/maps/features/drawFeature.kt b/maps-kt-features/src/jvmMain/kotlin/center/sciprog/maps/features/drawFeature.kt index 8e1f2d4..efd5ca5 100644 --- a/maps-kt-features/src/jvmMain/kotlin/center/sciprog/maps/features/drawFeature.kt +++ b/maps-kt-features/src/jvmMain/kotlin/center/sciprog/maps/features/drawFeature.kt @@ -32,7 +32,7 @@ public fun DrawScope.drawFeature( is FeatureSelector -> drawFeature(state, painterCache, feature.selector(state.zoom)) is CircleFeature -> drawCircle( color, - feature.size.toPx(), + feature.radius.toPx(), center = feature.center.toOffset() ) diff --git a/maps-kt-scheme/src/jvmMain/kotlin/center/sciprog/maps/svg/exportToSvg.kt b/maps-kt-scheme/src/jvmMain/kotlin/center/sciprog/maps/svg/exportToSvg.kt index dae8cc8..edac4b3 100644 --- a/maps-kt-scheme/src/jvmMain/kotlin/center/sciprog/maps/svg/exportToSvg.kt +++ b/maps-kt-scheme/src/jvmMain/kotlin/center/sciprog/maps/svg/exportToSvg.kt @@ -66,7 +66,7 @@ fun FeatureStateSnapshot.generateSvg( is CircleFeature -> drawCircle( color, - feature.size.toPx(), + feature.radius.toPx(), center = feature.center.toOffset(), alpha = alpha )