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