-
Notifications
You must be signed in to change notification settings - Fork 2
/
mapdescriptorwidget.cpp
225 lines (194 loc) · 10.4 KB
/
mapdescriptorwidget.cpp
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
#include "mapdescriptorwidget.h"
#include <QCheckBox>
#include <QFileDialog>
#include <QHeaderView>
#include <QMessageBox>
#include <QPushButton>
#include <QTextStream>
#include "lib/datafileset.h"
#include "lib/importexportutils.h"
#include "lib/getordefault.h"
#include "lib/progresscanceled.h"
#include "venturecarddialog.h"
#include "csmmprogressdialog.h"
// Internal types for table items
static constexpr int MAP_SET_TYPE = QTableWidgetItem::UserType;
static constexpr int ZONE_TYPE = QTableWidgetItem::UserType + 1;
static constexpr int ORDER_TYPE = QTableWidgetItem::UserType + 2;
static constexpr int UNLOCK_ID_TYPE = QTableWidgetItem::UserType + 3;
MapDescriptorWidget::MapDescriptorWidget(QWidget *parent) : QTableWidget(parent) {
QStringList labels{"", "", "Name", "MapSet [E]", "Zone [E]", "Order [E]",
"Tut. Map? [E]", "Unlock ID [E]", "Ruleset", "Initial Cash", "Target Amount",
"Base Salary", "Salary Increment", "Max. Dice Roll",
"Venture Cards", "FRB Files", "Switch Origin Points",
"Board Theme", "Background", "Background Music ID",
"Map Icon", "Looping Mode", "Looping Mode Radius",
"Looping Mode Horiz. Padding", "Looping Mode Vertical Square Count",
"Tour Bankruptcy Limit", "Tour Initial Cash", "Tour Opponents",
"Tour Clear Rank", "Name Msg ID", "Desc Msg ID",
"Description", "Internal Name", "District Names", "District Name IDs"};
setColumnCount(labels.size());
setHorizontalHeaderLabels(labels);
horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
connect(this, &MapDescriptorWidget::cellChanged, this, [&](int row, int col) {
auto theItem = item(row, col);
if (theItem->type() < QTableWidgetItem::UserType) return;
bool ok;
int value = theItem->text().toInt(&ok);
if (!ok) return;
if (theItem->type() == MAP_SET_TYPE) {
descriptors[row]->mapSet = value;
} else if (theItem->type() == ZONE_TYPE) {
descriptors[row]->zone = value;
} else if (theItem->type() == ORDER_TYPE) {
descriptors[row]->order = value;
} else if (theItem->type() == UNLOCK_ID_TYPE) {
descriptors[row]->unlockId = value;
} else {
return;
}
dirty = true;
});
setSelectionBehavior(QAbstractItemView::SelectRows);
}
static QTableWidgetItem *readOnlyItem(const QString &str) {
auto item = new QTableWidgetItem(str);
item->setFlags(item->flags() & ~Qt::ItemIsEditable);
return item;
}
void MapDescriptorWidget::loadRowWithMapDescriptor(int row, const MapDescriptor &descriptor) {
descriptors[row] = QSharedPointer<MapDescriptor>::create(descriptor);
// Descriptor pointer is so that the button event handlers work properly when descriptors
// are shifted
auto descriptorPtr = descriptors[row];
int colIdx = 0;
setVerticalHeaderItem(row, readOnlyItem(QString::number(row)));
auto importYamlButton = new QPushButton("Import .yaml or .zip");
connect(importYamlButton, &QPushButton::clicked, this, [=](bool) {
auto openYaml = QFileDialog::getOpenFileName(this, "Import .yaml or .zip", QString(), "Map Descriptor Files (*.yaml *.zip)", nullptr);
if (openYaml.isEmpty()) return;
MapDescriptor newDescriptor;
try {
CSMMProgressDialog dialog("Importing yaml", QString(), 0, 100, nullptr, Qt::WindowFlags(), true);
ImportExportUtils::importYaml(openYaml, newDescriptor, getImportDirectory(), [&](double progress) {
dialog.setValue(100 * progress);
});
descriptorPtr->setFromImport(newDescriptor);
descriptorPtr->mapDescriptorFilePath = openYaml;
dirty = true;
loadRowWithMapDescriptor(descriptors.indexOf(descriptorPtr), *descriptorPtr);
} catch (const ProgressCanceled &) {
// nothing to do
} catch (const std::runtime_error &exception) {
QMessageBox::critical(this, "Import .yaml", QString("Error loading the map: %1").arg(exception.what()));
}
});
setCellWidget(row, colIdx++, importYamlButton);
auto exportYamlButton = new QPushButton("Export .yaml");
connect(exportYamlButton, &QPushButton::clicked, this, [=](bool) {
auto gameDirectory = getGameDirectory();
auto saveYamlTo = QFileDialog::getSaveFileName(exportYamlButton, "Export .yaml", descriptorPtr->internalName + ".yaml", "Map Descriptor Files (*.yaml)", nullptr, QFileDialog::DontUseNativeDialog);
if (saveYamlTo.isEmpty()) return;
try {
ImportExportUtils::exportYaml(gameDirectory, saveYamlTo, *descriptorPtr);
} catch (const std::runtime_error &exception) {
QMessageBox::critical(this, "Export .yaml", QString("Error exporting the map: %1").arg(exception.what()));
}
});
setCellWidget(row, colIdx++, exportYamlButton);
setItem(row, colIdx++, readOnlyItem(getOrDefault(descriptor.names, "en", QString())));
setItem(row, colIdx++, new QTableWidgetItem(QString::number(descriptor.mapSet), MAP_SET_TYPE));
setItem(row, colIdx++, new QTableWidgetItem(QString::number(descriptor.zone), ZONE_TYPE));
setItem(row, colIdx++, new QTableWidgetItem(QString::number(descriptor.order), ORDER_TYPE));
auto isPracticeBoardCheck = new QCheckBox();
isPracticeBoardCheck->setChecked(descriptor.isPracticeBoard);
connect(isPracticeBoardCheck, &QCheckBox::clicked, this, [=](bool isChecked) {
descriptorPtr->isPracticeBoard = isChecked;
dirty = true;
});
setCellWidget(row, colIdx++, isPracticeBoardCheck);
setItem(row, colIdx++, new QTableWidgetItem(QString::number(descriptor.unlockId), UNLOCK_ID_TYPE));
setItem(row, colIdx++, readOnlyItem(descriptor.ruleSet == Easy ? "Easy" : "Standard"));
setItem(row, colIdx++, readOnlyItem(QString::number(descriptor.initialCash)));
setItem(row, colIdx++, readOnlyItem(QString::number(descriptor.targetAmount)));
setItem(row, colIdx++, readOnlyItem(QString::number(descriptor.baseSalary)));
setItem(row, colIdx++, readOnlyItem(QString::number(descriptor.salaryIncrement)));
setItem(row, colIdx++, readOnlyItem(QString::number(descriptor.maxDiceRoll)));
QPushButton *ventureButton = new QPushButton("View Venture Cards"); // note: setCellWidget takes control of button
connect(ventureButton, &QPushButton::clicked, this, [=](bool) {
(new VentureCardDialog(*descriptorPtr /* maintain descriptor when other descs are removed */, this))->exec();
});
setCellWidget(row, colIdx++, ventureButton);
QStringList frbFilesList;
for (auto &str: descriptor.frbFiles) {
if (!str.isEmpty()) {
frbFilesList.append(str);
}
}
setItem(row, colIdx++, readOnlyItem(frbFilesList.join("; ")));
QStringList originPointsStrList;
for (auto &originPoint: descriptor.switchRotationOrigins) originPointsStrList.append(QString(originPoint));
setItem(row, colIdx++, readOnlyItem(originPointsStrList.join("; ")));
setItem(row, colIdx++, readOnlyItem(descriptor.theme == Mario ? "Mario" : "DragonQuest"));
setItem(row, colIdx++, readOnlyItem(descriptor.background));
setItem(row, colIdx++, readOnlyItem(Bgm::bgmIdToString(descriptor.bgmId)));
setItem(row, colIdx++, readOnlyItem(descriptor.mapIcon));
setItem(row, colIdx++, readOnlyItem(QString::number(descriptor.loopingMode)));
setItem(row, colIdx++, readOnlyItem(QString::number(descriptor.loopingModeRadius)));
setItem(row, colIdx++, readOnlyItem(QString::number(descriptor.loopingModeHorizontalPadding)));
setItem(row, colIdx++, readOnlyItem(QString::number(descriptor.loopingModeVerticalSquareCount)));
setItem(row, colIdx++, readOnlyItem(QString::number(descriptor.tourBankruptcyLimit)));
setItem(row, colIdx++, readOnlyItem(QString::number(descriptor.tourInitialCash)));
QStringList tourOpponentsList;
for (auto character: descriptor.tourCharacters) tourOpponentsList.append(tourCharacterToString(character));
setItem(row, colIdx++, readOnlyItem(tourOpponentsList.join("; ")));
setItem(row, colIdx++, readOnlyItem(QString::number(descriptor.tourClearRank)));
setItem(row, colIdx++, readOnlyItem(QString::number(descriptor.nameMsgId)));
setItem(row, colIdx++, readOnlyItem(QString::number(descriptor.descMsgId)));
setItem(row, colIdx++, readOnlyItem(getOrDefault(descriptor.descs, "en", QString())));
setItem(row, colIdx++, readOnlyItem(descriptor.internalName));
auto distNames = getOrDefault(descriptor.districtNames, "en", std::vector<QString>());
setItem(row, colIdx++, readOnlyItem(QStringList(distNames.begin(), distNames.end()).join("; ")));
QStringList districtNameIdStrs;
for (quint32 v: descriptor.districtNameIds) {
districtNameIdStrs.push_back(QString::number(v));
}
setItem(row, colIdx++, readOnlyItem(districtNameIdStrs.join("; ")));
}
void MapDescriptorWidget::appendMapDescriptor(const MapDescriptor &descriptor) {
descriptors.append(QSharedPointer<MapDescriptor>::create(descriptor));
insertRow(descriptors.size()-1);
loadRowWithMapDescriptor(descriptors.size()-1, descriptor);
dirty = true;
}
void MapDescriptorWidget::duplicateSelectedMapDescriptors() {
auto selectedRows = selectionModel()->selectedRows();
for (auto &selectedRow: selectedRows) {
appendMapDescriptor(*descriptors[selectedRow.row()]); // TODO handle mutators with care when duplicating
}
}
void MapDescriptorWidget::removeSelectedMapDescriptors() {
auto selectedRows = selectionModel()->selectedRows();
std::sort(selectedRows.begin(), selectedRows.end(), [](const QModelIndex &A, const QModelIndex &B) { return A.row() > B.row(); });
for (auto &selectedRow: selectedRows) {
descriptors.remove(selectedRow.row());
removeRow(selectedRow.row());
dirty = true;
}
}
void MapDescriptorWidget::clearDescriptors() {
clearContents();
setRowCount(0);
descriptors.clear();
dirty = false;
}
const QVector<QSharedPointer<MapDescriptor>> &MapDescriptorWidget::getDescriptors() {
return descriptors;
}
void MapDescriptorWidget::setGameDirectoryFunction(const std::function<QString()> &fn) {
getGameDirectory = fn;
}
void MapDescriptorWidget::setImportDirectoryFunction(const std::function<QString ()> &fn)
{
getImportDirectory = fn;
}