Files
2026-02-21 16:45:37 +08:00

212 lines
4.3 KiB
C#

using BitStrap;
using Crosstales.Radio;
using Crosstales.Radio.Model;
using Crosstales.Radio.Provider;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class RadioManager : MonoBehaviour
{
private static RadioManager instance;
[ReadOnly]
public bool isPlaying;
[ReadOnly]
public bool isSetToPlay;
public SimplePlayer simplePlayer;
public RadioPlayer radioPlayer;
public RadioProviderUser radioProviderUser;
[HideInInspector]
public SoundSettings soundSettings;
public float volumeFactor = 0.4f;
[Header("GUI")]
public RadioWindow radioWindow;
public Button playButton;
public Button pauseButton;
public Button randomButton;
public Button topPlayButton;
public Button topPauseButton;
public Button topRandomButton;
public Text radioText;
public Text radioNameText;
public static RadioManager Instance
{
get
{
return instance;
}
}
private RadioManager()
{
}
private void Awake()
{
if (!(playButton == null))
{
if (instance == null)
{
instance = this;
simplePlayer.OnErrorInfo += OnErrorInfo;
}
else
{
Object.Destroy(base.gameObject);
}
}
}
private void Start()
{
if (!(playButton == null))
{
soundSettings = GlobalSettings.Instance.soundSettings;
radioNameText.text = string.Empty;
Stop();
}
}
private void Update()
{
if (isPlaying)
{
simplePlayer.Player.Source.volume = soundSettings.GetVolume(SoundSettings.SoundCategory.MUSIC) * volumeFactor;
}
}
[Button]
public void Play(bool scrollTo = true)
{
if (simplePlayer.RadioStation == null || simplePlayer.RadioStation.Name == null)
{
Debug.LogError("simplePlayer.RadioStation == null; check Radios_user.txt");
return;
}
simplePlayer.Play();
isPlaying = true;
isSetToPlay = true;
playButton.gameObject.SetActive(!isPlaying);
pauseButton.gameObject.SetActive(isPlaying);
randomButton.gameObject.SetActive(isPlaying);
topPlayButton.gameObject.SetActive(!isPlaying);
topPauseButton.gameObject.SetActive(isPlaying);
topRandomButton.gameObject.SetActive(isPlaying);
radioNameText.text = simplePlayer.RadioStation.Name;
radioWindow.SelectCurrentStation();
if (scrollTo)
{
radioWindow.ScrollTo(radioWindow.GetCurrentStationId());
}
if (SceneManagerHelper.ActiveSceneName == "MainMenu")
{
AudioController.PauseMusic();
}
EventSystem.current.SetSelectedGameObject(topPauseButton.gameObject);
}
[Button]
public void Stop()
{
simplePlayer.Stop();
isPlaying = false;
isSetToPlay = false;
playButton.gameObject.SetActive(!isPlaying);
pauseButton.gameObject.SetActive(isPlaying);
randomButton.gameObject.SetActive(isPlaying);
topPlayButton.gameObject.SetActive(!isPlaying);
topPauseButton.gameObject.SetActive(isPlaying);
topRandomButton.gameObject.SetActive(isPlaying);
radioNameText.text = Utilities.GetTranslation("GUI/ONLINE_RADIO");
radioWindow.StopAllStations();
if (SceneManagerHelper.ActiveSceneName == "MainMenu")
{
AudioController.UnpauseMusic();
}
EventSystem.current.SetSelectedGameObject(topPlayButton.gameObject);
}
[Button]
public void Toggle()
{
if (isPlaying)
{
Stop();
}
else
{
Play();
}
}
public void SetStation(string stationName)
{
for (int i = 0; i < simplePlayer.Manager.Stations.Count; i++)
{
if (simplePlayer.Manager.Stations[i].Name == stationName)
{
radioPlayer.Station = simplePlayer.Manager.Stations[i];
break;
}
}
}
[Button]
public void NextRandomStation()
{
simplePlayer.Next(true);
radioNameText.text = simplePlayer.RadioStation.Name;
radioWindow.SelectCurrentStation();
radioWindow.ScrollTo(radioWindow.GetCurrentStationId());
}
public void ShowRadioWindow(bool show)
{
radioWindow.transform.parent.gameObject.SetActive(true);
if (show)
{
MenuManager.Instance.ChangeState(MenuManager.MenuState.RADIO);
}
else
{
MenuManager.Instance.SetPrevState();
}
MenuManager.Instance.ShowBackgroundObjects(!show);
}
public void ToggleRadioWindow()
{
ShowRadioWindow(MenuManager.Instance.currentState != MenuManager.MenuState.RADIO);
}
private void OnErrorInfo(RadioStation station, string info)
{
NextRandomStation();
}
public void LanguageChanged()
{
if (!isPlaying)
{
radioNameText.text = Utilities.GetTranslation("GUI/ONLINE_RADIO");
}
}
}