Fix view rectangle computation

This commit is contained in:
Alexander Nozik 2022-12-28 21:02:23 +03:00
parent d34c5099f6
commit 7643968d39
7 changed files with 16 additions and 100 deletions

View File

@ -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)

View File

@ -86,7 +86,6 @@ fun App() {
},
onViewChange = { viewPoint = this }
),
schemeFeaturesState.features.values,
initialViewPoint = initialViewPoint,
)

View File

@ -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)

View File

@ -16,7 +16,7 @@ public class MapViewScope internal constructor(
) : CoordinateViewScope<Gmc>(config) {
override val space: CoordinateSpace<Gmc> get() = WebMercatorSpace
public val scaleFactor: Float
private val scaleFactor: Float
get() = WebMercatorProjection.scaleFactor(zoom)
public val intZoom: Int get() = floor(zoom).toInt()

View File

@ -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<T : Any>(
public val config: ViewConfig<T>,
@ -17,8 +13,8 @@ public abstract class CoordinateViewScope<T : Any>(
public abstract val space: CoordinateSpace<T>
protected var canvasSizeState: MutableState<DpSize?> = mutableStateOf(null)
protected var viewPointState: MutableState<ViewPoint<T>?> = mutableStateOf(null)
private var canvasSizeState: MutableState<DpSize?> = mutableStateOf(null)
private var viewPointState: MutableState<ViewPoint<T>?> = mutableStateOf(null)
public var canvasSize: DpSize
get() = canvasSizeState.value ?: DpSize(512.dp, 512.dp)

View File

@ -156,8 +156,8 @@ public class LineFeature<T : Any>(
override fun getBoundingBox(zoom: Float): Rectangle<T> =
space.Rectangle(a, b)
override fun contains(veiwPoint: ViewPoint<T>): Boolean = with(space) {
veiwPoint.focus in space.Rectangle(a, b) && veiwPoint.focus.distanceToLine(a, b, veiwPoint.zoom).value < 5f
override fun contains(viewPoint: ViewPoint<T>): Boolean = with(space) {
viewPoint.focus in space.Rectangle(a, b) && viewPoint.focus.distanceToLine(a, b, viewPoint.zoom).value < 5f
}
}

View File

@ -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<FeatureId<*>,>,
// 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<XY>? = null,
// config: ViewConfig<XY> = ViewConfig(),
// modifier: Modifier = Modifier.fillMaxSize(),
// buildFeatures: FeaturesState<XY>.() -> 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,
// )
//}