Merge pull request #658 from Thunder07/qt_gamepad_cleanup_master

Qt gamepad cleanup master
This commit is contained in:
Jean-Philip Desjardins 2018-01-15 11:26:04 -05:00 committed by GitHub
commit da902b8e2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 186 additions and 54 deletions

View File

@ -65,35 +65,29 @@ void ControllerConfigDialog::on_buttonBox_clicked(QAbstractButton *button)
void ControllerConfigDialog::on_tableView_doubleClicked(const QModelIndex &index)
{
std::string button(PS2::CControllerInfo::m_buttonName[index.row()]);
std::transform(button.begin(), button.end(),button.begin(), ::toupper);
InputEventSelectionDialog IESD;
IESD.Setup(button.c_str(), m_inputManager,
static_cast<PS2::CControllerInfo::BUTTON>(index.row()), m_inputDeviceManager);
IESD.exec();
m_inputDeviceManager.get()->DisconnectInputEventCallback();
OpenBindConfigDialog(index.row());
}
void ControllerConfigDialog::on_ConfigAllButton_clicked()
{
for(int i = 0; i < PS2::CControllerInfo::MAX_BUTTONS; ++i)
{
std::string button(PS2::CControllerInfo::m_buttonName[i]);
std::transform(button.begin(), button.end(),button.begin(), ::toupper);
InputEventSelectionDialog IESD;
IESD.Setup(button.c_str(), m_inputManager,
static_cast<PS2::CControllerInfo::BUTTON>(i), m_inputDeviceManager);
if(IESD.exec())
if(!OpenBindConfigDialog(i))
{
m_inputDeviceManager.get()->DisconnectInputEventCallback();
std::this_thread::sleep_for(std::chrono::seconds(2));
}
else
{
m_inputDeviceManager.get()->DisconnectInputEventCallback();
break;
}
}
}
int ControllerConfigDialog::OpenBindConfigDialog(int index)
{
std::string button(PS2::CControllerInfo::m_buttonName[index]);
std::transform(button.begin(), button.end(),button.begin(), ::toupper);
InputEventSelectionDialog IESD;
IESD.Setup(button.c_str(), m_inputManager,
static_cast<PS2::CControllerInfo::BUTTON>(index), m_inputDeviceManager);
auto res = IESD.exec();
m_inputDeviceManager.get()->DisconnectInputEventCallback();
return res;
}

View File

@ -27,6 +27,7 @@ private slots:
void on_ConfigAllButton_clicked();
private:
int OpenBindConfigDialog(int index);
CInputBindingManager* m_inputManager;
std::unique_ptr<CGamePadDeviceListener> m_inputDeviceManager;
Ui::ControllerConfigDialog *ui;

View File

