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