| | 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 | | [SerializeField] private BaseComponentView activatorComponent; |
| | 15 | |
|
| | 16 | | private bool mouseIsOver; |
| | 17 | |
|
| | 18 | | private RectTransform rectTransform; |
| 74 | 19 | | private RectTransform rect => rectTransform ??= GetComponent<RectTransform>(); |
| | 20 | |
|
| | 21 | | public override void Awake() |
| | 22 | | { |
| 69 | 23 | | base.Awake(); |
| | 24 | |
|
| 69 | 25 | | if (activatorComponent == null) |
| 0 | 26 | | return; |
| | 27 | |
|
| 69 | 28 | | activatorComponent.onFocused += isFocused => |
| | 29 | | { |
| 70 | 30 | | if (isFocused) |
| 0 | 31 | | Show(); |
| | 32 | | else |
| 70 | 33 | | Hide(); |
| 70 | 34 | | }; |
| 69 | 35 | | } |
| | 36 | |
|
| | 37 | | public override void OnEnable() |
| | 38 | | { |
| 70 | 39 | | base.OnEnable(); |
| | 40 | |
|
| 70 | 41 | | if (EventSystem.current) |
| 0 | 42 | | EventSystem.current.SetSelectedGameObject(gameObject); |
| 70 | 43 | | } |
| | 44 | |
|
| | 45 | | public override void OnFocus() |
| | 46 | | { |
| 0 | 47 | | base.OnFocus(); |
| | 48 | |
|
| 0 | 49 | | if (EventSystem.current) |
| 0 | 50 | | EventSystem.current.SetSelectedGameObject(gameObject); |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | public override void OnLoseFocus() |
| | 54 | | { |
| 70 | 55 | | base.OnLoseFocus(); |
| | 56 | |
|
| 70 | 57 | | if (EventSystem.current) |
| 0 | 58 | | EventSystem.current.SetSelectedGameObject(gameObject); |
| 70 | 59 | | } |
| | 60 | |
|
| | 61 | | public void OnDeselect(BaseEventData eventData) |
| | 62 | | { |
| 0 | 63 | | if (!isFocused) |
| 0 | 64 | | Hide(); |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | public override void Show(bool instant = false) |
| | 68 | | { |
| 0 | 69 | | gameObject.SetActive(true); |
| 0 | 70 | | base.Show(instant); |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | public override void RefreshControl() |
| | 74 | | { |
| 37 | 75 | | SetText(model.message); |
| 37 | 76 | | SetAutoAdaptModeActive(model.autoAdaptContainerSize); |
| 37 | 77 | | AdjustTooltipSizeToText(); |
| | 78 | |
|
| | 79 | | void AdjustTooltipSizeToText() |
| | 80 | | { |
| 37 | 81 | | if (!model.autoAdaptContainerSize) |
| 0 | 82 | | return; |
| | 83 | |
|
| 37 | 84 | | rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, text.preferredWidth + (OFFSET * 2)); |
| 37 | 85 | | rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, text.preferredHeight + (OFFSET * 2)); |
| 37 | 86 | | } |
| 37 | 87 | | } |
| | 88 | |
|
| | 89 | | private void SetText(string newText) |
| | 90 | | { |
| 37 | 91 | | model.message = newText; |
| | 92 | |
|
| 37 | 93 | | if (text) |
| 37 | 94 | | text.text = newText; |
| 37 | 95 | | } |
| | 96 | |
|
| | 97 | | private void SetAutoAdaptModeActive(bool isActive) => |
| 37 | 98 | | model.autoAdaptContainerSize = isActive; |
| | 99 | | } |
| | 100 | | } |