| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.Interface; |
| | 5 | | using DCL.Tasks; |
| | 6 | | using DCLServices.CopyPaste.Analytics; |
| | 7 | | using DCLServices.MapRendererV2; |
| | 8 | | using DCLServices.MapRendererV2.CommonBehavior; |
| | 9 | | using DCLServices.MapRendererV2.ConsumerUtils; |
| | 10 | | using DCLServices.MapRendererV2.MapCameraController; |
| | 11 | | using DCLServices.MapRendererV2.MapLayers; |
| | 12 | | using DCLServices.MapRendererV2.MapLayers.PlayerMarker; |
| | 13 | | using DCLServices.PlacesAPIService; |
| | 14 | | using System; |
| | 15 | | using System.Collections.Generic; |
| | 16 | | using System.Threading; |
| | 17 | | using UnityEngine; |
| | 18 | | using Object = UnityEngine.Object; |
| | 19 | |
|
| | 20 | | public class MinimapHUDController : IHUD, IMapActivityOwner |
| | 21 | | { |
| | 22 | | private static bool VERBOSE = false; |
| | 23 | | private const MapLayer RENDER_LAYERS = MapLayer.SatelliteAtlas | MapLayer.ParcelsAtlas | MapLayer.HomePoint | MapLay |
| | 24 | |
|
| | 25 | | public MinimapHUDView view; |
| | 26 | |
|
| 10 | 27 | | private FloatVariable minimapZoom => CommonScriptableObjects.minimapZoom; |
| 20 | 28 | | private IntVariable currentSceneNumber => CommonScriptableObjects.sceneNumber; |
| 11 | 29 | | private Vector2IntVariable playerCoords => CommonScriptableObjects.playerCoords; |
| 10 | 30 | | private bool isContentModerationFeatureEnabled => DataStore.i.featureFlags.flags.Get().IsFeatureEnabled("content_mod |
| | 31 | | private Vector2Int currentCoords; |
| 10 | 32 | | private Vector2Int homeCoords = new (0, 0); |
| | 33 | |
|
| | 34 | | private readonly MinimapMetadataController metadataController; |
| | 35 | | private readonly IHomeLocationController locationController; |
| | 36 | | private readonly DCL.Environment.Model environment; |
| | 37 | | private readonly IPlacesAPIService placesAPIService; |
| | 38 | | private readonly IPlacesAnalytics placesAnalytics; |
| | 39 | | private readonly IClipboard clipboard; |
| | 40 | | private readonly ICopyPasteAnalyticsService copyPasteAnalyticsService; |
| | 41 | | private readonly DataStore_ContentModeration contentModerationDataStore; |
| | 42 | | private readonly IWorldState worldState; |
| 10 | 43 | | private readonly BaseVariable<bool> minimapVisible = DataStore.i.HUDs.minimapVisible; |
| 10 | 44 | | private readonly CancellationTokenSource disposingCts = new (); |
| | 45 | |
|
| 10 | 46 | | public IReadOnlyDictionary<MapLayer, IMapLayerParameter> LayersParameters { get; } = new Dictionary<MapLayer, IMapLa |
| | 47 | | { { MapLayer.PlayerMarker, new PlayerMarkerParameter {BackgroundIsActive = false} } }; |
| | 48 | |
|
| | 49 | | private Service<IMapRenderer> mapRenderer; |
| | 50 | | private IMapCameraController mapCameraController; |
| | 51 | | private MapRendererTrackPlayerPosition mapRendererTrackPlayerPosition; |
| | 52 | | private CancellationTokenSource retrievingFavoritesCts; |
| | 53 | | private SceneContentCategory currentContentCategory; |
| | 54 | |
|
| 42 | 55 | | private MinimapHUDModel model { get; set; } = new (); |
| | 56 | |
|
| 10 | 57 | | public MinimapHUDController( |
| | 58 | | MinimapMetadataController minimapMetadataController, |
| | 59 | | IHomeLocationController locationController, |
| | 60 | | DCL.Environment.Model environment, |
| | 61 | | IPlacesAPIService placesAPIService, |
| | 62 | | IPlacesAnalytics placesAnalytics, |
| | 63 | | IClipboard clipboard, |
| | 64 | | ICopyPasteAnalyticsService copyPasteAnalyticsService, |
| | 65 | | DataStore_ContentModeration contentModerationDataStore, |
| | 66 | | IWorldState worldState) |
| | 67 | | { |
| 10 | 68 | | minimapZoom.Set(1f); |
| 10 | 69 | | metadataController = minimapMetadataController; |
| 10 | 70 | | this.locationController = locationController; |
| 10 | 71 | | this.environment = environment; |
| 10 | 72 | | this.placesAPIService = placesAPIService; |
| 10 | 73 | | this.placesAnalytics = placesAnalytics; |
| 10 | 74 | | this.clipboard = clipboard; |
| 10 | 75 | | this.copyPasteAnalyticsService = copyPasteAnalyticsService; |
| 10 | 76 | | this.contentModerationDataStore = contentModerationDataStore; |
| 10 | 77 | | this.worldState = worldState; |
| | 78 | |
|
| 10 | 79 | | if (metadataController != null) |
| 0 | 80 | | metadataController.OnHomeChanged += SetNewHome; |
| | 81 | |
|
| 10 | 82 | | minimapVisible.OnChange += SetVisibility; |
| 10 | 83 | | currentSceneNumber.OnChange += OnSceneNumberChanged; |
| 10 | 84 | | } |
| | 85 | |
|
| | 86 | | protected virtual MinimapHUDView CreateView() => |
| 10 | 87 | | MinimapHUDView.Create(this); |
| | 88 | |
|
| | 89 | | public void Initialize() |
| | 90 | | { |
| 10 | 91 | | view = CreateView(); |
| 10 | 92 | | view.SetReportSceneButtonActive(!isContentModerationFeatureEnabled); |
| 10 | 93 | | view.OnFavoriteToggleClicked += OnFavoriteToggleClicked; |
| 10 | 94 | | view.OnCopyLocationRequested += OnCopyLocationToClipboard; |
| 10 | 95 | | view.contentModerationButton.OnContentModerationPressed += OpenContentModerationPanel; |
| 10 | 96 | | InitializeMapRenderer(); |
| | 97 | |
|
| 10 | 98 | | OnPlayerCoordsChange(CommonScriptableObjects.playerCoords.Get(), Vector2Int.zero); |
| 10 | 99 | | SetVisibility(minimapVisible.Get()); |
| | 100 | |
|
| 10 | 101 | | CommonScriptableObjects.playerCoords.OnChange += OnPlayerCoordsChange; |
| 10 | 102 | | CommonScriptableObjects.isFullscreenHUDOpen.OnChange += OnFullscreenUIVisibilityChange; |
| 10 | 103 | | MinimapMetadata.GetMetadata().OnSceneInfoUpdated += OnSceneInfoUpdated; |
| 10 | 104 | | } |
| | 105 | |
|
| | 106 | | public void Dispose() |
| | 107 | | { |
| 10 | 108 | | disposingCts?.SafeCancelAndDispose(); |
| | 109 | |
|
| 10 | 110 | | if (view != null) |
| | 111 | | { |
| 10 | 112 | | DisposeMapRenderer(); |
| 10 | 113 | | Object.Destroy(view.gameObject); |
| | 114 | | } |
| | 115 | |
|
| 10 | 116 | | CommonScriptableObjects.playerCoords.OnChange -= OnPlayerCoordsChange; |
| 10 | 117 | | MinimapMetadata.GetMetadata().OnSceneInfoUpdated -= OnSceneInfoUpdated; |
| | 118 | |
|
| 10 | 119 | | if (metadataController != null) |
| 0 | 120 | | metadataController.OnHomeChanged -= SetNewHome; |
| | 121 | |
|
| 10 | 122 | | minimapVisible.OnChange -= SetVisibility; |
| 10 | 123 | | view.contentModerationButton.OnContentModerationPressed -= OpenContentModerationPanel; |
| 10 | 124 | | currentSceneNumber.OnChange -= OnSceneNumberChanged; |
| 10 | 125 | | } |
| | 126 | |
|
| | 127 | | private void InitializeMapRenderer() |
| | 128 | | { |
| 10 | 129 | | view.pixelPerfectMapRendererTextureProvider.SetHudCamera(DataStore.i.camera.hudsCamera.Get()); |
| | 130 | |
|
| 10 | 131 | | mapCameraController = mapRenderer.Ref.RentCamera( |
| | 132 | | new MapCameraInput( |
| | 133 | | this, |
| | 134 | | RENDER_LAYERS, |
| | 135 | | Vector2Int.RoundToInt(MapRendererTrackPlayerPosition.GetPlayerCentricCoords(playerCoords.Get())), |
| | 136 | | 1, |
| | 137 | | view.pixelPerfectMapRendererTextureProvider.GetPixelPerfectTextureResolution(), |
| | 138 | | new Vector2Int(view.mapRendererVisibleParcels, view.mapRendererVisibleParcels) |
| | 139 | | ) |
| | 140 | | ); |
| | 141 | |
|
| 10 | 142 | | mapRendererTrackPlayerPosition = new MapRendererTrackPlayerPosition(mapCameraController, DataStore.i.player.play |
| 10 | 143 | | view.mapRendererTargetImage.texture = mapCameraController.GetRenderTexture(); |
| 10 | 144 | | view.pixelPerfectMapRendererTextureProvider.Activate(mapCameraController); |
| | 145 | |
|
| 10 | 146 | | DataStore.i.HUDs.navmapIsRendered.OnChange += OnNavMapIsRenderedChange; |
| 10 | 147 | | } |
| | 148 | |
|
| | 149 | | private void OnNavMapIsRenderedChange(bool current, bool previous) |
| | 150 | | { |
| 0 | 151 | | if (current) |
| 0 | 152 | | mapCameraController.SuspendRendering(); |
| | 153 | | else |
| 0 | 154 | | mapCameraController.ResumeRendering(); |
| 0 | 155 | | } |
| | 156 | |
|
| | 157 | | private void DisposeMapRenderer() |
| | 158 | | { |
| 10 | 159 | | if (mapCameraController != null) |
| | 160 | | { |
| 10 | 161 | | DataStore.i.HUDs.navmapIsRendered.OnChange -= OnNavMapIsRenderedChange; |
| 10 | 162 | | view.pixelPerfectMapRendererTextureProvider.Deactivate(); |
| 10 | 163 | | mapRendererTrackPlayerPosition.Dispose(); |
| 10 | 164 | | mapCameraController.Release(this); |
| 10 | 165 | | mapCameraController = null; |
| | 166 | | } |
| 10 | 167 | | } |
| | 168 | |
|
| | 169 | | private void OnPlayerCoordsChange(Vector2Int current, Vector2Int previous) |
| | 170 | | { |
| 10 | 171 | | currentCoords = current; |
| 10 | 172 | | UpdatePlayerPosition(currentCoords); |
| 10 | 173 | | UpdateSetHomePanel(); |
| 10 | 174 | | MinimapMetadata.MinimapSceneInfo sceneInfo = MinimapMetadata.GetMetadata().GetSceneInfo(currentCoords.x, current |
| | 175 | |
|
| 10 | 176 | | if (sceneInfo != null) |
| | 177 | | { |
| 0 | 178 | | UpdateSceneName(sceneInfo.name); |
| | 179 | |
|
| 0 | 180 | | if (sceneInfo.parcels.Count > 0) |
| 0 | 181 | | RetrieveFavoriteState(sceneInfo.parcels[0]); |
| | 182 | | } |
| 10 | 183 | | } |
| | 184 | |
|
| | 185 | | private void OnFullscreenUIVisibilityChange(bool current, bool previous) |
| | 186 | | { |
| 0 | 187 | | OnPlayerCoordsChange(currentCoords, Vector2Int.zero); |
| 0 | 188 | | } |
| | 189 | |
|
| | 190 | | private void SetNewHome(Vector2Int newHomeCoordinates) |
| | 191 | | { |
| 0 | 192 | | homeCoords = newHomeCoordinates; |
| 0 | 193 | | UpdateSetHomePanel(); |
| 0 | 194 | | } |
| | 195 | |
|
| | 196 | | private void UpdateData(MinimapHUDModel model) |
| | 197 | | { |
| 0 | 198 | | this.model = model; |
| 0 | 199 | | view.UpdateData(this.model); |
| 0 | 200 | | } |
| | 201 | |
|
| | 202 | | public void UpdateSceneName(string sceneName) |
| | 203 | | { |
| 2 | 204 | | model.sceneName = sceneName; |
| 2 | 205 | | view.UpdateData(model); |
| 2 | 206 | | } |
| | 207 | |
|
| | 208 | | public void UpdatePlayerPosition(Vector2 position) |
| | 209 | | { |
| | 210 | | const string format = "{0},{1}"; |
| 12 | 211 | | UpdatePlayerPosition(string.Format(format, position.x, position.y)); |
| 12 | 212 | | } |
| | 213 | |
|
| | 214 | | private void UpdateSetHomePanel() |
| | 215 | | { |
| 10 | 216 | | view.UpdateSetHomePanel(currentCoords == homeCoords); |
| 10 | 217 | | } |
| | 218 | |
|
| | 219 | | public void UpdatePlayerPosition(string position) |
| | 220 | | { |
| 13 | 221 | | model.playerPosition = position; |
| 13 | 222 | | view.UpdateData(model); |
| 13 | 223 | | } |
| | 224 | |
|
| | 225 | | public void AddZoomDelta(float delta) |
| | 226 | | { |
| 0 | 227 | | minimapZoom.Set(Mathf.Clamp01(minimapZoom.Get() + delta)); |
| 0 | 228 | | } |
| | 229 | |
|
| | 230 | | public void ToggleOptions() |
| | 231 | | { |
| 2 | 232 | | view.ToggleOptions(); |
| 2 | 233 | | } |
| | 234 | |
|
| | 235 | | public void ToggleSceneUI(bool isUIOn) |
| | 236 | | { |
| 0 | 237 | | DataStore.i.HUDs.isCurrentSceneUiEnabled.Set(isUIOn); |
| 0 | 238 | | } |
| | 239 | |
|
| | 240 | | public void AddBookmark() |
| | 241 | | { |
| | 242 | | //TODO: |
| 0 | 243 | | if (VERBOSE) { Debug.Log("Add bookmark pressed"); } |
| 0 | 244 | | } |
| | 245 | |
|
| | 246 | | public void ReportScene() |
| | 247 | | { |
| 1 | 248 | | var coords = playerCoords.Get(); |
| 1 | 249 | | WebInterface.SendReportScene(environment.world.state.GetSceneNumberByCoords(coords)); |
| 1 | 250 | | } |
| | 251 | |
|
| | 252 | | public void SetHomeScene(bool isOn) |
| | 253 | | { |
| 0 | 254 | | var coords = playerCoords.Get(); |
| | 255 | |
|
| 0 | 256 | | if (playerCoords == homeCoords) |
| | 257 | | { |
| 0 | 258 | | if (!isOn) |
| 0 | 259 | | locationController.SetHomeScene(new Vector2(0, 0)); |
| | 260 | | } |
| | 261 | | else |
| | 262 | | { |
| 0 | 263 | | if (isOn) |
| 0 | 264 | | locationController.SetHomeScene(new Vector2(coords.x, coords.y)); |
| | 265 | | } |
| 0 | 266 | | } |
| | 267 | |
|
| | 268 | | public void SetVisibility(bool visible) |
| | 269 | | { |
| 11 | 270 | | view.SetVisibility(visible && minimapVisible.Get()); |
| 11 | 271 | | } |
| | 272 | |
|
| | 273 | | private void OnSceneInfoUpdated(MinimapMetadata.MinimapSceneInfo sceneInfo) |
| | 274 | | { |
| 1 | 275 | | if (sceneInfo.parcels.Contains(CommonScriptableObjects.playerCoords.Get())) |
| | 276 | | { |
| 0 | 277 | | UpdateSceneName(sceneInfo.name); |
| | 278 | |
|
| 0 | 279 | | if (sceneInfo.parcels.Count > 0) |
| 0 | 280 | | RetrieveFavoriteState(sceneInfo.parcels[0]); |
| | 281 | | } |
| 1 | 282 | | } |
| | 283 | |
|
| | 284 | | private void RetrieveFavoriteState(Vector2Int currentParcel) |
| | 285 | | { |
| 0 | 286 | | retrievingFavoritesCts?.SafeCancelAndDispose(); |
| 0 | 287 | | retrievingFavoritesCts = CancellationTokenSource.CreateLinkedTokenSource(disposingCts.Token); |
| | 288 | |
|
| 0 | 289 | | RetrieveFavoriteStateAsync(currentParcel, retrievingFavoritesCts.Token).Forget(); |
| 0 | 290 | | } |
| | 291 | |
|
| | 292 | | private async UniTaskVoid RetrieveFavoriteStateAsync(Vector2Int currentParcel, CancellationToken ct) |
| | 293 | | { |
| | 294 | | try |
| | 295 | | { |
| 0 | 296 | | var place = await placesAPIService.GetPlace(currentParcel, ct); |
| 0 | 297 | | bool isFavorite = await placesAPIService.IsFavoritePlace(place, ct); |
| 0 | 298 | | view.SetIsAPlace(true); |
| 0 | 299 | | view.SetCurrentFavoriteStatus(place.id, isFavorite); |
| 0 | 300 | | } |
| 0 | 301 | | catch (NotAPlaceException) { view.SetIsAPlace(false); } |
| 0 | 302 | | catch (OperationCanceledException) { view.SetIsAPlace(false); } |
| 0 | 303 | | } |
| | 304 | |
|
| | 305 | | private void OnFavoriteToggleClicked(string uuid, bool isFavorite) |
| | 306 | | { |
| 0 | 307 | | if (isFavorite) |
| 0 | 308 | | placesAnalytics.AddFavorite(uuid, IPlacesAnalytics.ActionSource.FromMinimap); |
| | 309 | | else |
| 0 | 310 | | placesAnalytics.RemoveFavorite(uuid, IPlacesAnalytics.ActionSource.FromMinimap); |
| | 311 | |
|
| 0 | 312 | | placesAPIService.SetPlaceFavorite(uuid, isFavorite, default).Forget(); |
| 0 | 313 | | } |
| | 314 | |
|
| | 315 | | private void SetVisibility(bool current, bool _) => |
| 0 | 316 | | SetVisibility(current); |
| | 317 | |
|
| | 318 | | private void OnCopyLocationToClipboard() |
| | 319 | | { |
| 1 | 320 | | clipboard.WriteText($"{model.sceneName}: {model.playerPosition}"); |
| 1 | 321 | | view.ShowLocationCopiedToast(); |
| 1 | 322 | | copyPasteAnalyticsService.Copy("location"); |
| 1 | 323 | | } |
| | 324 | |
|
| | 325 | | private void OpenContentModerationPanel() |
| | 326 | | { |
| 0 | 327 | | if (!isContentModerationFeatureEnabled) |
| 0 | 328 | | return; |
| | 329 | |
|
| 0 | 330 | | contentModerationDataStore.reportingScenePanelVisible.Set((!contentModerationDataStore.reportingScenePanelVisibl |
| 0 | 331 | | } |
| | 332 | |
|
| | 333 | | private void OnSceneNumberChanged(int sceneNumber, int _) |
| | 334 | | { |
| 0 | 335 | | if (!worldState.TryGetScene(sceneNumber, out IParcelScene currentParcelScene)) |
| 0 | 336 | | return; |
| | 337 | |
|
| 0 | 338 | | currentContentCategory = currentParcelScene.contentCategory; |
| 0 | 339 | | view.contentModerationButton.SetContentCategory(currentParcelScene.contentCategory); |
| 0 | 340 | | } |
| | 341 | | } |