Add edit history for border edits via script

This commit is contained in:
GriffinR 2022-08-29 14:48:16 -04:00
parent 21ed9bc140
commit 3e7bfe126a
7 changed files with 86 additions and 42 deletions

View file

@ -343,12 +343,14 @@ public:
/// Implements a command to commit map edits from the scripting API. /// Implements a command to commit map edits from the scripting API.
/// The scripting api can edit metatiles and map dimensions. /// The scripting api can edit map/border blocks and dimensions.
class ScriptEditMap : public QUndoCommand { class ScriptEditMap : public QUndoCommand {
public: public:
ScriptEditMap(Map *map, ScriptEditMap(Map *map,
QSize oldMapDimensions, QSize newMapDimensions, QSize oldMapDimensions, QSize newMapDimensions,
const Blockdata &oldMetatiles, const Blockdata &newMetatiles, const Blockdata &oldMetatiles, const Blockdata &newMetatiles,
QSize oldBorderDimensions, QSize newBorderDimensions,
const Blockdata &oldBorder, const Blockdata &newBorder,
QUndoCommand *parent = nullptr); QUndoCommand *parent = nullptr);
void undo() override; void undo() override;
@ -363,10 +365,18 @@ private:
Blockdata newMetatiles; Blockdata newMetatiles;
Blockdata oldMetatiles; Blockdata oldMetatiles;
Blockdata newBorder;
Blockdata oldBorder;
int oldMapWidth; int oldMapWidth;
int oldMapHeight; int oldMapHeight;
int newMapWidth; int newMapWidth;
int newMapHeight; int newMapHeight;
int oldBorderWidth;
int oldBorderHeight;
int newBorderWidth;
int newBorderHeight;
}; };
#endif // EDITCOMMANDS_H #endif // EDITCOMMANDS_H

View file

@ -33,8 +33,10 @@ public:
Blockdata cached_border; Blockdata cached_border;
struct { struct {
Blockdata blocks; Blockdata blocks;
QSize dimensions; QSize mapDimensions;
} lastCommitMapBlocks; // to track map changes Blockdata border;
QSize borderDimensions;
} lastCommitBlocks; // to track map changes
int getWidth(); int getWidth();
int getHeight(); int getHeight();

View file

@ -157,6 +157,7 @@ public slots:
void openScript(const QString &scriptLabel) const; void openScript(const QString &scriptLabel) const;
void openProjectInTextEditor() const; void openProjectInTextEditor() const;
void maskNonVisibleConnectionTiles(); void maskNonVisibleConnectionTiles();
void onBorderMetatilesChanged();
private: private:
void setConnectionItemsVisible(bool); void setConnectionItemsVisible(bool);
@ -192,7 +193,6 @@ private slots:
void onConnectionItemSelected(ConnectionPixmapItem* connectionItem); void onConnectionItemSelected(ConnectionPixmapItem* connectionItem);
void onConnectionItemDoubleClicked(ConnectionPixmapItem* connectionItem); void onConnectionItemDoubleClicked(ConnectionPixmapItem* connectionItem);
void onConnectionDirectionChanged(QString newDirection); void onConnectionDirectionChanged(QString newDirection);
void onBorderMetatilesChanged();
void onHoveredMovementPermissionChanged(uint16_t, uint16_t); void onHoveredMovementPermissionChanged(uint16_t, uint16_t);
void onHoveredMovementPermissionCleared(); void onHoveredMovementPermissionCleared();
void onHoveredMetatileSelectionChanged(uint16_t); void onHoveredMetatileSelectionChanged(uint16_t);

View file

