Files
RedBookPlayer/RedBookPlayer.GUI/Settings.cs

198 lines
5.6 KiB
C#
Raw Normal View History

2021-03-24 17:34:04 -03:00
using System;
using System.IO;
using System.Text.Json;
2021-06-30 16:16:49 -07:00
using Avalonia.Input;
using RedBookPlayer.GUI.Views;
2021-03-24 17:34:04 -03:00
namespace RedBookPlayer.GUI
2021-03-24 17:34:04 -03:00
{
public class Settings
{
2021-06-30 16:16:49 -07:00
#region Player Settings
/// <summary>
/// Indicates if discs should start playing on load
/// </summary>
public bool AutoPlay { get; set; } = false;
2021-03-24 17:34:04 -03:00
/// <summary>
/// Indicates if an index change can trigger a track change
/// </summary>
public bool IndexButtonChangeTrack { get; set; } = false;
/// <summary>
2021-07-05 22:13:00 -07:00
/// Indicates if hidden tracks should be played
/// </summary>
2021-07-05 22:13:00 -07:00
/// <remarks>
/// Hidden tracks can be one of the following:
/// - TrackSequence == 0
/// - Larget pregap of track 1 (> 150 sectors)
/// </remarks>
public bool PlayHiddenTracks { get; set; } = false;
/// <summary>
/// Indicates if data tracks should be played like old, non-compliant players
/// </summary>
public bool PlayDataTracks { get; set; } = false;
/// <summary>
/// Generate a TOC if the disc is missing one
/// </summary>
public bool GenerateMissingTOC { get; set; } = true;
2021-03-24 17:34:04 -03:00
/// <summary>
/// Indicates the default playback volume
/// </summary>
2021-07-01 11:50:38 -07:00
public int Volume
{
get => _volume;
set
{
if(value > 100)
_volume = 100;
else if(value < 0)
_volume = 0;
else
_volume = value;
}
}
/// <summary>
/// Indicates the currently selected theme
/// </summary>
public string SelectedTheme { get; set; } = "default";
2021-06-30 16:16:49 -07:00
#endregion
#region Key Mappings
/// <summary>
/// Key assigned to open settings
/// </summary>
public Key OpenSettingsKey { get; set; } = Key.F1;
/// <summary>
/// Key assigned to load a new image
/// </summary>
public Key LoadImageKey { get; set; } = Key.F2;
/// <summary>
/// Key assigned to toggle play and pause
/// </summary>
public Key TogglePlaybackKey { get; set; } = Key.Space;
/// <summary>
/// Key assigned to stop playback
/// </summary>
public Key StopPlaybackKey { get; set; } = Key.Escape;
/// <summary>
/// Key assigned to move to the next track
/// </summary>
public Key NextTrackKey { get; set; } = Key.Right;
/// <summary>
/// Key assigned to move to the previous track
/// </summary>
public Key PreviousTrackKey { get; set; } = Key.Left;
/// <summary>
/// Key assigned to move to the next index
/// </summary>
public Key NextIndexKey { get; set; } = Key.OemCloseBrackets;
/// <summary>
/// Key assigned to move to the previous index
/// </summary>
public Key PreviousIndexKey { get; set; } = Key.OemOpenBrackets;
/// <summary>
/// Key assigned to fast forward playback
/// </summary>
public Key FastForwardPlaybackKey { get; set; } = Key.OemPeriod;
/// <summary>
/// Key assigned to rewind playback
/// </summary>
public Key RewindPlaybackKey { get; set; } = Key.OemComma;
2021-07-01 10:19:25 -07:00
/// <summary>
/// Key assigned to raise volume
/// </summary>
2021-07-01 11:38:08 -07:00
public Key VolumeUpKey { get; set; } = Key.Add;
2021-07-01 10:19:25 -07:00
/// <summary>
/// Key assigned to lower volume
/// </summary>
2021-07-01 11:38:08 -07:00
public Key VolumeDownKey { get; set; } = Key.Subtract;
2021-07-01 10:19:25 -07:00
/// <summary>
/// Key assigned to toggle mute
/// </summary>
public Key ToggleMuteKey { get; set; } = Key.M;
2021-06-30 16:16:49 -07:00
/// <summary>
/// Key assigned to toggle de-emphasis
/// </summary>
public Key ToggleDeEmphasisKey { get; set; } = Key.E;
#endregion
/// <summary>
/// Path to the settings file
/// </summary>
private string _filePath;
2021-07-01 11:50:38 -07:00
/// <summary>
/// Internal value for the volume
/// </summary>
private int _volume = 100;
public Settings() {}
2021-06-06 20:28:36 +01:00
public Settings(string filePath) => _filePath = filePath;
2021-03-24 17:34:04 -03:00
/// <summary>
/// Load settings from a file
/// </summary>
/// <param name="filePath">Path to the settings JSON file</param>
/// <returns>Settings derived from the input file, if possible</returns>
2021-03-24 17:34:04 -03:00
public static Settings Load(string filePath)
{
2021-06-06 20:28:36 +01:00
if(File.Exists(filePath))
2021-03-24 17:34:04 -03:00
{
try
{
Settings settings = JsonSerializer.Deserialize<Settings>(File.ReadAllText(filePath));
settings._filePath = filePath;
2021-03-24 17:34:04 -03:00
MainWindow.ApplyTheme(settings.SelectedTheme);
return settings;
}
2021-06-06 20:28:36 +01:00
catch(JsonException)
2021-03-24 17:34:04 -03:00
{
Console.WriteLine("Couldn't parse settings, reverting to default");
2021-06-06 20:28:36 +01:00
2021-03-24 17:34:04 -03:00
return new Settings(filePath);
}
}
2021-06-06 20:28:36 +01:00
return new Settings(filePath);
2021-03-24 17:34:04 -03:00
}
/// <summary>
/// Save settings to a file
/// </summary>
2021-03-24 17:34:04 -03:00
public void Save()
{
2021-06-06 20:28:36 +01:00
var options = new JsonSerializerOptions
2021-03-24 17:34:04 -03:00
{
WriteIndented = true
};
string json = JsonSerializer.Serialize(this, options);
File.WriteAllText(_filePath, json);
2021-03-24 17:34:04 -03:00
}
}
}