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( val zoom = log2(
min( min(
canvasSize.width.value / width, canvasSize.width.value / width,
@ -72,7 +72,7 @@ fun MapView(
featuresBuilder.buildFeatures() featuresBuilder.buildFeatures()
MapView( MapView(
mapTileProvider, mapTileProvider,
box.getComputeViewPoint(mapTileProvider), box.computeViewPoint(mapTileProvider),
featuresBuilder.build(), featuresBuilder.build(),
config, config,
modifier modifier

View File

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