Debian Codegraph @

Source/Core/DolphinQt/SkylanderPortal/SkylanderPortalWindow.cpp

raw symbols SCIP
1 // Copyright 2022 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 // DolphinQt code copied and modified for Dolphin from the RPCS3 Qt utility for Creating, Loading
5 // and Clearing Skylanders
6
7 #include "DolphinQt/SkylanderPortal/SkylanderPortalWindow.h"
8
9 #include <QCheckBox>
10 #include <QComboBox>
11 #include <QCompleter>
12 #include <QDialogButtonBox>
13 #include <QEvent>
14 #include <QGroupBox>
15 #include <QKeyEvent>
16 #include <QKeySequence>
17 #include <QLabel>
18 #include <QLineEdit>
19 #include <QListWidget>
20 #include <QMessageBox>
21 #include <QPushButton>
22 #include <QScrollArea>
23 #include <QStackedWidget>
24 #include <QString>
25 #include <QStringConverter>
26 #include <QStringList>
27 #include <QThread>
28 #include <QVBoxLayout>
29
30 #include "Common/IOFile.h"
31
32 #include "Common/FileUtil.h"
33 #include "Core/Config/MainSettings.h"
34 #include "Core/IOS/USB/Emulated/Skylanders/Skylander.h"
35 #include "Core/System.h"
36
37 #include "DolphinQt/QtUtils/DolphinFileDialog.h"
38 #include "DolphinQt/Resources.h"
39 #include "DolphinQt/Settings.h"
40 #include "SkylanderModifyDialog.h"
41
42 SkylanderPortalWindow::SkylanderPortalWindow(QWidget* parent) : QWidget(parent)
43 {
44 setWindowTitle(tr("Skylanders Manager"));
45 setWindowIcon(Resources::GetAppIcon());
46 setObjectName(QStringLiteral("skylanders_manager"));
47 setMinimumSize(QSize(650, 500));
48
49 m_only_show_collection = new QCheckBox(tr("Only Show Collection"));
50
51 CreateMainWindow();
52
53 connect(&Settings::Instance(), &Settings::EmulationStateChanged, this,
54 &SkylanderPortalWindow::OnEmulationStateChanged);
55
56 installEventFilter(this);
57
58 OnEmulationStateChanged(Core::GetState(Core::System::GetInstance()));
59
60 connect(m_skylander_list, &QListWidget::itemSelectionChanged, this,
61 &SkylanderPortalWindow::UpdateCurrentIDs);
62
63 QDir skylanders_folder;
64 // skylanders folder in user directory
65 QString user_path =
66 QString::fromStdString(File::GetUserPath(D_USER_IDX)) + QStringLiteral("Skylanders");
67 // first time initialize path in config
68 if (Config::Get(Config::MAIN_SKYLANDERS_PATH).empty())
69 {
70 Config::SetBase(Config::MAIN_SKYLANDERS_PATH, user_path.toStdString());
71 skylanders_folder = QDir(user_path);
72 }
73 else
74 {
75 skylanders_folder = QDir(QString::fromStdString(Config::Get(Config::MAIN_SKYLANDERS_PATH)));
76 }
77 if (!skylanders_folder.exists())
78 skylanders_folder.mkdir(skylanders_folder.path());
79
80 m_collection_path = QDir::toNativeSeparators(skylanders_folder.path()) + QDir::separator();
81 m_last_skylander_path = m_collection_path;
82 m_path_edit->setText(m_collection_path);
83 }
84
85 SkylanderPortalWindow::~SkylanderPortalWindow() = default;
86
87 // Window creation
88 void SkylanderPortalWindow::CreateMainWindow()
89 {
90 auto* main_layout = new QVBoxLayout();
91
92 auto* select_layout = new QHBoxLayout;
93
94 setMinimumWidth(770);
95
96 // yes, that +1 on 755 is needed to avoid scroll bars on the element selection
97 setMinimumHeight(756);
98
99 // left and right widgets within window separated into own functions for readability
100 select_layout->addLayout(CreateSlotLayout());
101 select_layout->addLayout(CreateFinderLayout());
102 select_layout->setStretch(0, 1);
103 select_layout->setStretch(1, 2);
104
105 main_layout->addLayout(select_layout);
106
107 // create command buttons at bottom of window
108 QBoxLayout* command_layout = new QHBoxLayout;
109 m_command_buttons = new QFrame();
110 command_layout->setAlignment(Qt::AlignCenter);
111 auto* create_btn = new QPushButton(tr("Customize"));
112 auto* load_file_btn = new QPushButton(tr("Load File"));
113 auto* clear_btn = new QPushButton(tr("Clear Slot"));
114 auto* load_btn = new QPushButton(tr("Load Slot"));
115 auto* modify_btn = new QPushButton(tr("Modify Slot"));
116 connect(create_btn, &QAbstractButton::clicked, this,
117 &SkylanderPortalWindow::CreateSkylanderAdvanced);
118 connect(clear_btn, &QAbstractButton::clicked, this, [this] { ClearSlot(GetCurrentSlot()); });
119 connect(load_btn, &QAbstractButton::clicked, this, &SkylanderPortalWindow::LoadSelected);
120 connect(load_file_btn, &QAbstractButton::clicked, this, &SkylanderPortalWindow::LoadFromFile);
121 connect(modify_btn, &QAbstractButton::clicked, this, &SkylanderPortalWindow::ModifySkylander);
122 command_layout->addWidget(create_btn);
123 command_layout->addWidget(load_file_btn);
124 command_layout->addWidget(clear_btn);
125 command_layout->addWidget(load_btn);
126 command_layout->addWidget(modify_btn);
127 m_command_buttons->setLayout(command_layout);
128 main_layout->addWidget(m_command_buttons);
129
130 setLayout(main_layout);
131
132 RefreshList();
133 m_skylander_list->setCurrentItem(m_skylander_list->item(0), QItemSelectionModel::Select);
134 UpdateSlotNames();
135 }
136
137 QVBoxLayout* SkylanderPortalWindow::CreateSlotLayout()
138 {
139 auto* slot_layout = new QVBoxLayout();
140
141 // WIDGET: Portal Enable Checkbox
142 auto* checkbox_layout = new QVBoxLayout();
143 m_enabled_checkbox = new QCheckBox(tr("Emulate Skylander Portal"), this);
144 m_enabled_checkbox->setChecked(Config::Get(Config::MAIN_EMULATE_SKYLANDER_PORTAL));
145 m_emulating = Config::Get(Config::MAIN_EMULATE_SKYLANDER_PORTAL);
146 connect(m_enabled_checkbox, &QCheckBox::toggled, this, &SkylanderPortalWindow::EmulatePortal);
147 checkbox_layout->addWidget(m_enabled_checkbox);
148 slot_layout->addLayout(checkbox_layout);
149
150 // WIDGET: Portal Slots
151 // Helper function for creating slots
152 auto add_line = [](QVBoxLayout* vbox) {
153 auto* line = new QFrame();
154 line->setFrameShape(QFrame::HLine);
155 line->setFrameShadow(QFrame::Sunken);
156 vbox->addWidget(line);
157 };
158
159 // Create portal slots
160 auto* portal_slots_group = new QGroupBox(tr("Portal Slots"));
161 auto* portal_slots_layout = new QVBoxLayout();
162 m_group_skylanders = new QFrame();
163 auto* vbox_group = new QVBoxLayout();
164 auto* scroll_area = new QScrollArea();
165
166 for (auto i = 0; i < MAX_SKYLANDERS; i++)
167 {
168 if (i != 0)
169 {
170 add_line(vbox_group);
171 }
172
173 auto* hbox_skylander = new QHBoxLayout();
174 auto* label_skyname = new QLabel(tr("Skylander %1").arg(i + 1));
175 m_edit_skylanders[i] = new QLineEdit();
176 m_edit_skylanders[i]->setEnabled(false);
177
178 QRadioButton* button = new QRadioButton;
179 m_slot_radios[i] = button;
180 button->setProperty("id", i);
181 hbox_skylander->addWidget(button);
182 hbox_skylander->addWidget(label_skyname);
183 hbox_skylander->addWidget(m_edit_skylanders[i]);
184
185 vbox_group->addLayout(hbox_skylander);
186 }
187 m_slot_radios[0]->setChecked(true);
188
189 m_group_skylanders->setLayout(vbox_group);
190 m_group_skylanders->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
191 scroll_area->setWidget(m_group_skylanders);
192 scroll_area->setWidgetResizable(true);
193 scroll_area->setFrameStyle(QFrame::NoFrame);
194 m_group_skylanders->setVisible(Config::Get(Config::MAIN_EMULATE_SKYLANDER_PORTAL));
195 portal_slots_layout->addWidget(scroll_area);
196 portal_slots_group->setLayout(portal_slots_layout);
197 slot_layout->addWidget(portal_slots_group);
198
199 return slot_layout;
200 }
201
202 QVBoxLayout* SkylanderPortalWindow::CreateFinderLayout()
203 {
204 auto* main_layout = new QVBoxLayout();
205
206 // WIDGET: Skylander List
207 m_skylander_list = new QListWidget;
208 connect(m_skylander_list, &QListWidget::itemDoubleClicked, this,
209 &SkylanderPortalWindow::LoadSelected);
210
211 auto* collection_layout = new QHBoxLayout();
212
213 // WIDGET: Skylander Collection Path
214 collection_layout->addWidget(new QLabel(tr("Skylander Collection Path:")));
215 m_path_edit = new QLineEdit;
216 collection_layout->addWidget(m_path_edit);
217 m_path_select = new QPushButton(tr("Choose"));
218 connect(m_path_edit, &QLineEdit::editingFinished, this,
219 &SkylanderPortalWindow::OnCollectionPathChanged);
220 connect(m_path_select, &QAbstractButton::clicked, this,
221 &SkylanderPortalWindow::SelectCollectionPath);
222 collection_layout->addWidget(m_path_select);
223
224 main_layout->addLayout(collection_layout);
225
226 auto* finder_layout = new QVBoxLayout();
227
228 // WIDGET: Search Bar
229 auto* search_bar_layout = new QHBoxLayout;
230 m_sky_search = new QLineEdit;
231 m_sky_search->setClearButtonEnabled(true);
232 connect(m_sky_search, &QLineEdit::textChanged, this, &SkylanderPortalWindow::RefreshList);
233 search_bar_layout->addWidget(new QLabel(tr("Search:")));
234 search_bar_layout->addWidget(m_sky_search);
235 finder_layout->addLayout(search_bar_layout);
236
237 auto* finder_list_layout = new QHBoxLayout();
238
239 auto* search_filters_layout = new QVBoxLayout();
240
241 // WIDGET: Filter by Game
242 auto* search_game_group = new QGroupBox(tr("Game"));
243 auto* search_game_layout = new QVBoxLayout();
244 auto* search_checkbox_scroll_area = new QScrollArea();
245 search_checkbox_scroll_area->setContentsMargins(0, 0, 0, 0);
246 search_checkbox_scroll_area->setFrameStyle(QFrame::NoFrame);
247 auto* search_checkbox_group = new QFrame();
248 search_checkbox_group->setContentsMargins(0, 0, 0, 0);
249 auto* search_checkbox_layout = new QVBoxLayout();
250
251 for (size_t i = 0; i < m_game_filters.size(); ++i)
252 {
253 QCheckBox* checkbox = new QCheckBox(this);
254 checkbox->setChecked(true);
255 connect(checkbox, &QCheckBox::toggled, this, &SkylanderPortalWindow::RefreshList);
256 m_game_filters[i] = checkbox;
257 search_checkbox_layout->addWidget(checkbox);
258 }
259 // i18n: Figures for the game Skylanders: Spyro's Adventure. The game has the same title in all
260 // countries it was released in, except Japan, where it's named スカイランダーズ スパイロの大冒険.
261 m_game_filters[GetGameID(Game::SpyrosAdv)]->setText(tr("Spyro's Adventure"));
262 // i18n: Figures for the game Skylanders: Giants. The game has the same title in all countries
263 // it was released in. It was not released in Japan.
264 m_game_filters[GetGameID(Game::Giants)]->setText(tr("Giants"));
265 // i18n: Figures for the game Skylanders: Swap Force. The game has the same title in all countries
266 // it was released in. It was not released in Japan.
267 m_game_filters[GetGameID(Game::SwapForce)]->setText(tr("Swap Force"));
268 // i18n: Figures for the game Skylanders: Trap Team. The game has the same title in all countries
269 // it was released in. It was not released in Japan.
270 m_game_filters[GetGameID(Game::TrapTeam)]->setText(tr("Trap Team"));
271 // i18n: Figures for the games Skylanders: SuperChargers (not available for the Wii) and
272 // Skylanders: SuperChargers Racing (available for the Wii). The games have the same titles in
273 // all countries they were released in. They were not released in Japan.
274 m_game_filters[GetGameID(Game::Superchargers)]->setText(tr("SuperChargers"));
275 search_checkbox_group->setLayout(search_checkbox_layout);
276 search_checkbox_scroll_area->setWidget(search_checkbox_group);
277 search_game_layout->addWidget(search_checkbox_scroll_area);
278 search_game_group->setLayout(search_game_layout);
279 search_game_group->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
280 search_filters_layout->addWidget(search_game_group);
281
282 // WIDGET: Filter by Element
283
284 // i18n: Elements are a trait of Skylanders figures. For official translations of this term,
285 // check the Skylanders SuperChargers manual at https://support.activision.com/manuals
286 auto* search_element_group = new QGroupBox(tr("Element"));
287 auto* search_element_layout = new QVBoxLayout();
288 auto* search_radio_scroll_area = new QScrollArea();
289 search_radio_scroll_area->setContentsMargins(0, 0, 0, 0);
290 search_radio_scroll_area->setFrameStyle(QFrame::NoFrame);
291 auto* search_radio_group = new QFrame();
292 search_radio_group->setContentsMargins(0, 0, 0, 0);
293 auto* search_radio_layout = new QHBoxLayout();
294
295 auto* radio_layout_left = new QVBoxLayout();
296 auto* radio_layout_right = new QVBoxLayout();
297 for (int i = 0; i < NUM_SKYLANDER_ELEMENTS_RADIO; i++)
298 {
299 QRadioButton* radio = new QRadioButton(this);
300 radio->setProperty("id", i);
301 if (i == 0)
302 {
303 radio->setChecked(true);
304 }
305 connect(radio, &QRadioButton::toggled, this, &SkylanderPortalWindow::RefreshList);
306 m_element_filter[i] = radio;
307
308 if (i % 2 == 0)
309 {
310 radio_layout_left->addWidget(radio);
311 }
312 else
313 {
314 radio_layout_right->addWidget(radio);
315 }
316 }
317 search_radio_layout->addLayout(radio_layout_left);
318 search_radio_layout->setDirection(QBoxLayout::Direction::LeftToRight);
319 search_radio_layout->addSpacing(35);
320 search_radio_layout->addLayout(radio_layout_right);
321
322 m_element_filter[0]->setText(tr("All"));
323 m_element_filter[0]->setChecked(true);
324 // i18n: One of the elements in the Skylanders games. Japanese: まほう. For official translations
325 // in other languages, check the SuperChargers manual at https://support.activision.com/manuals
326 m_element_filter[GetElementID(Element::Magic)]->setText(tr("Magic"));
327 // i18n: One of the elements in the Skylanders games. Japanese: 水. For official translations
328 // in other languages, check the SuperChargers manual at https://support.activision.com/manuals
329 m_element_filter[GetElementID(Element::Water)]->setText(tr("Water"));
330 // i18n: One of the elements in the Skylanders games. Japanese: マシン. For official translations
331 // in other languages, check the SuperChargers manual at https://support.activision.com/manuals
332 m_element_filter[GetElementID(Element::Tech)]->setText(tr("Tech"));
333 // i18n: One of the elements in the Skylanders games. Japanese: 火. For official translations
334 // in other languages, check the SuperChargers manual at https://support.activision.com/manuals
335 m_element_filter[GetElementID(Element::Fire)]->setText(tr("Fire"));
336 // i18n: One of the elements in the Skylanders games. Japanese: 土. For official translations
337 // in other languages, check the SuperChargers manual at https://support.activision.com/manuals
338 m_element_filter[GetElementID(Element::Earth)]->setText(tr("Earth"));
339 // i18n: One of the elements in the Skylanders games. Japanese: ライフ. For official translations
340 // in other languages, check the SuperChargers manual at https://support.activision.com/manuals
341 m_element_filter[GetElementID(Element::Life)]->setText(tr("Life"));
342 // i18n: One of the elements in the Skylanders games. Japanese: 風. For official translations
343 // in other languages, check the SuperChargers manual at https://support.activision.com/manuals
344 m_element_filter[GetElementID(Element::Air)]->setText(tr("Air"));
345 // i18n: One of the elements in the Skylanders games. Japanese: アンデッド. For official
346 // translations in other languages, check the SuperChargers manual at
347 // https://support.activision.com/manuals
348 m_element_filter[GetElementID(Element::Undead)]->setText(tr("Undead"));
349 // i18n: One of the elements in the Skylanders games. For official translations
350 // in other languages, check the SuperChargers manual at https://support.activision.com/manuals
351 m_element_filter[GetElementID(Element::Dark)]->setText(tr("Dark"));
352 // i18n: One of the elements in the Skylanders games. For official translations
353 // in other languages, check the SuperChargers manual at https://support.activision.com/manuals
354 m_element_filter[GetElementID(Element::Light)]->setText(tr("Light"));
355 m_element_filter[GetElementID(Element::Other)]->setText(tr("Other"));
356
357 search_radio_group->setLayout(search_radio_layout);
358 search_radio_scroll_area->setWidget(search_radio_group);
359 search_element_layout->addWidget(search_radio_scroll_area);
360 search_radio_scroll_area->setAlignment(Qt::AlignHCenter);
361 search_element_group->setLayout(search_element_layout);
362 search_element_group->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
363 search_filters_layout->addWidget(search_element_group);
364
365 // Widget: Filter by Type
366 auto* search_type_group = new QGroupBox(tr("Figure type"));
367 auto* search_type_layout = new QVBoxLayout();
368 auto* search_type_radio_scroll_area = new QScrollArea();
369 search_type_radio_scroll_area->setContentsMargins(0, 0, 0, 0);
370 search_type_radio_scroll_area->setFrameStyle(QFrame::NoFrame);
371 auto* search_type_radio_group = new QFrame();
372 search_type_radio_group->setContentsMargins(0, 0, 0, 0);
373 auto* search_type_radio_layout = new QHBoxLayout();
374
375 auto* radio_type_layout_left = new QVBoxLayout();
376 auto* radio_type_layout_right = new QVBoxLayout();
377 for (int i = 0; i < NUM_SKYLANDER_TYPES; i++)
378 {
379 QRadioButton* radio = new QRadioButton(this);
380 radio->setProperty("id", i);
381 if (i == 0)
382 {
383 radio->setChecked(true);
384 }
385 connect(radio, &QRadioButton::toggled, this, &SkylanderPortalWindow::RefreshList);
386 m_type_filter[i] = radio;
387
388 if (i % 2 == 0)
389 {
390 radio_type_layout_left->addWidget(radio);
391 }
392 else
393 {
394 radio_type_layout_right->addWidget(radio);
395 }
396 }
397 search_type_radio_layout->addLayout(radio_type_layout_left);
398 search_type_radio_layout->addLayout(radio_type_layout_right);
399
400 m_type_filter[0]->setText(tr("All"));
401 // i18n: One of the figure types in the Skylanders games.
402 m_type_filter[GetTypeID(Type::Skylander)]->setText(tr("Skylander"));
403 // i18n: One of the figure types in the Skylanders games.
404 m_type_filter[GetTypeID(Type::Giant)]->setText(tr("Giant"));
405 // i18n: One of the figure types in the Skylanders games.
406 m_type_filter[GetTypeID(Type::Swapper)]->setText(tr("Swapper"));
407 // i18n: One of the figure types in the Skylanders games.
408 m_type_filter[GetTypeID(Type::TrapMaster)]->setText(tr("Trap Master"));
409 // i18n: One of the figure types in the Skylanders games.
410 m_type_filter[GetTypeID(Type::Mini)]->setText(tr("Mini"));
411 // i18n: One of the figure types in the Skylanders games.
412 m_type_filter[GetTypeID(Type::Item)]->setText(tr("Item"));
413 // i18n: One of the figure types in the Skylanders games.
414 m_type_filter[GetTypeID(Type::Trophy)]->setText(tr("Trophy"));
415 // i18n: One of the figure types in the Skylanders games. For official translations
416 // in other languages, check the SuperChargers manual at https://support.activision.com/manuals
417 m_type_filter[GetTypeID(Type::Vehicle)]->setText(tr("Vehicle"));
418 // i18n: One of the figure types in the Skylanders games.
419 m_type_filter[GetTypeID(Type::Trap)]->setText(tr("Trap"));
420
421 search_type_radio_group->setLayout(search_type_radio_layout);
422 search_type_radio_scroll_area->setWidget(search_type_radio_group);
423 search_type_radio_scroll_area->setAlignment(Qt::AlignHCenter);
424 search_type_layout->addWidget(search_type_radio_scroll_area);
425 search_type_group->setLayout(search_type_layout);
426 search_type_group->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
427 search_filters_layout->addWidget(search_type_group);
428
429 // Widget: Other Filters
430 auto* other_box = new QGroupBox(tr("Other"));
431 auto* other_layout = new QVBoxLayout;
432 connect(m_only_show_collection, &QCheckBox::toggled, this, &SkylanderPortalWindow::RefreshList);
433 other_layout->addWidget(m_only_show_collection);
434 other_box->setLayout(other_layout);
435 search_filters_layout->addWidget(other_box);
436
437 search_checkbox_scroll_area->setMinimumWidth(search_radio_group->width() + 20);
438 search_radio_scroll_area->setMinimumWidth(search_radio_group->width() + 20);
439 other_box->setMinimumWidth(search_radio_group->width() + 20);
440
441 search_filters_layout->addStretch(1);
442
443 finder_list_layout->addLayout(search_filters_layout);
444
445 finder_list_layout->addWidget(m_skylander_list);
446
447 finder_layout->addLayout(finder_list_layout);
448
449 main_layout->addLayout(finder_layout);
450
451 return main_layout;
452 }
453
454 bool SkylanderPortalWindow::eventFilter(QObject* object, QEvent* event)
455 {
456 if (event->type() == QEvent::KeyPress)
457 {
458 // Close when escape is pressed
459 if (static_cast<QKeyEvent*>(event)->matches(QKeySequence::Cancel))
460 hide();
461 }
462
463 return false;
464 }
465
466 void SkylanderPortalWindow::closeEvent(QCloseEvent* event)
467 {
468 hide();
469 }
470
471 // UI
472 void SkylanderPortalWindow::EmulatePortal(bool emulate)
473 {
474 Config::SetBaseOrCurrent(Config::MAIN_EMULATE_SKYLANDER_PORTAL, emulate);
475 m_group_skylanders->setVisible(emulate);
476 m_command_buttons->setVisible(emulate);
477 m_emulating = emulate;
478 }
479
480 void SkylanderPortalWindow::SelectCollectionPath()
481 {
482 QString dir = QDir::toNativeSeparators(DolphinFileDialog::getExistingDirectory(
483 this, tr("Select Skylander Collection"), m_collection_path));
484 if (!dir.isEmpty())
485 {
486 dir += QDir::separator();
487 m_path_edit->setText(dir);
488 m_collection_path = dir;
489 }
490 Config::SetBase(Config::MAIN_SKYLANDERS_PATH, dir.toStdString());
491
492 if (m_only_show_collection->isChecked())
493 RefreshList();
494 }
495
496 void SkylanderPortalWindow::LoadSelected()
497 {
498 if (!m_emulating)
499 return;
500
501 const u8 slot = GetCurrentSlot();
502
503 QString file_path;
504 if (m_only_show_collection->isChecked())
505 {
506 if (m_skylander_list->currentItem() == nullptr)
507 return;
508 file_path =
509 m_collection_path + m_skylander_list->currentItem()->text() + QStringLiteral(".sky");
510 }
511 else
512 {
513 file_path = GetFilePath(m_sky_id, m_sky_var);
514 }
515
516 if (file_path.isEmpty())
517 {
518 QMessageBox::StandardButton create_file_response;
519 create_file_response =
520 QMessageBox::question(this, tr("Create Skylander File"),
521 tr("Skylander not found in this collection. Create new file?"),
522 QMessageBox::Yes | QMessageBox::No);
523
524 if (create_file_response == QMessageBox::Yes)
525 {
526 QString predef_name = m_collection_path;
527 const auto found_sky =
528 IOS::HLE::USB::list_skylanders.find(std::make_pair(m_sky_id, m_sky_var));
529 if (found_sky != IOS::HLE::USB::list_skylanders.end())
530 {
531 predef_name += QString::fromStdString(std::string(found_sky->second.name) + ".sky");
532 }
533 else
534 {
535 // i18n: This is used to create a file name. The string must end in ".sky".
536 const QString str = tr("Unknown(%1 %2).sky");
537 predef_name += str.arg(m_sky_id, m_sky_var);
538 }
539
540 CreateSkyfile(predef_name, true);
541 }
542 }
543 else
544 {
545 m_last_skylander_path = QFileInfo(file_path).absolutePath() + QLatin1Char{'/'};
546
547 LoadSkyfilePath(slot, file_path);
548 }
549 }
550
551 void SkylanderPortalWindow::LoadFromFile()
552 {
553 const u8 slot = GetCurrentSlot();
554 const QString file_path =
555 DolphinFileDialog::getOpenFileName(this, tr("Select Skylander File"), m_last_skylander_path,
556 tr("Skylander (*.sky *.bin *.dmp *.dump);;All Files (*)"));
557 ;
558 if (file_path.isEmpty())
559 {
560 return;
561 }
562 m_last_skylander_path =
563 QDir::toNativeSeparators(QFileInfo(file_path).absolutePath()) + QDir::separator();
564
565 LoadSkyfilePath(slot, file_path);
566 }
567
568 void SkylanderPortalWindow::CreateSkylanderAdvanced()
569 {
570 QDialog* create_window = new QDialog;
571
572 auto* layout = new QVBoxLayout;
573
574 auto* hbox_idvar = new QHBoxLayout();
575 auto* label_id = new QLabel(tr("ID:"));
576 auto* label_var = new QLabel(tr("Variant:"));
577 auto* edit_id = new QLineEdit(tr("0"));
578 auto* edit_var = new QLineEdit(tr("0"));
579 auto* rxv = new QRegularExpressionValidator(QRegularExpression(QStringLiteral("\\d*")), this);
580 edit_id->setValidator(rxv);
581 edit_var->setValidator(rxv);
582 hbox_idvar->addWidget(label_id);
583 hbox_idvar->addWidget(edit_id);
584 hbox_idvar->addWidget(label_var);
585 hbox_idvar->addWidget(edit_var);
586 layout->addLayout(hbox_idvar);
587
588 auto* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
589 buttons->button(QDialogButtonBox::Ok)->setText(tr("Create"));
590 layout->addWidget(buttons);
591
592 create_window->setLayout(layout);
593
594 connect(buttons, &QDialogButtonBox::accepted, this, [=, this] {
595 bool ok_id = false, ok_var = false;
596 m_sky_id = edit_id->text().toUShort(&ok_id);
597 if (!ok_id)
598 {
599 QMessageBox::warning(this, tr("Error converting value"), tr("ID entered is invalid!"),
600 QMessageBox::Ok);
601 return;
602 }
603 m_sky_var = edit_var->text().toUShort(&ok_var);
604 if (!ok_var)
605 {
606 QMessageBox::warning(this, tr("Error converting value"), tr("Variant entered is invalid!"),
607 QMessageBox::Ok);
608 return;
609 }
610
611 QString predef_name = m_last_skylander_path;
612 const auto found_sky = IOS::HLE::USB::list_skylanders.find(std::make_pair(m_sky_id, m_sky_var));
613 if (found_sky != IOS::HLE::USB::list_skylanders.end())
614 {
615 predef_name += QString::fromStdString(std::string(found_sky->second.name) + ".sky");
616 }
617 else
618 {
619 // i18n: This is used to create a file name. The string must end in ".sky".
620 QString str = tr("Unknown(%1 %2).sky");
621 predef_name += str.arg(m_sky_id, m_sky_var);
622 }
623
624 QString file_path = DolphinFileDialog::getSaveFileName(
625 this, tr("Create Skylander File"), predef_name, tr("Skylander (*.sky);;All Files (*)"));
626 if (file_path.isEmpty())
627 {
628 return;
629 }
630
631 CreateSkyfile(file_path, true);
632 create_window->accept();
633 });
634
635 connect(buttons, &QDialogButtonBox::rejected, create_window, &QDialog::reject);
636
637 create_window->show();
638 create_window->raise();
639 }
640
641 void SkylanderPortalWindow::ModifySkylander()
642 {
643 if (auto sky_slot = m_sky_slots[GetCurrentSlot()])
644 {
645 new SkylanderModifyDialog(this, sky_slot.value().portal_slot);
646 }
647 else
648 {
649 QMessageBox::warning(this, tr("Failed to modify Skylander!"),
650 tr("Make sure there is a Skylander in slot %1!").arg(GetCurrentSlot()),
651 QMessageBox::Ok);
652 }
653 }
654
655 void SkylanderPortalWindow::ClearSlot(u8 slot)
656 {
657 auto& system = Core::System::GetInstance();
658 if (auto slot_infos = m_sky_slots[slot])
659 {
660 if (!system.GetSkylanderPortal().RemoveSkylander(slot_infos->portal_slot))
661 {
662 QMessageBox::warning(this, tr("Failed to clear Skylander!"),
663 tr("Failed to clear the Skylander from slot %1!").arg(slot),
664 QMessageBox::Ok);
665 return;
666 }
667 m_sky_slots[slot].reset();
668 if (m_only_show_collection->isChecked())
669 RefreshList();
670 UpdateSlotNames();
671 }
672 }
673
674 // Behind the scenes
675 void SkylanderPortalWindow::OnCollectionPathChanged()
676 {
677 m_collection_path = m_path_edit->text();
678 Config::SetBase(Config::MAIN_SKYLANDERS_PATH, m_path_edit->text().toStdString());
679 RefreshList();
680 }
681
682 void SkylanderPortalWindow::OnEmulationStateChanged(Core::State state)
683 {
684 const bool running = state != Core::State::Uninitialized;
685
686 m_enabled_checkbox->setEnabled(!running);
687 }
688
689 void SkylanderPortalWindow::UpdateCurrentIDs()
690 {
691 const u32 sky_info = m_skylander_list->currentItem()->data(1).toUInt();
692 if (sky_info != 0xFFFFFFFF)
693 {
694 m_sky_id = sky_info >> 16;
695 m_sky_var = sky_info & 0xFFFF;
696 }
697 }
698
699 void SkylanderPortalWindow::RefreshList()
700 {
701 const bool is_dark_theme = Settings::Instance().IsThemeDark();
702
703 const int row = m_skylander_list->currentRow();
704 m_skylander_list->clear();
705 if (m_only_show_collection->isChecked())
706 {
707 const QDir collection = QDir(m_collection_path);
708 auto& system = Core::System::GetInstance();
709 for (const auto& file : collection.entryInfoList(
710 QStringList() << QStringLiteral("*.sky") << QStringLiteral("*.bin")
711 << QStringLiteral("*.dmp") << QStringLiteral("*.dump")))
712 {
713 File::IOFile sky_file(file.filePath().toStdString(), "r+b");
714 if (!sky_file)
715 {
716 continue;
717 }
718 std::array<u8, 0x40 * 0x10> file_data;
719 if (!sky_file.ReadBytes(file_data.data(), file_data.size()))
720 {
721 continue;
722 }
723 auto ids = system.GetSkylanderPortal().CalculateIDs(file_data);
724 if (PassesFilter(file.baseName(), ids.first, ids.second))
725 {
726 const uint qvar = (ids.first << 16) | ids.second;
727 QListWidgetItem* skylander = new QListWidgetItem(file.baseName());
728 if (is_dark_theme)
729 {
730 skylander->setBackground(GetBaseColor(ids, true));
731 skylander->setForeground(QBrush(QColor(220, 220, 220)));
732 }
733 else
734 {
735 skylander->setBackground(GetBaseColor(ids, false));
736 skylander->setForeground(QBrush(QColor(0, 0, 0)));
737 }
738 skylander->setData(1, qvar);
739 m_skylander_list->addItem(skylander);
740 }
741 }
742 }
743 else
744 {
745 for (const auto& entry : IOS::HLE::USB::list_skylanders)
746 {
747 int id = entry.first.first;
748 int var = entry.first.second;
749 if (PassesFilter(tr(entry.second.name), id, var))
750 {
751 const uint qvar = (entry.first.first << 16) | entry.first.second;
752 QListWidgetItem* skylander = new QListWidgetItem(tr(entry.second.name));
753 if (is_dark_theme)
754 {
755 skylander->setBackground(GetBaseColor(entry.first, true));
756 skylander->setForeground(QBrush(QColor(220, 220, 220)));
757 }
758 else
759 {
760 skylander->setBackground(GetBaseColor(entry.first, false));
761 skylander->setForeground(QBrush(QColor(0, 0, 0)));
762 }
763 skylander->setData(1, qvar);
764 m_skylander_list->addItem(skylander);
765 }
766 }
767 }
768 if (m_skylander_list->count() >= row)
769 {
770 m_skylander_list->setCurrentItem(m_skylander_list->item(row), QItemSelectionModel::Select);
771 }
772 else if (m_skylander_list->count() > 0)
773 {
774 m_skylander_list->setCurrentItem(m_skylander_list->item(m_skylander_list->count() - 1),
775 QItemSelectionModel::Select);
776 }
777 }
778
779 void SkylanderPortalWindow::CreateSkyfile(const QString& path, bool load_after)
780 {
781 {
782 IOS::HLE::USB::SkylanderFigure figure(path.toStdString());
783 if (!figure.Create(m_sky_id, m_sky_var))
784 {
785 QMessageBox::warning(
786 this, tr("Failed to create Skylander file!"),
787 tr("Failed to create Skylander file:\n%1\n\nThe Skylander may already be on the portal.")
788 .arg(path),
789 QMessageBox::Ok);
790 return;
791 }
792 figure.Close();
793 }
794 m_last_skylander_path = QFileInfo(path).absolutePath() + QLatin1Char{'/'};
795
796 if (load_after)
797 LoadSkyfilePath(GetCurrentSlot(), path);
798 }
799
800 void SkylanderPortalWindow::LoadSkyfilePath(u8 slot, const QString& path)
801 {
802 File::IOFile sky_file(path.toStdString(), "r+b");
803 if (!sky_file)
804 {
805 QMessageBox::warning(this, tr("Failed to open the Skylander file!"),
806 tr("Failed to open the Skylander file:\n%1\n\nThe file may already be in "
807 "use on the portal.")
808 .arg(path),
809 QMessageBox::Ok);
810 return;
811 }
812 std::array<u8, 0x40 * 0x10> file_data;
813 if (!sky_file.ReadBytes(file_data.data(), file_data.size()))
814 {
815 QMessageBox::warning(
816 this, tr("Failed to read the Skylander file!"),
817 tr("Failed to read the Skylander file:\n%1\n\nThe file was too small.").arg(path),
818 QMessageBox::Ok);
819 return;
820 }
821
822 ClearSlot(slot);
823
824 auto& system = Core::System::GetInstance();
825 const std::pair<u16, u16> id_var = system.GetSkylanderPortal().CalculateIDs(file_data);
826 const u8 portal_slot = system.GetSkylanderPortal().LoadSkylander(
827 std::make_unique<IOS::HLE::USB::SkylanderFigure>(std::move(sky_file)));
828 if (portal_slot == 0xFF)
829 {
830 QMessageBox::warning(this, tr("Failed to load the Skylander file!"),
831 tr("Failed to load the Skylander file:\n%1").arg(path), QMessageBox::Ok);
832 return;
833 }
834 m_sky_slots[slot] = {portal_slot, id_var.first, id_var.second};
835 RefreshList();
836 UpdateSlotNames();
837 }
838
839 void SkylanderPortalWindow::UpdateSlotNames()
840 {
841 for (auto i = 0; i < MAX_SKYLANDERS; i++)
842 {
843 QString display_string;
844 if (auto sd = m_sky_slots[i])
845 {
846 auto found_sky =
847 IOS::HLE::USB::list_skylanders.find(std::make_pair(sd->m_sky_id, sd->m_sky_var));
848 if (found_sky != IOS::HLE::USB::list_skylanders.end())
849 {
850 display_string = QString::fromStdString(found_sky->second.name);
851 }
852 else
853 {
854 // i18n: "Var" is short for "variant"
855 display_string = tr("Unknown (Id:%1 Var:%2)").arg(sd->m_sky_id).arg(sd->m_sky_var);
856 }
857 }
858 else
859 {
860 display_string = tr("None");
861 }
862
863 m_edit_skylanders[i]->setText(display_string);
864 }
865 }
866
867 // Helpers
868 bool SkylanderPortalWindow::PassesFilter(const QString& name, u16 id, u16 var) const
869 {
870 const auto skypair = IOS::HLE::USB::list_skylanders.find(std::make_pair(id, var));
871 IOS::HLE::USB::SkyData character;
872 if (skypair == IOS::HLE::USB::list_skylanders.end())
873 {
874 return false;
875 }
876 else
877 {
878 character = skypair->second;
879 }
880
881 bool pass = false;
882
883 // Check against active game filters
884 for (size_t i = 0; i < NUM_SKYLANDER_GAMES; i++)
885 {
886 if (m_game_filters[i]->isChecked() && character.game == (Game)i)
887 {
888 pass = true;
889 }
890 }
891 if (!pass)
892 return false;
893
894 // Check against search bar filter
895 if (!name.contains(m_sky_search->text(), Qt::CaseInsensitive))
896 return false;
897
898 // Check against active element filter
899 if ((Element)GetElementRadio() != character.element && GetElementRadio() != 0)
900 return false;
901
902 // Check against active type filter
903 if ((Type)GetTypeRadio() != character.type && GetTypeRadio() != 0)
904 return false;
905
906 return true;
907 }
908
909 QString SkylanderPortalWindow::GetFilePath(u16 id, u16 var) const
910 {
911 const QDir collection = QDir(m_collection_path);
912 auto& system = Core::System::GetInstance();
913 for (const auto& file : collection.entryInfoList(
914 QStringList() << QStringLiteral("*.sky") << QStringLiteral("*.bin")
915 << QStringLiteral("*.dmp") << QStringLiteral("*.dump")))
916 {
917 File::IOFile sky_file(file.filePath().toStdString(), "r+b");
918 if (!sky_file)
919 {
920 continue;
921 }
922 std::array<u8, 0x40 * 0x10> file_data;
923 if (!sky_file.ReadBytes(file_data.data(), file_data.size()))
924 {
925 continue;
926 }
927 auto ids = system.GetSkylanderPortal().CalculateIDs(file_data);
928 if (ids.first == id && ids.second == var)
929 {
930 return file.filePath();
931 }
932 }
933 return {};
934 }
935
936 u8 SkylanderPortalWindow::GetCurrentSlot() const
937 {
938 for (const auto* radio : m_slot_radios)
939 {
940 if (radio->isChecked())
941 {
942 return radio->property("id").toInt();
943 }
944 }
945 return 0;
946 }
947
948 int SkylanderPortalWindow::GetElementRadio() const
949 {
950 for (const auto* radio : m_element_filter)
951 {
952 if (radio->isChecked())
953 {
954 return radio->property("id").toInt();
955 }
956 }
957 return -1;
958 }
959
960 int SkylanderPortalWindow::GetTypeRadio() const
961 {
962 for (const auto* radio : m_type_filter)
963 {
964 if (radio->isChecked())
965 {
966 return radio->property("id").toInt();
967 }
968 }
969 return -1;
970 }
971
972 QBrush SkylanderPortalWindow::GetBaseColor(std::pair<const u16, const u16> ids, bool dark_theme)
973 {
974 auto skylander = IOS::HLE::USB::list_skylanders.find(ids);
975
976 if (skylander == IOS::HLE::USB::list_skylanders.end())
977 return QBrush(dark_theme ? QColor(32, 32, 32) : QColor(255, 255, 255));
978
979 switch (skylander->second.game)
980 {
981 case Game::SpyrosAdv:
982 return QBrush(dark_theme ? QColor(10, 42, 90) : QColor(240, 255, 240));
983 case Game::Giants:
984 return QBrush(dark_theme ? QColor(120, 16, 12) : QColor(255, 240, 215));
985 case Game::SwapForce:
986 return QBrush(dark_theme ? QColor(28, 45, 12) : QColor(240, 245, 255));
987 case Game::TrapTeam:
988 return QBrush(dark_theme ? QColor(0, 56, 76) : QColor(255, 240, 240));
989 case Game::Superchargers:
990 return QBrush(dark_theme ? QColor(90, 12, 12) : QColor(247, 228, 215));
991 default:
992 return QBrush(dark_theme ? QColor(32, 32, 32) : QColor(255, 255, 255));
993 }
994 }
995
996 int SkylanderPortalWindow::GetGameID(Game game)
997 {
998 return (int)game;
999 }
1000
1001 int SkylanderPortalWindow::GetElementID(Element elem)
1002 {
1003 return (int)elem;
1004 }
1005
1006 int SkylanderPortalWindow::GetTypeID(Type type)
1007 {
1008 return (int)type;
1009 }