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