src/platform/qt/BattleChipView.cpp (view raw)
1/* Copyright (c) 2013-2019 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "BattleChipView.h"
7
8#include "CoreController.h"
9
10#include <QFile>
11#include <QStringList>
12
13using namespace QGBA;
14
15BattleChipView::BattleChipView(std::shared_ptr<CoreController> controller, QWidget* parent)
16 : QDialog(parent)
17 , m_controller(controller)
18{
19 m_ui.setupUi(this);
20
21 char title[9];
22 CoreController::Interrupter interrupter(m_controller);
23 mCore* core = m_controller->thread()->core;
24 title[8] = '\0';
25 core->getGameCode(core, title);
26 QString qtitle(title);
27
28
29 connect(m_ui.chipId, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), m_ui.inserted, [this]() {
30 m_ui.inserted->setChecked(Qt::Unchecked);
31 });
32 connect(m_ui.chipName, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), m_ui.chipId, [this](int id) {
33 m_ui.chipId->setValue(m_chipIndexToId[id]);
34 });
35
36 connect(m_ui.inserted, &QAbstractButton::toggled, this, &BattleChipView::insertChip);
37 connect(controller.get(), &CoreController::stopping, this, &QWidget::close);
38
39 connect(m_ui.gateBattleChip, &QAbstractButton::toggled, this, [this](bool on) {
40 if (on) {
41 setFlavor(GBA_FLAVOR_BATTLECHIP_GATE);
42 }
43 });
44 connect(m_ui.gateProgress, &QAbstractButton::toggled, this, [this](bool on) {
45 if (on) {
46 setFlavor(GBA_FLAVOR_PROGRESS_GATE);
47 }
48 });
49 connect(m_ui.gateBeastLink, &QAbstractButton::toggled, this, [this, qtitle](bool on) {
50 if (on) {
51 if (qtitle.endsWith('E') || qtitle.endsWith('P')) {
52 setFlavor(GBA_FLAVOR_BEAST_LINK_GATE_US);
53 } else {
54 setFlavor(GBA_FLAVOR_BEAST_LINK_GATE);
55 }
56 }
57 });
58
59 m_controller->attachBattleChipGate();
60 setFlavor(4);
61 if (qtitle.startsWith("AGB-B4B") || qtitle.startsWith("AGB-B4W") || qtitle.startsWith("AGB-BR4") || qtitle.startsWith("AGB-BZ3")) {
62 m_ui.gateBattleChip->setChecked(Qt::Checked);
63 } else if (qtitle.startsWith("AGB-BRB") || qtitle.startsWith("AGB-BRK")) {
64 m_ui.gateProgress->setChecked(Qt::Checked);
65 } else if (qtitle.startsWith("AGB-BR5") || qtitle.startsWith("AGB-BR6")) {
66 m_ui.gateBeastLink->setChecked(Qt::Checked);
67 }
68}
69
70BattleChipView::~BattleChipView() {
71 m_controller->detachBattleChipGate();
72}
73
74void BattleChipView::setFlavor(int flavor) {
75 m_controller->setBattleChipFlavor(flavor);
76 loadChipNames(flavor);
77}
78
79void BattleChipView::insertChip(bool inserted) {
80 if (inserted) {
81 m_controller->setBattleChipId(m_ui.chipId->value());
82 } else {
83 m_controller->setBattleChipId(0);
84 }
85}
86
87void BattleChipView::loadChipNames(int flavor) {
88 QStringList chipNames;
89 chipNames.append(tr("(None)"));
90
91 m_chipIndexToId.clear();
92 if (flavor == GBA_FLAVOR_BEAST_LINK_GATE_US) {
93 flavor = GBA_FLAVOR_BEAST_LINK_GATE;
94 }
95
96 QFile file(QString(":/res/chip-names-%1.txt").arg(flavor));
97 file.open(QIODevice::ReadOnly | QIODevice::Text);
98 int id = 0;
99 while (true) {
100 QByteArray line = file.readLine();
101 if (line.isEmpty()) {
102 break;
103 }
104 ++id;
105 if (line.trimmed().isEmpty()) {
106 continue;
107 }
108 m_chipIndexToId[chipNames.length()] = id;
109 chipNames.append(QString::fromUtf8(line).trimmed());
110 }
111
112 m_ui.chipName->clear();
113 m_ui.chipName->addItems(chipNames);
114}