| | 1 | | using System; |
| | 2 | | using TMPro; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | internal class PreviewMenuVisibilityToggleView : MonoBehaviour, IDisposable |
| | 7 | | { |
| | 8 | | [SerializeField] internal Color colorON; |
| | 9 | | [SerializeField] internal Color colorOFF; |
| | 10 | | [SerializeField] internal Sprite imageON; |
| | 11 | | [SerializeField] internal Sprite imageOFF; |
| | 12 | |
|
| | 13 | | [SerializeField] internal TextMeshProUGUI textReference; |
| | 14 | | [SerializeField] internal Image imageReference; |
| | 15 | | [SerializeField] internal Button buttonReference; |
| | 16 | |
|
| | 17 | | private Func<bool> isEnableFunc; |
| | 18 | | private Action<bool> onToggleAction; |
| | 19 | |
|
| | 20 | | private bool isDestroyed; |
| | 21 | |
|
| | 22 | | public void SetUp(string text, Func<bool> isEnableFunc, Action<bool> onToggleAction) |
| | 23 | | { |
| 11 | 24 | | textReference.text = text; |
| | 25 | |
|
| 11 | 26 | | this.isEnableFunc = isEnableFunc; |
| 11 | 27 | | this.onToggleAction = onToggleAction; |
| | 28 | |
|
| 11 | 29 | | bool isEnable = isEnableFunc?.Invoke() ?? false; |
| 11 | 30 | | SetToggle(isEnable); |
| 11 | 31 | | } |
| | 32 | |
|
| | 33 | | public void Dispose() |
| | 34 | | { |
| 10 | 35 | | if (!isDestroyed) |
| | 36 | | { |
| 10 | 37 | | Destroy(gameObject); |
| | 38 | | } |
| 10 | 39 | | } |
| | 40 | |
|
| | 41 | | private void Awake() |
| | 42 | | { |
| 11 | 43 | | buttonReference.onClick.AddListener(() => |
| | 44 | | { |
| 3 | 45 | | bool isEnable = isEnableFunc?.Invoke() ?? false; |
| 3 | 46 | | if (onToggleAction != null) |
| | 47 | | { |
| 3 | 48 | | onToggleAction.Invoke(!isEnable); |
| 3 | 49 | | SetToggle(!isEnable); |
| | 50 | | } |
| 3 | 51 | | }); |
| 11 | 52 | | } |
| | 53 | |
|
| | 54 | | private void OnDestroy() |
| | 55 | | { |
| 11 | 56 | | isDestroyed = true; |
| 11 | 57 | | } |
| | 58 | |
|
| | 59 | | private void OnEnable() |
| | 60 | | { |
| 11 | 61 | | bool isEnable = isEnableFunc?.Invoke() ?? false; |
| 11 | 62 | | SetToggle(isEnable); |
| 11 | 63 | | } |
| | 64 | |
|
| | 65 | | private void SetToggle(bool isOn) |
| | 66 | | { |
| 25 | 67 | | Color color = isOn ? colorON : colorOFF; |
| 25 | 68 | | imageReference.sprite = isOn ? imageON : imageOFF; |
| 25 | 69 | | imageReference.color = color; |
| 25 | 70 | | textReference.color = color; |
| 25 | 71 | | } |
| | 72 | | } |