Insert new tilesets in sorted order

This commit is contained in:
GriffinR 2022-10-24 09:33:51 -04:00
parent 09ce5b5913
commit 8fbcee7f21
4 changed files with 23 additions and 18 deletions

View file

@ -389,6 +389,7 @@ private:
QObjectList shortcutableObjects() const; QObjectList shortcutableObjects() const;
void addCustomHeaderValue(QString key, QJsonValue value, bool isNew = false); void addCustomHeaderValue(QString key, QJsonValue value, bool isNew = false);
int insertTilesetLabel(QStringList * list, QString label);
}; };
enum MapListUserRoles { enum MapListUserRoles {

View file

@ -171,8 +171,7 @@ public:
QString defaultSong; QString defaultSong;
QStringList getVisibilities(); QStringList getVisibilities();
void insertTilesetLabel(QString label, bool isSecondary); void appendTilesetLabel(QString label, QString isSecondaryStr);
void insertTilesetLabel(QString label, QString isSecondaryStr);
bool readTilesetLabels(); bool readTilesetLabels();
bool readTilesetProperties(); bool readTilesetProperties();
bool readMaxMapDataSize(); bool readMaxMapDataSize();

View file

@ -1239,6 +1239,15 @@ void MainWindow::on_action_NewMap_triggered() {
openNewMapPopupWindow(MapSortOrder::Group, 0); openNewMapPopupWindow(MapSortOrder::Group, 0);
} }
// Insert label for newly-created tileset into sorted list of existing labels
int MainWindow::insertTilesetLabel(QStringList * list, QString label) {
int i = 0;
for (; i < list->length(); i++)
if (list->at(i) > label) break;
list->insert(i, label);
return i;
}
void MainWindow::on_actionNew_Tileset_triggered() { void MainWindow::on_actionNew_Tileset_triggered() {
NewTilesetDialog *createTilesetDialog = new NewTilesetDialog(editor->project, this); NewTilesetDialog *createTilesetDialog = new NewTilesetDialog(editor->project, this);
if(createTilesetDialog->exec() == QDialog::Accepted){ if(createTilesetDialog->exec() == QDialog::Accepted){
@ -1324,12 +1333,14 @@ void MainWindow::on_actionNew_Tileset_triggered() {
newSet.appendToGraphics(editor->project->root, createTilesetDialog->friendlyName, editor->project->usingAsmTilesets); newSet.appendToGraphics(editor->project->root, createTilesetDialog->friendlyName, editor->project->usingAsmTilesets);
newSet.appendToMetatiles(editor->project->root, createTilesetDialog->friendlyName, editor->project->usingAsmTilesets); newSet.appendToMetatiles(editor->project->root, createTilesetDialog->friendlyName, editor->project->usingAsmTilesets);
if(!createTilesetDialog->isSecondary) { if (!createTilesetDialog->isSecondary) {
this->ui->comboBox_PrimaryTileset->addItem(createTilesetDialog->fullSymbolName); int index = insertTilesetLabel(&editor->project->primaryTilesetLabels, createTilesetDialog->fullSymbolName);
this->ui->comboBox_PrimaryTileset->insertItem(index, createTilesetDialog->fullSymbolName);
} else { } else {
this->ui->comboBox_SecondaryTileset->addItem(createTilesetDialog->fullSymbolName); int index = insertTilesetLabel(&editor->project->secondaryTilesetLabels, createTilesetDialog->fullSymbolName);
this->ui->comboBox_SecondaryTileset->insertItem(index, createTilesetDialog->fullSymbolName);
} }
editor->project->insertTilesetLabel(createTilesetDialog->fullSymbolName, createTilesetDialog->isSecondary); insertTilesetLabel(&editor->project->tilesetLabelsOrdered, createTilesetDialog->fullSymbolName);
QMessageBox msgBox(this); QMessageBox msgBox(this);
msgBox.setText("Successfully created tileset."); msgBox.setText("Successfully created tileset.");

View file

@ -1851,22 +1851,16 @@ QString Project::getDefaultSecondaryTilesetLabel() {
return defaultLabel; return defaultLabel;
} }
void Project::insertTilesetLabel(QString label, bool isSecondary) { void Project::appendTilesetLabel(QString label, QString isSecondaryStr) {
if (!isSecondary)
this->primaryTilesetLabels.append(label);
else
this->secondaryTilesetLabels.append(label);
this->tilesetLabelsOrdered.append(label);
}
void Project::insertTilesetLabel(QString label, QString isSecondaryStr) {
bool ok; bool ok;
bool isSecondary = ParseUtil::gameStringToBool(isSecondaryStr, &ok); bool isSecondary = ParseUtil::gameStringToBool(isSecondaryStr, &ok);
if (!ok) { if (!ok) {
logError(QString("Unable to convert value '%1' of isSecondary to bool for tileset %2.").arg(isSecondaryStr).arg(label)); logError(QString("Unable to convert value '%1' of isSecondary to bool for tileset %2.").arg(isSecondaryStr).arg(label));
return; return;
} }
insertTilesetLabel(label, isSecondary); QStringList * list = isSecondary ? &this->secondaryTilesetLabels : &this->primaryTilesetLabels;
list->append(label);
this->tilesetLabelsOrdered.append(label);
} }
bool Project::readTilesetLabels() { bool Project::readTilesetLabels() {
@ -1891,7 +1885,7 @@ bool Project::readTilesetLabels() {
QRegularExpressionMatchIterator iter = re.globalMatch(text); QRegularExpressionMatchIterator iter = re.globalMatch(text);
while (iter.hasNext()) { while (iter.hasNext()) {
QRegularExpressionMatch match = iter.next(); QRegularExpressionMatch match = iter.next();
insertTilesetLabel(match.captured("label"), match.captured("isSecondary")); appendTilesetLabel(match.captured("label"), match.captured("isSecondary"));
} }
filename = asm_filename; // For error reporting further down filename = asm_filename; // For error reporting further down
} else { } else {
@ -1900,7 +1894,7 @@ bool Project::readTilesetLabels() {
QStringList labels = structs.keys(); QStringList labels = structs.keys();
// TODO: This is alphabetical, AdvanceMap import wants the vanilla order in tilesetLabelsOrdered // TODO: This is alphabetical, AdvanceMap import wants the vanilla order in tilesetLabelsOrdered
for (const auto tilesetLabel : labels){ for (const auto tilesetLabel : labels){
insertTilesetLabel(tilesetLabel, structs[tilesetLabel].value("isSecondary")); appendTilesetLabel(tilesetLabel, structs[tilesetLabel].value("isSecondary"));
} }
} }