2017-05-20 15:53:17 +00:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-05-23 21:47:42 +00:00
|
|
|
#include <future>
|
2017-06-13 15:16:41 +00:00
|
|
|
#include <thread>
|
2018-05-23 21:47:42 +00:00
|
|
|
#include <utility>
|
2017-06-13 15:16:41 +00:00
|
|
|
|
2018-05-20 16:27:13 +00:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QFontMetrics>
|
2017-05-20 15:53:17 +00:00
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QRegExp>
|
|
|
|
#include <QString>
|
2018-02-06 10:00:23 +00:00
|
|
|
#include <QTimer>
|
|
|
|
|
2017-05-20 15:53:17 +00:00
|
|
|
#include "DolphinQt2/Config/Mapping/MappingButton.h"
|
|
|
|
|
|
|
|
#include "Common/Thread.h"
|
2018-05-10 23:46:05 +00:00
|
|
|
#include "Core/Core.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"
|
2018-02-06 10:00:23 +00:00
|
|
|
#include "DolphinQt2/Settings.h"
|
2018-05-10 23:46:05 +00:00
|
|
|
|
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
|
|
|
|
2018-05-02 16:29:05 +00:00
|
|
|
constexpr int SLIDER_TICK_COUNT = 100;
|
2018-05-20 16:27:13 +00:00
|
|
|
constexpr int VERTICAL_PADDING = 2;
|
2018-05-02 16:29:05 +00:00
|
|
|
|
2017-07-02 11:15:46 +00:00
|
|
|
static QString EscapeAmpersand(QString&& string)
|
|
|
|
{
|
|
|
|
return string.replace(QStringLiteral("&"), QStringLiteral("&&"));
|
|
|
|
}
|
|
|
|
|
2018-04-01 14:25:34 +00:00
|
|
|
bool MappingButton::IsInput() const
|
|
|
|
{
|
|
|
|
return m_reference->IsInput();
|
|
|
|
}
|
|
|
|
|
2018-02-06 10:00:23 +00:00
|
|
|
MappingButton::MappingButton(MappingWidget* widget, ControlReference* ref, bool indicator)
|
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
|
|
|
{
|
2018-05-20 16:27:13 +00:00
|
|
|
// Force all mapping buttons to use stay at a minimal height
|
|
|
|
int height = QFontMetrics(qApp->font()).height() + 2 * VERTICAL_PADDING;
|
|
|
|
|
|
|
|
setMinimumHeight(height);
|
2018-05-30 14:39:07 +00:00
|
|
|
|
|
|
|
// macOS needs some wiggle room to always get round buttons
|
|
|
|
setMaximumHeight(height + 8);
|
2018-05-20 16:27:13 +00:00
|
|
|
|
|
|
|
// Make sure that long entries don't throw our layout out of whack
|
|
|
|
setMaximumWidth(115);
|
|
|
|
|
|
|
|
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
|
|
|
|
2017-05-20 15:53:17 +00:00
|
|
|
Connect();
|
2018-02-07 17:16:15 +00:00
|
|
|
setToolTip(
|
|
|
|
tr("Left-click to detect input.\nMiddle-click to clear.\nRight-click for more options."));
|
2018-02-06 10:00:23 +00:00
|
|
|
if (!m_reference->IsInput() || !indicator)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_timer = new QTimer(this);
|
|
|
|
connect(m_timer, &QTimer::timeout, this, [this] {
|
|
|
|
if (!isActiveWindow())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Settings::Instance().SetControllerStateNeeded(true);
|
|
|
|
|
2018-05-10 23:46:05 +00:00
|
|
|
if (Core::GetState() == Core::State::Uninitialized || Core::GetState() == Core::State::Paused)
|
|
|
|
g_controller_interface.UpdateInput();
|
|
|
|
|
2018-02-06 10:00:23 +00:00
|
|
|
auto state = m_reference->State();
|
|
|
|
|
|
|
|
QFont f = m_parent->font();
|
|
|
|
QPalette p = m_parent->palette();
|
|
|
|
|
|
|
|
if (state != 0)
|
|
|
|
{
|
|
|
|
f.setBold(true);
|
|
|
|
p.setColor(QPalette::ButtonText, Qt::red);
|
|
|
|
}
|
|
|
|
|
|
|
|
setFont(f);
|
|
|
|
setPalette(p);
|
|
|
|
|
|
|
|
Settings::Instance().SetControllerStateNeeded(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
m_timer->start(1000 / 30);
|
2017-05-20 15:53:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MappingButton::Connect()
|
|
|
|
{
|
2018-04-01 14:25:34 +00:00
|
|
|
connect(this, &MappingButton::pressed, this, &MappingButton::Detect);
|
2017-05-20 15:53:17 +00:00
|
|
|
}
|
|
|
|
|
2018-04-01 14:25:34 +00:00
|
|
|
void MappingButton::Detect()
|
2017-05-20 15:53:17 +00:00
|
|
|
{
|
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();
|
|
|
|
|
2017-05-20 15:53:17 +00:00
|
|
|
// Make sure that we don't block event handling
|
2018-04-01 14:25:34 +00:00
|
|
|
std::thread thread([this] {
|
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
|
|
|
|
2018-05-23 21:47:42 +00:00
|
|
|
std::vector<std::future<std::pair<QString, QString>>> futures;
|
|
|
|
QString expr;
|
|
|
|
|
|
|
|
if (m_parent->GetParent()->IsMappingAllDevices())
|
|
|
|
{
|
|
|
|
for (const std::string& device_str : g_controller_interface.GetAllDeviceStrings())
|
|
|
|
{
|
|
|
|
ciface::Core::DeviceQualifier devq;
|
|
|
|
devq.FromString(device_str);
|
|
|
|
|
|
|
|
auto dev = g_controller_interface.FindDevice(devq);
|
|
|
|
|
|
|
|
auto future = std::async(std::launch::async, [this, devq, dev, device_str] {
|
|
|
|
return std::make_pair(
|
|
|
|
QString::fromStdString(device_str),
|
|
|
|
MappingCommon::DetectExpression(m_reference, dev.get(),
|
|
|
|
m_parent->GetController()->GetDefaultDevice()));
|
|
|
|
});
|
|
|
|
|
|
|
|
futures.push_back(std::move(future));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool done = false;
|
|
|
|
|
|
|
|
while (!done)
|
|
|
|
{
|
|
|
|
for (auto& future : futures)
|
|
|
|
{
|
|
|
|
const auto status = future.wait_for(std::chrono::milliseconds(10));
|
|
|
|
if (status == std::future_status::ready)
|
|
|
|
{
|
|
|
|
const auto pair = future.get();
|
|
|
|
|
|
|
|
done = true;
|
|
|
|
|
|
|
|
if (pair.second.isEmpty())
|
|
|
|
break;
|
|
|
|
|
|
|
|
expr = QStringLiteral("`%1:%2`")
|
|
|
|
.arg(pair.first)
|
|
|
|
.arg(pair.second.startsWith(QLatin1Char('`')) ? pair.second.mid(1) :
|
|
|
|
pair.second);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const auto dev = m_parent->GetDevice();
|
|
|
|
expr = MappingCommon::DetectExpression(m_reference, dev.get(),
|
|
|
|
m_parent->GetController()->GetDefaultDevice());
|
|
|
|
}
|
2017-05-20 15:53:17 +00:00
|
|
|
|
2017-06-14 00:11:52 +00:00
|
|
|
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());
|
2018-02-04 21:03:38 +00:00
|
|
|
m_parent->SaveSettings();
|
2017-06-13 15:16:41 +00:00
|
|
|
Update();
|
2018-05-13 19:17:46 +00:00
|
|
|
m_parent->GetController()->UpdateReferences(g_controller_interface);
|
2018-04-01 14:25:34 +00:00
|
|
|
|
|
|
|
if (m_parent->IsIterativeInput())
|
|
|
|
m_parent->NextButton(this);
|
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
|
|
|
}
|
2018-04-01 14:25:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
thread.detach();
|
2017-05-20 15:53:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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("");
|
2018-05-02 16:29:05 +00:00
|
|
|
m_reference->range = 100.0 / SLIDER_TICK_COUNT;
|
2017-11-03 20:28:45 +00:00
|
|
|
m_parent->SaveSettings();
|
2018-02-07 17:16:15 +00:00
|
|
|
Update();
|
2017-05-20 15:53:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MappingButton::Update()
|
|
|
|
{
|
|
|
|
const auto lock = ControllerEmu::EmulatedController::GetStateLock();
|
2017-11-04 22:29:15 +00:00
|
|
|
m_reference->UpdateReference(g_controller_interface,
|
|
|
|
m_parent->GetController()->GetDefaultDevice());
|
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;
|
2018-02-07 17:16:15 +00:00
|
|
|
case Qt::MouseButton::MidButton:
|
2017-06-13 15:16:41 +00:00
|
|
|
Clear();
|
|
|
|
return;
|
|
|
|
case Qt::MouseButton::RightButton:
|
|
|
|
emit AdvancedPressed();
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
return;
|
2017-05-20 15:53:17 +00:00
|
|
|
}
|
|
|
|
}
|