| | 1 | | using TMPro; |
| | 2 | | using UIComponents.Scripts.Components.Tooltip; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.EventSystems; |
| | 5 | |
|
| | 6 | | namespace UIComponents.Scripts.Components |
| | 7 | | { |
| | 8 | | [SelectionBase, DisallowMultipleComponent] |
| | 9 | | public class TooltipComponentView : BaseComponentView<TooltipComponentModel>, IDeselectHandler |
| | 10 | | { |
| | 11 | | private const int OFFSET = 16; |
| | 12 | |
|
| | 13 | | [SerializeField] private TMP_Text text; |
| | 14 | |
|
| | 15 | | private bool mouseIsOver; |
| | 16 | |
|
| | 17 | | private RectTransform rectTransform; |
| 70 | 18 | | private RectTransform rect => rectTransform ??= GetComponent<RectTransform>(); |
| | 19 | |
|
| | 20 | | public override void OnEnable() |
| | 21 | | { |
| 0 | 22 | | base.OnEnable(); |
| 0 | 23 | | EventSystem.current.SetSelectedGameObject(gameObject); |
| 0 | 24 | | } |
| | 25 | |
|
| | 26 | | public override void OnFocus() |
| | 27 | | { |
| 0 | 28 | | base.OnFocus(); |
| 0 | 29 | | EventSystem.current.SetSelectedGameObject(gameObject); |
| 0 | 30 | | } |
| | 31 | |
|
| | 32 | | public override void OnLoseFocus() |
| | 33 | | { |
| 0 | 34 | | base.OnLoseFocus(); |
| 0 | 35 | | EventSystem.current.SetSelectedGameObject(gameObject); |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | public void OnDeselect(BaseEventData eventData) |
| | 39 | | { |
| 0 | 40 | | if (!isFocused) |
| 0 | 41 | | Hide(); |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | public override void Show(bool instant = false) |
| | 45 | | { |
| 0 | 46 | | gameObject.SetActive(true); |
| 0 | 47 | | base.Show(instant); |
| 0 | 48 | | } |
| | 49 | |
|
| | 50 | | public override void RefreshControl() |
| | 51 | | { |
| 35 | 52 | | SetText(model.message); |
| 35 | 53 | | AdjustTooltipSizeToText(); |
| | 54 | |
|
| | 55 | | void AdjustTooltipSizeToText() |
| | 56 | | { |
| 35 | 57 | | rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, text.preferredWidth + (OFFSET * 2)); |
| 35 | 58 | | rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, text.preferredHeight + (OFFSET * 2)); |
| 35 | 59 | | } |
| 35 | 60 | | } |
| | 61 | |
|
| | 62 | | private void SetText(string newText) |
| | 63 | | { |
| 35 | 64 | | model.message = newText; |
| | 65 | |
|
| 35 | 66 | | if (text != null) |
| 35 | 67 | | text.text = newText; |
| 35 | 68 | | } |
| | 69 | | } |
| | 70 | | } |