Skip to content

Commit

Permalink
add UI feedback for reloading song list
Browse files Browse the repository at this point in the history
  • Loading branch information
Murph9 committed Aug 27, 2023
1 parent 005fc6e commit c763fd8
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 96 deletions.
2 changes: 1 addition & 1 deletion MainScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public override void _Ready()
}

private void LoadStartMenu() {
_startMenu = GD.Load<PackedScene>("res://StartMenu.tscn").Instantiate<StartMenu>();
_startMenu = GD.Load<PackedScene>("res://scenes/StartMenu.tscn").Instantiate<StartMenu>();
AddChild(_startMenu);

_startMenu.Closed += () => {
Expand Down
69 changes: 0 additions & 69 deletions StartMenu.tscn

This file was deleted.

18 changes: 11 additions & 7 deletions TabPlayer.Songs/Models/SongInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,20 @@ public class SongMetadata
public string Album { get; private set; }
public int? Year { get; private set; }
public float SongLength { get; private set; }
//TODO genre
public string Genre { get; private set; }

public SongMetadata(string name, string artist, string album, int? year, float songLength) : this(name, artist, album, year, songLength, null) {
}

[JsonConstructor]
public SongMetadata(string name, string artist, string album, int? year, float songLength)
public SongMetadata(string name, string artist, string album, int? year, float songLength, string genre)
{
this.Name = name;
this.Artist = artist;
this.Album = album;
this.Year = year;
this.SongLength = songLength;
Name = name;
Artist = artist;
Album = album;
Year = year;
SongLength = songLength;
Genre = genre;
}
}

Expand Down
24 changes: 11 additions & 13 deletions TabPlayer.Songs/SongFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,16 @@ public static DirectoryInfo GetOrCreateSongFolder(SongInfo info) {
return dir;
}

public static SongFileList GetSongFileList(Action<string> output, bool update = false) {
if (!update) {
var configFile = EnsureConfigExists();
public static SongFileList GetSongFileList() {
var configFile = EnsureConfigExists();

using var sr = new StreamReader(configFile);
string result = sr.ReadToEnd();
output("Reading existing song file from: " + result);

return JsonConvert.DeserializeObject<SongFileList>(result) ?? new SongFileList();
}
using var sr = new StreamReader(configFile);
string result = sr.ReadToEnd();

return JsonConvert.DeserializeObject<SongFileList>(result) ?? new SongFileList();
}

public static void UpdateSongList(Action<string> output) {
var songList = new SongFileList();
output("Loading all songs from: " + SONG_FOLDER);
var songDirs = Directory.EnumerateDirectories(SONG_FOLDER).Select(x => new DirectoryInfo(x)).ToList();
Expand All @@ -60,7 +59,7 @@ public static SongFileList GetSongFileList(Action<string> output, bool update =
if (file == null) continue;

var noteInfo = JsonConvert.DeserializeObject<SongInfo>(File.ReadAllText(file.FullName));
output($"Reading all songs {i}/{total}. Current: {noteInfo?.Metadata?.Name}");
output($"{i}/{total} Reading all songs, current: {noteInfo?.Metadata?.Name}");
var instruments = noteInfo.Instruments.Select(x => new SongFileInstrument(x.Name, x == noteInfo.MainInstrument, x.Config.Tuning, x.Notes.Count, x.Config.CapoFret)).ToArray();
var lyrics = noteInfo.Lyrics != null ? new SongFileLyrics(noteInfo.Lyrics.Lines?.Sum(x => x.Words?.Count) ?? 0) : null;
songList.Data.Add(new SongFile(songDir.Name, noteInfo.Metadata.Name,
Expand All @@ -71,12 +70,11 @@ public static SongFileList GetSongFileList(Action<string> output, bool update =
}
songList.Data.Sort((x, y) => x.SongName.CompareTo(y.SongName)); // default sort of name

output($"Completed {total}/{total}. Sorting");
output($"Completed {total}/{total}. Saving");
var songListContents = JsonConvert.SerializeObject(songList);
File.WriteAllText(PLAY_DATA_FILE, songListContents);

output($"Completed all.");
return songList;
output($"Loaded all songs {total}/{total} into {PLAY_DATA_FILE}");
}
}

Expand Down
2 changes: 1 addition & 1 deletion scenes/SongList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public Row(SongFile song, Action buttonAction) {
public delegate void OpenedSongEventHandler(string folder, string instrument);

public SongList() {
var songList = SongFileManager.GetSongFileList((str) => {});
var songList = SongFileManager.GetSongFileList();
_rows = songList.Data.ToArray().Select(x => new Row(x, () => ItemActivated(x))).ToList();
}

Expand Down
26 changes: 21 additions & 5 deletions StartMenu.cs → scenes/StartMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using murph9.TabPlayer.Songs;
using System;

namespace murph9.TabPlayer;
namespace murph9.TabPlayer.scenes;

public partial class StartMenu : Node
{
Expand All @@ -17,9 +17,13 @@ public partial class StartMenu : Node
[Signal]
public delegate void SongListChangedEventHandler();

private string _progressText;

public override void _Ready() { }

public override void _Process(double delta) { }
public override void _Process(double delta) {
GetNode<Label>("ReloadProgressLabel").Text = _progressText;
}

public void StartButton_Pressed() {
EmitSignal(SignalName.SongListOpened);
Expand All @@ -34,9 +38,21 @@ public void ConvertButton_Pressed() {
}

public void ReloadButton_Pressed() {
// TODO cleanup so that there is UI progress
SongFileManager.GetSongFileList(GD.Print, true);
EmitSignal(SignalName.SongListChanged);
var startButton = GetNode<Button>("%StartButton");
startButton.Disabled = true;
var convertButton = GetNode<Button>("%ConvertButton");
convertButton.Disabled = true;
var reloadButton = GetNode<Button>("%ReloadButton");
reloadButton.Disabled = true;

Task.Run(() => {
SongFileManager.UpdateSongList((str) => _progressText = str);
EmitSignal(SignalName.SongListChanged);
reloadButton.Disabled = false;
convertButton.Disabled = false;
startButton.Disabled = false;
});
}

public void QuitButton_Pressed() {
Expand Down
87 changes: 87 additions & 0 deletions scenes/StartMenu.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
[gd_scene load_steps=3 format=3 uid="uid://bitdqk5i3b1jq"]

[ext_resource type="Script" path="res://scenes/StartMenu.cs" id="1_wbodt"]

[sub_resource type="LabelSettings" id="LabelSettings_f02ew"]
font_size = 46

[node name="StartMenu" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
size_flags_horizontal = 3
size_flags_vertical = 3
script = ExtResource("1_wbodt")

[node name="VBoxContainer_FullPage" type="VBoxContainer" parent="."]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
alignment = 1

[node name="Title" type="Label" parent="VBoxContainer_FullPage"]
layout_mode = 2
size_flags_horizontal = 4
text = "Tab Player"
label_settings = SubResource("LabelSettings_f02ew")

[node name="VBoxContainer_Buttons" type="VBoxContainer" parent="VBoxContainer_FullPage"]
layout_mode = 2
size_flags_horizontal = 4

[node name="StartButton" type="Button" parent="VBoxContainer_FullPage/VBoxContainer_Buttons"]
unique_name_in_owner = true
custom_minimum_size = Vector2(150, 0)
layout_mode = 2
text = "Start"

[node name="HBoxContainer_Convert" type="HBoxContainer" parent="VBoxContainer_FullPage/VBoxContainer_Buttons"]
layout_mode = 2

[node name="ConvertButton" type="Button" parent="VBoxContainer_FullPage/VBoxContainer_Buttons/HBoxContainer_Convert"]
unique_name_in_owner = true
layout_mode = 2
text = "Convert Songs"

[node name="ReloadButton" type="Button" parent="VBoxContainer_FullPage/VBoxContainer_Buttons/HBoxContainer_Convert"]
unique_name_in_owner = true
layout_mode = 2
text = "Reload Song List"

[node name="HBoxContainer_Info" type="HBoxContainer" parent="VBoxContainer_FullPage/VBoxContainer_Buttons"]
layout_mode = 2

[node name="Credits" type="Label" parent="VBoxContainer_FullPage/VBoxContainer_Buttons/HBoxContainer_Info"]
layout_mode = 2
text = "Made by murph9"

[node name="InfoButton" type="Button" parent="VBoxContainer_FullPage/VBoxContainer_Buttons/HBoxContainer_Info"]
layout_mode = 2
size_flags_horizontal = 3
text = "Info"

[node name="QuitButton" type="Button" parent="VBoxContainer_FullPage/VBoxContainer_Buttons/HBoxContainer_Info"]
layout_mode = 2
text = "Quit
"

[node name="ReloadProgressLabel" type="Label" parent="."]
layout_mode = 1
anchors_preset = 2
anchor_top = 1.0
anchor_bottom = 1.0
offset_top = -23.0
offset_right = 258.0
grow_vertical = 0

[connection signal="pressed" from="VBoxContainer_FullPage/VBoxContainer_Buttons/StartButton" to="." method="StartButton_Pressed"]
[connection signal="pressed" from="VBoxContainer_FullPage/VBoxContainer_Buttons/HBoxContainer_Convert/ConvertButton" to="." method="ConvertButton_Pressed"]
[connection signal="pressed" from="VBoxContainer_FullPage/VBoxContainer_Buttons/HBoxContainer_Convert/ReloadButton" to="." method="ReloadButton_Pressed"]
[connection signal="pressed" from="VBoxContainer_FullPage/VBoxContainer_Buttons/HBoxContainer_Info/InfoButton" to="." method="InfoButton_Pressed"]
[connection signal="pressed" from="VBoxContainer_FullPage/VBoxContainer_Buttons/HBoxContainer_Info/QuitButton" to="." method="QuitButton_Pressed"]

0 comments on commit c763fd8

Please sign in to comment.