fix the scripting api and many other changes

- remove obsolete Map::commit()
- add ScriptEditMap command
- reorganize metatile history
- fix next selected event for multi event deletion
This commit is contained in:
garakmon
2020-08-03 20:49:00 -04:00
committed by garak
parent 6c2d035dfa
commit 392e595a03
18 changed files with 210 additions and 145 deletions

View File

@@ -38,6 +38,8 @@ void PaintMetatile::redo() {
map->layout->blockdata->copyFrom(newMetatiles);
}
map->layout->lastCommitMapBlocks.blocks->copyFrom(map->layout->blockdata);
renderMapBlocks(map);
}
@@ -48,6 +50,8 @@ void PaintMetatile::undo() {
map->layout->blockdata->copyFrom(oldMetatiles);
}
map->layout->lastCommitMapBlocks.blocks->copyFrom(map->layout->blockdata);
renderMapBlocks(map);
QUndoCommand::undo();
@@ -172,6 +176,8 @@ void ShiftMetatiles::redo() {
map->layout->blockdata->copyFrom(newMetatiles);
}
map->layout->lastCommitMapBlocks.blocks->copyFrom(map->layout->blockdata);
renderMapBlocks(map, true);
}
@@ -182,6 +188,8 @@ void ShiftMetatiles::undo() {
map->layout->blockdata->copyFrom(oldMetatiles);
}
map->layout->lastCommitMapBlocks.blocks->copyFrom(map->layout->blockdata);
renderMapBlocks(map, true);
QUndoCommand::undo();
@@ -253,6 +261,8 @@ void ResizeMap::redo() {
map->setBorderDimensions(newBorderWidth, newBorderHeight, false);
}
map->layout->lastCommitMapBlocks.dimensions = QSize(map->getWidth(), map->getHeight());
map->mapNeedsRedrawing();
}
@@ -269,6 +279,8 @@ void ResizeMap::undo() {
map->setBorderDimensions(oldBorderWidth, oldBorderHeight, false);
}
map->layout->lastCommitMapBlocks.dimensions = QSize(map->getWidth(), map->getHeight());
map->mapNeedsRedrawing();
QUndoCommand::undo();
@@ -420,7 +432,8 @@ void EventDelete::redo() {
map->objectsChanged();
editor->selected_events->clear();
editor->selected_events->append(nextSelectedEvent->pixmapItem);
if (nextSelectedEvent)
editor->selected_events->append(nextSelectedEvent->pixmapItem);
editor->updateSelectedEvents();
}
@@ -499,3 +512,65 @@ void EventDuplicate::undo() {
QUndoCommand::undo();
}
/******************************************************************************
************************************************************************
******************************************************************************/
ScriptEditMap::ScriptEditMap(Map *map,
QSize oldMapDimensions, QSize newMapDimensions,
Blockdata *oldMetatiles, Blockdata *newMetatiles,
QUndoCommand *parent) : QUndoCommand(parent) {
setText("Script Edit Map");
this->map = map;
this->newMetatiles = newMetatiles;
this->oldMetatiles = oldMetatiles;
this->oldMapWidth = oldMapWidth;
this->oldMapHeight = oldMapHeight;
this->newMapWidth = newMapWidth;
this->newMapHeight = newMapHeight;
}
ScriptEditMap::~ScriptEditMap() {
if (newMetatiles) delete newMetatiles;
if (oldMetatiles) delete oldMetatiles;
}
void ScriptEditMap::redo() {
QUndoCommand::redo();
if (!map) return;
if (map->layout->blockdata) {
map->layout->blockdata->copyFrom(newMetatiles);
if (newMapWidth != map->getWidth() || newMapHeight != map->getHeight()) {
map->setDimensions(newMapWidth, newMapHeight);
}
}
map->layout->lastCommitMapBlocks.blocks->copyFrom(newMetatiles);
map->layout->lastCommitMapBlocks.dimensions = QSize(newMapWidth, newMapHeight);
renderMapBlocks(map);
}
void ScriptEditMap::undo() {
if (!map) return;
if (map->layout->blockdata) {
map->layout->blockdata->copyFrom(oldMetatiles);
if (oldMapWidth != map->getWidth() || oldMapHeight != map->getHeight()) {
map->setDimensions(oldMapWidth, oldMapHeight);
}
}
map->layout->lastCommitMapBlocks.blocks->copyFrom(oldMetatiles);
map->layout->lastCommitMapBlocks.dimensions = QSize(oldMapWidth, oldMapHeight);
renderMapBlocks(map);
QUndoCommand::undo();
}

