From 09856df67bee4fe59470a6b6ffec920a26b554b5 Mon Sep 17 00:00:00 2001 From: kalaposfos13 <153381648+kalaposfos13@users.noreply.github.com> Date: Wed, 1 Jan 2025 19:41:29 +0100 Subject: [PATCH] More work on review comments --- CMakeLists.txt | 2 + src/input/input_handler.cpp | 67 +++++----------------------------- src/input/input_handler.h | 10 +---- src/input/input_mouse.cpp | 73 +++++++++++++++++++++++++++++++++++++ src/input/input_mouse.h | 18 +++++++++ src/sdl_window.cpp | 1 + 6 files changed, 105 insertions(+), 66 deletions(-) create mode 100644 src/input/input_mouse.cpp create mode 100644 src/input/input_mouse.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 900eaa753..adb9adb2a 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -795,6 +795,8 @@ set(INPUT src/input/controller.cpp src/input/controller.h src/input/input_handler.cpp src/input/input_handler.h + src/input/input_mouse.cpp + src/input/input_mouse.h ) set(EMULATOR src/emulator.cpp diff --git a/src/input/input_handler.cpp b/src/input/input_handler.cpp index 884e0eeec..9bab958f6 100644 --- a/src/input/input_handler.cpp +++ b/src/input/input_handler.cpp @@ -24,6 +24,7 @@ #include "common/path_util.h" #include "common/version.h" #include "input/controller.h" +#include "input/input_mouse.h" namespace Input { /* @@ -55,10 +56,7 @@ Don't be an idiot and test only the changed part expecting everything else to no // Flags and values for varying purposes // todo: can we change these? -int mouse_joystick_binding = 0; -float mouse_deadzone_offset = 0.5, mouse_speed = 1, mouse_speed_offset = 0.1250; -Uint32 mouse_polling_id = 0; -bool mouse_enabled = false, leftjoystick_halfmode = false, rightjoystick_halfmode = false; +bool leftjoystick_halfmode = false, rightjoystick_halfmode = false; std::list> pressed_keys; std::list toggled_keys; @@ -98,11 +96,6 @@ auto output_array = std::array{ ControllerOutput(OrbisPadButtonDataOffset::None, Axis::AxisMax), }; -// We had to go through 3 files of indirection just to update a flag -void ToggleMouseEnabled() { - mouse_enabled ^= true; -} - // parsing related functions u32 GetAxisInputId(AxisMapping a) { // LOG_INFO(Input, "Parsing an axis..."); @@ -218,9 +211,9 @@ void ParseInputConfig(const std::string game_id = "") { // we reset these here so in case the user fucks up or doesn't include this, // we can fall back to default connections.clear(); - mouse_deadzone_offset = 0.5; - mouse_speed = 1; - mouse_speed_offset = 0.125; + float mouse_deadzone_offset = 0.5; + float mouse_speed = 1; + float mouse_speed_offset = 0.125; int lineCount = 0; std::ifstream file(config_file); @@ -260,11 +253,12 @@ void ParseInputConfig(const std::string game_id = "") { if (output_string == "mouse_to_joystick") { if (input_string == "left") { - mouse_joystick_binding = 1; + SetMouseToJoystick(1); } else if (input_string == "right") { - mouse_joystick_binding = 2; + SetMouseToJoystick(2); } else { - mouse_joystick_binding = 0; // default to 'none' or invalid + LOG_WARNING(Input, "Invalid argument for mouse-to-joystick binding"); + SetMouseToJoystick(0); } continue; } @@ -645,45 +639,4 @@ void ActivateOutputsFromInputs() { } } -Uint32 MousePolling(void* param, Uint32 id, Uint32 interval) { - auto* controller = (GameController*)param; - if (!mouse_enabled) - return interval; - - Axis axis_x, axis_y; - switch (mouse_joystick_binding) { - case 1: - axis_x = Axis::LeftX; - axis_y = Axis::LeftY; - break; - case 2: - axis_x = Axis::RightX; - axis_y = Axis::RightY; - break; - case 0: - default: - return interval; // no update needed - } - - float d_x = 0, d_y = 0; - SDL_GetRelativeMouseState(&d_x, &d_y); - - float output_speed = - SDL_clamp((sqrt(d_x * d_x + d_y * d_y) + mouse_speed_offset * 128) * mouse_speed, - mouse_deadzone_offset * 128, 128.0); - - float angle = atan2(d_y, d_x); - float a_x = cos(angle) * output_speed, a_y = sin(angle) * output_speed; - - if (d_x != 0 && d_y != 0) { - controller->Axis(0, axis_x, GetAxis(-0x80, 0x80, a_x)); - controller->Axis(0, axis_y, GetAxis(-0x80, 0x80, a_y)); - } else { - controller->Axis(0, axis_x, GetAxis(-0x80, 0x80, 0)); - controller->Axis(0, axis_y, GetAxis(-0x80, 0x80, 0)); - } - - return interval; -} - -} // namespace Input \ No newline at end of file +} // namespace Input diff --git a/src/input/input_handler.h b/src/input/input_handler.h index 05c006fe0..0a9f83f0d 100644 --- a/src/input/input_handler.h +++ b/src/input/input_handler.h @@ -188,9 +188,6 @@ const std::map string_to_keyboard_key_map = { {"capslock", SDLK_CAPSLOCK}, }; -// literally the only flag that needs external access -void ToggleMouseEnabled(); - void ParseInputConfig(const std::string game_id); class InputBinding { @@ -338,9 +335,4 @@ bool UpdatePressedKeys(u32 button, bool is_pressed); void ActivateOutputsFromInputs(); -void UpdateMouse(GameController* controller); - -// Polls the mouse for changes, and simulates joystick movement from it. -Uint32 MousePolling(void* param, Uint32 id, Uint32 interval); - -} // namespace Input \ No newline at end of file +} // namespace Input diff --git a/src/input/input_mouse.cpp b/src/input/input_mouse.cpp new file mode 100644 index 000000000..f36a70098 --- /dev/null +++ b/src/input/input_mouse.cpp @@ -0,0 +1,73 @@ +// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "input_mouse.h" + +#include "common/types.h" +#include "controller.h" + +#include "SDL3/SDL.h" + +namespace Input { + +int mouse_joystick_binding = 0; +float mouse_deadzone_offset = 0.5, mouse_speed = 1, mouse_speed_offset = 0.1250; +Uint32 mouse_polling_id = 0; +bool mouse_enabled = false; + +// We had to go through 3 files of indirection just to update a flag +void ToggleMouseEnabled() { + mouse_enabled = !mouse_enabled; +} + +void SetMouseToJoystick(int joystick) { + mouse_joystick_binding = joystick; +} + +void SetMouseParams(float mdo, float ms, float mso) { + mouse_deadzone_offset = mdo; + mouse_speed = ms; + mouse_speed_offset = mso; +} + +Uint32 MousePolling(void* param, Uint32 id, Uint32 interval) { + auto* controller = (GameController*)param; + if (!mouse_enabled) + return interval; + + Axis axis_x, axis_y; + switch (mouse_joystick_binding) { + case 1: + axis_x = Axis::LeftX; + axis_y = Axis::LeftY; + break; + case 2: + axis_x = Axis::RightX; + axis_y = Axis::RightY; + break; + default: + return interval; // no update needed + } + + float d_x = 0, d_y = 0; + SDL_GetRelativeMouseState(&d_x, &d_y); + + float output_speed = + SDL_clamp((sqrt(d_x * d_x + d_y * d_y) + mouse_speed_offset * 128) * mouse_speed, + mouse_deadzone_offset * 128, 128.0); + + float angle = atan2(d_y, d_x); + float a_x = cos(angle) * output_speed, a_y = sin(angle) * output_speed; + + if (d_x != 0 && d_y != 0) { + controller->Axis(0, axis_x, GetAxis(-0x80, 0x80, a_x)); + controller->Axis(0, axis_y, GetAxis(-0x80, 0x80, a_y)); + } else { + controller->Axis(0, axis_x, GetAxis(-0x80, 0x80, 0)); + controller->Axis(0, axis_y, GetAxis(-0x80, 0x80, 0)); + } + + return interval; +} + +} // namespace Input \ No newline at end of file diff --git a/src/input/input_mouse.h b/src/input/input_mouse.h new file mode 100644 index 000000000..da18ee04e --- /dev/null +++ b/src/input/input_mouse.h @@ -0,0 +1,18 @@ +// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "SDL3/SDL.h" +#include "common/types.h" + +namespace Input { + +void ToggleMouseEnabled(); +void SetMouseToJoystick(int joystick); +void SetMouseParams(float mouse_deadzone_offset, float mouse_speed, float mouse_speed_offset); + +// Polls the mouse for changes, and simulates joystick movement from it. +Uint32 MousePolling(void* param, Uint32 id, Uint32 interval); + +} // namespace Input diff --git a/src/sdl_window.cpp b/src/sdl_window.cpp index ad4f1ed0c..1dd5f553a 100644 --- a/src/sdl_window.cpp +++ b/src/sdl_window.cpp @@ -16,6 +16,7 @@ #include "imgui/renderer/imgui_core.h" #include "input/controller.h" #include "input/input_handler.h" +#include "input/input_mouse.h" #include "sdl_window.h" #include "video_core/renderdoc.h"