Respect map dimension changes in undo/redo history
This commit is contained in:
parent
37903a5acb
commit
aafe258842
6 changed files with 81 additions and 43 deletions
20
editor.cpp
20
editor.cpp
|
@ -26,13 +26,15 @@ void Editor::save() {
|
||||||
|
|
||||||
void Editor::undo() {
|
void Editor::undo() {
|
||||||
if (current_view) {
|
if (current_view) {
|
||||||
((MapPixmapItem*)current_view)->undo();
|
map->undo();
|
||||||
|
map_item->draw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Editor::redo() {
|
void Editor::redo() {
|
||||||
if (current_view) {
|
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) {
|
void MapPixmapItem::updateCurHoveredTile(QPointF pos) {
|
||||||
int x = ((int)pos.x()) / 16;
|
int x = ((int)pos.x()) / 16;
|
||||||
int y = ((int)pos.y()) / 16;
|
int y = ((int)pos.y()) / 16;
|
||||||
|
|
2
editor.h
2
editor.h
|
@ -252,8 +252,6 @@ public:
|
||||||
virtual void floodFill(QGraphicsSceneMouseEvent*);
|
virtual void floodFill(QGraphicsSceneMouseEvent*);
|
||||||
virtual void pick(QGraphicsSceneMouseEvent*);
|
virtual void pick(QGraphicsSceneMouseEvent*);
|
||||||
virtual void select(QGraphicsSceneMouseEvent*);
|
virtual void select(QGraphicsSceneMouseEvent*);
|
||||||
virtual void undo();
|
|
||||||
virtual void redo();
|
|
||||||
virtual void draw(bool ignoreCache = false);
|
virtual void draw(bool ignoreCache = false);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -149,7 +149,22 @@ void MainWindow::setMap(QString map_name) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
editor->setMap(map_name);
|
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());
|
on_tabWidget_currentChanged(ui->tabWidget->currentIndex());
|
||||||
|
|
||||||
ui->graphicsView_Map->setScene(editor->scene);
|
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->setScene(editor->scene_elevation_metatiles);
|
||||||
//ui->graphicsView_Elevation->setSceneRect(editor->scene_elevation_metatiles->sceneRect());
|
//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);
|
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) {
|
void MainWindow::setRecentMap(QString map_name) {
|
||||||
|
@ -787,6 +792,10 @@ void MainWindow::onMapChanged(Map *map) {
|
||||||
updateMapList();
|
updateMapList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainWindow::onMapNeedsRedrawing(Map *map) {
|
||||||
|
redrawMapScene();
|
||||||
|
}
|
||||||
|
|
||||||
void MainWindow::on_action_Export_Map_Image_triggered()
|
void MainWindow::on_action_Export_Map_Image_triggered()
|
||||||
{
|
{
|
||||||
QString defaultFilepath = QString("%1/%2.png").arg(editor->project->root).arg(editor->map->name);
|
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) {
|
if (dialog.exec() == QDialog::Accepted) {
|
||||||
editor->map->setDimensions(widthSpinBox->value(), heightSpinBox->value());
|
editor->map->setDimensions(widthSpinBox->value(), heightSpinBox->value());
|
||||||
setMap(editor->map->name);
|
editor->map->commit();
|
||||||
|
onMapNeedsRedrawing(editor->map);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,7 @@ private slots:
|
||||||
|
|
||||||
void onLoadMapRequested(QString, QString);
|
void onLoadMapRequested(QString, QString);
|
||||||
void onMapChanged(Map *map);
|
void onMapChanged(Map *map);
|
||||||
|
void onMapNeedsRedrawing(Map *map);
|
||||||
|
|
||||||
void on_action_Save_triggered();
|
void on_action_Save_triggered();
|
||||||
void on_tabWidget_2_currentChanged(int index);
|
void on_tabWidget_2_currentChanged(int index);
|
||||||
|
@ -102,6 +103,7 @@ private:
|
||||||
Editor *editor = NULL;
|
Editor *editor = NULL;
|
||||||
QIcon* mapIcon;
|
QIcon* mapIcon;
|
||||||
void setMap(QString);
|
void setMap(QString);
|
||||||
|
void redrawMapScene();
|
||||||
void loadDataStructures();
|
void loadDataStructures();
|
||||||
void populateMapList();
|
void populateMapList();
|
||||||
QString getExistingDirectory(QString);
|
QString getExistingDirectory(QString);
|
||||||
|
|
51
map.cpp
51
map.cpp
|
@ -6,6 +6,7 @@
|
||||||
#include <QImage>
|
#include <QImage>
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
|
|
||||||
|
|
||||||
Map::Map(QObject *parent) : QObject(parent)
|
Map::Map(QObject *parent) : QObject(parent)
|
||||||
{
|
{
|
||||||
paint_tile_index = 1;
|
paint_tile_index = 1;
|
||||||
|
@ -276,6 +277,7 @@ QPixmap Map::render(bool ignoreCache = false) {
|
||||||
cacheBlockdata();
|
cacheBlockdata();
|
||||||
pixmap = pixmap.fromImage(image);
|
pixmap = pixmap.fromImage(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
return pixmap;
|
return pixmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -427,7 +429,7 @@ QPixmap Map::renderMetatiles() {
|
||||||
return QPixmap::fromImage(image);
|
return QPixmap::fromImage(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Map::setDimensions(int newWidth, int newHeight) {
|
void Map::setNewDimensionsBlockdata(int newWidth, int newHeight) {
|
||||||
int oldWidth = getWidth();
|
int oldWidth = getWidth();
|
||||||
int oldHeight = getHeight();
|
int oldHeight = getHeight();
|
||||||
|
|
||||||
|
@ -444,9 +446,15 @@ void Map::setDimensions(int newWidth, int newHeight) {
|
||||||
}
|
}
|
||||||
|
|
||||||
layout->blockdata->copyFrom(newBlockData);
|
layout->blockdata->copyFrom(newBlockData);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Map::setDimensions(int newWidth, int newHeight, bool setNewBlockdata) {
|
||||||
|
if (setNewBlockdata) {
|
||||||
|
setNewDimensionsBlockdata(newWidth, newHeight);
|
||||||
|
}
|
||||||
|
|
||||||
layout->width = QString::number(newWidth);
|
layout->width = QString::number(newWidth);
|
||||||
layout->height = QString::number(newHeight);
|
layout->height = QString::number(newHeight);
|
||||||
commit();
|
|
||||||
|
|
||||||
emit mapChanged(this);
|
emit mapChanged(this);
|
||||||
}
|
}
|
||||||
|
@ -602,29 +610,48 @@ void Map::_floodFillCollisionElevation(int x, int y, uint collision, uint elevat
|
||||||
|
|
||||||
|
|
||||||
void Map::undo() {
|
void Map::undo() {
|
||||||
|
HistoryItem *commit = history.back();
|
||||||
|
if (!commit)
|
||||||
|
return;
|
||||||
|
|
||||||
if (layout->blockdata) {
|
if (layout->blockdata) {
|
||||||
Blockdata *commit = history.back();
|
layout->blockdata->copyFrom(commit->metatiles);
|
||||||
if (commit != NULL) {
|
if (commit->layoutWidth != this->getWidth() || commit->layoutHeight != this->getHeight())
|
||||||
layout->blockdata->copyFrom(commit);
|
{
|
||||||
emit mapChanged(this);
|
this->setDimensions(commit->layoutWidth, commit->layoutHeight, false);
|
||||||
|
emit mapNeedsRedrawing(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
emit mapChanged(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Map::redo() {
|
void Map::redo() {
|
||||||
|
HistoryItem *commit = history.next();
|
||||||
|
if (!commit)
|
||||||
|
return;
|
||||||
|
|
||||||
if (layout->blockdata) {
|
if (layout->blockdata) {
|
||||||
Blockdata *commit = history.next();
|
layout->blockdata->copyFrom(commit->metatiles);
|
||||||
if (commit != NULL) {
|
if (commit->layoutWidth != this->getWidth() || commit->layoutHeight != this->getHeight())
|
||||||
layout->blockdata->copyFrom(commit);
|
{
|
||||||
emit mapChanged(this);
|
this->setDimensions(commit->layoutWidth, commit->layoutHeight, false);
|
||||||
|
emit mapNeedsRedrawing(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
emit mapChanged(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Map::commit() {
|
void Map::commit() {
|
||||||
if (layout->blockdata) {
|
if (layout->blockdata) {
|
||||||
if (!layout->blockdata->equals(history.current())) {
|
HistoryItem *item = history.current();
|
||||||
Blockdata* commit = layout->blockdata->copy();
|
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);
|
history.push(commit);
|
||||||
emit mapChanged(this);
|
emit mapChanged(this);
|
||||||
}
|
}
|
||||||
|
|
17
map.h
17
map.h
|
@ -10,6 +10,17 @@
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QGraphicsPixmapItem>
|
#include <QGraphicsPixmapItem>
|
||||||
|
|
||||||
|
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 <typename T>
|
template <typename T>
|
||||||
class History {
|
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);
|
||||||
void _floodFillCollisionElevation(int x, int y, uint collision, uint elevation);
|
void _floodFillCollisionElevation(int x, int y, uint collision, uint elevation);
|
||||||
|
|
||||||
History<Blockdata*> history;
|
History<HistoryItem*> history;
|
||||||
void undo();
|
void undo();
|
||||||
void redo();
|
void redo();
|
||||||
void commit();
|
void commit();
|
||||||
|
@ -194,7 +205,8 @@ public:
|
||||||
|
|
||||||
QList<Connection*> connections;
|
QList<Connection*> connections;
|
||||||
QPixmap renderConnection(Connection);
|
QPixmap renderConnection(Connection);
|
||||||
void setDimensions(int, int);
|
void setNewDimensionsBlockdata(int newWidth, int newHeight);
|
||||||
|
void setDimensions(int newWidth, int newHeight, bool setNewBlockData = true);
|
||||||
|
|
||||||
QPixmap renderBorder();
|
QPixmap renderBorder();
|
||||||
void cacheBorder();
|
void cacheBorder();
|
||||||
|
@ -213,6 +225,7 @@ signals:
|
||||||
void paintTileChanged(Map *map);
|
void paintTileChanged(Map *map);
|
||||||
void paintCollisionChanged(Map *map);
|
void paintCollisionChanged(Map *map);
|
||||||
void mapChanged(Map *map);
|
void mapChanged(Map *map);
|
||||||
|
void mapNeedsRedrawing(Map *map);
|
||||||
void statusBarMessage(QString);
|
void statusBarMessage(QString);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
|
|
Loading…
Reference in a new issue