View File

@@ -1,4 +0,0 @@
#include "history.h"
#include "historyitem.h"

View File

@@ -1,30 +0,0 @@
#include "historyitem.h"
HistoryItem::HistoryItem(Blockdata *metatiles, Blockdata *border, int layoutWidth, int layoutHeight, int borderWidth, int borderHeight) {
this->metatiles = metatiles;
this->border = border;
this->layoutWidth = layoutWidth;
this->layoutHeight = layoutHeight;
this->borderWidth = borderWidth;
this->borderHeight = borderHeight;
}
HistoryItem::~HistoryItem() {
if (this->metatiles) delete this->metatiles;
if (this->border) delete this->border;
}
RegionMapHistoryItem::RegionMapHistoryItem(int which, QVector<uint8_t> tiles, QString cityMap) {
this->which = which;
this->tiles = tiles;
this->cityMap = cityMap;
}
RegionMapHistoryItem::RegionMapHistoryItem(int which, QVector<uint8_t> tiles, int width, int height) {
this->which = which;
this->tiles = tiles;
this->mapWidth = width;
this->mapHeight = height;
}
RegionMapHistoryItem::~RegionMapHistoryItem() {}

View File

@@ -1,5 +1,4 @@
#include "history.h"
#include "historyitem.h"
#include "map.h"
#include "imageproviders.h"
#include "scripting.h"
@@ -416,15 +415,10 @@ void Map::_floodFillCollisionElevation(int x, int y, uint16_t collision, uint16_
}
}
void Map::commit() {
return;
}
void Map::floodFillCollisionElevation(int x, int y, uint16_t collision, uint16_t elevation) {
Block *block = getBlock(x, y);
if (block && (block->collision != collision || block->elevation != elevation)) {
_floodFillCollisionElevation(x, y, collision, elevation);
commit();
}
}
@@ -444,7 +438,6 @@ void Map::magicFillCollisionElevation(int initialX, int initialY, uint16_t colli
}
}
}
commit();
}
}

View File

@@ -1,4 +1,5 @@
#include "regionmap.h"
#include "regionmapeditor.h"
#include "paletteutil.h"
#include "log.h"
#include "config.h"

View File

