Migrate Audio to SettingsAudioViewModel

This commit is contained in:
Isaac Marovitz 2024-04-19 13:53:23 -04:00
parent 2d73107dc0
commit 388597b4e6
No known key found for this signature in database
GPG key ID: 97250B2B09A132E1
7 changed files with 129 additions and 58 deletions

View file

@ -0,0 +1,89 @@
using Avalonia.Threading;
using Ryujinx.Audio.Backends.OpenAL;
using Ryujinx.Audio.Backends.SDL2;
using Ryujinx.Audio.Backends.SoundIo;
using Ryujinx.Common.Logging;
using Ryujinx.UI.Common.Configuration;
using System;
using System.Threading.Tasks;
namespace Ryujinx.Ava.UI.ViewModels.Settings
{
public class SettingsAudioViewModel : BaseModel
{
public event Action DirtyEvent;
private int _audioBackend;
public int AudioBackend
{
get => _audioBackend;
set
{
_audioBackend = value;
DirtyEvent?.Invoke();
}
}
private float _volume;
public float Volume
{
get => _volume;
set
{
_volume = value;
DirtyEvent?.Invoke();
}
}
public bool IsOpenAlEnabled { get; set; }
public bool IsSoundIoEnabled { get; set; }
public bool IsSDL2Enabled { get; set; }
public SettingsAudioViewModel()
{
ConfigurationState config = ConfigurationState.Instance;
Task.Run(CheckSoundBackends);
AudioBackend = (int)config.System.AudioBackend.Value;
Volume = config.System.AudioVolume * 100;
}
public async Task CheckSoundBackends()
{
IsOpenAlEnabled = OpenALHardwareDeviceDriver.IsSupported;
IsSoundIoEnabled = SoundIoHardwareDeviceDriver.IsSupported;
IsSDL2Enabled = SDL2HardwareDeviceDriver.IsSupported;
await Dispatcher.UIThread.InvokeAsync(() =>
{
OnPropertyChanged(nameof(IsOpenAlEnabled));
OnPropertyChanged(nameof(IsSoundIoEnabled));
OnPropertyChanged(nameof(IsSDL2Enabled));
});
}
public bool CheckIfModified(ConfigurationState config)
{
bool isDirty = false;
isDirty |= config.System.AudioBackend.Value != (AudioBackend)AudioBackend;
isDirty |= config.System.AudioVolume.Value != Volume / 100;
return isDirty;
}
public void Save(ConfigurationState config)
{
AudioBackend audioBackend = (AudioBackend)AudioBackend;
if (audioBackend != config.System.AudioBackend.Value)
{
config.System.AudioBackend.Value = audioBackend;
Logger.Info?.Print(LogClass.Application, $"AudioBackend toggled to: {audioBackend}");
}
config.System.AudioVolume.Value = Volume / 100;
}
}
}

View file

