| | 1 | | using DCL; |
| | 2 | | using DCLServices.MapRendererV2.CommonBehavior; |
| | 3 | | using DCLServices.MapRendererV2.CoordsUtils; |
| | 4 | | using DCLServices.MapRendererV2.Culling; |
| | 5 | | using DCLServices.MapRendererV2.MapLayers; |
| | 6 | | using DG.Tweening; |
| | 7 | | using System; |
| | 8 | | using UnityEngine; |
| | 9 | | using Utils = DCL.Helpers.Utils; |
| | 10 | |
|
| | 11 | | namespace DCLServices.MapRendererV2.MapCameraController |
| | 12 | | { |
| | 13 | | internal partial class MapCameraController : IMapCameraControllerInternal |
| | 14 | | { |
| | 15 | | private const float CAMERA_HEIGHT = 0; |
| | 16 | |
|
| | 17 | | private const int MAX_TEXTURE_SIZE = 4096; |
| | 18 | |
|
| | 19 | | public event Action<IMapActivityOwner, IMapCameraControllerInternal> OnReleasing; |
| | 20 | | public event Action<float, float> ZoomChanged; |
| | 21 | |
|
| 0 | 22 | | public MapLayer EnabledLayers { get; private set; } |
| | 23 | |
|
| 0 | 24 | | public Camera Camera => mapCameraObject.mapCamera; |
| | 25 | |
|
| 0 | 26 | | public float Zoom => Mathf.InverseLerp(zoomValues.y, zoomValues.x, mapCameraObject.mapCamera.orthographicSize); |
| | 27 | |
|
| 0 | 28 | | public Vector2 LocalPosition => mapCameraObject.mapCamera.transform.localPosition; |
| | 29 | |
|
| 0 | 30 | | public Vector2 CoordsPosition => coordsUtils.PositionToCoordsUnclamped(LocalPosition); |
| | 31 | |
|
| | 32 | | private readonly IMapInteractivityControllerInternal interactivityBehavior; |
| | 33 | | private readonly ICoordsUtils coordsUtils; |
| | 34 | | private readonly IMapCullingController cullingController; |
| | 35 | | private readonly MapCameraObject mapCameraObject; |
| | 36 | |
|
| | 37 | | private RenderTexture renderTexture; |
| | 38 | |
|
| | 39 | | // Zoom Thresholds in Parcels |
| | 40 | | private Vector2Int zoomValues; |
| | 41 | |
|
| | 42 | | private Rect cameraPositionBounds; |
| | 43 | | private Sequence translationSequence; |
| | 44 | |
|
| 0 | 45 | | public MapCameraController( |
| | 46 | | IMapInteractivityControllerInternal interactivityBehavior, |
| | 47 | | MapCameraObject mapCameraObject, |
| | 48 | | ICoordsUtils coordsUtils, |
| | 49 | | IMapCullingController cullingController |
| | 50 | | ) |
| | 51 | | { |
| 0 | 52 | | this.interactivityBehavior = interactivityBehavior; |
| 0 | 53 | | this.coordsUtils = coordsUtils; |
| 0 | 54 | | this.mapCameraObject = mapCameraObject; |
| 0 | 55 | | this.cullingController = cullingController; |
| | 56 | |
|
| 0 | 57 | | mapCameraObject.transform.localPosition = Vector3.up * CAMERA_HEIGHT; |
| 0 | 58 | | mapCameraObject.mapCamera.orthographic = true; |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | void IMapCameraControllerInternal.Initialize(Vector2Int textureResolution, Vector2Int zoomValues, MapLayer layer |
| | 62 | | { |
| 0 | 63 | | textureResolution = ClampTextureResolution(textureResolution); |
| 0 | 64 | | renderTexture = new RenderTexture(textureResolution.x, textureResolution.y, 0, RenderTextureFormat.Default, |
| | 65 | | // Bilinear and Trilinear make texture blurry |
| 0 | 66 | | renderTexture.filterMode = FilterMode.Point; |
| 0 | 67 | | renderTexture.autoGenerateMips = false; |
| 0 | 68 | | renderTexture.useMipMap = false; |
| | 69 | |
|
| 0 | 70 | | this.zoomValues = zoomValues * coordsUtils.ParcelSize; |
| | 71 | |
|
| 0 | 72 | | EnabledLayers = layers; |
| | 73 | |
|
| 0 | 74 | | mapCameraObject.mapCamera.targetTexture = renderTexture; |
| | 75 | |
|
| 0 | 76 | | cullingController.OnCameraAdded(this); |
| | 77 | |
|
| 0 | 78 | | interactivityBehavior.Initialize(layers); |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | public void ResizeTexture(Vector2Int textureResolution) |
| | 82 | | { |
| 0 | 83 | | if (!Camera) return; |
| | 84 | |
|
| 0 | 85 | | if (renderTexture.IsCreated()) |
| 0 | 86 | | renderTexture.Release(); |
| | 87 | |
|
| 0 | 88 | | textureResolution = ClampTextureResolution(textureResolution); |
| 0 | 89 | | renderTexture.width = textureResolution.x; |
| 0 | 90 | | renderTexture.height = textureResolution.y; |
| 0 | 91 | | renderTexture.Create(); |
| | 92 | |
|
| 0 | 93 | | Camera.ResetAspect(); |
| | 94 | |
|
| 0 | 95 | | SetLocalPosition(mapCameraObject.transform.localPosition); |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | private Vector2Int ClampTextureResolution(Vector2Int desiredRes) |
| | 99 | | { |
| 0 | 100 | | float factor = Mathf.Min(1, MAX_TEXTURE_SIZE / (float) Mathf.Max(desiredRes.x, desiredRes.y)); |
| 0 | 101 | | return Vector2Int.FloorToInt((Vector2) desiredRes * factor); |
| | 102 | | } |
| | 103 | |
|
| | 104 | | public float GetVerticalSizeInLocalUnits() => |
| 0 | 105 | | mapCameraObject.mapCamera.orthographicSize * 2; |
| | 106 | |
|
| | 107 | | public RenderTexture GetRenderTexture() |
| | 108 | | { |
| 0 | 109 | | if (renderTexture == null) |
| 0 | 110 | | throw new Exception("Trying to get RenderTexture from a not initialized MapCameraController"); |
| | 111 | |
|
| 0 | 112 | | return renderTexture; |
| | 113 | | } |
| | 114 | |
|
| | 115 | | public IMapInteractivityController GetInteractivityController() => |
| 0 | 116 | | interactivityBehavior; |
| | 117 | |
|
| | 118 | | public void SetZoom(float value) |
| | 119 | | { |
| 0 | 120 | | SetCameraSize(value); |
| | 121 | | // Clamp local position as boundaries are dependent on zoom |
| 0 | 122 | | SetLocalPositionClamped(mapCameraObject.transform.localPosition); |
| 0 | 123 | | cullingController.SetCameraDirty(this); |
| 0 | 124 | | } |
| | 125 | |
|
| | 126 | | public void SetPosition(Vector2 coordinates) |
| | 127 | | { |
| 0 | 128 | | translationSequence.Kill(); |
| 0 | 129 | | translationSequence = null; |
| | 130 | |
|
| 0 | 131 | | Vector3 position = coordsUtils.CoordsToPositionUnclamped(coordinates); |
| 0 | 132 | | mapCameraObject.transform.localPosition = ClampLocalPosition(new Vector3(position.x, position.y, CAMERA_HEIG |
| 0 | 133 | | cullingController.SetCameraDirty(this); |
| 0 | 134 | | } |
| | 135 | |
|
| | 136 | | public void SetLocalPosition(Vector2 localCameraPosition) |
| | 137 | | { |
| 0 | 138 | | SetLocalPositionClamped(localCameraPosition); |
| 0 | 139 | | cullingController.SetCameraDirty(this); |
| 0 | 140 | | } |
| | 141 | |
|
| | 142 | | private void SetLocalPositionClamped(Vector2 localCameraPosition) |
| | 143 | | { |
| 0 | 144 | | translationSequence.Kill(); |
| 0 | 145 | | translationSequence = null; |
| | 146 | |
|
| 0 | 147 | | mapCameraObject.transform.localPosition = ClampLocalPosition(localCameraPosition); |
| 0 | 148 | | } |
| | 149 | |
|
| | 150 | | public void SetPositionAndZoom(Vector2 coordinates, float zoom) |
| | 151 | | { |
| 0 | 152 | | translationSequence.Kill(); |
| 0 | 153 | | translationSequence = null; |
| | 154 | |
|
| 0 | 155 | | SetCameraSize(zoom); |
| | 156 | |
|
| 0 | 157 | | Vector3 position = coordsUtils.CoordsToPositionUnclamped(coordinates); |
| 0 | 158 | | mapCameraObject.transform.localPosition = ClampLocalPosition(new Vector3(position.x, position.y, CAMERA_HEIG |
| 0 | 159 | | cullingController.SetCameraDirty(this); |
| 0 | 160 | | } |
| | 161 | |
|
| | 162 | | public void TranslateTo(Vector2 coordinates, float duration, Action onComplete = null) |
| | 163 | | { |
| 0 | 164 | | translationSequence = DOTween.Sequence(); |
| | 165 | |
|
| 0 | 166 | | Vector3 position = coordsUtils.CoordsToPositionUnclamped(coordinates); |
| 0 | 167 | | Vector3 targetPosition = ClampLocalPosition(new Vector3(position.x, position.y, CAMERA_HEIGHT)); |
| | 168 | |
|
| 0 | 169 | | translationSequence.Join(mapCameraObject.transform.DOLocalMove(targetPosition, duration).SetEase(Ease.OutQua |
| | 170 | | .OnComplete(() => |
| | 171 | | { |
| 0 | 172 | | CalculateCameraPositionBounds(); |
| 0 | 173 | | cullingController.SetCameraDirty(this); |
| 0 | 174 | | onComplete?.Invoke(); |
| 0 | 175 | | }); |
| 0 | 176 | | } |
| | 177 | |
|
| | 178 | | private void SetCameraSize(float zoom) |
| | 179 | | { |
| 0 | 180 | | zoom = Mathf.Clamp01(zoom); |
| 0 | 181 | | mapCameraObject.mapCamera.orthographicSize = Mathf.Lerp(zoomValues.y, zoomValues.x, zoom); |
| | 182 | |
|
| 0 | 183 | | if (DataStore.i.featureFlags.flags.Get().IsFeatureEnabled("map_focus_home_or_user")) |
| | 184 | | { |
| 0 | 185 | | interactivityBehavior.ApplyCameraZoom(zoomValues.x, mapCameraObject.mapCamera.orthographicSize); |
| 0 | 186 | | ZoomChanged?.Invoke(zoomValues.x, mapCameraObject.mapCamera.orthographicSize); |
| | 187 | | } |
| | 188 | |
|
| 0 | 189 | | CalculateCameraPositionBounds(); |
| 0 | 190 | | } |
| | 191 | |
|
| | 192 | | private Vector3 ClampLocalPosition(Vector3 localPos) |
| | 193 | | { |
| 0 | 194 | | localPos.x = Mathf.Clamp(localPos.x, cameraPositionBounds.xMin, cameraPositionBounds.xMax); |
| 0 | 195 | | localPos.y = Mathf.Clamp(localPos.y, cameraPositionBounds.yMin, cameraPositionBounds.yMax); |
| | 196 | |
|
| 0 | 197 | | return localPos; |
| | 198 | | } |
| | 199 | |
|
| | 200 | | private void CalculateCameraPositionBounds() |
| | 201 | | { |
| 0 | 202 | | var worldBounds = coordsUtils.VisibleWorldBounds; |
| | 203 | |
|
| 0 | 204 | | var cameraYSize = mapCameraObject.mapCamera.orthographicSize; |
| 0 | 205 | | var cameraXSize = cameraYSize * mapCameraObject.mapCamera.aspect; |
| | 206 | |
|
| 0 | 207 | | float xMin = worldBounds.xMin + cameraXSize; |
| 0 | 208 | | float xMax = worldBounds.xMax - cameraXSize; |
| | 209 | |
|
| 0 | 210 | | float yMin = worldBounds.yMin + cameraYSize; |
| 0 | 211 | | float yMax = worldBounds.yMax - cameraYSize; |
| | 212 | |
|
| | 213 | | // If the map's width is smaller than the camera's width, disable X-drag |
| 0 | 214 | | if (worldBounds.xMax - worldBounds.xMin < 2 * cameraXSize) |
| 0 | 215 | | xMin = xMax = 0; |
| | 216 | |
|
| | 217 | | // If the map's height is smaller than the camera's height, disable Y-drag |
| 0 | 218 | | if (worldBounds.yMax - worldBounds.yMin < 2 * cameraYSize) |
| 0 | 219 | | yMin = yMax = 0; |
| | 220 | |
|
| 0 | 221 | | cameraPositionBounds = Rect.MinMaxRect(xMin, yMin, xMax, yMax); |
| 0 | 222 | | } |
| | 223 | |
|
| | 224 | | public void SuspendRendering() |
| | 225 | | { |
| 0 | 226 | | mapCameraObject.mapCamera.enabled = false; |
| 0 | 227 | | } |
| | 228 | |
|
| | 229 | | public void ResumeRendering() |
| | 230 | | { |
| 0 | 231 | | mapCameraObject.mapCamera.enabled = true; |
| 0 | 232 | | } |
| | 233 | |
|
| | 234 | | public void SetActive(bool active) |
| | 235 | | { |
| 0 | 236 | | mapCameraObject.gameObject.SetActive(active); |
| 0 | 237 | | } |
| | 238 | |
|
| | 239 | | public Rect GetCameraRect() |
| | 240 | | { |
| 0 | 241 | | var cameraYSize = mapCameraObject.mapCamera.orthographicSize; |
| 0 | 242 | | var cameraXSize = cameraYSize * mapCameraObject.mapCamera.aspect; |
| | 243 | |
|
| 0 | 244 | | var size = new Vector2(cameraXSize * 2f, cameraYSize * 2f); |
| | 245 | |
|
| 0 | 246 | | return new Rect((Vector2) mapCameraObject.transform.localPosition - new Vector2(cameraXSize, cameraYSize), s |
| | 247 | | } |
| | 248 | |
|
| | 249 | | public void Release(IMapActivityOwner owner) |
| | 250 | | { |
| 0 | 251 | | cullingController.OnCameraRemoved(this); |
| 0 | 252 | | renderTexture?.Release(); |
| 0 | 253 | | interactivityBehavior.Release(); |
| 0 | 254 | | OnReleasing?.Invoke(owner, this); |
| 0 | 255 | | } |
| | 256 | |
|
| | 257 | | public void Dispose() |
| | 258 | | { |
| 0 | 259 | | translationSequence.Kill(); |
| 0 | 260 | | translationSequence = null; |
| | 261 | |
|
| 0 | 262 | | if (mapCameraObject != null) |
| 0 | 263 | | Utils.SafeDestroy(mapCameraObject.gameObject); |
| | 264 | |
|
| 0 | 265 | | interactivityBehavior.Dispose(); |
| | 266 | |
|
| 0 | 267 | | renderTexture?.Release(); |
| 0 | 268 | | renderTexture = null; |
| 0 | 269 | | } |
| | 270 | | } |
| | 271 | | } |