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