@@ -108,6 +108,7 @@ void MainWindow::initExtraShortcuts() {
new QShortcut(QKeySequence("Ctrl+G"), ui->checkBox_ToggleGrid, SLOT(toggle()));
new QShortcut(QKeySequence("Ctrl+D"), this, SLOT(duplicate()));
new QShortcut(QKeySequence::Delete, this, SLOT(on_toolButton_deleteObject_clicked()));
new QShortcut(QKeySequence("Backspace"), this, SLOT(on_toolButton_deleteObject_clicked()));
ui->actionZoom_In->setShortcuts({QKeySequence("Ctrl++"), QKeySequence("Ctrl+=")});
}
@@ -2131,13 +2132,15 @@ void MainWindow::on_toolButton_deleteObject_clicked() {
if (editor->selected_events->length()) {
DraggablePixmapItem *nextSelectedEvent = nullptr;
QList<Event *> selectedEvents;
int numEvents = editor->selected_events->length();
int numDeleted = 0;
for (DraggablePixmapItem *item : *editor->selected_events) {
QString event_group = item->event->get("event_group_type");
if (event_group != "heal_event_group") {
// Get the index for the event that should be selected after this event has been deleted.
// If it's at the end of the list, select the previous event, otherwise select the next one.
// Don't bother getting the event to select if there are still more events to delete
if (editor->selected_events->length() == 1) {
if (++numDeleted == numEvents) {
int index = editor->map->events.value(event_group).indexOf(item->event);
if (index != editor->map->events.value(event_group).size() - 1)
index++;
@@ -2161,7 +2164,7 @@ void MainWindow::on_toolButton_deleteObject_clicked() {
logWarn(QString("Cannot delete event of type '%1'").arg(item->event->get("event_type")));
}
}
editor->map->editHistory.push(new EventDelete(editor, editor->map, selectedEvents, nextSelectedEvent->event));
editor->map->editHistory.push(new EventDelete(editor, editor->map, selectedEvents, nextSelectedEvent ? nextSelectedEvent->event : nullptr));
}
}
}

View File

@@ -1,6 +1,9 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "scripting.h"
#include "editcommands.h"
static Blockdata *oldBlockdata;
QJSValue MainWindow::getBlock(int x, int y) {
if (!this->editor || !this->editor->map)
@@ -21,7 +24,13 @@ void MainWindow::tryRedrawMapArea(bool forceRedraw) {
void MainWindow::tryCommitMapChanges(bool commitChanges) {
if (commitChanges) {
this->editor->map->commit();
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->copy(), map->layout->blockdata->copy()
));
}
}
}
@@ -179,7 +188,7 @@ void MainWindow::setDimensions(int width, int height) {
if (!Project::mapDimensionsValid(width, height))
return;
this->editor->map->setDimensions(width, height);
this->editor->map->commit();
this->tryCommitMapChanges(true);
this->onMapNeedsRedrawing();
}
@@ -189,7 +198,7 @@ void MainWindow::setWidth(int width) {
if (!Project::mapDimensionsValid(width, this->editor->map->getHeight()))
return;
this->editor->map->setDimensions(width, this->editor->map->getHeight());
this->editor->map->commit();
this->tryCommitMapChanges(true);
this->onMapNeedsRedrawing();
}
@@ -199,7 +208,7 @@ void MainWindow::setHeight(int height) {
if (!Project::mapDimensionsValid(this->editor->map->getWidth(), height))
return;
this->editor->map->setDimensions(this->editor->map->getWidth(), height);
this->editor->map->commit();
this->tryCommitMapChanges(true);
this->onMapNeedsRedrawing();
}

View File

@@ -1,7 +1,6 @@
#include "project.h"
#include "config.h"
#include "history.h"
#include "historyitem.h"
#include "log.h"
#include "parseutil.h"
#include "paletteutil.h"
@@ -170,8 +169,6 @@ Map* Project::loadMap(QString map_name) {
if (!(loadMapData(map) && loadMapLayout(map)))
return nullptr;
map->commit();
map->metatileHistory.save();
mapCache->insert(map_name, map);
return map;
}
@@ -1186,6 +1183,12 @@ bool Project::loadBlockdata(Map *map) {
QString path = QString("%1/%2").arg(root).arg(map->layout->blockdata_path);
map->layout->blockdata = readBlockdata(path);
if (map->layout->lastCommitMapBlocks.blocks) {
delete map->layout->lastCommitMapBlocks.blocks;
}
map->layout->lastCommitMapBlocks.blocks = new Blockdata;
map->layout->lastCommitMapBlocks.blocks->copyFrom(map->layout->blockdata);
map->layout->lastCommitMapBlocks.dimensions = QSize(map->getWidth(), map->getHeight());
if (map->layout->blockdata->blocks->count() != map->getWidth() * map->getHeight()) {
logWarn(QString("Layout blockdata length %1 does not match dimensions %2x%3 (should be %4). Resizing blockdata.")
@@ -1204,6 +1207,8 @@ void Project::setNewMapBlockdata(Map *map) {
blockdata->addBlock(qint16(0x3001));
}
map->layout->blockdata = blockdata;
map->layout->lastCommitMapBlocks.blocks->copyFrom(map->layout->blockdata);
map->layout->lastCommitMapBlocks.dimensions = QSize(map->getWidth(), map->getHeight());
}
bool Project::loadMapBorder(Map *map) {
@@ -1251,7 +1256,6 @@ void Project::saveLayoutBorder(Map *map) {
void Project::saveLayoutBlockdata(Map* map) {
QString path = QString("%1/%2").arg(root).arg(map->layout->blockdata_path);
writeBlockdata(path, map->layout->blockdata);
map->metatileHistory.save();
}
void Project::writeBlockdata(QString path, Blockdata *blockdata) {
@@ -1897,8 +1901,6 @@ Map* Project::addNewMapToGroup(QString mapName, int groupNum) {
setNewMapBorder(map);
setNewMapEvents(map);
setNewMapConnections(map);
map->commit();
map->metatileHistory.save();
mapCache->insert(mapName, map);
return map;
@@ -1928,9 +1930,6 @@ Map* Project::addNewMapToGroup(QString mapName, int groupNum, Map *newMap, bool
setNewMapEvents(map);
setNewMapConnections(map);
map->commit();
map->metatileHistory.save();
return map;
}

View File

@@ -54,7 +54,6 @@ void BorderMetatilesPixmapItem::draw() {
}
painter.end();
map->commit();
this->setPixmap(QPixmap::fromImage(image));
emit borderMetatilesChanged();

View File

@@ -52,7 +52,7 @@ void MapPixmapItem::shift(QGraphicsSceneMouseEvent *event) {
}
}
void MapPixmapItem::shift(int xDelta, int yDelta) {
void MapPixmapItem::shift(int xDelta, int yDelta, bool fromScriptCall) {
Blockdata *backupBlockdata = map->layout->blockdata->copy();
for (int i = 0; i < map->getWidth(); i++)
for (int j = 0; j < map->getHeight(); j++) {
@@ -70,9 +70,11 @@ void MapPixmapItem::shift(int xDelta, int yDelta) {
map->setBlock(destX, destY, srcBlock);
}
Blockdata *newMetatiles = map->layout->blockdata->copy();
ShiftMetatiles *paintEvent = new ShiftMetatiles(map, backupBlockdata, newMetatiles, actionId_);
map->editHistory.push(paintEvent);
if (!fromScriptCall) {
map->editHistory.push(new ShiftMetatiles(map, backupBlockdata, map->layout->blockdata->copy(), actionId_));
} else {
delete backupBlockdata;
}
}
void MapPixmapItem::paintNormal(int x, int y, bool fromScriptCall) {
@@ -93,7 +95,8 @@ void MapPixmapItem::paintNormal(int x, int y, bool fromScriptCall) {
y = initialY + (yDiff / selectionDimensions.y()) * selectionDimensions.y();
// for edit history
Blockdata *oldMetatiles = map->layout->blockdata->copy();
Blockdata *oldMetatiles;
if (!fromScriptCall) oldMetatiles = map->layout->blockdata->copy();
for (int i = 0; i < selectionDimensions.x() && i + x < map->getWidth(); i++)
for (int j = 0; j < selectionDimensions.y() && j + y < map->getHeight(); j++) {
@@ -111,9 +114,9 @@ void MapPixmapItem::paintNormal(int x, int y, bool fromScriptCall) {
}
}
Blockdata *newMetatiles = map->layout->blockdata->copy();
PaintMetatile *paintEvent = new PaintMetatile(map, oldMetatiles, newMetatiles, actionId_);
map->editHistory.push(paintEvent);
if (!fromScriptCall) {
map->editHistory.push(new PaintMetatile(map, oldMetatiles, map->layout->blockdata->copy(), actionId_));
}
}
// These are tile offsets from the top-left tile in the 3x3 smart path selection.
@@ -339,7 +342,8 @@ void MapPixmapItem::magicFill(
return;
}
Blockdata *oldMetatiles = map->layout->blockdata->copy();
Blockdata *oldMetatiles;
if (!fromScriptCall) oldMetatiles = map->layout->blockdata->copy();
bool setCollisions = selectedCollisions && selectedCollisions->length() == selectedMetatiles->length();
uint16_t tile = block->tile;
@@ -364,9 +368,9 @@ void MapPixmapItem::magicFill(
}
}
Blockdata *newMetatiles = map->layout->blockdata->copy();
MagicFillMetatile *paintEvent = new MagicFillMetatile(map, oldMetatiles, newMetatiles, actionId_);
map->editHistory.push(paintEvent);
if (!fromScriptCall) {
map->editHistory.push(new MagicFillMetatile(map, oldMetatiles, map->layout->blockdata->copy(), actionId_));
}
}
}
@@ -397,7 +401,8 @@ void MapPixmapItem::floodFill(
for (int i = 0; i < numMetatiles; i++)
visited[i] = false;
Blockdata *oldMetatiles = map->layout->blockdata->copy();
Blockdata *oldMetatiles;
if (!fromScriptCall) oldMetatiles = map->layout->blockdata->copy();
QList<QPoint> todo;
todo.append(QPoint(initialX, initialY));
@@ -447,9 +452,9 @@ void MapPixmapItem::floodFill(
}
}
Blockdata *newMetatiles = map->layout->blockdata->copy();
BucketFillMetatile *paintEvent = new BucketFillMetatile(map, oldMetatiles, newMetatiles, actionId_);
map->editHistory.push(paintEvent);
if (!fromScriptCall) {
map->editHistory.push(new BucketFillMetatile(map, oldMetatiles, map->layout->blockdata->copy(), actionId_));
}
delete[] visited;
}