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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
// Copyright 2018 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "DolphinQt/TAS/TASInputWindow.h"
#include <cmath>
#include <utility>
#include <QApplication>
#include <QCheckBox>
#include <QEvent>
#include <QGridLayout>
#include <QGroupBox>
#include <QLabel>
#include <QScrollArea>
#include <QShortcut>
#include <QSlider>
#include <QSpinBox>
#include "DolphinQt/Host.h"
#include "DolphinQt/QtUtils/AspectRatioWidget.h"
#include "DolphinQt/Resources.h"
#include "DolphinQt/TAS/StickWidget.h"
#include "DolphinQt/TAS/TASCheckBox.h"
#include "DolphinQt/TAS/TASSlider.h"
#include "DolphinQt/TAS/TASSpinBox.h"
#include "InputCommon/ControllerEmu/ControllerEmu.h"
#include "InputCommon/ControllerEmu/StickGate.h"
void InputOverrider::AddFunction(std::string_view group_name, std::string_view control_name,
OverrideFunction function)
{
m_functions.emplace(std::make_pair(group_name, control_name), std::move(function));
}
ControllerEmu::InputOverrideFunction InputOverrider::GetInputOverrideFunction() const
{
return [this](std::string_view group_name, std::string_view control_name,
ControlState controller_state) {
const auto it = m_functions.find(std::make_pair(group_name, control_name));
return it != m_functions.end() ? it->second(controller_state) : std::nullopt;
};
}
TASInputWindow::TASInputWindow(QWidget* parent) : QDialog(parent)
{
setWindowIcon(Resources::GetAppIcon());
QGridLayout* settings_layout = new QGridLayout;
m_use_controller = new QCheckBox(tr("Enable Controller Inpu&t"));
m_use_controller->setToolTip(tr("Warning: Analog inputs may reset to controller values at "
"random. In some cases this can be fixed by adding a deadzone."));
settings_layout->addWidget(m_use_controller, 0, 0, 1, 2);
QLabel* turbo_press_label = new QLabel(tr("Duration of Turbo Button Press (frames):"));
m_turbo_press_frames = new QSpinBox();
m_turbo_press_frames->setMinimum(1);
settings_layout->addWidget(turbo_press_label, 1, 0);
settings_layout->addWidget(m_turbo_press_frames, 1, 1);
QLabel* turbo_release_label = new QLabel(tr("Duration of Turbo Button Release (frames):"));
m_turbo_release_frames = new QSpinBox();
m_turbo_release_frames->setMinimum(1);
settings_layout->addWidget(turbo_release_label, 2, 0);
settings_layout->addWidget(m_turbo_release_frames, 2, 1);
m_settings_box = new QGroupBox(tr("Settings"));
m_settings_box->setLayout(settings_layout);
}
int TASInputWindow::GetTurboPressFrames() const
{
return m_turbo_press_frames->value();
}
int TASInputWindow::GetTurboReleaseFrames() const
{
return m_turbo_release_frames->value();
}
TASCheckBox* TASInputWindow::CreateButton(const QString& text, std::string_view group_name,
std::string_view control_name, InputOverrider* overrider)
{
TASCheckBox* checkbox = new TASCheckBox(text, this);
overrider->AddFunction(group_name, control_name, [this, checkbox](ControlState controller_state) {
return GetButton(checkbox, controller_state);
});
return checkbox;
}
QGroupBox* TASInputWindow::CreateStickInputs(const QString& text, std::string_view group_name,
InputOverrider* overrider, int min_x, int min_y,
int max_x, int max_y, Qt::Key x_shortcut_key,
Qt::Key y_shortcut_key)
{
const QKeySequence x_shortcut_key_sequence = QKeySequence(Qt::ALT | x_shortcut_key);
const QKeySequence y_shortcut_key_sequence = QKeySequence(Qt::ALT | y_shortcut_key);
auto* box =
new QGroupBox(QStringLiteral("%1 (%2/%3)")
.arg(text, x_shortcut_key_sequence.toString(QKeySequence::NativeText),
y_shortcut_key_sequence.toString(QKeySequence::NativeText)));
const int x_default = static_cast<int>(std::round(max_x / 2.));
const int y_default = static_cast<int>(std::round(max_y / 2.));
auto* box_layout = new QGridLayout;
TASSpinBox* x_value = CreateSliderValuePair(box_layout, x_default, max_x, x_shortcut_key_sequence,
Qt::Horizontal, box);
TASSpinBox* y_value = CreateSliderValuePair(box_layout, y_default, max_y, y_shortcut_key_sequence,
Qt::Vertical, box);
auto* visual = new StickWidget(this, max_x, max_y);
visual->SetX(x_default);
visual->SetY(y_default);
connect(x_value, &QSpinBox::valueChanged, visual, &StickWidget::SetX);
connect(y_value, &QSpinBox::valueChanged, visual, &StickWidget::SetY);
connect(visual, &StickWidget::ChangedX, x_value, &QSpinBox::setValue);
connect(visual, &StickWidget::ChangedY, y_value, &QSpinBox::setValue);
auto* visual_ar = new AspectRatioWidget(visual, max_x, max_y);
// This is done to prevent the stick widget from stretching
box_layout->addItem(new QSpacerItem(0, 0), 2, 1);
box_layout->addWidget(visual_ar, 1, 1);
box->setLayout(box_layout);
overrider->AddFunction(group_name, ControllerEmu::ReshapableInput::X_INPUT_OVERRIDE,
[this, x_value, x_default, min_x, max_x](ControlState controller_state) {
return GetSpinBox(x_value, x_default, min_x, max_x, controller_state);
});
overrider->AddFunction(group_name, ControllerEmu::ReshapableInput::Y_INPUT_OVERRIDE,
[this, y_value, y_default, min_y, max_y](ControlState controller_state) {
return GetSpinBox(y_value, y_default, min_y, max_y, controller_state);
});
return box;
}
QGridLayout* TASInputWindow::CreateSliderValuePairLayout(
const QString& text, std::string_view group_name, std::string_view control_name,
InputOverrider* overrider, int zero, int default_, int min, int max, Qt::Key shortcut_key,
QWidget* shortcut_widget, std::optional<ControlState> scale)
{
const QKeySequence shortcut_key_sequence = QKeySequence(Qt::ALT | shortcut_key);
auto* label = new QLabel(QStringLiteral("%1 (%2)").arg(
text, shortcut_key_sequence.toString(QKeySequence::NativeText)));
QGridLayout* layout = new QGridLayout;
layout->addWidget(label, 0, 0);
CreateSliderValuePair(group_name, control_name, overrider, layout, zero, default_, min, max,
shortcut_key_sequence, Qt::Horizontal, shortcut_widget, scale);
return layout;
}
TASSpinBox* TASInputWindow::CreateSliderValuePair(
std::string_view group_name, std::string_view control_name, InputOverrider* overrider,
QGridLayout* layout, int zero, int default_, int min, int max,
const QKeySequence& shortcut_key_sequence, Qt::Orientation orientation,
QWidget* shortcut_widget, std::optional<ControlState> scale)
{
TASSpinBox* value = CreateSliderValuePair(layout, default_, max, shortcut_key_sequence,
orientation, shortcut_widget);
InputOverrider::OverrideFunction func;
if (scale)
{
func = [this, value, zero, scale](ControlState controller_state) {
return GetSpinBox(value, zero, controller_state, *scale);
};
}
else
{
func = [this, value, zero, min, max](ControlState controller_state) {
return GetSpinBox(value, zero, min, max, controller_state);
};
}
overrider->AddFunction(group_name, control_name, std::move(func));
return value;
}
// The shortcut_widget argument needs to specify the container widget that will be hidden/shown.
// This is done to avoid ambiguous shortcuts
TASSpinBox* TASInputWindow::CreateSliderValuePair(QGridLayout* layout, int default_, int max,
const QKeySequence& shortcut_key_sequence,
Qt::Orientation orientation,
QWidget* shortcut_widget)
{
auto* value = new TASSpinBox();
value->setRange(0, max);
value->setValue(default_);
auto* slider = new TASSlider(default_, orientation);
slider->setRange(0, max);
slider->setValue(default_);
slider->setFocusPolicy(Qt::ClickFocus);
value->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
connect(slider, &QSlider::valueChanged, value, &QSpinBox::setValue);
connect(value, &QSpinBox::valueChanged, slider, &QSlider::setValue);
auto* shortcut = new QShortcut(shortcut_key_sequence, shortcut_widget);
connect(shortcut, &QShortcut::activated, [value] {
value->setFocus();
value->selectAll();
});
if (orientation == Qt::Vertical)
{
layout->addWidget(slider, 1, 2);
layout->addWidget(value, 2, 2);
layout->setAlignment(slider, Qt::AlignHCenter);
slider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
}
else
{
layout->addWidget(slider, 0, 1);
layout->addWidget(value, 0, 2);
slider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
}
return value;
}
void TASInputWindow::SetupScrollArea(QLayout* layout)
{
m_scroll_widget = new QWidget;
m_scroll_widget->setLayout(layout);
auto* scroll_area = new QScrollArea;
scroll_area->setWidget(m_scroll_widget);
scroll_area->setWidgetResizable(true);
scroll_area->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
scroll_area->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
auto* outer_layout = new QVBoxLayout;
outer_layout->setContentsMargins(0, 0, 0, 0);
outer_layout->addWidget(scroll_area);
setLayout(outer_layout);
layout->activate();
m_scroll_widget->layout()->activate();
}
std::optional<ControlState> TASInputWindow::GetButton(TASCheckBox* checkbox,
ControlState controller_state)
{
const bool pressed = std::llround(controller_state) > 0;
if (m_use_controller->isChecked())
checkbox->OnControllerValueChanged(pressed);
return checkbox->GetValue() ? 1.0 : 0.0;
}
std::optional<ControlState> TASInputWindow::GetSpinBox(TASSpinBox* spin, int zero, int min, int max,
ControlState controller_state)
{
const int controller_value = ControllerEmu::MapFloat<int>(controller_state, zero, 0, max);
if (m_use_controller->isChecked())
spin->OnControllerValueChanged(controller_value);
return ControllerEmu::MapToFloat<ControlState, int>(spin->GetValue(), zero, min, max);
}
std::optional<ControlState> TASInputWindow::GetSpinBox(TASSpinBox* spin, int zero,
ControlState controller_state,
ControlState scale)
{
const int controller_value = static_cast<int>(std::llround(controller_state * scale + zero));
if (m_use_controller->isChecked())
spin->OnControllerValueChanged(controller_value);
return (spin->GetValue() - zero) / scale;
}
void TASInputWindow::changeEvent(QEvent* const event)
{
if (event->type() == QEvent::ActivationChange)
{
const bool active_window_is_tas_input =
qobject_cast<TASInputWindow*>(QApplication::activeWindow()) != nullptr;
// Switching between TAS Input windows will call SetTASInputFocus(true) twice, but that's fine.
Host::GetInstance()->SetTASInputFocus(active_window_is_tas_input);
}
QDialog::changeEvent(event);
}
|