|
| 1 | +/** |
| 2 | + * DepNav -- department navigator. |
| 3 | + * Copyright (C) 2024 Timofei Pushkin |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +package ru.spbu.depnav.ui.component |
| 20 | + |
| 21 | +import androidx.compose.animation.AnimatedVisibility |
| 22 | +import androidx.compose.animation.fadeIn |
| 23 | +import androidx.compose.animation.fadeOut |
| 24 | +import androidx.compose.animation.scaleIn |
| 25 | +import androidx.compose.animation.scaleOut |
| 26 | +import androidx.compose.animation.slideIn |
| 27 | +import androidx.compose.animation.slideOut |
| 28 | +import androidx.compose.foundation.layout.absoluteOffset |
| 29 | +import androidx.compose.runtime.Composable |
| 30 | +import androidx.compose.runtime.LaunchedEffect |
| 31 | +import androidx.compose.runtime.getValue |
| 32 | +import androidx.compose.runtime.mutableStateOf |
| 33 | +import androidx.compose.runtime.remember |
| 34 | +import androidx.compose.runtime.setValue |
| 35 | +import androidx.compose.ui.Modifier |
| 36 | +import androidx.compose.ui.draw.rotate |
| 37 | +import androidx.compose.ui.platform.LocalDensity |
| 38 | +import androidx.compose.ui.unit.IntOffset |
| 39 | +import androidx.compose.ui.unit.IntSize |
| 40 | +import androidx.lifecycle.compose.collectAsStateWithLifecycle |
| 41 | +import kotlinx.coroutines.flow.Flow |
| 42 | +import ovh.plrapps.mapcompose.api.VisibleArea |
| 43 | +import ovh.plrapps.mapcompose.api.fullSize |
| 44 | +import ovh.plrapps.mapcompose.api.getLayoutSizeFlow |
| 45 | +import ovh.plrapps.mapcompose.ui.state.MapState |
| 46 | +import ovh.plrapps.mapcompose.utils.AngleDegree |
| 47 | +import ovh.plrapps.mapcompose.utils.Point |
| 48 | +import ru.spbu.depnav.data.model.Marker |
| 49 | +import ru.spbu.depnav.utils.map.LineSegment |
| 50 | +import ru.spbu.depnav.utils.map.bottom |
| 51 | +import ru.spbu.depnav.utils.map.centroid |
| 52 | +import ru.spbu.depnav.utils.map.contains |
| 53 | +import ru.spbu.depnav.utils.map.left |
| 54 | +import ru.spbu.depnav.utils.map.rectangularVisibleArea |
| 55 | +import ru.spbu.depnav.utils.map.right |
| 56 | +import ru.spbu.depnav.utils.map.rotation |
| 57 | +import ru.spbu.depnav.utils.map.top |
| 58 | + |
| 59 | +/** |
| 60 | + * When the pin is outside of the map area visible on the screen, shows a pointer towards the pin. |
| 61 | + * |
| 62 | + * It is intended to be placed exactly over the map's composable. |
| 63 | + */ |
| 64 | +@Composable |
| 65 | +fun PinPointer(mapState: MapState, pin: Marker?) { |
| 66 | + var mapLayoutSizeFlow by remember { mutableStateOf<Flow<IntSize>?>(null) } |
| 67 | + LaunchedEffect(mapState) { mapLayoutSizeFlow = mapState.getLayoutSizeFlow() } |
| 68 | + val mapLayoutSize by mapLayoutSizeFlow?.collectAsStateWithLifecycle(IntSize.Zero) ?: return |
| 69 | + if (mapLayoutSize == IntSize.Zero) { |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + val pinSize = with(LocalDensity.current) { PIN_SIZE.roundToPx() } |
| 74 | + |
| 75 | + val pinPoint = pin?.run { Point(x * mapState.fullSize.width, y * mapState.fullSize.height) } |
| 76 | + val visibleArea = mapState.rectangularVisibleArea(mapLayoutSize) // Area visible on the screen |
| 77 | + |
| 78 | + val currentPointerPose = |
| 79 | + if ( |
| 80 | + pinPoint != null && |
| 81 | + !mapState.rectangularVisibleArea( // Area on which the pin is visible on the screen |
| 82 | + mapLayoutSize, |
| 83 | + leftPadding = pinSize / 2, |
| 84 | + rightPadding = pinSize / 2, |
| 85 | + bottomPadding = pinSize |
| 86 | + ).contains(pinPoint) |
| 87 | + ) { |
| 88 | + calculatePointerPose(visibleArea, pinPoint) |
| 89 | + } else { |
| 90 | + null // There is no pin or it is visible on the screen |
| 91 | + } |
| 92 | + |
| 93 | + // Have to remember the latest non-null pointer pose to continue showing it while the exit |
| 94 | + // animation is still in progress |
| 95 | + var lastPointerPose by remember { mutableStateOf(PinPointerPose.Empty) } |
| 96 | + if (currentPointerPose != null) { |
| 97 | + lastPointerPose = currentPointerPose |
| 98 | + } |
| 99 | + |
| 100 | + AnimatedVisibility( |
| 101 | + visible = currentPointerPose != null, |
| 102 | + modifier = Modifier.absoluteOffset { lastPointerPose.coordinates(mapLayoutSize, pinSize) }, |
| 103 | + enter = fadeIn() + slideIn { lastPointerPose.slideAnimationOffset(it) } + scaleIn(), |
| 104 | + exit = fadeOut() + slideOut { lastPointerPose.slideAnimationOffset(it) } + scaleOut() |
| 105 | + ) { |
| 106 | + // Cannot use mapState.rotation since it has a different pivot |
| 107 | + Pin(modifier = Modifier.rotate(lastPointerPose.direction - visibleArea.rotation())) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +private data class PinPointerPose( |
| 112 | + val side: Side, |
| 113 | + val sideFraction: Float, |
| 114 | + val direction: AngleDegree |
| 115 | +) { |
| 116 | + enum class Side { LEFT, RIGHT, TOP, BOTTOM } |
| 117 | + |
| 118 | + companion object { |
| 119 | + val Empty = PinPointerPose(Side.TOP, 0f, 0f) |
| 120 | + } |
| 121 | + |
| 122 | + fun coordinates(boxSize: IntSize, pinSize: Int): IntOffset { |
| 123 | + return when (side) { |
| 124 | + Side.LEFT -> IntOffset( |
| 125 | + x = 0, |
| 126 | + y = (boxSize.height * sideFraction - pinSize / 2f) |
| 127 | + .toInt() |
| 128 | + .coerceIn(0, boxSize.height - pinSize) |
| 129 | + ) |
| 130 | + Side.RIGHT -> IntOffset( |
| 131 | + x = (boxSize.width - pinSize).coerceAtLeast(0), |
| 132 | + y = (boxSize.height * sideFraction - pinSize / 2f) |
| 133 | + .toInt() |
| 134 | + .coerceIn(0, boxSize.height - pinSize) |
| 135 | + ) |
| 136 | + Side.TOP -> IntOffset( |
| 137 | + x = (boxSize.width * sideFraction - pinSize / 2f) |
| 138 | + .toInt() |
| 139 | + .coerceIn(0, boxSize.width - pinSize), |
| 140 | + y = 0 |
| 141 | + ) |
| 142 | + Side.BOTTOM -> IntOffset( |
| 143 | + x = (boxSize.width * sideFraction - pinSize / 2f) |
| 144 | + .toInt() |
| 145 | + .coerceIn(0, boxSize.width - pinSize), |
| 146 | + y = (boxSize.height - pinSize).coerceAtLeast(0) |
| 147 | + ) |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + fun slideAnimationOffset(pinSize: IntSize) = when (side) { |
| 152 | + Side.LEFT -> IntOffset(x = -pinSize.width, y = 0) |
| 153 | + Side.RIGHT -> IntOffset(x = pinSize.width, y = 0) |
| 154 | + Side.TOP -> IntOffset(x = 0, y = -pinSize.height) |
| 155 | + Side.BOTTOM -> IntOffset(x = 0, y = pinSize.height) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +private fun calculatePointerPose(visibleArea: VisibleArea, pin: Point): PinPointerPose { |
| 160 | + val centroidPinSegment = LineSegment(visibleArea.centroid(), pin) |
| 161 | + val direction = centroidPinSegment.slope() - 90 |
| 162 | + |
| 163 | + visibleArea.top().fractionOfIntersectionWith(centroidPinSegment)?.let { fraction -> |
| 164 | + return PinPointerPose(PinPointerPose.Side.TOP, fraction, direction) |
| 165 | + } |
| 166 | + visibleArea.right().fractionOfIntersectionWith(centroidPinSegment)?.let { fraction -> |
| 167 | + return PinPointerPose(PinPointerPose.Side.RIGHT, fraction, direction) |
| 168 | + } |
| 169 | + visibleArea.bottom().fractionOfIntersectionWith(centroidPinSegment)?.let { fraction -> |
| 170 | + return PinPointerPose(PinPointerPose.Side.BOTTOM, fraction, direction) |
| 171 | + } |
| 172 | + visibleArea.left().fractionOfIntersectionWith(centroidPinSegment)?.let { fraction -> |
| 173 | + return PinPointerPose(PinPointerPose.Side.LEFT, fraction, direction) |
| 174 | + } |
| 175 | + |
| 176 | + throw IllegalArgumentException("Pin lies inside the visible area") |
| 177 | +} |
0 commit comments