| | 1 | | using DCL; |
| | 2 | | using DCL.Social.Friends; |
| | 3 | | using System; |
| | 4 | | using TMPro; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.EventSystems; |
| | 7 | | using UnityEngine.UI; |
| | 8 | |
|
| | 9 | | public class FriendEntryBase : BaseComponentView |
| | 10 | | { |
| | 11 | | private const string OPEN_PASSPORT_SOURCE = "FriendsHUD"; |
| 2 | 12 | | public FriendEntryModel Model { get; private set; } = new FriendEntryModel(); |
| | 13 | |
|
| | 14 | | public Image playerBlockedImage; |
| | 15 | |
|
| | 16 | | [SerializeField] private RectTransform menuPositionReference; |
| | 17 | | [SerializeField] protected internal TextMeshProUGUI playerNameText; |
| | 18 | | [SerializeField] protected internal RawImage playerImage; |
| | 19 | | [SerializeField] protected internal Button menuButton; |
| | 20 | | [SerializeField] protected internal AudioEvent audioEventHover; |
| | 21 | | [SerializeField] protected internal GameObject onlineStatusContainer; |
| | 22 | | [SerializeField] protected internal GameObject offlineStatusContainer; |
| | 23 | | [SerializeField] protected internal Button passportButton; |
| | 24 | |
|
| | 25 | | private BaseVariable<(string playerId, string source)> currentPlayerInfoCardId; |
| | 26 | | private bool avatarFetchingEnabled; |
| | 27 | |
|
| | 28 | | public event Action<FriendEntryBase> OnMenuToggle; |
| | 29 | |
|
| | 30 | | public override void Awake() |
| | 31 | | { |
| 0 | 32 | | menuButton.onClick.RemoveAllListeners(); |
| 0 | 33 | | menuButton.onClick.AddListener(() => OnMenuToggle?.Invoke(this)); |
| 0 | 34 | | passportButton?.onClick.RemoveAllListeners(); |
| 0 | 35 | | passportButton?.onClick.AddListener(ShowUserProfile); |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | public override void OnPointerEnter(PointerEventData eventData) |
| | 39 | | { |
| 0 | 40 | | if (audioEventHover != null) |
| 0 | 41 | | audioEventHover.Play(true); |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | public void Dock(UserContextMenu contextMenuPanel) |
| | 45 | | { |
| 0 | 46 | | var panelTransform = (RectTransform) contextMenuPanel.transform; |
| 0 | 47 | | panelTransform.pivot = menuPositionReference.pivot; |
| 0 | 48 | | panelTransform.position = menuPositionReference.position; |
| 0 | 49 | | } |
| | 50 | |
|
| | 51 | | public override void OnDisable() |
| | 52 | | { |
| 0 | 53 | | DisableAvatarSnapshotFetching(); |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | protected void OnDestroy() |
| | 57 | | { |
| 0 | 58 | | DisableAvatarSnapshotFetching(); |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | public virtual void EnableAvatarSnapshotFetching() |
| | 62 | | { |
| 0 | 63 | | if (avatarFetchingEnabled) return; |
| 0 | 64 | | avatarFetchingEnabled = true; |
| | 65 | | // TODO: replace image loading for ImageComponentView implementation |
| 0 | 66 | | Model?.avatarSnapshotObserver?.AddListener(OnAvatarImageChange); |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | public virtual void DisableAvatarSnapshotFetching() |
| | 70 | | { |
| 0 | 71 | | if (!avatarFetchingEnabled) return; |
| 0 | 72 | | avatarFetchingEnabled = false; |
| | 73 | | // TODO: replace image loading for ImageComponentView implementation |
| 0 | 74 | | Model?.avatarSnapshotObserver?.RemoveListener(OnAvatarImageChange); |
| 0 | 75 | | } |
| | 76 | |
|
| | 77 | | public override void RefreshControl() |
| | 78 | | { |
| 0 | 79 | | if (playerNameText.text != Model.userName) |
| 0 | 80 | | playerNameText.text = Model.userName; |
| | 81 | |
|
| 0 | 82 | | playerBlockedImage.enabled = Model.blocked; |
| | 83 | |
|
| 0 | 84 | | Model?.avatarSnapshotObserver?.RemoveListener(OnAvatarImageChange); |
| | 85 | |
|
| 0 | 86 | | if (isActiveAndEnabled && avatarFetchingEnabled) |
| | 87 | | // TODO: replace image loading for ImageComponentView implementation |
| 0 | 88 | | Model.avatarSnapshotObserver?.AddListener(OnAvatarImageChange); |
| | 89 | |
|
| 0 | 90 | | if (onlineStatusContainer != null) |
| 0 | 91 | | onlineStatusContainer.SetActive(Model.status == PresenceStatus.ONLINE && !Model.blocked); |
| 0 | 92 | | if (offlineStatusContainer != null) |
| 0 | 93 | | offlineStatusContainer.SetActive(Model.status != PresenceStatus.ONLINE && !Model.blocked); |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | public virtual void Populate(FriendEntryModel model) |
| | 97 | | { |
| 0 | 98 | | Model = model; |
| 0 | 99 | | RefreshControl(); |
| 0 | 100 | | } |
| | 101 | |
|
| | 102 | | public virtual bool IsVisible(RectTransform container) |
| | 103 | | { |
| 0 | 104 | | if (!gameObject.activeSelf) return false; |
| 0 | 105 | | return ((RectTransform) transform).CountCornersVisibleFrom(container) > 0; |
| | 106 | | } |
| | 107 | |
|
| 0 | 108 | | private void OnAvatarImageChange(Texture2D texture) { playerImage.texture = texture; } |
| | 109 | |
|
| | 110 | | private void ShowUserProfile() |
| | 111 | | { |
| 0 | 112 | | if (currentPlayerInfoCardId == null) |
| 0 | 113 | | currentPlayerInfoCardId = DataStore.i.HUDs.currentPlayerId; |
| | 114 | |
|
| 0 | 115 | | currentPlayerInfoCardId.Set((Model.userId, OPEN_PASSPORT_SOURCE)); |
| 0 | 116 | | } |
| | 117 | | } |