| | 1 | | using DCL; |
| | 2 | | using DCL.Interface; |
| | 3 | | using UnityEngine; |
| | 4 | | using System; |
| | 5 | |
|
| | 6 | | public class MinimapHUDController : IHUD |
| | 7 | | { |
| | 8 | | private static bool VERBOSE = false; |
| | 9 | |
|
| | 10 | | public MinimapHUDView view; |
| 11 | 11 | | private FloatVariable minimapZoom => CommonScriptableObjects.minimapZoom; |
| 0 | 12 | | private StringVariable currentSceneId => CommonScriptableObjects.sceneID; |
| 1 | 13 | | private Vector2IntVariable playerCoords => CommonScriptableObjects.playerCoords; |
| | 14 | | private Vector2Int currentCoords; |
| 11 | 15 | | private Vector2Int homeCoords = new Vector2Int(0,0); |
| | 16 | | private MinimapMetadataController metadataController; |
| | 17 | | private IHomeLocationController locationController; |
| | 18 | |
|
| 11 | 19 | | public MinimapHUDModel model { get; private set; } = new MinimapHUDModel(); |
| | 20 | |
|
| 22 | 21 | | public MinimapHUDController(MinimapMetadataController minimapMetadataController, IHomeLocationController locationCon |
| | 22 | |
|
| 11 | 23 | | public MinimapHUDController(MinimapHUDModel model, MinimapMetadataController minimapMetadataController, IHomeLocatio |
| | 24 | | { |
| 11 | 25 | | CommonScriptableObjects.playerCoords.OnChange += OnPlayerCoordsChange; |
| 11 | 26 | | CommonScriptableObjects.builderInWorldNotNecessaryUIVisibilityStatus.OnChange += ChangeVisibilityForBuilderInWor |
| 11 | 27 | | minimapZoom.Set(1f); |
| 11 | 28 | | UpdateData(model); |
| 11 | 29 | | metadataController = minimapMetadataController; |
| 11 | 30 | | this.locationController = locationController; |
| 11 | 31 | | if(metadataController != null) |
| 0 | 32 | | metadataController.OnHomeChanged += SetNewHome; |
| 11 | 33 | | } |
| | 34 | |
|
| 11 | 35 | | protected internal virtual MinimapHUDView CreateView() { return MinimapHUDView.Create(this); } |
| | 36 | |
|
| | 37 | | public void Initialize() |
| | 38 | | { |
| 11 | 39 | | view = CreateView(); |
| 11 | 40 | | UpdateData(model); |
| 11 | 41 | | } |
| | 42 | |
|
| | 43 | | public void Dispose() |
| | 44 | | { |
| 11 | 45 | | if (view != null) |
| 11 | 46 | | UnityEngine.Object.Destroy(view.gameObject); |
| | 47 | |
|
| 11 | 48 | | CommonScriptableObjects.playerCoords.OnChange -= OnPlayerCoordsChange; |
| 11 | 49 | | CommonScriptableObjects.builderInWorldNotNecessaryUIVisibilityStatus.OnChange -= ChangeVisibilityForBuilderInWor |
| 11 | 50 | | MinimapMetadata.GetMetadata().OnSceneInfoUpdated -= OnOnSceneInfoUpdated; |
| | 51 | |
|
| 11 | 52 | | if (metadataController != null) |
| 0 | 53 | | metadataController.OnHomeChanged -= SetNewHome; |
| 11 | 54 | | } |
| | 55 | |
|
| | 56 | | private void OnPlayerCoordsChange(Vector2Int current, Vector2Int previous) |
| | 57 | | { |
| 1 | 58 | | currentCoords = current; |
| 1 | 59 | | UpdatePlayerPosition(currentCoords); |
| 1 | 60 | | UpdateSetHomePanel(); |
| 1 | 61 | | MinimapMetadata.GetMetadata().OnSceneInfoUpdated -= OnOnSceneInfoUpdated; |
| 1 | 62 | | MinimapMetadata.MinimapSceneInfo sceneInfo = MinimapMetadata.GetMetadata().GetSceneInfo(currentCoords.x, current |
| 1 | 63 | | UpdateSceneName(sceneInfo?.name); |
| | 64 | |
|
| | 65 | | // NOTE: in some cases playerCoords OnChange is triggered before kernel's message with the scene info arrives. |
| | 66 | | // so in that scenario we subscribe to MinimapMetadata event to wait for the scene info. |
| 1 | 67 | | if (sceneInfo == null) |
| | 68 | | { |
| 0 | 69 | | MinimapMetadata.GetMetadata().OnSceneInfoUpdated += OnOnSceneInfoUpdated; |
| | 70 | | } |
| 1 | 71 | | } |
| | 72 | |
|
| | 73 | | private void SetNewHome(Vector2Int newHomeCoordinates) |
| | 74 | | { |
| 0 | 75 | | homeCoords = newHomeCoordinates; |
| 0 | 76 | | UpdateSetHomePanel(); |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | public void UpdateData(MinimapHUDModel model) |
| | 80 | | { |
| 22 | 81 | | this.model = model; |
| 22 | 82 | | view?.UpdateData(this.model); |
| 11 | 83 | | } |
| | 84 | |
|
| | 85 | | public void UpdateSceneName(string sceneName) |
| | 86 | | { |
| 2 | 87 | | model.sceneName = sceneName; |
| 2 | 88 | | view?.UpdateData(model); |
| 2 | 89 | | } |
| | 90 | |
|
| | 91 | | public void UpdatePlayerPosition(Vector2 position) |
| | 92 | | { |
| | 93 | | const string format = "{0},{1}"; |
| 2 | 94 | | UpdatePlayerPosition(string.Format(format, position.x, position.y)); |
| 2 | 95 | | } |
| | 96 | |
|
| | 97 | | public void UpdateSetHomePanel() |
| | 98 | | { |
| 1 | 99 | | view.UpdateSetHomePanel(currentCoords == homeCoords); |
| 1 | 100 | | } |
| | 101 | |
|
| | 102 | | public void UpdatePlayerPosition(string position) |
| | 103 | | { |
| 3 | 104 | | model.playerPosition = position; |
| 3 | 105 | | view?.UpdateData(model); |
| 3 | 106 | | } |
| | 107 | |
|
| 0 | 108 | | public void AddZoomDelta(float delta) { minimapZoom.Set(Mathf.Clamp01(minimapZoom.Get() + delta)); } |
| | 109 | |
|
| 4 | 110 | | public void ToggleOptions() { view.ToggleOptions(); } |
| | 111 | |
|
| 0 | 112 | | public void ToggleSceneUI(bool isUIOn) { DataStore.i.HUDs.isSceneUIEnabled.Set(isUIOn); } |
| | 113 | |
|
| | 114 | | public void AddBookmark() |
| | 115 | | { |
| | 116 | | //TODO: |
| 0 | 117 | | if (VERBOSE) |
| | 118 | | { |
| 0 | 119 | | Debug.Log("Add bookmark pressed"); |
| | 120 | | } |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | public void ReportScene() |
| | 124 | | { |
| 1 | 125 | | var coords = playerCoords.Get(); |
| 1 | 126 | | WebInterface.SendReportScene($"{coords.x},{coords.y}"); |
| 1 | 127 | | } |
| | 128 | |
|
| | 129 | | public void SetHomeScene(bool isOn) |
| | 130 | | { |
| 0 | 131 | | var coords = playerCoords.Get(); |
| 0 | 132 | | if (playerCoords == homeCoords) |
| | 133 | | { |
| 0 | 134 | | if (!isOn) |
| 0 | 135 | | locationController.SetHomeScene(new Vector2(0,0)); |
| 0 | 136 | | } |
| | 137 | | else |
| | 138 | | { |
| 0 | 139 | | if(isOn) |
| 0 | 140 | | locationController.SetHomeScene(new Vector2(coords.x,coords.y)); |
| | 141 | | } |
| 0 | 142 | | } |
| | 143 | |
|
| 0 | 144 | | public void ChangeVisibilityForBuilderInWorld(bool current, bool previus) { view.gameObject.SetActive(current); } |
| | 145 | |
|
| 2 | 146 | | public void SetVisibility(bool visible) { view.SetVisibility(visible); } |
| | 147 | |
|
| | 148 | | /// <summary> |
| | 149 | | /// Enable user's around button/indicator that shows the amount of users around player |
| | 150 | | /// and toggle the list of players' visibility when pressed |
| | 151 | | /// </summary> |
| | 152 | | /// <param name="controller">Controller for the players' list HUD</param> |
| | 153 | | public void AddUsersAroundIndicator(UsersAroundListHUDController controller) |
| | 154 | | { |
| 0 | 155 | | view.usersAroundListHudButton.gameObject.SetActive(true); |
| 0 | 156 | | controller.SetButtonView(view.usersAroundListHudButton); |
| 0 | 157 | | controller.ToggleUsersCount(false); |
| 0 | 158 | | KernelConfig.i.EnsureConfigInitialized().Then(kc => controller.ToggleUsersCount(kc.features.enablePeopleCounter) |
| 0 | 159 | | } |
| | 160 | |
|
| | 161 | | private void OnOnSceneInfoUpdated(MinimapMetadata.MinimapSceneInfo sceneInfo) |
| | 162 | | { |
| 0 | 163 | | if (sceneInfo.parcels.Contains(CommonScriptableObjects.playerCoords.Get())) |
| | 164 | | { |
| 0 | 165 | | MinimapMetadata.GetMetadata().OnSceneInfoUpdated -= OnOnSceneInfoUpdated; |
| 0 | 166 | | UpdateSceneName(sceneInfo.name); |
| | 167 | | } |
| 0 | 168 | | } |
| | 169 | | } |