Browse Source

Respect map dimension changes in undo/redo history

Marcus Huderle 6 years ago
parent
commit
aafe258842
6 changed files with 81 additions and 43 deletions
  1. 4
    16
      editor.cpp
  2. 0
    2
      editor.h
  3. 21
    11
      mainwindow.cpp
  4. 2
    0
      mainwindow.h
  5. 39
    12
      map.cpp
  6. 15
    2
      map.h

+ 4
- 16
editor.cpp View File

26
 
26
 
27
 void Editor::undo() {
27
 void Editor::undo() {
28
     if (current_view) {
28
     if (current_view) {
29
-        ((MapPixmapItem*)current_view)->undo();
29
+        map->undo();
30
+        map_item->draw();
30
     }
31
     }
31
 }
32
 }
32
 
33
 
33
 void Editor::redo() {
34
 void Editor::redo() {
34
     if (current_view) {
35
     if (current_view) {
35
-        ((MapPixmapItem*)current_view)->redo();
36
+        map->redo();
37
+        map_item->draw();
36
     }
38
     }
37
 }
39
 }
38
 
40
 
1216
     }
1218
     }
1217
 }
1219
 }
1218
 
1220
 
1219
-void MapPixmapItem::undo() {
1220
-    if (map) {
1221
-        map->undo();
1222
-        draw();
1223
-    }
1224
-}
1225
-
1226
-void MapPixmapItem::redo() {
1227
-    if (map) {
1228
-        map->redo();
1229
-        draw();
1230
-    }
1231
-}
1232
-
1233
 void MapPixmapItem::updateCurHoveredTile(QPointF pos) {
1221
 void MapPixmapItem::updateCurHoveredTile(QPointF pos) {
1234
     int x = ((int)pos.x()) / 16;
1222
     int x = ((int)pos.x()) / 16;
1235
     int y = ((int)pos.y()) / 16;
1223
     int y = ((int)pos.y()) / 16;

+ 0
- 2
editor.h View File

252
     virtual void floodFill(QGraphicsSceneMouseEvent*);
252
     virtual void floodFill(QGraphicsSceneMouseEvent*);
253
     virtual void pick(QGraphicsSceneMouseEvent*);
253
     virtual void pick(QGraphicsSceneMouseEvent*);
254
     virtual void select(QGraphicsSceneMouseEvent*);
254
     virtual void select(QGraphicsSceneMouseEvent*);
255
-    virtual void undo();
256
-    virtual void redo();
257
     virtual void draw(bool ignoreCache = false);
255
     virtual void draw(bool ignoreCache = false);
258
 
256
 
259
 private:
257
 private:

+ 21
- 11
mainwindow.cpp View File

149
         return;
149
         return;
150
     }
150
     }
151
     editor->setMap(map_name);
151
     editor->setMap(map_name);
152
+    redrawMapScene();
153
+    displayMapProperties();
154
+
155
+    setWindowTitle(map_name + " - " + editor->project->getProjectTitle() + " - pretmap");
156
+
157
+    connect(editor->map, SIGNAL(mapChanged(Map*)), this, SLOT(onMapChanged(Map *)));
158
+    connect(editor->map, SIGNAL(mapNeedsRedrawing(Map*)), this, SLOT(onMapNeedsRedrawing(Map *)));
159
+    connect(editor->map, SIGNAL(statusBarMessage(QString)), this, SLOT(setStatusBarMessage(QString)));
152
 
160
 
161
+    setRecentMap(map_name);
162
+    updateMapList();
163
+}
164
+
165
+void MainWindow::redrawMapScene()
166
+{
167
+    editor->displayMap();
153
     on_tabWidget_currentChanged(ui->tabWidget->currentIndex());
168
     on_tabWidget_currentChanged(ui->tabWidget->currentIndex());
154
 
169
 
155
     ui->graphicsView_Map->setScene(editor->scene);
170
     ui->graphicsView_Map->setScene(editor->scene);
179
     ui->graphicsView_Elevation->setScene(editor->scene_elevation_metatiles);
194
     ui->graphicsView_Elevation->setScene(editor->scene_elevation_metatiles);
180
     //ui->graphicsView_Elevation->setSceneRect(editor->scene_elevation_metatiles->sceneRect());
195
     //ui->graphicsView_Elevation->setSceneRect(editor->scene_elevation_metatiles->sceneRect());
181
     ui->graphicsView_Elevation->setFixedSize(editor->elevation_metatiles_item->pixmap().width() + 2, editor->elevation_metatiles_item->pixmap().height() + 2);
196
     ui->graphicsView_Elevation->setFixedSize(editor->elevation_metatiles_item->pixmap().width() + 2, editor->elevation_metatiles_item->pixmap().height() + 2);
182
-
183
-    displayMapProperties();
184
-
185
-    setWindowTitle(map_name + " - " + editor->project->getProjectTitle() + " - pretmap");
186
-
187
-    connect(editor->map, SIGNAL(mapChanged(Map*)), this, SLOT(onMapChanged(Map *)));
188
-    connect(editor->map, SIGNAL(statusBarMessage(QString)), this, SLOT(setStatusBarMessage(QString)));
189
-
190
-    setRecentMap(map_name);
191
-    updateMapList();
192
 }
