-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathLayerApi.kt
194 lines (168 loc) · 5.24 KB
/
LayerApi.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
@file:Suppress("unused")
package ovh.plrapps.mapcompose.api
import ovh.plrapps.mapcompose.core.*
import ovh.plrapps.mapcompose.ui.state.MapState
import ovh.plrapps.mapcompose.utils.swap
/**
* Add a layer. By default, the layer is added on top of the layer stack (see [AboveAll]).
* Optionally, the layer can be added at the bottom of the stack, or above / below an existing layer.
*
* Note that [initialOpacity] is taken into account _only_ if the layer being added isn't the lowest
* one, or the only one. However, if later on another layer is added below this layer, the
* [initialOpacity] will be taken into account.
*
* @return The id of the created layer
*/
fun MapState.addLayer(
tileStreamProvider: TileStreamProvider,
initialOpacity: Float = 1f,
placement: LayerPlacement = AboveAll
): String {
val layers = tileCanvasState.layerFlow.value.toMutableList()
val id = makeLayerId()
val layer = Layer(id, tileStreamProvider, initialOpacity)
val newLayers = when (placement) {
AboveAll -> {
layers + layer
}
is AboveLayer -> {
val existingLayerIndex = layers.indexOfFirst { it.id == placement.layerId }
if (existingLayerIndex != -1 && existingLayerIndex < layers.lastIndex) {
layers.add(existingLayerIndex + 1, layer)
}
layers
}
BelowAll -> {
layers.add(0, layer)
layers
}
is BelowLayer -> {
val existingLayerIndex = layers.indexOfFirst { it.id == placement.layerId }
if (existingLayerIndex != -1) {
layers.add(existingLayerIndex, layer)
}
layers
}
}
setLayers(newLayers)
return id
}
/**
* Replaces a layer. If the layer doesn't exist, no layer is added.
*
* @return The id of the added layer, or null if [layerId] doesn't match with any existing layer
*/
fun MapState.replaceLayer(
layerId: String,
tileStreamProvider: TileStreamProvider,
initialOpacity: Float = 1f
): String? {
val layers = tileCanvasState.layerFlow.value.toMutableList()
val index = layers.indexOfFirst {
it.id == layerId
}
val id = makeLayerId()
return if (index != -1) {
layers[index] = Layer(id, tileStreamProvider, initialOpacity)
setLayers(layers)
id
} else null
}
/**
* Moves a layer up in the layer stack, making it drawn on top of the layer which was previously
* above it.
*/
fun MapState.moveLayerUp(layerId: String) {
val layers = tileCanvasState.layerFlow.value.toMutableList()
val index = layers.indexOfFirst {
it.id == layerId
}
if (index < layers.lastIndex) {
layers.swap(index + 1, index)
setLayers(layers)
}
}
/**
* Moves a layer down in the layer stack, making it drawn below the layer which was previously
* below it.
*/
fun MapState.moveLayerDown(layerId: String) {
val layers = tileCanvasState.layerFlow.value.toMutableList()
val index = layers.indexOfFirst {
it.id == layerId
}
if (index > 0) {
layers.swap(index - 1, index)
setLayers(layers)
}
}
/**
* Remove the top layer from the stack.
*/
fun MapState.removeLastLayer() {
val layers = tileCanvasState.layerFlow.value.toMutableList()
val remainingLayers = layers.subList(0, layers.size - 1)
setLayers(remainingLayers)
}
/**
* Remove the top [n] layers from the stack.
* @param n The number of layers to remove.
*/
fun MapState.removeLastLayers(n: Int) {
val layers = tileCanvasState.layerFlow.value.toMutableList()
val remainingLayers = layers.subList(0, (layers.size - n).coerceAtLeast(0))
setLayers(remainingLayers)
}
/**
* Reorder layers in the order of the provided list of ids. Layers listed first will be drawn before
* subsequent layers (so the later will be above).
* Existing layers not included in the provided list will be removed
*/
fun MapState.reorderLayers(layerIds: List<String>) {
val layerForId = tileCanvasState.layerFlow.value.associateBy { it.id }
val layers = layerIds.mapNotNull { layerForId[it] }
setLayers(layers)
}
/**
* Remove all layers.
*/
fun MapState.removeAllLayers() {
setLayers(emptyList())
}
/**
* Remove some layers.
*/
fun MapState.removeLayers(layerIds: List<String>) {
val remainingLayers = tileCanvasState.layerFlow.value.filterNot {
it.id in layerIds
}
setLayers(remainingLayers)
}
/**
* Remove a layer.
*/
fun MapState.removeLayer(layerId: String) {
val remainingLayers = tileCanvasState.layerFlow.value.filterNot {
it.id == layerId
}
setLayers(remainingLayers)
}
/**
* Dynamically update the opacity of a layer. If the layer is the lowest one or the only one, the
* new opacity won't have effect until a layer is added below it.
*/
fun MapState.setLayerOpacity(layerId: String, opacity: Float) {
val newLayers = tileCanvasState.layerFlow.value.map {
if (it.id == layerId) {
it.copy(alpha = opacity.coerceIn(0f..1f))
} else it
}
setLayers(newLayers)
}
/**
* Utility function to automatically refresh tiles after a change of layers.
*/
private fun MapState.setLayers(layers: List<Layer>) {
tileCanvasState.setLayers(layers)
renderVisibleTilesThrottled()
}