| | 1 | | using System; |
| | 2 | | using System.Linq; |
| | 3 | | using TMPro; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.EventSystems; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | namespace DCL.Builder |
| | 9 | | { |
| | 10 | | internal class LeftMenuButtonToggleView : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHan |
| | 11 | | { |
| | 12 | | public static event Action<LeftMenuButtonToggleView> OnToggleOn; |
| | 13 | |
|
| | 14 | | [Header("Action")] |
| | 15 | | [SerializeField] public SectionId openSection; |
| | 16 | |
|
| | 17 | | [Header("Settings")] |
| | 18 | | [SerializeField] private Color colorBackgroundDefault; |
| | 19 | | [SerializeField] private Color colorBackgroundSelected; |
| | 20 | | [SerializeField] private Color colorTextDefault; |
| | 21 | | [SerializeField] private Color colorTextSelected; |
| | 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) |
| 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 | |
|
| | 47 | | public void Setup() |
| | 48 | | { |
| 153 | 49 | | if (isSetup) |
| 0 | 50 | | return; |
| | 51 | |
|
| 153 | 52 | | isSetup = true; |
| 153 | 53 | | OnToggleOn += OnReceiveToggleOn; |
| 153 | 54 | | } |
| | 55 | |
|
| | 56 | | public void SetIsOnWithoutNotify(bool value) |
| | 57 | | { |
| 18 | 58 | | isToggleOn = value; |
| | 59 | |
|
| 18 | 60 | | if (isToggleOn) |
| | 61 | | { |
| 3 | 62 | | SetSelectColor(); |
| 3 | 63 | | } |
| | 64 | | else |
| | 65 | | { |
| 15 | 66 | | SetDefaultColor(); |
| | 67 | | } |
| 15 | 68 | | } |
| | 69 | |
|
| 48 | 70 | | private void OnDestroy() { OnToggleOn -= OnReceiveToggleOn; } |
| | 71 | |
|
| | 72 | | void IPointerEnterHandler.OnPointerEnter(PointerEventData eventData) |
| | 73 | | { |
| 0 | 74 | | if (isOn) |
| 0 | 75 | | return; |
| | 76 | |
|
| 0 | 77 | | SetSelectColor(); |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | void IPointerExitHandler.OnPointerExit(PointerEventData eventData) |
| | 81 | | { |
| 0 | 82 | | if (isOn) |
| 0 | 83 | | return; |
| | 84 | |
|
| 0 | 85 | | SetDefaultColor(); |
| 0 | 86 | | } |
| | 87 | |
|
| | 88 | | void IPointerClickHandler.OnPointerClick(PointerEventData eventData) |
| | 89 | | { |
| 0 | 90 | | if (isOn) |
| 0 | 91 | | return; |
| | 92 | |
|
| 0 | 93 | | isOn = true; |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | private void SetSelectColor() |
| | 97 | | { |
| 3 | 98 | | imageBackground.color = colorBackgroundSelected; |
| 3 | 99 | | text.color = colorTextSelected; |
| 3 | 100 | | } |
| | 101 | |
|
| | 102 | | private void SetDefaultColor() |
| | 103 | | { |
| 15 | 104 | | imageBackground.color = colorBackgroundDefault; |
| 15 | 105 | | text.color = colorTextDefault; |
| 15 | 106 | | } |
| | 107 | |
|
| | 108 | | private void OnReceiveToggleOn(LeftMenuButtonToggleView toggle) |
| | 109 | | { |
| 0 | 110 | | if (!isOn) |
| 0 | 111 | | return; |
| | 112 | |
|
| 0 | 113 | | if (toggle != this) |
| | 114 | | { |
| 0 | 115 | | isOn = false; |
| | 116 | | } |
| 0 | 117 | | } |
| | 118 | | } |
| | 119 | | } |