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