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