Merge pull request #13209 from jordan-woyak/sdl-touchpad

InputCommon/SDL: Add touchpad inputs.
This commit is contained in:
JMC47 2024-12-10 12:52:43 -05:00 committed by GitHub
commit 6ea8edd531
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 59 additions and 0 deletions

View File

@ -283,6 +283,39 @@ private:
const Motor m_motor;
};
class NormalizedInput : public Input
{
public:
NormalizedInput(const char* name, const float* state) : m_name{std::move(name)}, m_state{*state}
{
}
std::string GetName() const override { return std::string{m_name}; }
ControlState GetState() const override { return m_state; }
private:
const char* const m_name;
const float& m_state;
};
template <int Scale>
class NonDetectableDirectionalInput : public Input
{
public:
NonDetectableDirectionalInput(const char* name, const float* state)
: m_name{std::move(name)}, m_state{*state}
{
}
std::string GetName() const override { return std::string{m_name} + (Scale > 0 ? '+' : '-'); }
bool IsDetectable() const override { return false; }
ControlState GetState() const override { return m_state * Scale; }
private:
const char* const m_name;
const float& m_state;
};
class MotionInput : public Input
{
public:
@ -316,6 +349,17 @@ public:
Core::DeviceRemoval UpdateInput() override
{
m_battery_value = GetBatteryValueFromSDLPowerLevel(SDL_JoystickCurrentPowerLevel(m_joystick));
// We only support one touchpad and one finger.
const int touchpad_index = 0;
const int finger_index = 0;
Uint8 state = 0;
SDL_GameControllerGetTouchpadFinger(m_gamecontroller, touchpad_index, finger_index, &state,
&m_touchpad_x, &m_touchpad_y, &m_touchpad_pressure);
m_touchpad_x = m_touchpad_x * 2 - 1;
m_touchpad_y = m_touchpad_y * 2 - 1;
return Core::DeviceRemoval::Keep;
}
@ -325,6 +369,9 @@ private:
SDL_Joystick* const m_joystick;
SDL_Haptic* m_haptic = nullptr;
ControlState m_battery_value;
float m_touchpad_x = 0.f;
float m_touchpad_y = 0.f;
float m_touchpad_pressure = 0.f;
};
class InputBackend final : public ciface::InputBackend
@ -712,6 +759,18 @@ GameController::GameController(SDL_GameController* const gamecontroller,
AddOutput(new MotorR(m_gamecontroller));
}
// Touchpad
if (SDL_GameControllerGetNumTouchpads(m_gamecontroller) > 0)
{
const char* const name_x = "Touchpad X";
AddInput(new NonDetectableDirectionalInput<-1>(name_x, &m_touchpad_x));
AddInput(new NonDetectableDirectionalInput<+1>(name_x, &m_touchpad_x));
const char* const name_y = "Touchpad Y";
AddInput(new NonDetectableDirectionalInput<-1>(name_y, &m_touchpad_y));
AddInput(new NonDetectableDirectionalInput<+1>(name_y, &m_touchpad_y));
AddInput(new NormalizedInput("Touchpad Pressure", &m_touchpad_pressure));
}
// Motion
const auto add_sensor = [this](SDL_SensorType type, std::string_view sensor_name,
const SDLMotionAxisList& axes) {