Debian Codegraph @

Source/Core/DolphinQt/TAS/TASInputWindow.cpp

raw symbols SCIP
1 // Copyright 2018 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "DolphinQt/TAS/TASInputWindow.h"
5
6 #include <cmath>
7 #include <utility>
8
9 #include <QApplication>
10 #include <QCheckBox>
11 #include <QEvent>
12 #include <QGridLayout>
13 #include <QGroupBox>
14 #include <QLabel>
15 #include <QScrollArea>
16 #include <QShortcut>
17 #include <QSlider>
18 #include <QSpinBox>
19
20 #include "DolphinQt/Host.h"
21 #include "DolphinQt/QtUtils/AspectRatioWidget.h"
22 #include "DolphinQt/Resources.h"
23 #include "DolphinQt/TAS/StickWidget.h"
24 #include "DolphinQt/TAS/TASCheckBox.h"
25 #include "DolphinQt/TAS/TASSlider.h"
26 #include "DolphinQt/TAS/TASSpinBox.h"
27
28 #include "InputCommon/ControllerEmu/ControllerEmu.h"
29 #include "InputCommon/ControllerEmu/StickGate.h"
30
31 void InputOverrider::AddFunction(std::string_view group_name, std::string_view control_name,
32 OverrideFunction function)
33 {
34 m_functions.emplace(std::make_pair(group_name, control_name), std::move(function));
35 }
36
37 ControllerEmu::InputOverrideFunction InputOverrider::GetInputOverrideFunction() const
38 {
39 return [this](std::string_view group_name, std::string_view control_name,
40 ControlState controller_state) {
41 const auto it = m_functions.find(std::make_pair(group_name, control_name));
42 return it != m_functions.end() ? it->second(controller_state) : std::nullopt;
43 };
44 }
45
46 TASInputWindow::TASInputWindow(QWidget* parent) : QDialog(parent)
47 {
48 setWindowIcon(Resources::GetAppIcon());
49
50 QGridLayout* settings_layout = new QGridLayout;
51
52 m_use_controller = new QCheckBox(tr("Enable Controller Inpu&t"));
53 m_use_controller->setToolTip(tr("Warning: Analog inputs may reset to controller values at "
54 "random. In some cases this can be fixed by adding a deadzone."));
55 settings_layout->addWidget(m_use_controller, 0, 0, 1, 2);
56
57 QLabel* turbo_press_label = new QLabel(tr("Duration of Turbo Button Press (frames):"));
58 m_turbo_press_frames = new QSpinBox();
59 m_turbo_press_frames->setMinimum(1);
60 settings_layout->addWidget(turbo_press_label, 1, 0);
61 settings_layout->addWidget(m_turbo_press_frames, 1, 1);
62
63 QLabel* turbo_release_label = new QLabel(tr("Duration of Turbo Button Release (frames):"));
64 m_turbo_release_frames = new QSpinBox();
65 m_turbo_release_frames->setMinimum(1);
66 settings_layout->addWidget(turbo_release_label, 2, 0);
67 settings_layout->addWidget(m_turbo_release_frames, 2, 1);
68
69 m_settings_box = new QGroupBox(tr("Settings"));
70 m_settings_box->setLayout(settings_layout);
71 }
72
73 int TASInputWindow::GetTurboPressFrames() const
74 {
75 return m_turbo_press_frames->value();
76 }
77
78 int TASInputWindow::GetTurboReleaseFrames() const
79 {
80 return m_turbo_release_frames->value();
81 }
82
83 TASCheckBox* TASInputWindow::CreateButton(const QString& text, std::string_view group_name,
84 std::string_view control_name, InputOverrider* overrider)
85 {
86 TASCheckBox* checkbox = new TASCheckBox(text, this);
87
88 overrider->AddFunction(group_name, control_name, [this, checkbox](ControlState controller_state) {
89 return GetButton(checkbox, controller_state);
90 });
91
92 return checkbox;
93 }
94
95 QGroupBox* TASInputWindow::CreateStickInputs(const QString& text, std::string_view group_name,
96 InputOverrider* overrider, int min_x, int min_y,
97 int max_x, int max_y, Qt::Key x_shortcut_key,
98 Qt::Key y_shortcut_key)
99 {
100 const QKeySequence x_shortcut_key_sequence = QKeySequence(Qt::ALT | x_shortcut_key);
101 const QKeySequence y_shortcut_key_sequence = QKeySequence(Qt::ALT | y_shortcut_key);
102
103 auto* box =
104 new QGroupBox(QStringLiteral("%1 (%2/%3)")
105 .arg(text, x_shortcut_key_sequence.toString(QKeySequence::NativeText),
106 y_shortcut_key_sequence.toString(QKeySequence::NativeText)));
107
108 const int x_default = static_cast<int>(std::round(max_x / 2.));
109 const int y_default = static_cast<int>(std::round(max_y / 2.));
110
111 auto* box_layout = new QGridLayout;
112 TASSpinBox* x_value = CreateSliderValuePair(box_layout, x_default, max_x, x_shortcut_key_sequence,
113 Qt::Horizontal, box);
114
115 TASSpinBox* y_value = CreateSliderValuePair(box_layout, y_default, max_y, y_shortcut_key_sequence,
116 Qt::Vertical, box);
117
118 auto* visual = new StickWidget(this, max_x, max_y);
119 visual->SetX(x_default);
120 visual->SetY(y_default);
121
122 connect(x_value, &QSpinBox::valueChanged, visual, &StickWidget::SetX);
123 connect(y_value, &QSpinBox::valueChanged, visual, &StickWidget::SetY);
124 connect(visual, &StickWidget::ChangedX, x_value, &QSpinBox::setValue);
125 connect(visual, &StickWidget::ChangedY, y_value, &QSpinBox::setValue);
126
127 auto* visual_ar = new AspectRatioWidget(visual, max_x, max_y);
128
129 // This is done to prevent the stick widget from stretching
130 box_layout->addItem(new QSpacerItem(0, 0), 2, 1);
131 box_layout->addWidget(visual_ar, 1, 1);
132 box->setLayout(box_layout);
133
134 overrider->AddFunction(group_name, ControllerEmu::ReshapableInput::X_INPUT_OVERRIDE,
135 [this, x_value, x_default, min_x, max_x](ControlState controller_state) {
136 return GetSpinBox(x_value, x_default, min_x, max_x, controller_state);
137 });
138
139 overrider->AddFunction(group_name, ControllerEmu::ReshapableInput::Y_INPUT_OVERRIDE,
140 [this, y_value, y_default, min_y, max_y](ControlState controller_state) {
141 return GetSpinBox(y_value, y_default, min_y, max_y, controller_state);
142 });
143
144 return box;
145 }
146
147 QGridLayout* TASInputWindow::CreateSliderValuePairLayout(
148 const QString& text, std::string_view group_name, std::string_view control_name,
149 InputOverrider* overrider, int zero, int default_, int min, int max, Qt::Key shortcut_key,
150 QWidget* shortcut_widget, std::optional<ControlState> scale)
151 {
152 const QKeySequence shortcut_key_sequence = QKeySequence(Qt::ALT | shortcut_key);
153
154 auto* label = new QLabel(QStringLiteral("%1 (%2)").arg(
155 text, shortcut_key_sequence.toString(QKeySequence::NativeText)));
156
157 QGridLayout* layout = new QGridLayout;
158 layout->addWidget(label, 0, 0);
159
160 CreateSliderValuePair(group_name, control_name, overrider, layout, zero, default_, min, max,
161 shortcut_key_sequence, Qt::Horizontal, shortcut_widget, scale);
162
163 return layout;
164 }
165
166 TASSpinBox* TASInputWindow::CreateSliderValuePair(
167 std::string_view group_name, std::string_view control_name, InputOverrider* overrider,
168 QGridLayout* layout, int zero, int default_, int min, int max,
169 const QKeySequence& shortcut_key_sequence, Qt::Orientation orientation,
170 QWidget* shortcut_widget, std::optional<ControlState> scale)
171 {
172 TASSpinBox* value = CreateSliderValuePair(layout, default_, max, shortcut_key_sequence,
173 orientation, shortcut_widget);
174
175 InputOverrider::OverrideFunction func;
176 if (scale)
177 {
178 func = [this, value, zero, scale](ControlState controller_state) {
179 return GetSpinBox(value, zero, controller_state, *scale);
180 };
181 }
182 else
183 {
184 func = [this, value, zero, min, max](ControlState controller_state) {
185 return GetSpinBox(value, zero, min, max, controller_state);
186 };
187 }
188
189 overrider->AddFunction(group_name, control_name, std::move(func));
190
191 return value;
192 }
193
194 // The shortcut_widget argument needs to specify the container widget that will be hidden/shown.
195 // This is done to avoid ambiguous shortcuts
196 TASSpinBox* TASInputWindow::CreateSliderValuePair(QGridLayout* layout, int default_, int max,
197 const QKeySequence& shortcut_key_sequence,
198 Qt::Orientation orientation,
199 QWidget* shortcut_widget)
200 {
201 auto* value = new TASSpinBox();
202 value->setRange(0, max);
203 value->setValue(default_);
204
205 auto* slider = new TASSlider(default_, orientation);
206 slider->setRange(0, max);
207 slider->setValue(default_);
208 slider->setFocusPolicy(Qt::ClickFocus);
209
210 value->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
211
212 connect(slider, &QSlider::valueChanged, value, &QSpinBox::setValue);
213 connect(value, &QSpinBox::valueChanged, slider, &QSlider::setValue);
214
215 auto* shortcut = new QShortcut(shortcut_key_sequence, shortcut_widget);
216 connect(shortcut, &QShortcut::activated, [value] {
217 value->setFocus();
218 value->selectAll();
219 });
220
221 if (orientation == Qt::Vertical)
222 {
223 layout->addWidget(slider, 1, 2);
224 layout->addWidget(value, 2, 2);
225
226 layout->setAlignment(slider, Qt::AlignHCenter);
227 slider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
228 }
229 else
230 {
231 layout->addWidget(slider, 0, 1);
232 layout->addWidget(value, 0, 2);
233
234 slider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
235 }
236
237 return value;
238 }
239
240 void TASInputWindow::SetupScrollArea(QLayout* layout)
241 {
242 m_scroll_widget = new QWidget;
243 m_scroll_widget->setLayout(layout);
244
245 auto* scroll_area = new QScrollArea;
246 scroll_area->setWidget(m_scroll_widget);
247 scroll_area->setWidgetResizable(true);
248 scroll_area->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
249 scroll_area->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
250
251 auto* outer_layout = new QVBoxLayout;
252 outer_layout->setContentsMargins(0, 0, 0, 0);
253 outer_layout->addWidget(scroll_area);
254 setLayout(outer_layout);
255
256 layout->activate();
257 m_scroll_widget->layout()->activate();
258 }
259
260 std::optional<ControlState> TASInputWindow::GetButton(TASCheckBox* checkbox,
261 ControlState controller_state)
262 {
263 const bool pressed = std::llround(controller_state) > 0;
264 if (m_use_controller->isChecked())
265 checkbox->OnControllerValueChanged(pressed);
266
267 return checkbox->GetValue() ? 1.0 : 0.0;
268 }
269
270 std::optional<ControlState> TASInputWindow::GetSpinBox(TASSpinBox* spin, int zero, int min, int max,
271 ControlState controller_state)
272 {
273 const int controller_value = ControllerEmu::MapFloat<int>(controller_state, zero, 0, max);
274
275 if (m_use_controller->isChecked())
276 spin->OnControllerValueChanged(controller_value);
277
278 return ControllerEmu::MapToFloat<ControlState, int>(spin->GetValue(), zero, min, max);
279 }
280
281 std::optional<ControlState> TASInputWindow::GetSpinBox(TASSpinBox* spin, int zero,
282 ControlState controller_state,
283 ControlState scale)
284 {
285 const int controller_value = static_cast<int>(std::llround(controller_state * scale + zero));
286
287 if (m_use_controller->isChecked())
288 spin->OnControllerValueChanged(controller_value);
289
290 return (spin->GetValue() - zero) / scale;
291 }
292
293 void TASInputWindow::changeEvent(QEvent* const event)
294 {
295 if (event->type() == QEvent::ActivationChange)
296 {
297 const bool active_window_is_tas_input =
298 qobject_cast<TASInputWindow*>(QApplication::activeWindow()) != nullptr;
299
300 // Switching between TAS Input windows will call SetTASInputFocus(true) twice, but that's fine.
301 Host::GetInstance()->SetTASInputFocus(active_window_is_tas_input);
302 }
303 QDialog::changeEvent(event);
304 }