diff --git a/include/mainwindow.h b/include/mainwindow.h index b8653d58..973c3ed3 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -389,6 +389,7 @@ private: QObjectList shortcutableObjects() const; void addCustomHeaderValue(QString key, QJsonValue value, bool isNew = false); + int insertTilesetLabel(QStringList * list, QString label); }; enum MapListUserRoles { diff --git a/include/project.h b/include/project.h index ed009374..c5599489 100644 --- a/include/project.h +++ b/include/project.h @@ -171,8 +171,7 @@ public: QString defaultSong; QStringList getVisibilities(); - void insertTilesetLabel(QString label, bool isSecondary); - void insertTilesetLabel(QString label, QString isSecondaryStr); + void appendTilesetLabel(QString label, QString isSecondaryStr); bool readTilesetLabels(); bool readTilesetProperties(); bool readMaxMapDataSize(); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index c01eda47..cd3b62d0 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1239,6 +1239,15 @@ void MainWindow::on_action_NewMap_triggered() { 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() { NewTilesetDialog *createTilesetDialog = new NewTilesetDialog(editor->project, this); 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.appendToMetatiles(editor->project->root, createTilesetDialog->friendlyName, editor->project->usingAsmTilesets); - if(!createTilesetDialog->isSecondary) { - this->ui->comboBox_PrimaryTileset->addItem(createTilesetDialog->fullSymbolName); + if (!createTilesetDialog->isSecondary) { + int index = insertTilesetLabel(&editor->project->primaryTilesetLabels, createTilesetDialog->fullSymbolName); + this->ui->comboBox_PrimaryTileset->insertItem(index, createTilesetDialog->fullSymbolName); } 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); msgBox.setText("Successfully created tileset."); diff --git a/src/project.cpp b/src/project.cpp index 1ae57898..af90cd9d 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -1851,22 +1851,16 @@ QString Project::getDefaultSecondaryTilesetLabel() { return defaultLabel; } -void Project::insertTilesetLabel(QString label, bool isSecondary) { - if (!isSecondary) - this->primaryTilesetLabels.append(label); - else - this->secondaryTilesetLabels.append(label); - this->tilesetLabelsOrdered.append(label); -} - -void Project::insertTilesetLabel(QString label, QString isSecondaryStr) { +void Project::appendTilesetLabel(QString label, QString isSecondaryStr) { bool ok; bool isSecondary = ParseUtil::gameStringToBool(isSecondaryStr, &ok); if (!ok) { logError(QString("Unable to convert value '%1' of isSecondary to bool for tileset %2.").arg(isSecondaryStr).arg(label)); return; } - insertTilesetLabel(label, isSecondary); + QStringList * list = isSecondary ? &this->secondaryTilesetLabels : &this->primaryTilesetLabels; + list->append(label); + this->tilesetLabelsOrdered.append(label); } bool Project::readTilesetLabels() { @@ -1891,7 +1885,7 @@ bool Project::readTilesetLabels() { QRegularExpressionMatchIterator iter = re.globalMatch(text); while (iter.hasNext()) { 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 } else { @@ -1900,7 +1894,7 @@ bool Project::readTilesetLabels() { QStringList labels = structs.keys(); // TODO: This is alphabetical, AdvanceMap import wants the vanilla order in tilesetLabelsOrdered for (const auto tilesetLabel : labels){ - insertTilesetLabel(tilesetLabel, structs[tilesetLabel].value("isSecondary")); + appendTilesetLabel(tilesetLabel, structs[tilesetLabel].value("isSecondary")); } }