From 6c2d035dfab29ff2bae260efc583de6938235686 Mon Sep 17 00:00:00 2001 From: garakmon Date: Fri, 31 Jul 2020 20:30:35 -0400 Subject: [PATCH] more undo commands - add edit command for duplicating map events - add edit commands for painting map collision - edit commands that delete events now select the proper next event --- include/core/editcommands.h | 69 +++++++++++++++++++++++++++----- include/core/map.h | 4 ++ include/ui/collisionpixmapitem.h | 4 ++ include/ui/mappixmapitem.h | 2 +- src/core/editcommands.cpp | 36 +++++++++-------- src/ui/collisionpixmapitem.cpp | 34 +++++++++++----- src/ui/mappixmapitem.cpp | 20 ++++----- 7 files changed, 122 insertions(+), 47 deletions(-) diff --git a/include/core/editcommands.h b/include/core/editcommands.h index cb79ff2b..ebd63811 100644 --- a/include/core/editcommands.h +++ b/include/core/editcommands.h @@ -25,7 +25,7 @@ enum CommandId { ID_EventShift, // - done ID_EventCreate, // - done ID_EventDelete, // - done - ID_EventDuplicate, // - + ID_EventDuplicate, // - done ID_EventSetData, // - ? // Region map editor history commands }; @@ -38,8 +38,8 @@ class PaintMetatile : public QUndoCommand { public: PaintMetatile(Map *map, Blockdata *oldMetatiles, Blockdata *newMetatiles, - unsigned eventId, QUndoCommand *parent = nullptr); - ~PaintMetatile(); + unsigned actionId, QUndoCommand *parent = nullptr); + virtual ~PaintMetatile(); void undo() override; void redo() override; @@ -53,7 +53,23 @@ private: Blockdata *newMetatiles; Blockdata *oldMetatiles; - unsigned eventId; + unsigned actionId; +}; + + + +/// Implements a command to commit paint actions +/// on the metatile collision and elevation. +class PaintCollision : public PaintMetatile { +public: + PaintCollision(Map *map, + Blockdata *oldCollision, Blockdata *newCollision, + unsigned actionId, QUndoCommand *parent = nullptr) + : PaintMetatile(map, oldCollision, newCollision, actionId, parent) { + setText("Paint Collision"); + } + + int id() const override { return CommandId::ID_PaintCollision; } }; @@ -63,7 +79,7 @@ class PaintBorder : public QUndoCommand { public: PaintBorder(Map *map, Blockdata *oldBorder, Blockdata *newBorder, - unsigned eventId, QUndoCommand *parent = nullptr); + unsigned actionId, QUndoCommand *parent = nullptr); ~PaintBorder(); void undo() override; @@ -78,7 +94,7 @@ private: Blockdata *newBorder; Blockdata *oldBorder; - unsigned eventId; + unsigned actionId; }; @@ -89,7 +105,7 @@ class BucketFillMetatile : public PaintMetatile { public: BucketFillMetatile(Map *map, Blockdata *oldMetatiles, Blockdata *newMetatiles, - unsigned eventId, QUndoCommand *parent = nullptr); + unsigned actionId, QUndoCommand *parent = nullptr); ~BucketFillMetatile(); bool mergeWith(const QUndoCommand *command) override { return false; } @@ -98,13 +114,30 @@ public: +/// Implements a command to commit flood fill actions +/// on the metatile collision and elevation. +class BucketFillCollision : public PaintCollision { +public: + BucketFillCollision(Map *map, + Blockdata *oldCollision, Blockdata *newCollision, + QUndoCommand *parent = nullptr) + : PaintCollision(map, oldCollision, newCollision, -1, parent) { + setText("Flood Fill Collision"); + } + + bool mergeWith(const QUndoCommand *command) override { return false; } + int id() const override { return CommandId::ID_BucketFillCollision; } +}; + + + /// Implements a command to commit magic fill metatile actions /// with the bucket or paint tool onto the map. class MagicFillMetatile : public PaintMetatile { public: MagicFillMetatile(Map *map, Blockdata *oldMetatiles, Blockdata *newMetatiles, - unsigned eventId, QUndoCommand *parent = nullptr); + unsigned actionId, QUndoCommand *parent = nullptr); ~MagicFillMetatile(); bool mergeWith(const QUndoCommand *command) override { return false; } @@ -113,13 +146,28 @@ public: +/// Implements a command to commit magic fill collision actions. +class MagicFillCollision : public PaintCollision { +public: + MagicFillCollision(Map *map, + Blockdata *oldCollision, Blockdata *newCollision, + QUndoCommand *parent = nullptr) + : PaintCollision(map, oldCollision, newCollision, -1, parent) { + setText("Magic Fill Collision"); + } + + bool mergeWith(const QUndoCommand *command) override { return false; } + int id() const override { return CommandId::ID_MagicFillCollision; } +}; + + /// Implements a command to commit metatile shift actions. class ShiftMetatiles : public QUndoCommand { public: ShiftMetatiles(Map *map, Blockdata *oldMetatiles, Blockdata *newMetatiles, - unsigned eventId, QUndoCommand *parent = nullptr); + unsigned actionId, QUndoCommand *parent = nullptr); ~ShiftMetatiles(); void undo() override; @@ -134,8 +182,7 @@ private: Blockdata *newMetatiles; Blockdata *oldMetatiles; - unsigned eventId; - bool mergeable = false; + unsigned actionId; }; diff --git a/include/core/map.h b/include/core/map.h index 10987d96..9542d37d 100644 --- a/include/core/map.h +++ b/include/core/map.h @@ -23,6 +23,7 @@ #define BORDER_DISTANCE 7 class MapPixmapItem; +class CollisionPixmapItem; class BorderMetatilesPixmapItem; class Map : public QObject @@ -97,6 +98,9 @@ public: MapPixmapItem *mapItem = nullptr; void setMapItem(MapPixmapItem *item) { mapItem = item; } + CollisionPixmapItem *collisionItem = nullptr; + void setCollisionItem(CollisionPixmapItem *item) { collisionItem = item; } + BorderMetatilesPixmapItem *borderItem = nullptr; void setBorderItem(BorderMetatilesPixmapItem *item) { borderItem = item; } diff --git a/include/ui/collisionpixmapitem.h b/include/ui/collisionpixmapitem.h index 63af0f6c..bd208c1b 100644 --- a/include/ui/collisionpixmapitem.h +++ b/include/ui/collisionpixmapitem.h @@ -14,6 +14,7 @@ public: : MapPixmapItem(map, metatileSelector, settings){ this->movementPermissionsSelector = movementPermissionsSelector; this->opacity = opacity; + map->setCollisionItem(this); } MovementPermissionsSelector *movementPermissionsSelector; qreal *opacity; @@ -24,6 +25,9 @@ public: virtual void pick(QGraphicsSceneMouseEvent*); void draw(bool ignoreCache = false); +private: + unsigned actionId_ = 0; + signals: void mouseEvent(QGraphicsSceneMouseEvent *, CollisionPixmapItem *); void hoveredMapMovementPermissionChanged(int, int); diff --git a/include/ui/mappixmapitem.h b/include/ui/mappixmapitem.h index fc9bfd2b..6c732e96 100644 --- a/include/ui/mappixmapitem.h +++ b/include/ui/mappixmapitem.h @@ -69,7 +69,7 @@ private: void paintSmartPath(int x, int y, bool fromScriptCall = false); static QList smartPathTable; - unsigned eventId_ = 0; + unsigned actionId_ = 0; signals: void startPaint(QGraphicsSceneMouseEvent *, MapPixmapItem *); diff --git a/src/core/editcommands.cpp b/src/core/editcommands.cpp index 25c4befe..9daaf244 100644 --- a/src/core/editcommands.cpp +++ b/src/core/editcommands.cpp @@ -8,16 +8,20 @@ +void renderMapBlocks(Map *map, bool ignoreCache = false) { + map->mapItem->draw(ignoreCache); + map->collisionItem->draw(ignoreCache); +} PaintMetatile::PaintMetatile(Map *map, Blockdata *oldMetatiles, Blockdata *newMetatiles, - unsigned eventId, QUndoCommand *parent) : QUndoCommand(parent) { + unsigned actionId, QUndoCommand *parent) : QUndoCommand(parent) { setText("Paint Metatiles"); this->map = map; this->oldMetatiles = oldMetatiles; this->newMetatiles = newMetatiles; - this->eventId = eventId; + this->actionId = actionId; } PaintMetatile::~PaintMetatile() { @@ -34,7 +38,7 @@ void PaintMetatile::redo() { map->layout->blockdata->copyFrom(newMetatiles); } - map->mapItem->draw(); + renderMapBlocks(map); } void PaintMetatile::undo() { @@ -44,7 +48,7 @@ void PaintMetatile::undo() { map->layout->blockdata->copyFrom(oldMetatiles); } - map->mapItem->draw(); + renderMapBlocks(map); QUndoCommand::undo(); } @@ -55,7 +59,7 @@ bool PaintMetatile::mergeWith(const QUndoCommand *command) { if (this->map != other->map) return false; - if (eventId != other->eventId) + if (actionId != other->actionId) return false; this->newMetatiles->copyFrom(other->newMetatiles); @@ -69,14 +73,14 @@ bool PaintMetatile::mergeWith(const QUndoCommand *command) { PaintBorder::PaintBorder(Map *map, Blockdata *oldBorder, Blockdata *newBorder, - unsigned eventId, QUndoCommand *parent) : QUndoCommand(parent) { + unsigned actionId, QUndoCommand *parent) : QUndoCommand(parent) { setText("Paint Border"); this->map = map; this->oldBorder = oldBorder; this->newBorder = newBorder; - this->eventId = eventId; + this->actionId = actionId; } PaintBorder::~PaintBorder() { @@ -114,8 +118,8 @@ void PaintBorder::undo() { BucketFillMetatile::BucketFillMetatile(Map *map, Blockdata *oldMetatiles, Blockdata *newMetatiles, - unsigned eventId, QUndoCommand *parent) - : PaintMetatile(map, oldMetatiles, newMetatiles, eventId, parent) { + unsigned actionId, QUndoCommand *parent) + : PaintMetatile(map, oldMetatiles, newMetatiles, actionId, parent) { setText("Bucket Fill Metatiles"); } @@ -129,8 +133,8 @@ BucketFillMetatile::~BucketFillMetatile() { MagicFillMetatile::MagicFillMetatile(Map *map, Blockdata *oldMetatiles, Blockdata *newMetatiles, - unsigned eventId, QUndoCommand *parent) - : PaintMetatile(map, oldMetatiles, newMetatiles, eventId, parent) { + unsigned actionId, QUndoCommand *parent) + : PaintMetatile(map, oldMetatiles, newMetatiles, actionId, parent) { setText("Magic Fill Metatiles"); } @@ -144,14 +148,14 @@ MagicFillMetatile::~MagicFillMetatile() { ShiftMetatiles::ShiftMetatiles(Map *map, Blockdata *oldMetatiles, Blockdata *newMetatiles, - unsigned eventId, QUndoCommand *parent) : QUndoCommand(parent) { + unsigned actionId, QUndoCommand *parent) : QUndoCommand(parent) { setText("Shift Metatiles"); this->map = map; this->oldMetatiles = oldMetatiles; this->newMetatiles = newMetatiles; - this->eventId = eventId; + this->actionId = actionId; } ShiftMetatiles::~ShiftMetatiles() { @@ -168,7 +172,7 @@ void ShiftMetatiles::redo() { map->layout->blockdata->copyFrom(newMetatiles); } - map->mapItem->draw(true); + renderMapBlocks(map, true); } void ShiftMetatiles::undo() { @@ -178,7 +182,7 @@ void ShiftMetatiles::undo() { map->layout->blockdata->copyFrom(oldMetatiles); } - map->mapItem->draw(true); + renderMapBlocks(map, true); QUndoCommand::undo(); } @@ -189,7 +193,7 @@ bool ShiftMetatiles::mergeWith(const QUndoCommand *command) { if (this->map != other->map) return false; - if (eventId != other->eventId) + if (actionId != other->actionId) return false; this->newMetatiles->copyFrom(other->newMetatiles); diff --git a/src/ui/collisionpixmapitem.cpp b/src/ui/collisionpixmapitem.cpp index 4b4bd376..d0652d33 100644 --- a/src/ui/collisionpixmapitem.cpp +++ b/src/ui/collisionpixmapitem.cpp @@ -1,4 +1,5 @@ #include "collisionpixmapitem.h" +#include "editcommands.h" void CollisionPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { int x = static_cast(event->pos().x()) / 16; @@ -26,12 +27,17 @@ void CollisionPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { void CollisionPixmapItem::draw(bool ignoreCache) { if (map) { + map->setCollisionItem(this); setPixmap(map->renderCollision(*this->opacity, ignoreCache)); } } void CollisionPixmapItem::paint(QGraphicsSceneMouseEvent *event) { - if (map) { + if (event->type() == QEvent::GraphicsSceneMouseRelease) { + actionId_++; + } else if (map) { + Blockdata *oldCollision = map->layout->blockdata->copy(); + QPointF pos = event->pos(); int x = static_cast(pos.x()) / 16; int y = static_cast(pos.y()) / 16; @@ -41,34 +47,44 @@ void CollisionPixmapItem::paint(QGraphicsSceneMouseEvent *event) { block->elevation = this->movementPermissionsSelector->getSelectedElevation(); map->setBlock(x, y, *block, true); } - if (event->type() == QEvent::GraphicsSceneMouseRelease) { - map->commit(); - } - draw(); + + Blockdata *newCollision = map->layout->blockdata->copy(); + map->editHistory.push(new PaintCollision(map, oldCollision, newCollision, actionId_)); } } void CollisionPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) { - if (map) { + if (event->type() != QEvent::GraphicsSceneMouseRelease) { + // do nothing + } else if (map) { + Blockdata *oldCollision = map->layout->blockdata->copy(); + QPointF pos = event->pos(); int x = static_cast(pos.x()) / 16; int y = static_cast(pos.y()) / 16; uint16_t collision = this->movementPermissionsSelector->getSelectedCollision(); uint16_t elevation = this->movementPermissionsSelector->getSelectedElevation(); map->floodFillCollisionElevation(x, y, collision, elevation); - draw(); + + Blockdata *newCollision = map->layout->blockdata->copy(); + map->editHistory.push(new BucketFillCollision(map, oldCollision, newCollision)); } } void CollisionPixmapItem::magicFill(QGraphicsSceneMouseEvent *event) { - if (map) { + if (event->type() != QEvent::GraphicsSceneMouseRelease) { + // do nothing + } else if (map) { + Blockdata *oldCollision = map->layout->blockdata->copy(); QPointF pos = event->pos(); int x = static_cast(pos.x()) / 16; int y = static_cast(pos.y()) / 16; uint16_t collision = this->movementPermissionsSelector->getSelectedCollision(); uint16_t elevation = this->movementPermissionsSelector->getSelectedElevation(); map->magicFillCollisionElevation(x, y, collision, elevation); - draw(); + + Blockdata *newCollision = map->layout->blockdata->copy(); + map->editHistory.push(new MagicFillCollision(map, oldCollision, newCollision)); } } diff --git a/src/ui/mappixmapitem.cpp b/src/ui/mappixmapitem.cpp index d6914286..7f9b7eb7 100644 --- a/src/ui/mappixmapitem.cpp +++ b/src/ui/mappixmapitem.cpp @@ -8,7 +8,7 @@ void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) { if (map) { if (event->type() == QEvent::GraphicsSceneMouseRelease) { - eventId_++; + actionId_++; } else { QPointF pos = event->pos(); int x = static_cast(pos.x()) / 16; @@ -29,7 +29,7 @@ void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) { void MapPixmapItem::shift(QGraphicsSceneMouseEvent *event) { if (map) { if (event->type() == QEvent::GraphicsSceneMouseRelease) { - eventId_++; + actionId_++; } else { QPointF pos = event->pos(); int x = static_cast(pos.x()) / 16; @@ -71,7 +71,7 @@ void MapPixmapItem::shift(int xDelta, int yDelta) { } Blockdata *newMetatiles = map->layout->blockdata->copy(); - ShiftMetatiles *paintEvent = new ShiftMetatiles(map, backupBlockdata, newMetatiles, eventId_); + ShiftMetatiles *paintEvent = new ShiftMetatiles(map, backupBlockdata, newMetatiles, actionId_); map->editHistory.push(paintEvent); } @@ -112,7 +112,7 @@ void MapPixmapItem::paintNormal(int x, int y, bool fromScriptCall) { } Blockdata *newMetatiles = map->layout->blockdata->copy(); - PaintMetatile *paintEvent = new PaintMetatile(map, oldMetatiles, newMetatiles, eventId_); + PaintMetatile *paintEvent = new PaintMetatile(map, oldMetatiles, newMetatiles, actionId_); map->editHistory.push(paintEvent); } @@ -225,7 +225,7 @@ void MapPixmapItem::paintSmartPath(int x, int y, bool fromScriptCall) { } Blockdata *newMetatiles = map->layout->blockdata->copy(); - PaintMetatile *paintEvent = new PaintMetatile(map, oldMetatiles, newMetatiles, eventId_); + PaintMetatile *paintEvent = new PaintMetatile(map, oldMetatiles, newMetatiles, actionId_); map->editHistory.push(paintEvent); } @@ -279,7 +279,7 @@ void MapPixmapItem::updateMetatileSelection(QGraphicsSceneMouseEvent *event) { void MapPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) { if (map) { if (event->type() == QEvent::GraphicsSceneMouseRelease) { - eventId_++; + actionId_++; } else { QPointF pos = event->pos(); int x = static_cast(pos.x()) / 16; @@ -302,7 +302,7 @@ void MapPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) { void MapPixmapItem::magicFill(QGraphicsSceneMouseEvent *event) { if (map) { if (event->type() == QEvent::GraphicsSceneMouseRelease) { - eventId_++; + actionId_++; } else { QPointF pos = event->pos(); int initialX = static_cast(pos.x()) / 16; @@ -365,7 +365,7 @@ void MapPixmapItem::magicFill( } Blockdata *newMetatiles = map->layout->blockdata->copy(); - MagicFillMetatile *paintEvent = new MagicFillMetatile(map, oldMetatiles, newMetatiles, eventId_); + MagicFillMetatile *paintEvent = new MagicFillMetatile(map, oldMetatiles, newMetatiles, actionId_); map->editHistory.push(paintEvent); } } @@ -448,7 +448,7 @@ void MapPixmapItem::floodFill( } Blockdata *newMetatiles = map->layout->blockdata->copy(); - BucketFillMetatile *paintEvent = new BucketFillMetatile(map, oldMetatiles, newMetatiles, eventId_); + BucketFillMetatile *paintEvent = new BucketFillMetatile(map, oldMetatiles, newMetatiles, actionId_); map->editHistory.push(paintEvent); delete[] visited; @@ -575,7 +575,7 @@ void MapPixmapItem::floodFillSmartPath(int initialX, int initialY, bool fromScri } Blockdata *newMetatiles = map->layout->blockdata->copy(); - BucketFillMetatile *paintEvent = new BucketFillMetatile(map, oldMetatiles, newMetatiles, eventId_); + BucketFillMetatile *paintEvent = new BucketFillMetatile(map, oldMetatiles, newMetatiles, actionId_); map->editHistory.push(paintEvent); delete[] visited;