InputCommon: Make SDL Motor L/R Outputs not fight each other.
This commit is contained in:
parent
510a688a2a
commit
fb512adc5f
|
@ -187,30 +187,55 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
// Rumble
|
// Rumble
|
||||||
template <int LowEnable, int HighEnable, int SuffixIndex>
|
class Motor : public Output
|
||||||
class GenericMotor : public Output
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit GenericMotor(SDL_GameController* gc) : m_gc(gc) {}
|
Motor(const char* name, GameController& gc, Uint16* state)
|
||||||
std::string GetName() const override
|
: m_name{name}, m_gc{gc}, m_state{*state}
|
||||||
{
|
{
|
||||||
return std::string("Motor") + motor_suffixes[SuffixIndex];
|
|
||||||
}
|
}
|
||||||
|
std::string GetName() const override { return m_name; }
|
||||||
void SetState(ControlState state) override
|
void SetState(ControlState state) override
|
||||||
{
|
{
|
||||||
Uint16 rumble = state * std::numeric_limits<Uint16>::max();
|
const auto new_state = state * std::numeric_limits<Uint16>::max();
|
||||||
SDL_GameControllerRumble(m_gc, rumble * LowEnable, rumble * HighEnable, RUMBLE_LENGTH_MS);
|
if (m_state == new_state)
|
||||||
|
return;
|
||||||
|
|
||||||
|
m_state = new_state;
|
||||||
|
m_gc.UpdateRumble();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SDL_GameController* const m_gc;
|
const char* const m_name;
|
||||||
|
GameController& m_gc;
|
||||||
|
Uint16& m_state;
|
||||||
};
|
};
|
||||||
|
|
||||||
static constexpr const char* motor_suffixes[] = {"", " L", " R"};
|
class CombinedMotor : public Output
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CombinedMotor(const char* name, GameController& gc, Uint16* low_state, Uint16* high_state)
|
||||||
|
: m_name{name}, m_gc{gc}, m_low_state{*low_state}, m_high_state{*high_state}
|
||||||
|
{
|
||||||
|
}
|
||||||
|
std::string GetName() const override { return m_name; }
|
||||||
|
void SetState(ControlState state) override
|
||||||
|
{
|
||||||
|
const auto new_state = state * std::numeric_limits<Uint16>::max();
|
||||||
|
if (m_low_state == new_state && m_high_state == new_state)
|
||||||
|
return;
|
||||||
|
|
||||||
using Motor = GenericMotor<1, 1, 0>;
|
m_low_state = new_state;
|
||||||
using MotorL = GenericMotor<1, 0, 1>;
|
m_high_state = new_state;
|
||||||
using MotorR = GenericMotor<0, 1, 2>;
|
m_gc.UpdateRumble();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
const char* const m_name;
|
||||||
|
GameController& m_gc;
|
||||||
|
Uint16& m_low_state;
|
||||||
|
Uint16& m_high_state;
|
||||||
|
};
|
||||||
|
|
||||||
class HapticEffect : public Output
|
class HapticEffect : public Output
|
||||||
{
|
{
|
||||||
|
@ -364,6 +389,15 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void UpdateRumble()
|
||||||
|
{
|
||||||
|
SDL_GameControllerRumble(m_gamecontroller, m_low_freq_rumble, m_high_freq_rumble,
|
||||||
|
RUMBLE_LENGTH_MS);
|
||||||
|
}
|
||||||
|
|
||||||
|
Uint16 m_low_freq_rumble = 0;
|
||||||
|
Uint16 m_high_freq_rumble = 0;
|
||||||
|
|
||||||
SDL_GameController* const m_gamecontroller;
|
SDL_GameController* const m_gamecontroller;
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
SDL_Joystick* const m_joystick;
|
SDL_Joystick* const m_joystick;
|
||||||
|
@ -754,9 +788,9 @@ GameController::GameController(SDL_GameController* const gamecontroller,
|
||||||
// Rumble
|
// Rumble
|
||||||
if (SDL_GameControllerHasRumble(m_gamecontroller))
|
if (SDL_GameControllerHasRumble(m_gamecontroller))
|
||||||
{
|
{
|
||||||
AddOutput(new Motor(m_gamecontroller));
|
AddOutput(new CombinedMotor("Motor", *this, &m_low_freq_rumble, &m_high_freq_rumble));
|
||||||
AddOutput(new MotorL(m_gamecontroller));
|
AddOutput(new Motor("Motor L", *this, &m_low_freq_rumble));
|
||||||
AddOutput(new MotorR(m_gamecontroller));
|
AddOutput(new Motor("Motor R", *this, &m_high_freq_rumble));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Touchpad
|
// Touchpad
|
||||||
|
|
Loading…
Reference in New Issue