Play-/Source/ui_qt/ControllerConfig/inputeventselectiondialog.cpp

257 lines
6.9 KiB
C++
Raw Permalink Normal View History

2018-11-21 12:41:42 -05:00
#include <QTimer>
#include "inputeventselectiondialog.h"
#include "ui_inputeventselectiondialog.h"
2018-11-21 12:41:42 -05:00
#define COUNTDOWN_SECS 3
2018-04-30 21:01:23 +01:00
InputEventSelectionDialog::InputEventSelectionDialog(QWidget* parent)
: QDialog(parent)
, ui(new Ui::InputEventSelectionDialog)
{
ui->setupUi(this);
2018-11-21 19:53:56 -05:00
ui->countdownlabel->setText(m_countingText.arg(COUNTDOWN_SECS));
2018-11-21 12:41:42 -05:00
m_countdownTimer = new QTimer(this);
connect(m_countdownTimer, SIGNAL(timeout()), this, SLOT(updateCountdown()));
2018-11-28 22:32:27 -05:00
setWindowFlags(Qt::Window | Qt::WindowCloseButtonHint);
setFixedSize(size());
2018-01-10 15:45:33 +00:00
// workaround to avoid direct thread ui access
2018-11-21 12:55:28 -05:00
connect(this, SIGNAL(startCountdown(QString)), this, SLOT(handleStartCountdown(QString)));
connect(this, SIGNAL(cancelCountdown()), this, SLOT(handleCancelCountdown()));
}
InputEventSelectionDialog::~InputEventSelectionDialog()
{
delete ui;
}
2023-04-20 19:03:13 -04:00
void InputEventSelectionDialog::Setup(const char* text, CInputBindingManager* inputManager, CInputProviderQtKey* qtKeyInputProvider, CInputProviderQtMouse* qtMouseInputProvider, uint32 padIndex, PS2::CControllerInfo::BUTTON button)
{
m_inputManager = inputManager;
2020-09-14 19:43:25 -04:00
m_padIndex = padIndex;
m_button = button;
m_buttonName = QString::fromUtf8(text);
m_qtKeyInputProvider = qtKeyInputProvider;
2023-04-20 19:03:13 -04:00
m_qtMouseInputProvider = qtMouseInputProvider;
ui->bindinglabel->setText(m_bindingText.arg(m_buttonName));
m_providersOverrideConnection = m_inputManager->OverrideInputEventHandler([this](auto target, auto value) { this->onInputEvent(target, value); });
connect(this, SIGNAL(countdownComplete()), this, SLOT(confirmBinding()));
}
static bool IsAxisIdle(uint32 value)
{
uint32 triggerRange = (255 * 20) / 100;
uint32 triggerVal1 = 255 - triggerRange;
uint32 triggerVal2 = 0 + triggerRange;
return (value < triggerVal1) && (value > triggerVal2);
}
void InputEventSelectionDialog::onInputEvent(const BINDINGTARGET& target, uint32 value)
{
2018-11-21 12:41:42 -05:00
auto setSelection =
2018-11-28 22:32:27 -05:00
[this](CInputBindingManager::BINDINGTYPE bindingType, const auto& target) {
m_selectedTarget = target;
m_bindingType = bindingType;
m_state = STATE::SELECTED;
auto targetDescription = m_inputManager->GetTargetDescription(target);
startCountdown(QString::fromStdString(targetDescription));
};
2018-11-21 12:41:42 -05:00
auto resetSelection =
2018-11-28 22:32:27 -05:00
[this]() {
m_selectedTarget = BINDINGTARGET();
m_state = STATE::WAITING;
m_bindingType = CInputBindingManager::BINDING_UNBOUND;
cancelCountdown();
};
auto setSelectionSimulatedAxis =
2018-11-28 22:32:27 -05:00
[this](const auto& target) {
m_selectedTargetSimulatedAxis = target;
m_state = STATE::SIMULATEDAXIS_SELECTED;
auto targetDescription = m_inputManager->GetTargetDescription(target);
startCountdown(QString::fromStdString(targetDescription));
};
auto resetSelectionSimulatedAxis =
2018-11-28 22:32:27 -05:00
[this]() {
m_selectedTargetSimulatedAxis = BINDINGTARGET();
m_state = STATE::SIMULATEDAXIS_WAITING;
cancelCountdown();
};
switch(m_state)
{
case STATE::WAITING:
//Check if we've pressed something
if(PS2::CControllerInfo::IsAxis(m_button))
{
switch(target.keyType)
{
case BINDINGTARGET::KEYTYPE::BUTTON:
if(value != 0)
{
setSelection(CInputBindingManager::BINDING_SIMULATEDAXIS, target);
}
break;
case BINDINGTARGET::KEYTYPE::AXIS:
if(!IsAxisIdle(value))
{
setSelection(CInputBindingManager::BINDING_SIMPLE, target);
}
break;
2018-10-23 01:40:35 +01:00
}
}
else
{
switch(target.keyType)
2018-10-23 01:40:35 +01:00
{
case BINDINGTARGET::KEYTYPE::BUTTON:
if(value != 0)
{
setSelection(CInputBindingManager::BINDING_SIMPLE, target);
}
break;
case BINDINGTARGET::KEYTYPE::AXIS:
if(!IsAxisIdle(value))
{
setSelection(CInputBindingManager::BINDING_SIMPLE, target);
}
break;
case BINDINGTARGET::KEYTYPE::POVHAT:
if(value < BINDINGTARGET::POVHAT_MAX)
{
m_bindingValue = value;
setSelection(CInputBindingManager::BINDING_POVHAT, target);
}
2018-10-23 01:40:35 +01:00
}
}
break;
case STATE::SELECTED:
if(target != m_selectedTarget) break;
switch(target.keyType)
2018-10-23 01:40:35 +01:00
{
case BINDINGTARGET::KEYTYPE::BUTTON:
if(value == 0)
2018-10-23 01:40:35 +01:00
{
2018-11-21 12:41:42 -05:00
resetSelection();
2018-10-23 01:40:35 +01:00
}
break;
case BINDINGTARGET::KEYTYPE::AXIS:
if(IsAxisIdle(value))
2018-10-23 01:40:35 +01:00
{
2018-11-21 12:41:42 -05:00
resetSelection();
}
break;
case BINDINGTARGET::KEYTYPE::POVHAT:
if(m_bindingValue != value)
{
2018-11-21 12:41:42 -05:00
resetSelection();
2018-10-23 01:40:35 +01:00
}
}
break;
case STATE::SIMULATEDAXIS_WAITING:
if((target.keyType == BINDINGTARGET::KEYTYPE::BUTTON) && (value != 0))
{
setSelectionSimulatedAxis(target);
}
break;
case STATE::SIMULATEDAXIS_SELECTED:
if(target != m_selectedTargetSimulatedAxis) break;
if(value == 0)
{
resetSelectionSimulatedAxis();
}
break;
}
2018-10-23 01:40:35 +01:00
}
2018-05-23 03:11:24 +03:00
void InputEventSelectionDialog::keyPressEvent(QKeyEvent* ev)
{
if(ev->key() == Qt::Key_Escape)
{
reject();
return;
}
if(ev->isAutoRepeat()) return;
m_qtKeyInputProvider->OnKeyPress(ev->key());
}
void InputEventSelectionDialog::keyReleaseEvent(QKeyEvent* ev)
{
2018-11-27 13:24:44 -05:00
if(ev->isAutoRepeat()) return;
m_qtKeyInputProvider->OnKeyRelease(ev->key());
}
2023-04-20 19:03:13 -04:00
void InputEventSelectionDialog::mousePressEvent(QMouseEvent* ev)
{
m_qtMouseInputProvider->OnMousePress(ev->button());
}
void InputEventSelectionDialog::mouseReleaseEvent(QMouseEvent* ev)
{
m_qtMouseInputProvider->OnMouseRelease(ev->button());
}
2018-11-21 12:55:28 -05:00
void InputEventSelectionDialog::handleStartCountdown(QString bindingDesc)
{
2018-11-21 12:41:42 -05:00
m_countdownRemain = COUNTDOWN_SECS - 1;
static_assert(COUNTDOWN_SECS >= 1, "COUNTDOWN_SECS must be at least 1");
2018-11-21 19:53:56 -05:00
ui->countdownlabel->setText(m_countingText.arg(m_countdownRemain));
ui->selectedbuttonlabel->setText(m_selectedkeyText.arg(bindingDesc));
2018-11-21 12:41:42 -05:00
m_countdownTimer->start(1000);
}
2018-11-21 12:55:28 -05:00
void InputEventSelectionDialog::handleCancelCountdown()
{
2018-11-21 12:41:42 -05:00
m_countdownTimer->stop();
2018-11-21 19:53:56 -05:00
ui->countdownlabel->setText(m_countingText.arg(COUNTDOWN_SECS));
ui->selectedbuttonlabel->setText(m_selectedkeyText.arg(QString("None")));
2018-11-21 12:41:42 -05:00
}
void InputEventSelectionDialog::updateCountdown()
{
if(m_countdownRemain == 0)
{
2018-11-21 12:41:42 -05:00
m_countdownTimer->stop();
countdownComplete();
}
2018-11-21 12:41:42 -05:00
else
{
2018-11-21 12:41:42 -05:00
m_countdownRemain--;
2018-11-21 19:53:56 -05:00
ui->countdownlabel->setText(m_countingText.arg(m_countdownRemain));
2018-11-21 12:41:42 -05:00
}
}
void InputEventSelectionDialog::confirmBinding()
2018-11-21 12:41:42 -05:00
{
switch(m_bindingType)
{
case CInputBindingManager::BINDING_SIMPLE:
2020-09-14 19:43:25 -04:00
m_inputManager->SetSimpleBinding(m_padIndex, m_button, m_selectedTarget);
accept();
break;
case CInputBindingManager::BINDING_POVHAT:
2020-09-14 19:43:25 -04:00
m_inputManager->SetPovHatBinding(m_padIndex, m_button, m_selectedTarget, m_bindingValue);
accept();
break;
case CInputBindingManager::BINDING_SIMULATEDAXIS:
if(m_state == STATE::SELECTED)
{
m_state = STATE::SIMULATEDAXIS_WAITING;
ui->countdownlabel->setText(m_countingText.arg(COUNTDOWN_SECS));
ui->bindinglabel->setText(m_nextbindingText.arg(m_buttonName));
ui->selectedbuttonlabel->setText(m_selectedkeyText.arg(QString("None")));
}
else if(m_state == STATE::SIMULATEDAXIS_SELECTED)
{
2020-09-14 19:43:25 -04:00
m_inputManager->SetSimulatedAxisBinding(m_padIndex, m_button, m_selectedTarget, m_selectedTargetSimulatedAxis);
accept();
}
break;
}
}