| | 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 | |
|
| | 22 | | [Header("References")] |
| | 23 | | [SerializeField] private Image imageBackground; |
| | 24 | | [SerializeField] private TextMeshProUGUI text; |
| | 25 | |
|
| | 26 | | public bool isOn |
| | 27 | | { |
| | 28 | | set |
| | 29 | | { |
| 0 | 30 | | if (isToggleOn == value) |
| 0 | 31 | | return; |
| | 32 | |
|
| 0 | 33 | | SetIsOnWithoutNotify(value); |
| | 34 | |
|
| 0 | 35 | | if (value) |
| | 36 | | { |
| 0 | 37 | | OnToggleOn?.Invoke(this); |
| | 38 | | } |
| 0 | 39 | | } |
| 0 | 40 | | get { return isToggleOn; } |
| | 41 | | } |
| | 42 | |
|
| | 43 | | private bool isToggleOn = false; |
| | 44 | | private bool isSetup = false; |
| | 45 | |
|
| | 46 | | public void Setup() |
| | 47 | | { |
| 153 | 48 | | if (isSetup) |
| 0 | 49 | | return; |
| | 50 | |
|
| 153 | 51 | | isSetup = true; |
| 153 | 52 | | OnToggleOn += OnReceiveToggleOn; |
| 153 | 53 | | } |
| | 54 | |
|
| | 55 | | public void SetIsOnWithoutNotify(bool value) |
| | 56 | | { |
| 18 | 57 | | isToggleOn = value; |
| | 58 | |
|
| 18 | 59 | | if (isToggleOn) |
| | 60 | | { |
| 3 | 61 | | SetSelectColor(); |
| 3 | 62 | | } |
| | 63 | | else |
| | 64 | | { |
| 15 | 65 | | SetDefaultColor(); |
| | 66 | | } |
| 15 | 67 | | } |
| | 68 | |
|
| 16 | 69 | | private void OnDestroy() { OnToggleOn -= OnReceiveToggleOn; } |
| | 70 | |
|
| | 71 | | void IPointerEnterHandler.OnPointerEnter(PointerEventData eventData) |
| | 72 | | { |
| 0 | 73 | | if (isOn) |
| 0 | 74 | | return; |
| | 75 | |
|
| 0 | 76 | | SetSelectColor(); |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | void IPointerExitHandler.OnPointerExit(PointerEventData eventData) |
| | 80 | | { |
| 0 | 81 | | if (isOn) |
| 0 | 82 | | return; |
| | 83 | |
|
| 0 | 84 | | SetDefaultColor(); |
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | void IPointerClickHandler.OnPointerClick(PointerEventData eventData) |
| | 88 | | { |
| 0 | 89 | | AudioScriptableObjects.buttonClick.Play(true); |
| | 90 | |
|
| 0 | 91 | | if (isOn) |
| 0 | 92 | | return; |
| | 93 | |
|
| 0 | 94 | | isOn = true; |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | private void SetSelectColor() |
| | 98 | | { |
| 3 | 99 | | imageBackground.color = colorBackgroundSelected; |
| 3 | 100 | | text.color = colorTextSelected; |
| 3 | 101 | | } |
| | 102 | |
|
| | 103 | | private void SetDefaultColor() |
| | 104 | | { |
| 15 | 105 | | imageBackground.color = colorBackgroundDefault; |
| 15 | 106 | | text.color = colorTextDefault; |
| 15 | 107 | | } |
| | 108 | |
|
| | 109 | | private void OnReceiveToggleOn(LeftMenuButtonToggleView toggle) |
| | 110 | | { |
| 0 | 111 | | if (!isOn) |
| 0 | 112 | | return; |
| | 113 | |
|
| 0 | 114 | | if (toggle != this) |
| | 115 | | { |
| 0 | 116 | | isOn = false; |
| | 117 | | } |
| 0 | 118 | | } |
| | 119 | | } |
| | 120 | | } |