| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | internal interface IOwnersTooltipView |
| | 7 | | { |
| | 8 | | event Action OnViewAllPressed; |
| | 9 | | event Action OnFocusLost; |
| | 10 | | void Show(); |
| | 11 | | void Hide(bool instant); |
| | 12 | | void KeepOnScreen(); |
| | 13 | | void SetElements(List<IOwnerInfoElement> elements); |
| | 14 | | bool IsActive(); |
| | 15 | | } |
| | 16 | |
|
| | 17 | | internal class OwnersTooltipView : MonoBehaviour, IOwnersTooltipView |
| | 18 | | { |
| | 19 | | internal const int MAX_ELEMENTS = 5; |
| | 20 | | private const int ADDRESS_MAX_CHARS = 11; |
| | 21 | | private const float HIDE_DELAY = 0.3f; |
| | 22 | | private const float ELEMENT_FONT_SIZE = 16; |
| | 23 | | private const float ELEMENT_HORIZONTAL_SPACING = 10; |
| | 24 | |
|
| | 25 | | [SerializeField] internal Transform ownerElementsContainer; |
| | 26 | | [SerializeField] internal Button_OnPointerDown viewAllButton; |
| | 27 | | [SerializeField] private ShowHideAnimator showHideAnimator; |
| | 28 | | [SerializeField] private UIHoverCallback hoverArea; |
| | 29 | |
|
| | 30 | | public event Action OnViewAllPressed; |
| | 31 | | public event Action OnFocusLost; |
| | 32 | | public event Action OnFocus; |
| | 33 | |
|
| | 34 | | private Coroutine hideRoutine; |
| | 35 | |
|
| | 36 | | void IOwnersTooltipView.Show() |
| | 37 | | { |
| 1 | 38 | | if (hideRoutine != null) |
| 0 | 39 | | StopCoroutine(hideRoutine); |
| | 40 | |
|
| 1 | 41 | | gameObject.SetActive(true); |
| 1 | 42 | | showHideAnimator.Show(); |
| 1 | 43 | | } |
| | 44 | |
|
| | 45 | | void IOwnersTooltipView.Hide(bool instant) |
| | 46 | | { |
| 2 | 47 | | if (instant) |
| | 48 | | { |
| 2 | 49 | | gameObject.SetActive(false); |
| 2 | 50 | | } |
| | 51 | | else |
| | 52 | | { |
| 0 | 53 | | hideRoutine = StartCoroutine(HideRoutine()); |
| | 54 | | } |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | void IOwnersTooltipView.KeepOnScreen() |
| | 58 | | { |
| 0 | 59 | | if (hideRoutine != null) |
| 0 | 60 | | StopCoroutine(hideRoutine); |
| | 61 | |
|
| 0 | 62 | | showHideAnimator.Show(true); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | void IOwnersTooltipView.SetElements(List<IOwnerInfoElement> elements) |
| | 66 | | { |
| 24 | 67 | | for (int i = 0; i < elements.Count && i < MAX_ELEMENTS; i++) |
| | 68 | | { |
| 10 | 69 | | elements[i].SetParent(ownerElementsContainer); |
| 10 | 70 | | elements[i].SetAddressLength(ADDRESS_MAX_CHARS); |
| 10 | 71 | | elements[i].SetColorIndex(i); |
| 10 | 72 | | elements[i].SetConfig(ELEMENT_FONT_SIZE, ELEMENT_HORIZONTAL_SPACING); |
| 10 | 73 | | elements[i].SetActive(true); |
| | 74 | | } |
| 2 | 75 | | viewAllButton.gameObject.SetActive(elements.Count > MAX_ELEMENTS); |
| 2 | 76 | | } |
| | 77 | |
|
| 3 | 78 | | bool IOwnersTooltipView.IsActive() { return gameObject.activeSelf; } |
| | 79 | |
|
| | 80 | | private void Awake() |
| | 81 | | { |
| 7 | 82 | | viewAllButton.onPointerDown += ViewAllPressed; |
| 7 | 83 | | hoverArea.OnPointerEnter += OnPointerEnter; |
| 7 | 84 | | hoverArea.OnPointerExit += OnPointerExit; |
| 7 | 85 | | } |
| | 86 | |
|
| | 87 | | private void OnDestroy() |
| | 88 | | { |
| 7 | 89 | | viewAllButton.onPointerDown -= ViewAllPressed; |
| 7 | 90 | | hoverArea.OnPointerEnter -= OnPointerEnter; |
| 7 | 91 | | hoverArea.OnPointerExit -= OnPointerExit; |
| 7 | 92 | | } |
| | 93 | |
|
| 0 | 94 | | private void ViewAllPressed() { OnViewAllPressed?.Invoke(); } |
| | 95 | |
|
| 0 | 96 | | private void OnPointerEnter() { OnFocus?.Invoke(); } |
| | 97 | |
|
| 0 | 98 | | private void OnPointerExit() { OnFocusLost?.Invoke(); } |
| | 99 | |
|
| | 100 | | IEnumerator HideRoutine() |
| | 101 | | { |
| 0 | 102 | | yield return new WaitForSeconds(HIDE_DELAY); |
| 0 | 103 | | showHideAnimator.Hide(); |
| 0 | 104 | | hideRoutine = null; |
| 0 | 105 | | } |
| | 106 | | } |