Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added navigation buttons "Next section" and "Random song" #385

Merged
merged 3 commits into from
May 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 81 additions & 8 deletions Assets/Script/UI/MusicLibrary/SongSelection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ private set {
private float _scrollTimer = 0f;
private bool searchBoxShouldBeEnabled = false;

private List<char> songsFirstLetter;

private void Awake() {
refreshFlag = true;
Instance = this;
Expand Down Expand Up @@ -113,9 +115,14 @@ private void OnEnable() {
Back();
}),
new NavigationScheme.Entry(MenuAction.Shortcut1, "Search Artist", () => {
if (_songs[SelectedIndex] is SongViewType view) {
searchField.text = $"artist:{view.SongEntry.Artist}";
}
SearchByArtist();
}),
new NavigationScheme.Entry(MenuAction.Shortcut2, "Random song", () => {
ClearSearchBox();
SelectRandomSong();
}),
new NavigationScheme.Entry(MenuAction.Shortcut3, "Next section", () => {
SelectNextSection();
})
}, false));

Expand Down Expand Up @@ -221,11 +228,7 @@ public void UpdateSearch() {
"RANDOM SONG",
"Icon/Random",
() => {
// Get how many non-song things there are
int skip = _songs.Count - SongContainer.Songs.Count;

// Select random between all of the songs
SelectedIndex = Random.Range(skip, SongContainer.Songs.Count);
SelectRandomSong();
}
));
} else {
Expand Down Expand Up @@ -334,10 +337,23 @@ public void UpdateSearch() {
SelectedIndex = Mathf.Max(1, index);
}

SetFirstLetters();
UpdateSongViews();
UpdateScrollbar();
}

private void SetFirstLetters(){
songsFirstLetter =
_songs
.OfType<SongViewType>()
.Select(song => song.SongEntry.NameNoParenthesis)
.Where(name => !string.IsNullOrEmpty(name))
.Select(name => Char.ToUpper(name[0]))
.Distinct()
.OrderBy(ch => ch)
.ToList();
}

private static string RemoveDiacritics(string text) {
if (text == null) {
return null;
Expand Down Expand Up @@ -459,5 +475,62 @@ private void ClearSearchBox() {
searchField.text = "";
searchField.ActivateInputField();
}

private void SelectRandomSong(){
// Get how many non-song things there are
int skip = _songs.Count - SongContainer.Songs.Count;
// Select random between all of the songs
SelectedIndex = Random.Range(skip, SongContainer.Songs.Count);
}

private void SearchByArtist(){
if (_songs[SelectedIndex] is SongViewType view) {
searchField.text = $"artist:{view.SongEntry.Artist}";
}
}

private void SelectNextSection(){
if (_songs[_selectedIndex] is not SongViewType song) {
EliteAsian123 marked this conversation as resolved.
Show resolved Hide resolved
return;
}

int skip = Mathf.Max(1, _songs.Count - SongContainer.Songs.Count);
var nameWithoutParenthesis = song.SongEntry.NameNoParenthesis;
string nextCharacter = GetNextLetterOrNumber(nameWithoutParenthesis);

// If an error occurs no change is made
if(string.IsNullOrEmpty(nextCharacter)){
return;
}

var index = _songs.FindIndex(skip, song =>
song is SongViewType songType &&
songType.SongEntry.NameNoParenthesis.Substring(0, 1) == nextCharacter
);

SelectedIndex = index;
}

private string GetNextLetterOrNumber(string input){
if(string.IsNullOrEmpty(input)){
return null;
}

char firstCharacter = Char.ToUpper(input[0]);

int indexOfActualLetter = songsFirstLetter.FindIndex(letter => {
return letter == firstCharacter;
});

bool isLast = indexOfActualLetter == (songsFirstLetter.Count - 1);

if(isLast){
var firstCharacterInList = Char.ToString(songsFirstLetter[0]);
return firstCharacterInList;
}

var nextCharacter = Char.ToString(songsFirstLetter[indexOfActualLetter + 1]);
return nextCharacter;
}
}
}