| | 1 | | using DCL; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using TMPro; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.UI; |
| | 6 | |
|
| | 7 | | public interface IEventCardComponentView |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Event that will be triggered when the jumpIn button is clicked. |
| | 11 | | /// </summary> |
| | 12 | | Button.ButtonClickedEvent onJumpInClick { get; } |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// Event that will be triggered when the info button is clicked. |
| | 16 | | /// </summary> |
| | 17 | | Button.ButtonClickedEvent onInfoClick { get; } |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Event that will be triggered when the subscribe event button is clicked. |
| | 21 | | /// </summary> |
| | 22 | | Button.ButtonClickedEvent onSubscribeClick { get; } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Event that will be triggered when the unsubscribe event button is clicked. |
| | 26 | | /// </summary> |
| | 27 | | Button.ButtonClickedEvent onUnsubscribeClick { get; } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Set the event picture directly from a sprite. |
| | 31 | | /// </summary> |
| | 32 | | /// <param name="sprite">Event picture (sprite).</param> |
| | 33 | | void SetEventPicture(Sprite sprite); |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Set the event picture from a 2D texture. |
| | 37 | | /// </summary> |
| | 38 | | /// <param name="texture">Event picture (url).</param> |
| | 39 | | void SetEventPicture(Texture2D texture); |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Set the event picture from an uri. |
| | 43 | | /// </summary> |
| | 44 | | /// <param name="uri"></param> |
| | 45 | | void SetEventPicture(string uri); |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// Set the event card as live mode. |
| | 49 | | /// </summary> |
| | 50 | | /// <param name="isLive">True to set the event as live.</param> |
| | 51 | | void SetEventAsLive(bool isLive); |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Set the live tag text. |
| | 55 | | /// </summary> |
| | 56 | | /// <param name="newText">New text.</param> |
| | 57 | | void SetLiveTagText(string newText); |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Set the event date. |
| | 61 | | /// </summary> |
| | 62 | | /// <param name="newDate">The new date showed in the event card.</param> |
| | 63 | | void SetEventDate(string newDate); |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// Set the event name in the card. |
| | 67 | | /// </summary> |
| | 68 | | /// <param name="newText">New event name.</param> |
| | 69 | | void SetEventName(string newText); |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Set the event description in the card. |
| | 73 | | /// </summary> |
| | 74 | | /// <param name="newText">New event description.</param> |
| | 75 | | void SetEventDescription(string newText); |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Set the event started time in the card. |
| | 79 | | /// </summary> |
| | 80 | | /// <param name="newText">New event started time.</param> |
| | 81 | | void SetEventStartedIn(string newText); |
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// Set the event dates range in the card. |
| | 85 | | /// </summary> |
| | 86 | | /// <param name="newText">New event date range.</param> |
| | 87 | | void SetEventStartsInFromTo(string newText); |
| | 88 | |
|
| | 89 | | /// <summary> |
| | 90 | | /// Set the event organizer in the card. |
| | 91 | | /// </summary> |
| | 92 | | /// <param name="newText">New event organizer.</param> |
| | 93 | | void SetEventOrganizer(string newText); |
| | 94 | |
|
| | 95 | | /// <summary> |
| | 96 | | /// Set the event place in the card. |
| | 97 | | /// </summary> |
| | 98 | | /// <param name="newText">New event place.</param> |
| | 99 | | void SetEventPlace(string newText); |
| | 100 | |
|
| | 101 | | /// <summary> |
| | 102 | | /// Set the the number of users subscribed to the event. |
| | 103 | | /// </summary> |
| | 104 | | /// <param name="newNumberOfUsers">Number of users subscribed.</param> |
| | 105 | | void SetSubscribersUsers(int newNumberOfUsers); |
| | 106 | |
|
| | 107 | | /// <summary> |
| | 108 | | /// Set the event coords. |
| | 109 | | /// </summary> |
| | 110 | | /// <param name="newCoords">Event coords.</param> |
| | 111 | | void SetCoords(Vector2Int newCoords); |
| | 112 | |
|
| | 113 | | /// <summary> |
| | 114 | | /// Active or deactive the loading indicator. |
| | 115 | | /// </summary> |
| | 116 | | /// <param name="isVisible">True for showing the loading indicator and hiding the card info.</param> |
| | 117 | | void SetLoadingIndicatorVisible(bool isVisible); |
| | 118 | | } |
| | 119 | |
|
| | 120 | | public class EventCardComponentView : BaseComponentView, IEventCardComponentView, IComponentModelConfig |
| | 121 | | { |
| | 122 | | internal const string USERS_CONFIRMED_MESSAGE = "{0} confirmed"; |
| | 123 | | internal const string NOBODY_CONFIRMED_MESSAGE = "Nobody confirmed yet"; |
| | 124 | |
|
| 1 | 125 | | internal static readonly int ON_FOCUS_CARD_COMPONENT_BOOL = Animator.StringToHash("OnFocus"); |
| | 126 | |
|
| | 127 | | [Header("Prefab References")] |
| | 128 | | [SerializeField] internal ImageComponentView eventImage; |
| | 129 | | [SerializeField] internal TagComponentView liveTag; |
| | 130 | | [SerializeField] internal TMP_Text eventDateText; |
| | 131 | | [SerializeField] internal TMP_Text eventNameText; |
| | 132 | | [SerializeField] internal TMP_Text eventDescText; |
| | 133 | | [SerializeField] internal TMP_Text eventStartedInTitleForLive; |
| | 134 | | [SerializeField] internal TMP_Text eventStartedInTitleForNotLive; |
| | 135 | | [SerializeField] internal TMP_Text eventStartedInText; |
| | 136 | | [SerializeField] internal TMP_Text eventStartsInFromToText; |
| | 137 | | [SerializeField] internal TMP_Text eventOrganizerText; |
| | 138 | | [SerializeField] internal TMP_Text eventPlaceText; |
| | 139 | | [SerializeField] internal TMP_Text subscribedUsersTitleForLive; |
| | 140 | | [SerializeField] internal TMP_Text subscribedUsersTitleForNotLive; |
| | 141 | | [SerializeField] internal TMP_Text subscribedUsersText; |
| | 142 | | [SerializeField] internal Button modalBackgroundButton; |
| | 143 | | [SerializeField] internal ButtonComponentView closeCardButton; |
| | 144 | | [SerializeField] internal InputAction_Trigger closeAction; |
| | 145 | | [SerializeField] internal ButtonComponentView infoButton; |
| | 146 | | [SerializeField] internal ButtonComponentView jumpinButton; |
| | 147 | | [SerializeField] internal ButtonComponentView subscribeEventButton; |
| | 148 | | [SerializeField] internal ButtonComponentView unsubscribeEventButton; |
| | 149 | | [SerializeField] internal GameObject imageContainer; |
| | 150 | | [SerializeField] internal GameObject eventInfoContainer; |
| | 151 | | [SerializeField] internal GameObject loadingSpinner; |
| | 152 | | [SerializeField] internal Animator cardAnimator; |
| | 153 | | [SerializeField] internal VerticalLayoutGroup contentVerticalLayout; |
| | 154 | | [SerializeField] internal VerticalLayoutGroup infoVerticalLayout; |
| | 155 | | [SerializeField] internal HorizontalLayoutGroup timeAndPlayersHorizontalLayout; |
| | 156 | |
|
| | 157 | | [Header("Configuration")] |
| | 158 | | [SerializeField] internal Sprite defaultPicture; |
| | 159 | | [SerializeField] internal bool isEventCardModal = false; |
| | 160 | | [SerializeField] internal EventCardComponentModel model; |
| | 161 | |
|
| 0 | 162 | | public Button.ButtonClickedEvent onJumpInClick => jumpinButton?.onClick; |
| 0 | 163 | | public Button.ButtonClickedEvent onInfoClick => infoButton?.onClick; |
| 0 | 164 | | public Button.ButtonClickedEvent onSubscribeClick => subscribeEventButton?.onClick; |
| 0 | 165 | | public Button.ButtonClickedEvent onUnsubscribeClick => unsubscribeEventButton?.onClick; |
| | 166 | |
|
| | 167 | | public override void Start() |
| | 168 | | { |
| 54 | 169 | | if (eventImage != null) |
| 54 | 170 | | eventImage.OnLoaded += OnEventImageLoaded; |
| | 171 | |
|
| 54 | 172 | | if (closeCardButton != null) |
| 26 | 173 | | closeCardButton.onClick.AddListener(CloseModal); |
| | 174 | |
|
| 54 | 175 | | if (closeAction != null) |
| 26 | 176 | | closeAction.OnTriggered += OnCloseActionTriggered; |
| | 177 | |
|
| 54 | 178 | | if (modalBackgroundButton != null) |
| 26 | 179 | | modalBackgroundButton.onClick.AddListener(CloseModal); |
| 54 | 180 | | } |
| | 181 | |
|
| | 182 | | public void Configure(BaseComponentModel newModel) |
| | 183 | | { |
| 18 | 184 | | model = (EventCardComponentModel)newModel; |
| 18 | 185 | | RefreshControl(); |
| 18 | 186 | | } |
| | 187 | |
|
| | 188 | | public override void RefreshControl() |
| | 189 | | { |
| 18 | 190 | | if (model == null) |
| 0 | 191 | | return; |
| | 192 | |
|
| 18 | 193 | | if (model.eventPictureSprite != null) |
| 18 | 194 | | SetEventPicture(model.eventPictureSprite); |
| 0 | 195 | | else if (model.eventPictureTexture != null) |
| 0 | 196 | | SetEventPicture(model.eventPictureTexture); |
| 0 | 197 | | else if (!string.IsNullOrEmpty(model.eventPictureUri)) |
| 0 | 198 | | SetEventPicture(model.eventPictureUri); |
| | 199 | | else |
| 0 | 200 | | SetEventPicture(sprite: null); |
| | 201 | |
|
| 18 | 202 | | SetEventAsLive(model.isLive); |
| 18 | 203 | | SetLiveTagText(model.liveTagText); |
| 18 | 204 | | SetEventDate(model.eventDateText); |
| 18 | 205 | | SetEventName(model.eventName); |
| 18 | 206 | | SetEventDescription(model.eventDescription); |
| 18 | 207 | | SetEventStartedIn(model.eventStartedIn); |
| 18 | 208 | | SetEventStartsInFromTo(model.eventStartsInFromTo); |
| 18 | 209 | | SetEventOrganizer(model.eventOrganizer); |
| 18 | 210 | | SetEventPlace(model.eventPlace); |
| 18 | 211 | | SetSubscribersUsers(model.subscribedUsers); |
| 18 | 212 | | SetCoords(model.coords); |
| | 213 | |
|
| 18 | 214 | | RebuildCardLayouts(); |
| 18 | 215 | | } |
| | 216 | |
|
| | 217 | | public override void OnFocus() |
| | 218 | | { |
| 26 | 219 | | base.OnFocus(); |
| | 220 | |
|
| 26 | 221 | | if (cardAnimator != null) |
| 26 | 222 | | cardAnimator.SetBool(ON_FOCUS_CARD_COMPONENT_BOOL, true); |
| 26 | 223 | | } |
| | 224 | |
|
| | 225 | | public override void OnLoseFocus() |
| | 226 | | { |
| 229 | 227 | | base.OnLoseFocus(); |
| | 228 | |
|
| 229 | 229 | | if (cardAnimator != null) |
| 187 | 230 | | cardAnimator.SetBool(ON_FOCUS_CARD_COMPONENT_BOOL, false); |
| 229 | 231 | | } |
| | 232 | |
|
| | 233 | | public override void Show(bool instant = false) |
| | 234 | | { |
| 4 | 235 | | base.Show(instant); |
| | 236 | |
|
| 4 | 237 | | DataStore.i.exploreV2.isSomeModalOpen.Set(true); |
| 4 | 238 | | } |
| | 239 | |
|
| | 240 | | public override void Hide(bool instant = false) |
| | 241 | | { |
| 40 | 242 | | base.Hide(instant); |
| | 243 | |
|
| 40 | 244 | | DataStore.i.exploreV2.isSomeModalOpen.Set(false); |
| 40 | 245 | | } |
| | 246 | |
|
| | 247 | | public override void Dispose() |
| | 248 | | { |
| 286 | 249 | | base.Dispose(); |
| | 250 | |
|
| 286 | 251 | | if (eventImage != null) |
| | 252 | | { |
| 286 | 253 | | eventImage.OnLoaded -= OnEventImageLoaded; |
| 286 | 254 | | eventImage.Dispose(); |
| | 255 | | } |
| | 256 | |
|
| 286 | 257 | | if (closeCardButton != null) |
| 119 | 258 | | closeCardButton.onClick.RemoveAllListeners(); |
| | 259 | |
|
| 286 | 260 | | if (closeAction != null) |
| 119 | 261 | | closeAction.OnTriggered -= OnCloseActionTriggered; |
| | 262 | |
|
| 286 | 263 | | if (modalBackgroundButton != null) |
| 119 | 264 | | modalBackgroundButton.onClick.RemoveAllListeners(); |
| 286 | 265 | | } |
| | 266 | |
|
| | 267 | | public void SetEventPicture(Sprite sprite) |
| | 268 | | { |
| 20 | 269 | | if (sprite == null && defaultPicture != null) |
| 0 | 270 | | sprite = defaultPicture; |
| | 271 | |
|
| 20 | 272 | | model.eventPictureSprite = sprite; |
| | 273 | |
|
| 20 | 274 | | if (eventImage == null) |
| 0 | 275 | | return; |
| | 276 | |
|
| 20 | 277 | | eventImage.SetImage(sprite); |
| 20 | 278 | | } |
| | 279 | |
|
| | 280 | | public void SetEventPicture(Texture2D texture) |
| | 281 | | { |
| 1 | 282 | | if (texture == null && defaultPicture != null) |
| | 283 | | { |
| 0 | 284 | | SetEventPicture(defaultPicture); |
| 0 | 285 | | return; |
| | 286 | | } |
| | 287 | |
|
| 1 | 288 | | model.eventPictureTexture = texture; |
| | 289 | |
|
| 1 | 290 | | if (!Application.isPlaying) |
| 0 | 291 | | return; |
| | 292 | |
|
| 1 | 293 | | if (eventImage == null) |
| 0 | 294 | | return; |
| | 295 | |
|
| 1 | 296 | | eventImage.SetImage(texture); |
| 1 | 297 | | } |
| | 298 | |
|
| | 299 | | public void SetEventPicture(string uri) |
| | 300 | | { |
| 1 | 301 | | if (string.IsNullOrEmpty(uri) && defaultPicture != null) |
| | 302 | | { |
| 0 | 303 | | SetEventPicture(defaultPicture); |
| 0 | 304 | | return; |
| | 305 | | } |
| | 306 | |
|
| 1 | 307 | | model.eventPictureUri = uri; |
| | 308 | |
|
| 1 | 309 | | if (!Application.isPlaying) |
| 0 | 310 | | return; |
| | 311 | |
|
| 1 | 312 | | if (eventImage == null) |
| 0 | 313 | | return; |
| | 314 | |
|
| 1 | 315 | | eventImage.SetImage(uri); |
| 1 | 316 | | } |
| | 317 | |
|
| | 318 | | public void SetEventAsLive(bool isLive) |
| | 319 | | { |
| 22 | 320 | | model.isLive = isLive; |
| | 321 | |
|
| 22 | 322 | | if (liveTag != null) |
| 22 | 323 | | liveTag.gameObject.SetActive(isLive); |
| | 324 | |
|
| 22 | 325 | | if (eventDateText != null) |
| 22 | 326 | | eventDateText.gameObject.SetActive(!isLive); |
| | 327 | |
|
| 22 | 328 | | if (jumpinButton != null) |
| 22 | 329 | | jumpinButton.gameObject.SetActive(isLive); |
| | 330 | |
|
| 22 | 331 | | if (subscribeEventButton != null) |
| 22 | 332 | | subscribeEventButton.gameObject.SetActive(!isLive && !model.isSubscribed); |
| | 333 | |
|
| 22 | 334 | | if (unsubscribeEventButton != null) |
| 22 | 335 | | unsubscribeEventButton.gameObject.SetActive(!isLive && model.isSubscribed); |
| | 336 | |
|
| 22 | 337 | | if (eventStartedInTitleForLive) |
| 19 | 338 | | eventStartedInTitleForLive.gameObject.SetActive(isLive); |
| | 339 | |
|
| 22 | 340 | | if (eventStartedInTitleForNotLive) |
| 19 | 341 | | eventStartedInTitleForNotLive.gameObject.SetActive(!isLive); |
| | 342 | |
|
| 22 | 343 | | if (subscribedUsersTitleForLive != null) |
| 19 | 344 | | subscribedUsersTitleForLive.gameObject.SetActive(isLive); |
| | 345 | |
|
| 22 | 346 | | if (subscribedUsersTitleForNotLive != null) |
| 19 | 347 | | subscribedUsersTitleForNotLive.gameObject.SetActive(!isLive); |
| 22 | 348 | | } |
| | 349 | |
|
| | 350 | | public void SetLiveTagText(string newText) |
| | 351 | | { |
| 19 | 352 | | model.liveTagText = newText; |
| | 353 | |
|
| 19 | 354 | | if (liveTag == null) |
| 0 | 355 | | return; |
| | 356 | |
|
| 19 | 357 | | liveTag.SetText(newText); |
| 19 | 358 | | } |
| | 359 | |
|
| | 360 | | public void SetEventDate(string newDate) |
| | 361 | | { |
| 19 | 362 | | model.eventDateText = newDate; |
| | 363 | |
|
| 19 | 364 | | if (eventDateText == null) |
| 0 | 365 | | return; |
| | 366 | |
|
| 19 | 367 | | eventDateText.text = newDate; |
| 19 | 368 | | } |
| | 369 | |
|
| | 370 | | public void SetEventName(string newText) |
| | 371 | | { |
| 19 | 372 | | model.eventName = newText; |
| | 373 | |
|
| 19 | 374 | | if (eventNameText == null) |
| 0 | 375 | | return; |
| | 376 | |
|
| 19 | 377 | | eventNameText.text = newText; |
| 19 | 378 | | } |
| | 379 | |
|
| | 380 | | public void SetEventDescription(string newText) |
| | 381 | | { |
| 19 | 382 | | model.eventDescription = newText; |
| | 383 | |
|
| 19 | 384 | | if (eventDescText == null) |
| 11 | 385 | | return; |
| | 386 | |
|
| 8 | 387 | | eventDescText.text = newText; |
| 8 | 388 | | } |
| | 389 | |
|
| | 390 | | public void SetEventStartedIn(string newText) |
| | 391 | | { |
| 19 | 392 | | model.eventStartedIn = newText; |
| | 393 | |
|
| 19 | 394 | | if (eventStartedInText == null) |
| 3 | 395 | | return; |
| | 396 | |
|
| 16 | 397 | | eventStartedInText.text = newText; |
| 16 | 398 | | } |
| | 399 | |
|
| | 400 | | public void SetEventStartsInFromTo(string newText) |
| | 401 | | { |
| 19 | 402 | | model.eventStartsInFromTo = newText; |
| | 403 | |
|
| 19 | 404 | | if (eventStartsInFromToText == null) |
| 15 | 405 | | return; |
| | 406 | |
|
| 4 | 407 | | eventStartsInFromToText.text = newText; |
| 4 | 408 | | } |
| | 409 | |
|
| | 410 | | public void SetEventOrganizer(string newText) |
| | 411 | | { |
| 19 | 412 | | model.eventOrganizer = newText; |
| | 413 | |
|
| 19 | 414 | | if (eventOrganizerText == null) |
| 15 | 415 | | return; |
| | 416 | |
|
| 4 | 417 | | eventOrganizerText.text = newText; |
| 4 | 418 | | } |
| | 419 | |
|
| | 420 | | public void SetEventPlace(string newText) |
| | 421 | | { |
| 19 | 422 | | model.eventPlace = newText; |
| | 423 | |
|
| 19 | 424 | | if (eventPlaceText == null) |
| 15 | 425 | | return; |
| | 426 | |
|
| 4 | 427 | | eventPlaceText.text = newText; |
| 4 | 428 | | } |
| | 429 | |
|
| | 430 | | public void SetSubscribersUsers(int newNumberOfUsers) |
| | 431 | | { |
| 22 | 432 | | model.subscribedUsers = newNumberOfUsers; |
| | 433 | |
|
| 22 | 434 | | if (subscribedUsersText == null) |
| 0 | 435 | | return; |
| | 436 | |
|
| 22 | 437 | | if (!isEventCardModal) |
| | 438 | | { |
| 17 | 439 | | subscribedUsersText.text = newNumberOfUsers.ToString(); |
| 17 | 440 | | } |
| | 441 | | else |
| | 442 | | { |
| 5 | 443 | | if (newNumberOfUsers > 0) |
| 4 | 444 | | subscribedUsersText.text = string.Format(USERS_CONFIRMED_MESSAGE, newNumberOfUsers); |
| | 445 | | else |
| 1 | 446 | | subscribedUsersText.text = NOBODY_CONFIRMED_MESSAGE; |
| | 447 | | } |
| 1 | 448 | | } |
| | 449 | |
|
| | 450 | | public void SetCoords(Vector2Int newCoords) |
| | 451 | | { |
| 19 | 452 | | model.coords = newCoords; |
| | 453 | |
|
| 19 | 454 | | if (jumpinButton == null || !isEventCardModal) |
| 15 | 455 | | return; |
| | 456 | |
|
| 4 | 457 | | jumpinButton.SetText($"{newCoords.x},{newCoords.y}"); |
| 4 | 458 | | } |
| | 459 | |
|
| | 460 | | public void SetLoadingIndicatorVisible(bool isVisible) |
| | 461 | | { |
| 2 | 462 | | imageContainer.SetActive(!isVisible); |
| 2 | 463 | | eventInfoContainer.SetActive(!isVisible); |
| 2 | 464 | | loadingSpinner.SetActive(isVisible); |
| 2 | 465 | | } |
| | 466 | |
|
| 2 | 467 | | internal void OnEventImageLoaded(Sprite sprite) { SetEventPicture(sprite); } |
| | 468 | |
|
| | 469 | | internal void RebuildCardLayouts() |
| | 470 | | { |
| 18 | 471 | | if (contentVerticalLayout != null) |
| 3 | 472 | | Utils.ForceRebuildLayoutImmediate(contentVerticalLayout.transform as RectTransform); |
| | 473 | |
|
| 18 | 474 | | if (infoVerticalLayout != null) |
| 3 | 475 | | Utils.ForceRebuildLayoutImmediate(infoVerticalLayout.transform as RectTransform); |
| | 476 | |
|
| 18 | 477 | | if (timeAndPlayersHorizontalLayout != null) |
| 15 | 478 | | Utils.ForceRebuildLayoutImmediate(timeAndPlayersHorizontalLayout.transform as RectTransform); |
| 18 | 479 | | } |
| | 480 | |
|
| 4 | 481 | | internal void CloseModal() { Hide(); } |
| | 482 | |
|
| 2 | 483 | | internal void OnCloseActionTriggered(DCLAction_Trigger action) { CloseModal(); } |
| | 484 | | } |