/
LayoutApi.kt
475 lines (423 loc) · 16.1 KB
/
LayoutApi.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
@file:Suppress("unused")
package ovh.plrapps.mapcompose.api
import androidx.compose.animation.core.AnimationSpec
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.SpringSpec
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.platform.ViewConfiguration
import androidx.compose.ui.unit.IntSize
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import ovh.plrapps.mapcompose.ui.layout.Fill
import ovh.plrapps.mapcompose.ui.layout.Fit
import ovh.plrapps.mapcompose.ui.layout.Forced
import ovh.plrapps.mapcompose.ui.layout.MinimumScaleMode
import ovh.plrapps.mapcompose.ui.state.MapState
import ovh.plrapps.mapcompose.ui.state.ZoomPanRotateState
import ovh.plrapps.mapcompose.utils.AngleDegree
import ovh.plrapps.mapcompose.utils.Point
import ovh.plrapps.mapcompose.utils.rotate
import ovh.plrapps.mapcompose.utils.rotateCenteredX
import ovh.plrapps.mapcompose.utils.rotateCenteredY
import ovh.plrapps.mapcompose.utils.scaleAxis
import ovh.plrapps.mapcompose.utils.toRad
import ovh.plrapps.mapcompose.utils.withRetry
import kotlin.math.min
/**
* The scale of the map. By convention, the scale at full dimension is 1f.
*/
var MapState.scale: Float
get() = zoomPanRotateState.scale
set(value) {
zoomPanRotateState.setScale(value)
}
/**
* The [rotation] property is the angle (in decimal degrees) of rotation,
* using the center of the view as the pivot point.
*/
var MapState.rotation: AngleDegree
get() = zoomPanRotateState.rotation
set(value) {
zoomPanRotateState.setRotation(value)
}
/**
* Get the current [scroll] - the position of the top-left corner of the visible viewport.
* This is a low-level concept (returned value is in scaled pixels).
*/
val MapState.scroll: Offset
get() = Offset(zoomPanRotateState.scrollX, zoomPanRotateState.scrollY)
/**
* Set the [scroll] - the position of the top-left corner of the visible viewport. This is a
* suspending call because it's required to wait the first composition. Otherwise, it's invoked
* immediately.
* This is a low-level concept (input value is expected to be in scaled pixels). To scroll to a
* known position, prefer the [snapScrollTo] API.
*/
suspend fun MapState.setScroll(offset: Offset) {
with(zoomPanRotateState) {
awaitLayout()
setScroll(offset.x, offset.y)
}
}
/**
* Get notified whenever the state ([scale] and/or [scroll] and/or [rotation]) changes.
*
* @param cb An extension function with [MapState] as receiver type
*/
fun MapState.setStateChangeListener(cb: MapState.() -> Unit) {
stateChangeListener = cb
}
/**
* Removes the state change listener.
*/
fun MapState.removeStateChangeListener() {
stateChangeListener = null
}
/**
* On double-tap, and if the scale is already at its maximum value, circle-back to the minimum scale.
*/
var MapState.shouldLoopScale
get() = zoomPanRotateState.shouldLoopScale
set(value) {
zoomPanRotateState.shouldLoopScale = value
}
/**
* Enable the rotation by user gestures.
*/
fun MapState.enableRotation() {
zoomPanRotateState.isRotationEnabled = true
}
/**
* Discard rotation gestures. The map can still be programmatically rotated using APIs such as
* [rotateTo] or [rotation].
*/
fun MapState.disableRotation() {
zoomPanRotateState.isRotationEnabled = false
}
/**
* Set the minimum scale mode. See [MinimumScaleMode].
* The minimum scale can be manually defined using [Forced], or can be inferred using [Fill], or
* [Fit] (the default).
* Note: When enabling map rotation, it's advised to use the [Fill] mode.
*/
var MapState.minimumScaleMode: MinimumScaleMode
get() = zoomPanRotateState.minimumScaleMode
set(value) {
zoomPanRotateState.minimumScaleMode = value
}
/**
* The default maximum scale is 2f.
* When changed, and if the current scale is greater than the new [maxScale], the current scale is
* changed to be equal to [maxScale].
*/
var MapState.maxScale: Float
get() = zoomPanRotateState.maxScale
set(value) {
zoomPanRotateState.maxScale = value
}
/**
* The scroll offset ratio allows to scroll past the default scroll limits. They are expressed in
* percentage of the layout dimensions.
* Values must be in [0f..1f] range, or an [IllegalArgumentException] is thrown.
* Setting a scroll offset ratio is useful when rotation is enabled, so that edges of the map are
* reachable.
*
* @param xRatio The horizontal scroll offset ratio. The scroll offset will be equal to this ratio
* multiplied by the layout width.
* @param yRatio The vertical scroll offset ratio. The scroll offset will be equal to this ratio
* multiplied by the layout height.
*/
fun MapState.setScrollOffsetRatio(xRatio: Float, yRatio: Float) {
zoomPanRotateState.scrollOffsetRatio = Offset(xRatio, yRatio)
}
/**
* Rotates to the specified [angle] in decimal degrees, animating the rotation.
*/
suspend fun MapState.rotateTo(
angle: AngleDegree,
animationSpec: AnimationSpec<Float> = SpringSpec(stiffness = Spring.StiffnessLow)
) {
withRetry(maxAnimationsRetries, animationsRetriesInterval) {
zoomPanRotateState.smoothRotateTo(angle, animationSpec)
}
}
/**
* Scrolls to a position. Defaults to centering on the provided scroll destination.
*
* @param x The normalized X position on the map, in range [0..1]
* @param y The normalized Y position on the map, in range [0..1]
* @param screenOffset Offset of the screen relatively to its dimension. Default is
* Offset(-0.5f, -0.5f), so moving the screen by half the width left and by half the height top,
* effectively centering on the scroll destination.
*/
suspend fun MapState.snapScrollTo(
x: Double,
y: Double,
screenOffset: Offset = Offset(-0.5f, -0.5f)
) {
with(zoomPanRotateState) {
awaitLayout()
val offsetX = screenOffset.x * layoutSize.width
val offsetY = screenOffset.y * layoutSize.height
val destScrollX = (x * fullWidth * scale + offsetX).toFloat()
val destScrollY = (y * fullHeight * scale + offsetY).toFloat()
setScroll(destScrollX, destScrollY)
}
}
/**
* Scrolls to a position, animating the scroll and the scale. Defaults to centering on the provided
* scroll destination.
*
* @param x The normalized X position on the map, in range [0..1]
* @param y The normalized Y position on the map, in range [0..1]
* @param destScale The destination scale. The default value is the current scale.
* @param animationSpec The [AnimationSpec]. Default is [SpringSpec] with low stiffness.
* @param screenOffset Offset of the screen relatively to its dimension. Default is
* Offset(-0.5f, -0.5f), so moving the screen by half the width left and by half the height top,
* effectively centering on the scroll destination.
*/
suspend fun MapState.scrollTo(
x: Double,
y: Double,
destScale: Float = scale,
animationSpec: AnimationSpec<Float> = SpringSpec(stiffness = Spring.StiffnessLow),
screenOffset: Offset = Offset(-0.5f, -0.5f)
) {
with(zoomPanRotateState) {
awaitLayout()
val offsetX = screenOffset.x * layoutSize.width
val offsetY = screenOffset.y * layoutSize.height
val effectiveDstScale = constrainScale(destScale)
val destScrollX = (x * fullWidth * effectiveDstScale + offsetX).toFloat()
val destScrollY = (y * fullHeight * effectiveDstScale + offsetY).toFloat()
withRetry(maxAnimationsRetries, animationsRetriesInterval) {
smoothScrollAndScale(
destScrollX,
destScrollY,
destScale,
animationSpec
)
}
}
}
/**
* Scrolls to an area. The target position will be centered on the area, scaled in as much as
* possible while still keeping the area plus the provided padding completely in view.
*
* @param area The [BoundingBox] of the target area to scroll to.
* @param padding Padding around the area defined as a fraction of the viewport.
*/
suspend fun MapState.snapScrollTo(
area: BoundingBox,
padding: Offset = Offset(0f, 0f)
) {
with(zoomPanRotateState) {
awaitLayout()
val (center, scale) = calculateScrollTo(area, padding)
setScale(scale)
snapScrollTo(center.x, center.y)
}
}
/**
* Scrolls to an area, animating the scroll and the scale. The target position will be centered
* on the area, scaled in as much as possible while still keeping the area plus the provided
* padding completely in view.
*
* @param area The [BoundingBox] of the target area to scroll to.
* @param padding Padding around the area defined as a fraction of the viewport.
* @param animationSpec The [AnimationSpec]. Default is [SpringSpec] with low stiffness.
*/
suspend fun MapState.scrollTo(
area: BoundingBox,
padding: Offset = Offset(0f, 0f),
animationSpec: AnimationSpec<Float> = SpringSpec(stiffness = Spring.StiffnessLow),
) {
with(zoomPanRotateState) {
awaitLayout()
val (center, scale) = calculateScrollTo(area, padding)
scrollTo(center.x, center.y, scale, animationSpec)
}
}
/**
* Calculates the target scroll position and scale that will be centered on the given [area] and
* scaled in as much as possible while keeping the [area] plus [padding] in view.
*
* @return The scroll position and scale, as a [Pair].
*/
private fun ZoomPanRotateState.calculateScrollTo(
area: BoundingBox,
padding: Offset
): Pair<Point, Float> {
val centerX = (area.xLeft + area.xRight) / 2
val centerY = (area.yTop + area.yBottom) / 2
val xAxisScale = fullHeight / fullWidth.toDouble()
val normalizedArea = area.scaleAxis(1 / xAxisScale)
val rotatedNormalizedArea =
normalizedArea.rotate(Point(centerX / xAxisScale, centerY), -rotation.toRad())
val rotatedArea = rotatedNormalizedArea.scaleAxis(xAxisScale)
val areaWidth = fullWidth * (rotatedArea.xRight - rotatedArea.xLeft)
val availableViewportWidth = layoutSize.width * (1 - padding.x)
val areaWidthLayoutFraction = areaWidth / availableViewportWidth
val horizontalScale = 1 / areaWidthLayoutFraction
val areaHeight = fullHeight * (rotatedArea.yBottom - rotatedArea.yTop)
val availableViewportHeight = layoutSize.height * (1 - padding.y)
val areaHeightLayoutFraction = areaHeight / availableViewportHeight
val verticalScale = 1 / areaHeightLayoutFraction
val targetScale = min(horizontalScale, verticalScale).toFloat()
val effectiveTargetScale = constrainScale(targetScale)
return Point(centerX, centerY) to effectiveTargetScale
}
/**
* The [centroidX] is the x coordinate of the center of rotation transformation. It changes with the
* scroll and the scale.
* This is a low-level concept, and is only useful when defining custom views.
* The value is a relative coordinate (in [0.0 .. 1.0] range).
*/
val MapState.centroidX: Double
get() = zoomPanRotateState.centroidX
/**
* The [centroidY] is the y coordinate of the center of rotation transformation. It changes with the
* scroll and the scale.
* This is a low-level concept, and is only useful when defining custom views.
* The value is a relative coordinate (in [0.0 .. 1.0] range).
*/
val MapState.centroidY: Double
get() = zoomPanRotateState.centroidY
/**
* A convenience property. It corresponds to the size used when creating the [MapState].
*/
val MapState.fullSize: IntSize
get() = IntSize(zoomPanRotateState.fullWidth, zoomPanRotateState.fullHeight)
/**
* Registers a tap callback for tap gestures. The callback is invoked with the relative coordinates
* of the tapped point on the map.
* Note: the tap gesture is detected only after the [ViewConfiguration.doubleTapMinTimeMillis] has
* passed, because the layout's gesture detector also detects double-tap gestures.
*/
fun MapState.onTap(tapCb: (x: Double, y: Double) -> Unit) {
zoomPanRotateState.tapCb = tapCb
}
/**
* Registers a callback for touch down event.
*/
fun MapState.onTouchDown(cb: () -> Unit) {
touchDownCb = cb
}
/**
* Stops all currently running animations. If other animations are scheduled to run (inside running
* coroutines), you might have to cancel those coroutines as well.
*/
suspend fun MapState.stopAnimations() {
zoomPanRotateState.stopAnimations()
}
/**
* Returns the visible area expressed in normalized coordinates. This does not account for rotation.
* When the map isn't rotated, the obtained [BoundingBox] represents the same area as the one
* obtained with the [visibleArea] API.
*/
suspend fun MapState.visibleBoundingBox(): BoundingBox {
return with(zoomPanRotateState) {
awaitLayout()
BoundingBox(
xLeft = centroidX - layoutSize.width / (2 * fullWidth * scale),
yTop = centroidY - layoutSize.height / (2 * fullHeight * scale),
xRight = centroidX + layoutSize.width / (2 * fullWidth * scale),
yBottom = centroidY + layoutSize.height / (2 * fullHeight * scale)
)
}
}
data class BoundingBox(val xLeft: Double, val yTop: Double, val xRight: Double, val yBottom: Double)
/**
* Returns the visible area expressed in normalized coordinates. This *does* account for rotation.
*
* @return The [VisibleArea], as follows:
* p1 p2
* ---------
* | |
* | |
* | |
* ---------
* p4 p3
*/
suspend fun MapState.visibleArea(): VisibleArea {
return with(zoomPanRotateState) {
awaitLayout()
val xLeft = centroidX - layoutSize.width / (2 * fullWidth * scale)
val yTop = centroidY - layoutSize.height / (2 * fullHeight * scale)
val xRight = centroidX + layoutSize.width / (2 * fullWidth * scale)
val yBottom = centroidY + layoutSize.height / (2 * fullHeight * scale)
val xAxisScale = fullHeight / fullWidth.toDouble()
val scaledCenterX = centroidX / xAxisScale
val p1x = rotateCenteredX(
xLeft / xAxisScale, yTop, scaledCenterX, centroidY, -rotation.toRad()
) * xAxisScale
val p1y = rotateCenteredY(
xLeft / xAxisScale, yTop, scaledCenterX, centroidY, -rotation.toRad()
)
val p2x = rotateCenteredX(
xRight / xAxisScale, yTop, scaledCenterX, centroidY, -rotation.toRad()
) * xAxisScale
val p2y = rotateCenteredY(
xRight / xAxisScale, yTop, scaledCenterX, centroidY, -rotation.toRad()
)
val p3x = rotateCenteredX(
xRight / xAxisScale, yBottom, scaledCenterX, centroidY, -rotation.toRad()
) * xAxisScale
val p3y = rotateCenteredY(
xRight / xAxisScale, yBottom, scaledCenterX, centroidY, -rotation.toRad()
)
val p4x = rotateCenteredX(
xLeft / xAxisScale, yBottom, scaledCenterX, centroidY, -rotation.toRad()
) * xAxisScale
val p4y = rotateCenteredY(
xLeft / xAxisScale, yBottom, scaledCenterX, centroidY, -rotation.toRad()
)
visibleAreaMutex.withLock {
val area = visibleArea
if (area == null) {
visibleArea = VisibleArea(p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y)
} else {
area._p1x = p1x
area._p1y = p1y
area._p2x = p2x
area._p2y = p2y
area._p3x = p3x
area._p3y = p3y
area._p4x = p4x
area._p4y = p4y
}
visibleArea as VisibleArea
}
}
}
data class VisibleArea(
internal var _p1x: Double,
internal var _p1y: Double,
internal var _p2x: Double,
internal var _p2y: Double,
internal var _p3x: Double,
internal var _p3y: Double,
internal var _p4x: Double,
internal var _p4y: Double,
) {
val p1x: Double
get() = _p1x
val p1y: Double
get() = _p1y
val p2x: Double
get() = _p2x
val p2y: Double
get() = _p2y
val p3x: Double
get() = _p3x
val p3y: Double
get() = _p3y
val p4x: Double
get() = _p4x
val p4y: Double
get() = _p4y
}
/* Internally, we're working on a single VisibleArea instance, and we must ensure mutual exclusion
* when creating the instance. */
internal val visibleAreaMutex = Mutex()
internal var visibleArea: VisibleArea? = null