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