Merge pull request #6952 from spycrab/qt_map_all

Qt/Mapping: Add option to map all devices at once
This commit is contained in:
spycrab 2018-05-24 01:21:23 +02:00 committed by GitHub
commit 4c1425b419
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 4 deletions

View File

@ -2,7 +2,9 @@
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <future>
#include <thread>
#include <utility>
#include <QApplication>
#include <QFontMetrics>
@ -108,15 +110,64 @@ void MappingButton::Detect()
// Make sure that we don't block event handling
std::thread thread([this] {
const auto dev = m_parent->GetDevice();
setText(QStringLiteral("..."));
// Avoid that the button press itself is registered as an event
Common::SleepCurrentThread(100);
const auto expr = MappingCommon::DetectExpression(
m_reference, dev.get(), m_parent->GetController()->GetDefaultDevice());
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());
}
releaseMouse();
releaseKeyboard();

View File

@ -222,10 +222,18 @@ void MappingWindow::OnSaveProfilePressed()
void MappingWindow::OnDeviceChanged(int index)
{
if (IsMappingAllDevices())
return;
const auto device = m_devices_combo->currentText().toStdString();
m_controller->SetDefaultDevice(device);
}
bool MappingWindow::IsMappingAllDevices() const
{
return m_devices_combo->currentIndex() == m_devices_combo->count() - 1;
}
void MappingWindow::RefreshDevices()
{
m_devices_combo->clear();
@ -245,6 +253,8 @@ void MappingWindow::RefreshDevices()
m_devices_combo->addItem(QString::fromStdString(name));
}
m_devices_combo->addItem(tr("All devices"));
m_devices_combo->setCurrentIndex(0);
});
}

View File

@ -52,6 +52,7 @@ public:
std::shared_ptr<ciface::Core::Device> GetDevice() const;
ControllerEmu::EmulatedController* GetController() const;
bool IsIterativeInput() const;
bool IsMappingAllDevices() const;
signals:
void Update();