From 7643968d39447831588f3b11f254c6971b09b493 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 28 Dec 2022 21:02:23 +0300 Subject: [PATCH] Fix view rectangle computation --- demo/maps/src/jvmMain/kotlin/Main.kt | 15 ++-- demo/scheme/src/jvmMain/kotlin/Main.kt | 1 - .../center/sciprog/maps/compose/MapView.kt | 4 +- .../sciprog/maps/compose/MapViewScope.kt | 2 +- .../maps/features/CoordinateViewScope.kt | 8 +- .../center/sciprog/maps/features/Feature.kt | 4 +- .../center/sciprog/maps/scheme/SchemeView.kt | 82 +------------------ 7 files changed, 16 insertions(+), 100 deletions(-) diff --git a/demo/maps/src/jvmMain/kotlin/Main.kt b/demo/maps/src/jvmMain/kotlin/Main.kt index c7497e8..cca512f 100644 --- a/demo/maps/src/jvmMain/kotlin/Main.kt +++ b/demo/maps/src/jvmMain/kotlin/Main.kt @@ -5,7 +5,6 @@ import androidx.compose.material.icons.filled.Home import androidx.compose.runtime.Composable import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope -import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.PointMode import androidx.compose.ui.unit.DpSize @@ -65,9 +64,9 @@ fun App() { image(pointOne, Icons.Filled.Home) - val marker1 = rectangle(55.744 to 37.614, size = DpSize(10.dp, 10.dp), color = Color.Magenta) - val marker2 = rectangle(55.8 to 37.5, size = DpSize(10.dp, 10.dp), color = Color.Magenta) - val marker3 = rectangle(56.0 to 37.5, size = DpSize(10.dp, 10.dp), color = Color.Magenta) + val marker1 = rectangle(55.744 to 38.614, size = DpSize(10.dp, 10.dp), color = Color.Magenta) + val marker2 = rectangle(55.8 to 38.5, size = DpSize(10.dp, 10.dp), color = Color.Magenta) + val marker3 = rectangle(56.0 to 38.5, size = DpSize(10.dp, 10.dp), color = Color.Magenta) draggableLine(marker1, marker2, color = Color.Blue) draggableLine(marker2, marker3, color = Color.Blue) @@ -94,10 +93,10 @@ fun App() { it.copy(color = Color(Random.nextFloat(), Random.nextFloat(), Random.nextFloat())) } - draw(position = pointThree) { - drawLine(start = Offset(-10f, -10f), end = Offset(10f, 10f), color = Color.Red) - drawLine(start = Offset(-10f, 10f), end = Offset(10f, -10f), color = Color.Red) - } +// draw(position = pointThree) { +// drawLine(start = Offset(-10f, -10f), end = Offset(10f, 10f), color = Color.Red) +// drawLine(start = Offset(-10f, 10f), end = Offset(10f, -10f), color = Color.Red) +// } arc(pointOne, 10.0.kilometers, (PI / 4).radians, -Angle.pi / 2) diff --git a/demo/scheme/src/jvmMain/kotlin/Main.kt b/demo/scheme/src/jvmMain/kotlin/Main.kt index 5bd5171..8aa286d 100644 --- a/demo/scheme/src/jvmMain/kotlin/Main.kt +++ b/demo/scheme/src/jvmMain/kotlin/Main.kt @@ -86,7 +86,6 @@ fun App() { }, onViewChange = { viewPoint = this } ), - schemeFeaturesState.features.values, initialViewPoint = initialViewPoint, ) diff --git a/maps-kt-compose/src/commonMain/kotlin/center/sciprog/maps/compose/MapView.kt b/maps-kt-compose/src/commonMain/kotlin/center/sciprog/maps/compose/MapView.kt index f70cf59..f1a4608 100644 --- a/maps-kt-compose/src/commonMain/kotlin/center/sciprog/maps/compose/MapView.kt +++ b/maps-kt-compose/src/commonMain/kotlin/center/sciprog/maps/compose/MapView.kt @@ -38,7 +38,7 @@ public fun MapView( mapTileProvider, config, initialViewPoint = initialViewPoint, - initialRectangle = initialRectangle ?: featureState.features.values.computeBoundingBox(WebMercatorSpace, 1f), + initialRectangle = initialRectangle ?: featureState.features.values.computeBoundingBox(WebMercatorSpace, Float.MAX_VALUE), ) MapView(mapState, featureState, modifier) @@ -67,7 +67,7 @@ public fun MapView( mapTileProvider, config, initialViewPoint = initialViewPoint, - initialRectangle = initialRectangle ?: featureState.features.values.computeBoundingBox(WebMercatorSpace, 1f), + initialRectangle = initialRectangle ?: featureState.features.values.computeBoundingBox(WebMercatorSpace, Float.MAX_VALUE), ) MapView(mapState, featureState, modifier) diff --git a/maps-kt-compose/src/commonMain/kotlin/center/sciprog/maps/compose/MapViewScope.kt b/maps-kt-compose/src/commonMain/kotlin/center/sciprog/maps/compose/MapViewScope.kt index 6406081..7895384 100644 --- a/maps-kt-compose/src/commonMain/kotlin/center/sciprog/maps/compose/MapViewScope.kt +++ b/maps-kt-compose/src/commonMain/kotlin/center/sciprog/maps/compose/MapViewScope.kt @@ -16,7 +16,7 @@ public class MapViewScope internal constructor( ) : CoordinateViewScope(config) { override val space: CoordinateSpace get() = WebMercatorSpace - public val scaleFactor: Float + private val scaleFactor: Float get() = WebMercatorProjection.scaleFactor(zoom) public val intZoom: Int get() = floor(zoom).toInt() diff --git a/maps-kt-features/src/commonMain/kotlin/center/sciprog/maps/features/CoordinateViewScope.kt b/maps-kt-features/src/commonMain/kotlin/center/sciprog/maps/features/CoordinateViewScope.kt index 2c8aa2a..8a2afaa 100644 --- a/maps-kt-features/src/commonMain/kotlin/center/sciprog/maps/features/CoordinateViewScope.kt +++ b/maps-kt-features/src/commonMain/kotlin/center/sciprog/maps/features/CoordinateViewScope.kt @@ -6,10 +6,6 @@ import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.compose.ui.geometry.Offset import androidx.compose.ui.unit.* -import kotlin.math.pow -import kotlin.math.sqrt - -private fun distanceBetween(a: DpOffset, b: DpOffset): Dp = sqrt((b.x - a.x).value.pow(2) + (b.y - a.y).value.pow(2)).dp public abstract class CoordinateViewScope( public val config: ViewConfig, @@ -17,8 +13,8 @@ public abstract class CoordinateViewScope( public abstract val space: CoordinateSpace - protected var canvasSizeState: MutableState = mutableStateOf(null) - protected var viewPointState: MutableState?> = mutableStateOf(null) + private var canvasSizeState: MutableState = mutableStateOf(null) + private var viewPointState: MutableState?> = mutableStateOf(null) public var canvasSize: DpSize get() = canvasSizeState.value ?: DpSize(512.dp, 512.dp) 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 a707320..f74e79f 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 @@ -156,8 +156,8 @@ public class LineFeature( override fun getBoundingBox(zoom: Float): Rectangle = space.Rectangle(a, b) - override fun contains(veiwPoint: ViewPoint): Boolean = with(space) { - veiwPoint.focus in space.Rectangle(a, b) && veiwPoint.focus.distanceToLine(a, b, veiwPoint.zoom).value < 5f + override fun contains(viewPoint: ViewPoint): Boolean = with(space) { + viewPoint.focus in space.Rectangle(a, b) && viewPoint.focus.distanceToLine(a, b, viewPoint.zoom).value < 5f } } diff --git a/maps-kt-scheme/src/jvmMain/kotlin/center/sciprog/maps/scheme/SchemeView.kt b/maps-kt-scheme/src/jvmMain/kotlin/center/sciprog/maps/scheme/SchemeView.kt index e070413..56f5c7f 100644 --- a/maps-kt-scheme/src/jvmMain/kotlin/center/sciprog/maps/scheme/SchemeView.kt +++ b/maps-kt-scheme/src/jvmMain/kotlin/center/sciprog/maps/scheme/SchemeView.kt @@ -96,7 +96,7 @@ public fun SchemeView( val state = rememberMapState( config, initialViewPoint = initialViewPoint, - initialRectangle = initialRectangle ?: featureState.features.values.computeBoundingBox(XYCoordinateSpace, 1f), + initialRectangle = initialRectangle ?: featureState.features.values.computeBoundingBox(XYCoordinateSpace, Float.MAX_VALUE), ) SchemeView(state, featureState, modifier) @@ -121,87 +121,9 @@ public fun SchemeView( val mapState: XYViewScope = rememberMapState( config, initialViewPoint = initialViewPoint, - initialRectangle = initialRectangle ?: featureState.features.values.computeBoundingBox(XYCoordinateSpace, 1f), + initialRectangle = initialRectangle ?: featureState.features.values.computeBoundingBox(XYCoordinateSpace, Float.MAX_VALUE), ) SchemeView(mapState, featureState, modifier) } -///** -// * A builder for a Scheme with static features. -// */ -//@Composable -//public fun SchemeView( -// initialViewPoint: XYViewPoint? = null, -// initialRectangle: XYRectangle? = null, -// featureMap: Map,>, -// config: SchemeViewConfig = SchemeViewConfig(), -// modifier: Modifier = Modifier.fillMaxSize(), -//) { -// val featuresState = key(featureMap) { -// SchemeFeaturesState.build { -// featureMap.forEach(::addFeature) -// } -// } -// -// val viewPointOverride: XYViewPoint = remember(initialViewPoint, initialRectangle) { -// initialViewPoint -// ?: initialRectangle?.computeViewPoint() -// ?: featureMap.values.computeBoundingBox(1f)?.computeViewPoint() -// ?: XYViewPoint(XY(0f, 0f)) -// } -// -// SchemeView(viewPointOverride, featuresState, config, modifier) -//} -// -///** -// * Draw a map using convenient parameters. If neither [initialViewPoint], noe [initialRectangle] is defined, -// * use map features to infer view region. -// * @param initialViewPoint The view point of the map using center and zoom. Is used if provided -// * @param initialRectangle The rectangle to be used for view point computation. Used if [initialViewPoint] is not defined. -// * @param buildFeatures - a builder for features -// */ -//@Composable -//public fun SchemeView( -// initialViewPoint: XYViewPoint? = null, -// initialRectangle: Rectangle? = null, -// config: ViewConfig = ViewConfig(), -// modifier: Modifier = Modifier.fillMaxSize(), -// buildFeatures: FeaturesState.() -> Unit = {}, -//) { -// val featureState = FeaturesState.remember(XYCoordinateSpace, buildFeatures) -// -// val features = featureState.features -// -// val viewPointOverride: XYViewPoint = remember(initialViewPoint, initialRectangle) { -// initialViewPoint -// ?: initialRectangle?.computeViewPoint() -// ?: features.values.computeBoundingBox(1f)?.computeViewPoint() -// ?: XYViewPoint(XY(0f, 0f)) -// } -// -//// val featureDrag = DragHandle.withPrimaryButton { _, start, end -> -//// val zoom = start.zoom -//// featureState.findAllWithAttribute(DraggableAttribute) { it }.forEach { id -> -//// val feature = features[id] as? DraggableMapFeature ?: return@forEach -//// val boundingBox = feature.getBoundingBox(zoom) ?: return@forEach -//// if (start.focus in boundingBox) { -//// featureState.addFeature(id, feature.withCoordinates(end.focus)) -//// return@withPrimaryButton false -//// } -//// } -//// return@withPrimaryButton true -//// } -//// -//// -//// val newConfig = config.copy( -//// dragHandle = DragHandle.combine(featureDrag, config.dragHandle) -//// ) -// -// SchemeView( -// initialViewPoint = viewPointOverride, -// featuresState = featureState, -// config = config, -// modifier = modifier, -// ) -//}