mirror of
https://github.com/shadps4-emu/shadPS4.git
synced 2025-08-04 08:22:32 +00:00
added better mouse handling with config options
This commit is contained in:
parent
649a5c0c0c
commit
92bbe40d8e
@ -137,75 +137,45 @@ std::map<std::string, AxisMapping> string_to_axis_map = {
|
|||||||
{"axis_right_y_minus", {Input::Axis::RightY, -127}},
|
{"axis_right_y_minus", {Input::Axis::RightY, -127}},
|
||||||
};
|
};
|
||||||
std::map<std::string, u32> string_to_keyboard_key_map = {
|
std::map<std::string, u32> string_to_keyboard_key_map = {
|
||||||
{"a", SDLK_A},
|
{"a", SDLK_A}, {"b", SDLK_B}, {"c", SDLK_C}, {"d", SDLK_D},
|
||||||
{"b", SDLK_B},
|
{"e", SDLK_E}, {"f", SDLK_F}, {"g", SDLK_G}, {"h", SDLK_H},
|
||||||
{"c", SDLK_C},
|
{"i", SDLK_I}, {"j", SDLK_J}, {"k", SDLK_K}, {"l", SDLK_L},
|
||||||
{"d", SDLK_D},
|
{"m", SDLK_M}, {"n", SDLK_N}, {"o", SDLK_O}, {"p", SDLK_P},
|
||||||
{"e", SDLK_E},
|
{"q", SDLK_Q}, {"r", SDLK_R}, {"s", SDLK_S}, {"t", SDLK_T},
|
||||||
{"f", SDLK_F},
|
{"u", SDLK_U}, {"v", SDLK_V}, {"w", SDLK_W}, {"x", SDLK_X},
|
||||||
{"g", SDLK_G},
|
{"y", SDLK_Y}, {"z", SDLK_Z},
|
||||||
{"h", SDLK_H},
|
{"0", SDLK_0}, {"1", SDLK_1}, {"2", SDLK_2}, {"3", SDLK_3},
|
||||||
{"i", SDLK_I},
|
{"4", SDLK_4}, {"5", SDLK_5}, {"6", SDLK_6}, {"7", SDLK_7},
|
||||||
{"j", SDLK_J},
|
{"8", SDLK_8}, {"9", SDLK_9},
|
||||||
{"k", SDLK_K},
|
{"kp0", SDLK_KP_0}, {"kp1", SDLK_KP_1}, {"kp2", SDLK_KP_2}, {"kp3", SDLK_KP_3},
|
||||||
{"l", SDLK_L},
|
{"kp4", SDLK_KP_4}, {"kp5", SDLK_KP_5}, {"kp6", SDLK_KP_6}, {"kp7", SDLK_KP_7},
|
||||||
{"m", SDLK_M},
|
{"kp8", SDLK_KP_8}, {"kp9", SDLK_KP_9},
|
||||||
{"n", SDLK_N},
|
{",", SDLK_COMMA},
|
||||||
{"o", SDLK_O},
|
{".", SDLK_PERIOD},
|
||||||
{"p", SDLK_P},
|
{"?", SDLK_QUESTION},
|
||||||
{"q", SDLK_Q},
|
{";", SDLK_SEMICOLON},
|
||||||
{"r", SDLK_R},
|
{"-", SDLK_MINUS},
|
||||||
{"s", SDLK_S},
|
{"_", SDLK_UNDERSCORE},
|
||||||
{"t", SDLK_T},
|
{"(", SDLK_LEFTPAREN},
|
||||||
{"u", SDLK_U},
|
{")", SDLK_RIGHTPAREN},
|
||||||
{"v", SDLK_V},
|
{"[", SDLK_LEFTBRACKET},
|
||||||
{"w", SDLK_W},
|
{"]", SDLK_RIGHTBRACKET},
|
||||||
{"x", SDLK_X},
|
{"{", SDLK_LEFTBRACE},
|
||||||
{"y", SDLK_Y},
|
{"}", SDLK_RIGHTBRACE},
|
||||||
{"z", SDLK_Z},
|
{"\\", SDLK_BACKSLASH},
|
||||||
{"0", SDLK_0},
|
{"/", SDLK_SLASH},
|
||||||
{"1", SDLK_1},
|
|
||||||
{"2", SDLK_2},
|
|
||||||
{"3", SDLK_3},
|
|
||||||
{"4", SDLK_4},
|
|
||||||
{"5", SDLK_5},
|
|
||||||
{"6", SDLK_6},
|
|
||||||
{"7", SDLK_7},
|
|
||||||
{"8", SDLK_8},
|
|
||||||
{"9", SDLK_9},
|
|
||||||
{"comma", SDLK_COMMA},
|
|
||||||
{"period", SDLK_PERIOD},
|
|
||||||
{"question", SDLK_QUESTION},
|
|
||||||
{"semicolon", SDLK_SEMICOLON},
|
|
||||||
{"minus", SDLK_MINUS},
|
|
||||||
{"underscore", SDLK_UNDERSCORE},
|
|
||||||
{"lparenthesis", SDLK_LEFTPAREN},
|
|
||||||
{"rparenthesis", SDLK_RIGHTPAREN},
|
|
||||||
{"lbracket", SDLK_LEFTBRACKET},
|
|
||||||
{"rbracket", SDLK_RIGHTBRACKET},
|
|
||||||
{"lbrace", SDLK_LEFTBRACE},
|
|
||||||
{"rbrace", SDLK_RIGHTBRACE},
|
|
||||||
{"backslash", SDLK_BACKSLASH},
|
|
||||||
{"dash", SDLK_SLASH},
|
|
||||||
{"enter", SDLK_RETURN},
|
{"enter", SDLK_RETURN},
|
||||||
{"space", SDLK_SPACE},
|
{"space", SDLK_SPACE},
|
||||||
{"tab", SDLK_TAB},
|
{"tab", SDLK_TAB},
|
||||||
{"backspace", SDLK_BACKSPACE},
|
{"backspace", SDLK_BACKSPACE},
|
||||||
{"escape", SDLK_ESCAPE},
|
{"escape", SDLK_ESCAPE},
|
||||||
{"left", SDLK_LEFT},
|
{"left", SDLK_LEFT}, {"right", SDLK_RIGHT},
|
||||||
{"right", SDLK_RIGHT},
|
{"up", SDLK_UP}, {"down", SDLK_DOWN},
|
||||||
{"up", SDLK_UP},
|
{"lctrl", SDLK_LCTRL}, {"rctrl", SDLK_RCTRL},
|
||||||
{"down", SDLK_DOWN},
|
{"lshift", SDLK_LSHIFT}, {"rshift", SDLK_RSHIFT},
|
||||||
{"lctrl", SDLK_LCTRL},
|
{"lalt", SDLK_LALT}, {"ralt", SDLK_RALT},
|
||||||
{"rctrl", SDLK_RCTRL},
|
{"lmeta", SDLK_LGUI}, {"rmeta", SDLK_RGUI},
|
||||||
{"lshift", SDLK_LSHIFT},
|
{"lwin", SDLK_LGUI}, {"rwin", SDLK_RGUI},
|
||||||
{"rshift", SDLK_RSHIFT},
|
|
||||||
{"lalt", SDLK_LALT},
|
|
||||||
{"ralt", SDLK_RALT},
|
|
||||||
{"lmeta", SDLK_LGUI},
|
|
||||||
{"rmeta", SDLK_RGUI},
|
|
||||||
{"lwin", SDLK_LGUI},
|
|
||||||
{"rwin", SDLK_RGUI},
|
|
||||||
{"leftbutton", SDL_BUTTON_LEFT},
|
{"leftbutton", SDL_BUTTON_LEFT},
|
||||||
{"rightbutton", SDL_BUTTON_RIGHT},
|
{"rightbutton", SDL_BUTTON_RIGHT},
|
||||||
{"middlebutton", SDL_BUTTON_MIDDLE},
|
{"middlebutton", SDL_BUTTON_MIDDLE},
|
||||||
@ -213,24 +183,10 @@ std::map<std::string, u32> string_to_keyboard_key_map = {
|
|||||||
{"mousewheeldown", SDL_EVENT_MOUSE_WHEEL_DOWN},
|
{"mousewheeldown", SDL_EVENT_MOUSE_WHEEL_DOWN},
|
||||||
{"mousewheelleft", SDL_EVENT_MOUSE_WHEEL_LEFT},
|
{"mousewheelleft", SDL_EVENT_MOUSE_WHEEL_LEFT},
|
||||||
{"mousewheelright", SDL_EVENT_MOUSE_WHEEL_RIGHT},
|
{"mousewheelright", SDL_EVENT_MOUSE_WHEEL_RIGHT},
|
||||||
{"kp0", SDLK_KP_0},
|
{"kpperiod", SDLK_KP_PERIOD}, {"kpcomma", SDLK_KP_COMMA},
|
||||||
{"kp1", SDLK_KP_1},
|
{"kpdivide", SDLK_KP_DIVIDE}, {"kpmultiply", SDLK_KP_MULTIPLY},
|
||||||
{"kp2", SDLK_KP_2},
|
{"kpminus", SDLK_KP_MINUS}, {"kpplus", SDLK_KP_PLUS},
|
||||||
{"kp3", SDLK_KP_3},
|
{"kpenter", SDLK_KP_ENTER}, {"kpequals", SDLK_KP_EQUALS},
|
||||||
{"kp4", SDLK_KP_4},
|
|
||||||
{"kp5", SDLK_KP_5},
|
|
||||||
{"kp6", SDLK_KP_6},
|
|
||||||
{"kp7", SDLK_KP_7},
|
|
||||||
{"kp8", SDLK_KP_8},
|
|
||||||
{"kp9", SDLK_KP_9},
|
|
||||||
{"kpperiod", SDLK_KP_PERIOD},
|
|
||||||
{"kpdivide", SDLK_KP_DIVIDE},
|
|
||||||
{"kpmultiply", SDLK_KP_MULTIPLY},
|
|
||||||
{"kpminus", SDLK_KP_MINUS},
|
|
||||||
{"kpplus", SDLK_KP_PLUS},
|
|
||||||
{"kpenter", SDLK_KP_ENTER},
|
|
||||||
{"kpequals", SDLK_KP_EQUALS},
|
|
||||||
{"kpcomma", SDLK_KP_COMMA},
|
|
||||||
};
|
};
|
||||||
std::map<std::string, u32> string_to_keyboard_mod_key_map = {
|
std::map<std::string, u32> string_to_keyboard_mod_key_map = {
|
||||||
{"lshift", SDL_KMOD_LSHIFT}, {"rshift", SDL_KMOD_RSHIFT}, {"lctrl", SDL_KMOD_LCTRL},
|
{"lshift", SDL_KMOD_LSHIFT}, {"rshift", SDL_KMOD_RSHIFT}, {"lctrl", SDL_KMOD_LCTRL},
|
||||||
@ -245,13 +201,14 @@ std::map<std::string, u32> string_to_keyboard_mod_key_map = {
|
|||||||
std::map<KeyBinding, u32> button_map = {};
|
std::map<KeyBinding, u32> button_map = {};
|
||||||
std::map<KeyBinding, AxisMapping> axis_map = {};
|
std::map<KeyBinding, AxisMapping> axis_map = {};
|
||||||
|
|
||||||
// Flags for varying purposes
|
// Flags and values for varying purposes
|
||||||
int mouse_joystick_binding = 0;
|
int mouse_joystick_binding = 0;
|
||||||
|
float mouse_deadzone_offset = 0.5, mouse_speed = 1, mouse_speed_offset = 0.125;
|
||||||
Uint32 mouse_polling_id = 0;
|
Uint32 mouse_polling_id = 0;
|
||||||
bool mouse_enabled = false, leftjoystick_halfmode = false, rightjoystick_halfmode = false;
|
bool mouse_enabled = false, leftjoystick_halfmode = false, rightjoystick_halfmode = false;
|
||||||
|
|
||||||
// i wrapped it in a function so I can collapse it
|
// i wrapped it in a function so I can collapse it
|
||||||
std::string getDefaultConfig() {
|
std::string getDefaultKeyboardConfig() {
|
||||||
std::string default_config =
|
std::string default_config =
|
||||||
R"(## SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
|
R"(## SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
|
||||||
## SPDX-License-Identifier: GPL-2.0-or-later
|
## SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
@ -318,7 +275,6 @@ axis_left_y_plus = s;
|
|||||||
}
|
}
|
||||||
|
|
||||||
void WindowSDL::parseInputConfig(const std::string& filename) {
|
void WindowSDL::parseInputConfig(const std::string& filename) {
|
||||||
|
|
||||||
// Read configuration file.
|
// Read configuration file.
|
||||||
const auto config_file = Common::FS::GetUserPath(Common::FS::PathType::UserDir) / filename;
|
const auto config_file = Common::FS::GetUserPath(Common::FS::PathType::UserDir) / filename;
|
||||||
if (!std::filesystem::exists(config_file)) {
|
if (!std::filesystem::exists(config_file)) {
|
||||||
@ -326,7 +282,7 @@ void WindowSDL::parseInputConfig(const std::string& filename) {
|
|||||||
std::ofstream file;
|
std::ofstream file;
|
||||||
file.open(config_file, std::ios::out);
|
file.open(config_file, std::ios::out);
|
||||||
if (file.is_open()) {
|
if (file.is_open()) {
|
||||||
file << getDefaultConfig();
|
file << getDefaultKeyboardConfig();
|
||||||
file.close();
|
file.close();
|
||||||
std::cout << "Config file generated.\n";
|
std::cout << "Config file generated.\n";
|
||||||
} else {
|
} else {
|
||||||
@ -338,7 +294,10 @@ void WindowSDL::parseInputConfig(const std::string& filename) {
|
|||||||
std::cerr << "Error opening file: " << filename << std::endl;
|
std::cerr << "Error opening file: " << filename << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// we reset this here so in case the user fucks up we can fall back to default
|
||||||
|
mouse_deadzone_offset = 0.5;
|
||||||
|
mouse_speed = 1;
|
||||||
|
mouse_speed_offset = 0.125;
|
||||||
button_map.clear();
|
button_map.clear();
|
||||||
axis_map.clear();
|
axis_map.clear();
|
||||||
int lineCount = 0;
|
int lineCount = 0;
|
||||||
@ -391,6 +350,26 @@ void WindowSDL::parseInputConfig(const std::string& filename) {
|
|||||||
mod_it != string_to_keyboard_mod_key_map.end()) {
|
mod_it != string_to_keyboard_mod_key_map.end()) {
|
||||||
binding.key = key_it->second;
|
binding.key = key_it->second;
|
||||||
binding.modifier = mod_it->second;
|
binding.modifier = mod_it->second;
|
||||||
|
} else if (controller_input == "mouse_movement_params") {
|
||||||
|
// handle mouse movement params
|
||||||
|
float p1 = 0.5, p2 = 1, p3 = 0.125;
|
||||||
|
std::size_t second_comma_pos = kbm_input.find(',');
|
||||||
|
try{
|
||||||
|
p1 = std::stof(key);
|
||||||
|
p2 = std::stof(mod.substr(0, second_comma_pos));
|
||||||
|
p3 = std::stof(mod.substr(second_comma_pos + 1));
|
||||||
|
mouse_deadzone_offset = p1;
|
||||||
|
mouse_speed = p2;
|
||||||
|
mouse_speed_offset = p3;
|
||||||
|
} catch (...) {
|
||||||
|
// fallback to default values
|
||||||
|
mouse_deadzone_offset = 0.5;
|
||||||
|
mouse_speed = 1;
|
||||||
|
mouse_speed_offset = 0.125;
|
||||||
|
std::cerr << "Parsing error while parsing kbm inputs at line " << lineCount
|
||||||
|
<< " line data: " << line << "\n";
|
||||||
|
}
|
||||||
|
continue;
|
||||||
} else {
|
} else {
|
||||||
std::cerr << "Syntax error while parsing kbm inputs at line " << lineCount
|
std::cerr << "Syntax error while parsing kbm inputs at line " << lineCount
|
||||||
<< " line data: " << line << "\n";
|
<< " line data: " << line << "\n";
|
||||||
@ -470,8 +449,11 @@ void WindowSDL::updateMouse() {
|
|||||||
float d_x = 0, d_y = 0;
|
float d_x = 0, d_y = 0;
|
||||||
SDL_GetRelativeMouseState(&d_x, &d_y);
|
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);
|
||||||
|
//std::cout << "speed: " << mouse_speed << "\n";
|
||||||
|
|
||||||
float angle = atan2(d_y, d_x);
|
float angle = atan2(d_y, d_x);
|
||||||
float a_x = cos(angle) * 128.0, a_y = sin(angle) * 128.0;
|
float a_x = cos(angle) * output_speed, a_y = sin(angle) * output_speed;
|
||||||
|
|
||||||
if (d_x != 0 && d_y != 0) {
|
if (d_x != 0 && d_y != 0) {
|
||||||
controller->Axis(0, axis_x, Input::GetAxis(-0x80, 0x80, a_x));
|
controller->Axis(0, axis_x, Input::GetAxis(-0x80, 0x80, a_x));
|
||||||
@ -534,6 +516,10 @@ WindowSDL::WindowSDL(s32 width_, s32 height_, Input::GameController* controller_
|
|||||||
#endif
|
#endif
|
||||||
// initialize kbm controls
|
// initialize kbm controls
|
||||||
parseInputConfig("keyboardInputConfig.ini");
|
parseInputConfig("keyboardInputConfig.ini");
|
||||||
|
// Start polling the mouse
|
||||||
|
if (mouse_polling_id == 0) {
|
||||||
|
mouse_polling_id = SDL_AddTimer(33, mousePolling, (void*)this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
WindowSDL::~WindowSDL() = default;
|
WindowSDL::~WindowSDL() = default;
|
||||||
@ -541,14 +527,12 @@ WindowSDL::~WindowSDL() = default;
|
|||||||
void WindowSDL::waitEvent() {
|
void WindowSDL::waitEvent() {
|
||||||
// Called on main thread
|
// Called on main thread
|
||||||
SDL_Event event{};
|
SDL_Event event{};
|
||||||
if (mouse_polling_id == 0) {
|
|
||||||
mouse_polling_id = SDL_AddTimer(33, mousePolling, (void*)this);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!SDL_WaitEvent(&event)) {
|
if (!SDL_WaitEvent(&event)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ImGui::Core::ProcessEvent(&event)) {
|
if (ImGui::Core::ProcessEvent(&event)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user