From 69e7b058d2060514e1204de8c893cbe453a94705 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Thu, 5 Jan 2023 17:24:27 +0300 Subject: [PATCH] Fix scheme --- demo/scheme/src/jvmMain/kotlin/Main.kt | 10 ++++---- .../center/sciprog/maps/compose/mapViewJvm.kt | 3 ++- .../sciprog/maps/scheme/schemeFeatures.kt | 24 ++++--------------- .../center/sciprog/maps/scheme/SchemeView.kt | 5 ++-- .../center/sciprog/maps/svg/exportToSvg.kt | 14 +++++++---- 5 files changed, 25 insertions(+), 31 deletions(-) diff --git a/demo/scheme/src/jvmMain/kotlin/Main.kt b/demo/scheme/src/jvmMain/kotlin/Main.kt index 45c7360..765b49d 100644 --- a/demo/scheme/src/jvmMain/kotlin/Main.kt +++ b/demo/scheme/src/jvmMain/kotlin/Main.kt @@ -31,10 +31,10 @@ fun App() { val schemeFeaturesState = FeatureGroup.remember(XYCoordinateSpace) { background(1600f, 1200f) { painterResource("middle-earth.jpg") } - circle(410.52737 to 868.7676, color = Color.Blue) - text(410.52737 to 868.7676, "Shire", color = Color.Blue) - circle(1132.0881 to 394.99127, color = Color.Red) - text(1132.0881 to 394.99127, "Ordruin", color = Color.Red) + circle(410.52737 to 868.7676).color(Color.Blue) + text(410.52737 to 868.7676, "Shire").color(Color.Blue) + circle(1132.0881 to 394.99127).color(Color.Red) + text(1132.0881 to 394.99127, "Ordruin").color(Color.Red) arc(center = 1132.0881 to 394.99127, radius = 20f, startAngle = 0f, 2 * PI.toFloat()) circle(410.52737 to 868.7676, id = "hobbit") @@ -44,7 +44,7 @@ fun App() { while (isActive) { val x = 410.52737 + t * (1132.0881 - 410.52737) val y = 868.7676 + t * (394.99127 - 868.7676) - circle(x to y, color = Color.Green, id = "hobbit") + circle(x to y, id = "hobbit").color(Color.Green) delay(100) t += 0.005 if (t >= 1.0) t = 0.0 diff --git a/maps-kt-compose/src/jvmMain/kotlin/center/sciprog/maps/compose/mapViewJvm.kt b/maps-kt-compose/src/jvmMain/kotlin/center/sciprog/maps/compose/mapViewJvm.kt index 5704dd9..3384e26 100644 --- a/maps-kt-compose/src/jvmMain/kotlin/center/sciprog/maps/compose/mapViewJvm.kt +++ b/maps-kt-compose/src/jvmMain/kotlin/center/sciprog/maps/compose/mapViewJvm.kt @@ -14,6 +14,7 @@ import androidx.compose.ui.graphics.toComposeImageBitmap import androidx.compose.ui.unit.IntOffset import androidx.compose.ui.unit.IntSize import androidx.compose.ui.unit.dp +import center.sciprog.attributes.z import center.sciprog.maps.coordinates.Gmc import center.sciprog.maps.features.FeatureGroup import center.sciprog.maps.features.PainterFeature @@ -120,7 +121,7 @@ public actual fun MapView( ) } - featuresState.features + featuresState.featureMap.values.sortedBy { it.z } .filter { viewPoint.zoom in it.zoomRange } .forEach { feature -> drawFeature(mapState, painterCache, feature) diff --git a/maps-kt-scheme/src/commonMain/kotlin/center/sciprog/maps/scheme/schemeFeatures.kt b/maps-kt-scheme/src/commonMain/kotlin/center/sciprog/maps/scheme/schemeFeatures.kt index 33acdf0..74cc049 100644 --- a/maps-kt-scheme/src/commonMain/kotlin/center/sciprog/maps/scheme/schemeFeatures.kt +++ b/maps-kt-scheme/src/commonMain/kotlin/center/sciprog/maps/scheme/schemeFeatures.kt @@ -1,7 +1,6 @@ package center.sciprog.maps.scheme import androidx.compose.runtime.Composable -import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.drawscope.DrawScope import androidx.compose.ui.graphics.painter.Painter import androidx.compose.ui.graphics.vector.ImageVector @@ -30,7 +29,6 @@ fun FeatureGroup.background( ScalableImageFeature( space, box, - zoomRange = defaultZoomRange, painter = painter, attributes = Attributes(ZAttribute, -100f) ) @@ -39,26 +37,21 @@ fun FeatureGroup.background( fun FeatureGroup.circle( centerCoordinates: Pair, - zoomRange: FloatRange = defaultZoomRange, size: Dp = 5.dp, - color: Color = Color.Red, id: String? = null, -): FeatureId> = circle(centerCoordinates.toCoordinates(), zoomRange, size, color, id = id) +): FeatureId> = circle(centerCoordinates.toCoordinates(), size, id = id) fun FeatureGroup.draw( position: Pair, - zoomRange: FloatRange = defaultZoomRange, id: String? = null, draw: DrawScope.() -> Unit, -): FeatureId> = draw(position.toCoordinates(), zoomRange = zoomRange, id = id, draw = draw) +): FeatureId> = draw(position.toCoordinates(), id = id, draw = draw) fun FeatureGroup.line( aCoordinates: Pair, bCoordinates: Pair, - scaleRange: FloatRange = defaultZoomRange, - color: Color = Color.Red, id: String? = null, -): FeatureId> = line(aCoordinates.toCoordinates(), bCoordinates.toCoordinates(), scaleRange, color, id) +): FeatureId> = line(aCoordinates.toCoordinates(), bCoordinates.toCoordinates(), id) public fun FeatureGroup.arc( @@ -66,15 +59,11 @@ public fun FeatureGroup.arc( radius: Float, startAngle: Float, arcLength: Float, - zoomRange: FloatRange = defaultZoomRange, - color: Color = Color.Red, id: String? = null, ): FeatureId> = arc( oval = XYCoordinateSpace.Rectangle(center.toCoordinates(), radius, radius), startAngle = startAngle, arcLength = arcLength, - zoomRange = zoomRange, - color = color, id = id ) @@ -82,16 +71,13 @@ fun FeatureGroup.image( position: Pair, image: ImageVector, size: DpSize = DpSize(image.defaultWidth, image.defaultHeight), - zoomRange: FloatRange = defaultZoomRange, id: String? = null, ): FeatureId> = - image(position.toCoordinates(), image, size = size, zoomRange = zoomRange, id = id) + image(position.toCoordinates(), image, size = size, id = id) fun FeatureGroup.text( position: Pair, text: String, - zoomRange: FloatRange = defaultZoomRange, - color: Color = Color.Red, id: String? = null, -): FeatureId> = text(position.toCoordinates(), text, zoomRange, color, id = id) +): FeatureId> = text(position.toCoordinates(), text, id = id) diff --git a/maps-kt-scheme/src/jvmMain/kotlin/center/sciprog/maps/scheme/SchemeView.kt b/maps-kt-scheme/src/jvmMain/kotlin/center/sciprog/maps/scheme/SchemeView.kt index f333949..3a76515 100644 --- a/maps-kt-scheme/src/jvmMain/kotlin/center/sciprog/maps/scheme/SchemeView.kt +++ b/maps-kt-scheme/src/jvmMain/kotlin/center/sciprog/maps/scheme/SchemeView.kt @@ -11,6 +11,7 @@ import androidx.compose.ui.graphics.drawscope.Stroke import androidx.compose.ui.graphics.drawscope.clipRect import androidx.compose.ui.graphics.painter.Painter import androidx.compose.ui.unit.DpSize +import center.sciprog.attributes.z import center.sciprog.maps.compose.mapControls import center.sciprog.maps.features.* import mu.KotlinLogging @@ -30,7 +31,7 @@ public fun SchemeView( featuresState.features.filterIsInstance>().associateWith { it.getPainter() } } - Canvas(modifier = modifier.mapControls(state, featuresState.features).fillMaxSize()) { + Canvas(modifier = modifier.mapControls(state, featuresState).fillMaxSize()) { if (canvasSize != size.toDpSize()) { canvasSize = size.toDpSize() @@ -38,7 +39,7 @@ public fun SchemeView( } clipRect { - featuresState.features + featuresState.featureMap.values.sortedBy { it.z } .filter { viewPoint.zoom in it.zoomRange } .forEach { background -> drawFeature(state, painterCache, background) diff --git a/maps-kt-scheme/src/jvmMain/kotlin/center/sciprog/maps/svg/exportToSvg.kt b/maps-kt-scheme/src/jvmMain/kotlin/center/sciprog/maps/svg/exportToSvg.kt index 03a6fef..89b13a8 100644 --- a/maps-kt-scheme/src/jvmMain/kotlin/center/sciprog/maps/svg/exportToSvg.kt +++ b/maps-kt-scheme/src/jvmMain/kotlin/center/sciprog/maps/svg/exportToSvg.kt @@ -3,10 +3,12 @@ package center.sciprog.maps.svg 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.drawscope.Stroke import androidx.compose.ui.graphics.drawscope.drawIntoCanvas import androidx.compose.ui.graphics.drawscope.translate import androidx.compose.ui.graphics.painter.Painter +import center.sciprog.attributes.AlphaAttribute import center.sciprog.maps.features.* import center.sciprog.maps.scheme.* import org.jfree.svg.SVGGraphics2D @@ -41,6 +43,10 @@ fun FeatureStateSnapshot.generateSvg( fun SvgDrawScope.drawFeature(scale: Float, feature: Feature) { + + val color = feature.color ?: Color.Red + val alpha = feature.attributes[AlphaAttribute]?:1f + when (feature) { is ScalableImageFeature -> { val offset = XY(feature.rectangle.left, feature.rectangle.top).toOffset() @@ -59,12 +65,12 @@ fun FeatureStateSnapshot.generateSvg( is FeatureSelector -> drawFeature(scale, feature.selector(scale)) is CircleFeature -> drawCircle( - feature.color, + color, feature.size.toPx(), center = feature.center.toOffset() ) - is LineFeature -> drawLine(feature.color, feature.a.toOffset(), feature.b.toOffset()) + is LineFeature -> drawLine(color, feature.a.toOffset(), feature.b.toOffset()) is ArcFeature -> { val topLeft = feature.oval.leftTop.toOffset() @@ -73,7 +79,7 @@ fun FeatureStateSnapshot.generateSvg( val size = Size(abs(topLeft.x - bottomRight.x), abs(topLeft.y - bottomRight.y)) drawArc( - color = feature.color, + color = color, startAngle = (feature.startAngle * 180 / PI).toFloat(), sweepAngle = (feature.arcLength * 180 / PI).toFloat(), useCenter = false, @@ -102,7 +108,7 @@ fun FeatureStateSnapshot.generateSvg( offset.x + 5, offset.y - 5, java.awt.Font(null, PLAIN, 16), - feature.color + color ) }