ソースを参照

Respect map dimension changes in undo/redo history

Marcus Huderle 6 年 前
コミット
aafe258842
共有6 個のファイルを変更した81 個の追加43 個の削除を含む
  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 ファイルの表示

@@ -26,13 +26,15 @@ void Editor::save() {
26 26
 
27 27
 void Editor::undo() {
28 28
     if (current_view) {
29
-        ((MapPixmapItem*)current_view)->undo();
29
+        map->undo();
30
+        map_item->draw();
30 31
     }
31 32
 }
32 33
 
33 34
 void Editor::redo() {
34 35
     if (current_view) {
35
-        ((MapPixmapItem*)current_view)->redo();
36
+        map->redo();
37
+        map_item->draw();
36 38
     }
37 39
 }
38 40
 
@@ -1216,20 +1218,6 @@ void MapPixmapItem::draw(bool ignoreCache) {
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 1221
 void MapPixmapItem::updateCurHoveredTile(QPointF pos) {
1234 1222
     int x = ((int)pos.x()) / 16;
1235 1223
     int y = ((int)pos.y()) / 16;

+ 0
- 2
editor.h ファイルの表示

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

+ 21
- 11
mainwindow.cpp ファイルの表示

@@ -149,7 +149,22 @@ void MainWindow::setMap(QString map_name) {
149 149
         return;
150 150
     }
151 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 168
     on_tabWidget_currentChanged(ui->tabWidget->currentIndex());
154 169
 
155 170
     ui->graphicsView_Map->setScene(editor->scene);
@@ -179,16 +194,6 @@ void MainWindow::setMap(QString map_name) {
179 194
     ui->graphicsView_Elevation->setScene(editor->scene_elevation_metatiles);
180 195
     //ui->graphicsView_Elevation->setSceneRect(editor->scene_elevation_metatiles->sceneRect());
181 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 199
 void MainWindow::setRecentMap(QString map_name) {
@@ -787,6 +792,10 @@ void MainWindow::onMapChanged(Map *map) {
787 792
     updateMapList();
788 793
 }
789 794
 
795
+void MainWindow::onMapNeedsRedrawing(Map *map) {
796
+    redrawMapScene();
797
+}
798
+
790 799
 void MainWindow::on_action_Export_Map_Image_triggered()
791 800
 {
792 801
     QString defaultFilepath = QString("%1/%2.png").arg(editor->project->root).arg(editor->map->name);
@@ -896,6 +905,7 @@ void MainWindow::on_pushButton_clicked()
896 905
 
897 906
     if (dialog.exec() == QDialog::Accepted) {
898 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 ファイルの表示

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

+ 39
- 12
map.cpp ファイルの表示

@@ -6,6 +6,7 @@
6 6
 #include <QImage>
7 7
 #include <QRegularExpression>
8 8
 
9
+
9 10
 Map::Map(QObject *parent) : QObject(parent)
10 11
 {
11 12
     paint_tile_index = 1;
@@ -276,6 +277,7 @@ QPixmap Map::render(bool ignoreCache = false) {
276 277
         cacheBlockdata();
277 278
         pixmap = pixmap.fromImage(image);
278 279
     }
280
+
279 281
     return pixmap;
280 282
 }
281 283
 
@@ -427,7 +429,7 @@ QPixmap Map::renderMetatiles() {
427 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 433
     int oldWidth = getWidth();
432 434
     int oldHeight = getHeight();
433 435
 
@@ -444,9 +446,15 @@ void Map::setDimensions(int newWidth, int newHeight) {
444 446
     }
445 447
 
446 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 456
     layout->width = QString::number(newWidth);
448 457
     layout->height = QString::number(newHeight);
449
-    commit();
450 458
 
451 459
     emit mapChanged(this);
452 460
 }
@@ -602,29 +610,48 @@ void Map::_floodFillCollisionElevation(int x, int y, uint collision, uint elevat
602 610
 
603 611
 
604 612
 void Map::undo() {
613
+    HistoryItem *commit = history.back();
614
+    if (!commit)
615
+        return;
616
+
605 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 629
 void Map::redo() {
630
+    HistoryItem *commit = history.next();
631
+    if (!commit)
632
+        return;
633
+
615 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 646
 void Map::commit() {
625 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 655
             history.push(commit);
629 656
             emit mapChanged(this);
630 657
         }

+ 15
- 2
map.h ファイルの表示

@@ -10,6 +10,17 @@
10 10
 #include <QDebug>
11 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 25
 template <typename T>
15 26
 class History {
@@ -182,7 +193,7 @@ public:
182 193
     void floodFillCollisionElevation(int x, int y, uint collision, uint elevation);
183 194
     void _floodFillCollisionElevation(int x, int y, uint collision, uint elevation);
184 195
 
185
-    History<Blockdata*> history;
196
+    History<HistoryItem*> history;
186 197
     void undo();
187 198
     void redo();
188 199
     void commit();
@@ -194,7 +205,8 @@ public:
194 205
 
195 206
     QList<Connection*> connections;
196 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 211
     QPixmap renderBorder();
200 212
     void cacheBorder();
@@ -213,6 +225,7 @@ signals:
213 225
     void paintTileChanged(Map *map);
214 226
     void paintCollisionChanged(Map *map);
215 227
     void mapChanged(Map *map);
228
+    void mapNeedsRedrawing(Map *map);
216 229
     void statusBarMessage(QString);
217 230
 
218 231
 public slots: