2020-01-05 02:46:03 +00:00
|
|
|
#include "inputbindingwidgets.h"
|
2020-04-26 07:23:42 +00:00
|
|
|
#include "common/bitutils.h"
|
2020-07-21 09:49:04 +00:00
|
|
|
#include "common/string_util.h"
|
2020-01-05 02:46:03 +00:00
|
|
|
#include "core/settings.h"
|
2020-03-21 14:49:46 +00:00
|
|
|
#include "frontend-common/controller_interface.h"
|
2020-07-22 16:35:15 +00:00
|
|
|
#include "inputbindingdialog.h"
|
2020-11-13 23:32:35 +00:00
|
|
|
#include "inputbindingmonitor.h"
|
2020-01-05 02:46:03 +00:00
|
|
|
#include "qthostinterface.h"
|
|
|
|
#include "qtutils.h"
|
|
|
|
#include <QtCore/QTimer>
|
|
|
|
#include <QtGui/QKeyEvent>
|
2020-04-14 06:35:04 +00:00
|
|
|
#include <QtGui/QMouseEvent>
|
2020-02-17 15:31:01 +00:00
|
|
|
#include <cmath>
|
2020-01-05 02:46:03 +00:00
|
|
|
|
2020-07-21 09:49:04 +00:00
|
|
|
InputBindingWidget::InputBindingWidget(QtHostInterface* host_interface, std::string section_name, std::string key_name,
|
2020-07-13 16:24:11 +00:00
|
|
|
QWidget* parent)
|
2020-07-21 09:49:04 +00:00
|
|
|
: QPushButton(parent), m_host_interface(host_interface), m_section_name(std::move(section_name)),
|
|
|
|
m_key_name(std::move(key_name))
|
2020-01-05 02:46:03 +00:00
|
|
|
{
|
2020-07-22 16:35:15 +00:00
|
|
|
m_bindings = m_host_interface->GetSettingStringList(m_section_name.c_str(), m_key_name.c_str());
|
|
|
|
updateText();
|
|
|
|
|
2020-04-14 06:35:04 +00:00
|
|
|
setMinimumWidth(150);
|
|
|
|
setMaximumWidth(150);
|
2020-01-05 02:46:03 +00:00
|
|
|
|
2020-04-26 07:22:29 +00:00
|
|
|
connect(this, &QPushButton::clicked, this, &InputBindingWidget::onClicked);
|
2020-01-05 02:46:03 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 15:06:11 +00:00
|
|
|
InputBindingWidget::~InputBindingWidget()
|
2020-01-05 02:46:03 +00:00
|
|
|
{
|
2020-02-17 15:06:11 +00:00
|
|
|
Q_ASSERT(!isListeningForInput());
|
2020-01-05 02:46:03 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 16:35:15 +00:00
|
|
|
void InputBindingWidget::updateText()
|
|
|
|
{
|
|
|
|
if (m_bindings.empty())
|
|
|
|
setText(QString());
|
|
|
|
else if (m_bindings.size() > 1)
|
2021-03-07 15:39:46 +00:00
|
|
|
setText(tr("%n bindings", "", static_cast<int>(m_bindings.size())));
|
2020-07-22 16:35:15 +00:00
|
|
|
else
|
|
|
|
setText(QString::fromStdString(m_bindings[0]));
|
|
|
|
}
|
|
|
|
|
2020-11-15 13:56:52 +00:00
|
|
|
void InputBindingWidget::bindToControllerAxis(int controller_index, int axis_index, bool inverted,
|
|
|
|
std::optional<bool> half_axis_positive)
|
2020-08-29 12:19:28 +00:00
|
|
|
{
|
2020-11-15 13:56:52 +00:00
|
|
|
const char* invert_char = inverted ? "-" : "";
|
2020-08-29 12:19:28 +00:00
|
|
|
const char* sign_char = "";
|
2020-11-15 13:56:52 +00:00
|
|
|
if (half_axis_positive)
|
2020-08-29 12:19:28 +00:00
|
|
|
{
|
2020-11-15 13:56:52 +00:00
|
|
|
sign_char = *half_axis_positive ? "+" : "-";
|
2020-08-29 12:19:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_new_binding_value =
|
2020-11-15 13:56:52 +00:00
|
|
|
StringUtil::StdStringFromFormat("Controller%d/%sAxis%d%s", controller_index, sign_char, axis_index, invert_char);
|
2020-08-29 12:19:28 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-11-15 13:56:52 +00:00
|
|
|
void InputBindingWidget::bindToControllerHat(int controller_index, int hat_index, const QString& hat_direction)
|
|
|
|
{
|
|
|
|
m_new_binding_value = StringUtil::StdStringFromFormat("Controller%d/Hat%d %s", controller_index, hat_index,
|
|
|
|
hat_direction.toLatin1().constData());
|
|
|
|
setNewBinding();
|
|
|
|
stopListeningForInput();
|
|
|
|
}
|
|
|
|
|
2020-03-21 14:50:09 +00:00
|
|
|
void InputBindingWidget::beginRebindAll()
|
|
|
|
{
|
|
|
|
m_is_binding_all = true;
|
|
|
|
if (isListeningForInput())
|
|
|
|
stopListeningForInput();
|
|
|
|
|
|
|
|
startListeningForInput(TIMEOUT_FOR_ALL_BINDING);
|
|
|
|
}
|
|
|
|
|
2020-02-17 15:06:11 +00:00
|
|
|
bool InputBindingWidget::eventFilter(QObject* watched, QEvent* event)
|
2020-01-05 02:46:03 +00:00
|
|
|
{
|
2020-01-06 05:14:47 +00:00
|
|
|
const QEvent::Type event_type = event->type();
|
|
|
|
|
2020-08-29 12:19:28 +00:00
|
|
|
// if the key is being released, set the input
|
|
|
|
if (event_type == QEvent::KeyRelease)
|
|
|
|
{
|
|
|
|
setNewBinding();
|
|
|
|
stopListeningForInput();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (event_type == QEvent::KeyPress)
|
|
|
|
{
|
2021-03-07 15:39:46 +00:00
|
|
|
const QKeyEvent* key_event = static_cast<const QKeyEvent*>(event);
|
|
|
|
const QString binding(QtUtils::KeyEventToString(key_event->key(), key_event->modifiers()));
|
2020-08-29 12:19:28 +00:00
|
|
|
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)
|
2020-01-05 02:46:03 +00:00
|
|
|
{
|
2020-01-06 05:14:47 +00:00
|
|
|
return true;
|
2020-01-05 02:46:03 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 05:14:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-22 16:35:15 +00:00
|
|
|
bool InputBindingWidget::event(QEvent* event)
|
|
|
|
{
|
|
|
|
if (event->type() == QEvent::MouseButtonRelease)
|
|
|
|
{
|
|
|
|
QMouseEvent* mev = static_cast<QMouseEvent*>(event);
|
|
|
|
if (mev->button() == Qt::LeftButton && mev->modifiers() & Qt::ShiftModifier)
|
|
|
|
{
|
|
|
|
openDialog();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QPushButton::event(event);
|
|
|
|
}
|
|
|
|
|
2020-03-21 14:49:55 +00:00
|
|
|
void InputBindingWidget::mouseReleaseEvent(QMouseEvent* e)
|
|
|
|
{
|
|
|
|
if (e->button() == Qt::RightButton)
|
|
|
|
{
|
|
|
|
clearBinding();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPushButton::mouseReleaseEvent(e);
|
|
|
|
}
|
|
|
|
|
2020-02-17 15:06:11 +00:00
|
|
|
void InputBindingWidget::setNewBinding()
|
2020-01-06 05:14:47 +00:00
|
|
|
{
|
2020-07-21 09:49:04 +00:00
|
|
|
if (m_new_binding_value.empty())
|
2020-01-06 05:14:47 +00:00
|
|
|
return;
|
|
|
|
|
2020-07-21 09:49:04 +00:00
|
|
|
m_host_interface->SetStringSettingValue(m_section_name.c_str(), m_key_name.c_str(), m_new_binding_value.c_str());
|
2020-01-06 05:14:47 +00:00
|
|
|
m_host_interface->updateInputMap();
|
|
|
|
|
2020-07-22 16:35:15 +00:00
|
|
|
m_bindings.clear();
|
|
|
|
m_bindings.push_back(std::move(m_new_binding_value));
|
2020-01-05 02:46:03 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 14:49:55 +00:00
|
|
|
void InputBindingWidget::clearBinding()
|
|
|
|
{
|
2020-07-22 16:35:15 +00:00
|
|
|
m_bindings.clear();
|
2020-07-21 09:49:04 +00:00
|
|
|
m_host_interface->RemoveSettingValue(m_section_name.c_str(), m_key_name.c_str());
|
2020-03-21 14:49:55 +00:00
|
|
|
m_host_interface->updateInputMap();
|
2020-07-22 16:35:15 +00:00
|
|
|
updateText();
|
2020-03-21 14:49:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 15:44:16 +00:00
|
|
|
void InputBindingWidget::reloadBinding()
|
|
|
|
{
|
2020-07-22 16:35:15 +00:00
|
|
|
m_bindings = m_host_interface->GetSettingStringList(m_section_name.c_str(), m_key_name.c_str());
|
|
|
|
updateText();
|
2020-04-14 15:44:16 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 07:22:29 +00:00
|
|
|
void InputBindingWidget::onClicked()
|
2020-01-05 02:46:03 +00:00
|
|
|
{
|
2020-07-22 16:35:15 +00:00
|
|
|
if (m_bindings.size() > 1)
|
|
|
|
{
|
|
|
|
openDialog();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-05 02:46:03 +00:00
|
|
|
if (isListeningForInput())
|
|
|
|
stopListeningForInput();
|
|
|
|
|
2020-03-21 14:50:09 +00:00
|
|
|
startListeningForInput(TIMEOUT_FOR_SINGLE_BINDING);
|
2020-01-05 02:46:03 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 15:06:11 +00:00
|
|
|
void InputBindingWidget::onInputListenTimerTimeout()
|
2020-01-05 02:46:03 +00:00
|
|
|
{
|
|
|
|
m_input_listen_remaining_seconds--;
|
|
|
|
if (m_input_listen_remaining_seconds == 0)
|
|
|
|
{
|
|
|
|
stopListeningForInput();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-17 15:06:11 +00:00
|
|
|
setText(tr("Push Button/Axis... [%1]").arg(m_input_listen_remaining_seconds));
|
|
|
|
}
|
|
|
|
|
2020-03-21 14:50:09 +00:00
|
|
|
void InputBindingWidget::startListeningForInput(u32 timeout_in_seconds)
|
2020-02-17 15:06:11 +00:00
|
|
|
{
|
|
|
|
m_input_listen_timer = new QTimer(this);
|
|
|
|
m_input_listen_timer->setSingleShot(false);
|
|
|
|
m_input_listen_timer->start(1000);
|
|
|
|
|
|
|
|
m_input_listen_timer->connect(m_input_listen_timer, &QTimer::timeout, this,
|
|
|
|
&InputBindingWidget::onInputListenTimerTimeout);
|
2020-03-21 14:50:09 +00:00
|
|
|
m_input_listen_remaining_seconds = timeout_in_seconds;
|
2020-02-17 15:06:11 +00:00
|
|
|
setText(tr("Push Button/Axis... [%1]").arg(m_input_listen_remaining_seconds));
|
|
|
|
|
|
|
|
installEventFilter(this);
|
|
|
|
grabKeyboard();
|
|
|
|
grabMouse();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputBindingWidget::stopListeningForInput()
|
|
|
|
{
|
2020-07-22 16:35:15 +00:00
|
|
|
updateText();
|
2020-02-17 15:06:11 +00:00
|
|
|
delete m_input_listen_timer;
|
|
|
|
m_input_listen_timer = nullptr;
|
|
|
|
|
|
|
|
releaseMouse();
|
|
|
|
releaseKeyboard();
|
|
|
|
removeEventFilter(this);
|
2020-03-21 14:50:09 +00:00
|
|
|
|
|
|
|
if (m_is_binding_all && m_next_widget)
|
|
|
|
m_next_widget->beginRebindAll();
|
|
|
|
m_is_binding_all = false;
|
2020-02-17 15:06:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 16:35:15 +00:00
|
|
|
void InputBindingWidget::openDialog() {}
|
|
|
|
|
2020-07-21 09:49:04 +00:00
|
|
|
InputButtonBindingWidget::InputButtonBindingWidget(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)
|
2020-02-17 15:06:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
InputButtonBindingWidget::~InputButtonBindingWidget()
|
|
|
|
{
|
|
|
|
if (isListeningForInput())
|
|
|
|
InputButtonBindingWidget::stopListeningForInput();
|
|
|
|
}
|
|
|
|
|
2020-02-15 15:14:53 +00:00
|
|
|
void InputButtonBindingWidget::hookControllerInput()
|
|
|
|
{
|
2020-03-21 14:49:46 +00:00
|
|
|
ControllerInterface* controller_interface = m_host_interface->getControllerInterface();
|
|
|
|
if (!controller_interface)
|
|
|
|
return;
|
|
|
|
|
2020-11-13 23:32:35 +00:00
|
|
|
controller_interface->SetHook(InputButtonBindingMonitor(this));
|
2020-02-15 15:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputButtonBindingWidget::unhookControllerInput()
|
|
|
|
{
|
2020-03-21 14:49:46 +00:00
|
|
|
ControllerInterface* controller_interface = m_host_interface->getControllerInterface();
|
|
|
|
if (!controller_interface)
|
|
|
|
return;
|
|
|
|
|
|
|
|
controller_interface->ClearHook();
|
2020-02-15 15:14:53 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 14:50:09 +00:00
|
|
|
void InputButtonBindingWidget::startListeningForInput(u32 timeout_in_seconds)
|
2020-01-05 02:46:03 +00:00
|
|
|
{
|
2020-03-21 14:50:09 +00:00
|
|
|
InputBindingWidget::startListeningForInput(timeout_in_seconds);
|
2020-02-15 15:14:53 +00:00
|
|
|
hookControllerInput();
|
2020-01-05 02:46:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputButtonBindingWidget::stopListeningForInput()
|
|
|
|
{
|
2020-02-17 15:06:11 +00:00
|
|
|
unhookControllerInput();
|
|
|
|
InputBindingWidget::stopListeningForInput();
|
|
|
|
}
|
|
|
|
|
2020-07-22 16:35:15 +00:00
|
|
|
void InputButtonBindingWidget::openDialog()
|
|
|
|
{
|
|
|
|
InputButtonBindingDialog binding_dialog(m_host_interface, m_section_name, m_key_name, m_bindings,
|
|
|
|
QtUtils::GetRootWidget(this));
|
|
|
|
binding_dialog.exec();
|
|
|
|
reloadBinding();
|
|
|
|
}
|
|
|
|
|
2020-07-21 09:49:04 +00:00
|
|
|
InputAxisBindingWidget::InputAxisBindingWidget(QtHostInterface* host_interface, std::string section_name,
|
2020-08-29 12:19:28 +00:00
|
|
|
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)
|
2020-02-17 15:06:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
InputAxisBindingWidget::~InputAxisBindingWidget()
|
|
|
|
{
|
|
|
|
if (isListeningForInput())
|
|
|
|
InputAxisBindingWidget::stopListeningForInput();
|
|
|
|
}
|
2020-01-06 05:14:47 +00:00
|
|
|
|
2020-02-17 15:06:11 +00:00
|
|
|
void InputAxisBindingWidget::hookControllerInput()
|
|
|
|
{
|
2020-03-21 14:49:46 +00:00
|
|
|
ControllerInterface* controller_interface = m_host_interface->getControllerInterface();
|
|
|
|
if (!controller_interface)
|
|
|
|
return;
|
|
|
|
|
2020-11-13 23:32:35 +00:00
|
|
|
controller_interface->SetHook(InputAxisBindingMonitor(this, m_axis_type));
|
2020-02-17 15:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputAxisBindingWidget::unhookControllerInput()
|
|
|
|
{
|
2020-03-21 14:49:46 +00:00
|
|
|
ControllerInterface* controller_interface = m_host_interface->getControllerInterface();
|
|
|
|
if (!controller_interface)
|
|
|
|
return;
|
|
|
|
|
|
|
|
controller_interface->ClearHook();
|
2020-02-17 15:06:11 +00:00
|
|
|
}
|
|
|
|
|
2020-08-29 12:19:28 +00:00
|
|
|
bool InputAxisBindingWidget::eventFilter(QObject* watched, QEvent* event)
|
2020-02-17 15:06:11 +00:00
|
|
|
{
|
2020-08-29 12:19:28 +00:00
|
|
|
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);
|
2020-02-17 15:06:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 14:50:09 +00:00
|
|
|
void InputAxisBindingWidget::startListeningForInput(u32 timeout_in_seconds)
|
2020-02-17 15:06:11 +00:00
|
|
|
{
|
2020-03-21 14:50:09 +00:00
|
|
|
InputBindingWidget::startListeningForInput(timeout_in_seconds);
|
2020-02-17 15:06:11 +00:00
|
|
|
hookControllerInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputAxisBindingWidget::stopListeningForInput()
|
|
|
|
{
|
2020-02-15 15:14:53 +00:00
|
|
|
unhookControllerInput();
|
2020-02-17 15:06:11 +00:00
|
|
|
InputBindingWidget::stopListeningForInput();
|
2020-01-05 02:46:03 +00:00
|
|
|
}
|
2020-04-14 06:35:04 +00:00
|
|
|
|
2020-07-22 16:35:15 +00:00
|
|
|
void InputAxisBindingWidget::openDialog()
|
|
|
|
{
|
2020-08-29 12:19:28 +00:00
|
|
|
InputAxisBindingDialog binding_dialog(m_host_interface, m_section_name, m_key_name, m_bindings, m_axis_type,
|
2020-07-22 16:35:15 +00:00
|
|
|
QtUtils::GetRootWidget(this));
|
|
|
|
binding_dialog.exec();
|
|
|
|
reloadBinding();
|
|
|
|
}
|
|
|
|
|
2020-07-21 09:49:04 +00:00
|
|
|
InputRumbleBindingWidget::InputRumbleBindingWidget(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)
|
2020-04-14 06:35:04 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
InputRumbleBindingWidget::~InputRumbleBindingWidget()
|
|
|
|
{
|
|
|
|
if (isListeningForInput())
|
|
|
|
InputRumbleBindingWidget::stopListeningForInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputRumbleBindingWidget::hookControllerInput()
|
|
|
|
{
|
|
|
|
ControllerInterface* controller_interface = m_host_interface->getControllerInterface();
|
|
|
|
if (!controller_interface)
|
|
|
|
return;
|
|
|
|
|
2020-11-13 23:32:35 +00:00
|
|
|
controller_interface->SetHook(InputRumbleBindingMonitor(this));
|
2020-04-14 06:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputRumbleBindingWidget::unhookControllerInput()
|
|
|
|
{
|
|
|
|
ControllerInterface* controller_interface = m_host_interface->getControllerInterface();
|
|
|
|
if (!controller_interface)
|
|
|
|
return;
|
|
|
|
|
|
|
|
controller_interface->ClearHook();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputRumbleBindingWidget::bindToControllerRumble(int controller_index)
|
|
|
|
{
|
2020-07-21 09:49:04 +00:00
|
|
|
m_new_binding_value = StringUtil::StdStringFromFormat("Controller%d", controller_index);
|
2020-04-14 06:35:04 +00:00
|
|
|
setNewBinding();
|
|
|
|
stopListeningForInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputRumbleBindingWidget::startListeningForInput(u32 timeout_in_seconds)
|
|
|
|
{
|
|
|
|
InputBindingWidget::startListeningForInput(timeout_in_seconds);
|
|
|
|
hookControllerInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputRumbleBindingWidget::stopListeningForInput()
|
|
|
|
{
|
|
|
|
unhookControllerInput();
|
|
|
|
InputBindingWidget::stopListeningForInput();
|
|
|
|
}
|