Debian Codegraph @

Source/Core/DolphinQt/SkylanderPortal/SkylanderModifyDialog.cpp

raw symbols SCIP
1 // Copyright 2023 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "SkylanderModifyDialog.h"
5
6 #include <QBoxLayout>
7 #include <QCheckBox>
8 #include <QDateTimeEdit>
9 #include <QDialog>
10 #include <QDialogButtonBox>
11 #include <QLabel>
12 #include <QLineEdit>
13 #include <QMessageBox>
14 #include <QStringConverter>
15 #include <QValidator>
16
17 #include "Core/IOS/USB/Emulated/Skylanders/Skylander.h"
18 #include "Core/System.h"
19
20 #include "DolphinQt/QtUtils/QtUtils.h"
21
22 SkylanderModifyDialog::SkylanderModifyDialog(QWidget* parent, u8 slot)
23 : QDialog(parent), m_slot(slot)
24 {
25 bool should_show = true;
26
27 QVBoxLayout* layout = new QVBoxLayout;
28
29 IOS::HLE::USB::Skylander* skylander =
30 Core::System::GetInstance().GetSkylanderPortal().GetSkylander(slot);
31
32 m_figure = skylander->figure.get();
33 m_figure_data = m_figure->GetData();
34
35 auto* hbox_name = new QHBoxLayout;
36 QString name = QString();
37
38 if ((m_figure_data.skylander_data.nickname[0] != 0x00 &&
39 m_figure_data.normalized_type == IOS::HLE::USB::Type::Skylander))
40 {
41 name = QStringLiteral("\"%1\"").arg(QString::fromUtf16(
42 reinterpret_cast<char16_t*>(m_figure_data.skylander_data.nickname.data())));
43 }
44 else
45 {
46 auto found = IOS::HLE::USB::list_skylanders.find(
47 std::make_pair(m_figure_data.figure_id, m_figure_data.variant_id));
48 if (found != IOS::HLE::USB::list_skylanders.end())
49 {
50 name = QString::fromStdString(found->second.name);
51 }
52 else
53 {
54 // Should never be able to happen. Still good to have
55 name =
56 // i18n: "Var" is short for "variant"
57 tr("Unknown (Id:%1 Var:%2)").arg(m_figure_data.figure_id).arg(m_figure_data.variant_id);
58 }
59 }
60
61 // i18n: %1 is a name
62 auto* label_name = new QLabel(tr("Modifying Skylander: %1").arg(name));
63
64 hbox_name->addWidget(label_name);
65
66 layout->addLayout(hbox_name);
67
68 m_buttons = new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Cancel);
69
70 connect(m_buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
71
72 if (m_figure_data.normalized_type == IOS::HLE::USB::Type::Skylander)
73 {
74 PopulateSkylanderOptions(layout);
75 }
76 else if (m_figure_data.normalized_type == IOS::HLE::USB::Type::Trophy)
77 {
78 should_show &= PopulateTrophyOptions(layout);
79 }
80 else if (m_figure_data.normalized_type == IOS::HLE::USB::Type::Item)
81 {
82 should_show = false;
83 QMessageBox::warning(
84 this, tr("No data to modify!"),
85 tr("The type of this Skylander does not have any data that can be modified!"),
86 QMessageBox::Ok);
87 }
88 else if (m_figure_data.normalized_type == IOS::HLE::USB::Type::Unknown)
89 {
90 should_show = false;
91 QMessageBox::warning(this, tr("Unknown Skylander type!"),
92 tr("The type of this Skylander is unknown!"), QMessageBox::Ok);
93 }
94 else
95 {
96 should_show = false;
97 QMessageBox::warning(
98 this, tr("Unable to modify Skylander!"),
99 tr("The type of this Skylander is unknown, or can't be modified at this time!"),
100 QMessageBox::Ok);
101 QMessageBox::warning(this, tr("Can't be modified yet!"),
102 tr("This Skylander type can't be modified yet!"), QMessageBox::Ok);
103 }
104
105 layout->addWidget(m_buttons);
106
107 this->setLayout(layout);
108
109 if (should_show)
110 {
111 this->show();
112 this->raise();
113 }
114 }
115
116 void SkylanderModifyDialog::PopulateSkylanderOptions(QVBoxLayout* layout)
117 {
118 auto* hbox_toy_code = new QHBoxLayout();
119 auto* label_toy_code = new QLabel(tr("Toy code:"));
120 auto* edit_toy_code = new QLineEdit(QString::fromUtf8(m_figure_data.skylander_data.toy_code));
121 edit_toy_code->setDisabled(true);
122
123 auto* hbox_money = new QHBoxLayout();
124 auto* label_money = new QLabel(tr("Money:"));
125 auto* edit_money = new QLineEdit(QStringLiteral("%1").arg(m_figure_data.skylander_data.money));
126
127 auto* hbox_hero = new QHBoxLayout();
128 auto* label_hero = new QLabel(tr("Hero level:"));
129 auto* edit_hero =
130 new QLineEdit(QStringLiteral("%1").arg(m_figure_data.skylander_data.hero_level));
131
132 auto toUtf16 = QStringDecoder(QStringDecoder::Utf16);
133 auto* hbox_nick = new QHBoxLayout();
134 auto* label_nick = new QLabel(tr("Nickname:"));
135 auto* edit_nick = new QLineEdit(QString::fromUtf16(
136 reinterpret_cast<char16_t*>(m_figure_data.skylander_data.nickname.data())));
137
138 auto* hbox_playtime = new QHBoxLayout();
139 // i18n: The total amount of time the Skylander has been used for
140 auto* label_playtime = new QLabel(tr("Playtime:"));
141 auto* edit_playtime =
142 new QLineEdit(QStringLiteral("%1").arg(m_figure_data.skylander_data.playtime));
143
144 auto* hbox_last_reset = new QHBoxLayout();
145 // i18n: A timestamp for when the Skylander was most recently reset
146 auto* label_last_reset = new QLabel(tr("Last reset:"));
147 auto* edit_last_reset =
148 new QDateTimeEdit(QDateTime(QDate(m_figure_data.skylander_data.last_reset.year,
149 m_figure_data.skylander_data.last_reset.month,
150 m_figure_data.skylander_data.last_reset.day),
151 QTime(m_figure_data.skylander_data.last_reset.hour,
152 m_figure_data.skylander_data.last_reset.minute)));
153
154 auto* hbox_last_placed = new QHBoxLayout();
155 // i18n: A timestamp for when the Skylander was most recently used
156 auto* label_last_placed = new QLabel(tr("Last placed:"));
157 auto* edit_last_placed =
158 new QDateTimeEdit(QDateTime(QDate(m_figure_data.skylander_data.last_placed.year,
159 m_figure_data.skylander_data.last_placed.month,
160 m_figure_data.skylander_data.last_placed.day),
161 QTime(m_figure_data.skylander_data.last_placed.hour,
162 m_figure_data.skylander_data.last_placed.minute)));
163
164 edit_money->setValidator(new QIntValidator(0, 65000, this));
165 edit_hero->setValidator(new QIntValidator(0, 100, this));
166 edit_nick->setValidator(
167 new QRegularExpressionValidator(QRegularExpression(QStringLiteral("^\\p{L}{0,15}$")), this));
168 edit_playtime->setValidator(new QIntValidator(0, INT_MAX, this));
169
170 QtUtils::ShowFourDigitYear(edit_last_reset);
171 QtUtils::ShowFourDigitYear(edit_last_placed);
172
173 edit_toy_code->setToolTip(tr("The toy code for this figure. Only available for real figures."));
174 edit_money->setToolTip(tr("The amount of money this Skylander has. Between 0 and 65000"));
175 edit_hero->setToolTip(tr("The hero level of this Skylander. Only seen in Skylanders: Spyro's "
176 "Adventures. Between 0 and 100"));
177 edit_nick->setToolTip(tr("The nickname for this Skylander. Limited to 15 characters"));
178 edit_playtime->setToolTip(
179 tr("The total time this figure has been used inside a game in seconds"));
180 edit_last_reset->setToolTip(tr("The last time the figure has been reset. If the figure has never "
181 "been reset, the first time the figure was placed on a portal"));
182 edit_last_placed->setToolTip(tr("The last time the figure has been placed on a portal"));
183
184 hbox_toy_code->addWidget(label_toy_code);
185 hbox_toy_code->addWidget(edit_toy_code);
186
187 hbox_money->addWidget(label_money);
188 hbox_money->addWidget(edit_money);
189
190 hbox_hero->addWidget(label_hero);
191 hbox_hero->addWidget(edit_hero);
192
193 hbox_nick->addWidget(label_nick);
194 hbox_nick->addWidget(edit_nick);
195
196 hbox_playtime->addWidget(label_playtime);
197 hbox_playtime->addWidget(edit_playtime);
198
199 hbox_last_reset->addWidget(label_last_reset);
200 hbox_last_reset->addWidget(edit_last_reset);
201
202 hbox_last_placed->addWidget(label_last_placed);
203 hbox_last_placed->addWidget(edit_last_placed);
204
205 layout->addLayout(hbox_toy_code);
206 layout->addLayout(hbox_money);
207 layout->addLayout(hbox_hero);
208 layout->addLayout(hbox_nick);
209 layout->addLayout(hbox_playtime);
210 layout->addLayout(hbox_last_reset);
211 layout->addLayout(hbox_last_placed);
212
213 connect(m_buttons, &QDialogButtonBox::accepted, this, [=, this] {
214 if (!edit_money->hasAcceptableInput())
215 {
216 QMessageBox::warning(this, tr("Incorrect money value!"),
217 tr("Make sure that the money value is between 0 and 65000!"),
218 QMessageBox::Ok);
219 }
220 else if (!edit_hero->hasAcceptableInput())
221 {
222 QMessageBox::warning(this, tr("Incorrect hero level value!"),
223 tr("Make sure that the hero level value is between 0 and 100!"),
224 QMessageBox::Ok);
225 }
226 else if (!edit_nick->hasAcceptableInput())
227 {
228 QMessageBox::warning(this, tr("Incorrect nickname!"),
229 tr("Make sure that the nickname is between 0 and 15 characters long!"),
230 QMessageBox::Ok);
231 }
232 else if (!edit_playtime->hasAcceptableInput())
233 {
234 QMessageBox::warning(this, tr("Incorrect playtime value!"),
235 tr("Make sure that the playtime value is valid!"), QMessageBox::Ok);
236 }
237 else if (!edit_last_reset->hasAcceptableInput())
238 {
239 QMessageBox::warning(this, tr("Incorrect last reset time!"),
240 tr("Make sure that the last reset datetime value is valid!"),
241 QMessageBox::Ok);
242 }
243 else if (!edit_last_placed->hasAcceptableInput())
244 {
245 QMessageBox::warning(this, tr("Incorrect last placed time!"),
246 tr("Make sure that the last placed datetime value is valid!"),
247 QMessageBox::Ok);
248 }
249 else
250 {
251 m_allow_close = true;
252 m_figure_data.skylander_data = {
253 .money = edit_money->text().toUShort(),
254 .hero_level = edit_hero->text().toUShort(),
255 .playtime = edit_playtime->text().toUInt(),
256 .last_reset = {.minute = static_cast<u8>(edit_last_reset->time().minute()),
257 .hour = static_cast<u8>(edit_last_reset->time().hour()),
258 .day = static_cast<u8>(edit_last_reset->date().day()),
259 .month = static_cast<u8>(edit_last_reset->date().month()),
260 .year = static_cast<u16>(edit_last_reset->date().year())},
261 .last_placed = {.minute = static_cast<u8>(edit_last_placed->time().minute()),
262 .hour = static_cast<u8>(edit_last_placed->time().hour()),
263 .day = static_cast<u8>(edit_last_placed->date().day()),
264 .month = static_cast<u8>(edit_last_placed->date().month()),
265 .year = static_cast<u16>(edit_last_placed->date().year())}};
266
267 std::u16string nickname = edit_nick->text().toStdU16String();
268 nickname.copy(reinterpret_cast<char16_t*>(m_figure_data.skylander_data.nickname.data()),
269 nickname.length());
270
271 if (m_figure->FileIsOpen())
272 {
273 m_figure->SetData(&m_figure_data);
274 }
275 else
276 {
277 QMessageBox::warning(this, tr("Could not save your changes!"),
278 tr("The file associated to this file was closed! Did you clear the "
279 "slot before saving?"),
280 QMessageBox::Ok);
281 }
282
283 this->accept();
284 }
285 });
286 }
287
288 bool SkylanderModifyDialog::PopulateTrophyOptions(QVBoxLayout* layout)
289 {
290 static constexpr u16 KAOS_TROPHY_ID = 3503;
291 static constexpr u16 SEA_TROPHY_ID = 3502;
292
293 if (m_figure_data.figure_id == KAOS_TROPHY_ID)
294 {
295 QMessageBox::warning(this, tr("Can't edit villains for this trophy!"),
296 tr("Kaos is the only villain for this trophy and is always unlocked. No "
297 "need to edit anything!"),
298 QMessageBox::Ok);
299 return false;
300 }
301
302 constexpr size_t MAX_VILLAINS = 4;
303 std::array<int, MAX_VILLAINS> shift_distances;
304
305 if (m_figure_data.figure_id == SEA_TROPHY_ID)
306 shift_distances = {0, 1, 2, 4};
307 else
308 shift_distances = {0, 2, 3, 4};
309
310 std::array<QCheckBox*, MAX_VILLAINS> edit_villains;
311 for (size_t i = 0; i < MAX_VILLAINS; ++i)
312 {
313 edit_villains[i] = new QCheckBox();
314 edit_villains[i]->setChecked(static_cast<bool>(m_figure_data.trophy_data.unlocked_villains &
315 (0b1 << shift_distances[i])));
316 // i18n: "Captured" is a participle here. This string is used when listing villains, not when a
317 // villain was just captured
318 auto* const label = new QLabel(tr("Captured villain %1:").arg(i + 1));
319 auto* const hbox = new QHBoxLayout();
320 hbox->addWidget(label);
321 hbox->addWidget(edit_villains[i]);
322
323 layout->addLayout(hbox);
324 }
325
326 connect(m_buttons, &QDialogButtonBox::accepted, this, [=, this] {
327 m_figure_data.trophy_data.unlocked_villains = 0x0;
328 for (size_t i = 0; i < MAX_VILLAINS; ++i)
329 m_figure_data.trophy_data.unlocked_villains |=
330 edit_villains[i]->isChecked() ? (0b1 << shift_distances[i]) : 0b0;
331
332 m_figure->SetData(&m_figure_data);
333 m_allow_close = true;
334 this->accept();
335 });
336
337 return true;
338 }
339
340 void SkylanderModifyDialog::accept()
341 {
342 if (m_allow_close)
343 {
344 auto* skylander = Core::System::GetInstance().GetSkylanderPortal().GetSkylander(m_slot);
345 skylander->queued_status.push(IOS::HLE::USB::Skylander::REMOVED);
346 skylander->queued_status.push(IOS::HLE::USB::Skylander::ADDED);
347 skylander->queued_status.push(IOS::HLE::USB::Skylander::READY);
348 QDialog::accept();
349 }
350 }