197
 }
193
 
198
 
194
 void MainWindow::setRecentMap(QString map_name) {
199
 void MainWindow::setRecentMap(QString map_name) {
787
     updateMapList();
792
     updateMapList();
788
 }
793
 }
789
 
794
 
795
+void MainWindow::onMapNeedsRedrawing(Map *map) {
796
+    redrawMapScene();
797
+}
798
+
790
 void MainWindow::on_action_Export_Map_Image_triggered()
799
 void MainWindow::on_action_Export_Map_Image_triggered()
791
 {
800
 {
792
     QString defaultFilepath = QString("%1/%2.png").arg(editor->project->root).arg(editor->map->name);
801
     QString defaultFilepath = QString("%1/%2.png").arg(editor->project->root).arg(editor->map->name);
896
 
905
 
897
     if (dialog.exec() == QDialog::Accepted) {
906
     if (dialog.exec() == QDialog::Accepted) {
898
         editor->map->setDimensions(widthSpinBox->value(), heightSpinBox->value());
907
         editor->map->setDimensions(widthSpinBox->value(), heightSpinBox->value());
899
-        setMap(editor->map->name);
908
+        editor->map->commit();
909
+        onMapNeedsRedrawing(editor->map);
900
     }
910
     }
901
 }
911
 }

+ 2
- 0
mainwindow.h View File

38
 
38
 
39
     void onLoadMapRequested(QString, QString);
39
     void onLoadMapRequested(QString, QString);
40
     void onMapChanged(Map *map);
40
     void onMapChanged(Map *map);
41
+    void onMapNeedsRedrawing(Map *map);
41
 
42
 
42
     void on_action_Save_triggered();
43
     void on_action_Save_triggered();
43
     void on_tabWidget_2_currentChanged(int index);
44
     void on_tabWidget_2_currentChanged(int index);
102
     Editor *editor = NULL;
103
     Editor *editor = NULL;
103
     QIcon* mapIcon;
104
     QIcon* mapIcon;
104
     void setMap(QString);
105
     void setMap(QString);
106
+    void redrawMapScene();
105
     void loadDataStructures();
107
     void loadDataStructures();
106
     void populateMapList();
108
     void populateMapList();
107
     QString getExistingDirectory(QString);
109
     QString getExistingDirectory(QString);

+ 39
- 12
map.cpp View File

6
 #include <QImage>
6
 #include <QImage>
7
 #include <QRegularExpression>
7
 #include <QRegularExpression>
8
 
8
 
9
+
9
 Map::Map(QObject *parent) : QObject(parent)
10
 Map::Map(QObject *parent) : QObject(parent)
10
 {
11
 {
11
     paint_tile_index = 1;
12
     paint_tile_index = 1;
276
         cacheBlockdata();
277
         cacheBlockdata();
277
         pixmap = pixmap.fromImage(image);
278
         pixmap = pixmap.fromImage(image);
278
     }
279
     }
280
+
279
     return pixmap;
281
     return pixmap;
280
 }
282
 }
281
 
283
 
427
     return QPixmap::fromImage(image);
429
     return QPixmap::fromImage(image);
428
 }
430
 }
429
 
431
 
430
-void Map::setDimensions(int newWidth, int newHeight) {
432
+void Map::setNewDimensionsBlockdata(int newWidth, int newHeight) {
431
     int oldWidth = getWidth();
433
     int oldWidth = getWidth();
432
     int oldHeight = getHeight();
434
     int oldHeight = getHeight();
433
 
435
 
444
     }
446
     }
445
 
447
 
446
     layout->blockdata->copyFrom(newBlockData);
448
     layout->blockdata->copyFrom(newBlockData);
449
+}
450
+
451
+void Map::setDimensions(int newWidth, int newHeight, bool setNewBlockdata) {
452
+    if (setNewBlockdata) {
453
+        setNewDimensionsBlockdata(newWidth, newHeight);
454
+    }
455
+
447
     layout->width = QString::number(newWidth);
456
     layout->width = QString::number(newWidth);
448
     layout->height = QString::number(newHeight);
457
     layout->height = QString::number(newHeight);
449
-    commit();
450
 
458
 
451
     emit mapChanged(this);
459
     emit mapChanged(this);
452
 }
460
 }
602
 
610
 
603
 
611
 
