Add LR triggers as axes, half press to initiate a press, add GC axis id in config, clarify some code blocks for better readability

This commit is contained in:
Ameer 2020-07-02 15:54:44 -04:00
parent 34a590e509
commit 6b7c8e469b
3 changed files with 80 additions and 35 deletions

View file

@ -144,6 +144,18 @@ void Adapter::Read() {
pads[port].axis_value = pads[port].substick_y; pads[port].axis_value = pads[port].substick_y;
pad_queue[port].Push(pads[port]); pad_queue[port].Push(pads[port]);
} }
if (pads[port].trigger_left > pads[port].TRIGGER_CENTER + pads[port].THRESHOLD ||
pads[port].trigger_left < pads[port].TRIGGER_CENTER - pads[port].THRESHOLD) {
pads[port].axis = GCAdapter::PadAxes::TriggerLeft;
pads[port].axis_value = pads[port].trigger_left;
pad_queue[port].Push(pads[port]);
}
if (pads[port].trigger_right > pads[port].TRIGGER_CENTER + pads[port].THRESHOLD ||
pads[port].trigger_right < pads[port].TRIGGER_CENTER - pads[port].THRESHOLD) {
pads[port].axis = GCAdapter::PadAxes::TriggerRight;
pads[port].axis_value = pads[port].trigger_right;
pad_queue[port].Push(pads[port]);
}
} }
PadToState(pads[port], state[port]); PadToState(pads[port], state[port]);
} }

View file