@ -46,8 +46,6 @@ public:
Q_INVOKABLE QJSValue getBlock(int x, int y); Q_INVOKABLE QJSValue getBlock(int x, int y);
void tryRedrawMapArea(bool forceRedraw); void tryRedrawMapArea(bool forceRedraw);
void tryCommitMapChanges(bool commitChanges); void tryCommitMapChanges(bool commitChanges);
void tryRedrawBorder(bool forceRedraw);
void tryCommitBorderChanges(bool commitChanges);
Q_INVOKABLE void setBlock(int x, int y, int tile, int collision, int elevation, bool forceRedraw = true, bool commitChanges = true); Q_INVOKABLE void setBlock(int x, int y, int tile, int collision, int elevation, bool forceRedraw = true, bool commitChanges = true);
Q_INVOKABLE void setBlocksFromSelection(int x, int y, bool forceRedraw = true, bool commitChanges = true); Q_INVOKABLE void setBlocksFromSelection(int x, int y, bool forceRedraw = true, bool commitChanges = true);
Q_INVOKABLE int getMetatileId(int x, int y); Q_INVOKABLE int getMetatileId(int x, int y);

View file

@ -49,7 +49,7 @@ void PaintMetatile::redo() {
map->setBlockdata(newMetatiles); map->setBlockdata(newMetatiles);
map->layout->lastCommitMapBlocks.blocks = map->layout->blockdata; map->layout->lastCommitBlocks.blocks = map->layout->blockdata;
renderMapBlocks(map); renderMapBlocks(map);
} }
@ -59,7 +59,7 @@ void PaintMetatile::undo() {
map->setBlockdata(oldMetatiles); map->setBlockdata(oldMetatiles);
map->layout->lastCommitMapBlocks.blocks = map->layout->blockdata; map->layout->lastCommitBlocks.blocks = map->layout->blockdata;
renderMapBlocks(map); renderMapBlocks(map);
@ -103,6 +103,8 @@ void PaintBorder::redo() {
map->setBorderBlockData(newBorder); map->setBorderBlockData(newBorder);
map->layout->lastCommitBlocks.border = map->layout->border;
map->borderItem->draw(); map->borderItem->draw();
} }
@ -111,6 +113,8 @@ void PaintBorder::undo() {
map->setBorderBlockData(oldBorder); map->setBorderBlockData(oldBorder);
map->layout->lastCommitBlocks.border = map->layout->border;
map->borderItem->draw(); map->borderItem->draw();
QUndoCommand::undo(); QUndoCommand::undo();
@ -139,7 +143,7 @@ void ShiftMetatiles::redo() {
map->setBlockdata(newMetatiles); map->setBlockdata(newMetatiles);
map->layout->lastCommitMapBlocks.blocks = map->layout->blockdata; map->layout->lastCommitBlocks.blocks = map->layout->blockdata;
renderMapBlocks(map, true); renderMapBlocks(map, true);
} }
@ -149,7 +153,7 @@ void ShiftMetatiles::undo() {
map->setBlockdata(oldMetatiles); map->setBlockdata(oldMetatiles);
map->layout->lastCommitMapBlocks.blocks = map->layout->blockdata; map->layout->lastCommitBlocks.blocks = map->layout->blockdata;
renderMapBlocks(map, true); renderMapBlocks(map, true);
@ -213,7 +217,8 @@ void ResizeMap::redo() {
map->layout->border = newBorder; map->layout->border = newBorder;
map->setBorderDimensions(newBorderWidth, newBorderHeight, false); map->setBorderDimensions(newBorderWidth, newBorderHeight, false);
map->layout->lastCommitMapBlocks.dimensions = QSize(map->getWidth(), map->getHeight()); map->layout->lastCommitBlocks.mapDimensions = QSize(map->getWidth(), map->getHeight());
map->layout->lastCommitBlocks.borderDimensions = QSize(map->getBorderWidth(), map->getBorderHeight());
map->mapNeedsRedrawing(); map->mapNeedsRedrawing();
} }
@ -227,7 +232,8 @@ void ResizeMap::undo() {
map->layout->border = oldBorder; map->layout->border = oldBorder;
map->setBorderDimensions(oldBorderWidth, oldBorderHeight, false); map->setBorderDimensions(oldBorderWidth, oldBorderHeight, false);
map->layout->lastCommitMapBlocks.dimensions = QSize(map->getWidth(), map->getHeight()); map->layout->lastCommitBlocks.mapDimensions = QSize(map->getWidth(), map->getHeight());
map->layout->lastCommitBlocks.borderDimensions = QSize(map->getBorderWidth(), map->getBorderHeight());
map->mapNeedsRedrawing(); map->mapNeedsRedrawing();
@ -484,6 +490,8 @@ int EventPaste::id() const {
ScriptEditMap::ScriptEditMap(Map *map, ScriptEditMap::ScriptEditMap(Map *map,
QSize oldMapDimensions, QSize newMapDimensions, QSize oldMapDimensions, QSize newMapDimensions,
const Blockdata &oldMetatiles, const Blockdata &newMetatiles, const Blockdata &oldMetatiles, const Blockdata &newMetatiles,
QSize oldBorderDimensions, QSize newBorderDimensions,
const Blockdata &oldBorder, const Blockdata &newBorder,
QUndoCommand *parent) : QUndoCommand(parent) { QUndoCommand *parent) : QUndoCommand(parent) {
setText("Script Edit Map"); setText("Script Edit Map");
@ -496,6 +504,14 @@ ScriptEditMap::ScriptEditMap(Map *map,
this->oldMapHeight = oldMapDimensions.height(); this->oldMapHeight = oldMapDimensions.height();
this->newMapWidth = newMapDimensions.width(); this->newMapWidth = newMapDimensions.width();
this->newMapHeight = newMapDimensions.height(); this->newMapHeight = newMapDimensions.height();
this->oldBorder = oldBorder;
this->newBorder = newBorder;
this->oldBorderWidth = oldBorderDimensions.width();
this->oldBorderHeight = oldBorderDimensions.height();
this->newBorderWidth = newBorderDimensions.width();
this->newBorderHeight = newBorderDimensions.height();
} }
void ScriptEditMap::redo() { void ScriptEditMap::redo() {
@ -510,10 +526,19 @@ void ScriptEditMap::redo() {
map->setBlockdata(newMetatiles); map->setBlockdata(newMetatiles);
} }
map->layout->lastCommitMapBlocks.blocks = newMetatiles; if (newBorderWidth != map->getBorderWidth() || newBorderHeight != map->getBorderHeight()) {
map->layout->lastCommitMapBlocks.dimensions = QSize(newMapWidth, newMapHeight); map->layout->border = newBorder;
map->setBorderDimensions(newBorderWidth, newBorderHeight, false);
} else {
map->setBorderBlockData(newBorder);
}
renderMapBlocks(map); map->layout->lastCommitBlocks.blocks = newMetatiles;
map->layout->lastCommitBlocks.mapDimensions = QSize(newMapWidth, newMapHeight);
map->layout->lastCommitBlocks.border = newBorder;
map->layout->lastCommitBlocks.borderDimensions = QSize(newBorderWidth, newBorderHeight);
map->mapNeedsRedrawing();
} }
void ScriptEditMap::undo() { void ScriptEditMap::undo() {
@ -526,10 +551,19 @@ void ScriptEditMap::undo() {
map->setBlockdata(oldMetatiles); map->setBlockdata(oldMetatiles);
} }
map->layout->lastCommitMapBlocks.blocks = oldMetatiles; if (oldBorderWidth != map->getBorderWidth() || oldBorderHeight != map->getBorderHeight()) {
map->layout->lastCommitMapBlocks.dimensions = QSize(oldMapWidth, oldMapHeight); map->layout->border = oldBorder;
map->setBorderDimensions(oldBorderWidth, oldBorderHeight, false);
} else {
map->setBorderBlockData(oldBorder);
}
renderMapBlocks(map); map->layout->lastCommitBlocks.blocks = oldMetatiles;
map->layout->lastCommitBlocks.mapDimensions = QSize(oldMapWidth, oldMapHeight);
map->layout->lastCommitBlocks.border = oldBorder;
map->layout->lastCommitBlocks.borderDimensions = QSize(oldBorderWidth, oldBorderHeight);
map->mapNeedsRedrawing();
QUndoCommand::undo(); QUndoCommand::undo();
} }

View file

@ -29,12 +29,15 @@ void MainWindow::tryRedrawMapArea(bool forceRedraw) {
if (this->needsFullRedraw) { if (this->needsFullRedraw) {
this->editor->map_item->draw(true); this->editor->map_item->draw(true);
this->editor->collision_item->draw(true); this->editor->collision_item->draw(true);
this->editor->selected_border_metatiles_item->draw();
this->editor->updateMapBorder(); this->editor->updateMapBorder();
this->editor->updateMapConnections(); this->editor->updateMapConnections();
this->needsFullRedraw = false; this->needsFullRedraw = false;
} else { } else {
this->editor->map_item->draw(); this->editor->map_item->draw();
this->editor->collision_item->draw(); this->editor->collision_item->draw();
this->editor->selected_border_metatiles_item->draw();
this->editor->updateMapBorder();
} }
} }
@ -43,23 +46,15 @@ void MainWindow::tryCommitMapChanges(bool commitChanges) {
Map *map = this->editor->map; Map *map = this->editor->map;
if (map) { if (map) {
map->editHistory.push(new ScriptEditMap(map, map->editHistory.push(new ScriptEditMap(map,
map->layout->lastCommitMapBlocks.dimensions, QSize(map->getWidth(), map->getHeight()), map->layout->lastCommitBlocks.mapDimensions, QSize(map->getWidth(), map->getHeight()),
map->layout->lastCommitMapBlocks.blocks, map->layout->blockdata map->layout->lastCommitBlocks.blocks, map->layout->blockdata,
map->layout->lastCommitBlocks.borderDimensions, QSize(map->getBorderWidth(), map->getBorderHeight()),
map->layout->lastCommitBlocks.border, map->layout->border
)); ));
} }
} }
} }
void MainWindow::tryRedrawBorder(bool forceRedraw) {
if (!forceRedraw) return;
// TODO
}
void MainWindow::tryCommitBorderChanges(bool commitChanges) {
if (!commitChanges) return;
// TODO
}
void MainWindow::setBlock(int x, int y, int tile, int collision, int elevation, bool forceRedraw, bool commitChanges) { void MainWindow::setBlock(int x, int y, int tile, int collision, int elevation, bool forceRedraw, bool commitChanges) {
if (!this->editor || !this->editor->map) if (!this->editor || !this->editor->map)
return; return;
@ -112,8 +107,8 @@ void MainWindow::setBorderMetatileId(int x, int y, int metatileId, bool forceRed
if (!this->editor->map->isWithinBorderBounds(x, y)) if (!this->editor->map->isWithinBorderBounds(x, y))
return; return;
this->editor->map->setBorderMetatileId(x, y, metatileId); this->editor->map->setBorderMetatileId(x, y, metatileId);
this->tryCommitBorderChanges(commitChanges); this->tryCommitMapChanges(commitChanges);
this->tryRedrawBorder(forceRedraw); this->tryRedrawMapArea(forceRedraw);
} }
int MainWindow::getCollision(int x, int y) { int MainWindow::getCollision(int x, int y) {
@ -280,8 +275,8 @@ void MainWindow::setBorderDimensions(int width, int height) {
if (width < 1 || height < 1 || width > MAX_BORDER_WIDTH || height > MAX_BORDER_HEIGHT) if (width < 1 || height < 1 || width > MAX_BORDER_WIDTH || height > MAX_BORDER_HEIGHT)
return; return;
this->editor->map->setBorderDimensions(width, height); this->editor->map->setBorderDimensions(width, height);
this->tryCommitBorderChanges(true); this->tryCommitMapChanges(true);
// TODO this->onMapNeedsRedrawing();
} }
void MainWindow::setBorderWidth(int width) { void MainWindow::setBorderWidth(int width) {
@ -290,8 +285,8 @@ void MainWindow::setBorderWidth(int width) {
if (width < 1 || width > MAX_BORDER_WIDTH) if (width < 1 || width > MAX_BORDER_WIDTH)
return; return;
this->editor->map->setBorderDimensions(width, this->editor->map->getBorderHeight()); this->editor->map->setBorderDimensions(width, this->editor->map->getBorderHeight());
this->tryCommitBorderChanges(true); this->tryCommitMapChanges(true);
// TODO this->onMapNeedsRedrawing();
} }
void MainWindow::setBorderHeight(int height) { void MainWindow::setBorderHeight(int height) {
@ -300,8 +295,8 @@ void MainWindow::setBorderHeight(int height) {
if (height < 1 || height > MAX_BORDER_HEIGHT) if (height < 1 || height > MAX_BORDER_HEIGHT)
return; return;
this->editor->map->setBorderDimensions(this->editor->map->getBorderWidth(), height); this->editor->map->setBorderDimensions(this->editor->map->getBorderWidth(), height);
this->tryCommitBorderChanges(true); this->tryCommitMapChanges(true);
// TODO this->onMapNeedsRedrawing();
} }
void MainWindow::clearOverlay(int layer) { void MainWindow::clearOverlay(int layer) {

View file

@ -1163,8 +1163,8 @@ Tileset* Project::loadTileset(QString label, Tileset *tileset) {
bool Project::loadBlockdata(MapLayout *layout) { bool Project::loadBlockdata(MapLayout *layout) {
QString path = QString("%1/%2").arg(root).arg(layout->blockdata_path); QString path = QString("%1/%2").arg(root).arg(layout->blockdata_path);
layout->blockdata = readBlockdata(path); layout->blockdata = readBlockdata(path);
layout->lastCommitMapBlocks.blocks = layout->blockdata; layout->lastCommitBlocks.blocks = layout->blockdata;
layout->lastCommitMapBlocks.dimensions = QSize(layout->getWidth(), layout->getHeight()); layout->lastCommitBlocks.mapDimensions = QSize(layout->getWidth(), layout->getHeight());
if (layout->blockdata.count() != layout->getWidth() * layout->getHeight()) { if (layout->blockdata.count() != layout->getWidth() * layout->getHeight()) {
logWarn(QString("Layout blockdata length %1 does not match dimensions %2x%3 (should be %4). Resizing blockdata.") logWarn(QString("Layout blockdata length %1 does not match dimensions %2x%3 (should be %4). Resizing blockdata.")
@ -1182,13 +1182,16 @@ void Project::setNewMapBlockdata(Map *map) {
for (int i = 0; i < map->getWidth() * map->getHeight(); i++) { for (int i = 0; i < map->getWidth() * map->getHeight(); i++) {
map->layout->blockdata.append(qint16(0x3001)); map->layout->blockdata.append(qint16(0x3001));
} }
map->layout->lastCommitMapBlocks.blocks = map->layout->blockdata; map->layout->lastCommitBlocks.blocks = map->layout->blockdata;
map->layout->lastCommitMapBlocks.dimensions = QSize(map->getWidth(), map->getHeight()); map->layout->lastCommitBlocks.mapDimensions = QSize(map->getWidth(), map->getHeight());
} }
bool Project::loadLayoutBorder(MapLayout *layout) { bool Project::loadLayoutBorder(MapLayout *layout) {
QString path = QString("%1/%2").arg(root).arg(layout->border_path); QString path = QString("%1/%2").arg(root).arg(layout->border_path);
layout->border = readBlockdata(path); layout->border = readBlockdata(path);
layout->lastCommitBlocks.border = layout->border;
layout->lastCommitBlocks.borderDimensions = QSize(layout->getBorderWidth(), layout->getBorderHeight());
int borderLength = layout->getBorderWidth() * layout->getBorderHeight(); int borderLength = layout->getBorderWidth() * layout->getBorderHeight();
if (layout->border.count() != borderLength) { if (layout->border.count() != borderLength) {
logWarn(QString("Layout border blockdata length %1 must be %2. Resizing border blockdata.") logWarn(QString("Layout border blockdata length %1 must be %2. Resizing border blockdata.")
@ -1216,6 +1219,8 @@ void Project::setNewMapBorder(Map *map) {
map->layout->border.append(qint16(0x01DC)); map->layout->border.append(qint16(0x01DC));
map->layout->border.append(qint16(0x01DD)); map->layout->border.append(qint16(0x01DD));
} }
map->layout->lastCommitBlocks.border = map->layout->border;
map->layout->lastCommitBlocks.borderDimensions = QSize(map->getBorderWidth(), map->getBorderHeight());
} }
void Project::saveLayoutBorder(Map *map) { void Project::saveLayoutBorder(Map *map) {