604
 void Map::undo() {
612
 void Map::undo() {
613
+    HistoryItem *commit = history.back();
614
+    if (!commit)
615
+        return;
616
+
605
     if (layout->blockdata) {
617
     if (layout->blockdata) {
606
-        Blockdata *commit = history.back();
607
-        if (commit != NULL) {
608
-            layout->blockdata->copyFrom(commit);
609
-            emit mapChanged(this);
618
+        layout->blockdata->copyFrom(commit->metatiles);
619
+        if (commit->layoutWidth != this->getWidth() || commit->layoutHeight != this->getHeight())
620
+        {
621
+            this->setDimensions(commit->layoutWidth, commit->layoutHeight, false);
622
+            emit mapNeedsRedrawing(this);
610
         }
623
         }
624
+
625
+        emit mapChanged(this);
611
     }
626
     }
612
 }
627
 }
613
 
628
 
614
 void Map::redo() {
629
 void Map::redo() {
630
+    HistoryItem *commit = history.next();
631
+    if (!commit)
632
+        return;
633
+
615
     if (layout->blockdata) {
634
     if (layout->blockdata) {
616
-        Blockdata *commit = history.next();
617
-        if (commit != NULL) {
618
-            layout->blockdata->copyFrom(commit);
619
-            emit mapChanged(this);
635
+        layout->blockdata->copyFrom(commit->metatiles);
636
+        if (commit->layoutWidth != this->getWidth() || commit->layoutHeight != this->getHeight())
637
+        {
638
+            this->setDimensions(commit->layoutWidth, commit->layoutHeight, false);
639
+            emit mapNeedsRedrawing(this);
620
         }
640
         }
641
+
642
+        emit mapChanged(this);
621
     }
643
     }
622
 }
644
 }
623
 
645
 
624
 void Map::commit() {
646
 void Map::commit() {
625
     if (layout->blockdata) {
647
     if (layout->blockdata) {
626
-        if (!layout->blockdata->equals(history.current())) {
627
-            Blockdata* commit = layout->blockdata->copy();
648
+        HistoryItem *item = history.current();
649
+        bool atCurrentHistory = item
650
+                && layout->blockdata->equals(item->metatiles)
651
+                && this->getWidth() == item->layoutWidth
652
+                && this->getHeight() == item->layoutHeight;
653
+        if (!atCurrentHistory) {
654
+            HistoryItem *commit = new HistoryItem(layout->blockdata->copy(), this->getWidth(), this->getHeight());
628
             history.push(commit);
655
             history.push(commit);
629
             emit mapChanged(this);
656
             emit mapChanged(this);
630
         }
657
         }

+ 15
- 2
map.h View File

10
 #include <QDebug>
10
 #include <QDebug>
11
 #include <QGraphicsPixmapItem>
11
 #include <QGraphicsPixmapItem>
12
 
12
 
13
+class HistoryItem {
14
+public:
15
+    Blockdata *metatiles;
16
+    short layoutWidth;
17
+    short layoutHeight;
18
+    HistoryItem(Blockdata *metatiles_, short layoutWidth_, short layoutHeight_) {
19
+        this->metatiles = metatiles_;
20
+        this->layoutWidth = layoutWidth_;
21
+        this->layoutHeight = layoutHeight_;
22
+    }
23
+};
13
 
24
 
14
 template <typename T>
25
 template <typename T>
15
 class History {
26
 class History {
182
     void floodFillCollisionElevation(int x, int y, uint collision, uint elevation);
193
     void floodFillCollisionElevation(int x, int y, uint collision, uint elevation);
183
     void _floodFillCollisionElevation(int x, int y, uint collision, uint elevation);
194
     void _floodFillCollisionElevation(int x, int y, uint collision, uint elevation);
184
 
195
 
185
-    History<Blockdata*> history;
196
+    History<HistoryItem*> history;
186
     void undo();
197
     void undo();
187
     void redo();
198
     void redo();
188
     void commit();
199
     void commit();
194
 
205
 
195
     QList<Connection*> connections;
206
     QList<Connection*> connections;
196
     QPixmap renderConnection(Connection);
207
     QPixmap renderConnection(Connection);
197
-    void setDimensions(int, int);
208
+    void setNewDimensionsBlockdata(int newWidth, int newHeight);
209
+    void setDimensions(int newWidth, int newHeight, bool setNewBlockData = true);
198
 
210
 
199
     QPixmap renderBorder();
211
     QPixmap renderBorder();
200
     void cacheBorder();
212
     void cacheBorder();
213
     void paintTileChanged(Map *map);
225
     void paintTileChanged(Map *map);
214
     void paintCollisionChanged(Map *map);
226
     void paintCollisionChanged(Map *map);
215
     void mapChanged(Map *map);
227
     void mapChanged(Map *map);
228
+    void mapNeedsRedrawing(Map *map);
216
     void statusBarMessage(QString);
229
     void statusBarMessage(QString);
217
 
230
 
218
 public slots:
231
 public slots: