InputCommon/SDL: Add touchpad inputs.

This commit is contained in:
Jordan Woyak 2024-11-08 22:27:03 -06:00
parent 8c3b9c9cf6
commit ad1511982a
1 changed files with 59 additions and 0 deletions

View File

@ -283,6 +283,39 @@ private:
const Motor m_motor; 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 class MotionInput : public Input
{ {
public: public:
@ -316,6 +349,17 @@ public:
Core::DeviceRemoval UpdateInput() override Core::DeviceRemoval UpdateInput() override
{ {
m_battery_value = GetBatteryValueFromSDLPowerLevel(SDL_JoystickCurrentPowerLevel(m_joystick)); 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; return Core::DeviceRemoval::Keep;
} }
@ -325,6 +369,9 @@ private:
SDL_Joystick* const m_joystick; SDL_Joystick* const m_joystick;
SDL_Haptic* m_haptic = nullptr; SDL_Haptic* m_haptic = nullptr;
ControlState m_battery_value; 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 class InputBackend final : public ciface::InputBackend
@ -712,6 +759,18 @@ GameController::GameController(SDL_GameController* const gamecontroller,
AddOutput(new MotorR(m_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 // Motion
const auto add_sensor = [this](SDL_SensorType type, std::string_view sensor_name, const auto add_sensor = [this](SDL_SensorType type, std::string_view sensor_name,
const SDLMotionAxisList& axes) { const SDLMotionAxisList& axes) {