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