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

@ -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)