| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | namespace DCL.Builder |
| | 9 | | { |
| | 10 | | public class PublishMapView : MonoBehaviour |
| | 11 | | { |
| | 12 | | private const float MAX_ZOOM = 4f; |
| | 13 | | private const float MIN_ZOOM = 1.0f; |
| | 14 | | private const float SIZE_PER_ZOOM = 0.5f; |
| | 15 | |
|
| | 16 | | public event Action<Vector2Int> OnParcelClicked; |
| | 17 | | public event Action<Vector2Int> OnParcelHover; |
| | 18 | |
|
| | 19 | | [Header("References")] |
| | 20 | | [SerializeField] internal ScrollRect scrollRect; |
| | 21 | | [SerializeField] RectTransform scrollRectContentTransform; |
| | 22 | | [SerializeField] internal Button lessZoomBtn; |
| | 23 | | [SerializeField] internal Button moreZoomBtn; |
| | 24 | |
|
| | 25 | | private RectTransform minimapViewport; |
| | 26 | | private RectTransform viewRectTransform; |
| | 27 | | private Transform mapRendererMinimapParent; |
| | 28 | | private Vector3 atlasOriginalPosition; |
| | 29 | | private Vector2 initialContentPosition; |
| | 30 | | private Vector2Int sceneSize; |
| | 31 | |
|
| | 32 | | private bool isVisible = false; |
| | 33 | | private bool isDragging = false; |
| | 34 | | private float lastScale = 0; |
| 0 | 35 | | private float currentZoomScale = 2f; |
| | 36 | |
|
| | 37 | | private void Start() |
| | 38 | | { |
| 0 | 39 | | lessZoomBtn.onClick.AddListener(LessZoom); |
| 0 | 40 | | moreZoomBtn.onClick.AddListener(MoreZoom); |
| | 41 | |
|
| 0 | 42 | | viewRectTransform = GetComponent<RectTransform>(); |
| 0 | 43 | | scrollRect.onValueChanged.AddListener((x) => |
| | 44 | | { |
| 0 | 45 | | if (isVisible) |
| 0 | 46 | | MapRenderer.i.atlas.UpdateCulling(); |
| 0 | 47 | | }); |
| | 48 | |
|
| 0 | 49 | | MapRenderer.OnParcelClicked += ParcelSelect; |
| 0 | 50 | | } |
| | 51 | |
|
| | 52 | | private void OnDestroy() |
| | 53 | | { |
| 0 | 54 | | moreZoomBtn.onClick.RemoveAllListeners(); |
| 0 | 55 | | lessZoomBtn.onClick.RemoveAllListeners(); |
| 0 | 56 | | MapRenderer.OnParcelClicked -= ParcelSelect; |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | // Note: this event is handled by an event trigger in the same gameobject as the scrollrect |
| | 60 | | public void BeginDrag() |
| | 61 | | { |
| 0 | 62 | | isDragging = true; |
| 0 | 63 | | MapRenderer.i.SetParcelHighlightActive(false); |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | // Note: this event is handled by an event trigger in the same gameobject as the scrollrect |
| | 67 | | public void EndDrag() |
| | 68 | | { |
| 0 | 69 | | isDragging = false; |
| 0 | 70 | | if(RectTransformUtility.RectangleContainsScreenPoint(viewRectTransform, Input.mousePosition)) |
| 0 | 71 | | MapRenderer.i.SetParcelHighlightActive(true); |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | internal void MoreZoom() |
| | 75 | | { |
| 0 | 76 | | currentZoomScale = Mathf.Clamp(currentZoomScale + SIZE_PER_ZOOM, MIN_ZOOM, MAX_ZOOM); |
| 0 | 77 | | ApplyCurrentZoom(); |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | internal void LessZoom() |
| | 81 | | { |
| 0 | 82 | | currentZoomScale = Mathf.Clamp(currentZoomScale - SIZE_PER_ZOOM, MIN_ZOOM, MAX_ZOOM); |
| 0 | 83 | | ApplyCurrentZoom(); |
| 0 | 84 | | } |
| | 85 | |
|
| | 86 | | internal void ApplyCurrentZoom() |
| | 87 | | { |
| 0 | 88 | | MapRenderer.i.transform.localScale = Vector3.one * currentZoomScale; |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | internal void UpdateOwnedLands() |
| | 92 | | { |
| 0 | 93 | | List<Vector2Int> landsToHighlight = new List<Vector2Int>(); |
| 0 | 94 | | List<Vector2Int> landsToHighlightWithContent = new List<Vector2Int>(); |
| 0 | 95 | | foreach (var land in DataStore.i.builderInWorld.landsWithAccess.Get()) |
| | 96 | | { |
| 0 | 97 | | foreach (Vector2Int landParcel in land.parcels) |
| | 98 | | { |
| 0 | 99 | | bool found = false; |
| 0 | 100 | | foreach (Scene scene in land.scenes) |
| | 101 | | { |
| 0 | 102 | | if(scene.isEmpty) |
| | 103 | | continue; |
| | 104 | |
|
| 0 | 105 | | foreach (Vector2Int sceneParcel in scene.parcels) |
| | 106 | | { |
| 0 | 107 | | if (sceneParcel == landParcel) |
| | 108 | | { |
| 0 | 109 | | found = true; |
| 0 | 110 | | break; |
| | 111 | | } |
| | 112 | | } |
| | 113 | | } |
| 0 | 114 | | if(found) |
| 0 | 115 | | landsToHighlightWithContent.Add(landParcel); |
| | 116 | | else |
| 0 | 117 | | landsToHighlight.Add(landParcel); |
| | 118 | | } |
| | 119 | | } |
| 0 | 120 | | MapRenderer.i.HighlightLands(landsToHighlight, landsToHighlightWithContent); |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | public void SetProjectSize(Vector2Int[] parcels) |
| | 124 | | { |
| 0 | 125 | | sceneSize = BIWUtils.GetSceneSize(parcels); |
| 0 | 126 | | MapRenderer.i.SetHighlighSize(sceneSize); |
| 0 | 127 | | } |
| | 128 | |
|
| | 129 | | public void GoToCoords(Vector2Int coords) |
| | 130 | | { |
| | 131 | | //Reset scroll |
| 0 | 132 | | scrollRect.content.anchoredPosition = initialContentPosition; |
| 0 | 133 | | MapRenderer.i.atlas.CenterToTile(coords); |
| 0 | 134 | | } |
| | 135 | |
|
| | 136 | | internal void SetVisible(bool visible) |
| | 137 | | { |
| 0 | 138 | | if (MapRenderer.i == null || isVisible == visible) |
| 0 | 139 | | return; |
| | 140 | |
|
| 0 | 141 | | isVisible = visible; |
| | 142 | |
|
| 0 | 143 | | scrollRect.StopMovement(); |
| 0 | 144 | | scrollRect.gameObject.SetActive(visible); |
| 0 | 145 | | MapRenderer.i.parcelHighlightEnabled = visible; |
| | 146 | |
|
| 0 | 147 | | if (visible) |
| 0 | 148 | | SetMapRendererInContainer(); |
| | 149 | | else |
| 0 | 150 | | RemoveMapRendererFromContainer(); |
| 0 | 151 | | } |
| | 152 | |
|
| | 153 | | public void SetAvailabilityToPublish(bool isAvailable) |
| | 154 | | { |
| 0 | 155 | | var style = isAvailable ? MapParcelHighlight.HighlighStyle.BUILDER_ENABLE : MapParcelHighlight.HighlighStyle |
| 0 | 156 | | MapRenderer.i.SetHighlightStyle(style); |
| 0 | 157 | | } |
| | 158 | |
|
| | 159 | | private void ParcelHovered(float x, float y) |
| | 160 | | { |
| 0 | 161 | | if(!isDragging) |
| 0 | 162 | | MapRenderer.i.SetParcelHighlightActive(true); |
| 0 | 163 | | OnParcelHover?.Invoke( new Vector2Int(Mathf.RoundToInt(x), Mathf.RoundToInt(y))); |
| 0 | 164 | | } |
| | 165 | |
|
| | 166 | | private void SetMapRendererInContainer() |
| | 167 | | { |
| 0 | 168 | | minimapViewport = MapRenderer.i.atlas.viewport; |
| 0 | 169 | | mapRendererMinimapParent = MapRenderer.i.transform.parent; |
| 0 | 170 | | atlasOriginalPosition = MapRenderer.i.atlas.chunksParent.transform.localPosition; |
| | 171 | |
|
| 0 | 172 | | lastScale = MapRenderer.i.transform.localScale.x; |
| | 173 | |
|
| 0 | 174 | | MapRenderer.i.SetHighlightStyle(MapParcelHighlight.HighlighStyle.BUILDER_DISABLE); |
| 0 | 175 | | MapRenderer.i.atlas.viewport = scrollRect.viewport; |
| 0 | 176 | | MapRenderer.i.transform.SetParent(scrollRectContentTransform); |
| 0 | 177 | | MapRenderer.i.atlas.UpdateCulling(); |
| 0 | 178 | | MapRenderer.i.OnMovedParcelCursor += ParcelHovered; |
| 0 | 179 | | MapRenderer.i.SetPointOfInterestActive(false); |
| 0 | 180 | | MapRenderer.i.SetPlayerIconActive(false); |
| 0 | 181 | | MapRenderer.i.SetOtherPlayersIconActive(false); |
| | 182 | |
|
| 0 | 183 | | currentZoomScale = 2f; |
| 0 | 184 | | ApplyCurrentZoom(); |
| | 185 | |
|
| 0 | 186 | | scrollRect.content = MapRenderer.i.atlas.chunksParent.transform as RectTransform; |
| 0 | 187 | | initialContentPosition = scrollRect.content.anchoredPosition; |
| | 188 | |
|
| | 189 | | // Reparent the player icon parent to scroll everything together |
| 0 | 190 | | MapRenderer.i.atlas.overlayLayerGameobject.transform.SetParent(scrollRect.content); |
| | 191 | |
|
| 0 | 192 | | UpdateOwnedLands(); |
| 0 | 193 | | } |
| | 194 | |
|
| | 195 | | private void RemoveMapRendererFromContainer() |
| | 196 | | { |
| 0 | 197 | | MapRenderer.i.CleanLandsHighlights(); |
| 0 | 198 | | MapRenderer.i.ClearLandHighlightsInfo(); |
| 0 | 199 | | MapRenderer.i.SetHighlightStyle(MapParcelHighlight.HighlighStyle.DEFAULT); |
| 0 | 200 | | MapRenderer.i.atlas.viewport = minimapViewport; |
| 0 | 201 | | MapRenderer.i.transform.SetParent(mapRendererMinimapParent); |
| 0 | 202 | | MapRenderer.i.OnMovedParcelCursor -= ParcelHovered; |
| 0 | 203 | | MapRenderer.i.atlas.chunksParent.transform.localPosition = atlasOriginalPosition; |
| 0 | 204 | | MapRenderer.i.atlas.UpdateCulling(); |
| 0 | 205 | | MapRenderer.i.SetPointOfInterestActive(true); |
| 0 | 206 | | MapRenderer.i.SetPlayerIconActive(true); |
| 0 | 207 | | MapRenderer.i.SetOtherPlayersIconActive(true); |
| 0 | 208 | | MapRenderer.i.transform.localScale = Vector3.one * lastScale; |
| | 209 | |
|
| | 210 | | // Restore the player icon to its original parent |
| 0 | 211 | | MapRenderer.i.atlas.overlayLayerGameobject.transform.SetParent(MapRenderer.i.atlas.chunksParent.transform.pa |
| 0 | 212 | | (MapRenderer.i.atlas.overlayLayerGameobject.transform as RectTransform).anchoredPosition = Vector2.zero; |
| | 213 | |
|
| 0 | 214 | | MapRenderer.i.UpdateRendering(Utils.WorldToGridPositionUnclamped(DataStore.i.player.playerWorldPosition.Get( |
| | 215 | |
|
| 0 | 216 | | } |
| | 217 | |
|
| | 218 | | public void SelectLandInMap(Vector2Int coord) |
| | 219 | | { |
| 0 | 220 | | MapRenderer.i.SelectLand(coord, sceneSize); |
| 0 | 221 | | } |
| | 222 | |
|
| | 223 | | private void ParcelSelect(int cursorTileX, int cursorTileY) |
| | 224 | | { |
| 0 | 225 | | OnParcelClicked?.Invoke(new Vector2Int(cursorTileX, cursorTileY)); |
| 0 | 226 | | } |
| | 227 | | } |
| | 228 | | } |