| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using TMPro; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.EventSystems; |
| | 7 | | using UnityEngine.UI; |
| | 8 | |
|
| | 9 | | public class FriendEntryBase : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler |
| | 10 | | { |
| | 11 | | public class Model |
| | 12 | | { |
| | 13 | | public PresenceStatus status; |
| | 14 | | public string userName; |
| | 15 | | public Vector2 coords; |
| | 16 | | public string realm; |
| | 17 | | public string realmServerName; |
| | 18 | | public string realmLayerName; |
| | 19 | | public ILazyTextureObserver avatarSnapshotObserver; |
| | 20 | | public bool blocked; |
| | 21 | | } |
| | 22 | |
|
| 81 | 23 | | public Model model { get; private set; } = new Model(); |
| | 24 | | [System.NonSerialized] public string userId; |
| | 25 | |
|
| | 26 | | public Image playerBlockedImage; |
| | 27 | | public Transform menuPositionReference; |
| | 28 | |
|
| | 29 | | [SerializeField] protected internal TextMeshProUGUI playerNameText; |
| | 30 | | [SerializeField] protected internal RawImage playerImage; |
| | 31 | | [SerializeField] protected internal Button menuButton; |
| | 32 | | [SerializeField] protected internal Image backgroundImage; |
| | 33 | | [SerializeField] protected internal Sprite hoveredBackgroundSprite; |
| | 34 | | [SerializeField] protected internal AudioEvent audioEventHover; |
| | 35 | | protected internal Sprite unhoveredBackgroundSprite; |
| | 36 | |
|
| | 37 | | public event System.Action<FriendEntryBase> OnMenuToggle; |
| | 38 | |
|
| | 39 | | public virtual void Awake() |
| | 40 | | { |
| 79 | 41 | | unhoveredBackgroundSprite = backgroundImage.sprite; |
| 79 | 42 | | menuButton.onClick.RemoveAllListeners(); |
| 85 | 43 | | menuButton.onClick.AddListener(() => OnMenuToggle?.Invoke(this)); |
| 79 | 44 | | } |
| | 45 | |
|
| | 46 | | public void OnPointerEnter(PointerEventData eventData) |
| | 47 | | { |
| 0 | 48 | | backgroundImage.sprite = hoveredBackgroundSprite; |
| 0 | 49 | | menuButton.gameObject.SetActive(true); |
| | 50 | |
|
| 0 | 51 | | if (audioEventHover != null) |
| 0 | 52 | | audioEventHover.Play(true); |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | public void OnPointerExit(PointerEventData eventData) |
| | 56 | | { |
| 125 | 57 | | backgroundImage.sprite = unhoveredBackgroundSprite; |
| 125 | 58 | | menuButton.gameObject.SetActive(false); |
| 125 | 59 | | } |
| | 60 | |
|
| | 61 | | private void OnEnable() |
| | 62 | | { |
| 125 | 63 | | model.avatarSnapshotObserver?.AddListener(OnAvatarImageChange); |
| 11 | 64 | | } |
| | 65 | |
|
| | 66 | | protected virtual void OnDisable() |
| | 67 | | { |
| 125 | 68 | | model.avatarSnapshotObserver?.RemoveListener(OnAvatarImageChange); |
| 125 | 69 | | OnPointerExit(null); |
| 125 | 70 | | } |
| | 71 | |
|
| | 72 | | protected void OnDestroy() |
| | 73 | | { |
| 79 | 74 | | model.avatarSnapshotObserver?.RemoveListener(OnAvatarImageChange); |
| 16 | 75 | | } |
| | 76 | |
|
| | 77 | | public virtual void Populate(Model model) |
| | 78 | | { |
| 40 | 79 | | if (playerNameText.text != model.userName) |
| 39 | 80 | | playerNameText.text = model.userName; |
| | 81 | |
|
| 40 | 82 | | playerBlockedImage.enabled = model.blocked; |
| | 83 | |
|
| 40 | 84 | | if (this.model != null && isActiveAndEnabled) |
| | 85 | | { |
| 39 | 86 | | this.model.avatarSnapshotObserver?.RemoveListener(OnAvatarImageChange); |
| 39 | 87 | | model.avatarSnapshotObserver?.AddListener(OnAvatarImageChange); |
| | 88 | | } |
| | 89 | |
|
| 40 | 90 | | this.model = model; |
| 40 | 91 | | } |
| | 92 | |
|
| 6 | 93 | | private void OnAvatarImageChange(Texture2D texture) { playerImage.texture = texture; } |
| | 94 | | } |