| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Tasks; |
| | 3 | | using DCLServices.MapRendererV2.MapCameraController; |
| | 4 | | using System; |
| | 5 | | using System.Threading; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.EventSystems; |
| | 8 | |
|
| | 9 | | namespace DCL |
| | 10 | | { |
| | 11 | | public class NavmapZoomViewController : IDisposable, INavmapZoomViewController |
| | 12 | | { |
| | 13 | | private const float MOUSE_WHEEL_THRESHOLD = 0.04f; |
| | 14 | |
|
| | 15 | | private readonly NavmapZoomView view; |
| | 16 | |
|
| | 17 | | private AnimationCurve normalizedCurve; |
| | 18 | | private int zoomSteps; |
| | 19 | |
|
| | 20 | | private bool active; |
| | 21 | |
|
| | 22 | | private CancellationTokenSource cts; |
| | 23 | | private bool isScaling; |
| | 24 | |
|
| | 25 | | private float targetNormalizedZoom; |
| | 26 | | private int currentZoomLevel; |
| | 27 | |
|
| | 28 | | private IMapCameraController cameraController; |
| | 29 | | private readonly BaseVariable<FeatureFlag> featureFlagsFlags; |
| | 30 | |
|
| 1 | 31 | | public NavmapZoomViewController(NavmapZoomView view, BaseVariable<FeatureFlag> featureFlagsFlags) |
| | 32 | | { |
| 1 | 33 | | this.view = view; |
| 1 | 34 | | this.featureFlagsFlags = featureFlagsFlags; |
| | 35 | |
|
| 1 | 36 | | if (featureFlagsFlags.Get().IsInitialized) |
| 0 | 37 | | HandleFeatureFlag(); |
| | 38 | | else |
| 1 | 39 | | featureFlagsFlags.OnChange += OnFeatureFlagsChanged; |
| | 40 | |
|
| 1 | 41 | | normalizedCurve = view.normalizedZoomCurve; |
| 1 | 42 | | zoomSteps = normalizedCurve.length; |
| | 43 | |
|
| 1 | 44 | | CurveClamp01(); |
| 1 | 45 | | } |
| | 46 | |
|
| | 47 | | private void OnFeatureFlagsChanged(FeatureFlag current, FeatureFlag previous) |
| | 48 | | { |
| 0 | 49 | | featureFlagsFlags.OnChange -= OnFeatureFlagsChanged; |
| 0 | 50 | | HandleFeatureFlag(); |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | private void HandleFeatureFlag() |
| | 54 | | { |
| 0 | 55 | | if (featureFlagsFlags.Get().IsFeatureEnabled("map_focus_home_or_user")) return; |
| | 56 | |
|
| 0 | 57 | | view.zoomVerticalRange = new Vector2Int(view.zoomVerticalRange.x, 40); |
| | 58 | |
|
| 0 | 59 | | normalizedCurve = new AnimationCurve(); |
| 0 | 60 | | normalizedCurve.AddKey(0, 0); |
| 0 | 61 | | normalizedCurve.AddKey(1, 0.25f); |
| 0 | 62 | | normalizedCurve.AddKey(2, 0.5f); |
| 0 | 63 | | normalizedCurve.AddKey(3, 0.75f); |
| 0 | 64 | | normalizedCurve.AddKey(4, 1); |
| 0 | 65 | | zoomSteps = normalizedCurve.length; |
| 0 | 66 | | } |
| | 67 | |
|
| | 68 | | public void Dispose() |
| | 69 | | { |
| 1 | 70 | | cts.SafeCancelAndDispose(); |
| 1 | 71 | | } |
| | 72 | |
|
| | 73 | | private void CurveClamp01() |
| | 74 | | { |
| | 75 | | // Keys should be int for zoomSteps to work properly |
| 16 | 76 | | for (var i = 0; i < normalizedCurve.keys.Length; i++) |
| | 77 | | { |
| 7 | 78 | | Keyframe keyFrame = normalizedCurve.keys[i]; |
| | 79 | |
|
| 7 | 80 | | if (i == 0) |
| | 81 | | { |
| 1 | 82 | | keyFrame.time = 0; |
| 1 | 83 | | keyFrame.value = 0; |
| | 84 | | } |
| 6 | 85 | | else if (i == normalizedCurve.length - 1) |
| | 86 | | { |
| 1 | 87 | | keyFrame.time = Mathf.RoundToInt(keyFrame.time); |
| 1 | 88 | | keyFrame.value = 1; |
| | 89 | | } |
| | 90 | | else |
| | 91 | | { |
| 5 | 92 | | keyFrame.time = Mathf.RoundToInt(keyFrame.time); |
| 5 | 93 | | keyFrame.value = keyFrame.value; |
| | 94 | | } |
| | 95 | |
|
| 7 | 96 | | normalizedCurve.MoveKey(i, keyFrame); |
| | 97 | | } |
| 1 | 98 | | } |
| | 99 | |
|
| | 100 | | public float ResetZoomToMidValue() |
| | 101 | | { |
| 0 | 102 | | SetZoomLevel(Mathf.FloorToInt((zoomSteps - 1) / 2f)); |
| 0 | 103 | | return targetNormalizedZoom; |
| | 104 | | } |
| | 105 | |
|
| | 106 | | public void Activate(IMapCameraController mapCameraController) |
| | 107 | | { |
| 0 | 108 | | if (active) |
| | 109 | | { |
| 0 | 110 | | if (cameraController == mapCameraController) |
| 0 | 111 | | return; |
| | 112 | |
|
| 0 | 113 | | Deactivate(); |
| | 114 | | } |
| | 115 | |
|
| 0 | 116 | | cts = new CancellationTokenSource(); |
| | 117 | |
|
| 0 | 118 | | cameraController = mapCameraController; |
| | 119 | |
|
| 0 | 120 | | view.MouseWheelAction.OnValueChanged += OnMouseWheelValueChanged; |
| 0 | 121 | | view.ZoomIn.InputAction.OnStarted += Zoom; |
| 0 | 122 | | view.ZoomOut.InputAction.OnStarted += Zoom; |
| 0 | 123 | | view.ZoomIn.Button.onClick.AddListener(() => Zoom(DCLAction_Hold.ZoomIn)); |
| 0 | 124 | | view.ZoomOut.Button.onClick.AddListener(() => Zoom(DCLAction_Hold.ZoomOut)); |
| | 125 | |
|
| 0 | 126 | | active = true; |
| 0 | 127 | | } |
| | 128 | |
|
| | 129 | | public void Deactivate() |
| | 130 | | { |
| 0 | 131 | | if (!active) return; |
| | 132 | |
|
| 0 | 133 | | cts.Cancel(); |
| 0 | 134 | | cts.Dispose(); |
| 0 | 135 | | cts = null; |
| | 136 | |
|
| 0 | 137 | | view.MouseWheelAction.OnValueChanged -= OnMouseWheelValueChanged; |
| 0 | 138 | | view.ZoomIn.InputAction.OnStarted -= Zoom; |
| 0 | 139 | | view.ZoomOut.InputAction.OnStarted -= Zoom; |
| 0 | 140 | | view.ZoomIn.Button.onClick.RemoveAllListeners(); |
| 0 | 141 | | view.ZoomOut.Button.onClick.RemoveAllListeners(); |
| | 142 | |
|
| 0 | 143 | | active = false; |
| 0 | 144 | | } |
| | 145 | |
|
| | 146 | | private void OnMouseWheelValueChanged(DCLAction_Measurable action, float value) |
| | 147 | | { |
| 0 | 148 | | if (value == 0 || Mathf.Abs(value) < MOUSE_WHEEL_THRESHOLD) |
| 0 | 149 | | return; |
| | 150 | |
|
| 0 | 151 | | DCLAction_Hold zoomAction = value > 0 ? DCLAction_Hold.ZoomIn : DCLAction_Hold.ZoomOut; |
| 0 | 152 | | Zoom(zoomAction); |
| 0 | 153 | | } |
| | 154 | |
|
| | 155 | | private void SetZoomLevel(int zoomLevel) |
| | 156 | | { |
| 0 | 157 | | currentZoomLevel = Mathf.Clamp(zoomLevel, 0, zoomSteps - 1); |
| 0 | 158 | | targetNormalizedZoom = normalizedCurve.Evaluate(currentZoomLevel); |
| | 159 | |
|
| 0 | 160 | | SetUiButtonsInteractivity(); |
| 0 | 161 | | } |
| | 162 | |
|
| | 163 | | private void Zoom(DCLAction_Hold action) |
| | 164 | | { |
| 0 | 165 | | if (!active || isScaling) |
| 0 | 166 | | return; |
| | 167 | |
|
| 0 | 168 | | EventSystem.current.SetSelectedGameObject(null); |
| | 169 | |
|
| 0 | 170 | | switch (action) |
| | 171 | | { |
| 0 | 172 | | case DCLAction_Hold.ZoomIn when Mathf.Approximately(targetNormalizedZoom, 1f): |
| 0 | 173 | | case DCLAction_Hold.ZoomOut when Mathf.Approximately(targetNormalizedZoom, 0f): |
| 0 | 174 | | return; |
| | 175 | | } |
| | 176 | |
|
| 0 | 177 | | SetZoomLevel(currentZoomLevel + (action == DCLAction_Hold.ZoomIn ? 1 : -1)); |
| 0 | 178 | | ScaleOverTime(cameraController.Zoom, targetNormalizedZoom, cts.Token).Forget(); |
| | 179 | |
|
| 0 | 180 | | SetUiButtonsInteractivity(); |
| 0 | 181 | | } |
| | 182 | |
|
| | 183 | | private void SetUiButtonsInteractivity() |
| | 184 | | { |
| 0 | 185 | | view.ZoomIn.SetUiInteractable(isInteractable: currentZoomLevel < zoomSteps - 1); |
| 0 | 186 | | view.ZoomOut.SetUiInteractable(isInteractable: currentZoomLevel > 0); |
| 0 | 187 | | } |
| | 188 | |
|
| | 189 | | private async UniTaskVoid ScaleOverTime(float from, float to, CancellationToken ct) |
| | 190 | | { |
| 0 | 191 | | isScaling = true; |
| 0 | 192 | | float scaleDuration = view.scaleDuration; |
| | 193 | |
|
| 0 | 194 | | for (float timer = 0; timer < scaleDuration; timer += Time.deltaTime) |
| | 195 | | { |
| 0 | 196 | | if (ct.IsCancellationRequested) |
| | 197 | | break; |
| | 198 | |
|
| 0 | 199 | | cameraController.SetZoom(Mathf.Lerp(from, to, timer / scaleDuration)); |
| | 200 | |
|
| | 201 | | // omit CT, handle cancellation gracefully |
| 0 | 202 | | await UniTask.NextFrame(); |
| | 203 | | } |
| | 204 | |
|
| 0 | 205 | | isScaling = false; |
| 0 | 206 | | } |
| | 207 | | } |
| | 208 | | } |