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,7 +75,6 @@ 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
@ -80,5 +83,4 @@ fun MapVectorImageFeature(
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,26 +112,9 @@ actual fun MapView(
} }
}.fillMaxSize() }.fillMaxSize()
fun DrawScope.drawFeature(zoom: Int, feature: MapFeature){
Canvas(canvasModifier) {
if (canvasSize != size) {
canvasSize = size
logger.debug { "Redraw canvas. Size: $size" }
}
clipRect {
mapTiles.forEach { (id, image) ->
//converting back from tile index to screen offset
val offset = Offset(
(canvasSize.width / 2 - centerCoordinates.x + mapTileProvider.toCoordinate(id.i)).toFloat(),
(canvasSize.height / 2 - centerCoordinates.y + mapTileProvider.toCoordinate(id.j)).toFloat()
)
drawImage(
image = image,
topLeft = offset
)
}
features.filter { zoom in it.zoomRange }.forEach { feature ->
when (feature) { when (feature) {
is MapFeatureSelector -> drawFeature(zoom, feature.selector(zoom))
is MapCircleFeature -> drawCircle( is MapCircleFeature -> drawCircle(
feature.color, feature.color,
feature.size, feature.size,
@ -159,6 +143,28 @@ actual fun MapView(
} }
} }
Canvas(canvasModifier) {
if (canvasSize != size) {
canvasSize = size
logger.debug { "Redraw canvas. Size: $size" }
}
clipRect {
mapTiles.forEach { (id, image) ->
//converting back from tile index to screen offset
val offset = Offset(
(canvasSize.width / 2 - centerCoordinates.x + mapTileProvider.toCoordinate(id.i)).toFloat(),
(canvasSize.height / 2 - centerCoordinates.y + mapTileProvider.toCoordinate(id.j)).toFloat()
)
drawImage(
image = image,
topLeft = offset
)
}
features.filter { zoom in it.zoomRange }.forEach { feature ->
drawFeature(zoom,feature)
}
} }
} }
} }