From 2598ca2277dfa90c01700f32945ac19381bba1a5 Mon Sep 17 00:00:00 2001 From: garakmon Date: Thu, 30 Jul 2020 20:54:44 -0400 Subject: [PATCH] edit commands for creating and deleting events --- include/core/editcommands.h | 62 +++++++++++++++++++++++-- include/core/event.h | 7 +++ include/core/map.h | 1 + src/core/editcommands.cpp | 92 +++++++++++++++++++++++++++++++++++++ src/core/event.cpp | 5 +- src/editor.cpp | 17 +++++-- src/mainwindow.cpp | 7 +-- 7 files changed, 177 insertions(+), 14 deletions(-) diff --git a/include/core/editcommands.h b/include/core/editcommands.h index 207da57d..a83d6ac5 100644 --- a/include/core/editcommands.h +++ b/include/core/editcommands.h @@ -9,6 +9,7 @@ class Map; class Blockdata; class Event; class DraggablePixmapItem; +class Editor; enum CommandId { ID_PaintMetatile, // - done @@ -18,9 +19,9 @@ enum CommandId { ID_ResizeMap, // - done ID_PaintBorder, // - done ID_EventMove, // - done - ID_EventShift, // - - ID_EventCreate, // - - ID_EventDelete, // - + ID_EventShift, // - done + ID_EventCreate, // - done + ID_EventDelete, // - done ID_EventSetData, // - ? // Tileset editor history commands // Region map editor history commands @@ -193,4 +194,59 @@ private: unsigned actionId; }; + + +/// +class EventShift : public EventMove { +public: + EventShift(QList events, + int deltaX, int deltaY, unsigned actionId, + QUndoCommand *parent = nullptr); + ~EventShift(); + + int id() const override { return CommandId::ID_EventShift; } +}; + + + +/// +class EventCreate : public QUndoCommand { +public: + EventCreate(Editor *editor, Map *map, Event *event, + QUndoCommand *parent = nullptr); + ~EventCreate(); + + void undo() override; + void redo() override; + + bool mergeWith(const QUndoCommand *command) override { return false; } + int id() const override { return CommandId::ID_EventCreate; } + +private: + Map *map; + Event *event; + Editor *editor; +}; + + + +/// +class EventDelete : public QUndoCommand { +public: + EventDelete(Editor *editor, Map *map, Event *event, + QUndoCommand *parent = nullptr); + ~EventDelete(); + + void undo() override; + void redo() override; + + bool mergeWith(const QUndoCommand *command) override { return false; } + int id() const override { return CommandId::ID_EventDelete; } + +private: + Map *map; + Event *event; + Editor *editor; +}; + #endif // EDITCOMMANDS_H diff --git a/include/core/event.h b/include/core/event.h index 48d13253..f4c3466f 100644 --- a/include/core/event.h +++ b/include/core/event.h @@ -99,8 +99,15 @@ public: bool hFlip = false; bool usingSprite; + void activate() { active = true; } + void deactivate() { active = false; } + bool isActive() { return active; } + DraggablePixmapItem *pixmapItem = nullptr; void setPixmapItem(DraggablePixmapItem *item) { pixmapItem = item; } + +private: + bool active = true; }; #endif // EVENT_H diff --git a/include/core/map.h b/include/core/map.h index a5391f4d..10987d96 100644 --- a/include/core/map.h +++ b/include/core/map.h @@ -111,6 +111,7 @@ private: signals: void mapChanged(Map *map); void mapNeedsRedrawing(); + void objectsChanged(); }; #endif // MAP_H diff --git a/src/core/editcommands.cpp b/src/core/editcommands.cpp index d619b5a5..563a38fc 100644 --- a/src/core/editcommands.cpp +++ b/src/core/editcommands.cpp @@ -2,6 +2,7 @@ #include "mappixmapitem.h" #include "draggablepixmapitem.h" #include "bordermetatilespixmapitem.h" +#include "editor.h" #include @@ -321,4 +322,95 @@ bool EventMove::mergeWith(const QUndoCommand *command) { ************************************************************************ ******************************************************************************/ +EventShift::EventShift(QList events, + int deltaX, int deltaY, unsigned actionId, + QUndoCommand *parent) + : EventMove(events, deltaX, deltaY, actionId, parent) { + setText("Shift Events"); +} +EventShift::~EventShift() {} + +/****************************************************************************** + ************************************************************************ + ******************************************************************************/ + +EventCreate::EventCreate(Editor *editor, Map *map, Event *event, + QUndoCommand *parent) : QUndoCommand(parent) { + // + setText("Create Event"); + + this->editor = editor; + + this->map = map; + this->event = event; +} + +EventCreate::~EventCreate() {} + +void EventCreate::redo() { + QUndoCommand::redo(); + + map->addEvent(event); + + editor->project->loadEventPixmaps(map->getAllEvents()); + editor->addMapEvent(event); + + map->objectsChanged(); +} + +void EventCreate::undo() { + // + map->removeEvent(event); + + if (editor->scene->items().contains(event->pixmapItem)) { + editor->scene->removeItem(event->pixmapItem); + } + editor->selected_events->removeOne(event->pixmapItem); + + map->objectsChanged(); + + QUndoCommand::undo(); +} + +/****************************************************************************** + ************************************************************************ + ******************************************************************************/ + +EventDelete::EventDelete(Editor *editor, Map *map, Event *event, + QUndoCommand *parent) : QUndoCommand(parent) { + // + setText("Delete Event"); + + this->editor = editor; + + this->map = map; + this->event = event; +} + +EventDelete::~EventDelete() {} + +void EventDelete::redo() { + QUndoCommand::redo(); + + map->removeEvent(event); + + if (editor->scene->items().contains(event->pixmapItem)) { + editor->scene->removeItem(event->pixmapItem); + } + editor->selected_events->removeOne(event->pixmapItem); + + map->objectsChanged(); +} + +void EventDelete::undo() { + // + map->addEvent(event); + + editor->project->loadEventPixmaps(map->getAllEvents()); + editor->addMapEvent(event); + + map->objectsChanged(); + + QUndoCommand::undo(); +} diff --git a/src/core/event.cpp b/src/core/event.cpp index c35c37f5..754a3ab9 100644 --- a/src/core/event.cpp +++ b/src/core/event.cpp @@ -41,7 +41,7 @@ Event::Event(QJsonObject obj, QString type) Event* Event::createNewEvent(QString event_type, QString map_name, Project *project) { - Event *event = new Event; + Event *event = nullptr; if (event_type == EventType::Object) { event = createNewObjectEvent(project); event->setFrameFromMovement(event->get("movement_type")); @@ -59,6 +59,9 @@ Event* Event::createNewEvent(QString event_type, QString map_name, Project *proj event = createNewHiddenItemEvent(project); } else if (event_type == EventType::SecretBase) { event = createNewSecretBaseEvent(project); + } else { + // should never be reached but just in case + event = new Event; } event->setX(0); diff --git a/src/editor.cpp b/src/editor.cpp index f0c67e7f..8742c958 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -7,6 +7,7 @@ #include "currentselectedmetatilespixmapitem.h" #include "mapsceneeventfilter.h" #include "montabwidget.h" +#include "editcommands.h" #include #include #include @@ -1081,9 +1082,11 @@ void Editor::mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item // do nothing here, at least for now } else if (obj_edit_mode == "shift" && item->map) { static QPoint selection_origin; + static unsigned actionId = 0; if (event->type() == QEvent::GraphicsSceneMouseRelease) { // TODO: commit / update history here + actionId++; } else { if (event->type() == QEvent::GraphicsSceneMousePress) { selection_origin = QPoint(x, y); @@ -1092,10 +1095,15 @@ void Editor::mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item int xDelta = x - selection_origin.x(); int yDelta = y - selection_origin.y(); + QList selectedEvents; + for (DraggablePixmapItem *item : *(getObjects())) { - item->move(xDelta, yDelta); + //item->move(xDelta, yDelta); + selectedEvents.append(item->event); } selection_origin = QPoint(x, y); + + map->editHistory.push(new EventShift(selectedEvents, xDelta, yDelta, actionId)); } } } @@ -1845,10 +1853,9 @@ DraggablePixmapItem* Editor::addNewEvent(QString event_type) { project->healLocations.append(hl); event->put("index", project->healLocations.length()); } - map->addEvent(event); - project->loadEventPixmaps(map->getAllEvents()); - DraggablePixmapItem *object = addMapEvent(event); - return object; + map->editHistory.push(new EventCreate(this, map, event)); + + return event->pixmapItem; } return nullptr; } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 555be39f..faf63557 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -514,6 +514,7 @@ bool MainWindow::setMap(QString map_name, bool scrollTreeView) { connect(editor->map, SIGNAL(mapChanged(Map*)), this, SLOT(onMapChanged(Map *))); connect(editor->map, SIGNAL(mapNeedsRedrawing()), this, SLOT(onMapNeedsRedrawing())); + connect(editor->map, SIGNAL(objectsChanged()), this, SLOT(updateObjects())); setRecentMap(map_name); updateMapList(); @@ -2124,11 +2125,7 @@ void MainWindow::on_toolButton_deleteObject_clicked() } } } - editor->deleteEvent(item->event); - if (editor->scene->items().contains(item)) { - editor->scene->removeItem(item); - } - editor->selected_events->removeOne(item); + editor->map->editHistory.push(new EventDelete(editor, editor->map, item->event)); } else { // don't allow deletion of heal locations logWarn(QString("Cannot delete event of type '%1'").arg(item->event->get("event_type")));