Fix circle clickable radius

This commit is contained in:
Alexander Nozik 2023-02-22 14:57:25 +03:00
parent 1bcae1f362
commit 2ed9d72029
5 changed files with 34 additions and 21 deletions

View File

@ -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<XY>(
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")

View File

@ -203,11 +203,15 @@ public data class PolygonFeature<T : Any>(
public data class CircleFeature<T : Any>(
override val space: CoordinateSpace<T>,
override val center: T,
public val size: Dp = 5.dp,
public val radius: Dp = 5.dp,
override val attributes: Attributes = Attributes.EMPTY,
) : MarkerFeature<T> {
override fun getBoundingBox(zoom: Float): Rectangle<T> =
space.Rectangle(center, zoom, DpSize(size, size))
space.Rectangle(center, zoom, DpSize(radius * 2, radius * 2))
override fun contains(viewPoint: ViewPoint<T>): Boolean = with(space) {
viewPoint.focus.distanceTo(center, viewPoint.zoom) < radius
}
override fun withCoordinates(newCoordinates: T): Feature<T> = copy(center = newCoordinates)

View File

@ -1,6 +1,7 @@
package center.sciprog.maps.features
import center.sciprog.attributes.Attributes
import kotlin.jvm.JvmName
public fun <T : Any> FeatureGroup<T>.draggableLine(
@ -67,4 +68,15 @@ public fun <T : Any> FeatureGroup<T>.draggableMultiLine(
}
return drawLines()
}
@JvmName("draggableMultiLineFromPoints")
public fun <T : Any> FeatureGroup<T>.draggableMultiLine(
points: List<T>,
id: String? = null,
): FeatureRef<T, MultiLineFeature<T>> {
val pointRefs = points.map {
circle(it)
}
return draggableMultiLine(pointRefs, id)
}

View File

@ -32,7 +32,7 @@ public fun <T : Any> 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()
)

View File

@ -66,7 +66,7 @@ fun FeatureStateSnapshot<XY>.generateSvg(
is CircleFeature -> drawCircle(
color,
feature.size.toPx(),
feature.radius.toPx(),
center = feature.center.toOffset(),
alpha = alpha
)