| | 1 | | using DCL; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.Serialization; |
| | 5 | | using UnityEngine.UI; |
| | 6 | |
|
| | 7 | | public class TaskbarButton : MonoBehaviour |
| | 8 | | { |
| | 9 | | [FormerlySerializedAs("openButton")] |
| | 10 | | public Button toggleButton; |
| | 11 | |
|
| | 12 | | public GameObject lineOffIndicator; |
| | 13 | | public ShowHideAnimator lineOnIndicator; |
| | 14 | | public Image iconImage; |
| | 15 | | public Color notInteractableColor; |
| | 16 | | public List<AppMode> compatibleModes; |
| | 17 | | public GameObject firstTimeLabelIndicator; |
| | 18 | |
|
| | 19 | | [Header("Transition")] |
| | 20 | | [SerializeField] private Sprite onImage; |
| | 21 | | [SerializeField] private Sprite offImage; |
| | 22 | |
|
| | 23 | | public event System.Action<TaskbarButton> OnToggleOn; |
| | 24 | | public event System.Action<TaskbarButton> OnToggleOff; |
| | 25 | |
|
| 30 | 26 | | public bool toggledOn { get; private set; } = true; |
| | 27 | |
|
| | 28 | | private Color originalIconColor; |
| | 29 | |
|
| | 30 | | public void Initialize() |
| | 31 | | { |
| 20 | 32 | | toggleButton.onClick.RemoveAllListeners(); |
| 20 | 33 | | toggleButton.onClick.AddListener(OnToggleButtonClick); |
| 20 | 34 | | SetToggleState(false, useCallback: false); |
| | 35 | |
|
| 20 | 36 | | if (iconImage != null) |
| 20 | 37 | | originalIconColor = iconImage.color; |
| | 38 | |
|
| 20 | 39 | | DataStore.i.common.appMode.OnChange += AppMode_OnChange; |
| 20 | 40 | | AppMode_OnChange(DataStore.i.common.appMode.Get(), AppMode.DEFAULT); |
| 20 | 41 | | } |
| | 42 | |
|
| 40 | 43 | | private void OnDestroy() { DataStore.i.common.appMode.OnChange -= AppMode_OnChange; } |
| | 44 | |
|
| | 45 | | private void AppMode_OnChange(AppMode currentMode, AppMode previousMode) |
| | 46 | | { |
| 20 | 47 | | bool isCompatible = compatibleModes.Contains(currentMode); |
| | 48 | |
|
| 20 | 49 | | SetInteractable(isCompatible); |
| | 50 | |
|
| 20 | 51 | | if (!isCompatible) |
| 0 | 52 | | SetToggleState(false); |
| 20 | 53 | | } |
| | 54 | |
|
| 8 | 55 | | private void OnToggleButtonClick() { SetToggleState(!toggledOn); } |
| | 56 | |
|
| | 57 | | public void SetToggleState(bool on, bool useCallback = true) |
| | 58 | | { |
| 101 | 59 | | if (toggledOn == on) return; |
| | 60 | |
|
| 25 | 61 | | if (on && firstTimeLabelIndicator != null) |
| 0 | 62 | | firstTimeLabelIndicator.SetActive(false); |
| | 63 | |
|
| 25 | 64 | | SetLineIndicator(on); |
| 25 | 65 | | SwapSprite(on); |
| 25 | 66 | | toggledOn = on; |
| | 67 | |
|
| 25 | 68 | | if (useCallback) |
| | 69 | | { |
| 5 | 70 | | if (on) |
| 3 | 71 | | OnToggleOn?.Invoke(this); |
| | 72 | | else |
| 2 | 73 | | OnToggleOff?.Invoke(this); |
| | 74 | | } |
| 22 | 75 | | } |
| | 76 | |
|
| 25 | 77 | | private void SwapSprite(bool on) => iconImage.sprite = on ? onImage : offImage; |
| | 78 | |
|
| | 79 | | private void SetLineIndicator(bool on) |
| | 80 | | { |
| 25 | 81 | | if (lineOnIndicator != null) |
| | 82 | | { |
| 25 | 83 | | if (on) |
| 3 | 84 | | lineOnIndicator.Show(); |
| | 85 | | else |
| 22 | 86 | | lineOnIndicator.Hide(); |
| | 87 | | } |
| | 88 | |
|
| 25 | 89 | | if (lineOffIndicator != null) |
| 0 | 90 | | lineOffIndicator.SetActive(!on); |
| 25 | 91 | | } |
| | 92 | |
|
| | 93 | | private void SetInteractable(bool isInteractable) |
| | 94 | | { |
| 20 | 95 | | toggleButton.interactable = isInteractable; |
| | 96 | |
|
| 20 | 97 | | if (iconImage != null) |
| 20 | 98 | | iconImage.color = isInteractable ? originalIconColor : notInteractableColor; |
| 20 | 99 | | } |
| | 100 | | } |