2021-03-19 17:07:27 -03:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2021-07-04 23:43:55 -07:00
|
|
|
using System.ComponentModel;
|
2021-03-19 17:07:27 -03:00
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Avalonia;
|
|
|
|
|
using Avalonia.Controls;
|
|
|
|
|
using Avalonia.Interactivity;
|
|
|
|
|
using Avalonia.Markup.Xaml;
|
|
|
|
|
using Avalonia.Media.Imaging;
|
|
|
|
|
using Avalonia.Platform;
|
|
|
|
|
using Avalonia.Threading;
|
2021-07-05 16:20:34 -07:00
|
|
|
using RedBookPlayer.Common;
|
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
|
|
|
|
|
{
|
2021-06-06 21:43:47 -07:00
|
|
|
/// <summary>
|
2021-07-03 15:37:56 -07:00
|
|
|
/// Read-only access to the view model
|
2021-06-06 21:43:47 -07:00
|
|
|
/// </summary>
|
2021-07-03 15:37:56 -07:00
|
|
|
public PlayerViewModel PlayerViewModel => DataContext as PlayerViewModel;
|
2021-03-19 17:07:27 -03:00
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
/// <summary>
|
|
|
|
|
/// Set of images representing the digits for the UI
|
|
|
|
|
/// </summary>
|
|
|
|
|
private Image[] _digits;
|
2021-03-19 17:07:27 -03:00
|
|
|
|
2021-07-05 11:55:36 -07:00
|
|
|
/// <summary>
|
|
|
|
|
/// Initialize the UI based on the default theme
|
|
|
|
|
/// </summary>
|
|
|
|
|
public PlayerView() : this(null) { }
|
|
|
|
|
|
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 11:55:36 -07:00
|
|
|
public PlayerView(string xaml)
|
2021-07-05 10:42:28 -07:00
|
|
|
{
|
|
|
|
|
DataContext = new PlayerViewModel();
|
|
|
|
|
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
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
#region Helpers
|
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-07-05 10:42:28 -07:00
|
|
|
return await Dispatcher.UIThread.InvokeAsync(() =>
|
2021-06-29 21:00:34 -07:00
|
|
|
{
|
2021-07-05 11:55:36 -07:00
|
|
|
PlayerViewModel.Init(path, App.Settings.GenerateMissingTOC, App.Settings.PlayDataTracks, App.Settings.AutoPlay, App.Settings.Volume);
|
2021-07-05 10:42:28 -07:00
|
|
|
if (PlayerViewModel.Initialized)
|
|
|
|
|
MainWindow.Instance.Title = "RedBookPlayer - " + path.Split('/').Last().Split('\\').Last();
|
|
|
|
|
|
2021-07-03 15:37:56 -07:00
|
|
|
return PlayerViewModel.Initialized;
|
2021-06-29 21:00:34 -07:00
|
|
|
});
|
2021-07-05 10:42:28 -07:00
|
|
|
}
|
2021-06-29 21:00:34 -07:00
|
|
|
|
2021-07-05 11:55:36 -07:00
|
|
|
/// <summary>
|
|
|
|
|
/// Update the view model with new settings
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void UpdateViewModel() => PlayerViewModel.SetLoadDataTracks(App.Settings.PlayDataTracks);
|
|
|
|
|
|
2021-07-05 10:42:28 -07:00
|
|
|
/// <summary>
|
|
|
|
|
/// Generate the digit string to be interpreted by the frontend
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>String representing the digits for the frontend</returns>
|
|
|
|
|
private string GenerateDigitString()
|
|
|
|
|
{
|
|
|
|
|
// If the disc isn't initialized, return all '-' characters
|
|
|
|
|
if(PlayerViewModel?.Initialized != true)
|
|
|
|
|
return string.Empty.PadLeft(20, '-');
|
|
|
|
|
|
|
|
|
|
// Otherwise, take the current time into account
|
|
|
|
|
ulong sectorTime = GetCurrentSectorTime();
|
|
|
|
|
|
|
|
|
|
int[] numbers = new int[]
|
2021-06-29 21:00:34 -07:00
|
|
|
{
|
2021-07-05 10:42:28 -07:00
|
|
|
PlayerViewModel.CurrentTrackNumber + 1,
|
|
|
|
|
PlayerViewModel.CurrentTrackIndex,
|
|
|
|
|
|
|
|
|
|
(int)(sectorTime / (75 * 60)),
|
|
|
|
|
(int)(sectorTime / 75 % 60),
|
|
|
|
|
(int)(sectorTime % 75),
|
|
|
|
|
|
|
|
|
|
PlayerViewModel.TotalTracks,
|
|
|
|
|
PlayerViewModel.TotalIndexes,
|
|
|
|
|
|
|
|
|
|
(int)(PlayerViewModel.TotalTime / (75 * 60)),
|
|
|
|
|
(int)(PlayerViewModel.TotalTime / 75 % 60),
|
|
|
|
|
(int)(PlayerViewModel.TotalTime % 75),
|
|
|
|
|
};
|
2021-06-29 21:00:34 -07:00
|
|
|
|
2021-07-05 10:42:28 -07:00
|
|
|
return string.Join("", numbers.Select(i => i.ToString().PadLeft(2, '0').Substring(0, 2)));
|
2021-06-29 21:00:34 -07:00
|
|
|
}
|
|
|
|
|
|
2021-06-29 15:51:24 -07:00
|
|
|
/// <summary>
|
2021-07-05 10:42:28 -07:00
|
|
|
/// Load the png image for a given character based on the theme
|
2021-06-29 15:51:24 -07:00
|
|
|
/// </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-29 15:51:24 -07:00
|
|
|
{
|
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
|
|
|
}
|
2021-06-29 15:51:24 -07:00
|
|
|
}
|
|
|
|
|
|
2021-07-05 11:55:36 -07:00
|
|
|
/// <summary>
|
|
|
|
|
/// Get current sector time, accounting for offsets
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>ulong representing the current sector time</returns>
|
|
|
|
|
private ulong GetCurrentSectorTime()
|
|
|
|
|
{
|
|
|
|
|
ulong sectorTime = PlayerViewModel.CurrentSector;
|
|
|
|
|
if(PlayerViewModel.SectionStartSector != 0)
|
|
|
|
|
sectorTime -= PlayerViewModel.SectionStartSector;
|
|
|
|
|
else
|
|
|
|
|
sectorTime += PlayerViewModel.TimeOffset;
|
|
|
|
|
|
|
|
|
|
return sectorTime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Generate a path selection dialog box
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>User-selected path, if possible</returns>
|
|
|
|
|
private async Task<string> GetPath()
|
|
|
|
|
{
|
|
|
|
|
var dialog = new OpenFileDialog { AllowMultiple = false };
|
|
|
|
|
List<string> knownExtensions = new Aaru.DiscImages.AaruFormat().KnownExtensions.ToList();
|
|
|
|
|
dialog.Filters.Add(new FileDialogFilter()
|
|
|
|
|
{
|
|
|
|
|
Name = "Aaru Image Format (*" + string.Join(", *", knownExtensions) + ")",
|
|
|
|
|
Extensions = knownExtensions.ConvertAll(e => e.TrimStart('.'))
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (await dialog.ShowAsync((Window)Parent.Parent))?.FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
/// <summary>
|
|
|
|
|
/// Initialize the displayed digits array
|
|
|
|
|
/// </summary>
|
2021-06-29 15:51:24 -07:00
|
|
|
private void InitializeDigits()
|
2021-03-19 17:07:27 -03:00
|
|
|
{
|
2021-06-06 21:43:47 -07: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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
/// <summary>
|
2021-07-04 23:43:55 -07:00
|
|
|
/// Update the UI from the view-model
|
2021-06-06 21:43:47 -07:00
|
|
|
/// </summary>
|
2021-07-04 23:43:55 -07:00
|
|
|
private void PlayerViewModelStateChanged(object sender, PropertyChangedEventArgs e)
|
2021-04-12 18:12:51 -03:00
|
|
|
{
|
2021-06-06 21:43:47 -07:00
|
|
|
Dispatcher.UIThread.InvokeAsync(() =>
|
|
|
|
|
{
|
2021-07-05 00:48:14 -07:00
|
|
|
string digitString = GenerateDigitString();
|
2021-07-04 23:43:55 -07:00
|
|
|
for(int i = 0; i < _digits.Length; i++)
|
2021-06-06 21:43:47 -07:00
|
|
|
{
|
2021-07-05 10:42:28 -07:00
|
|
|
Bitmap digitImage = GetBitmap(digitString[i]);
|
|
|
|
|
if(_digits[i] != null && digitImage != null)
|
|
|
|
|
_digits[i].Source = digitImage;
|
2021-06-06 21:43:47 -07:00
|
|
|
}
|
|
|
|
|
});
|
2021-03-19 17:07:27 -03:00
|
|
|
}
|
2021-06-06 20:28:36 +01:00
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
#endregion
|
2021-06-06 20:28:36 +01:00
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
#region Event Handlers
|
2021-06-06 20:28:36 +01:00
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
public async void LoadButton_Click(object sender, RoutedEventArgs e)
|
2021-04-14 20:36:34 -03:00
|
|
|
{
|
2021-06-06 21:43:47 -07: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
|
|
|
}
|
2021-06-06 21:43:47 -07:00
|
|
|
|
2021-07-04 23:17:30 -07:00
|
|
|
public void PlayButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.Play();
|
2021-06-06 21:43:47 -07:00
|
|
|
|
2021-07-04 23:17:30 -07:00
|
|
|
public void PauseButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.Pause();
|
2021-06-06 21:43:47 -07:00
|
|
|
|
2021-07-04 23:17:30 -07:00
|
|
|
public void PlayPauseButton_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if(PlayerViewModel.Playing == true)
|
|
|
|
|
PlayerViewModel.Pause();
|
|
|
|
|
else
|
|
|
|
|
PlayerViewModel.Play();
|
|
|
|
|
}
|
2021-06-29 22:58:08 -07:00
|
|
|
|
2021-07-04 23:17:30 -07:00
|
|
|
public void StopButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.Stop();
|
2021-06-06 21:43:47 -07:00
|
|
|
|
2021-07-03 15:37:56 -07:00
|
|
|
public void NextTrackButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.NextTrack();
|
2021-06-06 21:43:47 -07:00
|
|
|
|
2021-07-05 11:55:36 -07:00
|
|
|
public void PreviousTrackButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.PreviousTrack(App.Settings.AllowSkipHiddenTrack);
|
2021-06-06 21:43:47 -07:00
|
|
|
|
2021-07-03 15:37:56 -07:00
|
|
|
public void NextIndexButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.NextIndex(App.Settings.IndexButtonChangeTrack);
|
2021-06-06 21:43:47 -07:00
|
|
|
|
2021-07-05 11:55:36 -07:00
|
|
|
public void PreviousIndexButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.PreviousIndex(App.Settings.IndexButtonChangeTrack, App.Settings.AllowSkipHiddenTrack);
|
2021-06-06 21:43:47 -07:00
|
|
|
|
2021-07-03 15:37:56 -07:00
|
|
|
public void FastForwardButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.FastForward();
|
2021-06-06 21:43:47 -07:00
|
|
|
|
2021-07-03 15:37:56 -07:00
|
|
|
public void RewindButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.Rewind();
|
2021-06-06 21:43:47 -07:00
|
|
|
|
2021-07-05 16:20:34 -07:00
|
|
|
public void VolumeUpButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.SetVolume(PlayerViewModel.Volume + 1);
|
2021-07-01 10:19:25 -07:00
|
|
|
|
2021-07-05 16:20:34 -07:00
|
|
|
public void VolumeDownButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.SetVolume(PlayerViewModel.Volume - 1);
|
2021-07-01 10:19:25 -07:00
|
|
|
|
2021-07-03 16:21:14 -07:00
|
|
|
public void MuteToggleButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.ToggleMute();
|
2021-07-01 10:19:25 -07:00
|
|
|
|
2021-07-04 23:17:30 -07:00
|
|
|
public void EnableDeEmphasisButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.SetDeEmphasis(true);
|
2021-06-06 21:43:47 -07:00
|
|
|
|
2021-07-04 23:17:30 -07:00
|
|
|
public void DisableDeEmphasisButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.SetDeEmphasis(false);
|
2021-06-06 21:43:47 -07:00
|
|
|
|
2021-07-04 23:17:30 -07:00
|
|
|
public void EnableDisableDeEmphasisButton_Click(object sender, RoutedEventArgs e) => PlayerViewModel.SetDeEmphasis(!PlayerViewModel.ApplyDeEmphasis);
|
2021-06-30 13:26:41 -07:00
|
|
|
|
2021-06-06 21:43:47 -07:00
|
|
|
#endregion
|
2021-03-19 17:07:27 -03:00
|
|
|
}
|
|
|
|
|
}
|