Skip to main content

Catalogue browser

A media catalogue over an ST Data Table with about 40,000 rows. It shows pages of 8 items, sorts by title or creator, and searches by title and creator from two input fields. StageTime does the sorting and searching, so the script stays small and fast. An ST String Downloader on the same object loads named lists into a dropdown (see UI and interaction). The table, the downloader and the script sit on one GameObject, because their events reach scripts on the same object. Each row's + button calls the request queue, which reads that row's item with RowOnPage.

SongBrowser.cs
using StageTime.SDK;
using TMPro;
using UnityEngine;

/// <summary>
/// A catalogue browser over an <see cref="STDataTable"/> on the same GameObject (a tab-separated catalogue:
/// title rank, creator rank, creator, title, hidden). Pages of 8 rows sorted by title or creator. Title and creator search
/// fields filter the list (StageTime does the search, so the 40,000-row list never enters the script). An
/// <see cref="STStringDownloader"/> on the same GameObject loads playlists.txt (a row of list names, then one
/// column of item numbers per list) into a dropdown. Clicking a row selects that item (see <see cref="selectedRow"/>);
/// each row's + button (<see cref="addButtons"/>, wired to <see cref="SongQueue"/>) queues it straight away.
/// </summary>
public class SongBrowser : StageTimeBehaviour
{
private const int ArtistColumn = 2;
private const int TitleColumn = 3;
private const int HiddenColumn = 4;
private const int PageSize = 8;

public TMP_Text status;
public TMP_Text[] rows;
public TMP_InputField titleField;
public TMP_InputField artistField;
public TMP_Dropdown playlistDropdown;
/// <summary>One + button per row, hidden on empty rows.</summary>
public GameObject[] addButtons;

/// <summary>The table row of the last clicked item, or -1.</summary>
[HideInInspector] public int selectedRow = -1;

private STDataTable items;
private STStringDownloader playlists;
private int[] results = new int[350];
private int resultCount;
private string filter = ""; // "" = the whole list, otherwise what the results are
private int page;
private string[] playlistLines;
private string playlistInfo = "loading...";

private void Start()
{
items = GetComponent<STDataTable>();
playlists = GetComponent<STStringDownloader>();
Show();
}

public override void OnDataLoaded()
{
items.SortBy(TitleColumn);
Show();
}

public override void OnDataError() => Show();

// playlists.txt: line 0 holds the names, each line below one item number per list (column).
public override void OnStringLoadSuccess()
{
playlistLines = playlists.result.Split("\n", System.StringSplitOptions.RemoveEmptyEntries);
string[] names = playlistLines.Length > 0 ? playlistLines[0].Trim().Split("\t", System.StringSplitOptions.None) : new string[0];
string[] options = new string[names.Length + 1];
options[0] = "All items";
for (int i = 0; i < names.Length; i++) options[i + 1] = names[i].Trim();
DropdownOptions.Set(playlistDropdown, options);
playlistInfo = names.Length + " playlists";
Show();
}

public override void OnStringLoadError()
{
playlistInfo = "error: " + playlists.error;
Show();
}

/// <summary>Title or artist field: On End Edit / On Submit.</summary>
public void SearchEntered()
{
string title = titleField.text.Trim();
string artist = artistField.text.Trim();
playlistDropdown.SetValueWithoutNotify(0);
if (title.Length == 0 && artist.Length == 0)
{
ClearSearch();
return;
}
resultCount = items.SearchBoth(TitleColumn, title, ArtistColumn, artist, results);
filter = "search: " + items.lastMatchCount + " matches";
page = 0;
Show();
}

/// <summary>Playlist dropdown: On Value Changed.</summary>
public void PlaylistChanged()
{
int choice = playlistDropdown.value;
if (choice == 0 || playlistLines == null)
{
ClearSearch();
return;
}
titleField.SetTextWithoutNotify("");
artistField.SetTextWithoutNotify("");
int column = choice - 1;
resultCount = 0;
for (int i = 1; i < playlistLines.Length && resultCount < results.Length; i++)
{
string[] cells = playlistLines[i].Split("\t", System.StringSplitOptions.None);
if (column >= cells.Length) continue;
string cell = cells[column].Trim();
if (cell.Length > 0) results[resultCount++] = int.Parse(cell);
}
filter = DropdownOptions.Get(playlistDropdown, choice) + ": " + resultCount + " items";
page = 0;
Show();
}

public void SortByTitle() => Sort(TitleColumn);
public void SortByArtist() => Sort(ArtistColumn);

private void Sort(int column)
{
items.SortBy(column);
if (filter.StartsWith("search")) SearchEntered();
page = 0;
Show();
}

public void ClearSearch()
{
filter = "";
titleField.SetTextWithoutNotify("");
artistField.SetTextWithoutNotify("");
playlistDropdown.SetValueWithoutNotify(0);
page = 0;
Show();
}

public void NextPage()
{
if (page + 1 < PageCount()) page++;
Show();
}

public void PreviousPage()
{
if (page > 0) page--;
Show();
}

public void SelectRow0() => Select(0);
public void SelectRow1() => Select(1);
public void SelectRow2() => Select(2);
public void SelectRow3() => Select(3);
public void SelectRow4() => Select(4);
public void SelectRow5() => Select(5);
public void SelectRow6() => Select(6);
public void SelectRow7() => Select(7);

private void Select(int line)
{
int row = RowAt(page * PageSize + line);
if (row < 0) return;
selectedRow = row;
Show();
}

/// <summary>The table row on line <paramref name="line"/> (0-7) of the current page, or -1.</summary>
public int RowOnPage(int line) => RowAt(page * PageSize + line);

/// <summary>"Title - Creator" for a table row.</summary>
public string ItemName(int row) => items.GetString(row, TitleColumn) + " - " + items.GetString(row, ArtistColumn);

private int Total() => filter.Length > 0 ? resultCount : items.rowCount;

private int PageCount()
{
int total = Total();
return total == 0 ? 1 : (total + PageSize - 1) / PageSize;
}

/// <summary>The table row shown at <paramref name="position"/> (filtered or the whole list), or -1.</summary>
public int RowAt(int position)
{
if (position < 0 || position >= Total()) return -1;
return filter.Length > 0 ? results[position] : items.GetSortedRow(position);
}

private void Show()
{
if (status == null) return;
string state = items == null ? "-" : items.isLoaded ? items.rowCount + " items" : items.isLoading ? "loading..." : "error: " + items.error;
string mode = items != null && items.sortedColumn == ArtistColumn ? "creator" : "title";
status.text = state + " | sorted by " + mode + (filter.Length > 0 ? " | " + filter : "") + " | page " + (page + 1) + "/" + PageCount()
+ "\nPlaylists: " + playlistInfo + (selectedRow >= 0 ? " | selected: " + ItemName(selectedRow) : "");
for (int i = 0; i < rows.Length; i++)
{
int row = RowAt(page * PageSize + i);
if (i < addButtons.Length) addButtons[i].SetActive(row >= 0);
if (row < 0)
{
rows[i].text = "";
continue;
}
bool hidden = items.GetString(row, HiddenColumn).Trim() == "X";
string line = "#" + row + " " + ItemName(row);
rows[i].text = hidden ? "<alpha=#66>" + line : line;
}
}
}