@ -9,10 +9,20 @@ InputEventSelectionDialog::InputEventSelectionDialog(QWidget *parent) :
ui->setupUi(this);
setWindowFlags(Qt::Window | Qt::WindowCloseButtonHint);
setFixedSize(size());
// workaround to avoid direct thread ui access
connect(this, SIGNAL(setSelectedButtonLabelText(QString)), ui->selectedbuttonlabel, SLOT(setText(QString)));
connect(this, SIGNAL(setCountDownLabelText(QString)), ui->countdownlabel, SLOT(setText(QString)));
m_isCounting = false;
m_running = true;
m_thread = std::thread([this]{ CountDownThreadLoop(); });
}
InputEventSelectionDialog::~InputEventSelectionDialog()
{
m_running = false;
if(m_thread.joinable()) m_thread.join();
delete ui;
}
@ -20,67 +30,74 @@ void InputEventSelectionDialog::Setup(const char* text, CInputBindingManager *in
PS2::CControllerInfo::BUTTON button, std::unique_ptr<CGamePadDeviceListener>const &GPDL)
{
m_inputManager = inputManager;
ui->label->setText(labeltext.arg(text));
ui->bindinglabel->setText(m_bindingtext.arg(text));
m_button = button;
auto onInput = [=](std::array<unsigned int, 6> device, int code, int value, int type, const input_absinfo *abs)->void
{
if(value == 0 || type == 4) return;
if(type == 4) return;
if(!(type == 3 && abs->flat))
{
if(!setCounter(value)) return;
}
QString key = QString(libevdev_event_code_get_name(type, code));
if(type == 3)
{
if(abs->flat)
{
int midVal = abs->maximum/2;
int triggerVal = abs->maximum/10;
if (value < midVal + triggerVal && value > midVal - triggerVal) return;
ui->label->setText(ui->label->text() + "\n\nSelected Key: " + key);
int triggerRange = abs->maximum/100*20;
int triggerVal1 = abs->maximum - triggerRange;
int triggerVal2 = abs->minimum + triggerRange;
if(value < triggerVal1 && triggerVal2 < value)
{
setCounter(0);
return;
}
setCounter(1);
setSelectedButtonLabelText("Selected Key: " + key);
m_key1.id = code;
m_key1.device = device;
m_key1.type = type;
m_inputManager->SetSimpleBinding(m_button,m_key1);
m_key1.bindtype = CInputBindingManager::BINDINGTYPE::BINDING_SIMPLE;
}
else
{
m_key1.id = code;
m_key1.device = device;
m_key1.type = type;
m_inputManager->SetPovHatBinding(m_button,m_key1, value);
ui->label->setText(ui->label->text() + "\n\nSelected Key: " + key);
m_key1.value = value;
m_key1.bindtype = CInputBindingManager::BINDINGTYPE::BINDING_POVHAT;
setSelectedButtonLabelText("Selected Key: " + key);
}
accept();
}
else
{
if(PS2::CControllerInfo::IsAxis(m_button))
{
if(click_count++ == 0)
if(click_count == 0)
{
setSelectedButtonLabelText("(-) Key Selected: " + key);
m_key1.id = code;
m_key1.device = device;
m_key1.type = type;
ui->label->setText("(-) Key Selected: " + key + "\nPlease Select (+) Key");
return;
m_key1.bindtype = CInputBindingManager::BINDINGTYPE::BINDING_SIMULATEDAXIS;
}
else
{
CInputBindingManager::BINDINGINFO m_key2;
m_key2.id = code;
m_key2.device = device;
m_key2.type = type;
m_inputManager->SetSimulatedAxisBinding(m_button, m_key1, m_key2);
setSelectedButtonLabelText("(+) Key Selected: " + key);
}
}
else
{
ui->label->setText(ui->label->text() + "\n\nSelected Key: " + key);
setSelectedButtonLabelText("Selected Key: " + key);
m_key1.id = code;
m_key1.device = device;
m_key1.type = type;
m_inputManager->SetSimpleBinding(m_button,m_key1);
m_key1.bindtype = CInputBindingManager::BINDINGTYPE::BINDING_SIMPLE;
}
accept();
}
};
@ -91,34 +108,115 @@ void InputEventSelectionDialog::keyPressEvent(QKeyEvent* ev)
{
if(ev->key() == Qt::Key_Escape)
{
m_running = false;
reject();
return;
}
setCounter(1);
QString key = QKeySequence(ev->key()).toString();
ui->label->setText(ui->label->text() + "\n\nSelected Key: " + key);
if(PS2::CControllerInfo::IsAxis(m_button))
{
if(click_count++ == 0)
if(click_count == 0)
{
m_key1.id = ev->key();
m_key1.device = {0};
ui->label->setText("(-) Key Selected: " + key + "\nPlease Select (+) Key");
return;
m_key1.bindtype = CInputBindingManager::BINDINGTYPE::BINDING_SIMULATEDAXIS;
setSelectedButtonLabelText("(-) Key Selected: " + key);
}
else
{
CInputBindingManager::BINDINGINFO m_key2;
m_key2.id = ev->key();
m_key2.device = {0};
m_inputManager->SetSimulatedAxisBinding(m_button, m_key1, m_key2);
setSelectedButtonLabelText("(+) Key Selected: " + key);
}
}
else
{
m_key1.id = ev->key();
m_key1.device = {0};
m_inputManager->SetSimpleBinding(m_button,m_key1);
m_key1.bindtype = CInputBindingManager::BINDINGTYPE::BINDING_SIMPLE;
setSelectedButtonLabelText("Key Selected: " + key);
}
}
void InputEventSelectionDialog::keyReleaseEvent(QKeyEvent* ev)
{
if(!ev->isAutoRepeat())
{
setCounter(0);
}
}
void InputEventSelectionDialog::CountDownThreadLoop()
{
while(m_running)
{
auto time_now = std::chrono::system_clock::now();
std::chrono::duration<double> diff = time_now - m_countStart;
if(m_isCounting)
{
if(diff.count() < 3)
{
setCountDownLabelText(m_countingtext.arg(static_cast<int>(3 - diff.count())));
}
else
{
switch(m_key1.bindtype)
{
case CInputBindingManager::BINDINGTYPE::BINDING_SIMPLE:
m_inputManager->SetSimpleBinding(m_button, m_key1);
break;
case CInputBindingManager::BINDINGTYPE::BINDING_POVHAT:
m_inputManager->SetPovHatBinding(m_button,m_key1, m_key1.value);
break;
case CInputBindingManager::BINDINGTYPE::BINDING_SIMULATEDAXIS:
if(click_count == 0)
{
click_count++;
m_isCounting = 0;
setCountDownLabelText(m_countingtext.arg(3));
continue;
}
else if(m_key2.id > 0)
{
m_inputManager->SetSimulatedAxisBinding(m_button, m_key1, m_key2);
}
break;
}
m_running = false;
accept();
}
}
else if(diff.count() < 3)
{
setCountDownLabelText(m_countingtext.arg(3));
}
}
}
bool InputEventSelectionDialog::setCounter(int value)
{
if(value == 0)
{
if(click_count == 0)
{
m_key1.id = -1;
setSelectedButtonLabelText("Selected Key: None");
}
else
{
m_key2.id = -1;
setSelectedButtonLabelText("(+) Selected Key: None");
}
m_isCounting = false;
return false;
}
else if(m_isCounting == false)
{
m_countStart = std::chrono::system_clock::now();
m_isCounting = true;
}
return true;
}

View File

@ -2,7 +2,8 @@
#include <QDialog>
#include <QKeyEvent>
#include <chrono>
#include <thread>
#include "ui_unix/PH_HidUnix.h"
#include "GamePad/GamePadInputEventListener.h"
@ -24,12 +25,30 @@ public:
protected:
void keyPressEvent(QKeyEvent*) Q_DECL_OVERRIDE;
void keyReleaseEvent(QKeyEvent*) Q_DECL_OVERRIDE;
private:
struct BINDINGINFOEXTENDED : CInputBindingManager::BINDINGINFO
{
int value;
CInputBindingManager::BINDINGTYPE bindtype;
};
void CountDownThreadLoop();
bool setCounter(int);
int click_count = 0;
QString labeltext = QString("Select new binding for\n%1");
QString m_bindingtext = QString("Select new binding for\n%1");
QString m_countingtext = QString("Press & Hold Button for %1 Seconds to assign key");
PS2::CControllerInfo::BUTTON m_button;
CInputBindingManager::BINDINGINFO m_key1;
BINDINGINFOEXTENDED m_key1;
CInputBindingManager::BINDINGINFO m_key2;
CInputBindingManager *m_inputManager;
Ui::InputEventSelectionDialog *ui;
std::chrono::time_point<std::chrono::system_clock> m_countStart = std::chrono::system_clock::now();
std::thread m_thread;
std::atomic<bool> m_running;
std::atomic<bool> m_isCounting;
Q_SIGNALS:
void setSelectedButtonLabelText(QString);
void setCountDownLabelText(QString);
};

View File

@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>320</width>
<width>342</width>
<height>131</height>
</rect>
</property>
@ -14,8 +14,28 @@
<string>Button Configuration</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="2" column="0">
<widget class="QLabel" name="selectedbuttonlabel">
<property name="text">
<string>Selected Key: None</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="countdownlabel">
<property name="text">
<string>Press &amp; Hold Button for 3 Seconds to assign key</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<widget class="QLabel" name="bindinglabel">
<property name="text">
<string>Select new binding for