Fix scheme

This commit is contained in:
Alexander Nozik 2023-01-05 17:24:27 +03:00
parent 70fe9cf97a
commit 69e7b058d2
5 changed files with 25 additions and 31 deletions

View File

@ -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

View File

@ -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)

View File

@ -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<XY>.background(
ScalableImageFeature(
space,
box,
zoomRange = defaultZoomRange,
painter = painter,
attributes = Attributes(ZAttribute, -100f)
)
@ -39,26 +37,21 @@ fun FeatureGroup<XY>.background(
fun FeatureGroup<XY>.circle(
centerCoordinates: Pair<Number, Number>,
zoomRange: FloatRange = defaultZoomRange,
size: Dp = 5.dp,
color: Color = Color.Red,
id: String? = null,
): FeatureId<CircleFeature<XY>> = circle(centerCoordinates.toCoordinates(), zoomRange, size, color, id = id)
): FeatureId<CircleFeature<XY>> = circle(centerCoordinates.toCoordinates(), size, id = id)
fun FeatureGroup<XY>.draw(
position: Pair<Number, Number>,
zoomRange: FloatRange = defaultZoomRange,
id: String? = null,
draw: DrawScope.() -> Unit,
): FeatureId<DrawFeature<XY>> = draw(position.toCoordinates(), zoomRange = zoomRange, id = id, draw = draw)
): FeatureId<DrawFeature<XY>> = draw(position.toCoordinates(), id = id, draw = draw)
fun FeatureGroup<XY>.line(
aCoordinates: Pair<Number, Number>,
bCoordinates: Pair<Number, Number>,
scaleRange: FloatRange = defaultZoomRange,
color: Color = Color.Red,
id: String? = null,
): FeatureId<LineFeature<XY>> = line(aCoordinates.toCoordinates(), bCoordinates.toCoordinates(), scaleRange, color, id)
): FeatureId<LineFeature<XY>> = line(aCoordinates.toCoordinates(), bCoordinates.toCoordinates(), id)
public fun FeatureGroup<XY>.arc(
@ -66,15 +59,11 @@ public fun FeatureGroup<XY>.arc(
radius: Float,
startAngle: Float,
arcLength: Float,
zoomRange: FloatRange = defaultZoomRange,
color: Color = Color.Red,
id: String? = null,
): FeatureId<ArcFeature<XY>> = arc(
oval = XYCoordinateSpace.Rectangle(center.toCoordinates(), radius, radius),
startAngle = startAngle,
arcLength = arcLength,
zoomRange = zoomRange,
color = color,
id = id
)
@ -82,16 +71,13 @@ fun FeatureGroup<XY>.image(
position: Pair<Number, Number>,
image: ImageVector,
size: DpSize = DpSize(image.defaultWidth, image.defaultHeight),
zoomRange: FloatRange = defaultZoomRange,
id: String? = null,
): FeatureId<VectorImageFeature<XY>> =
image(position.toCoordinates(), image, size = size, zoomRange = zoomRange, id = id)
image(position.toCoordinates(), image, size = size, id = id)
fun FeatureGroup<XY>.text(
position: Pair<Number, Number>,
text: String,
zoomRange: FloatRange = defaultZoomRange,
color: Color = Color.Red,
id: String? = null,
): FeatureId<TextFeature<XY>> = text(position.toCoordinates(), text, zoomRange, color, id = id)
): FeatureId<TextFeature<XY>> = text(position.toCoordinates(), text, id = id)

View File

@ -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<PainterFeature<XY>>().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)

View File

@ -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<XY>.generateSvg(
fun SvgDrawScope.drawFeature(scale: Float, feature: Feature<XY>) {
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<XY>.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<XY>.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<XY>.generateSvg(
offset.x + 5,
offset.y - 5,
java.awt.Font(null, PLAIN, 16),
feature.color
color
)
}