refactor custom feature -> draw feature

This commit is contained in:
Alexander Nozik 2022-07-19 09:34:00 +03:00
parent f218853544
commit 52d0d959de
No known key found for this signature in database
GPG Key ID: F7FCF2DD25C71357
3 changed files with 12 additions and 17 deletions

View File

@ -8,7 +8,6 @@ import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.DrawScope import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.graphics.vector.ImageVector
import centre.sciprog.maps.GmcBox
typealias FeatureId = String typealias FeatureId = String
@ -23,6 +22,7 @@ internal class MapFeatureBuilder(initialFeatures: Map<FeatureId, MapFeature>) :
private val content: SnapshotStateMap<FeatureId, MapFeature> = mutableStateMapOf<FeatureId, MapFeature>().apply { private val content: SnapshotStateMap<FeatureId, MapFeature> = mutableStateMapOf<FeatureId, MapFeature>().apply {
putAll(initialFeatures) putAll(initialFeatures)
} }
private fun generateID(feature: MapFeature): FeatureId = "@feature[${feature.hashCode().toUInt()}]" private fun generateID(feature: MapFeature): FeatureId = "@feature[${feature.hashCode().toUInt()}]"
override fun addFeature(id: FeatureId?, feature: MapFeature): FeatureId { override fun addFeature(id: FeatureId?, feature: MapFeature): FeatureId {
@ -46,18 +46,10 @@ fun FeatureBuilder.circle(
fun FeatureBuilder.custom( fun FeatureBuilder.custom(
position: Pair<Double, Double>, position: Pair<Double, Double>,
zoomRange: IntRange = defaultZoomRange,
id: FeatureId? = null, id: FeatureId? = null,
customFeatureBuilder: DrawScope.(Offset) -> Unit, drawFeature: DrawScope.(Offset) -> Unit,
) = addFeature(id, object : MapCustomFeature(position = position.toCoordinates()) { ) = addFeature(id, MapDrawFeature(position.toCoordinates(), zoomRange, drawFeature))
override fun drawFeature(drawScope: DrawScope, offset: Offset) {
customFeatureBuilder(drawScope, offset)
}
override fun getBoundingBox(zoom: Int): GmcBox {
return GmcBox(position.toCoordinates(), position.toCoordinates())
}
})
fun FeatureBuilder.line( fun FeatureBuilder.line(
aCoordinates: Pair<Double, Double>, aCoordinates: Pair<Double, Double>,

View File

@ -30,14 +30,17 @@ internal val defaultZoomRange = 1..18
*/ */
class MapFeatureSelector(val selector: (zoom: Int) -> MapFeature) : MapFeature(defaultZoomRange) { class MapFeatureSelector(val selector: (zoom: Int) -> MapFeature) : MapFeature(defaultZoomRange) {
override fun getBoundingBox(zoom: Int): GmcBox = selector(zoom).getBoundingBox(zoom) override fun getBoundingBox(zoom: Int): GmcBox = selector(zoom).getBoundingBox(zoom)
} }
abstract class MapCustomFeature( class MapDrawFeature(
val position: GeodeticMapCoordinates,
zoomRange: IntRange = defaultZoomRange, zoomRange: IntRange = defaultZoomRange,
val position: GeodeticMapCoordinates val drawFeature: DrawScope.(Offset) -> Unit,
) : MapFeature(zoomRange) { ) : MapFeature(zoomRange) {
abstract fun drawFeature(drawScope: DrawScope, offset: Offset) override fun getBoundingBox(zoom: Int): GmcBox {
//TODO add box computation
return GmcBox(position, position)
}
} }
class MapCircleFeature( class MapCircleFeature(

View File

@ -236,7 +236,7 @@ actual fun MapView(
feature.color.toPaint() feature.color.toPaint()
) )
} }
is MapCustomFeature -> drawIntoCanvas { canvas -> is MapDrawFeature -> {
val offset = feature.position.toOffset() val offset = feature.position.toOffset()
feature.drawFeature(this, offset) feature.drawFeature(this, offset)
} }