Allow mapping half axes to buttons

This allows to bind pressure sensitive NeGcon buttons to keyboard,
mouse and controller buttons
This commit is contained in:
Silent
2020-08-29 14:19:28 +02:00
parent 79aaf908a6
commit 057bf986c4
17 changed files with 306 additions and 171 deletions

View File

@ -224,7 +224,7 @@ void ControllerSettingsWidget::createPortBindingSettingsUi(int index, PortSettin
const int num_rows = (static_cast<int>(axises.size()) + 1) / 2;
int current_row = 0;
int current_column = 0;
for (const auto& [axis_name, axis_code] : axises)
for (const auto& [axis_name, axis_code, axis_type] : axises)
{
if (current_row == num_rows)
{
@ -235,8 +235,8 @@ void ControllerSettingsWidget::createPortBindingSettingsUi(int index, PortSettin
std::string section_name = StringUtil::StdStringFromFormat("Controller%d", index + 1);
std::string key_name = StringUtil::StdStringFromFormat("Axis%s", axis_name.c_str());
QLabel* label = new QLabel(qApp->translate(cname, axis_name.c_str()), ui->bindings_container);
InputAxisBindingWidget* button = new InputAxisBindingWidget(m_host_interface, std::move(section_name),
std::move(key_name), ui->bindings_container);
InputAxisBindingWidget* button = new InputAxisBindingWidget(
m_host_interface, std::move(section_name), std::move(key_name), axis_type, ui->bindings_container);
layout->addWidget(label, start_row + current_row, current_column);
layout->addWidget(button, start_row + current_row, current_column + 1);

View File

@ -36,8 +36,32 @@ bool InputBindingDialog::eventFilter(QObject* watched, QEvent* event)
{
const QEvent::Type event_type = event->type();
if (event_type == QEvent::MouseButtonPress || event_type == QEvent::MouseButtonRelease ||
event_type == QEvent::MouseButtonDblClick)
// if the key is being released, set the input
if (event_type == QEvent::KeyRelease)
{
addNewBinding(std::move(m_new_binding_value));
stopListeningForInput();
return true;
}
else if (event_type == QEvent::KeyPress)
{
QString binding = QtUtils::KeyEventToString(static_cast<QKeyEvent*>(event));
if (!binding.isEmpty())
m_new_binding_value = QStringLiteral("Keyboard/%1").arg(binding).toStdString();
return true;
}
else if (event_type == QEvent::MouseButtonRelease)
{
const u32 button_mask = static_cast<u32>(static_cast<const QMouseEvent*>(event)->button());
const u32 button_index = (button_mask == 0u) ? 0 : CountTrailingZeros(button_mask);
m_new_binding_value = StringUtil::StdStringFromFormat("Mouse/Button%d", button_index + 1);
addNewBinding(std::move(m_new_binding_value));
stopListeningForInput();
return true;
}
if (event_type == QEvent::MouseButtonPress || event_type == QEvent::MouseButtonDblClick)
{
return true;
}
@ -103,6 +127,27 @@ void InputBindingDialog::addNewBinding(std::string new_binding)
saveListToSettings();
}
void InputBindingDialog::bindToControllerAxis(int controller_index, int axis_index, std::optional<bool> positive)
{
const char* sign_char = "";
if (positive)
{
sign_char = *positive ? "+" : "-";
}
std::string binding =
StringUtil::StdStringFromFormat("Controller%d/%sAxis%d", controller_index, sign_char, axis_index);
addNewBinding(std::move(binding));
stopListeningForInput();
}
void InputBindingDialog::bindToControllerButton(int controller_index, int button_index)
{
std::string binding = StringUtil::StdStringFromFormat("Controller%d/Button%d", controller_index, button_index);
addNewBinding(std::move(binding));
stopListeningForInput();
}
void InputBindingDialog::onAddBindingButtonClicked()
{
if (isListeningForInput())
@ -159,38 +204,6 @@ InputButtonBindingDialog::~InputButtonBindingDialog()
InputButtonBindingDialog::stopListeningForInput();
}
bool InputButtonBindingDialog::eventFilter(QObject* watched, QEvent* event)
{
const QEvent::Type event_type = event->type();
// if the key is being released, set the input
if (event_type == QEvent::KeyRelease)
{
addNewBinding(std::move(m_new_binding_value));
stopListeningForInput();
return true;
}
else if (event_type == QEvent::KeyPress)
{
QString binding = QtUtils::KeyEventToString(static_cast<QKeyEvent*>(event));
if (!binding.isEmpty())
m_new_binding_value = QStringLiteral("Keyboard/%1").arg(binding).toStdString();
return true;
}
else if (event_type == QEvent::MouseButtonRelease)
{
const u32 button_mask = static_cast<u32>(static_cast<const QMouseEvent*>(event)->button());
const u32 button_index = (button_mask == 0u) ? 0 : CountTrailingZeros(button_mask);
m_new_binding_value = StringUtil::StdStringFromFormat("Mouse/Button%d", button_index + 1);
addNewBinding(std::move(m_new_binding_value));
stopListeningForInput();
return true;
}
return InputBindingDialog::eventFilter(watched, event);
}
void InputButtonBindingDialog::hookControllerInput()
{
ControllerInterface* controller_interface = m_host_interface->getControllerInterface();
@ -206,7 +219,7 @@ void InputButtonBindingDialog::hookControllerInput()
// TODO: this probably should consider the "last value"
QMetaObject::invokeMethod(this, "bindToControllerAxis", Q_ARG(int, ei.controller_index),
Q_ARG(int, ei.button_or_axis_number), Q_ARG(bool, ei.value > 0));
Q_ARG(int, ei.button_or_axis_number), Q_ARG(std::optional<bool>, ei.value > 0));
return ControllerInterface::Hook::CallbackResult::StopMonitoring;
}
else if (ei.type == ControllerInterface::Hook::Type::Button && ei.value > 0.0f)
@ -229,21 +242,6 @@ void InputButtonBindingDialog::unhookControllerInput()
controller_interface->ClearHook();
}
void InputButtonBindingDialog::bindToControllerAxis(int controller_index, int axis_index, bool positive)
{
std::string binding =
StringUtil::StdStringFromFormat("Controller%d/%cAxis%d", controller_index, positive ? '+' : '-', axis_index);
addNewBinding(std::move(binding));
stopListeningForInput();
}
void InputButtonBindingDialog::bindToControllerButton(int controller_index, int button_index)
{
std::string binding = StringUtil::StdStringFromFormat("Controller%d/Button%d", controller_index, button_index);
addNewBinding(std::move(binding));
stopListeningForInput();
}
void InputButtonBindingDialog::startListeningForInput(u32 timeout_in_seconds)
{
InputBindingDialog::startListeningForInput(timeout_in_seconds);
@ -257,8 +255,10 @@ void InputButtonBindingDialog::stopListeningForInput()
}
InputAxisBindingDialog::InputAxisBindingDialog(QtHostInterface* host_interface, std::string section_name,
std::string key_name, std::vector<std::string> bindings, QWidget* parent)
: InputBindingDialog(host_interface, std::move(section_name), std::move(key_name), std::move(bindings), parent)
std::string key_name, std::vector<std::string> bindings,
Controller::AxisType axis_type, QWidget* parent)
: InputBindingDialog(host_interface, std::move(section_name), std::move(key_name), std::move(bindings), parent),
m_axis_type(axis_type)
{
}
@ -282,6 +282,13 @@ void InputAxisBindingDialog::hookControllerInput()
return ControllerInterface::Hook::CallbackResult::ContinueMonitoring;
QMetaObject::invokeMethod(this, "bindToControllerAxis", Q_ARG(int, ei.controller_index),
Q_ARG(int, ei.button_or_axis_number), Q_ARG(std::optional<bool>, std::nullopt));
return ControllerInterface::Hook::CallbackResult::StopMonitoring;
}
else if (ei.type == ControllerInterface::Hook::Type::Button && m_axis_type == Controller::AxisType::Half &&
ei.value > 0.0f)
{
QMetaObject::invokeMethod(this, "bindToControllerButton", Q_ARG(int, ei.controller_index),
Q_ARG(int, ei.button_or_axis_number));
return ControllerInterface::Hook::CallbackResult::StopMonitoring;
}
@ -299,11 +306,19 @@ void InputAxisBindingDialog::unhookControllerInput()
controller_interface->ClearHook();
}
void InputAxisBindingDialog::bindToControllerAxis(int controller_index, int axis_index)
bool InputAxisBindingDialog::eventFilter(QObject* watched, QEvent* event)
{
std::string binding = StringUtil::StdStringFromFormat("Controller%d/Axis%d", controller_index, axis_index);
addNewBinding(std::move(binding));
stopListeningForInput();
if (m_axis_type != Controller::AxisType::Half)
{
const QEvent::Type event_type = event->type();
if (event_type == QEvent::KeyRelease || event_type == QEvent::KeyPress || event_type == QEvent::MouseButtonRelease)
{
return true;
}
}
return InputBindingDialog::eventFilter(watched, event);
}
void InputAxisBindingDialog::startListeningForInput(u32 timeout_in_seconds)

