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

168 lines
5.9 KiB
C#
Raw Normal View History

2021-03-19 17:07:27 -03:00
using System;
2021-07-04 23:43:55 -07:00
using System.ComponentModel;
2021-03-19 17:07:27 -03:00
using System.IO;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using Avalonia.Threading;
2021-07-12 10:41:11 -07:00
using RedBookPlayer.GUI.ViewModels;
2021-03-19 17:07:27 -03:00
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>
private Image[] _digits;
2021-03-19 17:07:27 -03:00
/// <summary>
/// Initialize the UI based on the default theme
/// </summary>
2021-07-05 23:09:22 -07:00
public PlayerView() : this(null, null) { }
/// <summary>
/// Initialize the UI based on the default theme with an existing view model
/// </summary>
/// <param name="xaml">XAML data representing the theme, null for default</param>
/// <param name="playerViewModel">Existing PlayerViewModel to load in instead of creating a new one</param>
public PlayerView(PlayerViewModel playerViewModel) : this(null, playerViewModel) { }
2021-07-05 10:42:28 -07:00
/// <summary>
/// Initialize the UI based on the currently selected theme
/// </summary>
/// <param name="xaml">XAML data representing the theme, null for default</param>
2021-07-05 23:09:22 -07:00
/// <param name="playerViewModel">Existing PlayerViewModel to load in instead of creating a new one</param>
public PlayerView(string xaml, PlayerViewModel playerViewModel)
2021-07-05 10:42:28 -07:00
{
2021-07-05 23:09:22 -07:00
if(playerViewModel != null)
DataContext = playerViewModel;
else
DataContext = new PlayerViewModel();
2021-07-05 10:42:28 -07:00
PlayerViewModel.PropertyChanged += PlayerViewModelStateChanged;
2021-03-19 17:07:27 -03:00
2021-07-05 10:42:28 -07:00
LoadTheme(xaml);
InitializeDigits();
}
2021-03-19 17:07:27 -03:00
#region Helpers
2021-03-19 17:07:27 -03:00
/// <summary>
/// Update the view model with new settings
/// </summary>
2021-07-05 22:13:00 -07:00
public void UpdateViewModel()
{
PlayerViewModel.SetLoadDataTracks(App.Settings.PlayDataTracks);
PlayerViewModel.SetLoadHiddenTracks(App.Settings.PlayHiddenTracks);
}
/// <summary>
2021-07-05 10:42:28 -07:00
/// Load the png image for a given character based on the theme
/// </summary>
2021-07-05 10:42:28 -07:00
/// <param name="character">Character to load the image for</param>
/// <returns>Bitmap representing the loaded image</returns>
private Bitmap GetBitmap(char character)
{
2021-06-30 16:06:09 -07:00
try
{
2021-07-05 10:42:28 -07:00
if(App.Settings.SelectedTheme == "default")
{
IAssetLoader assets = AvaloniaLocator.Current.GetService<IAssetLoader>();
return new Bitmap(assets.Open(new Uri($"avares://RedBookPlayer/Assets/{character}.png")));
}
2021-06-30 16:06:09 -07:00
else
2021-07-05 10:42:28 -07:00
{
string themeDirectory = $"{Directory.GetCurrentDirectory()}/themes/{App.Settings.SelectedTheme}";
using FileStream stream = File.Open($"{themeDirectory}/{character}.png", FileMode.Open);
return new Bitmap(stream);
}
2021-06-30 16:06:09 -07:00
}
catch
{
2021-07-05 10:42:28 -07:00
return null;
2021-06-30 16:06:09 -07:00
}
}
/// <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
}
2021-07-05 10:42:28 -07:00
/// <summary>
/// Load the theme from a XAML, if possible
/// </summary>
/// <param name="xaml">XAML data representing the theme, null for default</param>
private void LoadTheme(string xaml)
{
try
{
if(xaml != null)
new AvaloniaXamlLoader().Load(xaml, null, this);
else
AvaloniaXamlLoader.Load(this);
}
catch
{
AvaloniaXamlLoader.Load(this);
}
}
/// <summary>
2021-07-04 23:43:55 -07:00
/// Update the UI from the view-model
/// </summary>
2021-07-04 23:43:55 -07:00
private void PlayerViewModelStateChanged(object sender, PropertyChangedEventArgs e)
{
Dispatcher.UIThread.InvokeAsync(() =>
{
2021-07-12 10:52:50 -07:00
string digitString = PlayerViewModel?.GenerateDigitString() ?? string.Empty.PadLeft(20, '-');
2021-07-04 23:43:55 -07:00
for(int i = 0; i < _digits.Length; i++)
{
2021-07-05 10:42:28 -07:00
Bitmap digitImage = GetBitmap(digitString[i]);
if(_digits[i] != null && digitImage != null)
_digits[i].Source = digitImage;
}
});
2021-03-19 17:07:27 -03:00
}
2021-06-06 20:28:36 +01:00
#endregion
2021-03-19 17:07:27 -03:00
}
}