| | 1 | | using System; |
| | 2 | | using System.Linq; |
| | 3 | | using TMPro; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.EventSystems; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | internal class LeftMenuButtonToggleView : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler |
| | 9 | | { |
| | 10 | | public static event Action<LeftMenuButtonToggleView> OnToggleOn; |
| | 11 | |
|
| | 12 | | [Header("Action")] |
| | 13 | | [SerializeField] public SectionId openSection; |
| | 14 | |
|
| | 15 | | [Header("Settings")] |
| | 16 | | [SerializeField] private Color colorBackgroundDefault; |
| | 17 | | [SerializeField] private Color colorBackgroundSelected; |
| | 18 | | [SerializeField] private Color colorTextDefault; |
| | 19 | | [SerializeField] private Color colorTextSelected; |
| | 20 | |
|
| | 21 | | [Header("References")] |
| | 22 | | [SerializeField] private Image imageBackground; |
| | 23 | | [SerializeField] private TextMeshProUGUI text; |
| | 24 | |
|
| | 25 | | public bool isOn |
| | 26 | | { |
| | 27 | | set |
| | 28 | | { |
| 0 | 29 | | if (isToggleOn == value) |
| 0 | 30 | | return; |
| | 31 | |
|
| 0 | 32 | | SetIsOnWithoutNotify(value); |
| | 33 | |
|
| 0 | 34 | | if (value) |
| | 35 | | { |
| 0 | 36 | | OnToggleOn?.Invoke(this); |
| | 37 | | } |
| 0 | 38 | | } |
| 0 | 39 | | get { return isToggleOn; } |
| | 40 | | } |
| | 41 | |
|
| | 42 | | private bool isToggleOn = false; |
| | 43 | | private bool isSetup = false; |
| | 44 | |
|
| | 45 | | public void Setup() |
| | 46 | | { |
| 144 | 47 | | if (isSetup) |
| 0 | 48 | | return; |
| | 49 | |
|
| 144 | 50 | | isSetup = true; |
| 144 | 51 | | OnToggleOn += OnReceiveToggleOn; |
| 144 | 52 | | } |
| | 53 | |
|
| | 54 | | public void SetIsOnWithoutNotify(bool value) |
| | 55 | | { |
| 27 | 56 | | isToggleOn = value; |
| | 57 | |
|
| 27 | 58 | | if (isToggleOn) |
| | 59 | | { |
| 5 | 60 | | SetSelectColor(); |
| 5 | 61 | | } |
| | 62 | | else |
| | 63 | | { |
| 22 | 64 | | SetDefaultColor(); |
| | 65 | | } |
| 22 | 66 | | } |
| | 67 | |
|
| 60 | 68 | | private void OnDestroy() { OnToggleOn -= OnReceiveToggleOn; } |
| | 69 | |
|
| | 70 | | void IPointerEnterHandler.OnPointerEnter(PointerEventData eventData) |
| | 71 | | { |
| 0 | 72 | | if (isOn) |
| 0 | 73 | | return; |
| | 74 | |
|
| 0 | 75 | | SetSelectColor(); |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | void IPointerExitHandler.OnPointerExit(PointerEventData eventData) |
| | 79 | | { |
| 0 | 80 | | if (isOn) |
| 0 | 81 | | return; |
| | 82 | |
|
| 0 | 83 | | SetDefaultColor(); |
| 0 | 84 | | } |
| | 85 | |
|
| | 86 | | void IPointerClickHandler.OnPointerClick(PointerEventData eventData) |
| | 87 | | { |
| 0 | 88 | | if (isOn) |
| 0 | 89 | | return; |
| | 90 | |
|
| 0 | 91 | | isOn = true; |
| 0 | 92 | | } |
| | 93 | |
|
| | 94 | | private void SetSelectColor() |
| | 95 | | { |
| 5 | 96 | | imageBackground.color = colorBackgroundSelected; |
| 5 | 97 | | text.color = colorTextSelected; |
| 5 | 98 | | } |
| | 99 | |
|
| | 100 | | private void SetDefaultColor() |
| | 101 | | { |
| 22 | 102 | | imageBackground.color = colorBackgroundDefault; |
| 22 | 103 | | text.color = colorTextDefault; |
| 22 | 104 | | } |
| | 105 | |
|
| | 106 | | private void OnReceiveToggleOn(LeftMenuButtonToggleView toggle) |
| | 107 | | { |
| 0 | 108 | | if (!isOn) |
| 0 | 109 | | return; |
| | 110 | |
|
| 0 | 111 | | if (toggle != this) |
| | 112 | | { |
| 0 | 113 | | isOn = false; |
| | 114 | | } |
| 0 | 115 | | } |
| | 116 | | } |