SettingsWindow.axaml.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using Avalonia.Controls;
  2. using Avalonia.Controls.Primitives;
  3. using Avalonia.Data;
  4. using Avalonia.Data.Converters;
  5. using Avalonia.Input;
  6. using Avalonia.Interactivity;
  7. using FluentAvalonia.Core;
  8. using FluentAvalonia.UI.Controls;
  9. using Ryujinx.Ava.Common.Locale;
  10. using Ryujinx.Ava.Ui.Controls;
  11. using Ryujinx.Ava.Ui.ViewModels;
  12. using Ryujinx.HLE.FileSystem;
  13. using Ryujinx.Input;
  14. using Ryujinx.Input.Assigner;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.IO;
  18. using System.Linq;
  19. using TimeZone = Ryujinx.Ava.Ui.Models.TimeZone;
  20. namespace Ryujinx.Ava.Ui.Windows
  21. {
  22. public partial class SettingsWindow : StyleableWindow
  23. {
  24. private ButtonKeyAssigner _currentAssigner;
  25. internal SettingsViewModel ViewModel { get; set; }
  26. public SettingsWindow(VirtualFileSystem virtualFileSystem, ContentManager contentManager)
  27. {
  28. Title = $"Ryujinx {Program.Version} - {LocaleManager.Instance["Settings"]}";
  29. ViewModel = new SettingsViewModel(virtualFileSystem, contentManager, this);
  30. DataContext = ViewModel;
  31. InitializeComponent();
  32. Load();
  33. FuncMultiValueConverter<string, string> converter = new(parts => string.Format("{0} {1} {2}", parts.ToArray()).Trim());
  34. MultiBinding tzMultiBinding = new() { Converter = converter };
  35. tzMultiBinding.Bindings.Add(new Binding("UtcDifference"));
  36. tzMultiBinding.Bindings.Add(new Binding("Location"));
  37. tzMultiBinding.Bindings.Add(new Binding("Abbreviation"));
  38. TimeZoneBox.ValueMemberBinding = tzMultiBinding;
  39. }
  40. public SettingsWindow()
  41. {
  42. ViewModel = new SettingsViewModel();
  43. DataContext = ViewModel;
  44. InitializeComponent();
  45. Load();
  46. }
  47. private void Load()
  48. {
  49. Pages.Children.Clear();
  50. NavPanel.SelectionChanged += NavPanelOnSelectionChanged;
  51. NavPanel.SelectedItem = NavPanel.MenuItems.ElementAt(0);
  52. }
  53. private void Button_Checked(object sender, RoutedEventArgs e)
  54. {
  55. if (sender is ToggleButton button)
  56. {
  57. if (_currentAssigner != null && button == _currentAssigner.ToggledButton)
  58. {
  59. return;
  60. }
  61. if (_currentAssigner == null && (bool)button.IsChecked)
  62. {
  63. _currentAssigner = new ButtonKeyAssigner(button);
  64. FocusManager.Instance.Focus(this, NavigationMethod.Pointer);
  65. PointerPressed += MouseClick;
  66. IKeyboard keyboard = (IKeyboard)ViewModel.AvaloniaKeyboardDriver.GetGamepad(ViewModel.AvaloniaKeyboardDriver.GamepadsIds[0]);
  67. IButtonAssigner assigner = new KeyboardKeyAssigner(keyboard);
  68. _currentAssigner.GetInputAndAssign(assigner);
  69. }
  70. else
  71. {
  72. if (_currentAssigner != null)
  73. {
  74. ToggleButton oldButton = _currentAssigner.ToggledButton;
  75. _currentAssigner.Cancel();
  76. _currentAssigner = null;
  77. button.IsChecked = false;
  78. }
  79. }
  80. }
  81. }
  82. private void Button_Unchecked(object sender, RoutedEventArgs e)
  83. {
  84. _currentAssigner?.Cancel();
  85. _currentAssigner = null;
  86. }
  87. private void MouseClick(object sender, PointerPressedEventArgs e)
  88. {
  89. bool shouldUnbind = false;
  90. if (e.GetCurrentPoint(this).Properties.IsMiddleButtonPressed)
  91. {
  92. shouldUnbind = true;
  93. }
  94. _currentAssigner?.Cancel(shouldUnbind);
  95. PointerPressed -= MouseClick;
  96. }
  97. private void NavPanelOnSelectionChanged(object sender, NavigationViewSelectionChangedEventArgs e)
  98. {
  99. if (e.SelectedItem is NavigationViewItem navitem)
  100. {
  101. NavPanel.Content = navitem.Tag.ToString() switch
  102. {
  103. "UiPage" => UiPage,
  104. "InputPage" => InputPage,
  105. "HotkeysPage" => HotkeysPage,
  106. "SystemPage" => SystemPage,
  107. "CpuPage" => CpuPage,
  108. "GraphicsPage" => GraphicsPage,
  109. "AudioPage" => AudioPage,
  110. "NetworkPage" => NetworkPage,
  111. "LoggingPage" => LoggingPage,
  112. _ => throw new NotImplementedException()
  113. };
  114. }
  115. }
  116. private async void AddButton_OnClick(object sender, RoutedEventArgs e)
  117. {
  118. string path = PathBox.Text;
  119. if (!string.IsNullOrWhiteSpace(path) && Directory.Exists(path) && !ViewModel.GameDirectories.Contains(path))
  120. {
  121. ViewModel.GameDirectories.Add(path);
  122. ViewModel.DirectoryChanged = true;
  123. }
  124. else
  125. {
  126. path = await new OpenFolderDialog().ShowAsync(this);
  127. if (!string.IsNullOrWhiteSpace(path))
  128. {
  129. ViewModel.GameDirectories.Add(path);
  130. ViewModel.DirectoryChanged = true;
  131. }
  132. }
  133. }
  134. private void RemoveButton_OnClick(object sender, RoutedEventArgs e)
  135. {
  136. int oldIndex = GameList.SelectedIndex;
  137. foreach (string path in new List<string>(GameList.SelectedItems.Cast<string>()))
  138. {
  139. ViewModel.GameDirectories.Remove(path);
  140. ViewModel.DirectoryChanged = true;
  141. }
  142. if (GameList.ItemCount > 0)
  143. {
  144. GameList.SelectedIndex = oldIndex < GameList.ItemCount ? oldIndex : 0;
  145. }
  146. }
  147. private void TimeZoneBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
  148. {
  149. if (e.AddedItems != null && e.AddedItems.Count > 0)
  150. {
  151. if (e.AddedItems[0] is TimeZone timeZone)
  152. {
  153. e.Handled = true;
  154. ViewModel.ValidateAndSetTimeZone(timeZone.Location);
  155. }
  156. }
  157. }
  158. private void TimeZoneBox_OnTextChanged(object sender, EventArgs e)
  159. {
  160. if (sender is AutoCompleteBox box)
  161. {
  162. if (box.SelectedItem != null && box.SelectedItem is TimeZone timeZone)
  163. {
  164. ViewModel.ValidateAndSetTimeZone(timeZone.Location);
  165. }
  166. }
  167. }
  168. protected override void OnClosed(EventArgs e)
  169. {
  170. ControllerSettings.Dispose();
  171. _currentAssigner?.Cancel();
  172. _currentAssigner = null;
  173. base.OnClosed(e);
  174. }
  175. }
  176. }