2017-05-20 15:53:17 +00:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-05-20 15:53:17 +00:00
|
|
|
|
2019-02-28 00:10:18 +00:00
|
|
|
#include "DolphinQt/Config/Mapping/MappingButton.h"
|
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 <QString>
|
2018-02-06 10:00:23 +00:00
|
|
|
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/Config/Mapping/IOWindow.h"
|
|
|
|
#include "DolphinQt/Config/Mapping/MappingCommon.h"
|
|
|
|
#include "DolphinQt/Config/Mapping/MappingWidget.h"
|
|
|
|
#include "DolphinQt/Config/Mapping/MappingWindow.h"
|
2018-05-10 23:46:05 +00:00
|
|
|
|
2017-05-20 15:53:17 +00:00
|
|
|
#include "InputCommon/ControlReference/ControlReference.h"
|
2018-12-31 13:31:11 +00:00
|
|
|
#include "InputCommon/ControllerEmu/ControlGroup/Buttons.h"
|
2017-05-20 15:53:17 +00:00
|
|
|
#include "InputCommon/ControllerEmu/ControllerEmu.h"
|
|
|
|
#include "InputCommon/ControllerInterface/ControllerInterface.h"
|
|
|
|
|
2018-05-02 16:29:05 +00:00
|
|
|
constexpr int SLIDER_TICK_COUNT = 100;
|
|
|
|
|
2021-05-09 20:52:45 +00:00
|
|
|
// Escape ampersands and simplify the text for a short preview
|
|
|
|
static QString RefToDisplayString(ControlReference* ref)
|
2017-07-02 11:15:46 +00:00
|
|
|
{
|
2021-05-09 20:52:45 +00:00
|
|
|
const bool expression_valid =
|
|
|
|
ref->GetParseStatus() != ciface::ExpressionParser::ParseStatus::SyntaxError;
|
|
|
|
QString expression;
|
|
|
|
{
|
|
|
|
const auto lock = ControllerEmu::EmulatedController::GetStateLock();
|
|
|
|
expression = QString::fromStdString(ref->GetExpression());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split by "`" so that we can give a better preview of control,
|
|
|
|
// without including their device in front of them, which is usually
|
|
|
|
// too long to actually see the control.
|
|
|
|
QStringList controls = expression.split(QLatin1Char{'`'});
|
|
|
|
// Do try to simplify controls if the parsing had failed, as it might create false positives.
|
|
|
|
if (expression_valid)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < controls.size(); i++)
|
|
|
|
{
|
|
|
|
// We have two ` for control so make sure to only consider the odd ones.
|
|
|
|
if (i % 2)
|
|
|
|
{
|
|
|
|
// Use the code from the ControlQualifier instead of duplicating it.
|
|
|
|
ciface::ExpressionParser::ControlQualifier qualifier;
|
|
|
|
qualifier.FromString(controls[i].toStdString());
|
|
|
|
// If the control has got a device specifier/path, add ":" in front of it, to make it clear.
|
|
|
|
controls[i] = qualifier.has_device ? QStringLiteral(":") : QString();
|
|
|
|
controls[i].append(QString::fromStdString(qualifier.control_name));
|
|
|
|
}
|
2021-08-02 21:04:22 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
controls[i].remove(QLatin1Char{' '});
|
|
|
|
}
|
2021-05-09 20:52:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Do not re-add "`" to the final string, we don't need to see it.
|
|
|
|
expression = controls.join(QStringLiteral(""));
|
|
|
|
|
|
|
|
expression.remove(QLatin1Char{'\t'});
|
|
|
|
expression.remove(QLatin1Char{'\n'});
|
|
|
|
expression.remove(QLatin1Char{'\r'});
|
|
|
|
expression.replace(QLatin1Char{'&'}, QStringLiteral("&&"));
|
|
|
|
|
|
|
|
return expression;
|
2017-07-02 11:15:46 +00:00
|
|
|
}
|
|
|
|
|
2018-04-01 14:25:34 +00:00
|
|
|
bool MappingButton::IsInput() const
|
|
|
|
{
|
|
|
|
return m_reference->IsInput();
|
|
|
|
}
|
|
|
|
|
2019-03-15 01:27:49 +00:00
|
|
|
MappingButton::MappingButton(MappingWidget* parent, ControlReference* ref, bool indicator)
|
2021-05-09 20:52:45 +00:00
|
|
|
: ElidedButton(RefToDisplayString(ref)), m_parent(parent), m_reference(ref)
|
2017-05-20 15:53:17 +00:00
|
|
|
{
|
2019-03-03 14:05:59 +00:00
|
|
|
// Force all mapping buttons to stay at a minimal height.
|
|
|
|
setFixedHeight(minimumSizeHint().height());
|
2018-05-20 16:27:13 +00:00
|
|
|
|
2019-03-03 14:05:59 +00:00
|
|
|
// Make sure that long entries don't throw our layout out of whack.
|
2019-08-01 13:28:04 +00:00
|
|
|
setFixedWidth(WIDGET_MAX_WIDTH);
|
2018-05-30 14:39:07 +00:00
|
|
|
|
2019-03-03 14:05:59 +00:00
|
|
|
setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
|
2018-05-20 16:27:13 +00:00
|
|
|
|
2019-11-04 00:20:08 +00:00
|
|
|
if (IsInput())
|
|
|
|
{
|
|
|
|
setToolTip(
|
|
|
|
tr("Left-click to detect input.\nMiddle-click to clear.\nRight-click for more options."));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setToolTip(tr("Left/Right-click to configure output.\nMiddle-click to clear."));
|
|
|
|
}
|
2018-05-10 23:46:05 +00:00
|
|
|
|
2019-11-04 00:17:51 +00:00
|
|
|
connect(this, &MappingButton::clicked, this, &MappingButton::Clicked);
|
2018-02-06 10:00:23 +00:00
|
|
|
|
2019-03-15 01:27:49 +00:00
|
|
|
if (indicator)
|
|
|
|
connect(parent, &MappingWidget::Update, this, &MappingButton::UpdateIndicator);
|
2018-02-06 10:00:23 +00:00
|
|
|
|
2019-03-15 01:27:49 +00:00
|
|
|
connect(parent, &MappingWidget::ConfigChanged, this, &MappingButton::ConfigChanged);
|
2017-05-20 15:53:17 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 01:27:49 +00:00
|
|
|
void MappingButton::AdvancedPressed()
|
2017-05-20 15:53:17 +00:00
|
|
|
{
|
2019-01-06 23:15:00 +00:00
|
|
|
IOWindow io(m_parent, m_parent->GetController(), m_reference,
|
2019-03-15 01:27:49 +00:00
|
|
|
m_reference->IsInput() ? IOWindow::Type::Input : IOWindow::Type::Output);
|
|
|
|
io.exec();
|
|
|
|
|
|
|
|
ConfigChanged();
|
|
|
|
m_parent->SaveSettings();
|
2017-05-20 15:53:17 +00:00
|
|
|
}
|
|
|
|
|
2019-11-04 00:17:51 +00:00
|
|
|
void MappingButton::Clicked()
|
2017-05-20 15:53:17 +00:00
|
|
|
{
|
2019-03-15 01:27:49 +00:00
|
|
|
if (!m_reference->IsInput())
|
2019-11-04 00:17:51 +00:00
|
|
|
{
|
|
|
|
AdvancedPressed();
|
2017-05-20 15:53:17 +00:00
|
|
|
return;
|
2019-11-04 00:17:51 +00:00
|
|
|
}
|
2017-05-20 15:53:17 +00:00
|
|
|
|
2019-02-28 00:10:18 +00:00
|
|
|
const auto default_device_qualifier = m_parent->GetController()->GetDefaultDevice();
|
2018-05-23 21:47:42 +00:00
|
|
|
|
2019-02-28 00:10:18 +00:00
|
|
|
QString expression;
|
2017-05-20 15:53:17 +00:00
|
|
|
|
2019-02-28 00:10:18 +00:00
|
|
|
if (m_parent->GetParent()->IsMappingAllDevices())
|
|
|
|
{
|
|
|
|
expression = MappingCommon::DetectExpression(this, g_controller_interface,
|
|
|
|
g_controller_interface.GetAllDeviceStrings(),
|
|
|
|
default_device_qualifier);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
expression = MappingCommon::DetectExpression(this, g_controller_interface,
|
|
|
|
{default_device_qualifier.ToString()},
|
|
|
|
default_device_qualifier);
|
|
|
|
}
|
2017-06-14 00:11:52 +00:00
|
|
|
|
2019-02-28 00:10:18 +00:00
|
|
|
if (expression.isEmpty())
|
|
|
|
return;
|
2018-04-01 14:25:34 +00:00
|
|
|
|
2019-02-28 00:10:18 +00:00
|
|
|
m_reference->SetExpression(expression.toStdString());
|
2018-12-30 22:06:29 +00:00
|
|
|
m_parent->GetController()->UpdateSingleControlReference(g_controller_interface, m_reference);
|
2017-05-20 15:53:17 +00:00
|
|
|
|
2019-03-15 01:27:49 +00:00
|
|
|
ConfigChanged();
|
|
|
|
m_parent->SaveSettings();
|
2017-05-20 15:53:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MappingButton::Clear()
|
|
|
|
{
|
2018-05-02 16:29:05 +00:00
|
|
|
m_reference->range = 100.0 / SLIDER_TICK_COUNT;
|
2019-03-15 01:27:49 +00:00
|
|
|
|
|
|
|
m_reference->SetExpression("");
|
2018-12-30 22:06:29 +00:00
|
|
|
m_parent->GetController()->UpdateSingleControlReference(g_controller_interface, m_reference);
|
2019-03-15 01:27:49 +00:00
|
|
|
|
2017-11-03 20:28:45 +00:00
|
|
|
m_parent->SaveSettings();
|
2019-03-15 01:27:49 +00:00
|
|
|
ConfigChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MappingButton::UpdateIndicator()
|
|
|
|
{
|
|
|
|
if (!isActiveWindow())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QFont f = m_parent->font();
|
|
|
|
|
2021-05-09 20:52:45 +00:00
|
|
|
// If the input state is "true" (we can't know the state of outputs), show it in bold.
|
|
|
|
if (m_reference->IsInput() && m_reference->GetState<bool>())
|
2019-03-15 01:27:49 +00:00
|
|
|
f.setBold(true);
|
2021-05-09 20:52:45 +00:00
|
|
|
// If the expression has failed to parse, show it in italic.
|
|
|
|
// Some expressions still work even the failed to parse so don't prevent the GetState() above.
|
|
|
|
if (m_reference->GetParseStatus() == ciface::ExpressionParser::ParseStatus::SyntaxError)
|
|
|
|
f.setItalic(true);
|
2019-03-15 01:27:49 +00:00
|
|
|
|
|
|
|
setFont(f);
|
2017-05-20 15:53:17 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 01:27:49 +00:00
|
|
|
void MappingButton::ConfigChanged()
|
2017-05-20 15:53:17 +00:00
|
|
|
{
|
2021-05-09 20:52:45 +00:00
|
|
|
setText(RefToDisplayString(m_reference));
|
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
|
|
|
{
|
2020-12-16 13:44:25 +00:00
|
|
|
case Qt::MouseButton::MiddleButton:
|
2017-06-13 15:16:41 +00:00
|
|
|
Clear();
|
|
|
|
return;
|
|
|
|
case Qt::MouseButton::RightButton:
|
2019-03-15 01:27:49 +00:00
|
|
|
AdvancedPressed();
|
2017-06-13 15:16:41 +00:00
|
|
|
return;
|
|
|
|
default:
|
2019-11-04 00:17:51 +00:00
|
|
|
QPushButton::mouseReleaseEvent(event);
|
2017-06-13 15:16:41 +00:00
|
|
|
return;
|
2017-05-20 15:53:17 +00:00
|
|
|
}
|
|
|
|
}
|