Browse Source

Properly support saving map connections

Marcus Huderle 6 years ago
parent
commit
9b0f686781
4 changed files with 94 additions and 19 deletions
  1. 1
    0
      editor.cpp
  2. 1
    0
      mainwindow.cpp
  3. 87
    19
      project.cpp
  4. 5
    0
      project.h

+ 1
- 0
editor.cpp View File

409
     for (ConnectionPixmapItem* item : connection_edit_items) {
409
     for (ConnectionPixmapItem* item : connection_edit_items) {
410
         delete item;
410
         delete item;
411
     }
411
     }
412
+    current_connection_edit_item = NULL;
412
     connection_edit_items.clear();
413
     connection_edit_items.clear();
413
 
414
 
414
     for (Connection *connection : map->connections) {
415
     for (Connection *connection : map->connections) {

+ 1
- 0
mainwindow.cpp View File

289
     project->readItemNames();
289
     project->readItemNames();
290
     project->readFlagNames();
290
     project->readFlagNames();
291
     project->readVarNames();
291
     project->readVarNames();
292
+    project->readMapsWithConnections();
292
 }
293
 }
293
 
294
 
294
 void MainWindow::populateMapList() {
295
 void MainWindow::populateMapList() {

+ 87
- 19
project.cpp View File

197
     text += QString("\t.4byte %1\n").arg(map->attributes_label);
197
     text += QString("\t.4byte %1\n").arg(map->attributes_label);
198
     text += QString("\t.4byte %1\n").arg(map->events_label);
198
     text += QString("\t.4byte %1\n").arg(map->events_label);
199
     text += QString("\t.4byte %1\n").arg(map->scripts_label);
199
     text += QString("\t.4byte %1\n").arg(map->scripts_label);
200
+
201
+    if (map->connections.length() == 0) {
202
+        map->connections_label = "0x0";
203
+    } else {
204
+        map->connections_label = QString("%1_MapConnections").arg(map->name);
205
+    }
200
     text += QString("\t.4byte %1\n").arg(map->connections_label);
206
     text += QString("\t.4byte %1\n").arg(map->connections_label);
207
+
201
     text += QString("\t.2byte %1\n").arg(map->song);
208
     text += QString("\t.2byte %1\n").arg(map->song);
202
     text += QString("\t.2byte %1\n").arg(map->index);
209
     text += QString("\t.2byte %1\n").arg(map->index);
203
     text += QString("\t.byte %1\n").arg(map->location);
210
     text += QString("\t.byte %1\n").arg(map->location);
211
 }
218
 }
212
 
219
 
213
 void Project::saveMapConnections(Map *map) {
220
 void Project::saveMapConnections(Map *map) {
214
-    QString connections_path = root + "/data/maps/" + map->name + "/connections.inc";
215
-    QString connectionsListLabel = QString("%1_MapConnectionsList").arg(map->name);
216
-    int numValidConnections = 0;
217
-    QString text = "";
218
-    text += QString("%1::\n").arg(connectionsListLabel);
219
-    for (Connection* connection : map->connections) {
220
-        if (mapNamesToMapConstants->contains(connection->map_name)) {
221
-            text += QString("\tconnection %1, %2, %3\n")
222
-                    .arg(connection->direction)
223
-                    .arg(connection->offset)
224
-                    .arg(mapNamesToMapConstants->value(connection->map_name));
225
-            numValidConnections++;
226
-        } else {
227
-            qDebug() << QString("Failed to write map connection. %1 not a valid map name").arg(connection->map_name);
221
+    QString path = root + "/data/maps/" + map->name + "/connections.inc";
222
+    if (map->connections.length() > 0) {
223
+        QString text = "";
224
+        QString connectionsListLabel = QString("%1_MapConnectionsList").arg(map->name);
225
+        int numValidConnections = 0;
226
+        text += QString("%1::\n").arg(connectionsListLabel);
227
+        for (Connection* connection : map->connections) {
228
+            if (mapNamesToMapConstants->contains(connection->map_name)) {
229
+                text += QString("\tconnection %1, %2, %3\n")
230
+                        .arg(connection->direction)
231
+                        .arg(connection->offset)
232
+                        .arg(mapNamesToMapConstants->value(connection->map_name));
233
+                numValidConnections++;
234
+            } else {
235
+                qDebug() << QString("Failed to write map connection. %1 not a valid map name").arg(connection->map_name);
236
+            }
237
+        }
238
+        text += QString("\n");
239
+        text += QString("%1::\n").arg(map->connections_label);
240
+        text += QString("\t.4byte %1\n").arg(numValidConnections);
241
+        text += QString("\t.4byte %1\n").arg(connectionsListLabel);
242
+        saveTextFile(path, text);
243
+    } else {
244
+        deleteFile(path);
245
+    }
246
+
247
+    updateMapsWithConnections(map);
248
+}
249
+
250
+void Project::updateMapsWithConnections(Map *map) {
251
+    if (map->connections.length() > 0) {
252
+        if (!mapsWithConnections.contains(map->name)) {
253
+            mapsWithConnections.append(map->name);
254
+        }
255
+    } else {
256
+        if (mapsWithConnections.contains(map->name)) {
257
+            mapsWithConnections.removeOne(map->name);
228
         }
258
         }
229
     }
259
     }
230
-    text += QString("\n");
231
-    text += QString("%1::\n").arg(map->connections_label);
232
-    text += QString("\t.4byte %1\n").arg(numValidConnections);
233
-    text += QString("\t.4byte %1\n").arg(connectionsListLabel);
234
-    saveTextFile(connections_path, text);
235
 }
260
 }
