1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
// Copyright 2017 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "DolphinQt/Config/Mapping/MappingButton.h"
#include <QApplication>
#include <QFontMetrics>
#include <QMouseEvent>
#include <QString>
#include "DolphinQt/Config/Mapping/IOWindow.h"
#include "DolphinQt/Config/Mapping/MappingCommon.h"
#include "DolphinQt/Config/Mapping/MappingWidget.h"
#include "DolphinQt/Config/Mapping/MappingWindow.h"
#include "DolphinQt/QtUtils/SetWindowDecorations.h"
#include "InputCommon/ControlReference/ControlReference.h"
#include "InputCommon/ControllerEmu/ControlGroup/Buttons.h"
#include "InputCommon/ControllerEmu/ControllerEmu.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h"
constexpr int SLIDER_TICK_COUNT = 100;
// Escape ampersands and simplify the text for a short preview
static QString RefToDisplayString(ControlReference* ref)
{
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));
}
else
{
controls[i].remove(QLatin1Char{' '});
}
}
}
// 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;
}
bool MappingButton::IsInput() const
{
return m_reference->IsInput();
}
MappingButton::MappingButton(MappingWidget* parent, ControlReference* ref, bool indicator)
: ElidedButton(RefToDisplayString(ref)), m_parent(parent), m_reference(ref)
{
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."));
}
connect(this, &MappingButton::clicked, this, &MappingButton::Clicked);
if (indicator)
connect(parent, &MappingWidget::Update, this, &MappingButton::UpdateIndicator);
connect(parent, &MappingWidget::ConfigChanged, this, &MappingButton::ConfigChanged);
}
void MappingButton::AdvancedPressed()
{
IOWindow io(m_parent, m_parent->GetController(), m_reference,
m_reference->IsInput() ? IOWindow::Type::Input : IOWindow::Type::Output);
SetQWidgetWindowDecorations(&io);
io.exec();
ConfigChanged();
m_parent->SaveSettings();
}
void MappingButton::Clicked()
{
if (!m_reference->IsInput())
{
AdvancedPressed();
return;
}
const auto default_device_qualifier = m_parent->GetController()->GetDefaultDevice();
QString expression;
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);
}
if (expression.isEmpty())
return;
m_reference->SetExpression(expression.toStdString());
m_parent->GetController()->UpdateSingleControlReference(g_controller_interface, m_reference);
ConfigChanged();
m_parent->SaveSettings();
}
void MappingButton::Clear()
{
m_reference->range = 100.0 / SLIDER_TICK_COUNT;
m_reference->SetExpression("");
m_parent->GetController()->UpdateSingleControlReference(g_controller_interface, m_reference);
m_parent->SaveSettings();
ConfigChanged();
}
void MappingButton::UpdateIndicator()
{
if (!isActiveWindow())
return;
QFont f = m_parent->font();
// 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>())
f.setBold(true);
// 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);
setFont(f);
}
void MappingButton::ConfigChanged()
{
setText(RefToDisplayString(m_reference));
}
void MappingButton::mouseReleaseEvent(QMouseEvent* event)
{
switch (event->button())
{
case Qt::MouseButton::MiddleButton:
Clear();
return;
case Qt::MouseButton::RightButton:
AdvancedPressed();
return;
default:
QPushButton::mouseReleaseEvent(event);
return;
}
}
|