small autozoom fix

This commit is contained in:
Alexander Nozik 2022-07-23 09:59:14 +03:00
parent c7d1797617
commit 7ada7f85f2
No known key found for this signature in database
GPG Key ID: F7FCF2DD25C71357
2 changed files with 25 additions and 25 deletions

View File

@ -49,7 +49,7 @@ fun MapView(
)
}
internal fun GmcBox.getComputeViewPoint(mapTileProvider: MapTileProvider): (canvasSize: DpSize) -> MapViewPoint = { canvasSize ->
internal fun GmcBox.computeViewPoint(mapTileProvider: MapTileProvider): (canvasSize: DpSize) -> MapViewPoint = { canvasSize ->
val zoom = log2(
min(
canvasSize.width.value / width,
@ -72,7 +72,7 @@ fun MapView(
featuresBuilder.buildFeatures()
MapView(
mapTileProvider,
box.getComputeViewPoint(mapTileProvider),
box.computeViewPoint(mapTileProvider),
featuresBuilder.build(),
config,
modifier

View File

@ -17,7 +17,7 @@ import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.input.pointer.*
import androidx.compose.ui.unit.*
import centre.sciprog.maps.*
import io.ktor.utils.io.CancellationException
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.launch
import mu.KotlinLogging
import org.jetbrains.skia.Font
@ -59,9 +59,12 @@ actual fun MapView(
) {
var canvasSize by remember { mutableStateOf(DpSize(512.dp, 512.dp)) }
var viewPointOverride: MapViewPoint? by remember {
mutableStateOf(
if (config.inferViewBoxFromFeatures) {
var viewPointInternal: MapViewPoint? by remember {
mutableStateOf(null)
}
val viewPoint: MapViewPoint by derivedStateOf {
viewPointInternal ?: if (config.inferViewBoxFromFeatures) {
features.values.computeBoundingBox(1)?.let { box ->
val zoom = log2(
min(
@ -70,15 +73,12 @@ actual fun MapView(
) * PI / mapTileProvider.tileSize
)
MapViewPoint(box.center, zoom)
}
} ?: computeViewPoint(canvasSize)
} else {
null
computeViewPoint(canvasSize)
}
)
}
val viewPoint by derivedStateOf { viewPointOverride ?: computeViewPoint(canvasSize) }
val zoom: Int by derivedStateOf { floor(viewPoint.zoom).toInt() }
val tileScale: Double by derivedStateOf { 2.0.pow(viewPoint.zoom - zoom) }
@ -132,10 +132,10 @@ actual fun MapView(
)
config.onSelect(gmcBox)
if (config.zoomOnSelect) {
val newViewPoint = gmcBox.getComputeViewPoint(mapTileProvider).invoke(canvasSize)
val newViewPoint = gmcBox.computeViewPoint(mapTileProvider).invoke(canvasSize)
config.onViewChange(newViewPoint)
viewPointOverride = newViewPoint
viewPointInternal = newViewPoint
}
selectRect = null
}
@ -150,7 +150,7 @@ actual fun MapView(
+dragAmount.y.toDp().value / tileScale
)
config.onViewChange(newViewPoint)
viewPointOverride = newViewPoint
viewPointInternal = newViewPoint
}
}
}
@ -164,7 +164,7 @@ actual fun MapView(
val invariant = DpOffset(xPos.toDp(), yPos.toDp()).toGeodetic()
val newViewPoint = viewPoint.zoom(-change.scrollDelta.y.toDouble() * config.zoomSpeed, invariant)
config.onViewChange(newViewPoint)
viewPointOverride = newViewPoint
viewPointInternal = newViewPoint
}.fillMaxSize()