View File

@ -1,7 +1,9 @@
#pragma once
#include "common/types.h"
#include "core/controller.h"
#include "ui_inputbindingdialog.h"
#include <QtWidgets/QDialog>
#include <optional>
#include <string>
#include <vector>
@ -17,6 +19,8 @@ public:
~InputBindingDialog();
protected Q_SLOTS:
void bindToControllerAxis(int controller_index, int axis_index, std::optional<bool> positive);
void bindToControllerButton(int controller_index, int button_index);
void onAddBindingButtonClicked();
void onRemoveBindingButtonClicked();
void onClearBindingsButtonClicked();
@ -52,7 +56,7 @@ protected:
u32 m_input_listen_remaining_seconds = 0;
};
class InputButtonBindingDialog : public InputBindingDialog
class InputButtonBindingDialog final : public InputBindingDialog
{
Q_OBJECT
@ -61,13 +65,6 @@ public:
std::vector<std::string> bindings, QWidget* parent);
~InputButtonBindingDialog();
protected:
bool eventFilter(QObject* watched, QEvent* event) override;
private Q_SLOTS:
void bindToControllerAxis(int controller_index, int axis_index, bool positive);
void bindToControllerButton(int controller_index, int button_index);
protected:
void startListeningForInput(u32 timeout_in_seconds) override;
void stopListeningForInput() override;
@ -75,21 +72,22 @@ protected:
void unhookControllerInput();
};
class InputAxisBindingDialog : public InputBindingDialog
class InputAxisBindingDialog final : public InputBindingDialog
{
Q_OBJECT
public:
InputAxisBindingDialog(QtHostInterface* host_interface, std::string section_name, std::string key_name,
std::vector<std::string> bindings, QWidget* parent);
std::vector<std::string> bindings, Controller::AxisType axis_type, QWidget* parent);
~InputAxisBindingDialog();
private Q_SLOTS:
void bindToControllerAxis(int controller_index, int axis_index);
protected:
bool eventFilter(QObject* watched, QEvent* event) override;
void startListeningForInput(u32 timeout_in_seconds) override;
void stopListeningForInput() override;
void hookControllerInput();
void unhookControllerInput();
private:
Controller::AxisType m_axis_type;
};