@ -34,7 +34,13 @@ public:
explicit GCAxisButton(int port_, int axis_, float threshold_, bool trigger_if_greater_, explicit GCAxisButton(int port_, int axis_, float threshold_, bool trigger_if_greater_,
GCAdapter::Adapter* adapter) GCAdapter::Adapter* adapter)
: port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_), : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_),
gcadapter(adapter) {} gcadapter(adapter) {
// L/R triggers range is only in positive direction beginning near 0
// 0.0 threshold equates to near half trigger press, but threshold accounts for variability.
if (axis > 3) {
threshold *= -0.5;
}
}
bool GetStatus() const override { bool GetStatus() const override {
const float axis_value = (gcadapter->GetPadState()[port].axes.at(axis) - 128.0f) / 128.0f; const float axis_value = (gcadapter->GetPadState()[port].axes.at(axis) - 128.0f) / 128.0f;
@ -60,10 +66,20 @@ GCButton::~GCButton() = default;
std::unique_ptr<Input::ButtonDevice> GCButtonFactory::Create(const Common::ParamPackage& params) { std::unique_ptr<Input::ButtonDevice> GCButtonFactory::Create(const Common::ParamPackage& params) {
const int button_id = params.Get("button", 0); const int button_id = params.Get("button", 0);
const int port = params.Get("port", 0); const int port = params.Get("port", 0);
constexpr int PAD_STICK_ID = static_cast<u16>(GCAdapter::PadButton::PAD_STICK);
// button is not an axis/stick button
if (button_id != PAD_STICK_ID) {
std::unique_ptr<GCButton> button =
std::make_unique<GCButton>(port, button_id, params.Get("axis", 0), adapter.get());
return std::move(button);
}
// For Axis buttons, used by the binary sticks. // For Axis buttons, used by the binary sticks.
if (params.Has("axis")) { if (button_id == PAD_STICK_ID) {
const int axis = params.Get("axis", 0); const int axis = params.Get("axis", 0);
const float threshold = params.Get("threshold", 0.5f); const float threshold = params.Get("threshold", 0.25f);
const std::string direction_name = params.Get("direction", ""); const std::string direction_name = params.Get("direction", "");
bool trigger_if_greater; bool trigger_if_greater;
if (direction_name == "+") { if (direction_name == "+") {
@ -77,10 +93,6 @@ std::unique_ptr<Input::ButtonDevice> GCButtonFactory::Create(const Common::Param
return std::make_unique<GCAxisButton>(port, axis, threshold, trigger_if_greater, return std::make_unique<GCAxisButton>(port, axis, threshold, trigger_if_greater,
adapter.get()); adapter.get());
} }
std::unique_ptr<GCButton> button =
std::make_unique<GCButton>(port, button_id, params.Get("axis", 0), adapter.get());
return std::move(button);
} }
Common::ParamPackage GCButtonFactory::GetNextInput() { Common::ParamPackage GCButtonFactory::GetNextInput() {
@ -106,10 +118,10 @@ Common::ParamPackage GCButtonFactory::GetNextInput() {
params.Set("button", static_cast<u16>(GCAdapter::PadButton::PAD_STICK)); params.Set("button", static_cast<u16>(GCAdapter::PadButton::PAD_STICK));
if (pad.axis_value > 128) { if (pad.axis_value > 128) {
params.Set("direction", "+"); params.Set("direction", "+");
params.Set("threshold", "0.5"); params.Set("threshold", "0.25");
} else { } else {
params.Set("direction", "-"); params.Set("direction", "-");
params.Set("threshold", "-0.5"); params.Set("threshold", "-0.25");
} }
break; break;
} }
@ -232,7 +244,7 @@ Common::ParamPackage GCAnalogFactory::GetNextInput() {
continue; continue;
} }
// An analog device needs two axes, so we need to store the axis for later and wait for // An analog device needs two axes, so we need to store the axis for later and wait for
// a second SDL event. The axes also must be from the same joystick. // a second input event. The axes also must be from the same joystick.
const u8 axis = static_cast<u8>(pad.axis); const u8 axis = static_cast<u8>(pad.axis);
if (analog_x_axis == -1) { if (analog_x_axis == -1) {
analog_x_axis = axis; analog_x_axis = axis;

View file

@ -120,7 +120,7 @@ static QString AnalogToText(const Common::ParamPackage& param, const std::string
return ButtonToText(Common::ParamPackage{param.Get(dir, "")}); return ButtonToText(Common::ParamPackage{param.Get(dir, "")});
} }
if (param.Get("engine", "") == "sdl" || param.Get("engine", "") == "gcpad") { if (param.Get("engine", "") == "sdl") {
if (dir == "modifier") { if (dir == "modifier") {
return QObject::tr("[unused]"); return QObject::tr("[unused]");
} }
@ -140,6 +140,25 @@ static QString AnalogToText(const Common::ParamPackage& param, const std::string
return {}; return {};
} }
if (param.Get("engine", "") == "gcpad") {
if (dir == "modifier") {
return QObject::tr("[unused]");
}
if (dir == "left" || dir == "right") {
const QString axis_x_str = QString::fromStdString(param.Get("axis_x", ""));
return QObject::tr("GC Axis %1").arg(axis_x_str);
}
if (dir == "up" || dir == "down") {
const QString axis_y_str = QString::fromStdString(param.Get("axis_y", ""));
return QObject::tr("GC Axis %1").arg(axis_y_str);
}
return {};
}
return QObject::tr("[unknown]"); return QObject::tr("[unknown]");
} }
@ -262,24 +281,25 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i
button->setContextMenuPolicy(Qt::CustomContextMenu); button->setContextMenuPolicy(Qt::CustomContextMenu);
connect(button, &QPushButton::clicked, [=] { connect(button, &QPushButton::clicked, [=] {
HandleClick(button_map[button_id], HandleClick(
[=](Common::ParamPackage params) { button_map[button_id],
// Workaround for ZL & ZR for analog triggers like on XBOX controllors. [=](Common::ParamPackage params) {
// Analog triggers (from controllers like the XBOX controller) would not // Workaround for ZL & ZR for analog triggers like on XBOX controllors.
// work due to a different range of their signals (from 0 to 255 on // Analog triggers (from controllers like the XBOX controller) would not
// analog triggers instead of -32768 to 32768 on analog joysticks). The // work due to a different range of their signals (from 0 to 255 on
// SDL driver misinterprets analog triggers as analog joysticks. // analog triggers instead of -32768 to 32768 on analog joysticks). The
// TODO: reinterpret the signal range for analog triggers to map the // SDL driver misinterprets analog triggers as analog joysticks.
// values correctly. This is required for the correct emulation of the // TODO: reinterpret the signal range for analog triggers to map the
// analog triggers of the GameCube controller. // values correctly. This is required for the correct emulation of the
if (button_id == Settings::NativeButton::ZL || // analog triggers of the GameCube controller.
button_id == Settings::NativeButton::ZR) { if (button_id == Settings::NativeButton::ZL ||
params.Set("direction", "+"); button_id == Settings::NativeButton::ZR) {
params.Set("threshold", "0.5"); params.Set("direction", "+");
} params.Set("threshold", "0.5");
buttons_param[button_id] = std::move(params); }
}, buttons_param[button_id] = std::move(params);
InputCommon::Polling::DeviceType::Button); },
InputCommon::Polling::DeviceType::Button);
}); });
connect(button, &QPushButton::customContextMenuRequested, [=](const QPoint& menu_location) { connect(button, &QPushButton::customContextMenuRequested, [=](const QPoint& menu_location) {
QMenu context_menu; QMenu context_menu;
@ -305,12 +325,13 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i
analog_button->setContextMenuPolicy(Qt::CustomContextMenu); analog_button->setContextMenuPolicy(Qt::CustomContextMenu);
connect(analog_button, &QPushButton::clicked, [=]() { connect(analog_button, &QPushButton::clicked, [=]() {
HandleClick(analog_map_buttons[analog_id][sub_button_id], HandleClick(
[=](const Common::ParamPackage& params) { analog_map_buttons[analog_id][sub_button_id],
SetAnalogButton(params, analogs_param[analog_id], [=](const Common::ParamPackage& params) {
analog_sub_buttons[sub_button_id]); SetAnalogButton(params, analogs_param[analog_id],
}, analog_sub_buttons[sub_button_id]);
InputCommon::Polling::DeviceType::Button); },
InputCommon::Polling::DeviceType::Button);
}); });
connect(analog_button, &QPushButton::customContextMenuRequested, connect(analog_button, &QPushButton::customContextMenuRequested,
[=](const QPoint& menu_location) { [=](const QPoint& menu_location) {