236
 
261
 
237
 void Project::readMapAttributesTable() {
262
 void Project::readMapAttributesTable() {
728
     saveAllMapAttributes();
753
     saveAllMapAttributes();
729
     saveMapGroupsTable();
754
     saveMapGroupsTable();
730
     saveMapConstantsHeader();
755
     saveMapConstantsHeader();
756
+    saveMapsWithConnections();
731
 }
757
 }
732
 
758
 
733
 void Project::loadTilesetAssets(Tileset* tileset) {
759
 void Project::loadTilesetAssets(Tileset* tileset) {
939
     }
965
     }
940
 }
966
 }
941
 
967
 
968
+void Project::deleteFile(QString path) {
969
+    QFile file(path);
970
+    if (file.exists() && !file.remove()) {
971
+        qDebug() << QString("Could not delete file '%1': ").arg(path) + file.errorString();
972
+    }
973
+}
974
+
942
 void Project::readMapGroups() {
975
 void Project::readMapGroups() {
943
     QString text = readTextFile(root + "/data/maps/_groups.inc");
976
     QString text = readTextFile(root + "/data/maps/_groups.inc");
944
     if (text.isNull()) {
977
     if (text.isNull()) {
1124
     }
1157
     }
1125
 }
1158
 }
1126
 
1159
 
1160
+void Project::readMapsWithConnections() {
1161
+    QString path = root + "/data/maps/connections.inc";
1162
+    QString text = readTextFile(path);
1163
+    if (text.isNull()) {
1164
+        return;
1165
+    }
1166
+
1167
+    mapsWithConnections.clear();
1168
+    QRegularExpression re("data\\/maps\\/(?<mapName>\\w+)\\/connections.inc");
1169
+    QList<QStringList>* includes = parseAsm(text);
1170
+    for (QStringList values : *includes) {
1171
+        if (values.length() != 2)
1172
+            continue;
1173
+
1174
+        QRegularExpressionMatch match = re.match(values.value(1));
1175
+        if (match.hasMatch()) {
1176
+            QString mapName = match.captured("mapName");
1177
+            mapsWithConnections.append(mapName);
1178
+        }
1179
+    }
1180
+}
1181
+
1182
+void Project::saveMapsWithConnections() {
1183
+    QString path = root + "/data/maps/connections.inc";
1184
+    QString text = "";
1185
+    for (QString mapName : mapsWithConnections) {
1186
+        if (mapNamesToMapConstants->contains(mapName)) {
1187
+            text += QString("\t.include \"data/maps/%1/connections.inc\"\n").arg(mapName);
1188
+        } else {
1189
+            qDebug() << QString("Failed to write connection include. %1 not a valid map name").arg(mapName);
1190
+        }
1191
+    }
1192
+    saveTextFile(path, text);
1193
+}
1194
+
1127
 QStringList Project::getSongNames() {
1195
 QStringList Project::getSongNames() {
1128
     QStringList names;
1196
     QStringList names;
1129
     QString text = readTextFile(root + "/include/constants/songs.h");
1197
     QString text = readTextFile(root + "/include/constants/songs.h");

+ 5
- 0
project.h View File

26
     QStringList *itemNames = NULL;
26
     QStringList *itemNames = NULL;
27
     QStringList *flagNames = NULL;
27
     QStringList *flagNames = NULL;
28
     QStringList *varNames = NULL;
28
     QStringList *varNames = NULL;
29
+    QStringList mapsWithConnections;
29
 
30
 
30
     QMap<QString, Map*> *map_cache;
31
     QMap<QString, Map*> *map_cache;
31
     Map* loadMap(QString);
32
     Map* loadMap(QString);
41
     QString readTextFile(QString path);
42
     QString readTextFile(QString path);
42
     void saveTextFile(QString path, QString text);
43
     void saveTextFile(QString path, QString text);
43
     void appendTextFile(QString path, QString text);
44
     void appendTextFile(QString path, QString text);
45
+    void deleteFile(QString path);
44
 
46
 
45
     void readMapGroups();
47
     void readMapGroups();
46
     Map* addNewMapToGroup(QString mapName, int groupNum);
48
     Map* addNewMapToGroup(QString mapName, int groupNum);
53
     void readMapAttributesTable();
55
     void readMapAttributesTable();
54
     void readAllMapAttributes();
56
     void readAllMapAttributes();
55
     void readMapAttributes(Map*);
57
     void readMapAttributes(Map*);
58
+    void readMapsWithConnections();
56
     void getTilesets(Map*);
59
     void getTilesets(Map*);
57
     void loadTilesetAssets(Tileset*);
60
     void loadTilesetAssets(Tileset*);
58
 
61
 
98
     QString getMapAssetsFilepath();
101
     QString getMapAssetsFilepath();
99
     void saveMapHeader(Map*);
102
     void saveMapHeader(Map*);
100
     void saveMapConnections(Map*);
103
     void saveMapConnections(Map*);
104
+    void updateMapsWithConnections(Map*);
105
+    void saveMapsWithConnections();
101
     void saveMapAttributesTable();
106
     void saveMapAttributesTable();
102
     void updateMapAttributes(Map* map);
107
     void updateMapAttributes(Map* map);
103
     void readCDefinesSorted(QString, QStringList, QStringList*);
108
     void readCDefinesSorted(QString, QStringList, QStringList*);