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"
|
2017-06-13 04:51:38 +01:00
|
|
|
#include "bindingmodel.h"
|
|
|
|
#include "ControllerInfo.h"
|
|
|
|
#include "inputeventselectiondialog.h"
|
2020-02-25 18:47:50 +00:00
|
|
|
#include "QStringUtils.h"
|
|
|
|
|
|
|
|
#include "filesystem_def.h"
|
|
|
|
#include <iostream>
|
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
|
|
|
std::string profile = CAppConfig::GetInstance().GetPreferenceString(PREF_INPUT_PAD1_PROFILE);
|
|
|
|
|
2018-11-19 13:20:46 -05:00
|
|
|
PrepareBindingsView();
|
2020-02-25 18:47:50 +00:00
|
|
|
|
|
|
|
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);
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-19 13:20:46 -05:00
|
|
|
void ControllerConfigDialog::PrepareBindingsView()
|
2018-10-23 01:41:02 +01:00
|
|
|
{
|
2018-04-30 21:01:23 +01:00
|
|
|
CBindingModel* model = new CBindingModel(this);
|
2017-06-13 04:51:38 +01:00
|
|
|
model->Setup(m_inputManager);
|
|
|
|
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);
|
|
|
|
ui->tableView->setModel(model);
|
|
|
|
ui->tableView->horizontalHeader()->setStretchLastSection(true);
|
|
|
|
ui->tableView->resizeColumnsToContents();
|
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
|
|
|
{
|
2018-04-30 21:01:23 +01:00
|
|
|
switch(ui->tabWidget->currentIndex())
|
2017-06-13 04:51:38 +01:00
|
|
|
{
|
|
|
|
case 0:
|
2018-11-19 19:11:39 -05:00
|
|
|
AutoConfigureKeyboard(m_inputManager);
|
2017-06-13 04:51:38 +01:00
|
|
|
static_cast<CBindingModel*>(ui->tableView->model())->Refresh();
|
2018-04-30 21:01:23 +01:00
|
|
|
break;
|
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
|
|
|
}
|
|
|
|
|
2018-04-30 21:01:23 +01:00
|
|
|
void ControllerConfigDialog::on_tableView_doubleClicked(const QModelIndex& index)
|
2017-05-23 16:08:53 +01:00
|
|
|
{
|
2018-01-10 20:31:37 +00:00
|
|
|
OpenBindConfigDialog(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
|
|
|
{
|
2017-06-13 04:51:38 +01:00
|
|
|
for(int i = 0; i < PS2::CControllerInfo::MAX_BUTTONS; ++i)
|
|
|
|
{
|
2018-01-10 20:31:37 +00:00
|
|
|
if(!OpenBindConfigDialog(i))
|
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
|
|
|
|
2018-11-19 19:11:39 -05:00
|
|
|
void ControllerConfigDialog::AutoConfigureKeyboard(CInputBindingManager* bindingManager)
|
|
|
|
{
|
2018-11-20 12:37:18 -05:00
|
|
|
bindingManager->SetSimpleBinding(0, PS2::CControllerInfo::START, CInputProviderQtKey::MakeBindingTarget(Qt::Key_Return));
|
|
|
|
bindingManager->SetSimpleBinding(0, PS2::CControllerInfo::SELECT, CInputProviderQtKey::MakeBindingTarget(Qt::Key_Backspace));
|
|
|
|
bindingManager->SetSimpleBinding(0, PS2::CControllerInfo::DPAD_LEFT, CInputProviderQtKey::MakeBindingTarget(Qt::Key_Left));
|
|
|
|
bindingManager->SetSimpleBinding(0, PS2::CControllerInfo::DPAD_RIGHT, CInputProviderQtKey::MakeBindingTarget(Qt::Key_Right));
|
|
|
|
bindingManager->SetSimpleBinding(0, PS2::CControllerInfo::DPAD_UP, CInputProviderQtKey::MakeBindingTarget(Qt::Key_Up));
|
|
|
|
bindingManager->SetSimpleBinding(0, PS2::CControllerInfo::DPAD_DOWN, CInputProviderQtKey::MakeBindingTarget(Qt::Key_Down));
|
|
|
|
bindingManager->SetSimpleBinding(0, PS2::CControllerInfo::SQUARE, CInputProviderQtKey::MakeBindingTarget(Qt::Key_A));
|
|
|
|
bindingManager->SetSimpleBinding(0, PS2::CControllerInfo::CROSS, CInputProviderQtKey::MakeBindingTarget(Qt::Key_Z));
|
|
|
|
bindingManager->SetSimpleBinding(0, PS2::CControllerInfo::TRIANGLE, CInputProviderQtKey::MakeBindingTarget(Qt::Key_S));
|
|
|
|
bindingManager->SetSimpleBinding(0, PS2::CControllerInfo::CIRCLE, CInputProviderQtKey::MakeBindingTarget(Qt::Key_X));
|
|
|
|
bindingManager->SetSimpleBinding(0, PS2::CControllerInfo::L1, CInputProviderQtKey::MakeBindingTarget(Qt::Key_1));
|
|
|
|
bindingManager->SetSimpleBinding(0, PS2::CControllerInfo::L2, CInputProviderQtKey::MakeBindingTarget(Qt::Key_2));
|
|
|
|
bindingManager->SetSimpleBinding(0, PS2::CControllerInfo::L3, CInputProviderQtKey::MakeBindingTarget(Qt::Key_3));
|
|
|
|
bindingManager->SetSimpleBinding(0, PS2::CControllerInfo::R1, CInputProviderQtKey::MakeBindingTarget(Qt::Key_8));
|
|
|
|
bindingManager->SetSimpleBinding(0, PS2::CControllerInfo::R2, CInputProviderQtKey::MakeBindingTarget(Qt::Key_9));
|
|
|
|
bindingManager->SetSimpleBinding(0, PS2::CControllerInfo::R3, CInputProviderQtKey::MakeBindingTarget(Qt::Key_0));
|
2018-11-28 22:32:27 -05:00
|
|
|
|
2018-11-20 12:37:18 -05:00
|
|
|
bindingManager->SetSimulatedAxisBinding(0, PS2::CControllerInfo::ANALOG_LEFT_X,
|
2018-11-28 22:32:27 -05:00
|
|
|
CInputProviderQtKey::MakeBindingTarget(Qt::Key_F),
|
|
|
|
CInputProviderQtKey::MakeBindingTarget(Qt::Key_H));
|
2018-11-20 12:37:18 -05:00
|
|
|
bindingManager->SetSimulatedAxisBinding(0, PS2::CControllerInfo::ANALOG_LEFT_Y,
|
2018-11-28 22:32:27 -05:00
|
|
|
CInputProviderQtKey::MakeBindingTarget(Qt::Key_T),
|
|
|
|
CInputProviderQtKey::MakeBindingTarget(Qt::Key_G));
|
|
|
|
|
2018-11-20 12:37:18 -05:00
|
|
|
bindingManager->SetSimulatedAxisBinding(0, PS2::CControllerInfo::ANALOG_RIGHT_X,
|
2018-11-28 22:32:27 -05:00
|
|
|
CInputProviderQtKey::MakeBindingTarget(Qt::Key_J),
|
|
|
|
CInputProviderQtKey::MakeBindingTarget(Qt::Key_L));
|
2018-11-20 12:37:18 -05:00
|
|
|
bindingManager->SetSimulatedAxisBinding(0, 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
|
|
|
}
|
|
|
|
|
2018-01-10 20:31:37 +00:00
|
|
|
int ControllerConfigDialog::OpenBindConfigDialog(int index)
|
|
|
|
{
|
|
|
|
std::string button(PS2::CControllerInfo::m_buttonName[index]);
|
2018-04-30 21:01:23 +01:00
|
|
|
std::transform(button.begin(), button.end(), button.begin(), ::toupper);
|
2018-01-10 20:31:37 +00:00
|
|
|
|
2018-11-19 13:20:46 -05:00
|
|
|
InputEventSelectionDialog IESD(this);
|
|
|
|
IESD.Setup(button.c_str(), m_inputManager, m_qtKeyInputProvider, static_cast<PS2::CControllerInfo::BUTTON>(index));
|
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)
|
|
|
|
{
|
|
|
|
ui->delProfileButton->setEnabled(index > 0);
|
|
|
|
|
|
|
|
auto profile = ui->comboBox->itemText(index).toStdString();
|
|
|
|
m_inputManager->Load(profile.c_str());
|
|
|
|
CAppConfig::GetInstance().SetPreferenceString(PREF_INPUT_PAD1_PROFILE, profile.c_str());
|
|
|
|
|
|
|
|
static_cast<CBindingModel*>(ui->tableView->model())->Refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
fs::remove(profile_path);
|
|
|
|
int index = ui->comboBox->currentIndex();
|
|
|
|
ui->comboBox->removeItem(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|