dolphin/Source/Core/DolphinQt2/Config/Mapping/MappingButton.cpp

133 lines
3.3 KiB
C++
Raw Normal View History

2017-05-20 15:53:17 +00:00
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#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"
#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"
#include "InputCommon/ControlReference/ControlReference.h"
#include "InputCommon/ControllerEmu/ControllerEmu.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h"
#include "InputCommon/ControllerInterface/Device.h"
2017-05-20 15:53:17 +00:00
MappingButton::MappingButton(MappingWidget* widget, ControlReference* ref)
: ElidedButton(QString::fromStdString(ref->expression)), m_parent(widget), 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()
{
if (m_block || m_parent->GetDevice() == nullptr || !m_reference->IsInput())
2017-05-20 15:53:17 +00:00
return;
// Make sure that we don't block event handling
std::thread([this] {
const auto dev = m_parent->GetDevice();
2017-05-20 15:53:17 +00:00
setText(QStringLiteral("..."));
2017-05-20 15:53:17 +00:00
Common::SleepCurrentThread(100);
2017-05-20 15:53:17 +00:00
SetBlockInputs(true);
2017-05-20 15:53:17 +00:00
if (m_parent->GetFirstButtonPress())
m_reference->Detect(10, dev.get());
2017-05-20 15:53:17 +00:00
// Avoid that the button press itself is registered as an event
Common::SleepCurrentThread(100);
2017-05-20 15:53:17 +00:00
const auto expr = MappingCommon::DetectExpression(m_reference, dev.get(),
m_parent->GetParent()->GetDeviceQualifier(),
m_parent->GetController()->default_device);
2017-05-20 15:53:17 +00:00
SetBlockInputs(false);
if (!expr.isEmpty())
{
m_reference->expression = expr.toStdString();
Update();
2017-05-20 15:53:17 +00:00
}
else
{
OnButtonTimeout();
2017-05-20 15:53:17 +00:00
}
}).detach();
}
void MappingButton::OnButtonTimeout()
{
setText(QString::fromStdString(m_reference->expression));
2017-05-20 15:53:17 +00:00
}
void MappingButton::Clear()
{
m_parent->Update();
2017-05-20 15:53:17 +00:00
m_reference->expression.clear();
Update();
}
void MappingButton::Update()
{
const auto lock = ControllerEmu::EmulatedController::GetStateLock();
m_reference->UpdateReference(g_controller_interface, m_parent->GetParent()->GetDeviceQualifier());
setText(QString::fromStdString(m_reference->expression));
m_parent->SaveSettings();
}
void MappingButton::SetBlockInputs(const bool block)
{
m_parent->SetBlockInputs(block);
m_block = block;
}
void MappingWindow::OnDefaultFieldsPressed()
{
if (m_controller == nullptr)
return;
m_controller->LoadDefaults(g_controller_interface);
m_controller->UpdateReferences(g_controller_interface);
emit Update();
}
bool MappingButton::event(QEvent* event)
{
return !m_block ? QPushButton::event(event) : true;
}
void MappingButton::mouseReleaseEvent(QMouseEvent* event)
{
switch (event->button())
2017-05-20 15:53:17 +00:00
{
case Qt::MouseButton::LeftButton:
if (m_reference->IsInput())
2017-05-20 15:53:17 +00:00
QPushButton::mouseReleaseEvent(event);
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
}
}