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