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