2016-09-06 00:23:31 +01:00
|
|
|
#include "controllerconfigdialog.h"
|
|
|
|
#include "ui_controllerconfigdialog.h"
|
|
|
|
|
|
|
|
#include <QRegularExpression>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QFile>
|
2017-05-23 16:08:53 +01:00
|
|
|
#include <QAbstractButton>
|
|
|
|
#include <QPushButton>
|
2020-02-25 18:47:50 +00:00
|
|
|
#include <QInputDialog>
|
2017-05-23 16:08:53 +01:00
|
|
|
|
2020-02-25 18:47:50 +00:00
|
|
|
#include "AppConfig.h"
|
|
|
|
#include "PreferenceDefs.h"
|
|
|
|
#include "PathUtils.h"
|
2020-09-16 15:09:14 -04:00
|
|
|
#include "inputbindingmodel.h"
|
2017-06-13 04:51:38 +01:00
|
|
|
#include "ControllerInfo.h"
|
|
|
|
#include "inputeventselectiondialog.h"
|
2020-02-25 18:47:50 +00:00
|
|
|
#include "QStringUtils.h"
|
|
|
|
|
|
|
|
#include "filesystem_def.h"
|
|
|
|
#include <iostream>
|
2020-09-16 16:31:45 -04:00
|
|
|
#include <cassert>
|
2016-09-06 00:23:31 +01:00
|
|
|
|
2018-11-19 13:20:46 -05:00
|
|
|
ControllerConfigDialog::ControllerConfigDialog(CInputBindingManager* inputBindingManager, CInputProviderQtKey* qtKeyInputProvider, QWidget* parent)
|
2018-04-30 21:01:23 +01:00
|
|
|
: QDialog(parent)
|
|
|
|
, ui(new Ui::ControllerConfigDialog)
|
2018-11-19 13:20:46 -05:00
|
|
|
, m_inputManager(inputBindingManager)
|
|
|
|
, m_qtKeyInputProvider(qtKeyInputProvider)
|
2016-09-06 00:23:31 +01:00
|
|
|
{
|
2017-06-13 04:51:38 +01:00
|
|
|
ui->setupUi(this);
|
2020-02-25 18:47:50 +00:00
|
|
|
|
2020-09-14 19:43:25 -04:00
|
|
|
m_bindingsViews.push_back(ui->pad1TableView);
|
|
|
|
m_bindingsViews.push_back(ui->pad2TableView);
|
2020-02-25 18:47:50 +00:00
|
|
|
|
2020-09-14 19:43:25 -04:00
|
|
|
for(uint32 padIndex = 0; padIndex < m_bindingsViews.size(); padIndex++)
|
2020-02-25 18:47:50 +00:00
|
|
|
{
|
2020-09-14 19:43:25 -04:00
|
|
|
PrepareBindingsView(padIndex);
|
|
|
|
QObject::connect(m_bindingsViews[padIndex], SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(bindingsViewDoubleClicked(const QModelIndex&)));
|
2020-02-25 18:47:50 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 19:43:25 -04:00
|
|
|
PrepareProfiles();
|
2017-06-13 04:53:18 +01:00
|
|
|
}
|
2016-09-06 00:23:31 +01:00
|
|
|
|
2017-06-13 04:53:18 +01:00
|
|
|
ControllerConfigDialog::~ControllerConfigDialog()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2020-09-14 19:43:25 -04:00
|
|
|
void ControllerConfigDialog::PrepareBindingsView(uint32 padIndex)
|
2018-10-23 01:41:02 +01:00
|
|
|
{
|
2020-09-16 15:09:14 -04:00
|
|
|
CInputBindingModel* model = new CInputBindingModel(this, m_inputManager, padIndex);
|
2017-06-13 04:51:38 +01:00
|
|
|
model->setHeaderData(0, Qt::Orientation::Horizontal, QVariant("Button"), Qt::DisplayRole);
|
|
|
|
model->setHeaderData(1, Qt::Orientation::Horizontal, QVariant("Binding Type"), Qt::DisplayRole);
|
|
|
|
model->setHeaderData(2, Qt::Orientation::Horizontal, QVariant("Binding Value"), Qt::DisplayRole);
|
2020-09-14 19:43:25 -04:00
|
|
|
assert(padIndex < m_bindingsViews.size());
|
|
|
|
auto bindingsView = m_bindingsViews[padIndex];
|
|
|
|
bindingsView->setModel(model);
|
|
|
|
bindingsView->horizontalHeader()->setStretchLastSection(true);
|
|
|
|
bindingsView->resizeColumnsToContents();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControllerConfigDialog::PrepareProfiles()
|
|
|
|
{
|
|
|
|
std::string profile = CAppConfig::GetInstance().GetPreferenceString(PREF_INPUT_PAD1_PROFILE);
|
|
|
|
|
|
|
|
auto path = CInputConfig::GetProfilePath();
|
|
|
|
if(fs::is_directory(path))
|
|
|
|
{
|
|
|
|
for(auto& entry : fs::directory_iterator(path))
|
|
|
|
{
|
|
|
|
auto profile = Framework::PathUtils::GetNativeStringFromPath(entry.path().stem());
|
|
|
|
ui->comboBox->addItem(profile.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto index = ui->comboBox->findText(profile.c_str());
|
|
|
|
if(index >= 0)
|
|
|
|
ui->comboBox->setCurrentIndex(index);
|
2016-09-06 00:23:31 +01:00
|
|
|
}
|
|
|
|
|
2018-04-30 21:01:23 +01:00
|
|
|
void ControllerConfigDialog::on_buttonBox_clicked(QAbstractButton* button)
|
2017-06-13 04:51:38 +01:00
|
|
|
{
|
|
|
|
if(button == ui->buttonBox->button(QDialogButtonBox::Ok))
|
|
|
|
{
|
|
|
|
m_inputManager->Save();
|
2020-02-25 18:47:50 +00:00
|
|
|
CAppConfig::GetInstance().Save();
|
2017-06-13 04:51:38 +01:00
|
|
|
}
|
2018-04-30 21:01:23 +01:00
|
|
|
else if(button == ui->buttonBox->button(QDialogButtonBox::Apply))
|
2017-06-13 04:51:38 +01:00
|
|
|
{
|
|
|
|
m_inputManager->Save();
|
2020-02-25 18:47:50 +00:00
|
|
|
CAppConfig::GetInstance().Save();
|
2017-06-13 04:51:38 +01:00
|
|
|
}
|
2018-04-30 21:01:23 +01:00
|
|
|
else if(button == ui->buttonBox->button(QDialogButtonBox::RestoreDefaults))
|
2017-06-13 04:51:38 +01:00
|
|
|
{
|
2020-09-14 19:43:25 -04:00
|
|
|
uint32 padIndex = ui->tabWidget->currentIndex();
|
|
|
|
assert(padIndex < CInputBindingManager::MAX_PADS);
|
|
|
|
AutoConfigureKeyboard(padIndex, m_inputManager);
|
|
|
|
for(auto& bindingsView : m_bindingsViews)
|
2017-06-13 04:51:38 +01:00
|
|
|
{
|
2020-09-16 15:09:14 -04:00
|
|
|
static_cast<CInputBindingModel*>(bindingsView->model())->Refresh();
|
2017-06-13 04:51:38 +01:00
|
|
|
}
|
|
|
|
}
|
2018-04-30 21:01:23 +01:00
|
|
|
else if(button == ui->buttonBox->button(QDialogButtonBox::Cancel))
|
2017-06-13 04:53:18 +01:00
|
|
|
{
|
2020-02-25 18:47:50 +00:00
|
|
|
m_inputManager->Reload();
|
2017-06-13 04:53:18 +01:00
|
|
|
}
|
2016-09-06 00:23:31 +01:00
|
|
|
}
|
|
|
|
|
2020-09-14 19:43:25 -04:00
|
|
|
void ControllerConfigDialog::bindingsViewDoubleClicked(const QModelIndex& index)
|
2017-05-23 16:08:53 +01:00
|
|
|
{
|
2020-09-21 16:13:22 -04:00
|
|
|
uint32 padIndex = ui->tabWidget->currentIndex();
|
|
|
|
assert(padIndex < CInputBindingManager::MAX_PADS);
|
2020-09-14 19:43:25 -04:00
|
|
|
OpenBindConfigDialog(padIndex, index.row());
|
2017-05-23 16:08:53 +01:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:51:38 +01:00
|
|
|
void ControllerConfigDialog::on_ConfigAllButton_clicked()
|
2016-09-06 00:23:31 +01:00
|
|
|
{
|
2020-09-21 16:13:22 -04:00
|
|
|
uint32 padIndex = ui->tabWidget->currentIndex();
|
|
|
|
assert(padIndex < CInputBindingManager::MAX_PADS);
|
2020-09-14 19:43:25 -04:00
|
|
|
for(uint32 buttonIndex = 0; buttonIndex < PS2::CControllerInfo::MAX_BUTTONS; ++buttonIndex)
|
2017-06-13 04:51:38 +01:00
|
|
|
{
|
2020-09-14 19:43:25 -04:00
|
|
|
if(!OpenBindConfigDialog(padIndex, buttonIndex))
|
2017-06-13 04:51:38 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-09-06 00:23:31 +01:00
|
|
|
}
|
2018-01-10 20:31:37 +00:00
|
|
|
|
2020-09-14 19:43:25 -04:00
|
|
|
void ControllerConfigDialog::AutoConfigureKeyboard(uint32 padIndex, CInputBindingManager* bindingManager)
|
2018-11-19 19:11:39 -05:00
|
|
|
{
|
2020-09-14 19:43:25 -04:00
|
|
|
bindingManager->SetSimpleBinding(padIndex, PS2::CControllerInfo::START, CInputProviderQtKey::MakeBindingTarget(Qt::Key_Return));
|
|
|
|
bindingManager->SetSimpleBinding(padIndex, PS2::CControllerInfo::SELECT, CInputProviderQtKey::MakeBindingTarget(Qt::Key_Backspace));
|
|
|
|
bindingManager->SetSimpleBinding(padIndex, PS2::CControllerInfo::DPAD_LEFT, CInputProviderQtKey::MakeBindingTarget(Qt::Key_Left));
|
|
|
|
bindingManager->SetSimpleBinding(padIndex, PS2::CControllerInfo::DPAD_RIGHT, CInputProviderQtKey::MakeBindingTarget(Qt::Key_Right));
|
|
|
|
bindingManager->SetSimpleBinding(padIndex, PS2::CControllerInfo::DPAD_UP, CInputProviderQtKey::MakeBindingTarget(Qt::Key_Up));
|
|
|
|
bindingManager->SetSimpleBinding(padIndex, PS2::CControllerInfo::DPAD_DOWN, CInputProviderQtKey::MakeBindingTarget(Qt::Key_Down));
|
|
|
|
bindingManager->SetSimpleBinding(padIndex, PS2::CControllerInfo::SQUARE, CInputProviderQtKey::MakeBindingTarget(Qt::Key_A));
|
|
|
|
bindingManager->SetSimpleBinding(padIndex, PS2::CControllerInfo::CROSS, CInputProviderQtKey::MakeBindingTarget(Qt::Key_Z));
|
|
|
|
bindingManager->SetSimpleBinding(padIndex, PS2::CControllerInfo::TRIANGLE, CInputProviderQtKey::MakeBindingTarget(Qt::Key_S));
|
|
|
|
bindingManager->SetSimpleBinding(padIndex, PS2::CControllerInfo::CIRCLE, CInputProviderQtKey::MakeBindingTarget(Qt::Key_X));
|
|
|
|
bindingManager->SetSimpleBinding(padIndex, PS2::CControllerInfo::L1, CInputProviderQtKey::MakeBindingTarget(Qt::Key_1));
|
|
|
|
bindingManager->SetSimpleBinding(padIndex, PS2::CControllerInfo::L2, CInputProviderQtKey::MakeBindingTarget(Qt::Key_2));
|
|
|
|
bindingManager->SetSimpleBinding(padIndex, PS2::CControllerInfo::L3, CInputProviderQtKey::MakeBindingTarget(Qt::Key_3));
|
|
|
|
bindingManager->SetSimpleBinding(padIndex, PS2::CControllerInfo::R1, CInputProviderQtKey::MakeBindingTarget(Qt::Key_8));
|
|
|
|
bindingManager->SetSimpleBinding(padIndex, PS2::CControllerInfo::R2, CInputProviderQtKey::MakeBindingTarget(Qt::Key_9));
|
|
|
|
bindingManager->SetSimpleBinding(padIndex, PS2::CControllerInfo::R3, CInputProviderQtKey::MakeBindingTarget(Qt::Key_0));
|
|
|
|
|
|
|
|
bindingManager->SetSimulatedAxisBinding(padIndex, PS2::CControllerInfo::ANALOG_LEFT_X,
|
2018-11-28 22:32:27 -05:00
|
|
|
CInputProviderQtKey::MakeBindingTarget(Qt::Key_F),
|
|
|
|
CInputProviderQtKey::MakeBindingTarget(Qt::Key_H));
|
2020-09-14 19:43:25 -04:00
|
|
|
bindingManager->SetSimulatedAxisBinding(padIndex, PS2::CControllerInfo::ANALOG_LEFT_Y,
|
2018-11-28 22:32:27 -05:00
|
|
|
CInputProviderQtKey::MakeBindingTarget(Qt::Key_T),
|
|
|
|
CInputProviderQtKey::MakeBindingTarget(Qt::Key_G));
|
|
|
|
|
2020-09-14 19:43:25 -04:00
|
|
|
bindingManager->SetSimulatedAxisBinding(padIndex, PS2::CControllerInfo::ANALOG_RIGHT_X,
|
2018-11-28 22:32:27 -05:00
|
|
|
CInputProviderQtKey::MakeBindingTarget(Qt::Key_J),
|
|
|
|
CInputProviderQtKey::MakeBindingTarget(Qt::Key_L));
|
2020-09-14 19:43:25 -04:00
|
|
|
bindingManager->SetSimulatedAxisBinding(padIndex, PS2::CControllerInfo::ANALOG_RIGHT_Y,
|
2018-11-28 22:32:27 -05:00
|
|
|
CInputProviderQtKey::MakeBindingTarget(Qt::Key_I),
|
|
|
|
CInputProviderQtKey::MakeBindingTarget(Qt::Key_K));
|
2018-11-19 19:11:39 -05:00
|
|
|
}
|
|
|
|
|
2020-09-14 19:43:25 -04:00
|
|
|
int ControllerConfigDialog::OpenBindConfigDialog(uint32 padIndex, uint32 buttonIndex)
|
2018-01-10 20:31:37 +00:00
|
|
|
{
|
2020-09-14 19:43:25 -04:00
|
|
|
auto button = static_cast<PS2::CControllerInfo::BUTTON>(buttonIndex);
|
|
|
|
std::string buttonName(PS2::CControllerInfo::m_buttonName[button]);
|
|
|
|
std::transform(buttonName.begin(), buttonName.end(), buttonName.begin(), ::toupper);
|
2018-01-10 20:31:37 +00:00
|
|
|
|
2018-11-19 13:20:46 -05:00
|
|
|
InputEventSelectionDialog IESD(this);
|
2020-09-14 19:43:25 -04:00
|
|
|
IESD.Setup(buttonName.c_str(), m_inputManager, m_qtKeyInputProvider, padIndex, button);
|
2018-01-10 20:31:37 +00:00
|
|
|
auto res = IESD.exec();
|
|
|
|
return res;
|
|
|
|
}
|
2020-02-25 18:47:50 +00:00
|
|
|
|
|
|
|
void ControllerConfigDialog::on_comboBox_currentIndexChanged(int index)
|
|
|
|
{
|
2019-11-06 01:00:53 +00:00
|
|
|
ui->delProfileButton->setEnabled(ui->comboBox->count() > 1);
|
2020-02-25 18:47:50 +00:00
|
|
|
|
|
|
|
auto profile = ui->comboBox->itemText(index).toStdString();
|
2020-02-25 18:44:54 +00:00
|
|
|
m_inputManager->Save();
|
2020-02-25 18:47:50 +00:00
|
|
|
m_inputManager->Load(profile.c_str());
|
|
|
|
CAppConfig::GetInstance().SetPreferenceString(PREF_INPUT_PAD1_PROFILE, profile.c_str());
|
|
|
|
|
2020-09-14 19:43:25 -04:00
|
|
|
for(auto& bindingsView : m_bindingsViews)
|
|
|
|
{
|
2020-09-16 15:09:14 -04:00
|
|
|
static_cast<CInputBindingModel*>(bindingsView->model())->Refresh();
|
2020-09-14 19:43:25 -04:00
|
|
|
}
|
2020-02-25 18:47:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ControllerConfigDialog::on_addProfileButton_clicked()
|
|
|
|
{
|
|
|
|
std::string profile_name;
|
|
|
|
while(profile_name.empty())
|
|
|
|
{
|
|
|
|
bool ok_pressed = false;
|
|
|
|
QString name = QInputDialog::getText(this, tr("Enter Profile Name"), tr("Only letters, numbers and dash characters allowed.\nProfile name:"),
|
|
|
|
QLineEdit::Normal, "", &ok_pressed);
|
|
|
|
|
|
|
|
if(!ok_pressed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!name.isEmpty() && CInputConfig::IsValidProfileName(name.toStdString()))
|
|
|
|
profile_name = name.toStdString();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto profile_path = CInputConfig::GetProfile(profile_name);
|
|
|
|
if(!fs::exists(profile_path))
|
|
|
|
{
|
|
|
|
ui->comboBox->addItem(profile_name.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto index = ui->comboBox->findText(profile_name.c_str());
|
|
|
|
if(index >= 0)
|
|
|
|
ui->comboBox->setCurrentIndex(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControllerConfigDialog::on_delProfileButton_clicked()
|
|
|
|
{
|
|
|
|
auto name = ui->comboBox->currentText();
|
|
|
|
std::string profile_name = name.toStdString();
|
|
|
|
{
|
|
|
|
auto profile_path = CInputConfig::GetProfile(profile_name);
|
|
|
|
if(fs::exists(profile_path))
|
|
|
|
{
|
|
|
|
int index = ui->comboBox->currentIndex();
|
|
|
|
ui->comboBox->removeItem(index);
|
2019-11-06 00:50:39 +00:00
|
|
|
fs::remove(profile_path);
|
2020-02-25 18:47:50 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-14 19:43:25 -04:00
|
|
|
}
|