| | 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 bool isEventCardModal = false; |
| | 158 | | [SerializeField] internal EventCardComponentModel model; |
| | 159 | |
|
| 0 | 160 | | public Button.ButtonClickedEvent onJumpInClick => jumpinButton?.onClick; |
| 0 | 161 | | public Button.ButtonClickedEvent onInfoClick => infoButton?.onClick; |
| 0 | 162 | | public Button.ButtonClickedEvent onSubscribeClick => subscribeEventButton?.onClick; |
| 0 | 163 | | public Button.ButtonClickedEvent onUnsubscribeClick => unsubscribeEventButton?.onClick; |
| | 164 | |
|
| | 165 | | public override void Start() |
| | 166 | | { |
| 52 | 167 | | if (eventImage != null) |
| 52 | 168 | | eventImage.OnLoaded += OnEventImageLoaded; |
| | 169 | |
|
| 52 | 170 | | if (closeCardButton != null) |
| 26 | 171 | | closeCardButton.onClick.AddListener(CloseModal); |
| | 172 | |
|
| 52 | 173 | | if (closeAction != null) |
| 26 | 174 | | closeAction.OnTriggered += OnCloseActionTriggered; |
| | 175 | |
|
| 52 | 176 | | if (modalBackgroundButton != null) |
| 26 | 177 | | modalBackgroundButton.onClick.AddListener(CloseModal); |
| 52 | 178 | | } |
| | 179 | |
|
| | 180 | | public void Configure(BaseComponentModel newModel) |
| | 181 | | { |
| 12 | 182 | | model = (EventCardComponentModel)newModel; |
| 12 | 183 | | RefreshControl(); |
| 12 | 184 | | } |
| | 185 | |
|
| | 186 | | public override void RefreshControl() |
| | 187 | | { |
| 12 | 188 | | if (model == null) |
| 0 | 189 | | return; |
| | 190 | |
|
| 12 | 191 | | if (model.eventPictureSprite != null) |
| 12 | 192 | | SetEventPicture(model.eventPictureSprite); |
| 0 | 193 | | else if (model.eventPictureTexture != null) |
| 0 | 194 | | SetEventPicture(model.eventPictureTexture); |
| 0 | 195 | | else if (!string.IsNullOrEmpty(model.eventPictureUri)) |
| 0 | 196 | | SetEventPicture(model.eventPictureUri); |
| | 197 | | else |
| 0 | 198 | | SetEventPicture(sprite: null); |
| | 199 | |
|
| 12 | 200 | | SetEventAsLive(model.isLive); |
| 12 | 201 | | SetLiveTagText(model.liveTagText); |
| 12 | 202 | | SetEventDate(model.eventDateText); |
| 12 | 203 | | SetEventName(model.eventName); |
| 12 | 204 | | SetEventDescription(model.eventDescription); |
| 12 | 205 | | SetEventStartedIn(model.eventStartedIn); |
| 12 | 206 | | SetEventStartsInFromTo(model.eventStartsInFromTo); |
| 12 | 207 | | SetEventOrganizer(model.eventOrganizer); |
| 12 | 208 | | SetEventPlace(model.eventPlace); |
| 12 | 209 | | SetSubscribersUsers(model.subscribedUsers); |
| 12 | 210 | | SetCoords(model.coords); |
| | 211 | |
|
| 12 | 212 | | RebuildCardLayouts(); |
| 12 | 213 | | } |
| | 214 | |
|
| | 215 | | public override void OnFocus() |
| | 216 | | { |
| 26 | 217 | | base.OnFocus(); |
| | 218 | |
|
| 26 | 219 | | if (cardAnimator != null) |
| 26 | 220 | | cardAnimator.SetBool(ON_FOCUS_CARD_COMPONENT_BOOL, true); |
| 26 | 221 | | } |
| | 222 | |
|
| | 223 | | public override void OnLoseFocus() |
| | 224 | | { |
| 26 | 225 | | base.OnLoseFocus(); |
| | 226 | |
|
| 26 | 227 | | if (cardAnimator != null) |
| 26 | 228 | | cardAnimator.SetBool(ON_FOCUS_CARD_COMPONENT_BOOL, false); |
| 26 | 229 | | } |
| | 230 | |
|
| | 231 | | public override void Dispose() |
| | 232 | | { |
| 256 | 233 | | base.Dispose(); |
| | 234 | |
|
| 256 | 235 | | if (eventImage != null) |
| | 236 | | { |
| 256 | 237 | | eventImage.OnLoaded -= OnEventImageLoaded; |
| 256 | 238 | | eventImage.Dispose(); |
| | 239 | | } |
| | 240 | |
|
| 256 | 241 | | if (closeCardButton != null) |
| 105 | 242 | | closeCardButton.onClick.RemoveAllListeners(); |
| | 243 | |
|
| 256 | 244 | | if (closeAction != null) |
| 105 | 245 | | closeAction.OnTriggered -= OnCloseActionTriggered; |
| | 246 | |
|
| 256 | 247 | | if (modalBackgroundButton != null) |
| 105 | 248 | | modalBackgroundButton.onClick.RemoveAllListeners(); |
| 256 | 249 | | } |
| | 250 | |
|
| | 251 | | public void SetEventPicture(Sprite sprite) |
| | 252 | | { |
| 14 | 253 | | model.eventPictureSprite = sprite; |
| | 254 | |
|
| 14 | 255 | | if (eventImage == null) |
| 0 | 256 | | return; |
| | 257 | |
|
| 14 | 258 | | eventImage.SetImage(sprite); |
| 14 | 259 | | } |
| | 260 | |
|
| | 261 | | public void SetEventPicture(Texture2D texture) |
| | 262 | | { |
| 1 | 263 | | model.eventPictureTexture = texture; |
| | 264 | |
|
| 1 | 265 | | if (!Application.isPlaying) |
| 0 | 266 | | return; |
| | 267 | |
|
| 1 | 268 | | if (eventImage == null) |
| 0 | 269 | | return; |
| | 270 | |
|
| 1 | 271 | | eventImage.SetImage(texture); |
| 1 | 272 | | } |
| | 273 | |
|
| | 274 | | public void SetEventPicture(string uri) |
| | 275 | | { |
| 1 | 276 | | model.eventPictureUri = uri; |
| | 277 | |
|
| 1 | 278 | | if (!Application.isPlaying) |
| 0 | 279 | | return; |
| | 280 | |
|
| 1 | 281 | | if (eventImage == null) |
| 0 | 282 | | return; |
| | 283 | |
|
| 1 | 284 | | eventImage.SetImage(uri); |
| 1 | 285 | | } |
| | 286 | |
|
| | 287 | | public void SetEventAsLive(bool isLive) |
| | 288 | | { |
| 16 | 289 | | model.isLive = isLive; |
| | 290 | |
|
| 16 | 291 | | if (liveTag != null) |
| 16 | 292 | | liveTag.gameObject.SetActive(isLive); |
| | 293 | |
|
| 16 | 294 | | if (eventDateText != null) |
| 16 | 295 | | eventDateText.gameObject.SetActive(!isLive); |
| | 296 | |
|
| 16 | 297 | | if (jumpinButton != null) |
| 16 | 298 | | jumpinButton.gameObject.SetActive(isLive); |
| | 299 | |
|
| 16 | 300 | | if (subscribeEventButton != null) |
| 16 | 301 | | subscribeEventButton.gameObject.SetActive(!isLive && !model.isSubscribed); |
| | 302 | |
|
| 16 | 303 | | if (unsubscribeEventButton != null) |
| 16 | 304 | | unsubscribeEventButton.gameObject.SetActive(!isLive && model.isSubscribed); |
| | 305 | |
|
| 16 | 306 | | if (eventStartedInTitleForLive) |
| 15 | 307 | | eventStartedInTitleForLive.gameObject.SetActive(isLive); |
| | 308 | |
|
| 16 | 309 | | if (eventStartedInTitleForNotLive) |
| 15 | 310 | | eventStartedInTitleForNotLive.gameObject.SetActive(!isLive); |
| | 311 | |
|
| 16 | 312 | | if (subscribedUsersTitleForLive != null) |
| 15 | 313 | | subscribedUsersTitleForLive.gameObject.SetActive(isLive); |
| | 314 | |
|
| 16 | 315 | | if (subscribedUsersTitleForNotLive != null) |
| 15 | 316 | | subscribedUsersTitleForNotLive.gameObject.SetActive(!isLive); |
| 16 | 317 | | } |
| | 318 | |
|
| | 319 | | public void SetLiveTagText(string newText) |
| | 320 | | { |
| 13 | 321 | | model.liveTagText = newText; |
| | 322 | |
|
| 13 | 323 | | if (liveTag == null) |
| 0 | 324 | | return; |
| | 325 | |
|
| 13 | 326 | | liveTag.SetText(newText); |
| 13 | 327 | | } |
| | 328 | |
|
| | 329 | | public void SetEventDate(string newDate) |
| | 330 | | { |
| 13 | 331 | | model.eventDateText = newDate; |
| | 332 | |
|
| 13 | 333 | | if (eventDateText == null) |
| 0 | 334 | | return; |
| | 335 | |
|
| 13 | 336 | | eventDateText.text = newDate; |
| 13 | 337 | | } |
| | 338 | |
|
| | 339 | | public void SetEventName(string newText) |
| | 340 | | { |
| 13 | 341 | | model.eventName = newText; |
| | 342 | |
|
| 13 | 343 | | if (eventNameText == null) |
| 0 | 344 | | return; |
| | 345 | |
|
| 13 | 346 | | eventNameText.text = newText; |
| 13 | 347 | | } |
| | 348 | |
|
| | 349 | | public void SetEventDescription(string newText) |
| | 350 | | { |
| 13 | 351 | | model.eventDescription = newText; |
| | 352 | |
|
| 13 | 353 | | if (eventDescText == null) |
| 9 | 354 | | return; |
| | 355 | |
|
| 4 | 356 | | eventDescText.text = newText; |
| 4 | 357 | | } |
| | 358 | |
|
| | 359 | | public void SetEventStartedIn(string newText) |
| | 360 | | { |
| 13 | 361 | | model.eventStartedIn = newText; |
| | 362 | |
|
| 13 | 363 | | if (eventStartedInText == null) |
| 1 | 364 | | return; |
| | 365 | |
|
| 12 | 366 | | eventStartedInText.text = newText; |
| 12 | 367 | | } |
| | 368 | |
|
| | 369 | | public void SetEventStartsInFromTo(string newText) |
| | 370 | | { |
| 13 | 371 | | model.eventStartsInFromTo = newText; |
| | 372 | |
|
| 13 | 373 | | if (eventStartsInFromToText == null) |
| 11 | 374 | | return; |
| | 375 | |
|
| 2 | 376 | | eventStartsInFromToText.text = newText; |
| 2 | 377 | | } |
| | 378 | |
|
| | 379 | | public void SetEventOrganizer(string newText) |
| | 380 | | { |
| 13 | 381 | | model.eventOrganizer = newText; |
| | 382 | |
|
| 13 | 383 | | if (eventOrganizerText == null) |
| 11 | 384 | | return; |
| | 385 | |
|
| 2 | 386 | | eventOrganizerText.text = newText; |
| 2 | 387 | | } |
| | 388 | |
|
| | 389 | | public void SetEventPlace(string newText) |
| | 390 | | { |
| 13 | 391 | | model.eventPlace = newText; |
| | 392 | |
|
| 13 | 393 | | if (eventPlaceText == null) |
| 11 | 394 | | return; |
| | 395 | |
|
| 2 | 396 | | eventPlaceText.text = newText; |
| 2 | 397 | | } |
| | 398 | |
|
| | 399 | | public void SetSubscribersUsers(int newNumberOfUsers) |
| | 400 | | { |
| 16 | 401 | | model.subscribedUsers = newNumberOfUsers; |
| | 402 | |
|
| 16 | 403 | | if (subscribedUsersText == null) |
| 0 | 404 | | return; |
| | 405 | |
|
| 16 | 406 | | if (!isEventCardModal) |
| | 407 | | { |
| 13 | 408 | | subscribedUsersText.text = newNumberOfUsers.ToString(); |
| 13 | 409 | | } |
| | 410 | | else |
| | 411 | | { |
| 3 | 412 | | if (newNumberOfUsers > 0) |
| 2 | 413 | | subscribedUsersText.text = string.Format(USERS_CONFIRMED_MESSAGE, newNumberOfUsers); |
| | 414 | | else |
| 1 | 415 | | subscribedUsersText.text = NOBODY_CONFIRMED_MESSAGE; |
| | 416 | | } |
| 1 | 417 | | } |
| | 418 | |
|
| | 419 | | public void SetCoords(Vector2Int newCoords) |
| | 420 | | { |
| 13 | 421 | | model.coords = newCoords; |
| | 422 | |
|
| 13 | 423 | | if (jumpinButton == null || !isEventCardModal) |
| 11 | 424 | | return; |
| | 425 | |
|
| 2 | 426 | | jumpinButton.SetText($"{newCoords.x},{newCoords.y}"); |
| 2 | 427 | | } |
| | 428 | |
|
| | 429 | | public void SetLoadingIndicatorVisible(bool isVisible) |
| | 430 | | { |
| 2 | 431 | | imageContainer.SetActive(!isVisible); |
| 2 | 432 | | eventInfoContainer.SetActive(!isVisible); |
| 2 | 433 | | loadingSpinner.SetActive(isVisible); |
| 2 | 434 | | } |
| | 435 | |
|
| 2 | 436 | | internal void OnEventImageLoaded(Sprite sprite) { SetEventPicture(sprite); } |
| | 437 | |
|
| | 438 | | internal void RebuildCardLayouts() |
| | 439 | | { |
| 12 | 440 | | if (contentVerticalLayout != null) |
| 1 | 441 | | Utils.ForceRebuildLayoutImmediate(contentVerticalLayout.transform as RectTransform); |
| | 442 | |
|
| 12 | 443 | | if (infoVerticalLayout != null) |
| 1 | 444 | | Utils.ForceRebuildLayoutImmediate(infoVerticalLayout.transform as RectTransform); |
| | 445 | |
|
| 12 | 446 | | if (timeAndPlayersHorizontalLayout != null) |
| 11 | 447 | | Utils.ForceRebuildLayoutImmediate(timeAndPlayersHorizontalLayout.transform as RectTransform); |
| 12 | 448 | | } |
| | 449 | |
|
| 4 | 450 | | internal void CloseModal() { Hide(); } |
| | 451 | |
|
| 2 | 452 | | internal void OnCloseActionTriggered(DCLAction_Trigger action) { CloseModal(); } |
| | 453 | | } |