412 lines
9.6 KiB
C#
412 lines
9.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Crosstales.Radio.Model;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Crosstales.Radio.Demo
|
|
{
|
|
[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 GUIRadioplayer : MonoBehaviour
|
|
{
|
|
[Header("Settings")]
|
|
[Tooltip("'RadioManager' from the scene.")]
|
|
public RadioManager Manager;
|
|
|
|
[Tooltip("Prefab for the radio list.")]
|
|
public GameObject ItemPrefab;
|
|
|
|
[Header("UI Objects")]
|
|
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;
|
|
|
|
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();
|
|
|
|
public void Start()
|
|
{
|
|
if (Manager != null)
|
|
{
|
|
Manager.OnProviderReady += onProviderReady;
|
|
if (StationCounter != null)
|
|
{
|
|
StationCounter.text = "Loading...";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("'Manager' is null!");
|
|
if (StationCounter != null)
|
|
{
|
|
StationCounter.text = "Error";
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
if (OrderPanel != null && Time.frameCount % 10 == 0)
|
|
{
|
|
OrderPanel.SetActive(isSorting);
|
|
}
|
|
}
|
|
|
|
public void OnDestroy()
|
|
{
|
|
if (Manager != null)
|
|
{
|
|
Manager.OnProviderReady -= onProviderReady;
|
|
}
|
|
}
|
|
|
|
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 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();
|
|
}
|
|
|
|
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());
|
|
}
|
|
}
|
|
|
|
private void onProviderReady()
|
|
{
|
|
StartCoroutine(buildRadioPlayerList());
|
|
}
|
|
|
|
private IEnumerator buildRadioPlayerList()
|
|
{
|
|
if (isSorting || !(Manager != null) || !(ItemPrefab != null) || !(Scroll != null))
|
|
{
|
|
yield break;
|
|
}
|
|
isSorting = true;
|
|
RectTransform rowRectTransform = ItemPrefab.GetComponent<RectTransform>();
|
|
RectTransform containerRectTransform = Target.GetComponent<RectTransform>();
|
|
Vector2 modifierVector = Vector2.zero;
|
|
for (int num = Target.transform.childCount - 1; num >= 0; num--)
|
|
{
|
|
Transform child = Target.transform.GetChild(num);
|
|
child.SetParent(null);
|
|
Object.Destroy(child.gameObject);
|
|
}
|
|
yield return null;
|
|
List<RadioPlayer> items = (orderByNameDesc ? Manager.PlayersByName(true, filter) : (orderByUrl ? Manager.PlayersByURL(false, filter) : (orderByUrlDesc ? Manager.PlayersByURL(true, filter) : (orderByFormat ? Manager.PlayersByFormat(false, filter) : (orderByFormatDesc ? Manager.PlayersByFormat(true, filter) : (orderByStation ? Manager.PlayersByStation(false, filter) : (orderByStationDesc ? Manager.PlayersByStation(true, filter) : (orderByBitrate ? Manager.PlayersByBitrate(false, filter) : (orderByBitrateDesc ? Manager.PlayersByBitrate(true, filter) : (orderByGenre ? Manager.PlayersByGenres(false, filter) : (orderByGenreDesc ? Manager.PlayersByGenres(true, filter) : (orderByRating ? Manager.PlayersByRating(false, filter) : ((!orderByRatingDesc) ? Manager.PlayersByName(false, filter) : Manager.PlayersByRating(true, filter))))))))))))));
|
|
StationCounter.text = "Stations: " + 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 ii = 0; ii < items.Count; ii++)
|
|
{
|
|
if (ii % ColumnCount == 0)
|
|
{
|
|
jj++;
|
|
}
|
|
GameObject newItem = Object.Instantiate(ItemPrefab);
|
|
newItem.name = Target.name + " item at (" + ii + "," + jj + ")";
|
|
newItem.transform.SetParent(Target.transform);
|
|
newItem.transform.localScale = Vector3.one;
|
|
if (ii % 2 == 0)
|
|
{
|
|
newItem.GetComponent<Image>().color = OddColor;
|
|
}
|
|
newItem.GetComponent<GUIRadioStatic>().Player = items[ii];
|
|
RectTransform rectTransform = newItem.GetComponent<RectTransform>();
|
|
modifierVector.Set(middleHeight + (width + SpaceWidth.x) * (float)(ii % 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;
|
|
if (ii % 13 == 0)
|
|
{
|
|
Scroll.value = 1f;
|
|
yield return null;
|
|
}
|
|
}
|
|
isSorting = false;
|
|
Scroll.value = 1f;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|