| | 1 | | using UnityEngine; |
| | 2 | | using UnityEngine.UI; |
| | 3 | | using DCL.Interface; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using TMPro; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | public class NavmapView : MonoBehaviour |
| | 10 | | { |
| | 11 | | [Header("References")] |
| | 12 | | [SerializeField] Button closeButton; |
| | 13 | | [SerializeField] InputAction_Trigger closeAction; |
| | 14 | | [SerializeField] internal ScrollRect scrollRect; |
| | 15 | | [SerializeField] Transform scrollRectContentTransform; |
| | 16 | | [SerializeField] internal TextMeshProUGUI currentSceneNameText; |
| | 17 | | [SerializeField] internal TextMeshProUGUI currentSceneCoordsText; |
| | 18 | | [SerializeField] internal NavmapToastView toastView; |
| | 19 | |
|
| | 20 | | InputAction_Trigger.Triggered selectParcelDelegate; |
| | 21 | | RectTransform minimapViewport; |
| | 22 | | Transform mapRendererMinimapParent; |
| | 23 | | Vector3 atlasOriginalPosition; |
| | 24 | | MinimapMetadata mapMetadata; |
| | 25 | |
|
| 0 | 26 | | public BaseVariable<bool> navmapVisible => DataStore.i.HUDs.navmapVisible; |
| | 27 | | public static event System.Action<bool> OnToggle; |
| | 28 | |
|
| | 29 | | void Start() |
| | 30 | | { |
| 104 | 31 | | mapMetadata = MinimapMetadata.GetMetadata(); |
| | 32 | |
|
| 104 | 33 | | closeButton.onClick.AddListener(() => |
| | 34 | | { |
| 0 | 35 | | navmapVisible.Set(false); |
| 0 | 36 | | Utils.UnlockCursor(); |
| 0 | 37 | | }); |
| 104 | 38 | | scrollRect.onValueChanged.AddListener((x) => |
| | 39 | | { |
| 0 | 40 | | if (!navmapVisible.Get()) |
| 0 | 41 | | return; |
| | 42 | |
|
| 0 | 43 | | MapRenderer.i.atlas.UpdateCulling(); |
| 0 | 44 | | toastView.OnCloseClick(); |
| 0 | 45 | | }); |
| | 46 | |
|
| 104 | 47 | | toastView.OnGotoClicked += () => navmapVisible.Set(false); |
| | 48 | |
|
| 104 | 49 | | MapRenderer.OnParcelClicked += TriggerToast; |
| 104 | 50 | | MapRenderer.OnParcelHold += TriggerToast; |
| 104 | 51 | | MapRenderer.OnParcelHoldCancel += () => { toastView.OnCloseClick(); }; |
| 104 | 52 | | CommonScriptableObjects.playerCoords.OnChange += UpdateCurrentSceneData; |
| 104 | 53 | | closeAction.OnTriggered += OnCloseAction; |
| 104 | 54 | | navmapVisible.OnChange += OnNavmapVisibleChanged; |
| | 55 | |
|
| 104 | 56 | | Initialize(); |
| 104 | 57 | | } |
| 0 | 58 | | private void OnNavmapVisibleChanged(bool current, bool previous) { SetVisible(current); } |
| | 59 | |
|
| | 60 | | public void Initialize() |
| | 61 | | { |
| 106 | 62 | | toastView.gameObject.SetActive(false); |
| 106 | 63 | | scrollRect.gameObject.SetActive(false); |
| 106 | 64 | | } |
| | 65 | |
|
| | 66 | | private void OnDestroy() |
| | 67 | | { |
| 103 | 68 | | MapRenderer.OnParcelClicked -= TriggerToast; |
| 103 | 69 | | MapRenderer.OnParcelHold -= TriggerToast; |
| 103 | 70 | | CommonScriptableObjects.playerCoords.OnChange -= UpdateCurrentSceneData; |
| 103 | 71 | | navmapVisible.OnChange -= OnNavmapVisibleChanged; |
| 103 | 72 | | closeAction.OnTriggered += OnCloseAction; |
| 103 | 73 | | } |
| | 74 | |
|
| | 75 | | internal void SetVisible(bool visible) |
| | 76 | | { |
| 2 | 77 | | if (MapRenderer.i == null) |
| 0 | 78 | | return; |
| | 79 | |
|
| 2 | 80 | | scrollRect.StopMovement(); |
| | 81 | |
|
| 2 | 82 | | scrollRect.gameObject.SetActive(visible); |
| 2 | 83 | | MapRenderer.i.parcelHighlightEnabled = visible; |
| | 84 | |
|
| 2 | 85 | | if (visible) |
| | 86 | | { |
| 2 | 87 | | Utils.UnlockCursor(); |
| | 88 | |
|
| 2 | 89 | | minimapViewport = MapRenderer.i.atlas.viewport; |
| 2 | 90 | | mapRendererMinimapParent = MapRenderer.i.transform.parent; |
| 2 | 91 | | atlasOriginalPosition = MapRenderer.i.atlas.chunksParent.transform.localPosition; |
| | 92 | |
|
| 2 | 93 | | MapRenderer.i.atlas.viewport = scrollRect.viewport; |
| 2 | 94 | | MapRenderer.i.transform.SetParent(scrollRectContentTransform); |
| 2 | 95 | | MapRenderer.i.atlas.UpdateCulling(); |
| | 96 | |
|
| 2 | 97 | | scrollRect.content = MapRenderer.i.atlas.chunksParent.transform as RectTransform; |
| | 98 | |
|
| | 99 | | // Reparent the player icon parent to scroll everything together |
| 2 | 100 | | MapRenderer.i.atlas.overlayLayerGameobject.transform.SetParent(scrollRect.content); |
| | 101 | |
|
| | 102 | | // Center map |
| 2 | 103 | | MapRenderer.i.atlas.CenterToTile(Utils.WorldToGridPositionUnclamped(CommonScriptableObjects.playerWorldP |
| | 104 | |
|
| | 105 | | // Set shorter interval of time for populated scenes markers fetch |
| 2 | 106 | | MapRenderer.i.usersPositionMarkerController?.SetUpdateMode(MapGlobalUsersPositionMarkerController.Update |
| | 107 | |
|
| 2 | 108 | | AudioScriptableObjects.dialogOpen.Play(true); |
| | 109 | |
|
| 2 | 110 | | CommonScriptableObjects.isFullscreenHUDOpen.Set(true); |
| 2 | 111 | | } |
| | 112 | | else |
| | 113 | | { |
| 0 | 114 | | Utils.LockCursor(); |
| | 115 | |
|
| 0 | 116 | | toastView.OnCloseClick(); |
| | 117 | |
|
| 0 | 118 | | MapRenderer.i.atlas.viewport = minimapViewport; |
| 0 | 119 | | MapRenderer.i.transform.SetParent(mapRendererMinimapParent); |
| 0 | 120 | | MapRenderer.i.atlas.chunksParent.transform.localPosition = atlasOriginalPosition; |
| 0 | 121 | | MapRenderer.i.atlas.UpdateCulling(); |
| | 122 | |
|
| | 123 | | // Restore the player icon to its original parent |
| 0 | 124 | | MapRenderer.i.atlas.overlayLayerGameobject.transform.SetParent(MapRenderer.i.atlas.chunksParent.transfor |
| 0 | 125 | | (MapRenderer.i.atlas.overlayLayerGameobject.transform as RectTransform).anchoredPosition = Vector2.zero; |
| | 126 | |
|
| 0 | 127 | | MapRenderer.i.UpdateRendering(Utils.WorldToGridPositionUnclamped(CommonScriptableObjects.playerWorldPosi |
| | 128 | |
|
| | 129 | | // Set longer interval of time for populated scenes markers fetch |
| 0 | 130 | | MapRenderer.i.usersPositionMarkerController?.SetUpdateMode(MapGlobalUsersPositionMarkerController.Update |
| | 131 | |
|
| 0 | 132 | | AudioScriptableObjects.dialogClose.Play(true); |
| 0 | 133 | | CommonScriptableObjects.isFullscreenHUDOpen.Set(false); |
| | 134 | | } |
| | 135 | |
|
| 2 | 136 | | OnToggle?.Invoke(visible); |
| 0 | 137 | | } |
| | 138 | |
|
| 198 | 139 | | private void OnCloseAction(DCLAction_Trigger action) { navmapVisible.Set(false); } |
| | 140 | | void UpdateCurrentSceneData(Vector2Int current, Vector2Int previous) |
| | 141 | | { |
| | 142 | | const string format = "{0},{1}"; |
| 18 | 143 | | currentSceneCoordsText.text = string.Format(format, current.x, current.y); |
| 18 | 144 | | currentSceneNameText.text = MinimapMetadata.GetMetadata().GetSceneInfo(current.x, current.y)?.name ?? "Unnam |
| 18 | 145 | | } |
| | 146 | |
|
| | 147 | | void TriggerToast(int cursorTileX, int cursorTileY) |
| | 148 | | { |
| 0 | 149 | | var sceneInfo = mapMetadata.GetSceneInfo(cursorTileX, cursorTileY); |
| 0 | 150 | | if (sceneInfo == null) |
| 0 | 151 | | WebInterface.RequestScenesInfoAroundParcel(new Vector2(cursorTileX, cursorTileY), 15); |
| | 152 | |
|
| 0 | 153 | | toastView.Populate(new Vector2Int(cursorTileX, cursorTileY), sceneInfo); |
| 0 | 154 | | } |
| | 155 | | } |
| | 156 | | } |