Skip to content

Commit 07f5b5b

Browse files
committed
Clean up markers
1 parent 60189c7 commit 07f5b5b

File tree

3 files changed

+76
-43
lines changed

3 files changed

+76
-43
lines changed

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/map/internal/MapGLRenderer.kt

+4-3
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,15 @@ class MapGLRenderer(private val resources: Resources, private val mapConfig: Map
5757
Matrix.rotateM(viewMatrix, 0, viewState.cameraLatLng.longitude.value, 0f, -1f, 0f)
5858

5959

60-
globe.draw(projectionMatrix, viewMatrix, mapConfig.globeColors)
60+
val vp = viewMatrix.copyOf()
61+
globe.draw(projectionMatrix, vp, mapConfig.globeColors)
6162

6263
viewState.locationMarker?.let {
6364
when (it.type) {
6465
MarkerType.SECURE ->
65-
secureLocationMarker.draw(projectionMatrix, viewMatrix, it.latLng, 0.02f)
66+
secureLocationMarker.draw(projectionMatrix, vp, it.latLng, 0.02f)
6667
MarkerType.UNSECURE ->
67-
unsecureLocationMarker.draw(projectionMatrix, viewMatrix, it.latLng, 0.02f)
68+
unsecureLocationMarker.draw(projectionMatrix, vp, it.latLng, 0.02f)
6869
}
6970
}
7071
}

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/map/internal/MapGLSurfaceView.kt

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.opengl.GLSurfaceView
66
import androidx.compose.ui.graphics.Color
77
import net.mullvad.mullvadvpn.compose.map.data.MapViewState
88
import net.mullvad.mullvadvpn.compose.map.internal.shapes.GlobeColors
9+
import net.mullvad.mullvadvpn.compose.map.internal.shapes.LocationMarkerColors
910

1011
@SuppressLint("ViewConstructor")
1112
class MapGLSurfaceView(context: Context, mapConfig: MapConfig) : GLSurfaceView(context) {
@@ -36,6 +37,8 @@ data class MapConfig(
3637
oceanColor = Color(0.098f, 0.18f, 0.271f),
3738
contourColor = Color(0.098f, 0.18f, 0.271f)
3839
),
39-
val secureMarkerColor: Color = Color(0.267f, 0.678f, 0.302f),
40-
val unsecureMarkerColor: Color = Color(0.89f, 0.251f, 0.224f)
40+
val secureMarkerColor: LocationMarkerColors =
41+
LocationMarkerColors(centerColor = Color(0.267f, 0.678f, 0.302f)),
42+
val unsecureMarkerColor: LocationMarkerColors =
43+
LocationMarkerColors(centerColor = Color(0.89f, 0.251f, 0.224f))
4144
)

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/map/internal/shapes/LocationMarker.kt

+67-38
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@ package net.mullvad.mullvadvpn.compose.map.internal.shapes
33
import android.opengl.GLES20
44
import android.opengl.Matrix
55
import androidx.compose.ui.graphics.Color
6-
import net.mullvad.mullvadvpn.compose.map.internal.COLOR_COMPONENT_SIZE
76
import java.nio.FloatBuffer
87
import kotlin.math.cos
98
import kotlin.math.sin
10-
import net.mullvad.mullvadvpn.compose.map.internal.VERTEX_COMPONENT_SIZE
119
import net.mullvad.mullvadvpn.compose.map.data.LatLng
10+
import net.mullvad.mullvadvpn.compose.map.internal.COLOR_COMPONENT_SIZE
11+
import net.mullvad.mullvadvpn.compose.map.internal.VERTEX_COMPONENT_SIZE
1212
import net.mullvad.mullvadvpn.compose.map.internal.initArrayBuffer
1313
import net.mullvad.mullvadvpn.compose.map.internal.initShaderProgram
14-
import net.mullvad.mullvadvpn.compose.map.internal.toFloatArrayWithoutAlpha
14+
import net.mullvad.mullvadvpn.compose.map.internal.toFloatArray
15+
16+
data class LocationMarkerColors(
17+
val centerColor: Color,
18+
val ringBorderColor: Color = Color.White,
19+
val shadowColor: Color = Color.Black.copy(alpha = 0.55f),
20+
val perimeterColors: Color = centerColor.copy(alpha = 0.4f)
21+
)
1522

