diff --git a/src/commonMain/kotlin/centre/sciprog/maps/compose/FeatureBuilder.kt b/src/commonMain/kotlin/centre/sciprog/maps/compose/FeatureBuilder.kt index 040a110..9a7e38f 100644 --- a/src/commonMain/kotlin/centre/sciprog/maps/compose/FeatureBuilder.kt +++ b/src/commonMain/kotlin/centre/sciprog/maps/compose/FeatureBuilder.kt @@ -3,9 +3,12 @@ package centre.sciprog.maps.compose import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateMapOf import androidx.compose.runtime.snapshots.SnapshotStateMap +import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Size import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.drawscope.DrawScope import androidx.compose.ui.graphics.vector.ImageVector +import centre.sciprog.maps.GmcBox typealias FeatureId = String @@ -15,7 +18,7 @@ interface FeatureBuilder { fun build(): SnapshotStateMap } -internal class MapFeatureBuilder(initialFeatures: Map) : FeatureBuilder { +internal class MapFeatureBuilder(initialFeatures: Map) : FeatureBuilder { private val content: SnapshotStateMap = mutableStateMapOf().apply { putAll(initialFeatures) @@ -41,6 +44,21 @@ fun FeatureBuilder.circle( id, MapCircleFeature(centerCoordinates.toCoordinates(), zoomRange, size, color) ) +fun FeatureBuilder.custom( + position: Pair, + id: FeatureId? = null, + customFeatureBuilder: DrawScope.(Offset) -> Unit, +) = addFeature(id, object : MapCustomFeature(position = position.toCoordinates()) { + 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( aCoordinates: Pair, bCoordinates: Pair, diff --git a/src/commonMain/kotlin/centre/sciprog/maps/compose/MapFeature.kt b/src/commonMain/kotlin/centre/sciprog/maps/compose/MapFeature.kt index 700e7aa..8b7defb 100644 --- a/src/commonMain/kotlin/centre/sciprog/maps/compose/MapFeature.kt +++ b/src/commonMain/kotlin/centre/sciprog/maps/compose/MapFeature.kt @@ -1,9 +1,11 @@ package centre.sciprog.maps.compose import androidx.compose.runtime.Composable +import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Size import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.ImageBitmap +import androidx.compose.ui.graphics.drawscope.DrawScope import androidx.compose.ui.graphics.painter.Painter import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.graphics.vector.rememberVectorPainter @@ -31,6 +33,13 @@ class MapFeatureSelector(val selector: (zoom: Int) -> MapFeature) : MapFeature(d } +abstract class MapCustomFeature( + zoomRange: IntRange = defaultZoomRange, + val position: GeodeticMapCoordinates +) : MapFeature(zoomRange) { + abstract fun drawFeature(drawScope: DrawScope, offset: Offset) +} + class MapCircleFeature( val center: GeodeticMapCoordinates, zoomRange: IntRange = defaultZoomRange, @@ -68,7 +77,7 @@ class MapBitmapImageFeature( } -class MapVectorImageFeature ( +class MapVectorImageFeature( val position: GeodeticMapCoordinates, val painter: Painter, val size: Size, diff --git a/src/jvmMain/kotlin/Main.kt b/src/jvmMain/kotlin/Main.kt index 7e60182..18594d4 100644 --- a/src/jvmMain/kotlin/Main.kt +++ b/src/jvmMain/kotlin/Main.kt @@ -6,6 +6,7 @@ import androidx.compose.material.Text import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Home import androidx.compose.runtime.* +import androidx.compose.ui.geometry.Size import androidx.compose.ui.graphics.Color import androidx.compose.ui.window.Window import androidx.compose.ui.window.application @@ -53,12 +54,22 @@ fun App() { ) { val pointOne = 55.568548 to 37.568604 val pointTwo = 55.929444 to 37.518434 + val pointThree = 60.929444 to 37.518434 image(pointOne, Icons.Filled.Home) //remember feature Id - val circleId: FeatureId = circle(pointTwo) + val circleId: FeatureId = circle( + centerCoordinates = pointTwo, + ) + custom(position = pointThree) { + drawRect( + color = Color.Red, + topLeft = it, + size = Size(20f, 20f) + ) + } line(pointOne, pointTwo) text(pointOne, "Home") diff --git a/src/jvmMain/kotlin/centre/sciprog/maps/compose/MapViewJvm.kt b/src/jvmMain/kotlin/centre/sciprog/maps/compose/MapViewJvm.kt index 9403e85..8ad0233 100644 --- a/src/jvmMain/kotlin/centre/sciprog/maps/compose/MapViewJvm.kt +++ b/src/jvmMain/kotlin/centre/sciprog/maps/compose/MapViewJvm.kt @@ -222,7 +222,10 @@ actual fun MapView( feature.color.toPaint() ) } - + is MapCustomFeature -> drawIntoCanvas { canvas -> + val offset = feature.position.toOffset() + feature.drawFeature(this, offset) + } } }