From 85d4d5ed4db2a5dc45187f5491f59b3ef677c215 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Sun, 29 Nov 2020 14:18:40 +1000 Subject: [PATCH] Pass global InputManager to JoypadApi via ctor --- src/BizHawk.Client.EmuHawk/Api/ApiManager.cs | 4 ++-- .../Api/Libraries/JoypadApi.cs | 23 +++++++++++-------- .../Api/Libraries/MovieApi.cs | 2 +- src/BizHawk.Client.EmuHawk/Api/UserDataApi.cs | 2 +- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/BizHawk.Client.EmuHawk/Api/ApiManager.cs b/src/BizHawk.Client.EmuHawk/Api/ApiManager.cs index d72c11fd20..f32fc73a56 100644 --- a/src/BizHawk.Client.EmuHawk/Api/ApiManager.cs +++ b/src/BizHawk.Client.EmuHawk/Api/ApiManager.cs @@ -24,7 +24,7 @@ namespace BizHawk.Client.EmuHawk private static readonly Type[] _ctorParamTypesB = { typeof(Action), typeof(IMainFormForApi) }; - private static readonly Type[] _ctorParamTypesC = { typeof(Action), typeof(IMovieSession) }; + private static readonly Type[] _ctorParamTypesC = { typeof(Action), typeof(InputManager), typeof(IMovieSession) }; private static readonly Type[] _ctorParamTypesD = { typeof(Action), typeof(IMainFormForApi), typeof(DisplayManager), typeof(InputManager), typeof(Config), typeof(IEmulator), typeof(IGameInfo) }; @@ -52,7 +52,7 @@ namespace BizHawk.Client.EmuHawk t => _apiTypes[t], t => (IExternalApi) ( t.GetConstructor(_ctorParamTypesD)?.Invoke(new object[] { logCallback, mainForm, displayManager, inputManager, config, emulator, game }) - ?? t.GetConstructor(_ctorParamTypesC)?.Invoke(new object[] { logCallback, movieSession }) + ?? t.GetConstructor(_ctorParamTypesC)?.Invoke(new object[] { logCallback, inputManager, movieSession }) ?? t.GetConstructor(_ctorParamTypesB)?.Invoke(new object[] { logCallback, mainForm }) ?? t.GetConstructor(_ctorParamTypesA)?.Invoke(new object[] { logCallback }) ?? t.GetConstructor(_ctorParamTypesTools)?.Invoke(new object[] { toolManager }) diff --git a/src/BizHawk.Client.EmuHawk/Api/Libraries/JoypadApi.cs b/src/BizHawk.Client.EmuHawk/Api/Libraries/JoypadApi.cs index cd45bd9abb..6e888d8c29 100644 --- a/src/BizHawk.Client.EmuHawk/Api/Libraries/JoypadApi.cs +++ b/src/BizHawk.Client.EmuHawk/Api/Libraries/JoypadApi.cs @@ -7,24 +7,27 @@ namespace BizHawk.Client.EmuHawk { public sealed class JoypadApi : IJoypadApi { + private readonly InputManager _inputManager; + private readonly IMovieSession _movieSession; private readonly Action LogCallback; - public JoypadApi(Action logCallback, IMovieSession movieSession) + public JoypadApi(Action logCallback, InputManager inputManager, IMovieSession movieSession) { LogCallback = logCallback; + _inputManager = inputManager; _movieSession = movieSession; } public IDictionary Get(int? controller = null) { - return GlobalWin.InputManager.AutofireStickyXorAdapter.ToDictionary(controller); + return _inputManager.AutofireStickyXorAdapter.ToDictionary(controller); } public IDictionary GetImmediate(int? controller = null) { - return GlobalWin.InputManager.ActiveController.ToDictionary(controller); + return _inputManager.ActiveController.ToDictionary(controller); } public void SetFromMnemonicStr(string inputLogEntry) @@ -39,15 +42,15 @@ namespace BizHawk.Client.EmuHawk LogCallback($"invalid mnemonic string: {inputLogEntry}"); return; } - foreach (var button in controller.Definition.BoolButtons) GlobalWin.InputManager.ButtonOverrideAdapter.SetButton(button, controller.IsPressed(button)); - foreach (var axis in controller.Definition.Axes.Keys) GlobalWin.InputManager.ButtonOverrideAdapter.SetAxis(axis, controller.AxisValue(axis)); + foreach (var button in controller.Definition.BoolButtons) _inputManager.ButtonOverrideAdapter.SetButton(button, controller.IsPressed(button)); + foreach (var axis in controller.Definition.Axes.Keys) _inputManager.ButtonOverrideAdapter.SetAxis(axis, controller.AxisValue(axis)); } public void Set(IDictionary buttons, int? controller = null) { // If a controller is specified, we need to iterate over unique button names. If not, we iterate over // ALL button names with P{controller} prefixes - foreach (var button in GlobalWin.InputManager.ActiveController.ToBoolButtonNameList(controller)) + foreach (var button in _inputManager.ActiveController.ToBoolButtonNameList(controller)) { Set(button, buttons.TryGetValue(button, out var state) ? state : (bool?) null, controller); } @@ -58,9 +61,9 @@ namespace BizHawk.Client.EmuHawk try { var buttonToSet = controller == null ? button : $"P{controller} {button}"; - if (state == null) GlobalWin.InputManager.ButtonOverrideAdapter.UnSet(buttonToSet); - else GlobalWin.InputManager.ButtonOverrideAdapter.SetButton(buttonToSet, state.Value); - GlobalWin.InputManager.ActiveController.Overrides(GlobalWin.InputManager.ButtonOverrideAdapter); + if (state == null) _inputManager.ButtonOverrideAdapter.UnSet(buttonToSet); + else _inputManager.ButtonOverrideAdapter.SetButton(buttonToSet, state.Value); + _inputManager.ActiveController.Overrides(_inputManager.ButtonOverrideAdapter); } catch { @@ -77,7 +80,7 @@ namespace BizHawk.Client.EmuHawk { try { - GlobalWin.InputManager.StickyXorAdapter.SetAxis(controller == null ? control : $"P{controller} {control}", value); + _inputManager.StickyXorAdapter.SetAxis(controller == null ? control : $"P{controller} {control}", value); } catch { diff --git a/src/BizHawk.Client.EmuHawk/Api/Libraries/MovieApi.cs b/src/BizHawk.Client.EmuHawk/Api/Libraries/MovieApi.cs index 049ca60373..60903a01c3 100644 --- a/src/BizHawk.Client.EmuHawk/Api/Libraries/MovieApi.cs +++ b/src/BizHawk.Client.EmuHawk/Api/Libraries/MovieApi.cs @@ -13,7 +13,7 @@ namespace BizHawk.Client.EmuHawk private readonly Action LogCallback; - public MovieApi(Action logCallback, IMovieSession movieSession) + public MovieApi(Action logCallback, InputManager inputManager, IMovieSession movieSession) { LogCallback = logCallback; _movieSession = movieSession; diff --git a/src/BizHawk.Client.EmuHawk/Api/UserDataApi.cs b/src/BizHawk.Client.EmuHawk/Api/UserDataApi.cs index 0b231e62dc..e5c2fb7a25 100644 --- a/src/BizHawk.Client.EmuHawk/Api/UserDataApi.cs +++ b/src/BizHawk.Client.EmuHawk/Api/UserDataApi.cs @@ -7,7 +7,7 @@ namespace BizHawk.Client.EmuHawk { private readonly IMovieSession _movieSession; - public UserDataApi(Action logCallback, IMovieSession movieSession) + public UserDataApi(Action logCallback, InputManager inputManager, IMovieSession movieSession) { _movieSession = movieSession; }