pcsx2/pcsx2-qt/Settings/ControllerBindingWidgets.cpp

300 lines
9.6 KiB
C++
Raw Normal View History

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"
2022-03-25 12:16:21 +00:00
#include <QtWidgets/QMenu>
2021-12-13 12:12:54 +00:00
#include <QtWidgets/QMessageBox>
#include <algorithm>
2022-06-08 12:15:10 +00:00
#include "Settings/ControllerBindingWidgets.h"
#include "Settings/ControllerSettingsDialog.h"
#include "Settings/ControllerSettingWidgetBinder.h"
#include "Settings/SettingsDialog.h"
2021-12-13 12:12:54 +00:00
#include "EmuThread.h"
#include "QtUtils.h"
#include "SettingWidgetBinder.h"
#include "common/StringUtil.h"
#include "pcsx2/HostSettings.h"
2021-12-13 12:12:54 +00:00
#include "pcsx2/PAD/Host/PAD.h"
ControllerBindingWidget::ControllerBindingWidget(QWidget* parent, ControllerSettingsDialog* dialog, u32 port)
: QWidget(parent)
, m_dialog(dialog)
, m_config_section(StringUtil::StdStringFromFormat("Pad%u", port + 1u))
, m_port_number(port)
{
m_ui.setupUi(this);
populateControllerTypes();
onTypeChanged();
2022-06-08 12:15:10 +00:00
ControllerSettingWidgetBinder::BindWidgetToInputProfileString(m_dialog->getProfileSettingsInterface(),
m_ui.controllerType, m_config_section, "Type", PAD::GetDefaultPadType(port));
2022-03-25 12:16:21 +00:00
connect(m_ui.controllerType, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ControllerBindingWidget::onTypeChanged);
connect(m_ui.automaticBinding, &QPushButton::clicked, this, &ControllerBindingWidget::doAutomaticBinding);
connect(m_ui.clearBindings, &QPushButton::clicked, this, &ControllerBindingWidget::doClearBindings);
2021-12-13 12:12:54 +00:00
}
2022-03-25 12:16:21 +00:00
ControllerBindingWidget::~ControllerBindingWidget() = default;
2021-12-13 12:12:54 +00:00
2022-06-08 12:15:10 +00:00
QIcon ControllerBindingWidget::getIcon() const
{
return m_current_widget->getIcon();
}
2021-12-13 12:12:54 +00:00
void ControllerBindingWidget::populateControllerTypes()
{
2022-05-29 13:56:03 +00:00
for (const auto& [name, display_name] : PAD::GetControllerTypeNames())
m_ui.controllerType->addItem(QString::fromStdString(display_name), QString::fromStdString(name));
2021-12-13 12:12:54 +00:00
}
void ControllerBindingWidget::onTypeChanged()
{
2022-06-08 12:15:10 +00:00
const bool is_initializing = (m_current_widget == nullptr);
m_controller_type = m_dialog->getStringValue(m_config_section.c_str(), "Type", PAD::GetDefaultPadType(m_port_number));
if (!is_initializing)
2021-12-13 12:12:54 +00:00
{
m_ui.verticalLayout->removeWidget(m_current_widget);
delete m_current_widget;
m_current_widget = nullptr;
}
const int index = m_ui.controllerType->findData(QString::fromStdString(m_controller_type));
if (index >= 0 && index != m_ui.controllerType->currentIndex())
{
QSignalBlocker sb(m_ui.controllerType);
m_ui.controllerType->setCurrentIndex(index);
}
if (m_controller_type == "DualShock2")
m_current_widget = ControllerBindingWidget_DualShock2::createInstance(this);
else
m_current_widget = new ControllerBindingWidget_Base(this);
m_ui.verticalLayout->addWidget(m_current_widget, 1);
2022-06-08 12:15:10 +00:00
// no need to do this on first init, only changes
if (!is_initializing)
m_dialog->updateListDescription(m_port_number, this);
2021-12-13 12:12:54 +00:00
}
2022-03-25 12:16:21 +00:00
void ControllerBindingWidget::doAutomaticBinding()
{
QMenu menu(this);
bool added = false;
for (const QPair<QString, QString>& dev : m_dialog->getDeviceList())
{
// we set it as data, because the device list could get invalidated while the menu is up
QAction* action = menu.addAction(QStringLiteral("%1 (%2)").arg(dev.first).arg(dev.second));
action->setData(dev.first);
connect(action, &QAction::triggered, this, [this, action]() {
doDeviceAutomaticBinding(action->data().toString());
});
added = true;
}
if (!added)
{
QAction* action = menu.addAction(tr("No devices available"));
action->setEnabled(false);
}
menu.exec(QCursor::pos());
}
void ControllerBindingWidget::doClearBindings()
{
if (QMessageBox::question(QtUtils::GetRootWidget(this), tr("Clear Bindings"),
2022-06-08 12:15:10 +00:00
tr("Are you sure you want to clear all bindings for this controller? This action cannot be undone.")) != QMessageBox::Yes)
{
return;
}
2022-06-08 12:15:10 +00:00
if (m_dialog->isEditingGlobalSettings())
{
auto lock = Host::GetSettingsLock();
PAD::ClearPortBindings(*Host::Internal::GetBaseSettingsLayer(), m_port_number);
}
2022-06-08 12:15:10 +00:00
else
{
PAD::ClearPortBindings(*m_dialog->getProfileSettingsInterface(), m_port_number);
}
saveAndRefresh();
}
2022-03-25 12:16:21 +00:00
void ControllerBindingWidget::doDeviceAutomaticBinding(const QString& device)
{
std::vector<std::pair<GenericInputBinding, std::string>> mapping = InputManager::GetGenericBindingMapping(device.toStdString());
if (mapping.empty())
{
QMessageBox::critical(QtUtils::GetRootWidget(this), tr("Automatic Binding"),
tr("No generic bindings were generated for device '%1'").arg(device));
return;
}
bool result;
2022-06-08 12:15:10 +00:00
if (m_dialog->isEditingGlobalSettings())
2022-03-25 12:16:21 +00:00
{
auto lock = Host::GetSettingsLock();
result = PAD::MapController(*Host::Internal::GetBaseSettingsLayer(), m_port_number, mapping);
2022-03-25 12:16:21 +00:00
}
2022-06-08 12:15:10 +00:00
else
{
result = PAD::MapController(*m_dialog->getProfileSettingsInterface(), m_port_number, mapping);
m_dialog->getProfileSettingsInterface()->Save();
g_emu_thread->reloadInputBindings();
}
2022-03-25 12:16:21 +00:00
// force a refresh after mapping
2022-03-25 12:16:21 +00:00
if (result)
saveAndRefresh();
}
void ControllerBindingWidget::saveAndRefresh()
{
onTypeChanged();
QtHost::QueueSettingsSave();
g_emu_thread->applySettings();
2022-03-25 12:16:21 +00:00
}
2021-12-13 12:12:54 +00:00
//////////////////////////////////////////////////////////////////////////
ControllerBindingWidget_Base::ControllerBindingWidget_Base(ControllerBindingWidget* parent)
: QWidget(parent)
{
}
ControllerBindingWidget_Base::~ControllerBindingWidget_Base()
{
}
2022-06-08 12:15:10 +00:00
QIcon ControllerBindingWidget_Base::getIcon() const
{
return QIcon::fromTheme("artboard-2-line");
}
2021-12-13 12:12:54 +00:00
void ControllerBindingWidget_Base::initBindingWidgets()
{
const std::string& type = getControllerType();
const std::string& config_section = getConfigSection();
std::vector<std::string> bindings(PAD::GetControllerBinds(type));
2022-06-08 12:15:10 +00:00
SettingsInterface* sif = getDialog()->getProfileSettingsInterface();
2021-12-13 12:12:54 +00:00
for (std::string& binding : bindings)
{
InputBindingWidget* widget = findChild<InputBindingWidget*>(QString::fromStdString(binding));
if (!widget)
{
Console.Error("(ControllerBindingWidget_Base) No widget found for '%s' (%.*s)",
2021-12-13 12:12:54 +00:00
binding.c_str(), static_cast<int>(type.size()), type.data());
continue;
}
2022-06-08 12:15:10 +00:00
widget->initialize(sif, config_section, std::move(binding));
2021-12-13 12:12:54 +00:00
}
const PAD::VibrationCapabilities vibe_caps = PAD::GetControllerVibrationCapabilities(type);
switch (vibe_caps)
{
case PAD::VibrationCapabilities::LargeSmallMotors:
{
InputVibrationBindingWidget* widget = findChild<InputVibrationBindingWidget*>(QStringLiteral("LargeMotor"));
if (widget)
widget->setKey(getDialog(), config_section, "LargeMotor");
widget = findChild<InputVibrationBindingWidget*>(QStringLiteral("SmallMotor"));
if (widget)
widget->setKey(getDialog(), config_section, "SmallMotor");
}
break;
case PAD::VibrationCapabilities::SingleMotor:
{
InputVibrationBindingWidget* widget = findChild<InputVibrationBindingWidget*>(QStringLiteral("Motor"));
if (widget)
widget->setKey(getDialog(), config_section, "Motor");
}
break;
case PAD::VibrationCapabilities::NoVibration:
default:
break;
}
2022-06-18 09:29:57 +00:00
if (QSlider* widget = findChild<QSlider*>(QStringLiteral("Deadzone")); widget)
{
const float range = static_cast<float>(widget->maximum());
QLabel* label = findChild<QLabel*>(QStringLiteral("DeadzoneLabel"));
if (label)
{
connect(widget, &QSlider::valueChanged, this, [range, label](int value) {
label->setText(tr("%1%").arg((static_cast<float>(value) / range) * 100.0f, 0, 'f', 0));
});
}
ControllerSettingWidgetBinder::BindWidgetToInputProfileNormalized(sif, widget, config_section, "Deadzone", range,
PAD::DEFAULT_STICK_DEADZONE);
}
if (QSlider* widget = findChild<QSlider*>(QStringLiteral("AxisScale")); widget)
{
// position 1.0f at the halfway point
const float range = static_cast<float>(widget->maximum()) * 0.5f;
QLabel* label = findChild<QLabel*>(QStringLiteral("AxisScaleLabel"));
if (label)
{
connect(widget, &QSlider::valueChanged, this, [range, label](int value) {
2022-06-18 09:29:57 +00:00
label->setText(tr("%1%").arg((static_cast<float>(value) / range) * 100.0f, 0, 'f', 0));
});
}
2022-06-18 09:29:57 +00:00
ControllerSettingWidgetBinder::BindWidgetToInputProfileNormalized(sif, widget, config_section, "AxisScale", range,
PAD::DEFAULT_STICK_SCALE);
}
if (QDoubleSpinBox* widget = findChild<QDoubleSpinBox*>(QStringLiteral("SmallMotorScale")); widget)
2022-06-08 12:15:10 +00:00
ControllerSettingWidgetBinder::BindWidgetToInputProfileFloat(sif, widget, config_section, "SmallMotorScale", 1.0f);
if (QDoubleSpinBox* widget = findChild<QDoubleSpinBox*>(QStringLiteral("LargeMotorScale")); widget)
2022-06-08 12:15:10 +00:00
ControllerSettingWidgetBinder::BindWidgetToInputProfileFloat(sif, widget, config_section, "LargeMotorScale", 1.0f);
2021-12-13 12:12:54 +00:00
}
ControllerBindingWidget_DualShock2::ControllerBindingWidget_DualShock2(ControllerBindingWidget* parent)
: ControllerBindingWidget_Base(parent)
{
m_ui.setupUi(this);
initBindingWidgets();
}
ControllerBindingWidget_DualShock2::~ControllerBindingWidget_DualShock2()
{
}
2022-06-08 12:15:10 +00:00
QIcon ControllerBindingWidget_DualShock2::getIcon() const
{
return QIcon::fromTheme("gamepad-line");
}
2021-12-13 12:12:54 +00:00
ControllerBindingWidget_Base* ControllerBindingWidget_DualShock2::createInstance(ControllerBindingWidget* parent)
{
return new ControllerBindingWidget_DualShock2(parent);
}
//////////////////////////////////////////////////////////////////////////