Files
RedBookPlayer/RedBookPlayer/GUI/PlayerView.xaml.cs

287 lines
9.9 KiB
C#
Raw Normal View History

2021-03-19 17:07:27 -03:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
2021-03-19 17:07:27 -03:00
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Timers;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using Avalonia.Threading;
2021-06-29 14:23:33 -07:00
namespace RedBookPlayer.GUI
2021-03-19 17:07:27 -03:00
{
public class PlayerView : UserControl
{
/// <summary>
/// Read-only access to the view model
/// </summary>
public PlayerViewModel PlayerViewModel => DataContext as PlayerViewModel;
2021-03-19 17:07:27 -03:00
/// <summary>
/// Set of images representing the digits for the UI
/// </summary>
/// <remarks>
/// TODO: Does it make sense to have this as an array?
/// </remarks>
private Image[] _digits;
2021-03-19 17:07:27 -03:00
/// <summary>
/// Timer for performing UI updates
/// </summary>
private Timer _updateTimer;
2021-03-19 17:07:27 -03:00
2021-07-01 10:19:25 -07:00
/// <summary>
/// Last volume for mute toggling
/// </summary>
private int? _lastVolume = null;
public PlayerView() => InitializeComponent(null);
2021-03-19 17:07:27 -03:00
public PlayerView(string xaml) => InitializeComponent(xaml);
2021-03-19 17:07:27 -03:00
#region Helpers
2021-03-19 17:07:27 -03:00
/// <summary>
/// Generate a path selection dialog box
/// </summary>
/// <returns>User-selected path, if possible</returns>
2021-03-19 17:07:27 -03:00
public async Task<string> GetPath()
{
var dialog = new OpenFileDialog { AllowMultiple = false };
List<string> knownExtensions = new Aaru.DiscImages.AaruFormat().KnownExtensions.ToList();
dialog.Filters.Add(new FileDialogFilter()
2021-03-19 17:07:27 -03:00
{
2021-06-06 20:28:36 +01:00
Name = "Aaru Image Format (*" + string.Join(", *", knownExtensions) + ")",
Extensions = knownExtensions.ConvertAll(e => e.TrimStart('.'))
2021-06-06 20:28:36 +01:00
});
2021-03-19 17:07:27 -03:00
2021-06-06 20:28:36 +01:00
return (await dialog.ShowAsync((Window)Parent.Parent))?.FirstOrDefault();
2021-03-19 17:07:27 -03:00
}
2021-06-29 21:00:34 -07:00
/// <summary>
/// Load an image from the path
/// </summary>
/// <param name="path">Path to the image to load</param>
public async Task<bool> LoadImage(string path)
{
2021-06-30 13:26:41 -07:00
// If the player is currently running, stop it
if(PlayerViewModel.Playing != true) PlayerViewModel.Playing = null;
2021-06-30 13:26:41 -07:00
bool result = await Dispatcher.UIThread.InvokeAsync(() =>
2021-06-29 21:00:34 -07:00
{
PlayerViewModel.Init(path, App.Settings.AutoPlay, App.Settings.Volume);
return PlayerViewModel.Initialized;
2021-06-29 21:00:34 -07:00
});
if(result)
{
await Dispatcher.UIThread.InvokeAsync(() =>
{
MainWindow.Instance.Title = "RedBookPlayer - " + path.Split('/').Last().Split('\\').Last();
});
}
return result;
}
/// <summary>
/// Load the png image for a given character based on the theme
/// </summary>
/// <param name="character">Character to load the image for</param>
/// <returns>Bitmap representing the loaded image</returns>
/// <remarks>
/// TODO: Currently assumes that an image must always exist
/// </remarks>
private Bitmap GetBitmap(char character)
2021-03-19 17:07:27 -03:00
{
2021-06-06 20:28:36 +01:00
if(App.Settings.SelectedTheme == "default")
2021-03-19 17:07:27 -03:00
{
IAssetLoader assets = AvaloniaLocator.Current.GetService<IAssetLoader>();
2021-06-06 20:28:36 +01:00
2021-03-19 17:07:27 -03:00
return new Bitmap(assets.Open(new Uri($"avares://RedBookPlayer/Assets/{character}.png")));
}
else
2021-03-19 17:07:27 -03:00
{
string themeDirectory = $"{Directory.GetCurrentDirectory()}/themes/{App.Settings.SelectedTheme}";
using FileStream stream = File.Open($"{themeDirectory}/{character}.png", FileMode.Open);
return new Bitmap(stream);
2021-03-19 17:07:27 -03:00
}
}
/// <summary>
/// Initialize the UI based on the currently selected theme
/// </summary>
/// <param name="xaml">XAML data representing the theme, null for default</param>
private void InitializeComponent(string xaml)
{
DataContext = new PlayerViewModel();
PlayerViewModel.PropertyChanged += UpdateModel;
2021-06-29 21:00:34 -07:00
// Load the theme
2021-06-30 16:06:09 -07:00
try
{
if(xaml != null)
new AvaloniaXamlLoader().Load(xaml, null, this);
else
AvaloniaXamlLoader.Load(this);
}
catch
{
AvaloniaXamlLoader.Load(this);
2021-06-30 16:06:09 -07:00
}
InitializeDigits();
_updateTimer = new Timer(1000 / 60);
_updateTimer.Elapsed += (sender, e) =>
{
try
{
UpdateView(sender, e);
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
};
_updateTimer.AutoReset = true;
_updateTimer.Start();
}
/// <summary>
/// Initialize the displayed digits array
/// </summary>
private void InitializeDigits()
2021-03-19 17:07:27 -03:00
{
_digits = new Image[]
{
this.FindControl<Image>("TrackDigit1"),
this.FindControl<Image>("TrackDigit2"),
this.FindControl<Image>("IndexDigit1"),
this.FindControl<Image>("IndexDigit2"),
this.FindControl<Image>("TimeDigit1"),
this.FindControl<Image>("TimeDigit2"),
this.FindControl<Image>("TimeDigit3"),
this.FindControl<Image>("TimeDigit4"),
this.FindControl<Image>("TimeDigit5"),
this.FindControl<Image>("TimeDigit6"),
this.FindControl<Image>("TotalTracksDigit1"),
this.FindControl<Image>("TotalTracksDigit2"),
this.FindControl<Image>("TotalIndexesDigit1"),
this.FindControl<Image>("TotalIndexesDigit2"),
this.FindControl<Image>("TotalTimeDigit1"),
this.FindControl<Image>("TotalTimeDigit2"),
this.FindControl<Image>("TotalTimeDigit3"),
this.FindControl<Image>("TotalTimeDigit4"),
this.FindControl<Image>("TotalTimeDigit5"),
this.FindControl<Image>("TotalTimeDigit6"),
};
2021-03-19 17:07:27 -03:00
}
/// <summary>
/// Update the Player with the most recent information from the UI
/// </summary>
private void UpdateModel(object sender, PropertyChangedEventArgs e)
{
Dispatcher.UIThread.InvokeAsync(() =>
{
PlayerViewModel.UpdateModel();
});
}
/// <summary>
/// Update the UI with the most recent information from the Player
/// </summary>
private void UpdateView(object sender, ElapsedEventArgs e)
{
Dispatcher.UIThread.InvokeAsync(() =>
{
string digitString = PlayerViewModel.GenerateDigitString();
for (int i = 0; i < _digits.Length; i++)
{
if (_digits[i] != null)
_digits[i].Source = GetBitmap(digitString[i]);
}
2021-06-06 20:28:36 +01:00
PlayerViewModel?.UpdateView();
});
2021-03-19 17:07:27 -03:00
}
2021-06-06 20:28:36 +01:00
#endregion
2021-06-06 20:28:36 +01:00
#region Event Handlers
2021-06-06 20:28:36 +01:00
public async void LoadButton_Click(object sender, RoutedEventArgs e)
2021-04-14 20:36:34 -03:00
{
string path = await GetPath();
if (path == null)
return;
2021-06-30 13:26:41 -07:00
await LoadImage(path);
2021-04-14 20:36:34 -03:00
}
public void PlayButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.Playing = true;
public void PauseButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.Playing = false;
public void PlayPauseButton_Click(object sender, RoutedEventArgs e)
{
if(PlayerViewModel.Playing == true)
PlayerViewModel.Playing = false;
else
PlayerViewModel.Playing = true;
}
public void StopButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.Playing = null;
public void NextTrackButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.NextTrack();
public void PreviousTrackButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.PreviousTrack();
public void NextIndexButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.NextIndex(App.Settings.IndexButtonChangeTrack);
public void PreviousIndexButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.PreviousIndex(App.Settings.IndexButtonChangeTrack);
public void FastForwardButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.FastForward();
public void RewindButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.Rewind();
public void VolumeUpButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.Volume++;
2021-07-01 10:19:25 -07:00
public void VolumeDownButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.Volume--;
2021-07-01 10:19:25 -07:00
public void MuteToggleButton_Click(object sender, RoutedEventArgs e)
{
if (_lastVolume == null)
{
_lastVolume = PlayerViewModel.Volume;
PlayerViewModel.Volume = 0;
2021-07-01 10:19:25 -07:00
}
else
{
PlayerViewModel.Volume = _lastVolume.Value;
2021-07-01 10:19:25 -07:00
_lastVolume = null;
}
}
public void EnableDeEmphasisButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.ApplyDeEmphasis = true;
public void DisableDeEmphasisButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.ApplyDeEmphasis = false;
public void EnableDisableDeEmphasisButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.ApplyDeEmphasis = !PlayerViewModel.ApplyDeEmphasis;
2021-06-30 13:26:41 -07:00
#endregion
2021-03-19 17:07:27 -03:00
}
}