| | 1 | | using DCL.Helpers; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 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 | |
|
| 396 | 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 | | { |
| 326 | 132 | | base.Awake(); |
| | 133 | |
|
| 326 | 134 | | if (placeImage != null) |
| 326 | 135 | | placeImage.OnLoaded += OnPlaceImageLoaded; |
| | 136 | |
|
| 326 | 137 | | if (cardSelectionFrame != null) |
| 103 | 138 | | cardSelectionFrame.SetActive(false); |
| | 139 | |
|
| 326 | 140 | | if (closeCardButton != null) |
| 34 | 141 | | closeCardButton.onClick.AddListener(CloseModal); |
| | 142 | |
|
| 326 | 143 | | if (closeAction != null) |
| 34 | 144 | | closeAction.OnTriggered += OnCloseActionTriggered; |
| | 145 | |
|
| 326 | 146 | | if (modalBackgroundButton != null) |
| 34 | 147 | | modalBackgroundButton.onClick.AddListener(CloseModal); |
| | 148 | |
|
| 326 | 149 | | CleanFriendHeadsItems(); |
| 326 | 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 | | { |
| 1 | 204 | | base.OnLoseFocus(); |
| | 205 | |
|
| 1 | 206 | | if (cardSelectionFrame != null) |
| 1 | 207 | | cardSelectionFrame.SetActive(false); |
| | 208 | |
|
| 1 | 209 | | if (cardAnimator != null) |
| 1 | 210 | | cardAnimator.SetBool(ON_FOCUS_CARD_COMPONENT_BOOL, false); |
| 1 | 211 | | } |
| | 212 | |
|
| | 213 | | public override void Dispose() |
| | 214 | | { |
| 406 | 215 | | base.Dispose(); |
| | 216 | |
|
| 406 | 217 | | if (placeImage != null) |
| | 218 | | { |
| 406 | 219 | | placeImage.OnLoaded -= OnPlaceImageLoaded; |
| 406 | 220 | | placeImage.Dispose(); |
| | 221 | | } |
| | 222 | |
|
| 406 | 223 | | if (closeCardButton != null) |
| 92 | 224 | | closeCardButton.onClick.RemoveAllListeners(); |
| | 225 | |
|
| 406 | 226 | | if (closeAction != null) |
| 92 | 227 | | closeAction.OnTriggered -= OnCloseActionTriggered; |
| | 228 | |
|
| 406 | 229 | | if (modalBackgroundButton != null) |
| 92 | 230 | | modalBackgroundButton.onClick.RemoveAllListeners(); |
| | 231 | |
|
| 406 | 232 | | if (friendsHandler != null) |
| | 233 | | { |
| 18 | 234 | | friendsHandler.OnFriendAddedEvent -= OnFriendAdded; |
| 18 | 235 | | friendsHandler.OnFriendRemovedEvent -= OnFriendRemoved; |
| | 236 | | } |
| | 237 | |
|
| 406 | 238 | | if (friendsGrid != null) |
| 313 | 239 | | friendsGrid.Dispose(); |
| 406 | 240 | | } |
| | 241 | |
|
| | 242 | | public void SetPlacePicture(Sprite sprite) |
| | 243 | | { |
| 14 | 244 | | if (sprite == null && defaultPicture != null) |
| 0 | 245 | | sprite = defaultPicture; |
| | 246 | |
|
| 14 | 247 | | model.placePictureSprite = sprite; |
| | 248 | |
|
| 14 | 249 | | if (placeImage == null) |
| 0 | 250 | | return; |
| | 251 | |
|
| 14 | 252 | | placeImage.SetImage(sprite); |
| 14 | 253 | | } |
| | 254 | |
|
| | 255 | | public void SetPlacePicture(Texture2D texture) |
| | 256 | | { |
| 1 | 257 | | if (texture == null && defaultPicture != null) |
| | 258 | | { |
| 0 | 259 | | SetPlacePicture(defaultPicture); |
| 0 | 260 | | return; |
| | 261 | | } |
| | 262 | |
|
| 1 | 263 | | model.placePictureTexture = texture; |
| | 264 | |
|
| 1 | 265 | | if (!Application.isPlaying) |
| 0 | 266 | | return; |
| | 267 | |
|
| 1 | 268 | | if (placeImage == null) |
| 0 | 269 | | return; |
| | 270 | |
|
| 1 | 271 | | placeImage.SetImage(texture); |
| 1 | 272 | | } |
| | 273 | |
|
| | 274 | | public void SetPlacePicture(string uri) |
| | 275 | | { |
| 1 | 276 | | if (string.IsNullOrEmpty(uri) && defaultPicture != null) |
| | 277 | | { |
| 0 | 278 | | SetPlacePicture(defaultPicture); |
| 0 | 279 | | return; |
| | 280 | | } |
| | 281 | |
|
| 1 | 282 | | model.placePictureUri = uri; |
| | 283 | |
|
| 1 | 284 | | if (!Application.isPlaying) |
| 0 | 285 | | return; |
| | 286 | |
|
| 1 | 287 | | if (placeImage == null) |
| 0 | 288 | | return; |
| | 289 | |
|
| 1 | 290 | | placeImage.SetImage(uri); |
| 1 | 291 | | } |
| | 292 | |
|
| | 293 | | public void SetPlaceName(string newText) |
| | 294 | | { |
| 13 | 295 | | model.placeName = newText; |
| | 296 | |
|
| 13 | 297 | | if (placeNameOnIdleText != null) |
| 13 | 298 | | placeNameOnIdleText.text = newText; |
| | 299 | |
|
| 13 | 300 | | if (placeNameOnFocusText != null) |
| 8 | 301 | | placeNameOnFocusText.text = newText; |
| 13 | 302 | | } |
| | 303 | |
|
| | 304 | | public void SetPlaceDescription(string newText) |
| | 305 | | { |
| 13 | 306 | | model.placeDescription = newText; |
| | 307 | |
|
| 13 | 308 | | if (placeDescText == null) |
| 7 | 309 | | return; |
| | 310 | |
|
| 6 | 311 | | placeDescText.text = newText; |
| 6 | 312 | | } |
| | 313 | |
|
| | 314 | | public void SetPlaceAuthor(string newText) |
| | 315 | | { |
| 13 | 316 | | model.placeAuthor = newText; |
| | 317 | |
|
| 13 | 318 | | if (placeAuthorText == null) |
| 9 | 319 | | return; |
| | 320 | |
|
| 4 | 321 | | placeAuthorText.text = newText; |
| 4 | 322 | | } |
| | 323 | |
|
| | 324 | | public void SetNumberOfUsers(int newNumberOfUsers) |
| | 325 | | { |
| 13 | 326 | | model.numberOfUsers = newNumberOfUsers; |
| | 327 | |
|
| 13 | 328 | | if (numberOfUsersText == null) |
| 3 | 329 | | return; |
| | 330 | |
|
| 10 | 331 | | numberOfUsersText.text = newNumberOfUsers.ToString(); |
| 10 | 332 | | } |
| | 333 | |
|
| | 334 | | public void SetCoords(Vector2Int newCoords) |
| | 335 | | { |
| 13 | 336 | | model.coords = newCoords; |
| | 337 | |
|
| 13 | 338 | | if (coordsText == null) |
| 9 | 339 | | return; |
| | 340 | |
|
| 4 | 341 | | coordsText.text = $"{newCoords.x},{newCoords.y}"; |
| 4 | 342 | | } |
| | 343 | |
|
| 0 | 344 | | public void SetParcels(Vector2Int[] parcels) { model.parcels = parcels; } |
| | 345 | |
|
| | 346 | | public void SetLoadingIndicatorVisible(bool isVisible) |
| | 347 | | { |
| 2 | 348 | | imageContainer.SetActive(!isVisible); |
| 2 | 349 | | placeInfoContainer.SetActive(!isVisible); |
| 2 | 350 | | loadingSpinner.SetActive(isVisible); |
| 2 | 351 | | } |
| | 352 | |
|
| | 353 | | internal void OnPlaceImageLoaded(Sprite sprite) |
| | 354 | | { |
| 1 | 355 | | if (sprite != null) |
| | 356 | | { |
| 1 | 357 | | SetPlacePicture(sprite); |
| 1 | 358 | | } |
| 0 | 359 | | else if (!thumbnailFromMarketPlaceRequested) |
| | 360 | | { |
| 0 | 361 | | thumbnailFromMarketPlaceRequested = true; |
| 0 | 362 | | SetPlacePicture(MapUtils.GetMarketPlaceThumbnailUrl(model.parcels, THMBL_MARKETPLACE_WIDTH, THMBL_MARKETPLAC |
| 0 | 363 | | } |
| | 364 | | else |
| | 365 | | { |
| 0 | 366 | | SetPlacePicture(sprite: null); |
| | 367 | | } |
| 0 | 368 | | } |
| | 369 | |
|
| | 370 | | internal void InitializeFriendsTracker() |
| | 371 | | { |
| 13 | 372 | | CleanFriendHeadsItems(); |
| | 373 | |
|
| 13 | 374 | | if (mapInfoHandler == null) |
| 11 | 375 | | mapInfoHandler = new MapInfoHandler(); |
| | 376 | |
|
| 13 | 377 | | if (friendsHandler == null) |
| | 378 | | { |
| 11 | 379 | | friendsHandler = new FriendsHandler(mapInfoHandler); |
| 11 | 380 | | friendsHandler.OnFriendAddedEvent += OnFriendAdded; |
| 11 | 381 | | friendsHandler.OnFriendRemovedEvent += OnFriendRemoved; |
| | 382 | | } |
| 13 | 383 | | } |
| | 384 | |
|
| | 385 | | internal void OnFriendAdded(UserProfile profile, Color backgroundColor) |
| | 386 | | { |
| 3 | 387 | | if (currentFriendHeads.ContainsKey(profile.userId)) |
| 0 | 388 | | return; |
| | 389 | |
|
| 3 | 390 | | BaseComponentView newFriend = InstantiateAndConfigureFriendHead( |
| | 391 | | new FriendHeadForPlaceCardComponentModel |
| | 392 | | { |
| | 393 | | userProfile = profile, |
| | 394 | | backgroundColor = backgroundColor |
| | 395 | | }, |
| | 396 | | friendHeadPrefab); |
| | 397 | |
|
| 3 | 398 | | if (friendsGrid != null) |
| 3 | 399 | | friendsGrid.AddItem(newFriend); |
| | 400 | |
|
| 3 | 401 | | currentFriendHeads.Add(profile.userId, newFriend); |
| 3 | 402 | | } |
| | 403 | |
|
| | 404 | | internal void OnFriendRemoved(UserProfile profile) |
| | 405 | | { |
| 1 | 406 | | if (!currentFriendHeads.ContainsKey(profile.userId)) |
| 0 | 407 | | return; |
| | 408 | |
|
| 1 | 409 | | if (friendsGrid != null) |
| 1 | 410 | | friendsGrid.RemoveItem(currentFriendHeads[profile.userId]); |
| | 411 | |
|
| 1 | 412 | | currentFriendHeads.Remove(profile.userId); |
| 1 | 413 | | } |
| | 414 | |
|
| | 415 | | internal void CleanFriendHeadsItems() |
| | 416 | | { |
| 340 | 417 | | if (friendsGrid != null) |
| | 418 | | { |
| 303 | 419 | | friendsGrid.RemoveItems(); |
| 303 | 420 | | currentFriendHeads.Clear(); |
| | 421 | | } |
| 340 | 422 | | } |
| | 423 | |
|
| | 424 | | internal BaseComponentView InstantiateAndConfigureFriendHead(FriendHeadForPlaceCardComponentModel friendInfo, Friend |
| | 425 | | { |
| 4 | 426 | | FriendHeadForPlaceCardComponentView friendHeadGO = GameObject.Instantiate(prefabToUse); |
| 4 | 427 | | friendHeadGO.Configure(friendInfo); |
| | 428 | |
|
| 4 | 429 | | return friendHeadGO; |
| | 430 | | } |
| | 431 | |
|
| | 432 | | internal void RebuildCardLayouts() |
| | 433 | | { |
| 12 | 434 | | if (contentVerticalLayout != null) |
| 3 | 435 | | Utils.ForceRebuildLayoutImmediate(contentVerticalLayout.transform as RectTransform); |
| | 436 | |
|
| 12 | 437 | | if (infoVerticalLayout != null) |
| 3 | 438 | | Utils.ForceRebuildLayoutImmediate(infoVerticalLayout.transform as RectTransform); |
| 12 | 439 | | } |
| | 440 | |
|
| 4 | 441 | | internal void CloseModal() { Hide(); } |
| | 442 | |
|
| 2 | 443 | | internal void OnCloseActionTriggered(DCLAction_Trigger action) { CloseModal(); } |
| | 444 | | } |