| | 1 | | using DCL; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using TMPro; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | public interface IPlaceCardComponentView |
| | 9 | | { |
| | 10 | | FriendsHandler friendsHandler { get; set; } |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// Event that will be triggered when the jumpIn button is clicked. |
| | 14 | | /// </summary> |
| | 15 | | Button.ButtonClickedEvent onJumpInClick { get; } |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// Event that will be triggered when the info button is clicked. |
| | 19 | | /// </summary> |
| | 20 | | Button.ButtonClickedEvent onInfoClick { get; } |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Set the place picture directly from a sprite. |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="sprite">Place picture (sprite).</param> |
| | 26 | | void SetPlacePicture(Sprite sprite); |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Set the place picture from a 2D texture. |
| | 30 | | /// </summary> |
| | 31 | | /// <param name="texture">Place picture (texture).</param> |
| | 32 | | void SetPlacePicture(Texture2D texture); |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Set the place picture from an uri. |
| | 36 | | /// </summary> |
| | 37 | | /// <param name="uri">Place picture (url).</param> |
| | 38 | | void SetPlacePicture(string uri); |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Set the place name in the card. |
| | 42 | | /// </summary> |
| | 43 | | /// <param name="newText">New place name.</param> |
| | 44 | | void SetPlaceName(string newText); |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Set the place description in the card. |
| | 48 | | /// </summary> |
| | 49 | | /// <param name="newText">New place description.</param> |
| | 50 | | void SetPlaceDescription(string newText); |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// Set the place organizer in the card. |
| | 54 | | /// </summary> |
| | 55 | | /// <param name="newText">New place organizer.</param> |
| | 56 | | void SetPlaceAuthor(string newText); |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Set the the number of users in the place. |
| | 60 | | /// </summary> |
| | 61 | | /// <param name="newNumberOfUsers">Number of users.</param> |
| | 62 | | void SetNumberOfUsers(int newNumberOfUsers); |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Set the place coords. |
| | 66 | | /// </summary> |
| | 67 | | /// <param name="newCoords">Place coords.</param> |
| | 68 | | void SetCoords(Vector2Int newCoords); |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// Set the parcels contained in the place. |
| | 72 | | /// </summary> |
| | 73 | | /// <param name="parcels">List of parcels.</param> |
| | 74 | | void SetParcels(Vector2Int[] parcels); |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// Active or deactive the loading indicator. |
| | 78 | | /// </summary> |
| | 79 | | /// <param name="isVisible">True for showing the loading indicator and hiding the card info.</param> |
| | 80 | | void SetLoadingIndicatorVisible(bool isVisible); |
| | 81 | | } |
| | 82 | |
|
| | 83 | | public class PlaceCardComponentView : BaseComponentView, IPlaceCardComponentView, IComponentModelConfig<PlaceCardCompone |
| | 84 | | { |
| | 85 | | internal const int THMBL_MARKETPLACE_WIDTH = 196; |
| | 86 | | internal const int THMBL_MARKETPLACE_HEIGHT = 143; |
| | 87 | | internal const int THMBL_MARKETPLACE_SIZEFACTOR = 50; |
| | 88 | |
|
| | 89 | | [Header("Assets References")] |
| | 90 | | [SerializeField] internal FriendHeadForPlaceCardComponentView friendHeadPrefab; |
| | 91 | |
|
| | 92 | | [Header("Prefab References")] |
| | 93 | | [SerializeField] internal ImageComponentView placeImage; |
| | 94 | | [SerializeField] internal TMP_Text placeNameOnIdleText; |
| | 95 | | [SerializeField] internal TMP_Text placeNameOnFocusText; |
| | 96 | | [SerializeField] internal TMP_Text placeDescText; |
| | 97 | | [SerializeField] internal TMP_Text placeAuthorText; |
| | 98 | | [SerializeField] internal TMP_Text numberOfUsersText; |
| | 99 | | [SerializeField] internal TMP_Text coordsText; |
| | 100 | | [SerializeField] internal Button modalBackgroundButton; |
| | 101 | | [SerializeField] internal ButtonComponentView closeCardButton; |
| | 102 | | [SerializeField] internal InputAction_Trigger closeAction; |
| | 103 | | [SerializeField] internal ButtonComponentView infoButton; |
| | 104 | | [SerializeField] internal ButtonComponentView jumpinButton; |
| | 105 | | [SerializeField] internal GridContainerComponentView friendsGrid; |
| | 106 | | [SerializeField] internal GameObject imageContainer; |
| | 107 | | [SerializeField] internal GameObject placeInfoContainer; |
| | 108 | | [SerializeField] internal GameObject loadingSpinner; |
| | 109 | | [SerializeField] internal GameObject cardSelectionFrame; |
| | 110 | | [SerializeField] internal VerticalLayoutGroup contentVerticalLayout; |
| | 111 | | [SerializeField] internal VerticalLayoutGroup infoVerticalLayout; |
| | 112 | | [SerializeField] internal PlaceCardAnimatorBase cardAnimator; |
| | 113 | |
|
| | 114 | | [Header("Configuration")] |
| | 115 | | [SerializeField] internal Sprite defaultPicture; |
| | 116 | | [SerializeField] internal PlaceCardComponentModel model; |
| | 117 | |
|
| 0 | 118 | | public FriendsHandler friendsHandler { get; set; } |
| 0 | 119 | | internal MapInfoHandler mapInfoHandler { get; set; } |
| | 120 | |
|
| 420 | 121 | | internal Dictionary<string, BaseComponentView> currentFriendHeads = new Dictionary<string, BaseComponentView>(); |
| | 122 | |
|
| 0 | 123 | | public Button.ButtonClickedEvent onJumpInClick => jumpinButton?.onClick; |
| 0 | 124 | | public Button.ButtonClickedEvent onInfoClick => infoButton?.onClick; |
| | 125 | |
|
| | 126 | | internal bool thumbnailFromMarketPlaceRequested = false; |
| | 127 | |
|
| | 128 | | public override void Awake() |
| | 129 | | { |
| 402 | 130 | | base.Awake(); |
| | 131 | |
|
| 402 | 132 | | if (placeImage != null) |
| 402 | 133 | | placeImage.OnLoaded += OnPlaceImageLoaded; |
| | 134 | |
|
| 402 | 135 | | if (cardSelectionFrame != null) |
| 161 | 136 | | cardSelectionFrame.SetActive(false); |
| | 137 | |
|
| 402 | 138 | | if (closeCardButton != null) |
| 46 | 139 | | closeCardButton.onClick.AddListener(CloseModal); |
| | 140 | |
|
| 402 | 141 | | if (closeAction != null) |
| 46 | 142 | | closeAction.OnTriggered += OnCloseActionTriggered; |
| | 143 | |
|
| 402 | 144 | | if (modalBackgroundButton != null) |
| 46 | 145 | | modalBackgroundButton.onClick.AddListener(CloseModal); |
| | 146 | |
|
| 402 | 147 | | CleanFriendHeadsItems(); |
| 402 | 148 | | } |
| | 149 | |
|
| | 150 | | public void Configure(PlaceCardComponentModel newModel) |
| | 151 | | { |
| 42 | 152 | | model = newModel; |
| | 153 | |
|
| 42 | 154 | | InitializeFriendsTracker(); |
| | 155 | |
|
| 42 | 156 | | if (mapInfoHandler != null) |
| 42 | 157 | | mapInfoHandler.SetMinimapSceneInfo(model.hotSceneInfo); |
| | 158 | |
|
| 42 | 159 | | RefreshControl(); |
| 42 | 160 | | } |
| | 161 | |
|
| | 162 | | public override void RefreshControl() |
| | 163 | | { |
| 42 | 164 | | thumbnailFromMarketPlaceRequested = false; |
| | 165 | |
|
| 42 | 166 | | if (model == null) |
| 0 | 167 | | return; |
| | 168 | |
|
| 42 | 169 | | SetParcels(model.parcels); |
| | 170 | |
|
| 42 | 171 | | if (model.placePictureSprite != null) |
| 42 | 172 | | SetPlacePicture(model.placePictureSprite); |
| 0 | 173 | | else if (model.placePictureTexture != null) |
| 0 | 174 | | SetPlacePicture(model.placePictureTexture); |
| 0 | 175 | | else if (!string.IsNullOrEmpty(model.placePictureUri)) |
| 0 | 176 | | SetPlacePicture(model.placePictureUri); |
| | 177 | | else |
| 0 | 178 | | OnPlaceImageLoaded(null); |
| | 179 | |
|
| 42 | 180 | | SetPlaceName(model.placeName); |
| 42 | 181 | | SetPlaceDescription(model.placeDescription); |
| 42 | 182 | | SetPlaceAuthor(model.placeAuthor); |
| 42 | 183 | | SetNumberOfUsers(model.numberOfUsers); |
| 42 | 184 | | SetCoords(model.coords); |
| | 185 | |
|
| 42 | 186 | | RebuildCardLayouts(); |
| 42 | 187 | | } |
| | 188 | |
|
| | 189 | | public override void OnFocus() |
| | 190 | | { |
| 1 | 191 | | base.OnFocus(); |
| | 192 | |
|
| 1 | 193 | | if (cardSelectionFrame != null) |
| 1 | 194 | | cardSelectionFrame.SetActive(true); |
| | 195 | |
|
| 1 | 196 | | cardAnimator?.Focus(); |
| 1 | 197 | | } |
| | 198 | |
|
| | 199 | | public override void OnLoseFocus() |
| | 200 | | { |
| 491 | 201 | | base.OnLoseFocus(); |
| | 202 | |
|
| 491 | 203 | | if (cardSelectionFrame != null) |
| 234 | 204 | | cardSelectionFrame.SetActive(false); |
| | 205 | |
|
| 491 | 206 | | cardAnimator?.Idle(); |
| 445 | 207 | | } |
| | 208 | |
|
| | 209 | | public override void Show(bool instant = false) |
| | 210 | | { |
| 4 | 211 | | base.Show(instant); |
| | 212 | |
|
| 4 | 213 | | DataStore.i.exploreV2.isSomeModalOpen.Set(true); |
| 4 | 214 | | } |
| | 215 | |
|
| | 216 | | public override void Hide(bool instant = false) |
| | 217 | | { |
| 124 | 218 | | base.Hide(instant); |
| | 219 | |
|
| 124 | 220 | | DataStore.i.exploreV2.isSomeModalOpen.Set(false); |
| 124 | 221 | | } |
| | 222 | |
|
| | 223 | | public override void Dispose() |
| | 224 | | { |
| 622 | 225 | | base.Dispose(); |
| | 226 | |
|
| 622 | 227 | | if (placeImage != null) |
| | 228 | | { |
| 622 | 229 | | placeImage.OnLoaded -= OnPlaceImageLoaded; |
| 622 | 230 | | placeImage.Dispose(); |
| | 231 | | } |
| | 232 | |
|
| 622 | 233 | | if (closeCardButton != null) |
| 245 | 234 | | closeCardButton.onClick.RemoveAllListeners(); |
| | 235 | |
|
| 622 | 236 | | if (closeAction != null) |
| 245 | 237 | | closeAction.OnTriggered -= OnCloseActionTriggered; |
| | 238 | |
|
| 622 | 239 | | if (modalBackgroundButton != null) |
| 245 | 240 | | modalBackgroundButton.onClick.RemoveAllListeners(); |
| | 241 | |
|
| 622 | 242 | | if (friendsHandler != null) |
| | 243 | | { |
| 26 | 244 | | friendsHandler.OnFriendAddedEvent -= OnFriendAdded; |
| 26 | 245 | | friendsHandler.OnFriendRemovedEvent -= OnFriendRemoved; |
| | 246 | | } |
| | 247 | |
|
| 622 | 248 | | if (friendsGrid != null) |
| 376 | 249 | | friendsGrid.Dispose(); |
| 622 | 250 | | } |
| | 251 | |
|
| | 252 | | public void SetPlacePicture(Sprite sprite) |
| | 253 | | { |
| 43 | 254 | | if (sprite == null && defaultPicture != null) |
| 0 | 255 | | sprite = defaultPicture; |
| | 256 | |
|
| 43 | 257 | | model.placePictureSprite = sprite; |
| | 258 | |
|
| 43 | 259 | | if (placeImage == null) |
| 0 | 260 | | return; |
| | 261 | |
|
| 43 | 262 | | placeImage.SetImage(sprite); |
| 43 | 263 | | } |
| | 264 | |
|
| | 265 | | public void SetPlacePicture(Texture2D texture) |
| | 266 | | { |
| 1 | 267 | | if (texture == null && defaultPicture != null) |
| | 268 | | { |
| 0 | 269 | | SetPlacePicture(defaultPicture); |
| 0 | 270 | | return; |
| | 271 | | } |
| | 272 | |
|
| 1 | 273 | | model.placePictureTexture = texture; |
| | 274 | |
|
| 1 | 275 | | if (!Application.isPlaying) |
| 0 | 276 | | return; |
| | 277 | |
|
| 1 | 278 | | if (placeImage == null) |
| 0 | 279 | | return; |
| | 280 | |
|
| 1 | 281 | | placeImage.SetImage(texture); |
| 1 | 282 | | } |
| | 283 | |
|
| | 284 | | public void SetPlacePicture(string uri) |
| | 285 | | { |
| 1 | 286 | | if (string.IsNullOrEmpty(uri) && defaultPicture != null) |
| | 287 | | { |
| 0 | 288 | | SetPlacePicture(defaultPicture); |
| 0 | 289 | | return; |
| | 290 | | } |
| | 291 | |
|
| 1 | 292 | | model.placePictureUri = uri; |
| | 293 | |
|
| 1 | 294 | | if (!Application.isPlaying) |
| 0 | 295 | | return; |
| | 296 | |
|
| 1 | 297 | | if (placeImage == null) |
| 0 | 298 | | return; |
| | 299 | |
|
| 1 | 300 | | placeImage.SetImage(uri); |
| 1 | 301 | | } |
| | 302 | |
|
| | 303 | | public void SetPlaceName(string newText) |
| | 304 | | { |
| 43 | 305 | | model.placeName = newText; |
| | 306 | |
|
| 43 | 307 | | if (placeNameOnIdleText != null) |
| 43 | 308 | | placeNameOnIdleText.text = newText; |
| | 309 | |
|
| 43 | 310 | | if (placeNameOnFocusText != null) |
| 38 | 311 | | placeNameOnFocusText.text = newText; |
| 43 | 312 | | } |
| | 313 | |
|
| | 314 | | public void SetPlaceDescription(string newText) |
| | 315 | | { |
| 43 | 316 | | model.placeDescription = newText; |
| | 317 | |
|
| 43 | 318 | | if (placeDescText == null) |
| 37 | 319 | | return; |
| | 320 | |
|
| 6 | 321 | | placeDescText.text = newText; |
| 6 | 322 | | } |
| | 323 | |
|
| | 324 | | public void SetPlaceAuthor(string newText) |
| | 325 | | { |
| 43 | 326 | | model.placeAuthor = newText; |
| | 327 | |
|
| 43 | 328 | | if (placeAuthorText == null) |
| 39 | 329 | | return; |
| | 330 | |
|
| 4 | 331 | | placeAuthorText.text = newText; |
| 4 | 332 | | } |
| | 333 | |
|
| | 334 | | public void SetNumberOfUsers(int newNumberOfUsers) |
| | 335 | | { |
| 43 | 336 | | model.numberOfUsers = newNumberOfUsers; |
| | 337 | |
|
| 43 | 338 | | if (numberOfUsersText == null) |
| 3 | 339 | | return; |
| | 340 | |
|
| 40 | 341 | | numberOfUsersText.text = newNumberOfUsers.ToString(); |
| 40 | 342 | | } |
| | 343 | |
|
| | 344 | | public void SetCoords(Vector2Int newCoords) |
| | 345 | | { |
| 43 | 346 | | model.coords = newCoords; |
| | 347 | |
|
| 43 | 348 | | if (coordsText == null) |
| 39 | 349 | | return; |
| | 350 | |
|
| 4 | 351 | | coordsText.text = $"{newCoords.x},{newCoords.y}"; |
| 4 | 352 | | } |
| | 353 | |
|
| 0 | 354 | | public void SetParcels(Vector2Int[] parcels) { model.parcels = parcels; } |
| | 355 | |
|
| | 356 | | public void SetLoadingIndicatorVisible(bool isVisible) |
| | 357 | | { |
| 2 | 358 | | imageContainer.SetActive(!isVisible); |
| 2 | 359 | | placeInfoContainer.SetActive(!isVisible); |
| 2 | 360 | | loadingSpinner.SetActive(isVisible); |
| 2 | 361 | | } |
| | 362 | |
|
| | 363 | | internal void OnPlaceImageLoaded(Sprite sprite) |
| | 364 | | { |
| 0 | 365 | | if (sprite != null) |
| 0 | 366 | | return; |
| | 367 | |
|
| 0 | 368 | | if (!thumbnailFromMarketPlaceRequested) |
| | 369 | | { |
| 0 | 370 | | thumbnailFromMarketPlaceRequested = true; |
| 0 | 371 | | SetPlacePicture(MapUtils.GetMarketPlaceThumbnailUrl(model.parcels, THMBL_MARKETPLACE_WIDTH, THMBL_MARKETPLAC |
| 0 | 372 | | } |
| | 373 | | else |
| | 374 | | { |
| 0 | 375 | | SetPlacePicture(sprite: null); |
| | 376 | | } |
| 0 | 377 | | } |
| | 378 | |
|
| | 379 | | internal void InitializeFriendsTracker() |
| | 380 | | { |
| 43 | 381 | | CleanFriendHeadsItems(); |
| | 382 | |
|
| 43 | 383 | | if (mapInfoHandler == null) |
| 19 | 384 | | mapInfoHandler = new MapInfoHandler(); |
| | 385 | |
|
| 43 | 386 | | if (friendsHandler == null) |
| | 387 | | { |
| 19 | 388 | | friendsHandler = new FriendsHandler(mapInfoHandler); |
| 19 | 389 | | friendsHandler.OnFriendAddedEvent += OnFriendAdded; |
| 19 | 390 | | friendsHandler.OnFriendRemovedEvent += OnFriendRemoved; |
| | 391 | | } |
| 43 | 392 | | } |
| | 393 | |
|
| | 394 | | internal void OnFriendAdded(UserProfile profile, Color backgroundColor) |
| | 395 | | { |
| 3 | 396 | | if (currentFriendHeads.ContainsKey(profile.userId)) |
| 0 | 397 | | return; |
| | 398 | |
|
| 3 | 399 | | BaseComponentView newFriend = InstantiateAndConfigureFriendHead( |
| | 400 | | new FriendHeadForPlaceCardComponentModel |
| | 401 | | { |
| | 402 | | userProfile = profile, |
| | 403 | | backgroundColor = backgroundColor |
| | 404 | | }, |
| | 405 | | friendHeadPrefab); |
| | 406 | |
|
| 3 | 407 | | if (friendsGrid != null) |
| 3 | 408 | | friendsGrid.AddItemWithResize(newFriend); |
| | 409 | |
|
| 3 | 410 | | currentFriendHeads.Add(profile.userId, newFriend); |
| 3 | 411 | | } |
| | 412 | |
|
| | 413 | | internal void OnFriendRemoved(UserProfile profile) |
| | 414 | | { |
| 1 | 415 | | if (!currentFriendHeads.ContainsKey(profile.userId)) |
| 0 | 416 | | return; |
| | 417 | |
|
| 1 | 418 | | if (friendsGrid != null) |
| 1 | 419 | | friendsGrid.RemoveItem(currentFriendHeads[profile.userId]); |
| | 420 | |
|
| 1 | 421 | | currentFriendHeads.Remove(profile.userId); |
| 1 | 422 | | } |
| | 423 | |
|
| | 424 | | internal void CleanFriendHeadsItems() |
| | 425 | | { |
| 446 | 426 | | if (friendsGrid != null) |
| | 427 | | { |
| 397 | 428 | | friendsGrid.RemoveItems(); |
| 397 | 429 | | currentFriendHeads.Clear(); |
| | 430 | | } |
| 446 | 431 | | } |
| | 432 | |
|
| | 433 | | internal BaseComponentView InstantiateAndConfigureFriendHead(FriendHeadForPlaceCardComponentModel friendInfo, Friend |
| | 434 | | { |
| 4 | 435 | | FriendHeadForPlaceCardComponentView friendHeadGO = GameObject.Instantiate(prefabToUse); |
| 4 | 436 | | friendHeadGO.Configure(friendInfo); |
| | 437 | |
|
| 4 | 438 | | return friendHeadGO; |
| | 439 | | } |
| | 440 | |
|
| | 441 | | internal void RebuildCardLayouts() |
| | 442 | | { |
| 42 | 443 | | if (contentVerticalLayout != null) |
| 3 | 444 | | Utils.ForceRebuildLayoutImmediate(contentVerticalLayout.transform as RectTransform); |
| | 445 | |
|
| 42 | 446 | | if (infoVerticalLayout != null) |
| 3 | 447 | | Utils.ForceRebuildLayoutImmediate(infoVerticalLayout.transform as RectTransform); |
| 42 | 448 | | } |
| | 449 | |
|
| 4 | 450 | | internal void CloseModal() { Hide(); } |
| | 451 | |
|
| 2 | 452 | | internal void OnCloseActionTriggered(DCLAction_Trigger action) { CloseModal(); } |
| | 453 | | } |