Factorize input hooks into reusable monitor classes

Those monitors will need some context so this will be useful
in the nearby future.
This commit is contained in:
Silent
2020-11-14 00:32:35 +01:00
parent 3f9ba4acb6
commit 99ec667b20
7 changed files with 118 additions and 93 deletions

View File

@ -3,6 +3,7 @@
#include "common/string_util.h"
#include "core/settings.h"
#include "frontend-common/controller_interface.h"
#include "inputbindingmonitor.h"
#include "qthostinterface.h"
#include "qtutils.h"
#include <QtCore/QTimer>
@ -210,27 +211,7 @@ void InputButtonBindingDialog::hookControllerInput()
if (!controller_interface)
return;
controller_interface->SetHook([this](const ControllerInterface::Hook& ei) {
if (ei.type == ControllerInterface::Hook::Type::Axis)
{
// wait until it's at least half pushed so we don't get confused between axises with small movement
if (std::abs(ei.value) < 0.5f)
return ControllerInterface::Hook::CallbackResult::ContinueMonitoring;
// 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(std::optional<bool>, ei.value > 0));
return ControllerInterface::Hook::CallbackResult::StopMonitoring;
}
else if (ei.type == ControllerInterface::Hook::Type::Button && 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;
}
return ControllerInterface::Hook::CallbackResult::ContinueMonitoring;
});
controller_interface->SetHook(InputButtonBindingMonitor(this));
}
void InputButtonBindingDialog::unhookControllerInput()
@ -274,27 +255,7 @@ void InputAxisBindingDialog::hookControllerInput()
if (!controller_interface)
return;
controller_interface->SetHook([this](const ControllerInterface::Hook& ei) {
if (ei.type == ControllerInterface::Hook::Type::Axis)
{
// wait until it's at least half pushed so we don't get confused between axises with small movement
if (std::abs(ei.value) < 0.5f)
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;
}
return ControllerInterface::Hook::CallbackResult::ContinueMonitoring;
});
controller_interface->SetHook(InputAxisBindingMonitor(this, m_axis_type));
}
void InputAxisBindingDialog::unhookControllerInput()