From 198dec94125661d46384a2e9550d2e06b26ac80f Mon Sep 17 00:00:00 2001 From: staphen Date: Sun, 30 Oct 2022 21:04:50 -0400 Subject: [PATCH] Iron out interactions between simulated mouse movement and character movement --- Source/controls/controller_motion.cpp | 28 +++++++++++++++++---------- Source/options.cpp | 15 ++++++++++++++ Source/options.h | 1 + 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/Source/controls/controller_motion.cpp b/Source/controls/controller_motion.cpp index c35b7c6e9..26b7c1fbb 100644 --- a/Source/controls/controller_motion.cpp +++ b/Source/controls/controller_motion.cpp @@ -96,6 +96,9 @@ bool SimulateRightStickWithDpad(ControllerButtonEvent ctrlEvent) rightStickX = 0; rightStickY = 0; + + // Cannot use PadmapperOptions::IsActive() because this function + // is invoked before PadmapperOptions::ButtonPressed() if (IsControllerButtonComboPressed(upCombo)) rightStickY += 1.F; if (IsControllerButtonComboPressed(downCombo)) @@ -104,8 +107,17 @@ bool SimulateRightStickWithDpad(ControllerButtonEvent ctrlEvent) rightStickX -= 1.F; if (IsControllerButtonComboPressed(rightCombo)) rightStickX += 1.F; - if (rightStickX != 0 || rightStickY != 0) - SetSimulatingMouseWithPadmapper(true); + + if (rightStickX == 0 && rightStickY == 0) { + // In this case, PadmapperOptions::IsActive() can be used to anticipate PadmapperOptions::ButtonReleased() + bool upReleased = ctrlEvent.up && ctrlEvent.button == upCombo.button && sgOptions.Padmapper.IsActive("MouseUp"); + bool downReleased = ctrlEvent.up && ctrlEvent.button == downCombo.button && sgOptions.Padmapper.IsActive("MouseDown"); + bool leftReleased = ctrlEvent.up && ctrlEvent.button == leftCombo.button && sgOptions.Padmapper.IsActive("MouseLeft"); + bool rightReleased = ctrlEvent.up && ctrlEvent.button == rightCombo.button && sgOptions.Padmapper.IsActive("MouseRight"); + return upReleased || downReleased || leftReleased || rightReleased; + } + + SetSimulatingMouseWithPadmapper(true); return true; } @@ -178,14 +190,10 @@ AxisDirection GetLeftStickOrDpadDirection(bool usePadmapper) bool isRightPressed = stickX >= 0.5; if (usePadmapper) { - ControllerButtonCombo upCombo = sgOptions.Padmapper.ButtonComboForAction("MoveUp"); - ControllerButtonCombo downCombo = sgOptions.Padmapper.ButtonComboForAction("MoveDown"); - ControllerButtonCombo leftCombo = sgOptions.Padmapper.ButtonComboForAction("MoveLeft"); - ControllerButtonCombo rightCombo = sgOptions.Padmapper.ButtonComboForAction("MoveRight"); - isUpPressed |= IsControllerButtonComboPressed(upCombo); - isDownPressed |= IsControllerButtonComboPressed(downCombo); - isLeftPressed |= IsControllerButtonComboPressed(leftCombo); - isRightPressed |= IsControllerButtonComboPressed(rightCombo); + isUpPressed |= sgOptions.Padmapper.IsActive("MoveUp"); + isDownPressed |= sgOptions.Padmapper.IsActive("MoveDown"); + isLeftPressed |= sgOptions.Padmapper.IsActive("MoveLeft"); + isRightPressed |= sgOptions.Padmapper.IsActive("MoveRight"); } else { isUpPressed |= IsControllerButtonPressed(ControllerButton_BUTTON_DPAD_UP); isDownPressed |= IsControllerButtonPressed(ControllerButton_BUTTON_DPAD_DOWN); diff --git a/Source/options.cpp b/Source/options.cpp index 9e7a93b56..627626abd 100644 --- a/Source/options.cpp +++ b/Source/options.cpp @@ -1648,6 +1648,21 @@ void PadmapperOptions::ButtonReleased(ControllerButton button, bool invokeAction buttonToReleaseAction.erase(button); } +bool PadmapperOptions::IsActive(string_view actionName) const +{ + for (const Action &action : actions) { + if (action.key != actionName) + continue; + ControllerButton button = action.boundInput.button; + auto it = buttonToReleaseAction.find(button); + if (it == buttonToReleaseAction.end()) + return false; + const Action &releaseAction = it->second.get(); + return releaseAction.key == actionName; + } + return false; +} + string_view PadmapperOptions::InputNameForAction(string_view actionName) const { for (const Action &action : actions) { diff --git a/Source/options.h b/Source/options.h index 68ccd4b32..2a90fb52a 100644 --- a/Source/options.h +++ b/Source/options.h @@ -738,6 +738,7 @@ struct PadmapperOptions : OptionCategoryBase { void CommitActions(); void ButtonPressed(ControllerButton button); void ButtonReleased(ControllerButton button, bool invokeAction = true); + bool IsActive(string_view actionName) const; string_view InputNameForAction(string_view actionName) const; ControllerButtonCombo ButtonComboForAction(string_view actionName) const;