Rework framework to allow for more types of mouse-to-something emulation and hook up gyro to it

This commit is contained in:
kalaposfos13 2025-06-19 21:28:02 +02:00
parent be12305f65
commit b3dd428243
3 changed files with 51 additions and 14 deletions

View File

@ -4,6 +4,7 @@
#include <cmath> #include <cmath>
#include "common/types.h" #include "common/types.h"
#include "common/assert.h"
#include "input/controller.h" #include "input/controller.h"
#include "input_mouse.h" #include "input_mouse.h"
@ -14,11 +15,17 @@ namespace Input {
int mouse_joystick_binding = 0; int mouse_joystick_binding = 0;
float mouse_deadzone_offset = 0.5, mouse_speed = 1, mouse_speed_offset = 0.1250; float mouse_deadzone_offset = 0.5, mouse_speed = 1, mouse_speed_offset = 0.1250;
Uint32 mouse_polling_id = 0; Uint32 mouse_polling_id = 0;
bool mouse_enabled = false; MouseMode mouse_mode = MouseMode::Off;
// We had to go through 3 files of indirection just to update a flag // Switches mouse to a set mode or turns mouse emulation off if it was already in that mode.
void ToggleMouseEnabled() { // Returns whether the mode is turned on.
mouse_enabled = !mouse_enabled; bool ToggleMouseModeTo(MouseMode m) {
if(mouse_mode == m) {
mouse_mode = MouseMode::Off;
} else {
mouse_mode = m;
}
return mouse_mode == m;
} }
void SetMouseToJoystick(int joystick) { void SetMouseToJoystick(int joystick) {
@ -31,10 +38,7 @@ void SetMouseParams(float mdo, float ms, float mso) {
mouse_speed_offset = mso; mouse_speed_offset = mso;
} }
Uint32 MousePolling(void* param, Uint32 id, Uint32 interval) { void EmulateJoystick(GameController* controller, u32 interval) {
auto* controller = (GameController*)param;
if (!mouse_enabled)
return interval;
Axis axis_x, axis_y; Axis axis_x, axis_y;
switch (mouse_joystick_binding) { switch (mouse_joystick_binding) {
@ -47,7 +51,7 @@ Uint32 MousePolling(void* param, Uint32 id, Uint32 interval) {
axis_y = Axis::RightY; axis_y = Axis::RightY;
break; break;
default: default:
return interval; // no update needed return; // no update needed
} }
float d_x = 0, d_y = 0; float d_x = 0, d_y = 0;
@ -67,7 +71,26 @@ Uint32 MousePolling(void* param, Uint32 id, Uint32 interval) {
controller->Axis(0, axis_x, GetAxis(-0x80, 0x7f, 0)); controller->Axis(0, axis_x, GetAxis(-0x80, 0x7f, 0));
controller->Axis(0, axis_y, GetAxis(-0x80, 0x7f, 0)); controller->Axis(0, axis_y, GetAxis(-0x80, 0x7f, 0));
} }
}
void EmulateGyro(GameController* controller, u32 interval) {
LOG_INFO(Input, "todo gyro");
}
Uint32 MousePolling(void* param, Uint32 id, Uint32 interval) {
auto* controller = (GameController*)param;
switch (mouse_mode)
{
case MouseMode::Joystick:
EmulateJoystick(controller, interval);
break;
case MouseMode::Gyro:
EmulateGyro(controller, interval);
break;
default:
break;
}
return interval; return interval;
} }

View File

@ -8,11 +8,20 @@
namespace Input { namespace Input {
void ToggleMouseEnabled(); enum MouseMode {
Off = 0,
Joystick,
Gyro,
};
bool ToggleMouseModeTo(MouseMode m);
void SetMouseToJoystick(int joystick); void SetMouseToJoystick(int joystick);
void SetMouseParams(float mouse_deadzone_offset, float mouse_speed, float mouse_speed_offset); void SetMouseParams(float mouse_deadzone_offset, float mouse_speed, float mouse_speed_offset);
// Polls the mouse for changes, and simulates joystick movement from it. void EmulateJoystick(GameController* controller, u32 interval);
void EmulateGyro(GameController* controller, u32 interval);
// Polls the mouse for changes
Uint32 MousePolling(void* param, Uint32 id, Uint32 interval); Uint32 MousePolling(void* param, Uint32 id, Uint32 interval);
} // namespace Input } // namespace Input

View File

@ -474,11 +474,16 @@ void WindowSDL::OnKeyboardMouseInput(const SDL_Event* event) {
Input::ParseInputConfig(std::string(Common::ElfInfo::Instance().GameSerial())); Input::ParseInputConfig(std::string(Common::ElfInfo::Instance().GameSerial()));
return; return;
} }
// Toggle mouse capture and movement input // Toggle mouse capture and joystick input emulation
else if (input_id == SDLK_F7) { else if (input_id == SDLK_F7) {
Input::ToggleMouseEnabled();
SDL_SetWindowRelativeMouseMode(this->GetSDLWindow(), SDL_SetWindowRelativeMouseMode(this->GetSDLWindow(),
!SDL_GetWindowRelativeMouseMode(this->GetSDLWindow())); Input::ToggleMouseModeTo(Input::MouseMode::Joystick));
return;
}
// Toggle mouse capture and gyro input emulation
else if (input_id == SDLK_F7) {
SDL_SetWindowRelativeMouseMode(this->GetSDLWindow(),
Input::ToggleMouseModeTo(Input::MouseMode::Gyro));
return; return;
} }
// Toggle fullscreen // Toggle fullscreen