2018-11-21 12:41:42 -05:00
|
|
|
#include <QTimer>
|
2017-05-23 16:08:53 +01:00
|
|
|
#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)
|
2017-05-23 16:08:53 +01:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2018-11-21 12:41:42 -05:00
|
|
|
ui->countdownlabel->setText(m_countingtext.arg(COUNTDOWN_SECS));
|
|
|
|
|
|
|
|
m_countdownTimer = new QTimer(this);
|
|
|
|
connect(m_countdownTimer, SIGNAL(timeout()), this, SLOT(updateCountdown()));
|
|
|
|
|
2017-06-13 04:51:38 +01:00
|
|
|
setWindowFlags(Qt::Window | Qt::WindowCloseButtonHint);
|
|
|
|
setFixedSize(size());
|
2018-01-10 16:24:21 +00:00
|
|
|
|
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()));
|
2017-05-23 16:08:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
InputEventSelectionDialog::~InputEventSelectionDialog()
|
|
|
|
{
|
2018-11-19 13:20:46 -05:00
|
|
|
m_inputManager->OverrideInputEventHandler(InputEventFunction());
|
2017-05-23 16:08:53 +01:00
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2018-11-19 13:20:46 -05:00
|
|
|
void InputEventSelectionDialog::Setup(const char* text, CInputBindingManager* inputManager, CInputProviderQtKey* qtKeyInputProvider, PS2::CControllerInfo::BUTTON button)
|
2017-05-23 16:08:53 +01:00
|
|
|
{
|
2017-06-13 04:51:38 +01:00
|
|
|
m_inputManager = inputManager;
|
2018-11-19 19:20:26 -05:00
|
|
|
m_inputManager->OverrideInputEventHandler([this] (auto target, auto value) { this->onInputEvent(target, value); } );
|
2018-11-21 12:41:42 -05:00
|
|
|
connect(this, SIGNAL(countdownComplete()), this, SLOT(completeSimpleBinding()));
|
2018-11-19 13:20:46 -05:00
|
|
|
m_qtKeyInputProvider = qtKeyInputProvider;
|
2018-01-10 16:24:21 +00:00
|
|
|
ui->bindinglabel->setText(m_bindingtext.arg(text));
|
2017-06-13 04:51:38 +01:00
|
|
|
m_button = button;
|
2018-07-31 19:00:16 -04:00
|
|
|
}
|
|
|
|
|
2018-11-19 13:20:46 -05:00
|
|
|
static bool IsAxisIdle(uint32 value)
|
|
|
|
{
|
|
|
|
uint32 triggerRange = (255 * 20) / 100;
|
|
|
|
uint32 triggerVal1 = 255 - triggerRange;
|
|
|
|
uint32 triggerVal2 = 0 + triggerRange;
|
|
|
|
return (value < triggerVal1) && (value > triggerVal2);
|
|
|
|
}
|
2017-06-13 04:51:38 +01:00
|
|
|
|
2018-11-19 13:20:46 -05:00
|
|
|
void InputEventSelectionDialog::onInputEvent(const BINDINGTARGET& target, uint32 value)
|
2018-07-31 19:00:16 -04:00
|
|
|
{
|
2018-11-21 12:41:42 -05:00
|
|
|
auto setSelection =
|
|
|
|
[this] (CInputBindingManager::BINDINGTYPE bindingType, const auto& target)
|
|
|
|
{
|
|
|
|
m_selectedTarget = target;
|
|
|
|
m_bindingType = bindingType;
|
|
|
|
m_state = STATE::SELECTED;
|
|
|
|
auto targetDescription = m_inputManager->GetTargetDescription(target);
|
2018-11-21 12:55:28 -05:00
|
|
|
startCountdown(QString::fromStdString(targetDescription));
|
2018-11-21 12:41:42 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
auto resetSelection =
|
|
|
|
[this] ()
|
|
|
|
{
|
|
|
|
m_selectedTarget = BINDINGTARGET();
|
|
|
|
m_state = STATE::NONE;
|
|
|
|
m_bindingType = CInputBindingManager::BINDING_UNBOUND;
|
|
|
|
cancelCountdown();
|
|
|
|
};
|
|
|
|
|
2018-11-19 13:20:46 -05:00
|
|
|
switch(m_state)
|
|
|
|
{
|
|
|
|
case STATE::NONE:
|
|
|
|
//Check if we've pressed something
|
|
|
|
switch(target.keyType)
|
2017-06-13 04:51:38 +01:00
|
|
|
{
|
2018-11-19 13:20:46 -05:00
|
|
|
case BINDINGTARGET::KEYTYPE::BUTTON:
|
|
|
|
if(value != 0)
|
2017-06-13 04:51:38 +01:00
|
|
|
{
|
2018-11-21 12:41:42 -05:00
|
|
|
setSelection(CInputBindingManager::BINDING_SIMPLE, target);
|
2017-06-13 04:51:38 +01:00
|
|
|
}
|
2018-11-19 13:20:46 -05:00
|
|
|
break;
|
|
|
|
case BINDINGTARGET::KEYTYPE::AXIS:
|
|
|
|
if(!IsAxisIdle(value))
|
2017-06-13 04:51:38 +01:00
|
|
|
{
|
2018-11-21 12:41:42 -05:00
|
|
|
setSelection(CInputBindingManager::BINDING_SIMPLE, target);
|
2018-10-23 01:40:35 +01:00
|
|
|
}
|
2018-11-19 13:20:46 -05:00
|
|
|
break;
|
|
|
|
case BINDINGTARGET::KEYTYPE::POVHAT:
|
2018-11-21 12:41:13 -05:00
|
|
|
if(value < BINDINGTARGET::POVHAT_MAX)
|
2018-10-23 01:40:35 +01:00
|
|
|
{
|
2018-11-21 12:41:42 -05:00
|
|
|
setSelection(CInputBindingManager::BINDING_POVHAT, target);
|
2018-10-23 01:40:35 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-19 13:20:46 -05:00
|
|
|
break;
|
|
|
|
case STATE::SELECTED:
|
|
|
|
if(target != m_selectedTarget) break;
|
|
|
|
switch(target.keyType)
|
2018-10-23 01:40:35 +01:00
|
|
|
{
|
2018-11-19 13:20:46 -05: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
|
|
|
}
|
2018-11-19 13:20:46 -05: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();
|
2018-11-19 13:20:46 -05:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2018-11-19 13:20:46 -05:00
|
|
|
break;
|
|
|
|
}
|
2018-10-23 01:40:35 +01:00
|
|
|
}
|
2018-05-23 03:11:24 +03:00
|
|
|
|
2017-05-23 16:08:53 +01:00
|
|
|
void InputEventSelectionDialog::keyPressEvent(QKeyEvent* ev)
|
|
|
|
{
|
2017-06-13 04:51:38 +01:00
|
|
|
if(ev->key() == Qt::Key_Escape)
|
|
|
|
{
|
|
|
|
reject();
|
|
|
|
return;
|
|
|
|
}
|
2018-11-19 13:20:46 -05:00
|
|
|
m_qtKeyInputProvider->OnKeyPress(ev->key());
|
2018-01-10 16:24:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputEventSelectionDialog::keyReleaseEvent(QKeyEvent* ev)
|
|
|
|
{
|
2018-11-19 13:20:46 -05:00
|
|
|
m_qtKeyInputProvider->OnKeyRelease(ev->key());
|
2018-01-10 16:24:21 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 12:55:28 -05:00
|
|
|
void InputEventSelectionDialog::handleStartCountdown(QString bindingDesc)
|
2018-01-10 16:24:21 +00:00
|
|
|
{
|
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");
|
|
|
|
ui->countdownlabel->setText(m_countingtext.arg(m_countdownRemain));
|
2018-11-21 12:55:28 -05:00
|
|
|
ui->selectedbuttonlabel->setText("Selected Key: " + bindingDesc);
|
2018-11-21 12:41:42 -05:00
|
|
|
m_countdownTimer->start(1000);
|
2018-01-10 16:24:21 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 12:55:28 -05:00
|
|
|
void InputEventSelectionDialog::handleCancelCountdown()
|
2018-01-10 16:24:21 +00:00
|
|
|
{
|
2018-11-21 12:41:42 -05:00
|
|
|
m_countdownTimer->stop();
|
|
|
|
ui->countdownlabel->setText(m_countingtext.arg(COUNTDOWN_SECS));
|
2018-11-21 12:55:28 -05:00
|
|
|
ui->selectedbuttonlabel->setText("Selected Key: None");
|
2018-11-21 12:41:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputEventSelectionDialog::updateCountdown()
|
|
|
|
{
|
|
|
|
if(m_countdownRemain == 0)
|
2018-01-10 16:24:21 +00:00
|
|
|
{
|
2018-11-21 12:41:42 -05:00
|
|
|
m_countdownTimer->stop();
|
|
|
|
countdownComplete();
|
2018-01-10 16:24:21 +00:00
|
|
|
}
|
2018-11-21 12:41:42 -05:00
|
|
|
else
|
2018-01-10 16:24:21 +00:00
|
|
|
{
|
2018-11-21 12:41:42 -05:00
|
|
|
m_countdownRemain--;
|
|
|
|
ui->countdownlabel->setText(m_countingtext.arg(m_countdownRemain));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputEventSelectionDialog::completeSimpleBinding()
|
|
|
|
{
|
|
|
|
switch(m_bindingType)
|
|
|
|
{
|
|
|
|
case CInputBindingManager::BINDING_SIMPLE:
|
|
|
|
m_inputManager->SetSimpleBinding(0, m_button, m_selectedTarget);
|
|
|
|
break;
|
|
|
|
case CInputBindingManager::BINDING_POVHAT:
|
|
|
|
m_inputManager->SetPovHatBinding(0, m_button, m_selectedTarget, m_bindingValue);
|
|
|
|
break;
|
2017-06-13 04:51:38 +01:00
|
|
|
}
|
2018-11-21 12:41:42 -05:00
|
|
|
accept();
|
2017-05-23 16:08:53 +01:00
|
|
|
}
|