3
0
Fork 0
forked from suyu/suyu

Merge pull request #6698 from german77/SDL_QoL

input_common: Improve SDL joystick and hide toggle option
This commit is contained in:
bunnei 2021-08-08 02:44:42 -07:00 committed by GitHub
commit b023413c98
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 33 deletions

View file

@ -304,10 +304,10 @@ std::vector<std::unique_ptr<Polling::DevicePoller>> InputSubsystem::GetPollers([
} }
std::string GenerateKeyboardParam(int key_code) { std::string GenerateKeyboardParam(int key_code) {
Common::ParamPackage param{ Common::ParamPackage param;
{"engine", "keyboard"}, param.Set("engine", "keyboard");
{"code", std::to_string(key_code)}, param.Set("code", key_code);
}; param.Set("toggle", false);
return param.Serialize(); return param.Serialize();
} }

View file

@ -57,6 +57,7 @@ Common::ParamPackage MouseButtonFactory::GetNextInput() const {
if (pad.button != MouseInput::MouseButton::Undefined) { if (pad.button != MouseInput::MouseButton::Undefined) {
params.Set("engine", "mouse"); params.Set("engine", "mouse");
params.Set("button", static_cast<u16>(pad.button)); params.Set("button", static_cast<u16>(pad.button));
params.Set("toggle", false);
return params; return params;
} }
} }

View file

