From 92abd3db43d89d4c2176a6207341957ff6712f0e Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 11 Jul 2022 14:46:11 +0300 Subject: [PATCH] Add MapFeatureSelector --- build.gradle.kts | 6 +- .../centre/sciprog/maps/compose/MapFeature.kt | 14 +++-- .../centre/sciprog/maps/compose/MapViewJvm.kt | 62 ++++++++++--------- 3 files changed, 45 insertions(+), 37 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 999905e..c689877 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,10 +1,10 @@ import org.jetbrains.compose.compose import org.jetbrains.compose.desktop.application.dsl.TargetFormat -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { kotlin("multiplatform") id("org.jetbrains.compose") + `maven-publish` } group = "center.sciptog" @@ -31,15 +31,15 @@ kotlin { api(compose.runtime) api(compose.foundation) api(compose.material) - implementation("io.ktor:ktor-client-core:$ktorVersion") + api("io.ktor:ktor-client-core:$ktorVersion") implementation("io.ktor:ktor-client-cio:$ktorVersion") implementation("io.github.microutils:kotlin-logging:2.1.23") - implementation("ch.qos.logback:logback-classic:1.2.11") } } val jvmMain by getting { dependencies { implementation(compose.desktop.currentOs) + implementation("ch.qos.logback:logback-classic:1.2.11") } } val jvmTest by getting diff --git a/src/commonMain/kotlin/centre/sciprog/maps/compose/MapFeature.kt b/src/commonMain/kotlin/centre/sciprog/maps/compose/MapFeature.kt index 83215f1..a74d1c6 100644 --- a/src/commonMain/kotlin/centre/sciprog/maps/compose/MapFeature.kt +++ b/src/commonMain/kotlin/centre/sciprog/maps/compose/MapFeature.kt @@ -13,9 +13,14 @@ import centre.sciprog.maps.GeodeticMapCoordinates //TODO replace zoom range with zoom-based representation change sealed class MapFeature(val zoomRange: IntRange) +internal fun Pair.toCoordinates() = GeodeticMapCoordinates.ofDegrees(first, second) + private val defaultZoomRange = 1..18 -internal fun Pair.toCoordinates() = GeodeticMapCoordinates.ofDegrees(first, second) +/** + * A feature that decides what to show depending on the zoom value (it could change size of shape) + */ +class MapFeatureSelector(val selector: (zoom: Int) -> MapFeature) : MapFeature(defaultZoomRange) class MapCircleFeature( val center: GeodeticMapCoordinates, @@ -62,7 +67,6 @@ class MapBitmapImageFeature( val image: ImageBitmap, val size: IntSize = IntSize(15, 15), zoomRange: IntRange = defaultZoomRange, - val color: Color = Color.Red, ) : MapFeature(zoomRange) @@ -71,14 +75,12 @@ class MapVectorImageFeature internal constructor( val painter: VectorPainter, val size: Size, zoomRange: IntRange = defaultZoomRange, - val color: Color = Color.Red, ) : MapFeature(zoomRange) @Composable fun MapVectorImageFeature( position: GeodeticMapCoordinates, image: ImageVector, - size: Size = Size(20f,20f), + size: Size = Size(20f, 20f), zoomRange: IntRange = defaultZoomRange, - color: Color = Color.Red, -): MapVectorImageFeature = MapVectorImageFeature(position, rememberVectorPainter(image), size, zoomRange, color) \ No newline at end of file +): MapVectorImageFeature = MapVectorImageFeature(position, rememberVectorPainter(image), size, zoomRange) \ No newline at end of file diff --git a/src/jvmMain/kotlin/centre/sciprog/maps/compose/MapViewJvm.kt b/src/jvmMain/kotlin/centre/sciprog/maps/compose/MapViewJvm.kt index 7a83fe8..39c34e1 100644 --- a/src/jvmMain/kotlin/centre/sciprog/maps/compose/MapViewJvm.kt +++ b/src/jvmMain/kotlin/centre/sciprog/maps/compose/MapViewJvm.kt @@ -9,6 +9,7 @@ import androidx.compose.ui.Modifier 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.drawscope.clipRect import androidx.compose.ui.graphics.drawscope.drawIntoCanvas import androidx.compose.ui.graphics.drawscope.translate @@ -111,6 +112,38 @@ actual fun MapView( } }.fillMaxSize() + fun DrawScope.drawFeature(zoom: Int, feature: MapFeature){ + when (feature) { + is MapFeatureSelector -> drawFeature(zoom, feature.selector(zoom)) + is MapCircleFeature -> drawCircle( + feature.color, + feature.size, + center = feature.center.toOffset() + ) + is MapLineFeature -> drawLine(feature.color, feature.a.toOffset(), feature.b.toOffset()) + is MapBitmapImageFeature -> drawImage(feature.image, feature.position.toOffset()) + is MapVectorImageFeature -> { + val offset = feature.position.toOffset() + translate(offset.x - feature.size.width / 2, offset.y - feature.size.height / 2) { + with(feature.painter) { + draw(feature.size) + } + } + } + is MapTextFeature -> drawIntoCanvas { canvas -> + val offset = feature.position.toOffset() + canvas.nativeCanvas.drawString( + feature.text, + offset.x + 5, + offset.y - 5, + Font().apply { size = 16f }, + feature.color.toPaint() + ) + } + + } + } + Canvas(canvasModifier) { if (canvasSize != size) { @@ -130,34 +163,7 @@ actual fun MapView( ) } features.filter { zoom in it.zoomRange }.forEach { feature -> - when (feature) { - is MapCircleFeature -> drawCircle( - feature.color, - feature.size, - center = feature.center.toOffset() - ) - is MapLineFeature -> drawLine(feature.color, feature.a.toOffset(), feature.b.toOffset()) - is MapBitmapImageFeature -> drawImage(feature.image, feature.position.toOffset()) - is MapVectorImageFeature -> { - val offset = feature.position.toOffset() - translate(offset.x - feature.size.width / 2, offset.y - feature.size.height / 2) { - with(feature.painter) { - draw(feature.size) - } - } - } - is MapTextFeature -> drawIntoCanvas { canvas -> - val offset = feature.position.toOffset() - canvas.nativeCanvas.drawString( - feature.text, - offset.x + 5, - offset.y - 5, - Font().apply { size = 16f }, - feature.color.toPaint() - ) - } - - } + drawFeature(zoom,feature) } } }