mirror of https://github.com/stenzek/duckstation
Factorize input hooks into reusable monitor classes
Those monitors will need some context so this will be useful in the nearby future.pull/1078/head
parent
3f9ba4acb6
commit
99ec667b20
@ -0,0 +1,60 @@
|
||||
#include "inputbindingmonitor.h"
|
||||
|
||||
ControllerInterface::Hook::CallbackResult
|
||||
InputButtonBindingMonitor::operator()(const ControllerInterface::Hook& ei) const
|
||||
{
|
||||
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(m_parent, "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(m_parent, "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;
|
||||
}
|
||||
|
||||
ControllerInterface::Hook::CallbackResult InputAxisBindingMonitor::operator()(const ControllerInterface::Hook& ei) const
|
||||
{
|
||||
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(m_parent, "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(m_parent, "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;
|
||||
}
|
||||
|
||||
ControllerInterface::Hook::CallbackResult
|
||||
InputRumbleBindingMonitor::operator()(const ControllerInterface::Hook& ei) const
|
||||
{
|
||||
if (ei.type == ControllerInterface::Hook::Type::Button && ei.value > 0.0f)
|
||||
{
|
||||
QMetaObject::invokeMethod(m_parent, "bindToControllerRumble", Q_ARG(int, ei.controller_index));
|
||||
return ControllerInterface::Hook::CallbackResult::StopMonitoring;
|
||||
}
|
||||
|
||||
return ControllerInterface::Hook::CallbackResult::ContinueMonitoring;
|
||||
}
|
||||
@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include "frontend-common/controller_interface.h"
|
||||
#include <QtCore/QObject>
|
||||
|
||||
// NOTE: Those Monitor classes must be copyable to meet the requirements of std::function, but at the same time we want
|
||||
// copies to be opaque to the caling code and share context. Therefore, all mutable context of the monitor (if required)
|
||||
// must be enclosed in a std::shared_ptr. m_parent/m_axis_type don't mutate so they don't need to be stored as such.
|
||||
|
||||
class InputButtonBindingMonitor
|
||||
{
|
||||
public:
|
||||
explicit InputButtonBindingMonitor(QObject* parent) : m_parent(parent) {}
|
||||
|
||||
ControllerInterface::Hook::CallbackResult operator()(const ControllerInterface::Hook& ei) const;
|
||||
|
||||
private:
|
||||
QObject* m_parent;
|
||||
};
|
||||
|
||||
class InputAxisBindingMonitor
|
||||
{
|
||||
public:
|
||||
explicit InputAxisBindingMonitor(QObject* parent, Controller::AxisType axis_type)
|
||||
: m_parent(parent), m_axis_type(axis_type)
|
||||
{
|
||||
}
|
||||
|
||||
ControllerInterface::Hook::CallbackResult operator()(const ControllerInterface::Hook& ei) const;
|
||||
|
||||
private:
|
||||
QObject* m_parent;
|
||||
Controller::AxisType m_axis_type;
|
||||
};
|
||||
|
||||
class InputRumbleBindingMonitor
|
||||
{
|
||||
public:
|
||||
explicit InputRumbleBindingMonitor(QObject* parent) : m_parent(parent) {}
|
||||
|
||||
ControllerInterface::Hook::CallbackResult operator()(const ControllerInterface::Hook& ei) const;
|
||||
|
||||
private:
|
||||
QObject* m_parent;
|
||||
};
|
||||
Loading…
Reference in New Issue