@ -82,6 +82,12 @@ public:
state.buttons.insert_or_assign(button, value); state.buttons.insert_or_assign(button, value);
} }
void PreSetButton(int button) {
if (!state.buttons.contains(button)) {
SetButton(button, false);
}
}
void SetMotion(SDL_ControllerSensorEvent event) { void SetMotion(SDL_ControllerSensorEvent event) {
constexpr float gravity_constant = 9.80665f; constexpr float gravity_constant = 9.80665f;
std::lock_guard lock{mutex}; std::lock_guard lock{mutex};
@ -155,9 +161,16 @@ public:
state.axes.insert_or_assign(axis, value); state.axes.insert_or_assign(axis, value);
} }
float GetAxis(int axis, float range) const { void PreSetAxis(int axis) {
if (!state.axes.contains(axis)) {
SetAxis(axis, 0);
}
}
float GetAxis(int axis, float range, float offset) const {
std::lock_guard lock{mutex}; std::lock_guard lock{mutex};
return static_cast<float>(state.axes.at(axis)) / (32767.0f * range); const float value = static_cast<float>(state.axes.at(axis)) / 32767.0f;
return (value + offset) / range;
} }
bool RumblePlay(u16 amp_low, u16 amp_high) { bool RumblePlay(u16 amp_low, u16 amp_high) {
@ -174,9 +187,10 @@ public:
return false; return false;
} }
std::tuple<float, float> GetAnalog(int axis_x, int axis_y, float range) const { std::tuple<float, float> GetAnalog(int axis_x, int axis_y, float range, float offset_x,
float x = GetAxis(axis_x, range); float offset_y) const {
float y = GetAxis(axis_y, range); float x = GetAxis(axis_x, range, offset_x);
float y = GetAxis(axis_y, range, offset_y);
y = -y; // 3DS uses an y-axis inverse from SDL y = -y; // 3DS uses an y-axis inverse from SDL
// Make sure the coordinates are in the unit circle, // Make sure the coordinates are in the unit circle,
@ -483,7 +497,7 @@ public:
trigger_if_greater(trigger_if_greater_) {} trigger_if_greater(trigger_if_greater_) {}
bool GetStatus() const override { bool GetStatus() const override {
const float axis_value = joystick->GetAxis(axis, 1.0f); const float axis_value = joystick->GetAxis(axis, 1.0f, 0.0f);
if (trigger_if_greater) { if (trigger_if_greater) {
return axis_value > threshold; return axis_value > threshold;
} }
@ -500,12 +514,14 @@ private:
class SDLAnalog final : public Input::AnalogDevice { class SDLAnalog final : public Input::AnalogDevice {
public: public:
explicit SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_, explicit SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_,
bool invert_x_, bool invert_y_, float deadzone_, float range_) bool invert_x_, bool invert_y_, float deadzone_, float range_,
float offset_x_, float offset_y_)
: joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), invert_x(invert_x_), : joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), invert_x(invert_x_),
invert_y(invert_y_), deadzone(deadzone_), range(range_) {} invert_y(invert_y_), deadzone(deadzone_), range(range_), offset_x(offset_x_),
offset_y(offset_y_) {}
std::tuple<float, float> GetStatus() const override { std::tuple<float, float> GetStatus() const override {
auto [x, y] = joystick->GetAnalog(axis_x, axis_y, range); auto [x, y] = joystick->GetAnalog(axis_x, axis_y, range, offset_x, offset_y);
const float r = std::sqrt((x * x) + (y * y)); const float r = std::sqrt((x * x) + (y * y));
if (invert_x) { if (invert_x) {
x = -x; x = -x;
@ -522,8 +538,8 @@ public:
} }
std::tuple<float, float> GetRawStatus() const override { std::tuple<float, float> GetRawStatus() const override {
const float x = joystick->GetAxis(axis_x, range); const float x = joystick->GetAxis(axis_x, range, offset_x);
const float y = joystick->GetAxis(axis_y, range); const float y = joystick->GetAxis(axis_y, range, offset_y);
return {x, -y}; return {x, -y};
} }
@ -555,6 +571,8 @@ private:
const bool invert_y; const bool invert_y;
const float deadzone; const float deadzone;
const float range; const float range;
const float offset_x;
const float offset_y;
}; };
class SDLVibration final : public Input::VibrationDevice { class SDLVibration final : public Input::VibrationDevice {
@ -621,7 +639,7 @@ public:
trigger_if_greater(trigger_if_greater_) {} trigger_if_greater(trigger_if_greater_) {}
Input::MotionStatus GetStatus() const override { Input::MotionStatus GetStatus() const override {
const float axis_value = joystick->GetAxis(axis, 1.0f); const float axis_value = joystick->GetAxis(axis, 1.0f, 0.0f);
bool trigger = axis_value < threshold; bool trigger = axis_value < threshold;
if (trigger_if_greater) { if (trigger_if_greater) {
trigger = axis_value > threshold; trigger = axis_value > threshold;
@ -720,13 +738,13 @@ public:
LOG_ERROR(Input, "Unknown direction {}", direction_name); LOG_ERROR(Input, "Unknown direction {}", direction_name);
} }
// This is necessary so accessing GetAxis with axis won't crash // This is necessary so accessing GetAxis with axis won't crash
joystick->SetAxis(axis, 0); joystick->PreSetAxis(axis);
return std::make_unique<SDLAxisButton>(joystick, axis, threshold, trigger_if_greater); return std::make_unique<SDLAxisButton>(joystick, axis, threshold, trigger_if_greater);
} }
const int button = params.Get("button", 0); const int button = params.Get("button", 0);
// This is necessary so accessing GetButton with button won't crash // This is necessary so accessing GetButton with button won't crash
joystick->SetButton(button, false); joystick->PreSetButton(button);
return std::make_unique<SDLButton>(joystick, button, toggle); return std::make_unique<SDLButton>(joystick, button, toggle);
} }
@ -757,13 +775,15 @@ public:
const std::string invert_y_value = params.Get("invert_y", "+"); const std::string invert_y_value = params.Get("invert_y", "+");
const bool invert_x = invert_x_value == "-"; const bool invert_x = invert_x_value == "-";
const bool invert_y = invert_y_value == "-"; const bool invert_y = invert_y_value == "-";
const float offset_x = params.Get("offset_x", 0.0f);
const float offset_y = params.Get("offset_y", 0.0f);
auto joystick = state.GetSDLJoystickByGUID(guid, port); auto joystick = state.GetSDLJoystickByGUID(guid, port);
// This is necessary so accessing GetAxis with axis_x and axis_y won't crash // This is necessary so accessing GetAxis with axis_x and axis_y won't crash
joystick->SetAxis(axis_x, 0); joystick->PreSetAxis(axis_x);
joystick->SetAxis(axis_y, 0); joystick->PreSetAxis(axis_y);
return std::make_unique<SDLAnalog>(joystick, axis_x, axis_y, invert_x, invert_y, deadzone, return std::make_unique<SDLAnalog>(joystick, axis_x, axis_y, invert_x, invert_y, deadzone,
range); range, offset_x, offset_y);
} }
private: private:
@ -844,13 +864,13 @@ public:
LOG_ERROR(Input, "Unknown direction {}", direction_name); LOG_ERROR(Input, "Unknown direction {}", direction_name);
} }
// This is necessary so accessing GetAxis with axis won't crash // This is necessary so accessing GetAxis with axis won't crash
joystick->SetAxis(axis, 0); joystick->PreSetAxis(axis);
return std::make_unique<SDLAxisMotion>(joystick, axis, threshold, trigger_if_greater); return std::make_unique<SDLAxisMotion>(joystick, axis, threshold, trigger_if_greater);
} }
const int button = params.Get("button", 0); const int button = params.Get("button", 0);
// This is necessary so accessing GetButton with button won't crash // This is necessary so accessing GetButton with button won't crash
joystick->SetButton(button, false); joystick->PreSetButton(button);
return std::make_unique<SDLButtonMotion>(joystick, button); return std::make_unique<SDLButtonMotion>(joystick, button);
} }
@ -995,6 +1015,7 @@ Common::ParamPackage BuildButtonParamPackageForButton(int port, std::string guid
params.Set("port", port); params.Set("port", port);
params.Set("guid", std::move(guid)); params.Set("guid", std::move(guid));
params.Set("button", button); params.Set("button", button);
params.Set("toggle", false);
return params; return params;
} }
@ -1134,13 +1155,15 @@ Common::ParamPackage BuildParamPackageForBinding(int port, const std::string& gu
} }
Common::ParamPackage BuildParamPackageForAnalog(int port, const std::string& guid, int axis_x, Common::ParamPackage BuildParamPackageForAnalog(int port, const std::string& guid, int axis_x,
int axis_y) { int axis_y, float offset_x, float offset_y) {
Common::ParamPackage params; Common::ParamPackage params;
params.Set("engine", "sdl"); params.Set("engine", "sdl");
params.Set("port", port); params.Set("port", port);
params.Set("guid", guid); params.Set("guid", guid);
params.Set("axis_x", axis_x); params.Set("axis_x", axis_x);
params.Set("axis_y", axis_y); params.Set("axis_y", axis_y);
params.Set("offset_x", offset_x);
params.Set("offset_y", offset_y);
params.Set("invert_x", "+"); params.Set("invert_x", "+");
params.Set("invert_y", "+"); params.Set("invert_y", "+");
return params; return params;
@ -1342,24 +1365,39 @@ AnalogMapping SDLState::GetAnalogMappingForDevice(const Common::ParamPackage& pa
const auto& binding_left_y = const auto& binding_left_y =
SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTY); SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTY);
if (params.Has("guid2")) { if (params.Has("guid2")) {
joystick2->PreSetAxis(binding_left_x.value.axis);
joystick2->PreSetAxis(binding_left_y.value.axis);
const auto left_offset_x = -joystick2->GetAxis(binding_left_x.value.axis, 1.0f, 0);
const auto left_offset_y = -joystick2->GetAxis(binding_left_y.value.axis, 1.0f, 0);
mapping.insert_or_assign( mapping.insert_or_assign(
Settings::NativeAnalog::LStick, Settings::NativeAnalog::LStick,
BuildParamPackageForAnalog(joystick2->GetPort(), joystick2->GetGUID(), BuildParamPackageForAnalog(joystick2->GetPort(), joystick2->GetGUID(),
binding_left_x.value.axis, binding_left_y.value.axis)); binding_left_x.value.axis, binding_left_y.value.axis,
left_offset_x, left_offset_y));
} else { } else {
joystick->PreSetAxis(binding_left_x.value.axis);
joystick->PreSetAxis(binding_left_y.value.axis);
const auto left_offset_x = -joystick->GetAxis(binding_left_x.value.axis, 1.0f, 0);
const auto left_offset_y = -joystick->GetAxis(binding_left_y.value.axis, 1.0f, 0);
mapping.insert_or_assign( mapping.insert_or_assign(
Settings::NativeAnalog::LStick, Settings::NativeAnalog::LStick,
BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(), BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(),
binding_left_x.value.axis, binding_left_y.value.axis)); binding_left_x.value.axis, binding_left_y.value.axis,
left_offset_x, left_offset_y));
} }
const auto& binding_right_x = const auto& binding_right_x =
SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_RIGHTX); SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_RIGHTX);
const auto& binding_right_y = const auto& binding_right_y =
SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_RIGHTY); SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_RIGHTY);
joystick->PreSetAxis(binding_right_x.value.axis);
joystick->PreSetAxis(binding_right_y.value.axis);
const auto right_offset_x = -joystick->GetAxis(binding_right_x.value.axis, 1.0f, 0);
const auto right_offset_y = -joystick->GetAxis(binding_right_y.value.axis, 1.0f, 0);
mapping.insert_or_assign(Settings::NativeAnalog::RStick, mapping.insert_or_assign(Settings::NativeAnalog::RStick,
BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(), BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(),
binding_right_x.value.axis, binding_right_x.value.axis,
binding_right_y.value.axis)); binding_right_y.value.axis, right_offset_x,
right_offset_y));
return mapping; return mapping;
} }
@ -1563,8 +1601,9 @@ public:
} }
if (const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which)) { if (const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which)) {
// Set offset to zero since the joystick is not on center
auto params = BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(), auto params = BuildParamPackageForAnalog(joystick->GetPort(), joystick->GetGUID(),
first_axis, axis); first_axis, axis, 0, 0);
first_axis = -1; first_axis = -1;
return params; return params;
} }

View file

@ -309,11 +309,14 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i
buttons_param[button_id].Clear(); buttons_param[button_id].Clear();
button_map[button_id]->setText(tr("[not set]")); button_map[button_id]->setText(tr("[not set]"));
}); });
context_menu.addAction(tr("Toggle button"), [&] { if (buttons_param[button_id].Has("toggle")) {
const bool toggle_value = !buttons_param[button_id].Get("toggle", false); context_menu.addAction(tr("Toggle button"), [&] {
buttons_param[button_id].Set("toggle", toggle_value); const bool toggle_value =
button_map[button_id]->setText(ButtonToText(buttons_param[button_id])); !buttons_param[button_id].Get("toggle", false);
}); buttons_param[button_id].Set("toggle", toggle_value);
button_map[button_id]->setText(ButtonToText(buttons_param[button_id]));
});
}
if (buttons_param[button_id].Has("threshold")) { if (buttons_param[button_id].Has("threshold")) {
context_menu.addAction(tr("Set threshold"), [&] { context_menu.addAction(tr("Set threshold"), [&] {
const int button_threshold = static_cast<int>( const int button_threshold = static_cast<int>(