using System.Collections; using System.Collections.Generic; using BitStrap; using Crosstales.Radio; using Crosstales.Radio.Model; using UnityEngine; using UnityEngine.UI; [HelpURLAttribute("https://www.crosstales.com/media/data/assets/radio/api/class_crosstales_1_1_radio_1_1_demo_1_1_g_u_i_radioplayer.html")] public class RadioWindow : MonoBehaviour { [Tooltip("'RadioManager' from the scene.")] [Header("Settings")] public Crosstales.Radio.RadioManager Manager; [Tooltip("Prefab for the radio list.")] public GameObject ItemPrefab; public List widgets = new List(); [Header("UI Objects")] public ScrollRect scrollRect; public GameObject Target; public GameObject OrderPanel; public Scrollbar Scroll; public int ColumnCount = 1; public Vector2 SpaceWidth = new Vector2(8f, 8f); public Vector2 SpaceHeight = new Vector2(8f, 8f); public Color32 EvenColor = new Color32(242, 236, 224, 128); public Color32 OddColor = new Color32(128, 128, 128, 128); public Text StationCounter; public Text songName; public Toggle favoriteToggle; [ReadOnly] public bool wasInitialized; private bool orderByNameDesc; private bool orderByNameStandard = true; private bool orderByStation; private bool orderByStationDesc; private bool orderByStationStandard = true; private bool orderByUrl; private bool orderByUrlDesc; private bool orderByUrlStandard = true; private bool orderByFormat; private bool orderByFormatDesc; private bool orderByFormatStandard; private bool orderByBitrate; private bool orderByBitrateDesc; private bool orderByBitrateStandard; private bool orderByGenre; private bool orderByGenreDesc; private bool orderByGenreStandard = true; private bool orderByRating; private bool orderByRatingDesc; private bool orderByRatingStandard; private bool isSorting; private RadioFilter filter = new RadioFilter(); private void OnEnable() { if (!GlobalSettings.Instance || !GlobalSettings.Instance.saveManager.isProfileLoaded) { base.transform.parent.gameObject.SetActive(false); return; } if (!wasInitialized) { StartCoroutine(buildRadioPlayerList()); } else { SelectCurrentStation(true); ScrollTo(GetCurrentStationId()); } Debug.Log("GetCurrentStationId " + GetCurrentStationId()); } private void OnDisable() { if ((bool)GlobalSettings.Instance && GlobalSettings.Instance.saveManager.isProfileLoaded) { GlobalSettings.Instance.soundSettings.Save(); RadioManager.Instance.radioProviderUser.Save(RadioManager.Instance.radioProviderUser.Entry.FinalPath); } } public void Start() { if (Manager != null) { Manager.OnProviderReady += onProviderReady; RadioManager.Instance.radioPlayer.OnBufferingProgressUpdate += onBufferingProgress; RadioManager.Instance.radioPlayer.OnErrorInfo += onErrorInfo; if (StationCounter != null) { StationCounter.text = Utilities.GetTranslation("GUI/LOADING") + "..."; } } else { Debug.LogError("'Manager' is null!"); if (StationCounter != null) { StationCounter.text = Utilities.GetTranslation("RADIO/ERROR"); } } } public void Update() { if (RadioManager.Instance.radioPlayer.isPlayback && RadioManager.Instance.radioPlayer.isBuffering) { songName.text = Utilities.GetTranslation("RADIO/BUFFERING") + ": " + RadioManager.Instance.radioPlayer.BufferProgress.ToString("0%"); } else if (RadioManager.Instance.radioPlayer.isPlayback) { songName.text = RadioManager.Instance.radioPlayer.RecordInfo.StreamTitle; } else { songName.text = string.Empty; } } public void OnDestroy() { if (Manager != null) { Manager.OnProviderReady -= onProviderReady; RadioManager.Instance.radioPlayer.OnBufferingProgressUpdate -= onBufferingProgress; RadioManager.Instance.radioPlayer.OnErrorInfo -= onErrorInfo; } } public void FilterName(string filter) { this.filter.Name = filter; OrderByName(); } public void FilterStation(string filter) { this.filter.Station = filter; OrderByStation(); } public void FilterUrl(string filter) { this.filter.Url = filter; OrderByUrl(); } public void FilterBitrateMin(string bitrate) { if (int.TryParse(bitrate, out filter.BitrateMin)) { OrderByBitrate(); } } public void FilterBitrateMax(string bitrate) { if (int.TryParse(bitrate, out filter.BitrateMax)) { OrderByBitrate(); } } public void FilterGenre(string filter) { this.filter.Genres = filter; OrderByGenre(); } public void FilterFavorite() { StartCoroutine(buildRadioPlayerList()); } public void FilterRatingMin(string rating) { if (float.TryParse(rating, out filter.RatingMin)) { OrderByRating(); } } public void FilterRatingMax(string rating) { if (float.TryParse(rating, out filter.RatingMax)) { OrderByRating(); } } public void FilterFormat(string filter) { this.filter.Format = filter; OrderByFormat(); } [Button] public void OrderByName() { if (!isSorting) { resetOrderBy(); if (orderByNameStandard) { orderByNameDesc = true; orderByNameStandard = false; } else { orderByNameDesc = false; orderByNameStandard = true; } StartCoroutine(buildRadioPlayerList()); } } public void OrderByStation() { if (!isSorting) { resetOrderBy(); if (orderByStationStandard) { orderByStation = true; orderByStationDesc = false; orderByStationStandard = false; } else { orderByStation = false; orderByStationDesc = true; orderByStationStandard = true; } StartCoroutine(buildRadioPlayerList()); } } public void OrderByUrl() { if (!isSorting) { resetOrderBy(); if (orderByUrlStandard) { orderByUrl = true; orderByUrlDesc = false; orderByUrlStandard = false; } else { orderByUrl = false; orderByUrlDesc = true; orderByUrlStandard = true; } StartCoroutine(buildRadioPlayerList()); } } public void OrderByFormat() { if (!isSorting) { resetOrderBy(); if (orderByFormatStandard) { orderByFormat = true; orderByFormatDesc = false; orderByFormatStandard = false; } else { orderByFormat = false; orderByFormatDesc = true; orderByFormatStandard = true; } StartCoroutine(buildRadioPlayerList()); } } public void OrderByBitrate() { if (!isSorting) { resetOrderBy(); if (orderByBitrateStandard) { orderByBitrate = true; orderByBitrateDesc = false; orderByBitrateStandard = false; } else { orderByBitrate = false; orderByBitrateDesc = true; orderByBitrateStandard = true; } StartCoroutine(buildRadioPlayerList()); } } public void OrderByGenre() { if (!isSorting) { resetOrderBy(); if (orderByGenreStandard) { orderByGenre = true; orderByGenreDesc = false; orderByGenreStandard = false; } else { orderByGenre = false; orderByGenreDesc = true; orderByGenreStandard = true; } StartCoroutine(buildRadioPlayerList()); } } public void OrderByRating() { if (!isSorting) { resetOrderBy(); if (orderByRatingStandard) { orderByRating = true; orderByRatingDesc = false; orderByRatingStandard = false; } else { orderByRating = false; orderByRatingDesc = true; orderByRatingStandard = true; } StartCoroutine(buildRadioPlayerList()); } } [Button] public void onProviderReady() { StartCoroutine(buildRadioPlayerList()); } private IEnumerator buildRadioPlayerList() { if (isSorting || !(Manager != null) || !(ItemPrefab != null) || !(Scroll != null)) { yield break; } isSorting = true; OrderPanel.SetActive(true); RectTransform rowRectTransform = ItemPrefab.GetComponent(); RectTransform containerRectTransform = Target.GetComponent(); Vector2 modifierVector = Vector2.zero; for (int i = 0; i < widgets.Count; i++) { Object.DestroyImmediate(widgets[i].gameObject); widgets[i] = null; } widgets.Clear(); yield return null; if (favoriteToggle.isOn) { filter.RatingMin = 6f; } else { filter.RatingMin = 0f; } List items = Manager.StationsByName(false, filter); StationCounter.text = Utilities.GetTranslation("RADIO/FILTER") + ": " + items.Count; float width = containerRectTransform.rect.width / (float)ColumnCount - (SpaceWidth.x + SpaceWidth.y) * (float)ColumnCount; float height = rowRectTransform.rect.height - (SpaceHeight.x + SpaceHeight.y); int rowCount = items.Count / ColumnCount; if (rowCount > 0 && items.Count % rowCount > 0) { rowCount++; } float scrollHeight = height * (float)rowCount; modifierVector.Set(containerRectTransform.offsetMin.x, (0f - scrollHeight) / 2f); containerRectTransform.offsetMin = modifierVector; modifierVector.Set(containerRectTransform.offsetMax.x, scrollHeight / 2f); containerRectTransform.offsetMax = modifierVector; int jj = 0; float middleHeight = (0f - containerRectTransform.rect.width) / 2f; float middleWidth = containerRectTransform.rect.height / 2f; for (int j = 0; j < items.Count; j++) { if (j % ColumnCount == 0) { jj++; } GameObject newItem = Object.Instantiate(ItemPrefab); newItem.transform.SetParent(Target.transform); newItem.transform.localScale = Vector3.one; RadioStationWidget component = newItem.GetComponent(); if (j % 2 == 0) { component.MainImage.color = OddColor; } else { component.MainImage.color = EvenColor; } component.NormalColor = component.MainImage.color; component.Player = RadioManager.Instance.radioPlayer; component.radioStation = items[j]; component.Initialize(); RectTransform rectTransform = newItem.GetComponent(); modifierVector.Set(middleHeight + (width + SpaceWidth.x) * (float)(j % ColumnCount) + SpaceWidth.x * (float)ColumnCount, middleWidth - height * (float)jj); rectTransform.offsetMin = modifierVector; modifierVector.Set(rectTransform.offsetMin.x + width, rectTransform.offsetMin.y + height); rectTransform.offsetMax = modifierVector; widgets.Add(component); } isSorting = false; OrderPanel.SetActive(false); ScrollTo(0); SelectCurrentStation(true); ScrollTo(GetCurrentStationId()); wasInitialized = true; } public void ScrollTo(int position) { float num = (float)position / (float)widgets.Count; if (position == -1) { num = 0f; } else if (position < 5) { num = 0f; } else if (position > widgets.Count - 7) { num = 1f; } scrollRect.verticalNormalizedPosition = 1f - num; } private void resetOrderBy() { orderByStation = false; orderByUrl = false; orderByFormat = false; orderByBitrate = false; orderByGenre = false; orderByRating = false; orderByNameDesc = false; orderByStationDesc = false; orderByUrlDesc = false; orderByFormatDesc = false; orderByBitrateDesc = false; orderByGenreDesc = false; orderByRatingDesc = false; } public void StopAllStations() { for (int i = 0; i < widgets.Count; i++) { widgets[i].SetState(false); } } public int GetCurrentStationId() { for (int i = 0; i < widgets.Count; i++) { if (widgets[i].radioStation == RadioManager.Instance.radioPlayer.Station) { return i; } } return -1; } public void SelectCurrentStation(bool additionalCheck = false) { for (int i = 0; i < widgets.Count; i++) { if (widgets[i].radioStation == RadioManager.Instance.radioPlayer.Station) { if (!additionalCheck || RadioManager.Instance.radioPlayer.isPlayback || RadioManager.Instance.radioPlayer.isBuffering) { widgets[i].SetState(true); } else { widgets[i].SetState(false); } widgets[i].SetSelection(true); } else { widgets[i].SetState(false); } } } private void onBufferingProgress(RadioStation station, float progress) { } private void onErrorInfo(RadioStation station, string info) { } }