2021-12-13 12:12:54 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
|
|
|
* Copyright (C) 2002-2022 PCSX2 Dev Team
|
|
|
|
*
|
|
|
|
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU Lesser General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with PCSX2.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "PrecompiledHeader.h"
|
|
|
|
|
|
|
|
#include "QtHost.h"
|
2022-06-21 07:47:32 +00:00
|
|
|
#include "QtUtils.h"
|
2021-12-13 12:12:54 +00:00
|
|
|
#include "Settings/InputBindingDialog.h"
|
2022-06-29 10:08:30 +00:00
|
|
|
#include "Settings/InputBindingWidget.h"
|
2021-12-13 12:12:54 +00:00
|
|
|
#include <QtCore/QTimer>
|
|
|
|
#include <QtGui/QKeyEvent>
|
|
|
|
#include <QtGui/QMouseEvent>
|
2022-07-02 11:51:23 +00:00
|
|
|
#include <QtGui/QWheelEvent>
|
2021-12-13 12:12:54 +00:00
|
|
|
|
|
|
|
// _BitScanForward()
|
|
|
|
#include "pcsx2/GS/GSIntrin.h"
|
|
|
|
|
2022-12-04 15:00:06 +00:00
|
|
|
InputBindingDialog::InputBindingDialog(SettingsInterface* sif, InputBindingInfo::Type bind_type, std::string section_name,
|
|
|
|
std::string key_name, std::vector<std::string> bindings, QWidget* parent)
|
2021-12-13 12:12:54 +00:00
|
|
|
: QDialog(parent)
|
2022-06-08 12:15:10 +00:00
|
|
|
, m_sif(sif)
|
2022-12-04 15:00:06 +00:00
|
|
|
, m_bind_type(bind_type)
|
2021-12-13 12:12:54 +00:00
|
|
|
, m_section_name(std::move(section_name))
|
|
|
|
, m_key_name(std::move(key_name))
|
|
|
|
, m_bindings(std::move(bindings))
|
|
|
|
{
|
|
|
|
m_ui.setupUi(this);
|
2022-12-04 15:00:06 +00:00
|
|
|
m_ui.title->setText(tr("Bindings for %1 %2").arg(QString::fromStdString(m_section_name)).arg(QString::fromStdString(m_key_name)));
|
2021-12-13 12:12:54 +00:00
|
|
|
m_ui.buttonBox->button(QDialogButtonBox::Close)->setText(tr("Close"));
|
|
|
|
|
|
|
|
connect(m_ui.addBinding, &QPushButton::clicked, this, &InputBindingDialog::onAddBindingButtonClicked);
|
|
|
|
connect(m_ui.removeBinding, &QPushButton::clicked, this, &InputBindingDialog::onRemoveBindingButtonClicked);
|
|
|
|
connect(m_ui.clearBindings, &QPushButton::clicked, this, &InputBindingDialog::onClearBindingsButtonClicked);
|
|
|
|
connect(m_ui.buttonBox, &QDialogButtonBox::rejected, [this]() { done(0); });
|
|
|
|
updateList();
|
|
|
|
}
|
|
|
|
|
|
|
|
InputBindingDialog::~InputBindingDialog()
|
|
|
|
{
|
|
|
|
Q_ASSERT(!isListeningForInput());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InputBindingDialog::eventFilter(QObject* watched, QEvent* event)
|
|
|
|
{
|
|
|
|
const QEvent::Type event_type = event->type();
|
|
|
|
|
|
|
|
// if the key is being released, set the input
|
2022-05-15 08:15:27 +00:00
|
|
|
if (event_type == QEvent::KeyRelease || event_type == QEvent::MouseButtonRelease)
|
2021-12-13 12:12:54 +00:00
|
|
|
{
|
|
|
|
addNewBinding();
|
|
|
|
stopListeningForInput();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (event_type == QEvent::KeyPress)
|
|
|
|
{
|
|
|
|
const QKeyEvent* key_event = static_cast<const QKeyEvent*>(event);
|
2022-06-21 07:47:32 +00:00
|
|
|
m_new_bindings.push_back(InputManager::MakeHostKeyboardKey(QtUtils::KeyEventToCode(key_event)));
|
2021-12-13 12:12:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-05-15 08:15:27 +00:00
|
|
|
else if (event_type == QEvent::MouseButtonPress || event_type == QEvent::MouseButtonDblClick)
|
2021-12-13 12:12:54 +00:00
|
|
|
{
|
2022-05-15 08:15:27 +00:00
|
|
|
// double clicks get triggered if we click bind, then click again quickly.
|
2021-12-13 12:12:54 +00:00
|
|
|
unsigned long button_index;
|
|
|
|
if (_BitScanForward(&button_index, static_cast<u32>(static_cast<const QMouseEvent*>(event)->button())))
|
2022-05-15 08:15:27 +00:00
|
|
|
m_new_bindings.push_back(InputManager::MakePointerButtonKey(0, button_index));
|
2021-12-13 12:12:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-07-02 11:51:23 +00:00
|
|
|
else if (event_type == QEvent::Wheel)
|
|
|
|
{
|
|
|
|
const QPoint delta_angle(static_cast<QWheelEvent*>(event)->angleDelta());
|
|
|
|
const float dx = std::clamp(static_cast<float>(delta_angle.x()) / QtUtils::MOUSE_WHEEL_DELTA, -1.0f, 1.0f);
|
|
|
|
if (dx != 0.0f)
|
|
|
|
{
|
|
|
|
InputBindingKey key(InputManager::MakePointerAxisKey(0, InputPointerAxis::WheelX));
|
2022-09-14 22:06:23 +00:00
|
|
|
key.modifier = dx < 0.0f ? InputModifier::Negate : InputModifier::None;
|
2022-07-02 11:51:23 +00:00
|
|
|
m_new_bindings.push_back(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
const float dy = std::clamp(static_cast<float>(delta_angle.y()) / QtUtils::MOUSE_WHEEL_DELTA, -1.0f, 1.0f);
|
|
|
|
if (dy != 0.0f)
|
|
|
|
{
|
|
|
|
InputBindingKey key(InputManager::MakePointerAxisKey(0, InputPointerAxis::WheelY));
|
2022-09-14 22:06:23 +00:00
|
|
|
key.modifier = dy < 0.0f ? InputModifier::Negate : InputModifier::None;
|
2022-07-02 11:51:23 +00:00
|
|
|
m_new_bindings.push_back(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dx != 0.0f || dy != 0.0f)
|
|
|
|
{
|
|
|
|
addNewBinding();
|
|
|
|
stopListeningForInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2022-06-29 10:08:30 +00:00
|
|
|
else if (event_type == QEvent::MouseMove && m_mouse_mapping_enabled)
|
2021-12-13 12:12:54 +00:00
|
|
|
{
|
2022-05-15 08:15:27 +00:00
|
|
|
// if we've moved more than a decent distance from the center of the widget, bind it.
|
|
|
|
// this is so we don't accidentally bind to the mouse if you bump it while reaching for your pad.
|
|
|
|
static constexpr const s32 THRESHOLD = 50;
|
|
|
|
const QPoint diff(static_cast<QMouseEvent*>(event)->globalPos() - m_input_listen_start_position);
|
|
|
|
bool has_one = false;
|
|
|
|
|
|
|
|
if (std::abs(diff.x()) >= THRESHOLD)
|
|
|
|
{
|
|
|
|
InputBindingKey key(InputManager::MakePointerAxisKey(0, InputPointerAxis::X));
|
2022-09-14 22:06:23 +00:00
|
|
|
key.modifier = diff.x() < 0 ? InputModifier::Negate : InputModifier::None;
|
2022-05-15 08:15:27 +00:00
|
|
|
m_new_bindings.push_back(key);
|
|
|
|
has_one = true;
|
|
|
|
}
|
|
|
|
if (std::abs(diff.y()) >= THRESHOLD)
|
|
|
|
{
|
|
|
|
InputBindingKey key(InputManager::MakePointerAxisKey(0, InputPointerAxis::Y));
|
2022-09-14 22:06:23 +00:00
|
|
|
key.modifier = diff.y() < 0 ? InputModifier::Negate : InputModifier::None;
|
2022-05-15 08:15:27 +00:00
|
|
|
m_new_bindings.push_back(key);
|
|
|
|
has_one = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_one)
|
|
|
|
{
|
|
|
|
addNewBinding();
|
|
|
|
stopListeningForInput();
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-13 12:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputBindingDialog::onInputListenTimerTimeout()
|
|
|
|
{
|
|
|
|
m_input_listen_remaining_seconds--;
|
|
|
|
if (m_input_listen_remaining_seconds == 0)
|
|
|
|
{
|
|
|
|
stopListeningForInput();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_ui.status->setText(tr("Push Button/Axis... [%1]").arg(m_input_listen_remaining_seconds));
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputBindingDialog::startListeningForInput(u32 timeout_in_seconds)
|
|
|
|
{
|
2022-12-17 12:10:33 +00:00
|
|
|
m_value_ranges.clear();
|
2022-05-24 15:17:52 +00:00
|
|
|
m_new_bindings.clear();
|
2022-06-29 10:08:30 +00:00
|
|
|
m_mouse_mapping_enabled = InputBindingWidget::isMouseMappingEnabled();
|
2022-05-15 08:15:27 +00:00
|
|
|
m_input_listen_start_position = QCursor::pos();
|
2021-12-13 12:12:54 +00:00
|
|
|
m_input_listen_timer = new QTimer(this);
|
|
|
|
m_input_listen_timer->setSingleShot(false);
|
|
|
|
m_input_listen_timer->start(1000);
|
|
|
|
|
2022-12-04 15:00:06 +00:00
|
|
|
m_input_listen_timer->connect(m_input_listen_timer, &QTimer::timeout, this, &InputBindingDialog::onInputListenTimerTimeout);
|
2021-12-13 12:12:54 +00:00
|
|
|
m_input_listen_remaining_seconds = timeout_in_seconds;
|
|
|
|
m_ui.status->setText(tr("Push Button/Axis... [%1]").arg(m_input_listen_remaining_seconds));
|
|
|
|
m_ui.addBinding->setEnabled(false);
|
|
|
|
m_ui.removeBinding->setEnabled(false);
|
|
|
|
m_ui.clearBindings->setEnabled(false);
|
|
|
|
m_ui.buttonBox->setEnabled(false);
|
|
|
|
|
|
|
|
installEventFilter(this);
|
|
|
|
grabKeyboard();
|
|
|
|
grabMouse();
|
2022-05-15 08:15:27 +00:00
|
|
|
setMouseTracking(true);
|
2022-05-24 15:17:52 +00:00
|
|
|
hookInputManager();
|
2021-12-13 12:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputBindingDialog::stopListeningForInput()
|
|
|
|
{
|
|
|
|
m_ui.status->clear();
|
|
|
|
m_ui.addBinding->setEnabled(true);
|
|
|
|
m_ui.removeBinding->setEnabled(true);
|
|
|
|
m_ui.clearBindings->setEnabled(true);
|
|
|
|
m_ui.buttonBox->setEnabled(true);
|
|
|
|
|
|
|
|
delete m_input_listen_timer;
|
|
|
|
m_input_listen_timer = nullptr;
|
|
|
|
|
2022-05-24 15:17:52 +00:00
|
|
|
unhookInputManager();
|
2021-12-13 12:12:54 +00:00
|
|
|
releaseMouse();
|
|
|
|
releaseKeyboard();
|
2022-05-15 08:15:27 +00:00
|
|
|
setMouseTracking(false);
|
2021-12-13 12:12:54 +00:00
|
|
|
removeEventFilter(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputBindingDialog::addNewBinding()
|
|
|
|
{
|
|
|
|
if (m_new_bindings.empty())
|
|
|
|
return;
|
|
|
|
|
2022-12-04 15:00:06 +00:00
|
|
|
const std::string new_binding(InputManager::ConvertInputBindingKeysToString(m_bind_type, m_new_bindings.data(), m_new_bindings.size()));
|
2021-12-13 12:12:54 +00:00
|
|
|
if (!new_binding.empty())
|
|
|
|
{
|
|
|
|
if (std::find(m_bindings.begin(), m_bindings.end(), new_binding) != m_bindings.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_ui.bindingList->addItem(QString::fromStdString(new_binding));
|
|
|
|
m_bindings.push_back(std::move(new_binding));
|
|
|
|
saveListToSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputBindingDialog::onAddBindingButtonClicked()
|
|
|
|
{
|
|
|
|
if (isListeningForInput())
|
|
|
|
stopListeningForInput();
|
|
|
|
|
|
|
|
startListeningForInput(TIMEOUT_FOR_BINDING);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputBindingDialog::onRemoveBindingButtonClicked()
|
|
|
|
{
|
|
|
|
const int row = m_ui.bindingList->currentRow();
|
|
|
|
if (row < 0 || static_cast<size_t>(row) >= m_bindings.size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_bindings.erase(m_bindings.begin() + row);
|
|
|
|
delete m_ui.bindingList->takeItem(row);
|
|
|
|
saveListToSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputBindingDialog::onClearBindingsButtonClicked()
|
|
|
|
{
|
|
|
|
m_bindings.clear();
|
|
|
|
m_ui.bindingList->clear();
|
|
|
|
saveListToSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputBindingDialog::updateList()
|
|
|
|
{
|
|
|
|
m_ui.bindingList->clear();
|
|
|
|
for (const std::string& binding : m_bindings)
|
|
|
|
m_ui.bindingList->addItem(QString::fromStdString(binding));
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputBindingDialog::saveListToSettings()
|
|
|
|
{
|
2022-06-08 12:15:10 +00:00
|
|
|
if (m_sif)
|
|
|
|
{
|
|
|
|
if (!m_bindings.empty())
|
|
|
|
m_sif->SetStringList(m_section_name.c_str(), m_key_name.c_str(), m_bindings);
|
|
|
|
else
|
|
|
|
m_sif->DeleteValue(m_section_name.c_str(), m_key_name.c_str());
|
|
|
|
m_sif->Save();
|
|
|
|
g_emu_thread->reloadGameSettings();
|
|
|
|
}
|
2021-12-13 12:12:54 +00:00
|
|
|
else
|
2022-06-08 12:15:10 +00:00
|
|
|
{
|
|
|
|
if (!m_bindings.empty())
|
2022-09-07 07:44:10 +00:00
|
|
|
Host::SetBaseStringListSettingValue(m_section_name.c_str(), m_key_name.c_str(), m_bindings);
|
2022-06-08 12:15:10 +00:00
|
|
|
else
|
2022-09-07 07:44:10 +00:00
|
|
|
Host::RemoveBaseSettingValue(m_section_name.c_str(), m_key_name.c_str());
|
|
|
|
Host::CommitBaseSettingChanges();
|
2022-06-08 12:15:10 +00:00
|
|
|
g_emu_thread->reloadInputBindings();
|
|
|
|
}
|
2021-12-13 12:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputBindingDialog::inputManagerHookCallback(InputBindingKey key, float value)
|
|
|
|
{
|
2022-12-17 12:10:33 +00:00
|
|
|
if (!isListeningForInput())
|
|
|
|
return;
|
|
|
|
|
|
|
|
float initial_value = value;
|
|
|
|
float min_value = value;
|
|
|
|
auto it = std::find_if(m_value_ranges.begin(), m_value_ranges.end(), [key](const auto& it) { return it.first.bits == key.bits; });
|
|
|
|
if (it != m_value_ranges.end())
|
|
|
|
{
|
|
|
|
initial_value = it->second.first;
|
|
|
|
min_value = it->second.second = std::min(it->second.second, value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_value_ranges.emplace_back(key, std::make_pair(initial_value, min_value));
|
|
|
|
}
|
|
|
|
|
2022-01-31 12:31:55 +00:00
|
|
|
const float abs_value = std::abs(value);
|
2022-12-17 12:10:33 +00:00
|
|
|
const bool reverse_threshold = (key.source_subtype == InputSubclass::ControllerAxis && initial_value > 0.5f);
|
2022-01-31 12:31:55 +00:00
|
|
|
|
2022-12-17 12:10:33 +00:00
|
|
|
for (InputBindingKey& other_key : m_new_bindings)
|
2021-12-13 12:12:54 +00:00
|
|
|
{
|
|
|
|
if (other_key.MaskDirection() == key.MaskDirection())
|
|
|
|
{
|
2022-12-17 12:10:33 +00:00
|
|
|
// for pedals, we wait for it to go back to near its starting point to commit the binding
|
|
|
|
if ((reverse_threshold ? ((initial_value - value) <= 0.25f) : (abs_value < 0.5f)))
|
2021-12-13 12:12:54 +00:00
|
|
|
{
|
2022-12-17 12:10:33 +00:00
|
|
|
// did we go the full range?
|
|
|
|
if (reverse_threshold && initial_value > 0.5f && min_value <= -0.5f)
|
|
|
|
other_key.modifier = InputModifier::FullAxis;
|
|
|
|
|
2021-12-13 12:12:54 +00:00
|
|
|
// if this key is in our new binding list, it's a "release", and we're done
|
|
|
|
addNewBinding();
|
|
|
|
stopListeningForInput();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, keep waiting
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// new binding, add it to the list, but wait for a decent distance first, and then wait for release
|
2022-12-17 12:10:33 +00:00
|
|
|
if ((reverse_threshold ? (abs_value < 0.5f) : (abs_value >= 0.5f)))
|
2022-01-31 12:31:55 +00:00
|
|
|
{
|
|
|
|
InputBindingKey key_to_add = key;
|
2022-12-17 12:10:33 +00:00
|
|
|
key_to_add.modifier = (value < 0.0f && !reverse_threshold) ? InputModifier::Negate : InputModifier::None;
|
|
|
|
key_to_add.invert = reverse_threshold;
|
2022-05-24 15:12:56 +00:00
|
|
|
m_new_bindings.push_back(key_to_add);
|
2022-01-31 12:31:55 +00:00
|
|
|
}
|
2021-12-13 12:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputBindingDialog::hookInputManager()
|
|
|
|
{
|
|
|
|
InputManager::SetHook([this](InputBindingKey key, float value) {
|
2022-12-04 15:00:06 +00:00
|
|
|
QMetaObject::invokeMethod(this, "inputManagerHookCallback", Qt::QueuedConnection, Q_ARG(InputBindingKey, key), Q_ARG(float, value));
|
2021-12-13 12:12:54 +00:00
|
|
|
return InputInterceptHook::CallbackResult::StopProcessingEvent;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputBindingDialog::unhookInputManager()
|
|
|
|
{
|
|
|
|
InputManager::RemoveHook();
|
|
|
|
}
|