2017-05-20 15:53:17 +00:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2017-06-13 15:16:41 +00:00
|
|
|
#include <thread>
|
|
|
|
|
2017-05-20 15:53:17 +00:00
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QRegExp>
|
|
|
|
#include <QString>
|
|
|
|
|
|
|
|
#include "DolphinQt2/Config/Mapping/MappingButton.h"
|
|
|
|
|
|
|
|
#include "Common/Thread.h"
|
2017-06-13 15:16:41 +00:00
|
|
|
#include "DolphinQt2/Config/Mapping/IOWindow.h"
|
|
|
|
#include "DolphinQt2/Config/Mapping/MappingCommon.h"
|
2017-05-20 15:53:17 +00:00
|
|
|
#include "DolphinQt2/Config/Mapping/MappingWidget.h"
|
|
|
|
#include "DolphinQt2/Config/Mapping/MappingWindow.h"
|
2017-06-27 05:46:54 +00:00
|
|
|
#include "DolphinQt2/QtUtils/BlockUserInputFilter.h"
|
2017-05-20 15:53:17 +00:00
|
|
|
#include "InputCommon/ControlReference/ControlReference.h"
|
|
|
|
#include "InputCommon/ControllerEmu/ControllerEmu.h"
|
|
|
|
#include "InputCommon/ControllerInterface/ControllerInterface.h"
|
2017-06-13 15:16:41 +00:00
|
|
|
#include "InputCommon/ControllerInterface/Device.h"
|
2017-05-20 15:53:17 +00:00
|
|
|
|
2017-07-02 11:15:46 +00:00
|
|
|
static QString EscapeAmpersand(QString&& string)
|
|
|
|
{
|
|
|
|
return string.replace(QStringLiteral("&"), QStringLiteral("&&"));
|
|
|
|
}
|
|
|
|
|
2017-05-20 15:53:17 +00:00
|
|
|
MappingButton::MappingButton(MappingWidget* widget, ControlReference* ref)
|
2017-06-08 02:02:16 +00:00
|
|
|
: ElidedButton(EscapeAmpersand(QString::fromStdString(ref->GetExpression()))), m_parent(widget),
|
2017-07-02 11:15:46 +00:00
|
|
|
m_reference(ref)
|
2017-05-20 15:53:17 +00:00
|
|
|
{
|
|
|
|
Connect();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MappingButton::Connect()
|
|
|
|
{
|
|
|
|
connect(this, &MappingButton::clicked, this, &MappingButton::OnButtonPressed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MappingButton::OnButtonPressed()
|
|
|
|
{
|
2017-06-14 00:36:30 +00:00
|
|
|
if (m_parent->GetDevice() == nullptr || !m_reference->IsInput())
|
2017-05-20 15:53:17 +00:00
|
|
|
return;
|
|
|
|
|
2017-06-27 05:46:54 +00:00
|
|
|
installEventFilter(BlockUserInputFilter::Instance());
|
2017-06-14 00:36:30 +00:00
|
|
|
grabKeyboard();
|
|
|
|
grabMouse();
|
|
|
|
|
2017-05-20 15:53:17 +00:00
|
|
|
// Make sure that we don't block event handling
|
|
|
|
std::thread([this] {
|
2017-06-13 15:16:41 +00:00
|
|
|
const auto dev = m_parent->GetDevice();
|
2017-05-20 15:53:17 +00:00
|
|
|
|
2017-06-13 15:16:41 +00:00
|
|
|
setText(QStringLiteral("..."));
|
2017-05-20 15:53:17 +00:00
|
|
|
|
2017-06-13 15:16:41 +00:00
|
|
|
// Avoid that the button press itself is registered as an event
|
|
|
|
Common::SleepCurrentThread(100);
|
2017-05-20 15:53:17 +00:00
|
|
|
|
2017-11-04 21:08:26 +00:00
|
|
|
const auto expr = MappingCommon::DetectExpression(
|
|
|
|
m_reference, dev.get(), m_parent->GetParent()->GetDeviceQualifier(),
|
|
|
|
m_parent->GetController()->GetDefaultDevice());
|
2017-05-20 15:53:17 +00:00
|
|
|
|
2017-06-14 00:11:52 +00:00
|
|
|
releaseMouse();
|
|
|
|
releaseKeyboard();
|
2017-06-27 05:46:54 +00:00
|
|
|
removeEventFilter(BlockUserInputFilter::Instance());
|
2017-06-14 00:11:52 +00:00
|
|
|
|
2017-06-13 15:16:41 +00:00
|
|
|
if (!expr.isEmpty())
|
|
|
|
{
|
2017-06-08 02:02:16 +00:00
|
|
|
m_reference->SetExpression(expr.toStdString());
|
2017-06-13 15:16:41 +00:00
|
|
|
Update();
|
2017-05-20 15:53:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-13 15:16:41 +00:00
|
|
|
OnButtonTimeout();
|
2017-05-20 15:53:17 +00:00
|
|
|
}
|
|
|
|
}).detach();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MappingButton::OnButtonTimeout()
|
|
|
|
{
|
2017-06-08 02:02:16 +00:00
|
|
|
setText(EscapeAmpersand(QString::fromStdString(m_reference->GetExpression())));
|
2017-05-20 15:53:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MappingButton::Clear()
|
|
|
|
{
|
2017-06-08 02:02:16 +00:00
|
|
|
m_reference->SetExpression("");
|
2017-11-03 20:28:45 +00:00
|
|
|
m_parent->SaveSettings();
|
2017-05-20 15:53:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MappingButton::Update()
|
|
|
|
{
|
|
|
|
const auto lock = ControllerEmu::EmulatedController::GetStateLock();
|
|
|
|
m_reference->UpdateReference(g_controller_interface, m_parent->GetParent()->GetDeviceQualifier());
|
2017-06-08 02:02:16 +00:00
|
|
|
setText(EscapeAmpersand(QString::fromStdString(m_reference->GetExpression())));
|
2017-05-20 15:53:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MappingButton::mouseReleaseEvent(QMouseEvent* event)
|
|
|
|
{
|
2017-06-13 15:16:41 +00:00
|
|
|
switch (event->button())
|
2017-05-20 15:53:17 +00:00
|
|
|
{
|
2017-06-13 15:16:41 +00:00
|
|
|
case Qt::MouseButton::LeftButton:
|
|
|
|
if (m_reference->IsInput())
|
2017-05-20 15:53:17 +00:00
|
|
|
QPushButton::mouseReleaseEvent(event);
|
2017-06-13 15:16:41 +00:00
|
|
|
else
|
|
|
|
emit AdvancedPressed();
|
|
|
|
return;
|
|
|
|
case Qt::MouseButton::MiddleButton:
|
|
|
|
Clear();
|
|
|
|
return;
|
|
|
|
case Qt::MouseButton::RightButton:
|
|
|
|
emit AdvancedPressed();
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
return;
|
2017-05-20 15:53:17 +00:00
|
|
|
}
|
|
|
|
}
|