edit commands for creating and deleting events

This commit is contained in:
garakmon 2020-07-30 20:54:44 -04:00 committed by garak
parent a0dc347ad3
commit 2598ca2277
7 changed files with 177 additions and 14 deletions

View file

@ -9,6 +9,7 @@ class Map;
class Blockdata; class Blockdata;
class Event; class Event;
class DraggablePixmapItem; class DraggablePixmapItem;
class Editor;
enum CommandId { enum CommandId {
ID_PaintMetatile, // - done ID_PaintMetatile, // - done
@ -18,9 +19,9 @@ enum CommandId {
ID_ResizeMap, // - done ID_ResizeMap, // - done
ID_PaintBorder, // - done ID_PaintBorder, // - done
ID_EventMove, // - done ID_EventMove, // - done
ID_EventShift, // - ID_EventShift, // - done
ID_EventCreate, // - ID_EventCreate, // - done
ID_EventDelete, // - ID_EventDelete, // - done
ID_EventSetData, // - ? ID_EventSetData, // - ?
// Tileset editor history commands // Tileset editor history commands
// Region map editor history commands // Region map editor history commands
@ -193,4 +194,59 @@ private:
unsigned actionId; unsigned actionId;
}; };
///
class EventShift : public EventMove {
public:
EventShift(QList<Event *> 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 #endif // EDITCOMMANDS_H

View file

@ -99,8 +99,15 @@ public:
bool hFlip = false; bool hFlip = false;
bool usingSprite; bool usingSprite;
void activate() { active = true; }
void deactivate() { active = false; }
bool isActive() { return active; }
DraggablePixmapItem *pixmapItem = nullptr; DraggablePixmapItem *pixmapItem = nullptr;
void setPixmapItem(DraggablePixmapItem *item) { pixmapItem = item; } void setPixmapItem(DraggablePixmapItem *item) { pixmapItem = item; }
private:
bool active = true;
}; };
#endif // EVENT_H #endif // EVENT_H

View file

@ -111,6 +111,7 @@ private:
signals: signals:
void mapChanged(Map *map); void mapChanged(Map *map);
void mapNeedsRedrawing(); void mapNeedsRedrawing();
void objectsChanged();
}; };
#endif // MAP_H #endif // MAP_H

View file

@ -2,6 +2,7 @@
#include "mappixmapitem.h" #include "mappixmapitem.h"
#include "draggablepixmapitem.h" #include "draggablepixmapitem.h"
#include "bordermetatilespixmapitem.h" #include "bordermetatilespixmapitem.h"
#include "editor.h"
#include <QDebug> #include <QDebug>
@ -321,4 +322,95 @@ bool EventMove::mergeWith(const QUndoCommand *command) {
************************************************************************ ************************************************************************
******************************************************************************/ ******************************************************************************/
EventShift::EventShift(QList<Event *> 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();
}

View file

@ -41,7 +41,7 @@ Event::Event(QJsonObject obj, QString type)
Event* Event::createNewEvent(QString event_type, QString map_name, Project *project) Event* Event::createNewEvent(QString event_type, QString map_name, Project *project)
{ {
Event *event = new Event; Event *event = nullptr;
if (event_type == EventType::Object) { if (event_type == EventType::Object) {
event = createNewObjectEvent(project); event = createNewObjectEvent(project);
event->setFrameFromMovement(event->get("movement_type")); event->setFrameFromMovement(event->get("movement_type"));
@ -59,6 +59,9 @@ Event* Event::createNewEvent(QString event_type, QString map_name, Project *proj
event = createNewHiddenItemEvent(project); event = createNewHiddenItemEvent(project);
} else if (event_type == EventType::SecretBase) { } else if (event_type == EventType::SecretBase) {
event = createNewSecretBaseEvent(project); event = createNewSecretBaseEvent(project);
} else {
// should never be reached but just in case
event = new Event;
} }
event->setX(0); event->setX(0);

View file

@ -7,6 +7,7 @@
#include "currentselectedmetatilespixmapitem.h" #include "currentselectedmetatilespixmapitem.h"
#include "mapsceneeventfilter.h" #include "mapsceneeventfilter.h"
#include "montabwidget.h" #include "montabwidget.h"
#include "editcommands.h"
#include <QCheckBox> #include <QCheckBox>
#include <QPainter> #include <QPainter>
#include <QMouseEvent> #include <QMouseEvent>
@ -1081,9 +1082,11 @@ void Editor::mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item
// do nothing here, at least for now // do nothing here, at least for now
} else if (obj_edit_mode == "shift" && item->map) { } else if (obj_edit_mode == "shift" && item->map) {
static QPoint selection_origin; static QPoint selection_origin;
static unsigned actionId = 0;
if (event->type() == QEvent::GraphicsSceneMouseRelease) { if (event->type() == QEvent::GraphicsSceneMouseRelease) {
// TODO: commit / update history here // TODO: commit / update history here
actionId++;
} else { } else {
if (event->type() == QEvent::GraphicsSceneMousePress) { if (event->type() == QEvent::GraphicsSceneMousePress) {
selection_origin = QPoint(x, y); selection_origin = QPoint(x, y);
@ -1092,10 +1095,15 @@ void Editor::mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item
int xDelta = x - selection_origin.x(); int xDelta = x - selection_origin.x();
int yDelta = y - selection_origin.y(); int yDelta = y - selection_origin.y();
QList<Event *> selectedEvents;
for (DraggablePixmapItem *item : *(getObjects())) { for (DraggablePixmapItem *item : *(getObjects())) {
item->move(xDelta, yDelta); //item->move(xDelta, yDelta);
selectedEvents.append(item->event);
} }
selection_origin = QPoint(x, y); 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); project->healLocations.append(hl);
event->put("index", project->healLocations.length()); event->put("index", project->healLocations.length());
} }
map->addEvent(event); map->editHistory.push(new EventCreate(this, map, event));
project->loadEventPixmaps(map->getAllEvents());
DraggablePixmapItem *object = addMapEvent(event); return event->pixmapItem;
return object;
} }
return nullptr; return nullptr;
} }

View file

@ -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(mapChanged(Map*)), this, SLOT(onMapChanged(Map *)));
connect(editor->map, SIGNAL(mapNeedsRedrawing()), this, SLOT(onMapNeedsRedrawing())); connect(editor->map, SIGNAL(mapNeedsRedrawing()), this, SLOT(onMapNeedsRedrawing()));
connect(editor->map, SIGNAL(objectsChanged()), this, SLOT(updateObjects()));
setRecentMap(map_name); setRecentMap(map_name);
updateMapList(); updateMapList();
@ -2124,11 +2125,7 @@ void MainWindow::on_toolButton_deleteObject_clicked()
} }
} }
} }
editor->deleteEvent(item->event); editor->map->editHistory.push(new EventDelete(editor, editor->map, item->event));
if (editor->scene->items().contains(item)) {
editor->scene->removeItem(item);
}
editor->selected_events->removeOne(item);
} }
else { // don't allow deletion of heal locations else { // don't allow deletion of heal locations
logWarn(QString("Cannot delete event of type '%1'").arg(item->event->get("event_type"))); logWarn(QString("Cannot delete event of type '%1'").arg(item->event->get("event_type")));