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

178 lines
4.5 KiB
C++
Raw 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 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()));
setWindowFlags(Qt::Window | Qt::WindowCloseButtonHint);
setFixedSize(size());
2018-01-10 15:45:33 +00:00
// workaround to avoid direct thread ui access
connect(this, SIGNAL(setSelectedButtonLabelText(QString)), ui->selectedbuttonlabel, SLOT(setText(QString)));
}
InputEventSelectionDialog::~InputEventSelectionDialog()
{
m_inputManager->OverrideInputEventHandler(InputEventFunction());
delete ui;
}
void InputEventSelectionDialog::Setup(const char* text, CInputBindingManager* inputManager, CInputProviderQtKey* qtKeyInputProvider, PS2::CControllerInfo::BUTTON button)
{
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()));
m_qtKeyInputProvider = qtKeyInputProvider;
ui->bindinglabel->setText(m_bindingtext.arg(text));
m_button = button;
}
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 =
[this] (CInputBindingManager::BINDINGTYPE bindingType, const auto& target)
{
m_selectedTarget = target;
m_bindingType = bindingType;
m_state = STATE::SELECTED;
startCountdown();
auto targetDescription = m_inputManager->GetTargetDescription(target);
setSelectedButtonLabelText("Selected Key: " + QString::fromStdString(targetDescription));
};
auto resetSelection =
[this] ()
{
m_selectedTarget = BINDINGTARGET();
m_state = STATE::NONE;
m_bindingType = CInputBindingManager::BINDING_UNBOUND;
cancelCountdown();
setSelectedButtonLabelText("Selected Key: None");
};
switch(m_state)
{
case STATE::NONE:
//Check if we've pressed something
switch(target.keyType)
{
case BINDINGTARGET::KEYTYPE::BUTTON:
if(value != 0)
{
2018-11-21 12:41:42 -05:00
setSelection(CInputBindingManager::BINDING_SIMPLE, target);
}
break;
case BINDINGTARGET::KEYTYPE::AXIS:
if(!IsAxisIdle(value))
{
2018-11-21 12:41:42 -05:00
setSelection(CInputBindingManager::BINDING_SIMPLE, target);
2018-10-23 01:40:35 +01: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
}
}
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;
}
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;
}
m_qtKeyInputProvider->OnKeyPress(ev->key());
}
void InputEventSelectionDialog::keyReleaseEvent(QKeyEvent* ev)
{
m_qtKeyInputProvider->OnKeyRelease(ev->key());
}
2018-11-21 12:41:42 -05:00
void InputEventSelectionDialog::startCountdown()
{
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));
m_countdownTimer->start(1000);
}
2018-11-21 12:41:42 -05:00
void InputEventSelectionDialog::cancelCountdown()
{
2018-11-21 12:41:42 -05:00
m_countdownTimer->stop();
ui->countdownlabel->setText(m_countingtext.arg(COUNTDOWN_SECS));
}
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--;
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;
}
2018-11-21 12:41:42 -05:00
accept();
}