Add MapFeatureSelector

This commit is contained in:
Alexander Nozik 2022-07-11 14:46:11 +03:00
parent 6868c1a5ca
commit 92abd3db43
No known key found for this signature in database
GPG Key ID: F7FCF2DD25C71357
3 changed files with 45 additions and 37 deletions

View File

@ -1,10 +1,10 @@
import org.jetbrains.compose.compose import org.jetbrains.compose.compose
import org.jetbrains.compose.desktop.application.dsl.TargetFormat import org.jetbrains.compose.desktop.application.dsl.TargetFormat
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins { plugins {
kotlin("multiplatform") kotlin("multiplatform")
id("org.jetbrains.compose") id("org.jetbrains.compose")
`maven-publish`
} }
group = "center.sciptog" group = "center.sciptog"
@ -31,15 +31,15 @@ kotlin {
api(compose.runtime) api(compose.runtime)
api(compose.foundation) api(compose.foundation)
api(compose.material) 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.ktor:ktor-client-cio:$ktorVersion")
implementation("io.github.microutils:kotlin-logging:2.1.23") implementation("io.github.microutils:kotlin-logging:2.1.23")
implementation("ch.qos.logback:logback-classic:1.2.11")
} }
} }
val jvmMain by getting { val jvmMain by getting {
dependencies { dependencies {
implementation(compose.desktop.currentOs) implementation(compose.desktop.currentOs)
implementation("ch.qos.logback:logback-classic:1.2.11")
} }
} }
val jvmTest by getting val jvmTest by getting

View File

@ -13,9 +13,14 @@ import centre.sciprog.maps.GeodeticMapCoordinates
//TODO replace zoom range with zoom-based representation change //TODO replace zoom range with zoom-based representation change
sealed class MapFeature(val zoomRange: IntRange) sealed class MapFeature(val zoomRange: IntRange)
internal fun Pair<Double, Double>.toCoordinates() = GeodeticMapCoordinates.ofDegrees(first, second)
private val defaultZoomRange = 1..18 private val defaultZoomRange = 1..18
internal fun Pair<Double, Double>.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( class MapCircleFeature(
val center: GeodeticMapCoordinates, val center: GeodeticMapCoordinates,
@ -62,7 +67,6 @@ class MapBitmapImageFeature(
val image: ImageBitmap, val image: ImageBitmap,
val size: IntSize = IntSize(15, 15), val size: IntSize = IntSize(15, 15),
zoomRange: IntRange = defaultZoomRange, zoomRange: IntRange = defaultZoomRange,
val color: Color = Color.Red,
) : MapFeature(zoomRange) ) : MapFeature(zoomRange)
@ -71,14 +75,12 @@ class MapVectorImageFeature internal constructor(
val painter: VectorPainter, val painter: VectorPainter,
val size: Size, val size: Size,
zoomRange: IntRange = defaultZoomRange, zoomRange: IntRange = defaultZoomRange,
val color: Color = Color.Red,
) : MapFeature(zoomRange) ) : MapFeature(zoomRange)
@Composable @Composable
fun MapVectorImageFeature( fun MapVectorImageFeature(
position: GeodeticMapCoordinates, position: GeodeticMapCoordinates,
image: ImageVector, image: ImageVector,
size: Size = Size(20f,20f), size: Size = Size(20f, 20f),
zoomRange: IntRange = defaultZoomRange, zoomRange: IntRange = defaultZoomRange,
color: Color = Color.Red, ): MapVectorImageFeature = MapVectorImageFeature(position, rememberVectorPainter(image), size, zoomRange)
): MapVectorImageFeature = MapVectorImageFeature(position, rememberVectorPainter(image), size, zoomRange, color)

View File

@ -9,6 +9,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size 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.clipRect import androidx.compose.ui.graphics.drawscope.clipRect
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.graphics.drawscope.translate import androidx.compose.ui.graphics.drawscope.translate
@ -111,6 +112,38 @@ actual fun MapView(
} }
}.fillMaxSize() }.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) { Canvas(canvasModifier) {
if (canvasSize != size) { if (canvasSize != size) {
@ -130,34 +163,7 @@ actual fun MapView(
) )
} }
features.filter { zoom in it.zoomRange }.forEach { feature -> features.filter { zoom in it.zoomRange }.forEach { feature ->
when (feature) { drawFeature(zoom,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()
)
}
}
} }
} }
} }