From aafe25884283418a4fbb8b1c6ea4813ea680fcf3 Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Mon, 9 Jul 2018 17:40:34 -0500 Subject: [PATCH] Respect map dimension changes in undo/redo history --- editor.cpp | 20 ++++---------------- editor.h | 2 -- mainwindow.cpp | 32 ++++++++++++++++++++----------- mainwindow.h | 2 ++ map.cpp | 51 ++++++++++++++++++++++++++++++++++++++------------ map.h | 17 +++++++++++++++-- 6 files changed, 81 insertions(+), 43 deletions(-) diff --git a/editor.cpp b/editor.cpp index 6ffa8bae..510c3bad 100755 --- a/editor.cpp +++ b/editor.cpp @@ -26,13 +26,15 @@ void Editor::save() { void Editor::undo() { if (current_view) { - ((MapPixmapItem*)current_view)->undo(); + map->undo(); + map_item->draw(); } } void Editor::redo() { if (current_view) { - ((MapPixmapItem*)current_view)->redo(); + map->redo(); + map_item->draw(); } } @@ -1216,20 +1218,6 @@ void MapPixmapItem::draw(bool ignoreCache) { } } -void MapPixmapItem::undo() { - if (map) { - map->undo(); - draw(); - } -} - -void MapPixmapItem::redo() { - if (map) { - map->redo(); - draw(); - } -} - void MapPixmapItem::updateCurHoveredTile(QPointF pos) { int x = ((int)pos.x()) / 16; int y = ((int)pos.y()) / 16; diff --git a/editor.h b/editor.h index b93f9bd5..916f2521 100755 --- a/editor.h +++ b/editor.h @@ -252,8 +252,6 @@ public: virtual void floodFill(QGraphicsSceneMouseEvent*); virtual void pick(QGraphicsSceneMouseEvent*); virtual void select(QGraphicsSceneMouseEvent*); - virtual void undo(); - virtual void redo(); virtual void draw(bool ignoreCache = false); private: diff --git a/mainwindow.cpp b/mainwindow.cpp index e9e97dd1..6d9ac7a5 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -149,7 +149,22 @@ void MainWindow::setMap(QString map_name) { return; } editor->setMap(map_name); + redrawMapScene(); + displayMapProperties(); + setWindowTitle(map_name + " - " + editor->project->getProjectTitle() + " - pretmap"); + + connect(editor->map, SIGNAL(mapChanged(Map*)), this, SLOT(onMapChanged(Map *))); + connect(editor->map, SIGNAL(mapNeedsRedrawing(Map*)), this, SLOT(onMapNeedsRedrawing(Map *))); + connect(editor->map, SIGNAL(statusBarMessage(QString)), this, SLOT(setStatusBarMessage(QString))); + + setRecentMap(map_name); + updateMapList(); +} + +void MainWindow::redrawMapScene() +{ + editor->displayMap(); on_tabWidget_currentChanged(ui->tabWidget->currentIndex()); ui->graphicsView_Map->setScene(editor->scene); @@ -179,16 +194,6 @@ void MainWindow::setMap(QString map_name) { ui->graphicsView_Elevation->setScene(editor->scene_elevation_metatiles); //ui->graphicsView_Elevation->setSceneRect(editor->scene_elevation_metatiles->sceneRect()); ui->graphicsView_Elevation->setFixedSize(editor->elevation_metatiles_item->pixmap().width() + 2, editor->elevation_metatiles_item->pixmap().height() + 2); - - displayMapProperties(); - - setWindowTitle(map_name + " - " + editor->project->getProjectTitle() + " - pretmap"); - - connect(editor->map, SIGNAL(mapChanged(Map*)), this, SLOT(onMapChanged(Map *))); - connect(editor->map, SIGNAL(statusBarMessage(QString)), this, SLOT(setStatusBarMessage(QString))); - - setRecentMap(map_name); - updateMapList(); } void MainWindow::setRecentMap(QString map_name) { @@ -787,6 +792,10 @@ void MainWindow::onMapChanged(Map *map) { updateMapList(); } +void MainWindow::onMapNeedsRedrawing(Map *map) { + redrawMapScene(); +} + void MainWindow::on_action_Export_Map_Image_triggered() { QString defaultFilepath = QString("%1/%2.png").arg(editor->project->root).arg(editor->map->name); @@ -896,6 +905,7 @@ void MainWindow::on_pushButton_clicked() if (dialog.exec() == QDialog::Accepted) { editor->map->setDimensions(widthSpinBox->value(), heightSpinBox->value()); - setMap(editor->map->name); + editor->map->commit(); + onMapNeedsRedrawing(editor->map); } } diff --git a/mainwindow.h b/mainwindow.h index b401edd4..530094d1 100755 --- a/mainwindow.h +++ b/mainwindow.h @@ -38,6 +38,7 @@ private slots: void onLoadMapRequested(QString, QString); void onMapChanged(Map *map); + void onMapNeedsRedrawing(Map *map); void on_action_Save_triggered(); void on_tabWidget_2_currentChanged(int index); @@ -102,6 +103,7 @@ private: Editor *editor = NULL; QIcon* mapIcon; void setMap(QString); + void redrawMapScene(); void loadDataStructures(); void populateMapList(); QString getExistingDirectory(QString); diff --git a/map.cpp b/map.cpp index 858bf156..98b859ce 100755 --- a/map.cpp +++ b/map.cpp @@ -6,6 +6,7 @@ #include #include + Map::Map(QObject *parent) : QObject(parent) { paint_tile_index = 1; @@ -276,6 +277,7 @@ QPixmap Map::render(bool ignoreCache = false) { cacheBlockdata(); pixmap = pixmap.fromImage(image); } + return pixmap; } @@ -427,7 +429,7 @@ QPixmap Map::renderMetatiles() { return QPixmap::fromImage(image); } -void Map::setDimensions(int newWidth, int newHeight) { +void Map::setNewDimensionsBlockdata(int newWidth, int newHeight) { int oldWidth = getWidth(); int oldHeight = getHeight(); @@ -444,9 +446,15 @@ void Map::setDimensions(int newWidth, int newHeight) { } layout->blockdata->copyFrom(newBlockData); +} + +void Map::setDimensions(int newWidth, int newHeight, bool setNewBlockdata) { + if (setNewBlockdata) { + setNewDimensionsBlockdata(newWidth, newHeight); + } + layout->width = QString::number(newWidth); layout->height = QString::number(newHeight); - commit(); emit mapChanged(this); } @@ -602,29 +610,48 @@ void Map::_floodFillCollisionElevation(int x, int y, uint collision, uint elevat void Map::undo() { + HistoryItem *commit = history.back(); + if (!commit) + return; + if (layout->blockdata) { - Blockdata *commit = history.back(); - if (commit != NULL) { - layout->blockdata->copyFrom(commit); - emit mapChanged(this); + layout->blockdata->copyFrom(commit->metatiles); + if (commit->layoutWidth != this->getWidth() || commit->layoutHeight != this->getHeight()) + { + this->setDimensions(commit->layoutWidth, commit->layoutHeight, false); + emit mapNeedsRedrawing(this); } + + emit mapChanged(this); } } void Map::redo() { + HistoryItem *commit = history.next(); + if (!commit) + return; + if (layout->blockdata) { - Blockdata *commit = history.next(); - if (commit != NULL) { - layout->blockdata->copyFrom(commit); - emit mapChanged(this); + layout->blockdata->copyFrom(commit->metatiles); + if (commit->layoutWidth != this->getWidth() || commit->layoutHeight != this->getHeight()) + { + this->setDimensions(commit->layoutWidth, commit->layoutHeight, false); + emit mapNeedsRedrawing(this); } + + emit mapChanged(this); } } void Map::commit() { if (layout->blockdata) { - if (!layout->blockdata->equals(history.current())) { - Blockdata* commit = layout->blockdata->copy(); + HistoryItem *item = history.current(); + bool atCurrentHistory = item + && layout->blockdata->equals(item->metatiles) + && this->getWidth() == item->layoutWidth + && this->getHeight() == item->layoutHeight; + if (!atCurrentHistory) { + HistoryItem *commit = new HistoryItem(layout->blockdata->copy(), this->getWidth(), this->getHeight()); history.push(commit); emit mapChanged(this); } diff --git a/map.h b/map.h index 450e39ce..19c06c03 100755 --- a/map.h +++ b/map.h @@ -10,6 +10,17 @@ #include #include +class HistoryItem { +public: + Blockdata *metatiles; + short layoutWidth; + short layoutHeight; + HistoryItem(Blockdata *metatiles_, short layoutWidth_, short layoutHeight_) { + this->metatiles = metatiles_; + this->layoutWidth = layoutWidth_; + this->layoutHeight = layoutHeight_; + } +}; template class History { @@ -182,7 +193,7 @@ public: void floodFillCollisionElevation(int x, int y, uint collision, uint elevation); void _floodFillCollisionElevation(int x, int y, uint collision, uint elevation); - History history; + History history; void undo(); void redo(); void commit(); @@ -194,7 +205,8 @@ public: QList connections; QPixmap renderConnection(Connection); - void setDimensions(int, int); + void setNewDimensionsBlockdata(int newWidth, int newHeight); + void setDimensions(int newWidth, int newHeight, bool setNewBlockData = true); QPixmap renderBorder(); void cacheBorder(); @@ -213,6 +225,7 @@ signals: void paintTileChanged(Map *map); void paintCollisionChanged(Map *map); void mapChanged(Map *map); + void mapNeedsRedrawing(Map *map); void statusBarMessage(QString); public slots: