From 794b814c470e6c60e2a532720e7ec939b71c1ef7 Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Tue, 13 Feb 2018 21:01:16 -0800 Subject: [PATCH 01/13] Add right-click context menu for map groups --- mainwindow.cpp | 45 +++++++++++++++++++++++++++++++++++++++++---- mainwindow.h | 9 +++++++++ 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index 5a5dcfc4..54cad524 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -293,13 +293,13 @@ void MainWindow::populateMapList() { mapIcon.addFile(QStringLiteral(":/icons/map.ico"), QSize(), QIcon::Normal, QIcon::Off); mapIcon.addFile(QStringLiteral(":/icons/image.ico"), QSize(), QIcon::Normal, QIcon::On); - QStandardItemModel *model = new QStandardItemModel; + mapListModel = new QStandardItemModel; QStandardItem *entry = new QStandardItem; entry->setText(project->getProjectTitle()); entry->setIcon(folderIcon); entry->setEditable(false); - model->appendRow(entry); + mapListModel->appendRow(entry); QStandardItem *maps = new QStandardItem; maps->setText("maps"); @@ -314,6 +314,8 @@ void MainWindow::populateMapList() { group->setText(group_name); group->setIcon(mapFolderIcon); group->setEditable(false); + group->setData(group_name, Qt::UserRole); + group->setData("map_group", MapListUserRoles::TypeRole); maps->appendRow(group); QStringList *names = project->groupedMapNames->value(i); for (int j = 0; j < names->length(); j++) { @@ -323,17 +325,52 @@ void MainWindow::populateMapList() { map->setIcon(mapIcon); map->setEditable(false); map->setData(map_name, Qt::UserRole); + map->setData("map_name", MapListUserRoles::TypeRole); group->appendRow(map); - //ui->mapList->setExpanded(model->indexFromItem(map), false); // redundant } } - ui->mapList->setModel(model); + // Right-clicking on items in the map list tree view brings up a context menu. + ui->mapList->setContextMenuPolicy(Qt::CustomContextMenu); + connect(ui->mapList, SIGNAL(customContextMenuRequested(const QPoint &)), + this, SLOT(onOpenMapListContextMenu(const QPoint &))); + + ui->mapList->setModel(mapListModel); ui->mapList->setUpdatesEnabled(true); ui->mapList->expandToDepth(2); ui->mapList->repaint(); } +void MainWindow::onOpenMapListContextMenu(const QPoint &point) +{ + QModelIndex index = ui->mapList->indexAt(point); + if (!index.isValid()) { + return; + } + + QStandardItem *selectedItem = mapListModel->itemFromIndex(index); + QVariant itemType = selectedItem->data(MapListUserRoles::TypeRole); + if (!itemType.isValid()) { + return; + } + + // Build custom context menu depending on which type of item was selected (map group, map name, etc.) + if (itemType == "map_group") { + QString groupName = selectedItem->data(Qt::UserRole).toString(); + QMenu* menu = new QMenu(); + QActionGroup* actions = new QActionGroup(menu); + actions->addAction(menu->addAction("Add New Map to Group"))->setData(groupName); + connect(actions, SIGNAL(triggered(QAction*)), this, SLOT(addNewMapToGroup(QAction*))); + menu->exec(QCursor::pos()); + } +} + +void MainWindow::addNewMapToGroup(QAction* triggeredAction) +{ + QString groupName = triggeredAction->data().toString(); + qDebug() << "Adding new map " << groupName; +} + void MainWindow::on_mapList_activated(const QModelIndex &index) { QVariant data = index.data(Qt::UserRole); diff --git a/mainwindow.h b/mainwindow.h index 6e0b5bcf..36a8010f 100755 --- a/mainwindow.h +++ b/mainwindow.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -65,8 +66,12 @@ private slots: void on_toolButton_Dropper_clicked(); + void onOpenMapListContextMenu(const QPoint &point); + void addNewMapToGroup(QAction* triggeredAction); + private: Ui::MainWindow *ui; + QStandardItemModel *mapListModel; Editor *editor = NULL; void setMap(QString); void populateMapList(); @@ -83,4 +88,8 @@ private: void checkToolButtons(); }; +enum MapListUserRoles { + TypeRole = Qt::UserRole + 10, // Used to differentiate between the different layers of the map list tree view. +}; + #endif // MAINWINDOW_H From 3fcad085c30db320254f9783060de2e5b7f5e0f3 Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Wed, 14 Feb 2018 20:33:55 -0800 Subject: [PATCH 02/13] Add new map to group that was selected (UI functionality only) --- mainwindow.cpp | 44 ++++++++++++++++++++++++++++++-------------- mainwindow.h | 6 +++++- map.h | 10 ++++++++++ project.cpp | 36 +++++++++++++++++++++++++++++------- project.h | 8 ++++++-- 5 files changed, 80 insertions(+), 24 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index 54cad524..7b367347 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -289,11 +289,12 @@ void MainWindow::populateMapList() { QIcon folderIcon; folderIcon.addFile(QStringLiteral(":/icons/folder_closed.ico"), QSize(), QIcon::Normal, QIcon::Off); - QIcon mapIcon; - mapIcon.addFile(QStringLiteral(":/icons/map.ico"), QSize(), QIcon::Normal, QIcon::Off); - mapIcon.addFile(QStringLiteral(":/icons/image.ico"), QSize(), QIcon::Normal, QIcon::On); + mapIcon = new QIcon; + mapIcon->addFile(QStringLiteral(":/icons/map.ico"), QSize(), QIcon::Normal, QIcon::Off); + mapIcon->addFile(QStringLiteral(":/icons/image.ico"), QSize(), QIcon::Normal, QIcon::On); mapListModel = new QStandardItemModel; + mapGroupsModel = new QList; QStandardItem *entry = new QStandardItem; entry->setText(project->getProjectTitle()); @@ -316,16 +317,13 @@ void MainWindow::populateMapList() { group->setEditable(false); group->setData(group_name, Qt::UserRole); group->setData("map_group", MapListUserRoles::TypeRole); + group->setData(i, MapListUserRoles::GroupRole); maps->appendRow(group); + mapGroupsModel->append(group); QStringList *names = project->groupedMapNames->value(i); for (int j = 0; j < names->length(); j++) { QString map_name = names->value(j); - QStandardItem *map = new QStandardItem; - map->setText(QString("[%1.%2] ").arg(i).arg(j, 2, 10, QLatin1Char('0')) + map_name); - map->setIcon(mapIcon); - map->setEditable(false); - map->setData(map_name, Qt::UserRole); - map->setData("map_name", MapListUserRoles::TypeRole); + QStandardItem *map = createMapItem(map_name, i, j); group->appendRow(map); } } @@ -341,6 +339,16 @@ void MainWindow::populateMapList() { ui->mapList->repaint(); } +QStandardItem* MainWindow::createMapItem(QString mapName, int groupNum, int inGroupNum) { + QStandardItem *map = new QStandardItem; + map->setText(QString("[%1.%2] ").arg(groupNum).arg(inGroupNum, 2, 10, QLatin1Char('0')) + mapName); + map->setIcon(*mapIcon); + map->setEditable(false); + map->setData(mapName, Qt::UserRole); + map->setData("map_name", MapListUserRoles::TypeRole); + return map; +} + void MainWindow::onOpenMapListContextMenu(const QPoint &point) { QModelIndex index = ui->mapList->indexAt(point); @@ -357,18 +365,26 @@ void MainWindow::onOpenMapListContextMenu(const QPoint &point) // Build custom context menu depending on which type of item was selected (map group, map name, etc.) if (itemType == "map_group") { QString groupName = selectedItem->data(Qt::UserRole).toString(); + int groupNum = selectedItem->data(MapListUserRoles::GroupRole).toInt(); QMenu* menu = new QMenu(); QActionGroup* actions = new QActionGroup(menu); - actions->addAction(menu->addAction("Add New Map to Group"))->setData(groupName); - connect(actions, SIGNAL(triggered(QAction*)), this, SLOT(addNewMapToGroup(QAction*))); + actions->addAction(menu->addAction("Add New Map to Group"))->setData(groupNum); + connect(actions, SIGNAL(triggered(QAction*)), this, SLOT(onAddNewMapToGroupClick(QAction*))); menu->exec(QCursor::pos()); } } -void MainWindow::addNewMapToGroup(QAction* triggeredAction) +void MainWindow::onAddNewMapToGroupClick(QAction* triggeredAction) { - QString groupName = triggeredAction->data().toString(); - qDebug() << "Adding new map " << groupName; + int groupNum = triggeredAction->data().toInt(); + QStandardItem* groupItem = mapGroupsModel->at(groupNum); + + QString newMapName = editor->project->getNewMapName(); + editor->project->addNewMapToGroup(newMapName, groupNum); + + int numMapsInGroup = groupItem->rowCount(); + QStandardItem *newMapItem = createMapItem(newMapName, groupNum, numMapsInGroup); + groupItem->appendRow(newMapItem); } void MainWindow::on_mapList_activated(const QModelIndex &index) diff --git a/mainwindow.h b/mainwindow.h index 36a8010f..a0dc1c1b 100755 --- a/mainwindow.h +++ b/mainwindow.h @@ -67,18 +67,21 @@ private slots: void on_toolButton_Dropper_clicked(); void onOpenMapListContextMenu(const QPoint &point); - void addNewMapToGroup(QAction* triggeredAction); + void onAddNewMapToGroupClick(QAction* triggeredAction); private: Ui::MainWindow *ui; QStandardItemModel *mapListModel; + QList *mapGroupsModel; Editor *editor = NULL; + QIcon* mapIcon; void setMap(QString); void populateMapList(); QString getExistingDirectory(QString); void openProject(QString dir); QString getDefaultMap(); void setRecentMap(QString map_name); + QStandardItem* createMapItem(QString mapName, int groupNum, int inGroupNum); void markAllEdited(QAbstractItemModel *model); void markEdited(QModelIndex index); @@ -89,6 +92,7 @@ private: }; enum MapListUserRoles { + GroupRole = Qt::UserRole + 1, // Used to hold the map group number. TypeRole = Qt::UserRole + 10, // Used to differentiate between the different layers of the map list tree view. }; diff --git a/map.h b/map.h index 30b8bd9b..9703215f 100755 --- a/map.h +++ b/map.h @@ -76,6 +76,7 @@ public: public: QString name; QString constantName; + QString group_num; QString attributes_label; QString events_label; QString scripts_label; @@ -190,4 +191,13 @@ signals: public slots: }; +class MapGroup : public QObject +{ + Q_OBJECT +public: + QString name; + int group_num; + QList maps; +}; + #endif // MAP_H diff --git a/project.cpp b/project.cpp index 812478fc..6e9c55e6 100755 --- a/project.cpp +++ b/project.cpp @@ -8,15 +8,19 @@ #include #include #include +#include #include #include Project::Project() { groupNames = new QStringList; + map_groups = new QMap; groupedMapNames = new QList; mapNames = new QStringList; map_cache = new QMap; + mapConstantsToMapNames = new QMap; + mapNamesToMapConstants = new QMap; tileset_cache = new QMap; } @@ -67,8 +71,8 @@ void Project::loadMapConnections(Map *map) { connection->direction = command.value(1); connection->offset = command.value(2); QString mapConstant = command.value(3); - if (mapConstantsToMapNames.contains(mapConstant)) { - connection->map_name = mapConstantsToMapNames[mapConstant]; + if (mapConstantsToMapNames->contains(mapConstant)) { + connection->map_name = mapConstantsToMapNames->value(mapConstant); map->connections.append(connection); } else { qDebug() << QString("Failed to find connected map for map constant '%1'").arg(mapConstant); @@ -521,11 +525,12 @@ void Project::readMapGroups() { QStringList *list = groupedMaps->value(group); list->append(mapName); maps->append(mapName); + map_groups->insert(mapName, group); // Build the mapping and reverse mapping between map constants and map names. QString mapConstant = Map::mapConstantFromName(mapName); - mapConstantsToMapNames.insert(mapConstant, mapName); - mapNamesToMapConstants.insert(mapName, mapConstant); + mapConstantsToMapNames->insert(mapConstant, mapName); + mapNamesToMapConstants->insert(mapName, mapConstant); } } } @@ -536,6 +541,23 @@ void Project::readMapGroups() { mapNames = maps; } +void Project::addNewMapToGroup(QString mapName, int groupNum) { + mapNames->append(mapName); + map_groups->insert(mapName, groupNum); + groupedMapNames->value(groupNum)->append(mapName); +} + +QString Project::getNewMapName() { + // Ensure default name doesn't already exist. + int i = 0; + QString newMapName; + do { + newMapName = QString("NewMap%1").arg(++i); + } while (mapNames->contains(newMapName)); + + return newMapName; +} + QList* Project::parse(QString text) { Asm *parser = new Asm; return parser->parse(text); @@ -739,7 +761,7 @@ void Project::saveMapEvents(Map *map) { text += QString(", %1").arg(warp->get("y")); text += QString(", %1").arg(warp->get("elevation")); text += QString(", %1").arg(warp->get("destination_warp")); - text += QString(", %1").arg(mapNamesToMapConstants[warp->get("destination_map_name")]); + text += QString(", %1").arg(mapNamesToMapConstants->value(warp->get("destination_map_name"))); text += "\n"; } text += "\n"; @@ -882,8 +904,8 @@ void Project::readMapEvents(Map *map) { // Ensure the warp destination map constant is valid before adding it to the warps. QString mapConstant = command.value(i++); - if (mapConstantsToMapNames.contains(mapConstant)) { - warp->put("destination_map_name", mapConstantsToMapNames[mapConstant]); + if (mapConstantsToMapNames->contains(mapConstant)) { + warp->put("destination_map_name", mapConstantsToMapNames->value(mapConstant)); warp->put("event_type", "warp"); map->events["warp"].append(warp); } else { diff --git a/project.h b/project.h index ebf1ed28..34987668 100755 --- a/project.h +++ b/project.h @@ -6,6 +6,7 @@ #include #include +#include class Project { @@ -13,10 +14,11 @@ public: Project(); QString root; QStringList *groupNames = NULL; + QMap *map_groups; QList *groupedMapNames = NULL; QStringList *mapNames = NULL; - QMap mapConstantsToMapNames; - QMap mapNamesToMapConstants; + QMap *mapConstantsToMapNames; + QMap *mapNamesToMapConstants; QMap *map_cache; Map* loadMap(QString); @@ -33,6 +35,8 @@ public: void saveTextFile(QString path, QString text); void readMapGroups(); + void addNewMapToGroup(QString mapName, int groupNum); + QString getNewMapName(); QString getProjectTitle(); QList* getLabelMacros(QList*, QString); From f6cb0025924a8db66a80bff16e9a3be8f860d14a Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Wed, 14 Feb 2018 22:31:23 -0800 Subject: [PATCH 03/13] Write files on add map functionality. Still need to address gMapGroups --- project.cpp | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) diff --git a/project.cpp b/project.cpp index 6e9c55e6..c74f370f 100755 --- a/project.cpp +++ b/project.cpp @@ -6,6 +6,7 @@ #include "event.h" #include +#include #include #include #include @@ -542,6 +543,154 @@ void Project::readMapGroups() { } void Project::addNewMapToGroup(QString mapName, int groupNum) { + int mapIndex = 0;// TODO: need to calculate the new map index. + + // Write new map to project files. + // 1. Create directory data/maps// + // 2. Create file data/maps//border.bin + // 3. Create file data/maps//header.inc + // 4. Create file data/maps//map.bin + // 5. Create file data/maps/events/.inc + // 6. Create file data/scripts/maps/.inc + // 7. Create file data/text/maps/.inc + // 8. Modify data/event_scripts.s: + // .include "data/scripts/maps/.inc" + // .include "data/text/maps/.inc" + // 9. Modify data/map_events.s: + // .include "data/maps/events/.inc" + // 10. Modify data/maps/_assets.inc + // 11. Modify data/maps/_groups.inc + // 12. Modify data/maps/attributes_table.inc + // 13. Modify data/maps/headers.inc + + // 1. Create directory data/maps// + QString dataDir = QString("%1/data/").arg(root); + QString dataMapsDir = QString("%1maps/").arg(dataDir); + QString newMapDataDir = QString("%1%2/").arg(dataMapsDir).arg(mapName); + if (!QDir::root().mkdir(newMapDataDir)) { + qDebug() << "Error: failed to create directory for new map. " << newMapDataDir; + return; + } + + // 2. Create file data/maps//border.bin + QFile borderFile(newMapDataDir + "border.bin"); + borderFile.open(QIODevice::WriteOnly); + QDataStream borderStream(&borderFile); + borderStream.setByteOrder(QDataStream::LittleEndian); + borderStream << qint16(0x01D4) << qint16(0x01D5) << qint16(0x01DC) << qint16(0x01DD); + borderFile.close(); + + // 3. Create file data/maps//header.inc + QFile headerFile(newMapDataDir + "header.inc"); + headerFile.open(QIODevice::WriteOnly); + QTextStream headerStream(&headerFile); + headerStream << mapName << "::" << endl + << "\t.4byte " << mapName << "_MapAttributes" << endl + << "\t.4byte " << mapName << "_MapEvents" << endl + << "\t.4byte " << mapName << "_MapScripts" << endl + << "\t.4byte 0x0" << endl + << "\t.2byte BGM_DAN02" << endl + << "\t.2byte " << mapIndex << endl + << "\t.byte 0" << endl + << "\t.byte 0" << endl + << "\t.byte 11" << endl + << "\t.byte 4" << endl + << "\t.2byte 0" << endl + << "\t.byte 1" << endl + << "\t.byte 0" << endl; + headerFile.close(); + + // 4. Create file data/maps//map.bin + QFile mapFile(newMapDataDir + "map.bin"); + mapFile.open(QIODevice::WriteOnly); + QDataStream mapStream(&mapFile); + mapStream.setByteOrder(QDataStream::LittleEndian); + for (int i = 0; i < 20 * 20; i++) { + mapStream << qint16(0x3001); + } + mapFile.close(); + + // 5. Create file data/maps/events/.inc + QFile eventsFile(dataMapsDir + "events/" + mapName + ".inc"); + eventsFile.open(QIODevice::WriteOnly); + QTextStream eventsStream(&eventsFile); + eventsStream << mapName << "_MapEvents::" << endl + << "\tmap_events 0x0, 0x0, 0x0, 0x0" << endl; + eventsFile.close(); + + // 6. Create file data/scripts/maps/.inc + QFile scriptsFile(dataDir + "scripts/maps/" + mapName + ".inc"); + scriptsFile.open(QIODevice::WriteOnly); + QTextStream scriptsStream(&scriptsFile); + scriptsStream << mapName << "_MapScripts::" << endl + << "\t.byte 0" << endl; + scriptsFile.close(); + + // 7. Create file data/text/maps/.inc + QFile textFile(dataDir + "text/maps/" + mapName + ".inc"); + textFile.open(QIODevice::WriteOnly); + QTextStream textStream(&textFile); + textStream << endl; + textFile.close(); + + // 8. Modify data/event_scripts.s: + QFile eventScriptsFile(dataDir + "event_scripts.s"); + eventScriptsFile.open(QIODevice::Append); + QTextStream eventScriptsStream(&eventScriptsFile); + eventScriptsStream << endl + << "\t.include \"data/scripts/maps/" << mapName << ".inc\"" << endl + << "\t.include \"data/text/maps/" << mapName << ".inc\"" << endl; + eventScriptsFile.close(); + + // 9. Modify data/map_events.s: + QFile mapEventsFile(dataDir + "map_events.s"); + mapEventsFile.open(QIODevice::Append); + QTextStream mapEventsStream(&mapEventsFile); + mapEventsStream << endl + << "\t.include \"data/maps/events/" << mapName << ".inc\"" << endl; + mapEventsFile.close(); + + // 10. Modify data/maps/_assets.inc + QFile assetsFile(dataMapsDir + "_assets.inc"); + assetsFile.open(QIODevice::Append); + QTextStream assetsStream(&assetsFile); + assetsStream << endl + << mapName << "_MapBorder::" << endl + << "\t.incbin \"data/maps/" << mapName << "/border.bin\"" << endl + << endl + << mapName << "_MapBlockdata::" << endl + << "\t.incbin \"data/maps/" << mapName << "/map.bin\"" << endl + << endl + << "\t.align 2" << endl + << mapName << "_MapAttributes::" << endl + << "\t.4byte 0x14" << endl + << "\t.4byte 0x14" << endl + << "\t.4byte " << mapName << "_MapBorder" << endl + << "\t.4byte " << mapName << "_MapBlockdata" << endl + << "\t.4byte gTileset_General" << endl + << "\t.4byte gTileset_Pacifidlog" << endl + << endl; + assetsFile.close(); + + // 11. Modify data/maps/_groups.inc + // TODO: + + // 12. Modify data/maps/attributes_table.inc + QFile attributesFile(dataMapsDir + "attributes_table.inc"); + attributesFile.open(QIODevice::Append); + QTextStream attributesStream(&attributesFile); + attributesStream << endl + << "\t.4byte " << mapName << "_MapAttributes" << endl; + attributesFile.close(); + + // 13. Modify data/maps/headers.inc + QFile headersFile(dataMapsDir + "headers.inc"); + headersFile.open(QIODevice::Append); + QTextStream headersStream(&headersFile); + headersStream << endl + << "\t.include \"data/maps/" << mapName << "/header.inc\"" << endl; + headersFile.close(); + mapNames->append(mapName); map_groups->insert(mapName, groupNum); groupedMapNames->value(groupNum)->append(mapName); From 40a0fbee0212bc6235bd997014bf77af31a7d082 Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Thu, 15 Feb 2018 18:38:19 -0800 Subject: [PATCH 04/13] Load and save the gMapAttributes array --- editor.cpp | 1 + mainwindow.cpp | 6 +++++ mainwindow.h | 1 + project.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++++--- project.h | 8 ++++++- 5 files changed, 72 insertions(+), 4 deletions(-) diff --git a/editor.cpp b/editor.cpp index b00c7948..4ccb9df9 100755 --- a/editor.cpp +++ b/editor.cpp @@ -9,6 +9,7 @@ Editor::Editor() void Editor::saveProject() { if (project) { + project->saveAllDataStructures(); project->saveAllMaps(); } } diff --git a/mainwindow.cpp b/mainwindow.cpp index 7b367347..42320bee 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -61,10 +61,12 @@ void MainWindow::openProject(QString dir) { editor->project = new Project; editor->project->root = dir; setWindowTitle(editor->project->getProjectTitle() + " - pretmap"); + loadDataStructures(); populateMapList(); setMap(getDefaultMap()); } else { setWindowTitle(editor->project->getProjectTitle() + " - pretmap"); + loadDataStructures(); populateMapList(); } } @@ -278,6 +280,10 @@ void MainWindow::on_checkBox_ShowLocation_clicked(bool checked) } } +void MainWindow::loadDataStructures() { + Project *project = editor->project; + project->readMapAttributesTable(); +} void MainWindow::populateMapList() { Project *project = editor->project; diff --git a/mainwindow.h b/mainwindow.h index a0dc1c1b..f9a911e1 100755 --- a/mainwindow.h +++ b/mainwindow.h @@ -76,6 +76,7 @@ private: Editor *editor = NULL; QIcon* mapIcon; void setMap(QString); + void loadDataStructures(); void populateMapList(); QString getExistingDirectory(QString); void openProject(QString dir); diff --git a/project.cpp b/project.cpp index c74f370f..aa94b9af 100755 --- a/project.cpp +++ b/project.cpp @@ -22,6 +22,7 @@ Project::Project() map_cache = new QMap; mapConstantsToMapNames = new QMap; mapNamesToMapConstants = new QMap; + mapAttributesTable = new QMap; tileset_cache = new QMap; } @@ -171,6 +172,53 @@ void Project::saveMapHeader(Map *map) { saveTextFile(header_path, text); } +void Project::readMapAttributesTable() { + int curMapIndex = 1; + QString attributesText = readTextFile(getMapAttributesTableFilepath()); + QList* values = parse(attributesText); + bool inAttributePointers = false; + for (int i = 0; i < values->length(); i++) { + QStringList params = values->value(i); + QString macro = params.value(0); + if (macro == ".label") { + if (inAttributePointers) { + break; + } + if (params.value(1) == "gMapAttributes") { + inAttributePointers = true; + } + } else if (macro == ".4byte" && inAttributePointers) { + QString mapName = params.value(1); + if (!mapName.contains("UnknownMapAttributes")) { + // Strip off "_MapAttributes" from the label if it's a real map label. + mapName = mapName.remove(mapName.length() - 14, 14); + } + mapAttributesTable->insert(curMapIndex, mapName); + curMapIndex++; + } + } +} + +void Project::saveMapAttributesTable() { + QString text = ""; + text += QString("\t.align 2\n"); + text += QString("gMapAttributes::\n"); + for (int i = 0; i < mapAttributesTable->count(); i++) { + int mapIndex = i + 1; + QString mapName = mapAttributesTable->value(mapIndex); + if (!mapName.contains("UnknownMapAttributes")) { + text += QString("\t.4byte %1_MapAttributes\n").arg(mapName); + } else { + text += QString("\t.4byte %1\n").arg(mapName); + } + } + saveTextFile(getMapAttributesTableFilepath(), text); +} + +QString Project::getMapAttributesTableFilepath() { + return QString("%1/data/maps/attributes_table.inc").arg(root); +} + void Project::readMapAttributes(Map* map) { Asm *parser = new Asm; @@ -277,6 +325,10 @@ void Project::saveMap(Map *map) { saveMapEvents(map); } +void Project::saveAllDataStructures() { + saveMapAttributesTable(); +} + void Project::loadTilesetAssets(Tileset* tileset) { Asm* parser = new Asm; QString category = (tileset->is_secondary == "TRUE") ? "secondary" : "primary"; @@ -543,8 +595,6 @@ void Project::readMapGroups() { } void Project::addNewMapToGroup(QString mapName, int groupNum) { - int mapIndex = 0;// TODO: need to calculate the new map index. - // Write new map to project files. // 1. Create directory data/maps// // 2. Create file data/maps//border.bin @@ -563,10 +613,14 @@ void Project::addNewMapToGroup(QString mapName, int groupNum) { // 12. Modify data/maps/attributes_table.inc // 13. Modify data/maps/headers.inc - // 1. Create directory data/maps// + int mapIndex = mapAttributesTable->count() + 1; + mapAttributesTable->insert(mapIndex, mapName); + QString dataDir = QString("%1/data/").arg(root); QString dataMapsDir = QString("%1maps/").arg(dataDir); QString newMapDataDir = QString("%1%2/").arg(dataMapsDir).arg(mapName); + + // 1. Create directory data/maps// if (!QDir::root().mkdir(newMapDataDir)) { qDebug() << "Error: failed to create directory for new map. " << newMapDataDir; return; diff --git a/project.h b/project.h index 34987668..b37a69ef 100755 --- a/project.h +++ b/project.h @@ -19,6 +19,7 @@ public: QStringList *mapNames = NULL; QMap *mapConstantsToMapNames; QMap *mapNamesToMapConstants; + QMap *mapAttributesTable; QMap *map_cache; Map* loadMap(QString); @@ -42,6 +43,7 @@ public: QList* getLabelMacros(QList*, QString); QStringList* getLabelValues(QList*, QString); void readMapHeader(Map*); + void readMapAttributesTable(); void readMapAttributes(Map*); void getTilesets(Map*); void loadTilesetAssets(Tileset*); @@ -51,7 +53,7 @@ public: void writeBlockdata(QString, Blockdata*); void saveAllMaps(); void saveMap(Map*); - void saveMapHeader(Map*); + void saveAllDataStructures(); QList* parse(QString text); QStringList getSongNames(); @@ -77,6 +79,10 @@ public: QStringList readCArray(QString text, QString label); QString readCIncbin(QString text, QString label); QMap readCDefines(QString text, QStringList prefixes); +private: + QString getMapAttributesTableFilepath(); + void saveMapHeader(Map*); + void saveMapAttributesTable(); }; #endif // PROJECT_H From 5c0f628f0d1ab972f242d8a3f62313405eb238f0 Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Thu, 15 Feb 2018 20:15:25 -0800 Subject: [PATCH 05/13] Build new maps in memory, rather than immediately writing to files --- mainwindow.cpp | 2 + map.cpp | 2 +- map.h | 2 + project.cpp | 368 +++++++++++++++++++++++++++++++------------------ project.h | 7 + 5 files changed, 249 insertions(+), 132 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index 42320bee..07079ba1 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -391,6 +391,8 @@ void MainWindow::onAddNewMapToGroupClick(QAction* triggeredAction) int numMapsInGroup = groupItem->rowCount(); QStandardItem *newMapItem = createMapItem(newMapName, groupNum, numMapsInGroup); groupItem->appendRow(newMapItem); + + setMap(newMapName); } void MainWindow::on_mapList_activated(const QModelIndex &index) diff --git a/map.cpp b/map.cpp index 5752ac62..03156002 100755 --- a/map.cpp +++ b/map.cpp @@ -732,5 +732,5 @@ void Map::addEvent(Event *event) { } bool Map::hasUnsavedChanges() { - return !history.isSaved(); + return !history.isSaved() || !isPersistedToFile; } diff --git a/map.h b/map.h index 9703215f..ec2343e3 100755 --- a/map.h +++ b/map.h @@ -103,6 +103,8 @@ public: Blockdata* blockdata = NULL; + bool isPersistedToFile = true; + public: void setName(QString mapName); static QString mapConstantFromName(QString mapName); diff --git a/project.cpp b/project.cpp index aa94b9af..7eb6c6a8 100755 --- a/project.cpp +++ b/project.cpp @@ -35,9 +35,15 @@ QString Project::getProjectTitle() { } Map* Project::loadMap(QString map_name) { - Map *map = new Map; + // New maps are saved to actual files yet, so we need to fetch their data from the map_cache. + Map *map; + if (map_cache->contains(map_name) && !map_cache->value(map_name)->isPersistedToFile) { + map = map_cache->value(map_name); + } else { + map = new Map; + map->setName(map_name); + } - map->setName(map_name); readMapHeader(map); readMapAttributes(map); getTilesets(map); @@ -53,6 +59,10 @@ Map* Project::loadMap(QString map_name) { } void Project::loadMapConnections(Map *map) { + if (!map->isPersistedToFile) { + return; + } + map->connections.clear(); if (!map->connections_label.isNull()) { QString path = root + QString("/data/maps/%1/connections.inc").arg(map->name); @@ -85,6 +95,10 @@ void Project::loadMapConnections(Map *map) { } } +void Project::setNewMapConnections(Map *map) { + map->connections.clear(); +} + QList* Project::getLabelMacros(QList *list, QString label) { bool in_label = false; QList *new_list = new QList; @@ -128,6 +142,10 @@ QStringList* Project::getLabelValues(QList *list, QString label) { } void Project::readMapHeader(Map* map) { + if (!map->isPersistedToFile) { + return; + } + QString label = map->name; Asm *parser = new Asm; @@ -151,6 +169,22 @@ void Project::readMapHeader(Map* map) { map->battle_scene = header->value(12); } +void Project::setNewMapHeader(Map* map, int mapIndex) { + map->attributes_label = QString("%1_MapAttributes").arg(map->name); + map->events_label = QString("%1_MapEvents").arg(map->name);; + map->scripts_label = QString("%1_MapScripts").arg(map->name);; + map->connections_label = "0x0"; + map->song = "BGM_DAN02"; + map->index = mapIndex; + map->location = "0"; + map->visibility = "0"; + map->weather = "2"; + map->type = "1"; + map->unknown = "0"; + map->show_location = "1"; + map->battle_scene = "0"; +} + void Project::saveMapHeader(Map *map) { QString label = map->name; QString header_path = root + "/data/maps/" + label + "/header.inc"; @@ -220,6 +254,10 @@ QString Project::getMapAttributesTableFilepath() { } void Project::readMapAttributes(Map* map) { + if (!map->isPersistedToFile) { + return; + } + Asm *parser = new Asm; QString assets_text = readTextFile(root + "/data/maps/_assets.inc"); @@ -235,6 +273,15 @@ void Project::readMapAttributes(Map* map) { map->tileset_secondary_label = attributes->value(5); } +void Project::setNewMapAttributes(Map* map) { + map->width = "20"; + map->height = "20"; + map->border_label = QString("%1_MapBorder").arg(map->name); + map->blockdata_label = QString("%1_MapBlockdata").arg(map->name); + map->tileset_primary_label = "gTileset_General"; + map->tileset_secondary_label = "gTileset_Petalburg"; +} + void Project::getTilesets(Map* map) { map->tileset_primary = getTileset(map->tileset_primary_label); map->tileset_secondary = getTileset(map->tileset_secondary_label); @@ -287,15 +334,40 @@ QString Project::getMapBorderPath(Map *map) { } void Project::loadBlockdata(Map* map) { + if (!map->isPersistedToFile) { + return; + } + QString path = getBlockdataPath(map); map->blockdata = readBlockdata(path); } +void Project::setNewMapBlockdata(Map* map) { + Blockdata *blockdata = new Blockdata; + for (int i = 0; i < map->getWidth() * map->getHeight(); i++) { + blockdata->addBlock(qint16(0x3001)); + } + map->blockdata = blockdata; +} + void Project::loadMapBorder(Map *map) { + if (!map->isPersistedToFile) { + return; + } + QString path = getMapBorderPath(map); map->border = readBlockdata(path); } +void Project::setNewMapBorder(Map *map) { + Blockdata *blockdata = new Blockdata; + blockdata->addBlock(qint16(0x01D4)); + blockdata->addBlock(qint16(0x01D5)); + blockdata->addBlock(qint16(0x01DC)); + blockdata->addBlock(qint16(0x01DD)); + map->border = blockdata; +} + void Project::saveBlockdata(Map* map) { QString path = getBlockdataPath(map); writeBlockdata(path, map->blockdata); @@ -616,138 +688,154 @@ void Project::addNewMapToGroup(QString mapName, int groupNum) { int mapIndex = mapAttributesTable->count() + 1; mapAttributesTable->insert(mapIndex, mapName); - QString dataDir = QString("%1/data/").arg(root); - QString dataMapsDir = QString("%1maps/").arg(dataDir); - QString newMapDataDir = QString("%1%2/").arg(dataMapsDir).arg(mapName); - - // 1. Create directory data/maps// - if (!QDir::root().mkdir(newMapDataDir)) { - qDebug() << "Error: failed to create directory for new map. " << newMapDataDir; - return; - } - - // 2. Create file data/maps//border.bin - QFile borderFile(newMapDataDir + "border.bin"); - borderFile.open(QIODevice::WriteOnly); - QDataStream borderStream(&borderFile); - borderStream.setByteOrder(QDataStream::LittleEndian); - borderStream << qint16(0x01D4) << qint16(0x01D5) << qint16(0x01DC) << qint16(0x01DD); - borderFile.close(); - - // 3. Create file data/maps//header.inc - QFile headerFile(newMapDataDir + "header.inc"); - headerFile.open(QIODevice::WriteOnly); - QTextStream headerStream(&headerFile); - headerStream << mapName << "::" << endl - << "\t.4byte " << mapName << "_MapAttributes" << endl - << "\t.4byte " << mapName << "_MapEvents" << endl - << "\t.4byte " << mapName << "_MapScripts" << endl - << "\t.4byte 0x0" << endl - << "\t.2byte BGM_DAN02" << endl - << "\t.2byte " << mapIndex << endl - << "\t.byte 0" << endl - << "\t.byte 0" << endl - << "\t.byte 11" << endl - << "\t.byte 4" << endl - << "\t.2byte 0" << endl - << "\t.byte 1" << endl - << "\t.byte 0" << endl; - headerFile.close(); - - // 4. Create file data/maps//map.bin - QFile mapFile(newMapDataDir + "map.bin"); - mapFile.open(QIODevice::WriteOnly); - QDataStream mapStream(&mapFile); - mapStream.setByteOrder(QDataStream::LittleEndian); - for (int i = 0; i < 20 * 20; i++) { - mapStream << qint16(0x3001); - } - mapFile.close(); - - // 5. Create file data/maps/events/.inc - QFile eventsFile(dataMapsDir + "events/" + mapName + ".inc"); - eventsFile.open(QIODevice::WriteOnly); - QTextStream eventsStream(&eventsFile); - eventsStream << mapName << "_MapEvents::" << endl - << "\tmap_events 0x0, 0x0, 0x0, 0x0" << endl; - eventsFile.close(); - - // 6. Create file data/scripts/maps/.inc - QFile scriptsFile(dataDir + "scripts/maps/" + mapName + ".inc"); - scriptsFile.open(QIODevice::WriteOnly); - QTextStream scriptsStream(&scriptsFile); - scriptsStream << mapName << "_MapScripts::" << endl - << "\t.byte 0" << endl; - scriptsFile.close(); - - // 7. Create file data/text/maps/.inc - QFile textFile(dataDir + "text/maps/" + mapName + ".inc"); - textFile.open(QIODevice::WriteOnly); - QTextStream textStream(&textFile); - textStream << endl; - textFile.close(); - - // 8. Modify data/event_scripts.s: - QFile eventScriptsFile(dataDir + "event_scripts.s"); - eventScriptsFile.open(QIODevice::Append); - QTextStream eventScriptsStream(&eventScriptsFile); - eventScriptsStream << endl - << "\t.include \"data/scripts/maps/" << mapName << ".inc\"" << endl - << "\t.include \"data/text/maps/" << mapName << ".inc\"" << endl; - eventScriptsFile.close(); - - // 9. Modify data/map_events.s: - QFile mapEventsFile(dataDir + "map_events.s"); - mapEventsFile.open(QIODevice::Append); - QTextStream mapEventsStream(&mapEventsFile); - mapEventsStream << endl - << "\t.include \"data/maps/events/" << mapName << ".inc\"" << endl; - mapEventsFile.close(); - - // 10. Modify data/maps/_assets.inc - QFile assetsFile(dataMapsDir + "_assets.inc"); - assetsFile.open(QIODevice::Append); - QTextStream assetsStream(&assetsFile); - assetsStream << endl - << mapName << "_MapBorder::" << endl - << "\t.incbin \"data/maps/" << mapName << "/border.bin\"" << endl - << endl - << mapName << "_MapBlockdata::" << endl - << "\t.incbin \"data/maps/" << mapName << "/map.bin\"" << endl - << endl - << "\t.align 2" << endl - << mapName << "_MapAttributes::" << endl - << "\t.4byte 0x14" << endl - << "\t.4byte 0x14" << endl - << "\t.4byte " << mapName << "_MapBorder" << endl - << "\t.4byte " << mapName << "_MapBlockdata" << endl - << "\t.4byte gTileset_General" << endl - << "\t.4byte gTileset_Pacifidlog" << endl - << endl; - assetsFile.close(); - - // 11. Modify data/maps/_groups.inc - // TODO: - - // 12. Modify data/maps/attributes_table.inc - QFile attributesFile(dataMapsDir + "attributes_table.inc"); - attributesFile.open(QIODevice::Append); - QTextStream attributesStream(&attributesFile); - attributesStream << endl - << "\t.4byte " << mapName << "_MapAttributes" << endl; - attributesFile.close(); - - // 13. Modify data/maps/headers.inc - QFile headersFile(dataMapsDir + "headers.inc"); - headersFile.open(QIODevice::Append); - QTextStream headersStream(&headersFile); - headersStream << endl - << "\t.include \"data/maps/" << mapName << "/header.inc\"" << endl; - headersFile.close(); - + // Setup new map in memory, but don't write to file until map is actually saved later. mapNames->append(mapName); map_groups->insert(mapName, groupNum); groupedMapNames->value(groupNum)->append(mapName); + + Map *map = new Map; + map->isPersistedToFile = false; + map->setName(mapName); + setNewMapHeader(map, mapIndex); + setNewMapAttributes(map); + getTilesets(map); + setNewMapBlockdata(map); + setNewMapBorder(map); + setNewMapEvents(map); + setNewMapConnections(map); + map->commit(); + map->history.save(); + map_cache->insert(mapName, map); + + +// QString dataDir = QString("%1/data/").arg(root); +// QString dataMapsDir = QString("%1maps/").arg(dataDir); +// QString newMapDataDir = QString("%1%2/").arg(dataMapsDir).arg(mapName); + +// // 1. Create directory data/maps// +// if (!QDir::root().mkdir(newMapDataDir)) { +// qDebug() << "Error: failed to create directory for new map. " << newMapDataDir; +// return; +// } + +// // 2. Create file data/maps//border.bin +// QFile borderFile(newMapDataDir + "border.bin"); +// borderFile.open(QIODevice::WriteOnly); +// QDataStream borderStream(&borderFile); +// borderStream.setByteOrder(QDataStream::LittleEndian); +// borderStream << qint16(0x01D4) << qint16(0x01D5) << qint16(0x01DC) << qint16(0x01DD); +// borderFile.close(); + +// // 3. Create file data/maps//header.inc +// QFile headerFile(newMapDataDir + "header.inc"); +// headerFile.open(QIODevice::WriteOnly); +// QTextStream headerStream(&headerFile); +// headerStream << mapName << "::" << endl +// << "\t.4byte " << mapName << "_MapAttributes" << endl +// << "\t.4byte " << mapName << "_MapEvents" << endl +// << "\t.4byte " << mapName << "_MapScripts" << endl +// << "\t.4byte 0x0" << endl +// << "\t.2byte BGM_DAN02" << endl +// << "\t.2byte " << mapIndex << endl +// << "\t.byte 0" << endl +// << "\t.byte 0" << endl +// << "\t.byte 11" << endl +// << "\t.byte 4" << endl +// << "\t.2byte 0" << endl +// << "\t.byte 1" << endl +// << "\t.byte 0" << endl; +// headerFile.close(); + +// // 4. Create file data/maps//map.bin +// QFile mapFile(newMapDataDir + "map.bin"); +// mapFile.open(QIODevice::WriteOnly); +// QDataStream mapStream(&mapFile); +// mapStream.setByteOrder(QDataStream::LittleEndian); +// for (int i = 0; i < 20 * 20; i++) { +// mapStream << qint16(0x3001); +// } +// mapFile.close(); + +// // 5. Create file data/maps/events/.inc +// QFile eventsFile(dataMapsDir + "events/" + mapName + ".inc"); +// eventsFile.open(QIODevice::WriteOnly); +// QTextStream eventsStream(&eventsFile); +// eventsStream << mapName << "_MapEvents::" << endl +// << "\tmap_events 0x0, 0x0, 0x0, 0x0" << endl; +// eventsFile.close(); + +// // 6. Create file data/scripts/maps/.inc +// QFile scriptsFile(dataDir + "scripts/maps/" + mapName + ".inc"); +// scriptsFile.open(QIODevice::WriteOnly); +// QTextStream scriptsStream(&scriptsFile); +// scriptsStream << mapName << "_MapScripts::" << endl +// << "\t.byte 0" << endl; +// scriptsFile.close(); + +// // 7. Create file data/text/maps/.inc +// QFile textFile(dataDir + "text/maps/" + mapName + ".inc"); +// textFile.open(QIODevice::WriteOnly); +// QTextStream textStream(&textFile); +// textStream << endl; +// textFile.close(); + +// // 8. Modify data/event_scripts.s: +// QFile eventScriptsFile(dataDir + "event_scripts.s"); +// eventScriptsFile.open(QIODevice::Append); +// QTextStream eventScriptsStream(&eventScriptsFile); +// eventScriptsStream << endl +// << "\t.include \"data/scripts/maps/" << mapName << ".inc\"" << endl +// << "\t.include \"data/text/maps/" << mapName << ".inc\"" << endl; +// eventScriptsFile.close(); + +// // 9. Modify data/map_events.s: +// QFile mapEventsFile(dataDir + "map_events.s"); +// mapEventsFile.open(QIODevice::Append); +// QTextStream mapEventsStream(&mapEventsFile); +// mapEventsStream << endl +// << "\t.include \"data/maps/events/" << mapName << ".inc\"" << endl; +// mapEventsFile.close(); + +// // 10. Modify data/maps/_assets.inc +// QFile assetsFile(dataMapsDir + "_assets.inc"); +// assetsFile.open(QIODevice::Append); +// QTextStream assetsStream(&assetsFile); +// assetsStream << endl +// << mapName << "_MapBorder::" << endl +// << "\t.incbin \"data/maps/" << mapName << "/border.bin\"" << endl +// << endl +// << mapName << "_MapBlockdata::" << endl +// << "\t.incbin \"data/maps/" << mapName << "/map.bin\"" << endl +// << endl +// << "\t.align 2" << endl +// << mapName << "_MapAttributes::" << endl +// << "\t.4byte 0x14" << endl +// << "\t.4byte 0x14" << endl +// << "\t.4byte " << mapName << "_MapBorder" << endl +// << "\t.4byte " << mapName << "_MapBlockdata" << endl +// << "\t.4byte gTileset_General" << endl +// << "\t.4byte gTileset_Pacifidlog" << endl +// << endl; +// assetsFile.close(); + +// // 11. Modify data/maps/_groups.inc +// // TODO: + +// // 12. Modify data/maps/attributes_table.inc +// QFile attributesFile(dataMapsDir + "attributes_table.inc"); +// attributesFile.open(QIODevice::Append); +// QTextStream attributesStream(&attributesFile); +// attributesStream << endl +// << "\t.4byte " << mapName << "_MapAttributes" << endl; +// attributesFile.close(); + +// // 13. Modify data/maps/headers.inc +// QFile headersFile(dataMapsDir + "headers.inc"); +// headersFile.open(QIODevice::Append); +// QTextStream headersStream(&headersFile); +// headersStream << endl +// << "\t.include \"data/maps/" << mapName << "/header.inc\"" << endl; +// headersFile.close(); } QString Project::getNewMapName() { @@ -1036,6 +1124,10 @@ void Project::saveMapEvents(Map *map) { } void Project::readMapEvents(Map *map) { + if (!map->isPersistedToFile) { + return; + } + // lazy QString path = root + QString("/data/maps/events/%1.inc").arg(map->name); QString text = readTextFile(path); @@ -1198,6 +1290,20 @@ void Project::readMapEvents(Map *map) { } } +void Project::setNewMapEvents(Map *map) { + map->object_events_label = "0x0"; + map->warps_label = "0x0"; + map->coord_events_label = "0x0"; + map->bg_events_label = "0x0"; + map->events["object"].clear(); + map->events["warp"].clear(); + map->events["trap"].clear(); + map->events["trap_weather"].clear(); + map->events["sign"].clear(); + map->events["event_hidden_item"].clear(); + map->events["event_secret_base"].clear(); +} + QStringList Project::readCArray(QString text, QString label) { QStringList list; diff --git a/project.h b/project.h index b37a69ef..dc14baea 100755 --- a/project.h +++ b/project.h @@ -48,6 +48,13 @@ public: void getTilesets(Map*); void loadTilesetAssets(Tileset*); + void setNewMapHeader(Map* map, int mapIndex); + void setNewMapAttributes(Map* map); + void setNewMapBlockdata(Map* map); + void setNewMapBorder(Map *map); + void setNewMapEvents(Map *map); + void setNewMapConnections(Map *map); + QString getBlockdataPath(Map*); void saveBlockdata(Map*); void writeBlockdata(QString, Blockdata*); From 50fc90697124d1d3ad3c4fa97bdf5dc068eca227 Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Sat, 17 Feb 2018 13:00:15 -0800 Subject: [PATCH 06/13] Parse all map attributes from _assets into memory --- asm.cpp | 2 +- mainwindow.cpp | 1 + project.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++ project.h | 3 ++ 4 files changed, 95 insertions(+), 1 deletion(-) diff --git a/asm.cpp b/asm.cpp index 60855b76..f291e3ba 100755 --- a/asm.cpp +++ b/asm.cpp @@ -26,7 +26,7 @@ QList* Asm::parse(QString text) { //QString macro; //QStringList *params; strip_comment(&line); - if (line.isEmpty()) { + if (line.trimmed().isEmpty()) { } else if (line.contains(':')) { label = line.left(line.indexOf(':')); QStringList *list = new QStringList; diff --git a/mainwindow.cpp b/mainwindow.cpp index 07079ba1..3d3b78ab 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -283,6 +283,7 @@ void MainWindow::on_checkBox_ShowLocation_clicked(bool checked) void MainWindow::loadDataStructures() { Project *project = editor->project; project->readMapAttributesTable(); + project->readAllMapAttributes(); } void MainWindow::populateMapList() { diff --git a/project.cpp b/project.cpp index 7eb6c6a8..07f7c757 100755 --- a/project.cpp +++ b/project.cpp @@ -23,6 +23,7 @@ Project::Project() mapConstantsToMapNames = new QMap; mapNamesToMapConstants = new QMap; mapAttributesTable = new QMap; + mapAttributes = new QMap*>; tileset_cache = new QMap; } @@ -273,6 +274,95 @@ void Project::readMapAttributes(Map* map) { map->tileset_secondary_label = attributes->value(5); } +void Project::readAllMapAttributes() { + mapAttributes->clear(); + + Asm *parser = new Asm; + QString assets_text = readTextFile(root + "/data/maps/_assets.inc"); + if (assets_text.isNull()) { + return; + } + + QList *commands = parser->parse(assets_text); + + // Assume the _assets.inc file is grouped consistently in the order of: + // 1. _MapBorder + // 2. _MapBlockdata + // 3. _MapAttributes + int i = 0; + while (i < commands->length()) { + // Read MapBorder assets. + QStringList borderParams = commands->value(i++); + bool isUnknownMapBorder = borderParams.value(1).startsWith("UnknownMapBorder_"); + if (borderParams.value(0) != ".label" || (!borderParams.value(1).endsWith("_MapBorder") && !isUnknownMapBorder)) { + qDebug() << QString("Expected MapBorder label, but found %1").arg(borderParams.value(1)); + continue; + } + QString borderLabel = borderParams.value(1); + QString mapName; + if (!isUnknownMapBorder) { + mapName = borderLabel.remove(borderLabel.length() - 10, 10); + } else { + // Unknown map name has to match the MapAttributes label. + mapName = borderLabel.replace("Border", "Attributes"); + } + mapAttributes->insert(mapName, new QMap); + mapAttributes->value(mapName)->insert("border_label", borderParams.value(1)); + borderParams = commands->value(i++); + mapAttributes->value(mapName)->insert("border_filepath", borderParams.value(1).replace("\"", "")); + + // Read MapBlockData assets. + QStringList blockDataParams = commands->value(i++); + bool isUnknownMapBlockdata = blockDataParams.value(1).startsWith("UnknownMapBlockdata_"); + if (blockDataParams.value(0) != ".label" || (!blockDataParams.value(1).endsWith("_MapBlockdata") && !isUnknownMapBlockdata)) { + qDebug() << QString("Expected MapBlockdata label, but found %1").arg(blockDataParams.value(1)); + continue; + } + QString blockDataLabel = blockDataParams.value(1); + mapAttributes->value(mapName)->insert("blockdata_label", blockDataLabel); + blockDataParams = commands->value(i++); + mapAttributes->value(mapName)->insert("blockdata_filepath", blockDataParams.value(1).replace("\"", "")); + + // Read MapAttributes assets. + i++; // skip .align + // Maps can share MapAttributes, so gather a list of them. + QStringList attributeMapLabels; + QStringList attributesParams; + while (i < commands->length()) { + attributesParams = commands->value(i); + if (attributesParams.value(0) != ".label") { + break; + } + attributeMapLabels.append(attributesParams.value(1)); + i++; + } + + // Apply the map attributes to each of the shared maps. + QString attrWidth = commands->value(i++).value(1); + QString attrHeight = commands->value(i++).value(1); + QString attrBorderLabel = commands->value(i++).value(1); + QString attrBlockdataLabel = commands->value(i++).value(1); + QString attrTilesetPrimary = commands->value(i++).value(1); + QString attrTilesetSecondary = commands->value(i++).value(1); + for (QString attributeMapLabel: attributeMapLabels) { + QString altMapName = attributeMapLabel; + if (!altMapName.startsWith("UnknownMapAttributes_")) { + altMapName.remove(altMapName.length() - 14, 14); + } + if (!mapAttributes->contains(altMapName)) { + mapAttributes->insert(altMapName, new QMap); + } + mapAttributes->value(altMapName)->insert("attributes_label", attributeMapLabel); + mapAttributes->value(altMapName)->insert("width", attrWidth); + mapAttributes->value(altMapName)->insert("height", attrHeight); + mapAttributes->value(altMapName)->insert("border_label", attrBorderLabel); + mapAttributes->value(altMapName)->insert("blockdata_label", attrBlockdataLabel); + mapAttributes->value(altMapName)->insert("tileset_primary", attrTilesetPrimary); + mapAttributes->value(altMapName)->insert("tileset_secondary", attrTilesetSecondary); + } + } +} + void Project::setNewMapAttributes(Map* map) { map->width = "20"; map->height = "20"; diff --git a/project.h b/project.h index dc14baea..0617c3cd 100755 --- a/project.h +++ b/project.h @@ -20,6 +20,8 @@ public: QMap *mapConstantsToMapNames; QMap *mapNamesToMapConstants; QMap *mapAttributesTable; + QMap*> *mapAttributes; + QMap *map_cache; Map* loadMap(QString); @@ -44,6 +46,7 @@ public: QStringList* getLabelValues(QList*, QString); void readMapHeader(Map*); void readMapAttributesTable(); + void readAllMapAttributes(); void readMapAttributes(Map*); void getTilesets(Map*); void loadTilesetAssets(Tileset*); From a9d71d15f8c613255ade0c2b4834bf4809616730 Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Sat, 17 Feb 2018 14:50:20 -0800 Subject: [PATCH 07/13] Save all map attributes to _assets file --- project.cpp | 102 ++++++++++++++++++++++++++++++++++++++++++++++------ project.h | 2 ++ 2 files changed, 94 insertions(+), 10 deletions(-) diff --git a/project.cpp b/project.cpp index 07f7c757..9f31615b 100755 --- a/project.cpp +++ b/project.cpp @@ -261,7 +261,7 @@ void Project::readMapAttributes(Map* map) { Asm *parser = new Asm; - QString assets_text = readTextFile(root + "/data/maps/_assets.inc"); + QString assets_text = readTextFile(getMapAssetsFilepath()); if (assets_text.isNull()) { return; } @@ -278,7 +278,7 @@ void Project::readAllMapAttributes() { mapAttributes->clear(); Asm *parser = new Asm; - QString assets_text = readTextFile(root + "/data/maps/_assets.inc"); + QString assets_text = readTextFile(getMapAssetsFilepath()); if (assets_text.isNull()) { return; } @@ -306,10 +306,12 @@ void Project::readAllMapAttributes() { // Unknown map name has to match the MapAttributes label. mapName = borderLabel.replace("Border", "Attributes"); } - mapAttributes->insert(mapName, new QMap); - mapAttributes->value(mapName)->insert("border_label", borderParams.value(1)); + + QMap* attrs = new QMap; + mapAttributes->insert(mapName, attrs); + attrs->insert("border_label", borderParams.value(1)); borderParams = commands->value(i++); - mapAttributes->value(mapName)->insert("border_filepath", borderParams.value(1).replace("\"", "")); + attrs->insert("border_filepath", borderParams.value(1).replace("\"", "")); // Read MapBlockData assets. QStringList blockDataParams = commands->value(i++); @@ -319,21 +321,24 @@ void Project::readAllMapAttributes() { continue; } QString blockDataLabel = blockDataParams.value(1); - mapAttributes->value(mapName)->insert("blockdata_label", blockDataLabel); + attrs->insert("blockdata_label", blockDataLabel); blockDataParams = commands->value(i++); - mapAttributes->value(mapName)->insert("blockdata_filepath", blockDataParams.value(1).replace("\"", "")); + attrs->insert("blockdata_filepath", blockDataParams.value(1).replace("\"", "")); // Read MapAttributes assets. i++; // skip .align // Maps can share MapAttributes, so gather a list of them. QStringList attributeMapLabels; QStringList attributesParams; + QStringList* sharedAttrMaps = new QStringList; while (i < commands->length()) { attributesParams = commands->value(i); if (attributesParams.value(0) != ".label") { break; } - attributeMapLabels.append(attributesParams.value(1)); + QString attrLabel = attributesParams.value(1); + attributeMapLabels.append(attrLabel); + sharedAttrMaps->append(attrLabel); i++; } @@ -359,10 +364,86 @@ void Project::readAllMapAttributes() { mapAttributes->value(altMapName)->insert("blockdata_label", attrBlockdataLabel); mapAttributes->value(altMapName)->insert("tileset_primary", attrTilesetPrimary); mapAttributes->value(altMapName)->insert("tileset_secondary", attrTilesetSecondary); + + if (sharedAttrMaps->length() > 1) { + mapAttributes->value(altMapName)->insert("shared_attr_maps", sharedAttrMaps->join(":")); + } } } } +void Project::saveAllMapAttributes() { + QString text = ""; + for (int i = 0; i < mapAttributesTable->count(); i++) { + int mapIndex = i + 1; + QString mapName = mapAttributesTable->value(mapIndex); + QMap* attrs = mapAttributes->value(mapName); + + // Find the map attributes object that contains the border data. + QMap* attrsWithBorder; + if (attrs->contains("border_filepath")) { + attrsWithBorder = attrs; + } else { + QStringList labels = attrs->value("shared_attr_maps").split(":"); + for (QString label : labels) { + label.remove(label.length() - 14, 14); + if (mapAttributes->contains(label) && mapAttributes->value(label)->contains("border_filepath")) { + attrsWithBorder = mapAttributes->value(label); + break; + } + } + } + if (attrsWithBorder != nullptr) { + text += QString("%1::\n").arg(attrsWithBorder->value("border_label")); + text += QString("\t.incbin \"%1\"\n").arg(attrsWithBorder->value("border_filepath")); + text += QString("\n"); + } + + // Find the map attributes object that contains the blockdata. + QMap* attrsWithBlockdata; + if (attrs->contains("blockdata_filepath")) { + attrsWithBlockdata = attrs; + } else { + QStringList labels = attrs->value("shared_attr_maps").split(":"); + for (QString label : labels) { + label.remove(label.length() - 14, 14); + if (mapAttributes->contains(label) && mapAttributes->value(label)->contains("blockdata_filepath")) { + attrsWithBlockdata = mapAttributes->value(label); + break; + } + } + } + if (attrsWithBlockdata != nullptr) { + text += QString("%1::\n").arg(attrsWithBlockdata->value("blockdata_label")); + text += QString("\t.incbin \"%1\"\n").arg(attrsWithBorder->value("blockdata_filepath")); + text += QString("\n"); + } + + text += QString("\t.align 2\n"); + if (attrs->contains("shared_attr_maps")) { + QStringList labels = attrs->value("shared_attr_maps").split(":"); + for (QString label : labels) { + text += QString("%1::\n").arg(label); + } + } else { + text += QString("%1::\n").arg(attrs->value("attributes_label")); + } + text += QString("\t.4byte %1\n").arg(attrs->value("width")); + text += QString("\t.4byte %1\n").arg(attrs->value("height")); + text += QString("\t.4byte %1\n").arg(attrs->value("border_label")); + text += QString("\t.4byte %1\n").arg(attrs->value("blockdata_label")); + text += QString("\t.4byte %1\n").arg(attrs->value("tileset_primary")); + text += QString("\t.4byte %1\n").arg(attrs->value("tileset_secondary")); + text += QString("\n"); + } + + saveTextFile(getMapAssetsFilepath(), text); +} + +QString Project::getMapAssetsFilepath() { + return root + "/data/maps/_assets.inc"; +} + void Project::setNewMapAttributes(Map* map) { map->width = "20"; map->height = "20"; @@ -400,7 +481,7 @@ Tileset* Project::loadTileset(QString label) { } QString Project::getBlockdataPath(Map* map) { - QString text = readTextFile(root + "/data/maps/_assets.inc"); + QString text = readTextFile(getMapAssetsFilepath()); QStringList *values = getLabelValues(parse(text), map->blockdata_label); QString path; if (!values->isEmpty()) { @@ -412,7 +493,7 @@ QString Project::getBlockdataPath(Map* map) { } QString Project::getMapBorderPath(Map *map) { - QString text = readTextFile(root + "/data/maps/_assets.inc"); + QString text = readTextFile(getMapAssetsFilepath()); QStringList *values = getLabelValues(parse(text), map->border_label); QString path; if (!values->isEmpty()) { @@ -489,6 +570,7 @@ void Project::saveMap(Map *map) { void Project::saveAllDataStructures() { saveMapAttributesTable(); + saveAllMapAttributes(); } void Project::loadTilesetAssets(Tileset* tileset) { diff --git a/project.h b/project.h index 0617c3cd..10996955 100755 --- a/project.h +++ b/project.h @@ -64,6 +64,7 @@ public: void saveAllMaps(); void saveMap(Map*); void saveAllDataStructures(); + void saveAllMapAttributes(); QList* parse(QString text); QStringList getSongNames(); @@ -91,6 +92,7 @@ public: QMap readCDefines(QString text, QStringList prefixes); private: QString getMapAttributesTableFilepath(); + QString getMapAssetsFilepath(); void saveMapHeader(Map*); void saveMapAttributesTable(); }; From bcc74b44618fdd5a571480049b49a49eef7639dc Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Sat, 17 Feb 2018 15:16:42 -0800 Subject: [PATCH 08/13] Add newly created map into global mapAttributes map --- project.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/project.cpp b/project.cpp index 9f31615b..69f69516 100755 --- a/project.cpp +++ b/project.cpp @@ -451,6 +451,19 @@ void Project::setNewMapAttributes(Map* map) { map->blockdata_label = QString("%1_MapBlockdata").arg(map->name); map->tileset_primary_label = "gTileset_General"; map->tileset_secondary_label = "gTileset_Petalburg"; + + // Insert new entry into the global map attributes. + QMap* attrs = new QMap; + attrs->insert("border_label", QString("%1_MapBorder").arg(map->name)); + attrs->insert("border_filepath", QString("data/maps/%1/border.bin").arg(map->name)); + attrs->insert("blockdata_label", QString("%1_MapBlockdata").arg(map->name)); + attrs->insert("blockdata_filepath", QString("data/maps/%1/map.bin").arg(map->name)); + attrs->insert("attributes_label", QString("%1_MapAttributes").arg(map->name)); + attrs->insert("width", map->width); + attrs->insert("height", map->height); + attrs->insert("tileset_primary", map->tileset_primary_label); + attrs->insert("tileset_secondary", map->tileset_secondary_label); + mapAttributes->insert(map->name, attrs); } void Project::getTilesets(Map* map) { From 43e15dfb0e12365e215c7a3a825fe090dd89bccb Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Sat, 17 Feb 2018 16:07:19 -0800 Subject: [PATCH 09/13] Save collateral files when creating a new map --- project.cpp | 67 +++++++++++++++++++++++++++++++++++++---------------- project.h | 2 ++ 2 files changed, 49 insertions(+), 20 deletions(-) diff --git a/project.cpp b/project.cpp index 69f69516..7e5ec69c 100755 --- a/project.cpp +++ b/project.cpp @@ -176,7 +176,7 @@ void Project::setNewMapHeader(Map* map, int mapIndex) { map->scripts_label = QString("%1_MapScripts").arg(map->name);; map->connections_label = "0x0"; map->song = "BGM_DAN02"; - map->index = mapIndex; + map->index = QString("%1").arg(mapIndex); map->location = "0"; map->visibility = "0"; map->weather = "2"; @@ -552,6 +552,11 @@ void Project::setNewMapBorder(Map *map) { map->border = blockdata; } +void Project::saveMapBorder(Map *map) { + QString path = getMapBorderPath(map); + writeBlockdata(path, map->border); +} + void Project::saveBlockdata(Map* map) { QString path = getBlockdataPath(map); writeBlockdata(path, map->blockdata); @@ -576,14 +581,45 @@ void Project::saveAllMaps() { } void Project::saveMap(Map *map) { - saveBlockdata(map); + // Create/Modify a few collateral files for brand new maps. + if (!map->isPersistedToFile) { + QString newMapDataDir = QString(root + "/data/maps/%1").arg(map->name); + if (!QDir::root().mkdir(newMapDataDir)) { + qDebug() << "Error: failed to create directory for new map. " << newMapDataDir; + } + + // Create file data/scripts/maps/.inc + QString text = QString("%1_MapScripts::\n\t.byte 0\n").arg(map->name); + saveTextFile(root + "/data/scripts/maps/" + map->name + ".inc", text); + + // Create file data/text/maps/.inc + saveTextFile(root + "/data/text/maps/" + map->name + ".inc", "\n"); + + // Simply append to data/event_scripts.s. + // TODO: In the future, this file needs more structure to allow for proper parsing. + text = QString("\n\t.include \"data/scripts/maps/%1.inc\"\n").arg(map->name); + text += QString("\t.include \"data/text/maps/%1.inc\"\n").arg(map->name); + appendTextFile(root + "/data/event_scripts.s", text); + + // Simply append to data/map_events.s. + text = QString("\n\t.include \"data/maps/events/%1.inc\"\n").arg(map->name); + appendTextFile(root + "/data/map_events.s", text); + + // Simply append to data/maps/headers.inc. + text = QString("\t.include \"data/maps/%1/header.inc\"\n").arg(map->name); + appendTextFile(root + "/data/maps/headers.inc", text); + } + + saveMapBorder(map); saveMapHeader(map); + saveBlockdata(map); saveMapEvents(map); } void Project::saveAllDataStructures() { saveMapAttributesTable(); saveAllMapAttributes(); + // TODO: saveMapGroupsTable(); } void Project::loadTilesetAssets(Tileset* tileset) { @@ -786,6 +822,15 @@ void Project::saveTextFile(QString path, QString text) { } } +void Project::appendTextFile(QString path, QString text) { + QFile file(path); + if (file.open(QIODevice::Append)) { + file.write(text.toUtf8()); + } else { + qDebug() << QString("Could not open '%1' for appending: ").arg(path) + file.errorString(); + } +} + void Project::readMapGroups() { QString text = readTextFile(root + "/data/maps/_groups.inc"); if (text.isNull()) { @@ -852,24 +897,6 @@ void Project::readMapGroups() { } void Project::addNewMapToGroup(QString mapName, int groupNum) { - // Write new map to project files. - // 1. Create directory data/maps// - // 2. Create file data/maps//border.bin - // 3. Create file data/maps//header.inc - // 4. Create file data/maps//map.bin - // 5. Create file data/maps/events/.inc - // 6. Create file data/scripts/maps/.inc - // 7. Create file data/text/maps/.inc - // 8. Modify data/event_scripts.s: - // .include "data/scripts/maps/.inc" - // .include "data/text/maps/.inc" - // 9. Modify data/map_events.s: - // .include "data/maps/events/.inc" - // 10. Modify data/maps/_assets.inc - // 11. Modify data/maps/_groups.inc - // 12. Modify data/maps/attributes_table.inc - // 13. Modify data/maps/headers.inc - int mapIndex = mapAttributesTable->count() + 1; mapAttributesTable->insert(mapIndex, mapName); diff --git a/project.h b/project.h index 10996955..fa5cd9ac 100755 --- a/project.h +++ b/project.h @@ -36,6 +36,7 @@ public: QString readTextFile(QString path); void saveTextFile(QString path, QString text); + void appendTextFile(QString path, QString text); void readMapGroups(); void addNewMapToGroup(QString mapName, int groupNum); @@ -60,6 +61,7 @@ public: QString getBlockdataPath(Map*); void saveBlockdata(Map*); + void saveMapBorder(Map*); void writeBlockdata(QString, Blockdata*); void saveAllMaps(); void saveMap(Map*); From 17e5892cd4e0b407c2a092bcbbadb901fc7edf00 Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Sat, 17 Feb 2018 16:29:38 -0800 Subject: [PATCH 10/13] Save map groups file --- project.cpp | 25 ++++++++++++++++++++++++- project.h | 1 + 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/project.cpp b/project.cpp index 7e5ec69c..bec5ea60 100755 --- a/project.cpp +++ b/project.cpp @@ -466,6 +466,28 @@ void Project::setNewMapAttributes(Map* map) { mapAttributes->insert(map->name, attrs); } +void Project::saveMapGroupsTable() { + QString text = ""; + int groupNum = 0; + for (QStringList* mapNames : *groupedMapNames) { + text += QString("\t.align 2\n"); + text += QString("gMapGroup%1::\n").arg(groupNum); + for (QString mapName : *mapNames) { + text += QString("\t.4byte %1\n").arg(mapName); + } + text += QString("\n"); + groupNum++; + } + + text += QString("\t.align 2\n"); + text += QString("gMapGroups::\n"); + for (int i = 0; i < groupNum; i++) { + text += QString("\t.4byte gMapGroup%1\n").arg(i); + } + + saveTextFile(root + "/data/maps/_groups.inc", text); +} + void Project::getTilesets(Map* map) { map->tileset_primary = getTileset(map->tileset_primary_label); map->tileset_secondary = getTileset(map->tileset_secondary_label); @@ -614,12 +636,13 @@ void Project::saveMap(Map *map) { saveMapHeader(map); saveBlockdata(map); saveMapEvents(map); + map->isPersistedToFile = true; } void Project::saveAllDataStructures() { saveMapAttributesTable(); saveAllMapAttributes(); - // TODO: saveMapGroupsTable(); + saveMapGroupsTable(); } void Project::loadTilesetAssets(Tileset* tileset) { diff --git a/project.h b/project.h index fa5cd9ac..b29c1d77 100755 --- a/project.h +++ b/project.h @@ -67,6 +67,7 @@ public: void saveMap(Map*); void saveAllDataStructures(); void saveAllMapAttributes(); + void saveMapGroupsTable(); QList* parse(QString text); QStringList getSongNames(); From e4c5d53ffd49546a29e5c16f0459098d505607b1 Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Sat, 17 Feb 2018 20:41:15 -0800 Subject: [PATCH 11/13] Maintain master and working copies of data structures, to enable saving single maps at a time --- editor.cpp | 3 +- mainwindow.cpp | 26 +++-- project.cpp | 296 +++++++++++++++---------------------------------- project.h | 15 ++- 4 files changed, 116 insertions(+), 224 deletions(-) diff --git a/editor.cpp b/editor.cpp index 4ccb9df9..7ae693c5 100755 --- a/editor.cpp +++ b/editor.cpp @@ -9,14 +9,15 @@ Editor::Editor() void Editor::saveProject() { if (project) { - project->saveAllDataStructures(); project->saveAllMaps(); + project->saveAllDataStructures(); } } void Editor::save() { if (project && map) { project->saveMap(map); + project->saveAllDataStructures(); } } diff --git a/mainwindow.cpp b/mainwindow.cpp index 3d3b78ab..3e1e48f2 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -73,26 +73,26 @@ void MainWindow::openProject(QString dir) { QString MainWindow::getDefaultMap() { if (editor && editor->project) { - QList *names = editor->project->groupedMapNames; - if (names) { + QList names = editor->project->groupedMapNames; + if (!names.isEmpty()) { QSettings settings; QString key = "project:" + editor->project->root; if (settings.contains(key)) { QMap qmap = settings.value(key).toMap(); if (qmap.contains("recent_map")) { QString map_name = qmap.value("recent_map").toString(); - for (int i = 0; i < names->length(); i++) { - if (names->value(i)->contains(map_name)) { + for (int i = 0; i < names.length(); i++) { + if (names.value(i).contains(map_name)) { return map_name; } } } } // Failing that, just get the first map in the list. - for (int i = 0; i < names->length(); i++) { - QStringList *list = names->value(i); - if (list->length()) { - return list->value(0); + for (int i = 0; i < names.length(); i++) { + QStringList list = names.value(i); + if (list.length()) { + return list.value(0); } } } @@ -327,9 +327,9 @@ void MainWindow::populateMapList() { group->setData(i, MapListUserRoles::GroupRole); maps->appendRow(group); mapGroupsModel->append(group); - QStringList *names = project->groupedMapNames->value(i); - for (int j = 0; j < names->length(); j++) { - QString map_name = names->value(j); + QStringList names = project->groupedMapNames.value(i); + for (int j = 0; j < names.length(); j++) { + QString map_name = names.value(j); QStandardItem *map = createMapItem(map_name, i, j); group->appendRow(map); } @@ -387,7 +387,9 @@ void MainWindow::onAddNewMapToGroupClick(QAction* triggeredAction) QStandardItem* groupItem = mapGroupsModel->at(groupNum); QString newMapName = editor->project->getNewMapName(); - editor->project->addNewMapToGroup(newMapName, groupNum); + Map* newMap = editor->project->addNewMapToGroup(newMapName, groupNum); + editor->project->saveMap(newMap); + editor->project->saveAllDataStructures(); int numMapsInGroup = groupItem->rowCount(); QStandardItem *newMapItem = createMapItem(newMapName, groupNum, numMapsInGroup); diff --git a/project.cpp b/project.cpp index bec5ea60..8efdf818 100755 --- a/project.cpp +++ b/project.cpp @@ -17,13 +17,10 @@ Project::Project() { groupNames = new QStringList; map_groups = new QMap; - groupedMapNames = new QList; mapNames = new QStringList; map_cache = new QMap; mapConstantsToMapNames = new QMap; mapNamesToMapConstants = new QMap; - mapAttributesTable = new QMap; - mapAttributes = new QMap*>; tileset_cache = new QMap; } @@ -36,10 +33,12 @@ QString Project::getProjectTitle() { } Map* Project::loadMap(QString map_name) { - // New maps are saved to actual files yet, so we need to fetch their data from the map_cache. Map *map; - if (map_cache->contains(map_name) && !map_cache->value(map_name)->isPersistedToFile) { + if (map_cache->contains(map_name)) { map = map_cache->value(map_name); + if (map->hasUnsavedChanges()) { + return map; + } } else { map = new Map; map->setName(map_name); @@ -228,19 +227,22 @@ void Project::readMapAttributesTable() { // Strip off "_MapAttributes" from the label if it's a real map label. mapName = mapName.remove(mapName.length() - 14, 14); } - mapAttributesTable->insert(curMapIndex, mapName); + mapAttributesTable.insert(curMapIndex, mapName); curMapIndex++; } } + + mapAttributesTableMaster = mapAttributesTable; + mapAttributesTableMaster.detach(); } void Project::saveMapAttributesTable() { QString text = ""; text += QString("\t.align 2\n"); text += QString("gMapAttributes::\n"); - for (int i = 0; i < mapAttributesTable->count(); i++) { + for (int i = 0; i < mapAttributesTableMaster.count(); i++) { int mapIndex = i + 1; - QString mapName = mapAttributesTable->value(mapIndex); + QString mapName = mapAttributesTableMaster.value(mapIndex); if (!mapName.contains("UnknownMapAttributes")) { text += QString("\t.4byte %1_MapAttributes\n").arg(mapName); } else { @@ -275,7 +277,7 @@ void Project::readMapAttributes(Map* map) { } void Project::readAllMapAttributes() { - mapAttributes->clear(); + mapAttributes.clear(); Asm *parser = new Asm; QString assets_text = readTextFile(getMapAssetsFilepath()); @@ -307,11 +309,9 @@ void Project::readAllMapAttributes() { mapName = borderLabel.replace("Border", "Attributes"); } - QMap* attrs = new QMap; - mapAttributes->insert(mapName, attrs); - attrs->insert("border_label", borderParams.value(1)); + mapAttributes[mapName].insert("border_label", borderParams.value(1)); borderParams = commands->value(i++); - attrs->insert("border_filepath", borderParams.value(1).replace("\"", "")); + mapAttributes[mapName].insert("border_filepath", borderParams.value(1).replace("\"", "")); // Read MapBlockData assets. QStringList blockDataParams = commands->value(i++); @@ -321,9 +321,9 @@ void Project::readAllMapAttributes() { continue; } QString blockDataLabel = blockDataParams.value(1); - attrs->insert("blockdata_label", blockDataLabel); + mapAttributes[mapName].insert("blockdata_label", blockDataLabel); blockDataParams = commands->value(i++); - attrs->insert("blockdata_filepath", blockDataParams.value(1).replace("\"", "")); + mapAttributes[mapName].insert("blockdata_filepath", blockDataParams.value(1).replace("\"", "")); // Read MapAttributes assets. i++; // skip .align @@ -354,86 +354,90 @@ void Project::readAllMapAttributes() { if (!altMapName.startsWith("UnknownMapAttributes_")) { altMapName.remove(altMapName.length() - 14, 14); } - if (!mapAttributes->contains(altMapName)) { - mapAttributes->insert(altMapName, new QMap); + if (!mapAttributes.contains(altMapName)) { + mapAttributes.insert(altMapName, QMap()); } - mapAttributes->value(altMapName)->insert("attributes_label", attributeMapLabel); - mapAttributes->value(altMapName)->insert("width", attrWidth); - mapAttributes->value(altMapName)->insert("height", attrHeight); - mapAttributes->value(altMapName)->insert("border_label", attrBorderLabel); - mapAttributes->value(altMapName)->insert("blockdata_label", attrBlockdataLabel); - mapAttributes->value(altMapName)->insert("tileset_primary", attrTilesetPrimary); - mapAttributes->value(altMapName)->insert("tileset_secondary", attrTilesetSecondary); + mapAttributes[altMapName]["attributes_label"] = attributeMapLabel; + mapAttributes[altMapName].insert("attributes_label", attributeMapLabel); + mapAttributes[altMapName].insert("width", attrWidth); + mapAttributes[altMapName].insert("height", attrHeight); + mapAttributes[altMapName].insert("border_label", attrBorderLabel); + mapAttributes[altMapName].insert("blockdata_label", attrBlockdataLabel); + mapAttributes[altMapName].insert("tileset_primary", attrTilesetPrimary); + mapAttributes[altMapName].insert("tileset_secondary", attrTilesetSecondary); if (sharedAttrMaps->length() > 1) { - mapAttributes->value(altMapName)->insert("shared_attr_maps", sharedAttrMaps->join(":")); + mapAttributes[altMapName].insert("shared_attr_maps", sharedAttrMaps->join(":")); } } } + + mapAttributesMaster = mapAttributes; + mapAttributesMaster.detach(); } void Project::saveAllMapAttributes() { QString text = ""; - for (int i = 0; i < mapAttributesTable->count(); i++) { + for (int i = 0; i < mapAttributesTableMaster.count(); i++) { int mapIndex = i + 1; - QString mapName = mapAttributesTable->value(mapIndex); - QMap* attrs = mapAttributes->value(mapName); + QString mapName = mapAttributesTableMaster.value(mapIndex); + QMap attrs = mapAttributesMaster.value(mapName); // Find the map attributes object that contains the border data. - QMap* attrsWithBorder; - if (attrs->contains("border_filepath")) { + QMap attrsWithBorder; + if (attrs.contains("border_filepath")) { attrsWithBorder = attrs; } else { - QStringList labels = attrs->value("shared_attr_maps").split(":"); + QStringList labels = attrs.value("shared_attr_maps").split(":"); for (QString label : labels) { label.remove(label.length() - 14, 14); - if (mapAttributes->contains(label) && mapAttributes->value(label)->contains("border_filepath")) { - attrsWithBorder = mapAttributes->value(label); + if (mapAttributesMaster.contains(label) && mapAttributesMaster.value(label).contains("border_filepath")) { + attrsWithBorder = mapAttributesMaster.value(label); break; } } } - if (attrsWithBorder != nullptr) { - text += QString("%1::\n").arg(attrsWithBorder->value("border_label")); - text += QString("\t.incbin \"%1\"\n").arg(attrsWithBorder->value("border_filepath")); + if (!attrsWithBorder.isEmpty()) { + text += QString("%1::\n").arg(attrsWithBorder.value("border_label")); + text += QString("\t.incbin \"%1\"\n").arg(attrsWithBorder.value("border_filepath")); text += QString("\n"); } // Find the map attributes object that contains the blockdata. - QMap* attrsWithBlockdata; - if (attrs->contains("blockdata_filepath")) { + QMap attrsWithBlockdata; + if (attrs.contains("blockdata_filepath")) { attrsWithBlockdata = attrs; } else { - QStringList labels = attrs->value("shared_attr_maps").split(":"); + QStringList labels = attrs["shared_attr_maps"].split(":"); for (QString label : labels) { label.remove(label.length() - 14, 14); - if (mapAttributes->contains(label) && mapAttributes->value(label)->contains("blockdata_filepath")) { - attrsWithBlockdata = mapAttributes->value(label); + if (mapAttributesMaster.contains(label) && mapAttributesMaster.value(label).contains("blockdata_filepath")) { + attrsWithBlockdata = mapAttributesMaster.value(label); break; } } } - if (attrsWithBlockdata != nullptr) { - text += QString("%1::\n").arg(attrsWithBlockdata->value("blockdata_label")); - text += QString("\t.incbin \"%1\"\n").arg(attrsWithBorder->value("blockdata_filepath")); + if (!attrsWithBlockdata.isEmpty()) { + text += QString("%1::\n").arg(attrsWithBlockdata.value("blockdata_label")); + text += QString("\t.incbin \"%1\"\n").arg(attrsWithBorder.value("blockdata_filepath")); text += QString("\n"); } text += QString("\t.align 2\n"); - if (attrs->contains("shared_attr_maps")) { - QStringList labels = attrs->value("shared_attr_maps").split(":"); + if (attrs.contains("shared_attr_maps")) { + QStringList labels = attrs.value("shared_attr_maps").split(":"); for (QString label : labels) { text += QString("%1::\n").arg(label); } } else { - text += QString("%1::\n").arg(attrs->value("attributes_label")); + text += QString("%1::\n").arg(attrs.value("attributes_label")); } - text += QString("\t.4byte %1\n").arg(attrs->value("width")); - text += QString("\t.4byte %1\n").arg(attrs->value("height")); - text += QString("\t.4byte %1\n").arg(attrs->value("border_label")); - text += QString("\t.4byte %1\n").arg(attrs->value("blockdata_label")); - text += QString("\t.4byte %1\n").arg(attrs->value("tileset_primary")); - text += QString("\t.4byte %1\n").arg(attrs->value("tileset_secondary")); + text += QString("\t.4byte %1\n").arg(attrs.value("width")); + text += QString("\t.4byte %1\n").arg(attrs.value("height")); + text += QString("\t.4byte %1\n").arg(attrs.value("border_label")); + text += QString("\t.4byte %1\n").arg(attrs.value("blockdata_label")); + text += QString("\t.4byte %1\n").arg(attrs.value("tileset_primary")); + text += QString("\t.4byte %1\n").arg(attrs.value("tileset_secondary")); text += QString("\n"); } @@ -453,26 +457,26 @@ void Project::setNewMapAttributes(Map* map) { map->tileset_secondary_label = "gTileset_Petalburg"; // Insert new entry into the global map attributes. - QMap* attrs = new QMap; - attrs->insert("border_label", QString("%1_MapBorder").arg(map->name)); - attrs->insert("border_filepath", QString("data/maps/%1/border.bin").arg(map->name)); - attrs->insert("blockdata_label", QString("%1_MapBlockdata").arg(map->name)); - attrs->insert("blockdata_filepath", QString("data/maps/%1/map.bin").arg(map->name)); - attrs->insert("attributes_label", QString("%1_MapAttributes").arg(map->name)); - attrs->insert("width", map->width); - attrs->insert("height", map->height); - attrs->insert("tileset_primary", map->tileset_primary_label); - attrs->insert("tileset_secondary", map->tileset_secondary_label); - mapAttributes->insert(map->name, attrs); + QMap attrs; + attrs.insert("border_label", QString("%1_MapBorder").arg(map->name)); + attrs.insert("border_filepath", QString("data/maps/%1/border.bin").arg(map->name)); + attrs.insert("blockdata_label", QString("%1_MapBlockdata").arg(map->name)); + attrs.insert("blockdata_filepath", QString("data/maps/%1/map.bin").arg(map->name)); + attrs.insert("attributes_label", QString("%1_MapAttributes").arg(map->name)); + attrs.insert("width", map->width); + attrs.insert("height", map->height); + attrs.insert("tileset_primary", map->tileset_primary_label); + attrs.insert("tileset_secondary", map->tileset_secondary_label); + mapAttributes.insert(map->name, attrs); } void Project::saveMapGroupsTable() { QString text = ""; int groupNum = 0; - for (QStringList* mapNames : *groupedMapNames) { + for (QStringList mapNames : groupedMapNames) { text += QString("\t.align 2\n"); text += QString("gMapGroup%1::\n").arg(groupNum); - for (QString mapName : *mapNames) { + for (QString mapName : mapNames) { text += QString("\t.4byte %1\n").arg(mapName); } text += QString("\n"); @@ -610,6 +614,7 @@ void Project::saveMap(Map *map) { qDebug() << "Error: failed to create directory for new map. " << newMapDataDir; } + // TODO: In the future, these files needs more structure to allow for proper parsing/saving. // Create file data/scripts/maps/.inc QString text = QString("%1_MapScripts::\n\t.byte 0\n").arg(map->name); saveTextFile(root + "/data/scripts/maps/" + map->name + ".inc", text); @@ -618,7 +623,6 @@ void Project::saveMap(Map *map) { saveTextFile(root + "/data/text/maps/" + map->name + ".inc", "\n"); // Simply append to data/event_scripts.s. - // TODO: In the future, this file needs more structure to allow for proper parsing. text = QString("\n\t.include \"data/scripts/maps/%1.inc\"\n").arg(map->name); text += QString("\t.include \"data/text/maps/%1.inc\"\n").arg(map->name); appendTextFile(root + "/data/event_scripts.s", text); @@ -636,9 +640,21 @@ void Project::saveMap(Map *map) { saveMapHeader(map); saveBlockdata(map); saveMapEvents(map); + + // Update global data structures with current map data. + updateMapAttributes(map); + map->isPersistedToFile = true; } +void Project::updateMapAttributes(Map* map) { + mapAttributesTableMaster.insert(map->index.toInt(nullptr, 0), map->name); + + QMap attrs = mapAttributes.value(map->name); + attrs.detach(); + mapAttributesMaster.insert(map->name, attrs); +} + void Project::saveAllDataStructures() { saveMapAttributesTable(); saveAllMapAttributes(); @@ -883,10 +899,9 @@ void Project::readMapGroups() { } } - QList *groupedMaps = new QList; + QList groupedMaps; for (int i = 0; i < groups->length(); i++) { - QStringList *list = new QStringList; - groupedMaps->append(list); + groupedMaps.append(QStringList()); } QStringList *maps = new QStringList; @@ -900,8 +915,7 @@ void Project::readMapGroups() { if (group != -1) { for (int j = 1; j < params.length(); j++) { QString mapName = params.value(j); - QStringList *list = groupedMaps->value(group); - list->append(mapName); + groupedMaps[group].append(mapName); maps->append(mapName); map_groups->insert(mapName, group); @@ -919,14 +933,14 @@ void Project::readMapGroups() { mapNames = maps; } -void Project::addNewMapToGroup(QString mapName, int groupNum) { - int mapIndex = mapAttributesTable->count() + 1; - mapAttributesTable->insert(mapIndex, mapName); +Map* Project::addNewMapToGroup(QString mapName, int groupNum) { + int mapIndex = mapAttributesTable.count() + 1; + mapAttributesTable.insert(mapIndex, mapName); // Setup new map in memory, but don't write to file until map is actually saved later. mapNames->append(mapName); map_groups->insert(mapName, groupNum); - groupedMapNames->value(groupNum)->append(mapName); + groupedMapNames[groupNum].append(mapName); Map *map = new Map; map->isPersistedToFile = false; @@ -942,135 +956,7 @@ void Project::addNewMapToGroup(QString mapName, int groupNum) { map->history.save(); map_cache->insert(mapName, map); - -// QString dataDir = QString("%1/data/").arg(root); -// QString dataMapsDir = QString("%1maps/").arg(dataDir); -// QString newMapDataDir = QString("%1%2/").arg(dataMapsDir).arg(mapName); - -// // 1. Create directory data/maps// -// if (!QDir::root().mkdir(newMapDataDir)) { -// qDebug() << "Error: failed to create directory for new map. " << newMapDataDir; -// return; -// } - -// // 2. Create file data/maps//border.bin -// QFile borderFile(newMapDataDir + "border.bin"); -// borderFile.open(QIODevice::WriteOnly); -// QDataStream borderStream(&borderFile); -// borderStream.setByteOrder(QDataStream::LittleEndian); -// borderStream << qint16(0x01D4) << qint16(0x01D5) << qint16(0x01DC) << qint16(0x01DD); -// borderFile.close(); - -// // 3. Create file data/maps//header.inc -// QFile headerFile(newMapDataDir + "header.inc"); -// headerFile.open(QIODevice::WriteOnly); -// QTextStream headerStream(&headerFile); -// headerStream << mapName << "::" << endl -// << "\t.4byte " << mapName << "_MapAttributes" << endl -// << "\t.4byte " << mapName << "_MapEvents" << endl -// << "\t.4byte " << mapName << "_MapScripts" << endl -// << "\t.4byte 0x0" << endl -// << "\t.2byte BGM_DAN02" << endl -// << "\t.2byte " << mapIndex << endl -// << "\t.byte 0" << endl -// << "\t.byte 0" << endl -// << "\t.byte 11" << endl -// << "\t.byte 4" << endl -// << "\t.2byte 0" << endl -// << "\t.byte 1" << endl -// << "\t.byte 0" << endl; -// headerFile.close(); - -// // 4. Create file data/maps//map.bin -// QFile mapFile(newMapDataDir + "map.bin"); -// mapFile.open(QIODevice::WriteOnly); -// QDataStream mapStream(&mapFile); -// mapStream.setByteOrder(QDataStream::LittleEndian); -// for (int i = 0; i < 20 * 20; i++) { -// mapStream << qint16(0x3001); -// } -// mapFile.close(); - -// // 5. Create file data/maps/events/.inc -// QFile eventsFile(dataMapsDir + "events/" + mapName + ".inc"); -// eventsFile.open(QIODevice::WriteOnly); -// QTextStream eventsStream(&eventsFile); -// eventsStream << mapName << "_MapEvents::" << endl -// << "\tmap_events 0x0, 0x0, 0x0, 0x0" << endl; -// eventsFile.close(); - -// // 6. Create file data/scripts/maps/.inc -// QFile scriptsFile(dataDir + "scripts/maps/" + mapName + ".inc"); -// scriptsFile.open(QIODevice::WriteOnly); -// QTextStream scriptsStream(&scriptsFile); -// scriptsStream << mapName << "_MapScripts::" << endl -// << "\t.byte 0" << endl; -// scriptsFile.close(); - -// // 7. Create file data/text/maps/.inc -// QFile textFile(dataDir + "text/maps/" + mapName + ".inc"); -// textFile.open(QIODevice::WriteOnly); -// QTextStream textStream(&textFile); -// textStream << endl; -// textFile.close(); - -// // 8. Modify data/event_scripts.s: -// QFile eventScriptsFile(dataDir + "event_scripts.s"); -// eventScriptsFile.open(QIODevice::Append); -// QTextStream eventScriptsStream(&eventScriptsFile); -// eventScriptsStream << endl -// << "\t.include \"data/scripts/maps/" << mapName << ".inc\"" << endl -// << "\t.include \"data/text/maps/" << mapName << ".inc\"" << endl; -// eventScriptsFile.close(); - -// // 9. Modify data/map_events.s: -// QFile mapEventsFile(dataDir + "map_events.s"); -// mapEventsFile.open(QIODevice::Append); -// QTextStream mapEventsStream(&mapEventsFile); -// mapEventsStream << endl -// << "\t.include \"data/maps/events/" << mapName << ".inc\"" << endl; -// mapEventsFile.close(); - -// // 10. Modify data/maps/_assets.inc -// QFile assetsFile(dataMapsDir + "_assets.inc"); -// assetsFile.open(QIODevice::Append); -// QTextStream assetsStream(&assetsFile); -// assetsStream << endl -// << mapName << "_MapBorder::" << endl -// << "\t.incbin \"data/maps/" << mapName << "/border.bin\"" << endl -// << endl -// << mapName << "_MapBlockdata::" << endl -// << "\t.incbin \"data/maps/" << mapName << "/map.bin\"" << endl -// << endl -// << "\t.align 2" << endl -// << mapName << "_MapAttributes::" << endl -// << "\t.4byte 0x14" << endl -// << "\t.4byte 0x14" << endl -// << "\t.4byte " << mapName << "_MapBorder" << endl -// << "\t.4byte " << mapName << "_MapBlockdata" << endl -// << "\t.4byte gTileset_General" << endl -// << "\t.4byte gTileset_Pacifidlog" << endl -// << endl; -// assetsFile.close(); - -// // 11. Modify data/maps/_groups.inc -// // TODO: - -// // 12. Modify data/maps/attributes_table.inc -// QFile attributesFile(dataMapsDir + "attributes_table.inc"); -// attributesFile.open(QIODevice::Append); -// QTextStream attributesStream(&attributesFile); -// attributesStream << endl -// << "\t.4byte " << mapName << "_MapAttributes" << endl; -// attributesFile.close(); - -// // 13. Modify data/maps/headers.inc -// QFile headersFile(dataMapsDir + "headers.inc"); -// headersFile.open(QIODevice::Append); -// QTextStream headersStream(&headersFile); -// headersStream << endl -// << "\t.include \"data/maps/" << mapName << "/header.inc\"" << endl; -// headersFile.close(); + return map; } QString Project::getNewMapName() { diff --git a/project.h b/project.h index b29c1d77..a9a39b0e 100755 --- a/project.h +++ b/project.h @@ -15,12 +15,14 @@ public: QString root; QStringList *groupNames = NULL; QMap *map_groups; - QList *groupedMapNames = NULL; + QList groupedMapNames; QStringList *mapNames = NULL; - QMap *mapConstantsToMapNames; - QMap *mapNamesToMapConstants; - QMap *mapAttributesTable; - QMap*> *mapAttributes; + QMap* mapConstantsToMapNames; + QMap* mapNamesToMapConstants; + QMap mapAttributesTable; + QMap mapAttributesTableMaster; + QMap> mapAttributes; + QMap> mapAttributesMaster; QMap *map_cache; @@ -39,7 +41,7 @@ public: void appendTextFile(QString path, QString text); void readMapGroups(); - void addNewMapToGroup(QString mapName, int groupNum); + Map* addNewMapToGroup(QString mapName, int groupNum); QString getNewMapName(); QString getProjectTitle(); @@ -98,6 +100,7 @@ private: QString getMapAssetsFilepath(); void saveMapHeader(Map*); void saveMapAttributesTable(); + void updateMapAttributes(Map* map); }; #endif // PROJECT_H From c61b8a9b1840e97dfadb99a2e2184fbcdc87b172 Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Sun, 18 Feb 2018 09:43:29 -0800 Subject: [PATCH 12/13] Save map constants header based on the current map group data --- project.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ project.h | 1 + 2 files changed, 41 insertions(+) diff --git a/project.cpp b/project.cpp index 8efdf818..f68b0f30 100755 --- a/project.cpp +++ b/project.cpp @@ -492,6 +492,43 @@ void Project::saveMapGroupsTable() { saveTextFile(root + "/data/maps/_groups.inc", text); } +void Project::saveMapConstantsHeader() { + QString text = QString("#ifndef GUARD_CONSTANTS_MAPS_H\n"); + text += QString("#define GUARD_CONSTANTS_MAPS_H\n"); + text += QString("\n"); + + int groupNum = 0; + for (QStringList mapNames : groupedMapNames) { + text += QString("// Map Group %1\n").arg(groupNum); + int maxLength = 0; + for (QString mapName : mapNames) { + QString mapConstantName = mapNamesToMapConstants->value(mapName); + if (mapConstantName.length() > maxLength) + maxLength = mapConstantName.length(); + } + int groupIndex = 0; + for (QString mapName : mapNames) { + QString mapConstantName = mapNamesToMapConstants->value(mapName); + text += QString("#define %1%2(%3 | (%4 << 8))\n") + .arg(mapConstantName) + .arg(QString(" ").repeated(maxLength - mapConstantName.length() + 1)) + .arg(groupIndex) + .arg(groupNum); + groupIndex++; + } + text += QString("\n"); + groupNum++; + } + + text += QString("\n"); + text += QString("#define MAP_NONE (0x7F | (0x7F << 8))\n"); + text += QString("#define MAP_UNDEFINED (0xFF | (0xFF << 8))\n\n\n"); + text += QString("#define MAP_GROUP(map) (MAP_##map >> 8)\n"); + text += QString("#define MAP_NUM(map) (MAP_##map & 0xFF)\n\n"); + text += QString("#endif // GUARD_CONSTANTS_MAPS_H\n"); + saveTextFile(root + "/include/constants/maps.h", text); +} + void Project::getTilesets(Map* map) { map->tileset_primary = getTileset(map->tileset_primary_label); map->tileset_secondary = getTileset(map->tileset_secondary_label); @@ -659,6 +696,7 @@ void Project::saveAllDataStructures() { saveMapAttributesTable(); saveAllMapAttributes(); saveMapGroupsTable(); + saveMapConstantsHeader(); } void Project::loadTilesetAssets(Tileset* tileset) { @@ -945,6 +983,8 @@ Map* Project::addNewMapToGroup(QString mapName, int groupNum) { Map *map = new Map; map->isPersistedToFile = false; map->setName(mapName); + mapConstantsToMapNames->insert(map->constantName, map->name); + mapNamesToMapConstants->insert(map->name, map->constantName); setNewMapHeader(map, mapIndex); setNewMapAttributes(map); getTilesets(map); diff --git a/project.h b/project.h index a9a39b0e..4edab894 100755 --- a/project.h +++ b/project.h @@ -70,6 +70,7 @@ public: void saveAllDataStructures(); void saveAllMapAttributes(); void saveMapGroupsTable(); + void saveMapConstantsHeader(); QList* parse(QString text); QStringList getSongNames(); From b39f44c6d0fa91563f948a89424f3ff3353dc2bd Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Sun, 18 Feb 2018 10:32:30 -0800 Subject: [PATCH 13/13] Small cleanup --- main.cpp | 1 + mainwindow.h | 2 +- map.h | 9 --------- project.cpp | 4 +++- project.h | 14 +++++++------- 5 files changed, 12 insertions(+), 18 deletions(-) diff --git a/main.cpp b/main.cpp index aab39bb8..94e1c1b8 100755 --- a/main.cpp +++ b/main.cpp @@ -4,6 +4,7 @@ int main(int argc, char *argv[]) { QApplication a(argc, argv); + a.setStyle("fusion"); MainWindow w; w.show(); diff --git a/mainwindow.h b/mainwindow.h index f9a911e1..34c94d63 100755 --- a/mainwindow.h +++ b/mainwindow.h @@ -94,7 +94,7 @@ private: enum MapListUserRoles { GroupRole = Qt::UserRole + 1, // Used to hold the map group number. - TypeRole = Qt::UserRole + 10, // Used to differentiate between the different layers of the map list tree view. + TypeRole = Qt::UserRole + 2, // Used to differentiate between the different layers of the map list tree view. }; #endif // MAINWINDOW_H diff --git a/map.h b/map.h index ec2343e3..c49d833e 100755 --- a/map.h +++ b/map.h @@ -193,13 +193,4 @@ signals: public slots: }; -class MapGroup : public QObject -{ - Q_OBJECT -public: - QString name; - int group_num; - QList maps; -}; - #endif // MAP_H diff --git a/project.cpp b/project.cpp index f68b0f30..cbe6f65d 100755 --- a/project.cpp +++ b/project.cpp @@ -232,6 +232,7 @@ void Project::readMapAttributesTable() { } } + // Deep copy mapAttributesTableMaster = mapAttributesTable; mapAttributesTableMaster.detach(); } @@ -357,7 +358,6 @@ void Project::readAllMapAttributes() { if (!mapAttributes.contains(altMapName)) { mapAttributes.insert(altMapName, QMap()); } - mapAttributes[altMapName]["attributes_label"] = attributeMapLabel; mapAttributes[altMapName].insert("attributes_label", attributeMapLabel); mapAttributes[altMapName].insert("width", attrWidth); mapAttributes[altMapName].insert("height", attrHeight); @@ -372,6 +372,7 @@ void Project::readAllMapAttributes() { } } + // Deep copy mapAttributesMaster = mapAttributes; mapAttributesMaster.detach(); } @@ -687,6 +688,7 @@ void Project::saveMap(Map *map) { void Project::updateMapAttributes(Map* map) { mapAttributesTableMaster.insert(map->index.toInt(nullptr, 0), map->name); + // Deep copy QMap attrs = mapAttributes.value(map->name); attrs.detach(); mapAttributesMaster.insert(map->name, attrs); diff --git a/project.h b/project.h index 4edab894..37ae487f 100755 --- a/project.h +++ b/project.h @@ -54,13 +54,6 @@ public: void getTilesets(Map*); void loadTilesetAssets(Tileset*); - void setNewMapHeader(Map* map, int mapIndex); - void setNewMapAttributes(Map* map); - void setNewMapBlockdata(Map* map); - void setNewMapBorder(Map *map); - void setNewMapEvents(Map *map); - void setNewMapConnections(Map *map); - QString getBlockdataPath(Map*); void saveBlockdata(Map*); void saveMapBorder(Map*); @@ -102,6 +95,13 @@ private: void saveMapHeader(Map*); void saveMapAttributesTable(); void updateMapAttributes(Map* map); + + void setNewMapHeader(Map* map, int mapIndex); + void setNewMapAttributes(Map* map); + void setNewMapBlockdata(Map* map); + void setNewMapBorder(Map *map); + void setNewMapEvents(Map *map); + void setNewMapConnections(Map *map); }; #endif // PROJECT_H