16-
class LocationMarker(val color: Color) {
23+
class LocationMarker(val colors: LocationMarkerColors) {
1724

1825
private val vertexShaderCode =
1926
"""
@@ -42,37 +49,35 @@ class LocationMarker(val color: Color) {
4249
"""
4350
.trimIndent()
4451

45-
private val white = floatArrayOf(1.0f, 1.0f, 1.0f)
46-
private val black = floatArrayOf(0.0f, 0.0f, 0.0f)
4752
private val rings =
4853
listOf(
4954
circleFanVertices(
5055
32,
5156
0.5f,
5257
floatArrayOf(0.0f, 0.0f, 0.0f),
53-
floatArrayOf(*color.toFloatArrayWithoutAlpha(), 0.4f),
54-
floatArrayOf(*color.toFloatArrayWithoutAlpha(), 0.4f)
58+
colors.perimeterColors,
59+
colors.perimeterColors,
5560
), // Semi-transparent outer
5661
circleFanVertices(
5762
16,
5863
0.28f,
5964
floatArrayOf(0.0f, -0.05f, 0.00001f),
60-
floatArrayOf(*black, 0.55f),
61-
floatArrayOf(*black, 0.0f)
65+
colors.shadowColor,
66+
colors.shadowColor.copy(alpha = 0.0f),
6267
), // shadow
6368
circleFanVertices(
6469
32,
6570
0.185f,
6671
floatArrayOf(0.0f, 0.0f, 0.00002f),
67-
floatArrayOf(*white, 1f),
68-
floatArrayOf(*white, 1f)
72+
colors.ringBorderColor,
73+
colors.ringBorderColor,
6974
), // white ring
7075
circleFanVertices(
7176
32,
7277
0.15f,
7378
floatArrayOf(0.0f, 0.0f, 0.00003f),
74-
floatArrayOf(*color.toFloatArrayWithoutAlpha(), 1f),
75-
floatArrayOf(*color.toFloatArrayWithoutAlpha(), 1f),
79+
colors.centerColor,
80+
colors.centerColor,
7681
) // Center colored circle
7782
)
7883

@@ -89,14 +94,14 @@ class LocationMarker(val color: Color) {
8994
private val ringSizes: List<Int> = rings.map { (positions, _) -> positions.size }
9095

9196
init {
92-
val positionArrayBuffer = rings.flatMap { it.first }
93-
val positionByteBuffer = FloatBuffer.wrap(positionArrayBuffer.toFloatArray())
97+
val positionFloatArray = joinMultipleArrays(rings.map { it.vertices })
98+
val positionFloatBuffer = FloatBuffer.wrap(positionFloatArray)
9499

95-
val colorArrayBuffer = rings.flatMap { it.second }
96-
val colorByteBuffer = FloatBuffer.wrap(colorArrayBuffer.toFloatArray())
100+
val colorFloatArray = joinMultipleArrays(rings.map { it.verticesColor })
101+
val colorFloatBuffer = FloatBuffer.wrap(colorFloatArray)
97102

98-
positionBuffer = initArrayBuffer(positionByteBuffer)
99-
colorBuffer = initArrayBuffer(colorByteBuffer)
103+
positionBuffer = initArrayBuffer(positionFloatBuffer)
104+
colorBuffer = initArrayBuffer(colorFloatBuffer)
100105

101106
shaderProgram = initShaderProgram(vertexShaderCode, fragmentShaderCode)
102107

@@ -164,28 +169,52 @@ class LocationMarker(val color: Color) {
164169
private fun circleFanVertices(
165170
numEdges: Int,
166171
radius: Float,
167-
offset: FloatArray,
168-
centerColor: FloatArray,
169-
ringColor: FloatArray,
170-
): Pair<List<Float>, List<Float>> {
171-
val positions = mutableListOf(*offset.toTypedArray())
172-
val colors = mutableListOf(*centerColor.toTypedArray())
173-
174-
for (i in 0..numEdges) {
175-
176-
val angle = (i.toFloat() / numEdges.toFloat()) * 2f * Math.PI
177-
val x = offset[0] + radius * cos(angle).toFloat()
178-
val y = offset[1] + radius * sin(angle).toFloat()
179-
val z = offset[2]
180-
positions.add(x)
181-
positions.add(y)
182-
positions.add(z)
183-
colors.addAll(ringColor.toTypedArray())
172+
offset: FloatArray = floatArrayOf(0.0f, 0.0f, 0.0f),
173+
centerColor: Color,
174+
ringColor: Color,
175+
): Ring {
176+
// Edges + center + first point
177+
val points = numEdges + 2
178+
179+
val positions = FloatArray(points * VERTEX_COMPONENT_SIZE)
180+
val positionsColor = FloatArray(points * COLOR_COMPONENT_SIZE)
181+
182+
// Start adding the center the center point
183+
offset.forEachIndexed { index, value -> positions[index] = value }
184+
centerColor.toFloatArray().forEachIndexed { index, value -> positionsColor[index] = value }
185+
186+
val ringColorArray = ringColor.toFloatArray()
187+
188+
for (i in 1 until points) {
189+
190+
val angle = (i.toFloat() / numEdges) * 2f * Math.PI
191+
val posIndex = i * VERTEX_COMPONENT_SIZE
192+
positions[posIndex] = offset[0] + radius * cos(angle).toFloat()
193+
positions[posIndex + 1] = offset[1] + radius * sin(angle).toFloat()
194+
positions[posIndex + 2] = offset[2]
195+
196+
val colorIndex = i * COLOR_COMPONENT_SIZE
197+
ringColorArray.forEachIndexed { index, value ->
198+
positionsColor[colorIndex + index] = value
199+
}
184200
}
185-
return positions.toList() to colors.toList()
201+
202+
return Ring(positions, positionsColor)
203+
}
204+
205+
private fun joinMultipleArrays(arrays: List<FloatArray>): FloatArray {
206+
val result = FloatArray(arrays.sumOf { it.size })
207+
var offset = 0
208+
for (array in arrays) {
209+
array.copyInto(result, offset)
210+
offset += array.size
211+
}
212+
return result
186213
}
187214

188215
companion object {
189216
private const val MARKER_TRANSLATE_Z_FACTOR = 1.0001f
190217
}
191218
}
219+
220+
data class Ring(val vertices: FloatArray, val verticesColor: FloatArray)

0 commit comments

Comments
 (0)