| | 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 |
| | 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 | |
|
| 1 | 89 | | internal static readonly int ON_FOCUS_CARD_COMPONENT_BOOL = Animator.StringToHash("OnFocus"); |
| | 90 | |
|
| | 91 | | [Header("Assets References")] |
| | 92 | | [SerializeField] internal FriendHeadForPlaceCardComponentView friendHeadPrefab; |
| | 93 | |
|
| | 94 | | [Header("Prefab References")] |
| | 95 | | [SerializeField] internal ImageComponentView placeImage; |
| | 96 | | [SerializeField] internal TMP_Text placeNameOnIdleText; |
| | 97 | | [SerializeField] internal TMP_Text placeNameOnFocusText; |
| | 98 | | [SerializeField] internal TMP_Text placeDescText; |
| | 99 | | [SerializeField] internal TMP_Text placeAuthorText; |
| | 100 | | [SerializeField] internal TMP_Text numberOfUsersText; |
| | 101 | | [SerializeField] internal TMP_Text coordsText; |
| | 102 | | [SerializeField] internal Button modalBackgroundButton; |
| | 103 | | [SerializeField] internal ButtonComponentView closeCardButton; |
| | 104 | | [SerializeField] internal InputAction_Trigger closeAction; |
| | 105 | | [SerializeField] internal ButtonComponentView infoButton; |
| | 106 | | [SerializeField] internal ButtonComponentView jumpinButton; |
| | 107 | | [SerializeField] internal GridContainerComponentView friendsGrid; |
| | 108 | | [SerializeField] internal GameObject imageContainer; |
| | 109 | | [SerializeField] internal GameObject placeInfoContainer; |
| | 110 | | [SerializeField] internal GameObject loadingSpinner; |
| | 111 | | [SerializeField] internal Animator cardAnimator; |
| | 112 | | [SerializeField] internal GameObject cardSelectionFrame; |
| | 113 | | [SerializeField] internal VerticalLayoutGroup contentVerticalLayout; |
| | 114 | | [SerializeField] internal VerticalLayoutGroup infoVerticalLayout; |
| | 115 | |
|
| | 116 | | [Header("Configuration")] |
| | 117 | | [SerializeField] internal Sprite defaultPicture; |
| | 118 | | [SerializeField] internal PlaceCardComponentModel model; |
| | 119 | |
|
| 0 | 120 | | public FriendsHandler friendsHandler { get; set; } |
| 0 | 121 | | internal MapInfoHandler mapInfoHandler { get; set; } |
| | 122 | |
|
| 401 | 123 | | internal Dictionary<string, BaseComponentView> currentFriendHeads = new Dictionary<string, BaseComponentView>(); |
| | 124 | |
|
| 0 | 125 | | public Button.ButtonClickedEvent onJumpInClick => jumpinButton?.onClick; |
| 0 | 126 | | public Button.ButtonClickedEvent onInfoClick => infoButton?.onClick; |
| | 127 | |
|
| | 128 | | internal bool thumbnailFromMarketPlaceRequested = false; |
| | 129 | |
|
| | 130 | | public override void Awake() |
| | 131 | | { |
| 178 | 132 | | base.Awake(); |
| | 133 | |
|
| 178 | 134 | | if (placeImage != null) |
| 178 | 135 | | placeImage.OnLoaded += OnPlaceImageLoaded; |
| | 136 | |
|
| 178 | 137 | | if (cardSelectionFrame != null) |
| 66 | 138 | | cardSelectionFrame.SetActive(false); |
| | 139 | |
|
| 178 | 140 | | if (closeCardButton != null) |
| 34 | 141 | | closeCardButton.onClick.AddListener(CloseModal); |
| | 142 | |
|
| 178 | 143 | | if (closeAction != null) |
| 34 | 144 | | closeAction.OnTriggered += OnCloseActionTriggered; |
| | 145 | |
|
| 178 | 146 | | if (modalBackgroundButton != null) |
| 34 | 147 | | modalBackgroundButton.onClick.AddListener(CloseModal); |
| | 148 | |
|
| 178 | 149 | | CleanFriendHeadsItems(); |
| 178 | 150 | | } |
| | 151 | |
|
| | 152 | | public void Configure(BaseComponentModel newModel) |
| | 153 | | { |
| 12 | 154 | | model = (PlaceCardComponentModel)newModel; |
| | 155 | |
|
| 12 | 156 | | InitializeFriendsTracker(); |
| | 157 | |
|
| 12 | 158 | | if (mapInfoHandler != null) |
| 12 | 159 | | mapInfoHandler.SetMinimapSceneInfo(model.hotSceneInfo); |
| | 160 | |
|
| 12 | 161 | | RefreshControl(); |
| 12 | 162 | | } |
| | 163 | |
|
| | 164 | | public override void RefreshControl() |
| | 165 | | { |
| 12 | 166 | | thumbnailFromMarketPlaceRequested = false; |
| | 167 | |
|
| 12 | 168 | | if (model == null) |
| 0 | 169 | | return; |
| | 170 | |
|
| 12 | 171 | | SetParcels(model.parcels); |
| | 172 | |
|
| 12 | 173 | | if (model.placePictureSprite != null) |
| 12 | 174 | | SetPlacePicture(model.placePictureSprite); |
| 0 | 175 | | else if (model.placePictureTexture != null) |
| 0 | 176 | | SetPlacePicture(model.placePictureTexture); |
| 0 | 177 | | else if (!string.IsNullOrEmpty(model.placePictureUri)) |
| 0 | 178 | | SetPlacePicture(model.placePictureUri); |
| | 179 | | else |
| 0 | 180 | | OnPlaceImageLoaded(null); |
| | 181 | |
|
| 12 | 182 | | SetPlaceName(model.placeName); |
| 12 | 183 | | SetPlaceDescription(model.placeDescription); |
| 12 | 184 | | SetPlaceAuthor(model.placeAuthor); |
| 12 | 185 | | SetNumberOfUsers(model.numberOfUsers); |
| 12 | 186 | | SetCoords(model.coords); |
| | 187 | |
|
| 12 | 188 | | RebuildCardLayouts(); |
| 12 | 189 | | } |
| | 190 | |
|
| | 191 | | public override void OnFocus() |
| | 192 | | { |
| 1 | 193 | | base.OnFocus(); |
| | 194 | |
|
| 1 | 195 | | if (cardSelectionFrame != null) |
| 1 | 196 | | cardSelectionFrame.SetActive(true); |
| | 197 | |
|
| 1 | 198 | | if (cardAnimator != null) |
| 1 | 199 | | cardAnimator.SetBool(ON_FOCUS_CARD_COMPONENT_BOOL, true); |
| 1 | 200 | | } |
| | 201 | |
|
| | 202 | | public override void OnLoseFocus() |
| | 203 | | { |
| 224 | 204 | | base.OnLoseFocus(); |
| | 205 | |
|
| 224 | 206 | | if (cardSelectionFrame != null) |
| 77 | 207 | | cardSelectionFrame.SetActive(false); |
| | 208 | |
|
| 224 | 209 | | if (cardAnimator != null) |
| 190 | 210 | | cardAnimator.SetBool(ON_FOCUS_CARD_COMPONENT_BOOL, false); |
| 224 | 211 | | } |
| | 212 | |
|
| | 213 | | public override void Show(bool instant = false) |
| | 214 | | { |
| 4 | 215 | | base.Show(instant); |
| | 216 | |
|
| 4 | 217 | | DataStore.i.exploreV2.isSomeModalOpen.Set(true); |
| 4 | 218 | | } |
| | 219 | |
|
| | 220 | | public override void Hide(bool instant = false) |
| | 221 | | { |
| 29 | 222 | | base.Hide(instant); |
| | 223 | |
|
| 29 | 224 | | DataStore.i.exploreV2.isSomeModalOpen.Set(false); |
| 29 | 225 | | } |
| | 226 | |
|
| | 227 | | public override void Dispose() |
| | 228 | | { |
| 258 | 229 | | base.Dispose(); |
| | 230 | |
|
| 258 | 231 | | if (placeImage != null) |
| | 232 | | { |
| 258 | 233 | | placeImage.OnLoaded -= OnPlaceImageLoaded; |
| 258 | 234 | | placeImage.Dispose(); |
| | 235 | | } |
| | 236 | |
|
| 258 | 237 | | if (closeCardButton != null) |
| 92 | 238 | | closeCardButton.onClick.RemoveAllListeners(); |
| | 239 | |
|
| 258 | 240 | | if (closeAction != null) |
| 92 | 241 | | closeAction.OnTriggered -= OnCloseActionTriggered; |
| | 242 | |
|
| 258 | 243 | | if (modalBackgroundButton != null) |
| 92 | 244 | | modalBackgroundButton.onClick.RemoveAllListeners(); |
| | 245 | |
|
| 258 | 246 | | if (friendsHandler != null) |
| | 247 | | { |
| 18 | 248 | | friendsHandler.OnFriendAddedEvent -= OnFriendAdded; |
| 18 | 249 | | friendsHandler.OnFriendRemovedEvent -= OnFriendRemoved; |
| | 250 | | } |
| | 251 | |
|
| 258 | 252 | | if (friendsGrid != null) |
| 165 | 253 | | friendsGrid.Dispose(); |
| 258 | 254 | | } |
| | 255 | |
|
| | 256 | | public void SetPlacePicture(Sprite sprite) |
| | 257 | | { |
| 14 | 258 | | if (sprite == null && defaultPicture != null) |
| 0 | 259 | | sprite = defaultPicture; |
| | 260 | |
|
| 14 | 261 | | model.placePictureSprite = sprite; |
| | 262 | |
|
| 14 | 263 | | if (placeImage == null) |
| 0 | 264 | | return; |
| | 265 | |
|
| 14 | 266 | | placeImage.SetImage(sprite); |
| 14 | 267 | | } |
| | 268 | |
|
| | 269 | | public void SetPlacePicture(Texture2D texture) |
| | 270 | | { |
| 1 | 271 | | if (texture == null && defaultPicture != null) |
| | 272 | | { |
| 0 | 273 | | SetPlacePicture(defaultPicture); |
| 0 | 274 | | return; |
| | 275 | | } |
| | 276 | |
|
| 1 | 277 | | model.placePictureTexture = texture; |
| | 278 | |
|
| 1 | 279 | | if (!Application.isPlaying) |
| 0 | 280 | | return; |
| | 281 | |
|
| 1 | 282 | | if (placeImage == null) |
| 0 | 283 | | return; |
| | 284 | |
|
| 1 | 285 | | placeImage.SetImage(texture); |
| 1 | 286 | | } |
| | 287 | |
|
| | 288 | | public void SetPlacePicture(string uri) |
| | 289 | | { |
| 1 | 290 | | if (string.IsNullOrEmpty(uri) && defaultPicture != null) |
| | 291 | | { |
| 0 | 292 | | SetPlacePicture(defaultPicture); |
| 0 | 293 | | return; |
| | 294 | | } |
| | 295 | |
|
| 1 | 296 | | model.placePictureUri = uri; |
| | 297 | |
|
| 1 | 298 | | if (!Application.isPlaying) |
| 0 | 299 | | return; |
| | 300 | |
|
| 1 | 301 | | if (placeImage == null) |
| 0 | 302 | | return; |
| | 303 | |
|
| 1 | 304 | | placeImage.SetImage(uri); |
| 1 | 305 | | } |
| | 306 | |
|
| | 307 | | public void SetPlaceName(string newText) |
| | 308 | | { |
| 13 | 309 | | model.placeName = newText; |
| | 310 | |
|
| 13 | 311 | | if (placeNameOnIdleText != null) |
| 13 | 312 | | placeNameOnIdleText.text = newText; |
| | 313 | |
|
| 13 | 314 | | if (placeNameOnFocusText != null) |
| 8 | 315 | | placeNameOnFocusText.text = newText; |
| 13 | 316 | | } |
| | 317 | |
|
| | 318 | | public void SetPlaceDescription(string newText) |
| | 319 | | { |
| 13 | 320 | | model.placeDescription = newText; |
| | 321 | |
|
| 13 | 322 | | if (placeDescText == null) |
| 7 | 323 | | return; |
| | 324 | |
|
| 6 | 325 | | placeDescText.text = newText; |
| 6 | 326 | | } |
| | 327 | |
|
| | 328 | | public void SetPlaceAuthor(string newText) |
| | 329 | | { |
| 13 | 330 | | model.placeAuthor = newText; |
| | 331 | |
|
| 13 | 332 | | if (placeAuthorText == null) |
| 9 | 333 | | return; |
| | 334 | |
|
| 4 | 335 | | placeAuthorText.text = newText; |
| 4 | 336 | | } |
| | 337 | |
|
| | 338 | | public void SetNumberOfUsers(int newNumberOfUsers) |
| | 339 | | { |
| 13 | 340 | | model.numberOfUsers = newNumberOfUsers; |
| | 341 | |
|
| 13 | 342 | | if (numberOfUsersText == null) |
| 3 | 343 | | return; |
| | 344 | |
|
| 10 | 345 | | numberOfUsersText.text = newNumberOfUsers.ToString(); |
| 10 | 346 | | } |
| | 347 | |
|
| | 348 | | public void SetCoords(Vector2Int newCoords) |
| | 349 | | { |
| 13 | 350 | | model.coords = newCoords; |
| | 351 | |
|
| 13 | 352 | | if (coordsText == null) |
| 9 | 353 | | return; |
| | 354 | |
|
| 4 | 355 | | coordsText.text = $"{newCoords.x},{newCoords.y}"; |
| 4 | 356 | | } |
| | 357 | |
|
| 0 | 358 | | public void SetParcels(Vector2Int[] parcels) { model.parcels = parcels; } |
| | 359 | |
|
| | 360 | | public void SetLoadingIndicatorVisible(bool isVisible) |
| | 361 | | { |
| 2 | 362 | | imageContainer.SetActive(!isVisible); |
| 2 | 363 | | placeInfoContainer.SetActive(!isVisible); |
| 2 | 364 | | loadingSpinner.SetActive(isVisible); |
| 2 | 365 | | } |
| | 366 | |
|
| | 367 | | internal void OnPlaceImageLoaded(Sprite sprite) |
| | 368 | | { |
| 1 | 369 | | if (sprite != null) |
| | 370 | | { |
| 1 | 371 | | SetPlacePicture(sprite); |
| 1 | 372 | | } |
| 0 | 373 | | else if (!thumbnailFromMarketPlaceRequested) |
| | 374 | | { |
| 0 | 375 | | thumbnailFromMarketPlaceRequested = true; |
| 0 | 376 | | SetPlacePicture(MapUtils.GetMarketPlaceThumbnailUrl(model.parcels, THMBL_MARKETPLACE_WIDTH, THMBL_MARKETPLAC |
| 0 | 377 | | } |
| | 378 | | else |
| | 379 | | { |
| 0 | 380 | | SetPlacePicture(sprite: null); |
| | 381 | | } |
| 0 | 382 | | } |
| | 383 | |
|
| | 384 | | internal void InitializeFriendsTracker() |
| | 385 | | { |
| 13 | 386 | | CleanFriendHeadsItems(); |
| | 387 | |
|
| 13 | 388 | | if (mapInfoHandler == null) |
| 11 | 389 | | mapInfoHandler = new MapInfoHandler(); |
| | 390 | |
|
| 13 | 391 | | if (friendsHandler == null) |
| | 392 | | { |
| 11 | 393 | | friendsHandler = new FriendsHandler(mapInfoHandler); |
| 11 | 394 | | friendsHandler.OnFriendAddedEvent += OnFriendAdded; |
| 11 | 395 | | friendsHandler.OnFriendRemovedEvent += OnFriendRemoved; |
| | 396 | | } |
| 13 | 397 | | } |
| | 398 | |
|
| | 399 | | internal void OnFriendAdded(UserProfile profile, Color backgroundColor) |
| | 400 | | { |
| 3 | 401 | | if (currentFriendHeads.ContainsKey(profile.userId)) |
| 0 | 402 | | return; |
| | 403 | |
|
| 3 | 404 | | BaseComponentView newFriend = InstantiateAndConfigureFriendHead( |
| | 405 | | new FriendHeadForPlaceCardComponentModel |
| | 406 | | { |
| | 407 | | userProfile = profile, |
| | 408 | | backgroundColor = backgroundColor |
| | 409 | | }, |
| | 410 | | friendHeadPrefab); |
| | 411 | |
|
| 3 | 412 | | if (friendsGrid != null) |
| 3 | 413 | | friendsGrid.AddItem(newFriend); |
| | 414 | |
|
| 3 | 415 | | currentFriendHeads.Add(profile.userId, newFriend); |
| 3 | 416 | | } |
| | 417 | |
|
| | 418 | | internal void OnFriendRemoved(UserProfile profile) |
| | 419 | | { |
| 1 | 420 | | if (!currentFriendHeads.ContainsKey(profile.userId)) |
| 0 | 421 | | return; |
| | 422 | |
|
| 1 | 423 | | if (friendsGrid != null) |
| 1 | 424 | | friendsGrid.RemoveItem(currentFriendHeads[profile.userId]); |
| | 425 | |
|
| 1 | 426 | | currentFriendHeads.Remove(profile.userId); |
| 1 | 427 | | } |
| | 428 | |
|
| | 429 | | internal void CleanFriendHeadsItems() |
| | 430 | | { |
| 192 | 431 | | if (friendsGrid != null) |
| | 432 | | { |
| 155 | 433 | | friendsGrid.RemoveItems(); |
| 155 | 434 | | currentFriendHeads.Clear(); |
| | 435 | | } |
| 192 | 436 | | } |
| | 437 | |
|
| | 438 | | internal BaseComponentView InstantiateAndConfigureFriendHead(FriendHeadForPlaceCardComponentModel friendInfo, Friend |
| | 439 | | { |
| 4 | 440 | | FriendHeadForPlaceCardComponentView friendHeadGO = GameObject.Instantiate(prefabToUse); |
| 4 | 441 | | friendHeadGO.Configure(friendInfo); |
| | 442 | |
|
| 4 | 443 | | return friendHeadGO; |
| | 444 | | } |
| | 445 | |
|
| | 446 | | internal void RebuildCardLayouts() |
| | 447 | | { |
| 12 | 448 | | if (contentVerticalLayout != null) |
| 3 | 449 | | Utils.ForceRebuildLayoutImmediate(contentVerticalLayout.transform as RectTransform); |
| | 450 | |
|
| 12 | 451 | | if (infoVerticalLayout != null) |
| 3 | 452 | | Utils.ForceRebuildLayoutImmediate(infoVerticalLayout.transform as RectTransform); |
| 12 | 453 | | } |
| | 454 | |
|
| 4 | 455 | | internal void CloseModal() { Hide(); } |
| | 456 | |
|
| 2 | 457 | | internal void OnCloseActionTriggered(DCLAction_Trigger action) { CloseModal(); } |
| | 458 | | } |