From 49b37550cae6b3c69f59a9c7a44b17e3c12a813b Mon Sep 17 00:00:00 2001
From: Isaac Marovitz <42140194+IsaacMarovitz@users.noreply.github.com>
Date: Sat, 21 Oct 2023 07:26:51 -0400
Subject: Ava UI: Input Menu Refactor (#4998)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* So much boilerplate

* Slow and steady

* Restructure + Ack suggestions

* Restructure + Ack suggestions

* Restructure

* Clean

* Propogate those fields i forgot about

* It builds

* Progress

* Almost there

* Fix stupid mistake

* Fix more stupid mistakes

* Actually fix fuck ups

* Start localising

* r/therestofthefuckingowl

* Localise ButtonKeyAssigner

* Are you feeling it now mr krabs

* We’re done at last

* Crimes against code

* Try me in the Hague

* Please be quiet

* Crimes are here to stay

* Dispose stuff

* Cleanup a couple things

* Visual fixes and improvements

One weird bug

* Fix rebase errors

* Fixes

* Ack Suggestions

Remaining ack suggestions

Update src/Ryujinx.Ava/UI/Models/Input/ControllerInputConfig.cs

Co-authored-by: Ac_K <Acoustik666@gmail.com>

Update src/Ryujinx.Ava/UI/Models/Input/ControllerInputConfig.cs

Co-authored-by: Ac_K <Acoustik666@gmail.com>

* Formatting and error

More Ava 11-ness

Whoops

* Code style fixes

* Style fixes

* Analyzer fix

* Remove all ReflectionBindings

* Remove ambigious object

* Remove redundant property

* Old man yells at formatter

* r e a d o n l y

* Fix profiles

* Use new Sliders

---------

Co-authored-by: Ac_K <Acoustik666@gmail.com>
---
 .../UI/Views/Input/ControllerInputView.axaml.cs    | 160 +++++++++++----------
 1 file changed, 86 insertions(+), 74 deletions(-)

(limited to 'src/Ryujinx.Ava/UI/Views/Input/ControllerInputView.axaml.cs')

diff --git a/src/Ryujinx.Ava/UI/Views/Input/ControllerInputView.axaml.cs b/src/Ryujinx.Ava/UI/Views/Input/ControllerInputView.axaml.cs
index 35129706..52fc6238 100644
--- a/src/Ryujinx.Ava/UI/Views/Input/ControllerInputView.axaml.cs
+++ b/src/Ryujinx.Ava/UI/Views/Input/ControllerInputView.axaml.cs
@@ -1,35 +1,28 @@
+using Avalonia;
 using Avalonia.Controls;
 using Avalonia.Controls.Primitives;
 using Avalonia.Input;
 using Avalonia.Interactivity;
 using Avalonia.LogicalTree;
-using Ryujinx.Ava.Common.Locale;
 using Ryujinx.Ava.UI.Helpers;
-using Ryujinx.Ava.UI.Models;
-using Ryujinx.Ava.UI.ViewModels;
+using Ryujinx.Ava.UI.ViewModels.Input;
 using Ryujinx.Common.Configuration.Hid.Controller;
 using Ryujinx.Input;
 using Ryujinx.Input.Assigner;
-using System;
 
 namespace Ryujinx.Ava.UI.Views.Input
 {
     public partial class ControllerInputView : UserControl
     {
-        private bool _dialogOpen;
-
         private ButtonKeyAssigner _currentAssigner;
-        internal ControllerInputViewModel ViewModel { get; set; }
 
         public ControllerInputView()
         {
-            DataContext = ViewModel = new ControllerInputViewModel(this);
-
             InitializeComponent();
 
             foreach (ILogical visual in SettingButtons.GetLogicalDescendants())
             {
-                if (visual is ToggleButton button && visual is not CheckBox)
+                if (visual is ToggleButton button and not CheckBox)
                 {
                     button.IsCheckedChanged += Button_IsCheckedChanged;
                 }
@@ -59,7 +52,7 @@ namespace Ryujinx.Ava.UI.Views.Input
 
                     bool isStick = button.Tag != null && button.Tag.ToString() == "stick";
 
-                    if (_currentAssigner == null)
+                    if (_currentAssigner == null && (bool)button.IsChecked)
                     {
                         _currentAssigner = new ButtonKeyAssigner(button);
 
@@ -67,14 +60,86 @@ namespace Ryujinx.Ava.UI.Views.Input
 
                         PointerPressed += MouseClick;
 
-                        IKeyboard keyboard = (IKeyboard)ViewModel.AvaloniaKeyboardDriver.GetGamepad("0"); // Open Avalonia keyboard for cancel operations.
+                        IKeyboard keyboard = (IKeyboard)(DataContext as ControllerInputViewModel).parentModel.AvaloniaKeyboardDriver.GetGamepad("0"); // Open Avalonia keyboard for cancel operations.
                         IButtonAssigner assigner = CreateButtonAssigner(isStick);
 
                         _currentAssigner.ButtonAssigned += (sender, e) =>
                         {
-                            if (e.IsAssigned)
+                            if (e.ButtonValue.HasValue)
                             {
-                                ViewModel.IsModified = true;
+                                var viewModel = (DataContext as ControllerInputViewModel);
+                                var buttonValue = e.ButtonValue.Value;
+                                viewModel.parentModel.IsModified = true;
+
+                                switch (button.Name)
+                                {
+                                    case "ButtonZl":
+                                        viewModel.Config.ButtonZl = buttonValue.AsGamepadButtonInputId();
+                                        break;
+                                    case "ButtonL":
+                                        viewModel.Config.ButtonL = buttonValue.AsGamepadButtonInputId();
+                                        break;
+                                    case "ButtonMinus":
+                                        viewModel.Config.ButtonMinus = buttonValue.AsGamepadButtonInputId();
+                                        break;
+                                    case "LeftStickButton":
+                                        viewModel.Config.LeftStickButton = buttonValue.AsGamepadButtonInputId();
+                                        break;
+                                    case "LeftJoystick":
+                                        viewModel.Config.LeftJoystick = buttonValue.AsGamepadStickId();
+                                        break;
+                                    case "DpadUp":
+                                        viewModel.Config.DpadUp = buttonValue.AsGamepadButtonInputId();
+                                        break;
+                                    case "DpadDown":
+                                        viewModel.Config.DpadDown = buttonValue.AsGamepadButtonInputId();
+                                        break;
+                                    case "DpadLeft":
+                                        viewModel.Config.DpadLeft = buttonValue.AsGamepadButtonInputId();
+                                        break;
+                                    case "DpadRight":
+                                        viewModel.Config.DpadRight = buttonValue.AsGamepadButtonInputId();
+                                        break;
+                                    case "LeftButtonSr":
+                                        viewModel.Config.LeftButtonSr = buttonValue.AsGamepadButtonInputId();
+                                        break;
+                                    case "LeftButtonSl":
+                                        viewModel.Config.LeftButtonSl = buttonValue.AsGamepadButtonInputId();
+                                        break;
+                                    case "RightButtonSr":
+                                        viewModel.Config.RightButtonSr = buttonValue.AsGamepadButtonInputId();
+                                        break;
+                                    case "RightButtonSl":
+                                        viewModel.Config.RightButtonSl = buttonValue.AsGamepadButtonInputId();
+                                        break;
+                                    case "ButtonZr":
+                                        viewModel.Config.ButtonZr = buttonValue.AsGamepadButtonInputId();
+                                        break;
+                                    case "ButtonR":
+                                        viewModel.Config.ButtonR = buttonValue.AsGamepadButtonInputId();
+                                        break;
+                                    case "ButtonPlus":
+                                        viewModel.Config.ButtonPlus = buttonValue.AsGamepadButtonInputId();
+                                        break;
+                                    case "ButtonA":
+                                        viewModel.Config.ButtonA = buttonValue.AsGamepadButtonInputId();
+                                        break;
+                                    case "ButtonB":
+                                        viewModel.Config.ButtonB = buttonValue.AsGamepadButtonInputId();
+                                        break;
+                                    case "ButtonX":
+                                        viewModel.Config.ButtonX = buttonValue.AsGamepadButtonInputId();
+                                        break;
+                                    case "ButtonY":
+                                        viewModel.Config.ButtonY = buttonValue.AsGamepadButtonInputId();
+                                        break;
+                                    case "RightStickButton":
+                                        viewModel.Config.RightStickButton = buttonValue.AsGamepadButtonInputId();
+                                        break;
+                                    case "RightJoystick":
+                                        viewModel.Config.RightJoystick = buttonValue.AsGamepadStickId();
+                                        break;
+                                }
                             }
                         };
 
@@ -100,82 +165,29 @@ namespace Ryujinx.Ava.UI.Views.Input
             }
         }
 
-        public void SaveCurrentProfile()
-        {
-            ViewModel.Save();
-        }
-
-        private IButtonAssigner CreateButtonAssigner(bool forStick)
-        {
-            IButtonAssigner assigner;
-
-            var device = ViewModel.Devices[ViewModel.Device];
-
-            if (device.Type == DeviceType.Keyboard)
-            {
-                assigner = new KeyboardKeyAssigner((IKeyboard)ViewModel.SelectedGamepad);
-            }
-            else if (device.Type == DeviceType.Controller)
-            {
-                assigner = new GamepadButtonAssigner(ViewModel.SelectedGamepad, (ViewModel.Config as StandardControllerInputConfig).TriggerThreshold, forStick);
-            }
-            else
-            {
-                throw new Exception("Controller not supported");
-            }
-
-            return assigner;
-        }
-
         private void MouseClick(object sender, PointerPressedEventArgs e)
         {
-            bool shouldUnbind = false;
-
-            if (e.GetCurrentPoint(this).Properties.IsMiddleButtonPressed)
-            {
-                shouldUnbind = true;
-            }
+            bool shouldUnbind = e.GetCurrentPoint(this).Properties.IsMiddleButtonPressed;
 
             _currentAssigner?.Cancel(shouldUnbind);
 
             PointerPressed -= MouseClick;
         }
 
-        private async void PlayerIndexBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
+        private IButtonAssigner CreateButtonAssigner(bool forStick)
         {
-            if (ViewModel.IsModified && !_dialogOpen)
-            {
-                _dialogOpen = true;
-
-                var result = await ContentDialogHelper.CreateConfirmationDialog(
-                    LocaleManager.Instance[LocaleKeys.DialogControllerSettingsModifiedConfirmMessage],
-                    LocaleManager.Instance[LocaleKeys.DialogControllerSettingsModifiedConfirmSubMessage],
-                    LocaleManager.Instance[LocaleKeys.InputDialogYes],
-                    LocaleManager.Instance[LocaleKeys.InputDialogNo],
-                    LocaleManager.Instance[LocaleKeys.RyujinxConfirm]);
-
-                if (result == UserResult.Yes)
-                {
-                    ViewModel.Save();
-                }
-
-                _dialogOpen = false;
+            IButtonAssigner assigner;
 
-                ViewModel.IsModified = false;
+            assigner = new GamepadButtonAssigner((DataContext as ControllerInputViewModel).parentModel.SelectedGamepad, ((DataContext as ControllerInputViewModel).parentModel.Config as StandardControllerInputConfig).TriggerThreshold, forStick);
 
-                if (e.AddedItems.Count > 0)
-                {
-                    var player = (PlayerModel)e.AddedItems[0];
-                    ViewModel.PlayerId = player.Id;
-                }
-            }
+            return assigner;
         }
 
-        public void Dispose()
+        protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
         {
+            base.OnDetachedFromVisualTree(e);
             _currentAssigner?.Cancel();
             _currentAssigner = null;
-            ViewModel.Dispose();
         }
     }
 }
-- 
cgit v1.2.3-70-g09d2