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