View File

@ -40,6 +40,27 @@ void InputBindingWidget::updateText()
setText(QString::fromStdString(m_bindings[0]));
}
void InputBindingWidget::bindToControllerAxis(int controller_index, int axis_index, std::optional<bool> positive)
{
const char* sign_char = "";
if (positive)
{
sign_char = *positive ? "+" : "-";
}
m_new_binding_value =
StringUtil::StdStringFromFormat("Controller%d/%sAxis%d", controller_index, sign_char, axis_index);
setNewBinding();
stopListeningForInput();
}
void InputBindingWidget::bindToControllerButton(int controller_index, int button_index)
{
m_new_binding_value = StringUtil::StdStringFromFormat("Controller%d/Button%d", controller_index, button_index);
setNewBinding();
stopListeningForInput();
}
void InputBindingWidget::beginRebindAll()
{
m_is_binding_all = true;
@ -53,8 +74,32 @@ bool InputBindingWidget::eventFilter(QObject* watched, QEvent* event)
{
const QEvent::Type event_type = event->type();
if (event_type == QEvent::MouseButtonPress || event_type == QEvent::MouseButtonRelease ||
event_type == QEvent::MouseButtonDblClick)
// if the key is being released, set the input
if (event_type == QEvent::KeyRelease)
{
setNewBinding();
stopListeningForInput();
return true;
}
else if (event_type == QEvent::KeyPress)
{
QString binding = QtUtils::KeyEventToString(static_cast<QKeyEvent*>(event));
if (!binding.isEmpty())
m_new_binding_value = QStringLiteral("Keyboard/%1").arg(binding).toStdString();
return true;
}
else if (event_type == QEvent::MouseButtonRelease)
{
const u32 button_mask = static_cast<u32>(static_cast<const QMouseEvent*>(event)->button());
const u32 button_index = (button_mask == 0u) ? 0 : CountTrailingZeros(button_mask);
m_new_binding_value = StringUtil::StdStringFromFormat("Mouse/Button%d", button_index + 1);
setNewBinding();
stopListeningForInput();
return true;
}
if (event_type == QEvent::MouseButtonPress || event_type == QEvent::MouseButtonDblClick)
{
return true;
}
@ -185,38 +230,6 @@ InputButtonBindingWidget::~InputButtonBindingWidget()
InputButtonBindingWidget::stopListeningForInput();
}
bool InputButtonBindingWidget::eventFilter(QObject* watched, QEvent* event)
{
const QEvent::Type event_type = event->type();
// if the key is being released, set the input
if (event_type == QEvent::KeyRelease)
{
setNewBinding();
stopListeningForInput();
return true;
}
else if (event_type == QEvent::KeyPress)
{
QString binding = QtUtils::KeyEventToString(static_cast<QKeyEvent*>(event));
if (!binding.isEmpty())
m_new_binding_value = QStringLiteral("Keyboard/%1").arg(binding).toStdString();
return true;
}
else if (event_type == QEvent::MouseButtonRelease)
{
const u32 button_mask = static_cast<u32>(static_cast<const QMouseEvent*>(event)->button());
const u32 button_index = (button_mask == 0u) ? 0 : CountTrailingZeros(button_mask);
m_new_binding_value = StringUtil::StdStringFromFormat("Mouse/Button%d", button_index + 1);
setNewBinding();
stopListeningForInput();
return true;
}
return InputBindingWidget::eventFilter(watched, event);
}
void InputButtonBindingWidget::hookControllerInput()
{
ControllerInterface* controller_interface = m_host_interface->getControllerInterface();
@ -232,7 +245,7 @@ void InputButtonBindingWidget::hookControllerInput()
// TODO: this probably should consider the "last value"
QMetaObject::invokeMethod(this, "bindToControllerAxis", Q_ARG(int, ei.controller_index),
Q_ARG(int, ei.button_or_axis_number), Q_ARG(bool, ei.value > 0));
Q_ARG(int, ei.button_or_axis_number), Q_ARG(std::optional<bool>, ei.value > 0));
return ControllerInterface::Hook::CallbackResult::StopMonitoring;
}
else if (ei.type == ControllerInterface::Hook::Type::Button && ei.value > 0.0f)
@ -255,21 +268,6 @@ void InputButtonBindingWidget::unhookControllerInput()
controller_interface->ClearHook();
}
void InputButtonBindingWidget::bindToControllerAxis(int controller_index, int axis_index, bool positive)
{
m_new_binding_value =
StringUtil::StdStringFromFormat("Controller%d/%cAxis%d", controller_index, positive ? '+' : '-', axis_index);
setNewBinding();
stopListeningForInput();
}
void InputButtonBindingWidget::bindToControllerButton(int controller_index, int button_index)
{
m_new_binding_value = StringUtil::StdStringFromFormat("Controller%d/Button%d", controller_index, button_index);
setNewBinding();
stopListeningForInput();
}
void InputButtonBindingWidget::startListeningForInput(u32 timeout_in_seconds)
{
InputBindingWidget::startListeningForInput(timeout_in_seconds);
@ -291,8 +289,8 @@ void InputButtonBindingWidget::openDialog()
}
InputAxisBindingWidget::InputAxisBindingWidget(QtHostInterface* host_interface, std::string section_name,
std::string key_name, QWidget* parent)
: InputBindingWidget(host_interface, std::move(section_name), std::move(key_name), parent)
std::string key_name, Controller::AxisType axis_type, QWidget* parent)
: InputBindingWidget(host_interface, std::move(section_name), std::move(key_name), parent), m_axis_type(axis_type)
{
}
@ -316,6 +314,13 @@ void InputAxisBindingWidget::hookControllerInput()
return ControllerInterface::Hook::CallbackResult::ContinueMonitoring;
QMetaObject::invokeMethod(this, "bindToControllerAxis", Q_ARG(int, ei.controller_index),
Q_ARG(int, ei.button_or_axis_number), Q_ARG(std::optional<bool>, std::nullopt));
return ControllerInterface::Hook::CallbackResult::StopMonitoring;
}
else if (ei.type == ControllerInterface::Hook::Type::Button && m_axis_type == Controller::AxisType::Half &&
ei.value > 0.0f)
{
QMetaObject::invokeMethod(this, "bindToControllerButton", Q_ARG(int, ei.controller_index),
Q_ARG(int, ei.button_or_axis_number));
return ControllerInterface::Hook::CallbackResult::StopMonitoring;
}
@ -333,11 +338,19 @@ void InputAxisBindingWidget::unhookControllerInput()
controller_interface->ClearHook();
}
void InputAxisBindingWidget::bindToControllerAxis(int controller_index, int axis_index)
bool InputAxisBindingWidget::eventFilter(QObject* watched, QEvent* event)
{
m_new_binding_value = StringUtil::StdStringFromFormat("Controller%d/Axis%d", controller_index, axis_index);
setNewBinding();
stopListeningForInput();
if (m_axis_type != Controller::AxisType::Half)
{
const QEvent::Type event_type = event->type();
if (event_type == QEvent::KeyRelease || event_type == QEvent::KeyPress || event_type == QEvent::MouseButtonRelease)
{
return true;
}
}
return InputBindingWidget::eventFilter(watched, event);
}
void InputAxisBindingWidget::startListeningForInput(u32 timeout_in_seconds)
@ -354,7 +367,7 @@ void InputAxisBindingWidget::stopListeningForInput()
void InputAxisBindingWidget::openDialog()
{
InputAxisBindingDialog binding_dialog(m_host_interface, m_section_name, m_key_name, m_bindings,
InputAxisBindingDialog binding_dialog(m_host_interface, m_section_name, m_key_name, m_bindings, m_axis_type,
QtUtils::GetRootWidget(this));
binding_dialog.exec();
reloadBinding();

View File

@ -1,6 +1,8 @@
#pragma once
#include "core/controller.h"
#include "core/types.h"
#include <QtWidgets/QPushButton>
#include <optional>
class QTimer;
@ -18,6 +20,8 @@ public:
ALWAYS_INLINE void setNextWidget(InputBindingWidget* widget) { m_next_widget = widget; }
public Q_SLOTS:
void bindToControllerAxis(int controller_index, int axis_index, std::optional<bool> positive);
void bindToControllerButton(int controller_index, int button_index);
void beginRebindAll();
void clearBinding();
void reloadBinding();
@ -66,13 +70,6 @@ public:
QWidget* parent);
~InputButtonBindingWidget();
protected:
bool eventFilter(QObject* watched, QEvent* event) override;
private Q_SLOTS:
void bindToControllerAxis(int controller_index, int axis_index, bool positive);
void bindToControllerButton(int controller_index, int button_index);
protected:
void startListeningForInput(u32 timeout_in_seconds) override;
void stopListeningForInput() override;
@ -87,18 +84,19 @@ class InputAxisBindingWidget : public InputBindingWidget
public:
InputAxisBindingWidget(QtHostInterface* host_interface, std::string section_name, std::string key_name,
QWidget* parent);
Controller::AxisType axis_type, QWidget* parent);
~InputAxisBindingWidget();
private Q_SLOTS:
void bindToControllerAxis(int controller_index, int axis_index);
protected:
bool eventFilter(QObject* watched, QEvent* event) override;
void startListeningForInput(u32 timeout_in_seconds) override;
void stopListeningForInput() override;
void openDialog() override;
void hookControllerInput();
void unhookControllerInput();
private:
Controller::AxisType m_axis_type;
};
class InputRumbleBindingWidget : public InputBindingWidget

View File

@ -1,6 +1,7 @@
#include "common/log.h"
#include "mainwindow.h"
#include "qthostinterface.h"
#include "qtutils.h"
#include <QtWidgets/QApplication>
#include <QtWidgets/QMessageBox>
#include <cstdlib>
@ -8,6 +9,9 @@
int main(int argc, char* argv[])
{
// Register any standard types we need elsewhere
qRegisterMetaType<std::optional<bool>>();
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);

View File

@ -1,9 +1,12 @@
#pragma once
#include <QtCore/QByteArray>
#include <QtCore/QMetaType>
#include <QtCore/QString>
#include <initializer_list>
#include <optional>
Q_DECLARE_METATYPE(std::optional<bool>);
class ByteStream;
class QFrame;