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

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