mirror of
https://git.suyu.dev/suyu/suyu.git
synced 2024-12-23 17:00:57 +01:00
fixup! Joystick: Allow for background events; Add deadzone to SDLAnalog
This commit is contained in:
parent
8b98f60e3c
commit
71817afbe9
1 changed files with 17 additions and 6 deletions
|
@ -330,17 +330,24 @@ private:
|
||||||
|
|
||||||
class SDLAnalog final : public Input::AnalogDevice {
|
class SDLAnalog final : public Input::AnalogDevice {
|
||||||
public:
|
public:
|
||||||
SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_)
|
SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_, float deadzone_)
|
||||||
: joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_) {}
|
: joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_) {}
|
||||||
|
|
||||||
std::tuple<float, float> GetStatus() const override {
|
std::tuple<float, float> GetStatus() const override {
|
||||||
return joystick->GetAnalog(axis_x, axis_y);
|
const auto [x, y] = joystick->GetAnalog(axis_x, axis_y);
|
||||||
|
const float r = std::sqrt((x * x) + (y * y));
|
||||||
|
if (r > deadzone) {
|
||||||
|
return std::make_tuple(x / r * (r - deadzone) / (1 - deadzone),
|
||||||
|
y / r * (r - deadzone) / (1 - deadzone));
|
||||||
|
}
|
||||||
|
return std::make_tuple<float, float>(0.0f, 0.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<SDLJoystick> joystick;
|
std::shared_ptr<SDLJoystick> joystick;
|
||||||
int axis_x;
|
const int axis_x;
|
||||||
int axis_y;
|
const int axis_y;
|
||||||
|
const float deadzone;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A button device factory that creates button devices from SDL joystick
|
/// A button device factory that creates button devices from SDL joystick
|
||||||
|
@ -435,13 +442,14 @@ public:
|
||||||
const int port = params.Get("port", 0);
|
const int port = params.Get("port", 0);
|
||||||
const int axis_x = params.Get("axis_x", 0);
|
const int axis_x = params.Get("axis_x", 0);
|
||||||
const int axis_y = params.Get("axis_y", 1);
|
const int axis_y = params.Get("axis_y", 1);
|
||||||
|
float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, .99f);
|
||||||
|
|
||||||
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->SetAxis(axis_x, 0);
|
||||||
joystick->SetAxis(axis_y, 0);
|
joystick->SetAxis(axis_y, 0);
|
||||||
return std::make_unique<SDLAnalog>(joystick, axis_x, axis_y);
|
return std::make_unique<SDLAnalog>(joystick, axis_x, axis_y, deadzone);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -459,6 +467,9 @@ SDLState::SDLState() {
|
||||||
LOG_CRITICAL(Input, "SDL_Init(SDL_INIT_JOYSTICK) failed with: {}", SDL_GetError());
|
LOG_CRITICAL(Input, "SDL_Init(SDL_INIT_JOYSTICK) failed with: {}", SDL_GetError());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1") == SDL_FALSE) {
|
||||||
|
LOG_ERROR(Input, "Failed to set Hint for background events", SDL_GetError());
|
||||||
|
}
|
||||||
|
|
||||||
SDL_AddEventWatch(&SDLEventWatcher, this);
|
SDL_AddEventWatch(&SDLEventWatcher, this);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue