diff --git a/include/core/editcommands.h b/include/core/editcommands.h index 142636cc..ea66b722 100644 --- a/include/core/editcommands.h +++ b/include/core/editcommands.h @@ -343,12 +343,14 @@ public: /// 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 { public: ScriptEditMap(Map *map, QSize oldMapDimensions, QSize newMapDimensions, const Blockdata &oldMetatiles, const Blockdata &newMetatiles, + QSize oldBorderDimensions, QSize newBorderDimensions, + const Blockdata &oldBorder, const Blockdata &newBorder, QUndoCommand *parent = nullptr); void undo() override; @@ -363,10 +365,18 @@ private: Blockdata newMetatiles; Blockdata oldMetatiles; + Blockdata newBorder; + Blockdata oldBorder; + int oldMapWidth; int oldMapHeight; int newMapWidth; int newMapHeight; + + int oldBorderWidth; + int oldBorderHeight; + int newBorderWidth; + int newBorderHeight; }; #endif // EDITCOMMANDS_H diff --git a/include/core/maplayout.h b/include/core/maplayout.h index 630bdeae..888e5372 100644 --- a/include/core/maplayout.h +++ b/include/core/maplayout.h @@ -33,8 +33,10 @@ public: Blockdata cached_border; struct { Blockdata blocks; - QSize dimensions; - } lastCommitMapBlocks; // to track map changes + QSize mapDimensions; + Blockdata border; + QSize borderDimensions; + } lastCommitBlocks; // to track map changes int getWidth(); int getHeight(); diff --git a/include/editor.h b/include/editor.h index 11efabda..92368b0b 100644 --- a/include/editor.h +++ b/include/editor.h @@ -157,6 +157,7 @@ public slots: void openScript(const QString &scriptLabel) const; void openProjectInTextEditor() const; void maskNonVisibleConnectionTiles(); + void onBorderMetatilesChanged(); private: void setConnectionItemsVisible(bool); @@ -192,7 +193,6 @@ private slots: void onConnectionItemSelected(ConnectionPixmapItem* connectionItem); void onConnectionItemDoubleClicked(ConnectionPixmapItem* connectionItem); void onConnectionDirectionChanged(QString newDirection); - void onBorderMetatilesChanged(); void onHoveredMovementPermissionChanged(uint16_t, uint16_t); void onHoveredMovementPermissionCleared(); void onHoveredMetatileSelectionChanged(uint16_t); diff --git a/include/mainwindow.h b/include/mainwindow.h index c68473b4..d64e0d65 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -46,8 +46,6 @@ public: Q_INVOKABLE QJSValue getBlock(int x, int y); void tryRedrawMapArea(bool forceRedraw); 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 setBlocksFromSelection(int x, int y, bool forceRedraw = true, bool commitChanges = true); Q_INVOKABLE int getMetatileId(int x, int y); diff --git a/src/core/editcommands.cpp b/src/core/editcommands.cpp index 62885aaa..24e7b86c 100644 --- a/src/core/editcommands.cpp +++ b/src/core/editcommands.cpp @@ -49,7 +49,7 @@ void PaintMetatile::redo() { map->setBlockdata(newMetatiles); - map->layout->lastCommitMapBlocks.blocks = map->layout->blockdata; + map->layout->lastCommitBlocks.blocks = map->layout->blockdata; renderMapBlocks(map); } @@ -59,7 +59,7 @@ void PaintMetatile::undo() { map->setBlockdata(oldMetatiles); - map->layout->lastCommitMapBlocks.blocks = map->layout->blockdata; + map->layout->lastCommitBlocks.blocks = map->layout->blockdata; renderMapBlocks(map); @@ -103,6 +103,8 @@ void PaintBorder::redo() { map->setBorderBlockData(newBorder); + map->layout->lastCommitBlocks.border = map->layout->border; + map->borderItem->draw(); } @@ -111,6 +113,8 @@ void PaintBorder::undo() { map->setBorderBlockData(oldBorder); + map->layout->lastCommitBlocks.border = map->layout->border; + map->borderItem->draw(); QUndoCommand::undo(); @@ -139,7 +143,7 @@ void ShiftMetatiles::redo() { map->setBlockdata(newMetatiles); - map->layout->lastCommitMapBlocks.blocks = map->layout->blockdata; + map->layout->lastCommitBlocks.blocks = map->layout->blockdata; renderMapBlocks(map, true); } @@ -149,7 +153,7 @@ void ShiftMetatiles::undo() { map->setBlockdata(oldMetatiles); - map->layout->lastCommitMapBlocks.blocks = map->layout->blockdata; + map->layout->lastCommitBlocks.blocks = map->layout->blockdata; renderMapBlocks(map, true); @@ -213,7 +217,8 @@ void ResizeMap::redo() { map->layout->border = newBorder; 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(); } @@ -227,7 +232,8 @@ void ResizeMap::undo() { map->layout->border = oldBorder; 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(); @@ -484,6 +490,8 @@ int EventPaste::id() const { ScriptEditMap::ScriptEditMap(Map *map, QSize oldMapDimensions, QSize newMapDimensions, const Blockdata &oldMetatiles, const Blockdata &newMetatiles, + QSize oldBorderDimensions, QSize newBorderDimensions, + const Blockdata &oldBorder, const Blockdata &newBorder, QUndoCommand *parent) : QUndoCommand(parent) { setText("Script Edit Map"); @@ -496,6 +504,14 @@ ScriptEditMap::ScriptEditMap(Map *map, this->oldMapHeight = oldMapDimensions.height(); this->newMapWidth = newMapDimensions.width(); 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() { @@ -510,10 +526,19 @@ void ScriptEditMap::redo() { map->setBlockdata(newMetatiles); } - map->layout->lastCommitMapBlocks.blocks = newMetatiles; - map->layout->lastCommitMapBlocks.dimensions = QSize(newMapWidth, newMapHeight); + if (newBorderWidth != map->getBorderWidth() || newBorderHeight != map->getBorderHeight()) { + 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() { @@ -526,10 +551,19 @@ void ScriptEditMap::undo() { map->setBlockdata(oldMetatiles); } - map->layout->lastCommitMapBlocks.blocks = oldMetatiles; - map->layout->lastCommitMapBlocks.dimensions = QSize(oldMapWidth, oldMapHeight); + if (oldBorderWidth != map->getBorderWidth() || oldBorderHeight != map->getBorderHeight()) { + 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(); } diff --git a/src/mainwindow_scriptapi.cpp b/src/mainwindow_scriptapi.cpp index 8ec8d688..6e0c2487 100644 --- a/src/mainwindow_scriptapi.cpp +++ b/src/mainwindow_scriptapi.cpp @@ -29,12 +29,15 @@ void MainWindow::tryRedrawMapArea(bool forceRedraw) { if (this->needsFullRedraw) { this->editor->map_item->draw(true); this->editor->collision_item->draw(true); + this->editor->selected_border_metatiles_item->draw(); this->editor->updateMapBorder(); this->editor->updateMapConnections(); this->needsFullRedraw = false; } else { this->editor->map_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; if (map) { map->editHistory.push(new ScriptEditMap(map, - map->layout->lastCommitMapBlocks.dimensions, QSize(map->getWidth(), map->getHeight()), - map->layout->lastCommitMapBlocks.blocks, map->layout->blockdata + map->layout->lastCommitBlocks.mapDimensions, QSize(map->getWidth(), map->getHeight()), + 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) { if (!this->editor || !this->editor->map) return; @@ -112,8 +107,8 @@ void MainWindow::setBorderMetatileId(int x, int y, int metatileId, bool forceRed if (!this->editor->map->isWithinBorderBounds(x, y)) return; this->editor->map->setBorderMetatileId(x, y, metatileId); - this->tryCommitBorderChanges(commitChanges); - this->tryRedrawBorder(forceRedraw); + this->tryCommitMapChanges(commitChanges); + this->tryRedrawMapArea(forceRedraw); } 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) return; this->editor->map->setBorderDimensions(width, height); - this->tryCommitBorderChanges(true); - // TODO + this->tryCommitMapChanges(true); + this->onMapNeedsRedrawing(); } void MainWindow::setBorderWidth(int width) { @@ -290,8 +285,8 @@ void MainWindow::setBorderWidth(int width) { if (width < 1 || width > MAX_BORDER_WIDTH) return; this->editor->map->setBorderDimensions(width, this->editor->map->getBorderHeight()); - this->tryCommitBorderChanges(true); - // TODO + this->tryCommitMapChanges(true); + this->onMapNeedsRedrawing(); } void MainWindow::setBorderHeight(int height) { @@ -300,8 +295,8 @@ void MainWindow::setBorderHeight(int height) { if (height < 1 || height > MAX_BORDER_HEIGHT) return; this->editor->map->setBorderDimensions(this->editor->map->getBorderWidth(), height); - this->tryCommitBorderChanges(true); - // TODO + this->tryCommitMapChanges(true); + this->onMapNeedsRedrawing(); } void MainWindow::clearOverlay(int layer) { diff --git a/src/project.cpp b/src/project.cpp index beda1e79..5d750ae2 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -1163,8 +1163,8 @@ Tileset* Project::loadTileset(QString label, Tileset *tileset) { bool Project::loadBlockdata(MapLayout *layout) { QString path = QString("%1/%2").arg(root).arg(layout->blockdata_path); layout->blockdata = readBlockdata(path); - layout->lastCommitMapBlocks.blocks = layout->blockdata; - layout->lastCommitMapBlocks.dimensions = QSize(layout->getWidth(), layout->getHeight()); + layout->lastCommitBlocks.blocks = layout->blockdata; + layout->lastCommitBlocks.mapDimensions = QSize(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.") @@ -1182,13 +1182,16 @@ void Project::setNewMapBlockdata(Map *map) { for (int i = 0; i < map->getWidth() * map->getHeight(); i++) { map->layout->blockdata.append(qint16(0x3001)); } - map->layout->lastCommitMapBlocks.blocks = map->layout->blockdata; - map->layout->lastCommitMapBlocks.dimensions = QSize(map->getWidth(), map->getHeight()); + map->layout->lastCommitBlocks.blocks = map->layout->blockdata; + map->layout->lastCommitBlocks.mapDimensions = QSize(map->getWidth(), map->getHeight()); } bool Project::loadLayoutBorder(MapLayout *layout) { QString path = QString("%1/%2").arg(root).arg(layout->border_path); layout->border = readBlockdata(path); + layout->lastCommitBlocks.border = layout->border; + layout->lastCommitBlocks.borderDimensions = QSize(layout->getBorderWidth(), layout->getBorderHeight()); + int borderLength = layout->getBorderWidth() * layout->getBorderHeight(); if (layout->border.count() != borderLength) { 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(0x01DD)); } + map->layout->lastCommitBlocks.border = map->layout->border; + map->layout->lastCommitBlocks.borderDimensions = QSize(map->getBorderWidth(), map->getBorderHeight()); } void Project::saveLayoutBorder(Map *map) {