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