| | 1 | | using DCLServices.MapRendererV2.CoordsUtils; |
| | 2 | | using DCLServices.MapRendererV2.MapLayers; |
| | 3 | | using DCLServices.MapRendererV2.MapLayers.ParcelHighlight; |
| | 4 | | using MainScripts.DCL.Helpers.Utils; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.Pool; |
| | 7 | |
|
| | 8 | | namespace DCLServices.MapRendererV2.MapCameraController |
| | 9 | | { |
| | 10 | | internal class MapCameraInteractivityController : IMapInteractivityControllerInternal |
| | 11 | | { |
| | 12 | | private readonly Transform cameraParent; |
| | 13 | | private readonly IObjectPool<IParcelHighlightMarker> markersPool; |
| | 14 | | private readonly ICoordsUtils coordsUtils; |
| | 15 | | private readonly Camera camera; |
| | 16 | |
|
| | 17 | | private IParcelHighlightMarker marker; |
| | 18 | |
|
| 0 | 19 | | public bool HighlightEnabled { get; private set; } |
| | 20 | |
|
| 0 | 21 | | public MapCameraInteractivityController( |
| | 22 | | Transform cameraParent, |
| | 23 | | Camera camera, |
| | 24 | | IObjectPool<IParcelHighlightMarker> markersPool, |
| | 25 | | ICoordsUtils coordsUtils) |
| | 26 | | { |
| 0 | 27 | | this.cameraParent = cameraParent; |
| 0 | 28 | | this.markersPool = markersPool; |
| 0 | 29 | | this.coordsUtils = coordsUtils; |
| 0 | 30 | | this.camera = camera; |
| 0 | 31 | | } |
| | 32 | |
|
| | 33 | | public void HighlightParcel(Vector2Int parcel) |
| | 34 | | { |
| 0 | 35 | | if (marker == null) |
| 0 | 36 | | return; |
| | 37 | |
|
| | 38 | | // make position discrete |
| 0 | 39 | | var localPosition = coordsUtils.CoordsToPosition(parcel, marker); |
| | 40 | |
|
| 0 | 41 | | marker.Activate(); |
| 0 | 42 | | marker.SetCoordinates(parcel, localPosition); |
| 0 | 43 | | } |
| | 44 | |
|
| | 45 | | public void Initialize(MapLayer layers) |
| | 46 | | { |
| 0 | 47 | | HighlightEnabled = EnumUtils.HasFlag(layers, MapLayer.ParcelHoverHighlight); |
| | 48 | |
|
| 0 | 49 | | if (HighlightEnabled) |
| 0 | 50 | | marker = markersPool.Get(); |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | public void RemoveHighlight() |
| | 54 | | { |
| 0 | 55 | | marker?.Deactivate(); |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | public bool TryGetParcel(Vector2 normalizedCoordinates, out Vector2Int parcel) => |
| 0 | 59 | | coordsUtils.TryGetCoordsWithinInteractableBounds(GetLocalPosition(normalizedCoordinates), out parcel); |
| | 60 | |
|
| | 61 | | public Vector2 GetNormalizedPosition(Vector2Int parcel) |
| | 62 | | { |
| 0 | 63 | | var discreteLocalPosition = coordsUtils.CoordsToPosition(parcel); |
| | 64 | |
|
| | 65 | | // Convert local Position to viewPort |
| 0 | 66 | | var worldPosition = cameraParent ? cameraParent.TransformPoint(discreteLocalPosition) : discreteLocalPositio |
| 0 | 67 | | var viewPortPosition = camera.WorldToViewportPoint(worldPosition); |
| | 68 | |
|
| 0 | 69 | | return viewPortPosition; |
| | 70 | | } |
| | 71 | |
|
| | 72 | | private Vector3 GetLocalPosition(Vector2 normalizedCoordinates) |
| | 73 | | { |
| | 74 | | // normalized position is equal to viewport position |
| 0 | 75 | | var worldPoint = camera.ViewportToWorldPoint(normalizedCoordinates); |
| | 76 | |
|
| 0 | 77 | | var localPosition = cameraParent ? cameraParent.InverseTransformPoint(worldPoint) : worldPoint; |
| 0 | 78 | | localPosition.z = 0; |
| 0 | 79 | | return localPosition; |
| | 80 | | } |
| | 81 | |
|
| | 82 | | public void Dispose() |
| | 83 | | { |
| | 84 | |
|
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | public void Release() |
| | 88 | | { |
| 0 | 89 | | if (marker != null) |
| | 90 | | { |
| 0 | 91 | | markersPool.Release(marker); |
| 0 | 92 | | marker = null; |
| | 93 | | } |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | public void ApplyCameraZoom(float baseZoom, float newZoom) |
| | 97 | | { |
| 0 | 98 | | marker?.SetZoom(baseZoom, newZoom); |
| 0 | 99 | | } |
| | 100 | | } |
| | 101 | | } |