@ -85,7 +85,7 @@ namespace Ryujinx.Ava.UI.ViewModels.Settings
public event Action NotifyChangesEvent; public event Action NotifyChangesEvent;
public SettingsViewModel SettingsViewModel; private readonly SettingsViewModel _settingsViewModel;
public object ConfigViewModel public object ConfigViewModel
{ {
@ -241,7 +241,7 @@ namespace Ryujinx.Ava.UI.ViewModels.Settings
public SettingsInputViewModel(UserControl owner, SettingsViewModel settingsViewModel) : this() public SettingsInputViewModel(UserControl owner, SettingsViewModel settingsViewModel) : this()
{ {
SettingsViewModel = settingsViewModel; _settingsViewModel = settingsViewModel;
if (Program.PreviewerDetached) if (Program.PreviewerDetached)
{ {

View file

@ -42,7 +42,6 @@ namespace Ryujinx.Ava.UI.ViewModels.Settings
private float _customResolutionScale; private float _customResolutionScale;
private int _resolutionScale; private int _resolutionScale;
private int _graphicsBackendMultithreadingIndex; private int _graphicsBackendMultithreadingIndex;
private float _volume;
private bool _isVulkanAvailable = true; private bool _isVulkanAvailable = true;
private bool _directoryChanged; private bool _directoryChanged;
private readonly List<string> _gpuIds = new(); private readonly List<string> _gpuIds = new();
@ -397,9 +396,6 @@ namespace Ryujinx.Ava.UI.ViewModels.Settings
} }
} }
public bool IsOpenAlEnabled { get; set; }
public bool IsSoundIoEnabled { get; set; }
public bool IsSDL2Enabled { get; set; }
public bool IsCustomResolutionScaleActive => _resolutionScale == 4; public bool IsCustomResolutionScaleActive => _resolutionScale == 4;
public bool IsScalingFilterActive => _scalingFilter == (int)Ryujinx.Common.Configuration.ScalingFilter.Fsr; public bool IsScalingFilterActive => _scalingFilter == (int)Ryujinx.Common.Configuration.ScalingFilter.Fsr;
@ -412,7 +408,6 @@ namespace Ryujinx.Ava.UI.ViewModels.Settings
public int Language { get; set; } public int Language { get; set; }
public int Region { get; set; } public int Region { get; set; }
public int FsGlobalAccessLogMode { get; set; } public int FsGlobalAccessLogMode { get; set; }
public int AudioBackend { get; set; }
public int MaxAnisotropy { get; set; } public int MaxAnisotropy { get; set; }
public int AspectRatio { get; set; } public int AspectRatio { get; set; }
public int AntiAliasingEffect { get; set; } public int AntiAliasingEffect { get; set; }
@ -453,18 +448,7 @@ namespace Ryujinx.Ava.UI.ViewModels.Settings
public int PreferredGpuIndex { get; set; } public int PreferredGpuIndex { get; set; }
public float Volume private readonly SettingsAudioViewModel _audioViewModel;
{
get => _volume;
set
{
_volume = value;
ConfigurationState.Instance.System.AudioVolume.Value = _volume / 100;
OnPropertyChanged();
}
}
public DateTimeOffset CurrentDate { get; set; } public DateTimeOffset CurrentDate { get; set; }
public TimeSpan CurrentTime { get; set; } public TimeSpan CurrentTime { get; set; }
@ -500,10 +484,17 @@ namespace Ryujinx.Ava.UI.ViewModels.Settings
} }
} }
public SettingsViewModel(VirtualFileSystem virtualFileSystem, ContentManager contentManager) : this() public SettingsViewModel(
VirtualFileSystem virtualFileSystem,
ContentManager contentManager,
SettingsAudioViewModel audioViewModel) : this()
{ {
_virtualFileSystem = virtualFileSystem; _virtualFileSystem = virtualFileSystem;
_contentManager = contentManager; _contentManager = contentManager;
_audioViewModel = audioViewModel;
_audioViewModel.DirtyEvent += CheckIfModified;
if (Program.PreviewerDetached) if (Program.PreviewerDetached)
{ {
Task.Run(LoadTimeZones); Task.Run(LoadTimeZones);
@ -518,7 +509,6 @@ namespace Ryujinx.Ava.UI.ViewModels.Settings
_validTzRegions = new List<string>(); _validTzRegions = new List<string>();
_networkInterfaces = new Dictionary<string, string>(); _networkInterfaces = new Dictionary<string, string>();
Task.Run(CheckSoundBackends);
Task.Run(PopulateNetworkInterfaces); Task.Run(PopulateNetworkInterfaces);
if (Program.PreviewerDetached) if (Program.PreviewerDetached)
@ -589,10 +579,10 @@ namespace Ryujinx.Ava.UI.ViewModels.Settings
isDirty |= config.Graphics.BackendThreading.Value != (BackendThreading)GraphicsBackendMultithreadingIndex; isDirty |= config.Graphics.BackendThreading.Value != (BackendThreading)GraphicsBackendMultithreadingIndex;
isDirty |= config.Graphics.ShadersDumpPath.Value != ShaderDumpPath; isDirty |= config.Graphics.ShadersDumpPath.Value != ShaderDumpPath;
// Audio if (_audioViewModel != null)
isDirty |= config.System.AudioBackend.Value != (AudioBackend)AudioBackend; {
isDirty |= config.System.AudioVolume.Value != Volume / 100; isDirty |= _audioViewModel.CheckIfModified(config);
}
// Network // Network
isDirty |= config.System.EnableInternetAccess.Value != EnableInternetAccess; isDirty |= config.System.EnableInternetAccess.Value != EnableInternetAccess;
@ -615,19 +605,7 @@ namespace Ryujinx.Ava.UI.ViewModels.Settings
IsModified = isDirty; IsModified = isDirty;
} }
public async Task CheckSoundBackends()
{
IsOpenAlEnabled = OpenALHardwareDeviceDriver.IsSupported;
IsSoundIoEnabled = SoundIoHardwareDeviceDriver.IsSupported;
IsSDL2Enabled = SDL2HardwareDeviceDriver.IsSupported;
await Dispatcher.UIThread.InvokeAsync(() =>
{
OnPropertyChanged(nameof(IsOpenAlEnabled));
OnPropertyChanged(nameof(IsSoundIoEnabled));
OnPropertyChanged(nameof(IsSDL2Enabled));
});
}
private async Task LoadAvailableGpus() private async Task LoadAvailableGpus()
{ {
@ -766,10 +744,6 @@ namespace Ryujinx.Ava.UI.ViewModels.Settings
ScalingFilter = (int)config.Graphics.ScalingFilter.Value; ScalingFilter = (int)config.Graphics.ScalingFilter.Value;
ScalingFilterLevel = config.Graphics.ScalingFilterLevel.Value; ScalingFilterLevel = config.Graphics.ScalingFilterLevel.Value;
// Audio
AudioBackend = (int)config.System.AudioBackend.Value;
Volume = config.System.AudioVolume * 100;
// Network // Network
EnableInternetAccess = config.System.EnableInternetAccess; EnableInternetAccess = config.System.EnableInternetAccess;
// LAN interface index is loaded asynchronously in PopulateNetworkInterfaces() // LAN interface index is loaded asynchronously in PopulateNetworkInterfaces()
@ -854,17 +828,11 @@ namespace Ryujinx.Ava.UI.ViewModels.Settings
config.Graphics.BackendThreading.Value = (BackendThreading)GraphicsBackendMultithreadingIndex; config.Graphics.BackendThreading.Value = (BackendThreading)GraphicsBackendMultithreadingIndex;
config.Graphics.ShadersDumpPath.Value = ShaderDumpPath; config.Graphics.ShadersDumpPath.Value = ShaderDumpPath;
// Audio if (_audioViewModel != null)
AudioBackend audioBackend = (AudioBackend)AudioBackend;
if (audioBackend != config.System.AudioBackend.Value)
{ {
config.System.AudioBackend.Value = audioBackend; _audioViewModel.Save(config);
Logger.Info?.Print(LogClass.Application, $"AudioBackend toggled to: {audioBackend}");
} }
config.System.AudioVolume.Value = Volume / 100;
// Network // Network
config.System.EnableInternetAccess.Value = EnableInternetAccess; config.System.EnableInternetAccess.Value = EnableInternetAccess;

View file

@ -9,9 +9,9 @@
xmlns:locale="clr-namespace:Ryujinx.Ava.Common.Locale" xmlns:locale="clr-namespace:Ryujinx.Ava.Common.Locale"
xmlns:viewModels="clr-namespace:Ryujinx.Ava.UI.ViewModels.Settings" xmlns:viewModels="clr-namespace:Ryujinx.Ava.UI.ViewModels.Settings"
mc:Ignorable="d" mc:Ignorable="d"
x:DataType="viewModels:SettingsViewModel"> x:DataType="viewModels:SettingsAudioViewModel">
<Design.DataContext> <Design.DataContext>
<viewModels:SettingsViewModel /> <viewModels:SettingsAudioViewModel />
</Design.DataContext> </Design.DataContext>
<ScrollViewer <ScrollViewer
Name="AudioPage" Name="AudioPage"

View file

@ -1,11 +1,15 @@
using Avalonia.Controls; using Avalonia.Controls;
using Ryujinx.Ava.UI.ViewModels.Settings;
namespace Ryujinx.Ava.UI.Views.Settings namespace Ryujinx.Ava.UI.Views.Settings
{ {
public partial class SettingsAudioView : UserControl public partial class SettingsAudioView : UserControl
{ {
public SettingsAudioViewModel ViewModel;
public SettingsAudioView() public SettingsAudioView()
{ {
DataContext = ViewModel = new SettingsAudioViewModel();
InitializeComponent(); InitializeComponent();
} }
} }

View file

@ -18,6 +18,11 @@ namespace Ryujinx.Ava.UI.Views.Settings
private ButtonKeyAssigner _currentAssigner; private ButtonKeyAssigner _currentAssigner;
private readonly IGamepadDriver _avaloniaKeyboardDriver; private readonly IGamepadDriver _avaloniaKeyboardDriver;
public SettingsHotkeysView()
{
}
public SettingsHotkeysView(SettingsViewModel viewModel) public SettingsHotkeysView(SettingsViewModel viewModel)
{ {
_viewModel = viewModel; _viewModel = viewModel;

View file

@ -29,13 +29,12 @@ namespace Ryujinx.Ava.UI.Windows
{ {
Title = $"{LocaleManager.Instance[LocaleKeys.Settings]}"; Title = $"{LocaleManager.Instance[LocaleKeys.Settings]}";
ViewModel = new SettingsViewModel(virtualFileSystem, contentManager); AudioPage = new SettingsAudioView();
DataContext = ViewModel;
ViewModel.CloseWindow += Close; ViewModel = new SettingsViewModel(
ViewModel.SaveSettingsEvent += SaveSettings; virtualFileSystem,
ViewModel.DirtyEvent += UpdateDirtyTitle; contentManager,
ViewModel.ToggleButtons += ToggleButtons; AudioPage.ViewModel);
UiPage = new SettingsUiView(ViewModel); UiPage = new SettingsUiView(ViewModel);
InputPage = new SettingsInputView(ViewModel); InputPage = new SettingsInputView(ViewModel);
@ -43,10 +42,16 @@ namespace Ryujinx.Ava.UI.Windows
SystemPage = new SettingsSystemView(ViewModel); SystemPage = new SettingsSystemView(ViewModel);
CpuPage = new SettingsCPUView(); CpuPage = new SettingsCPUView();
GraphicsPage = new SettingsGraphicsView(); GraphicsPage = new SettingsGraphicsView();
AudioPage = new SettingsAudioView();
NetworkPage = new SettingsNetworkView(); NetworkPage = new SettingsNetworkView();
LoggingPage = new SettingsLoggingView(); LoggingPage = new SettingsLoggingView();
DataContext = ViewModel;
ViewModel.CloseWindow += Close;
ViewModel.SaveSettingsEvent += SaveSettings;
ViewModel.DirtyEvent += UpdateDirtyTitle;
ViewModel.ToggleButtons += ToggleButtons;
InitializeComponent(); InitializeComponent();
Load(); Load();
} }