| | 1 | | using DCL.Helpers; |
| | 2 | | using System; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | namespace UIComponents.CollapsableSortedList |
| | 7 | | { |
| | 8 | | public class CollapsableListToggleButton : BaseComponentView |
| | 9 | | { |
| | 10 | | [SerializeField] private bool toggleOnAwake; |
| | 11 | | [SerializeField] private Button toggleButton; |
| | 12 | | [SerializeField] private Transform toggleButtonIcon; |
| | 13 | | [SerializeField] private Image toggleImage; |
| | 14 | | [SerializeField] private RectTransform containerRectTransform; |
| | 15 | | [SerializeField] private CollapsableListToggleButtonModel model; |
| | 16 | | [SerializeField] private Color disabledColor; |
| | 17 | |
|
| | 18 | | public event Action<bool> OnToggled; |
| | 19 | |
|
| | 20 | | private Color originalColor; |
| | 21 | |
|
| | 22 | | public override void Awake() |
| | 23 | | { |
| 83 | 24 | | base.Awake(); |
| | 25 | |
|
| 83 | 26 | | originalColor = toggleImage?.color ?? Color.black; |
| 83 | 27 | | toggleButton.onClick.AddListener(Toggle); |
| | 28 | |
|
| 83 | 29 | | if (toggleOnAwake) |
| 0 | 30 | | Toggle(); |
| 83 | 31 | | } |
| | 32 | |
|
| | 33 | | public override void RefreshControl() |
| | 34 | | { |
| 0 | 35 | | Toggle(model.isToggled); |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | public void Toggle(bool toggled) |
| | 39 | | { |
| 4 | 40 | | containerRectTransform.gameObject.SetActive(toggled); |
| 4 | 41 | | var absScale = Mathf.Abs(toggleButtonIcon.localScale.y); |
| 4 | 42 | | var scale = toggled ? absScale : -absScale; |
| 4 | 43 | | toggleButtonIcon.localScale = new Vector3(toggleButtonIcon.localScale.x, scale, 1f); |
| 4 | 44 | | Utils.ForceRebuildLayoutImmediate(containerRectTransform); |
| 4 | 45 | | model.isToggled = toggled; |
| 4 | 46 | | OnToggled?.Invoke(toggled); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | public void SetInteractability(bool interactable) |
| | 50 | | { |
| 6 | 51 | | toggleButton.interactable = interactable; |
| 6 | 52 | | toggleImage.color = interactable ? originalColor : disabledColor; |
| 6 | 53 | | } |
| | 54 | |
|
| | 55 | | private void Toggle() |
| | 56 | | { |
| 0 | 57 | | Toggle(!containerRectTransform.gameObject.activeSelf); |
| 0 | 58 | | } |
| | 59 | | } |
| | 60 | | } |