add EventMove command, fix extra signal call in map border resize

This commit is contained in:
garakmon
2020-07-30 16:35:48 -04:00
committed by garak
parent fb1eec1755
commit a0dc347ad3
10 changed files with 290 additions and 160 deletions

View File

@@ -1,5 +1,6 @@
#include "editcommands.h"
#include "mappixmapitem.h"
#include "draggablepixmapitem.h"
#include "bordermetatilespixmapitem.h"
#include <QDebug>
@@ -247,6 +248,8 @@ void ResizeMap::redo() {
map->layout->border->copyFrom(newBorder);
map->setBorderDimensions(newBorderWidth, newBorderHeight, false);
}
map->mapNeedsRedrawing();
}
void ResizeMap::undo() {
@@ -262,7 +265,60 @@ void ResizeMap::undo() {
map->setBorderDimensions(oldBorderWidth, oldBorderHeight, false);
}
map->mapNeedsRedrawing();
QUndoCommand::undo();
}
/******************************************************************************
************************************************************************
******************************************************************************/
EventMove::EventMove(QList<Event *> events,
int deltaX, int deltaY, unsigned actionId,
QUndoCommand *parent) : QUndoCommand(parent) {
setText("Move Event");
this->events = events;
this->deltaX = deltaX;
this->deltaY = deltaY;
this->actionId = actionId;
}
EventMove::~EventMove() {}
void EventMove::redo() {
QUndoCommand::redo();
for (Event *event : events) {
event->pixmapItem->move(deltaX, deltaY);
}
}
void EventMove::undo() {
for (Event *event : events) {
event->pixmapItem->move(-deltaX, -deltaY);
}
QUndoCommand::undo();
}
bool EventMove::mergeWith(const QUndoCommand *command) {
const EventMove *other = static_cast<const EventMove *>(command);
if (actionId != other->actionId)
return false;
this->deltaX += other->deltaX;
this->deltaY += other->deltaY;
return true;
}
/******************************************************************************
************************************************************************
******************************************************************************/

View File

@@ -345,7 +345,6 @@ void Map::setDimensions(int newWidth, int newHeight, bool setNewBlockdata) {
layout->height = QString::number(newHeight);
emit mapChanged(this);
emit mapNeedsRedrawing();
}
void Map::setBorderDimensions(int newWidth, int newHeight, bool setNewBlockdata) {
@@ -357,7 +356,6 @@ void Map::setBorderDimensions(int newWidth, int newHeight, bool setNewBlockdata)
layout->border_height = QString::number(newHeight);
emit mapChanged(this);
emit mapNeedsRedrawing();
}
Block* Map::getBlock(int x, int y) {

View File

@@ -1,4 +1,5 @@
#include "editor.h"
#include "draggablepixmapitem.h"
#include "event.h"
#include "imageproviders.h"
#include "log.h"
@@ -1751,62 +1752,6 @@ Tileset* Editor::getCurrentMapPrimaryTileset()
return project->getTileset(tilesetLabel);
}
void DraggablePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *mouse) {
active = true;
last_x = static_cast<int>(mouse->pos().x() + this->pos().x()) / 16;
last_y = static_cast<int>(mouse->pos().y() + this->pos().y()) / 16;
this->editor->selectMapEvent(this, mouse->modifiers() & Qt::ControlModifier);
//this->editor->updateSelectedEvents();
selectingEvent = true;
}
void DraggablePixmapItem::move(int x, int y) {
event->setX(event->x() + x);
event->setY(event->y() + y);
updatePosition();
emitPositionChanged();
}
void DraggablePixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouse) {
if (active) {
int x = static_cast<int>(mouse->pos().x() + this->pos().x()) / 16;
int y = static_cast<int>(mouse->pos().y() + this->pos().y()) / 16;
this->editor->playerViewRect->updateLocation(x, y);
this->editor->cursorMapTileRect->updateLocation(x, y);
if (x != last_x || y != last_y) {
if (editor->selected_events->contains(this)) {
for (DraggablePixmapItem *item : *editor->selected_events) {
item->move(x - last_x, y - last_y);
}
} else {
move(x - last_x, y - last_y);
}
last_x = x;
last_y = y;
}
}
}
void DraggablePixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *) {
active = false;
}
void DraggablePixmapItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *) {
if (this->event->get("event_type") == EventType::Warp) {
QString destMap = this->event->get("destination_map_name");
if (destMap != NONE_MAP_NAME) {
emit editor->warpEventDoubleClicked(this->event->get("destination_map_name"), this->event->get("destination_warp"));
}
}
else if (this->event->get("event_type") == EventType::SecretBase) {
QString baseId = this->event->get("secret_base_id");
QString destMap = editor->project->mapConstantsToMapNames->value("MAP_" + baseId.left(baseId.lastIndexOf("_")));
if (destMap != NONE_MAP_NAME) {
emit editor->warpEventDoubleClicked(destMap, "0");
}
}
}
QList<DraggablePixmapItem *> *Editor::getObjects() {
QList<DraggablePixmapItem *> *list = new QList<DraggablePixmapItem *>;
for (Event *event : map->getAllEvents()) {
@@ -1944,6 +1889,7 @@ void Editor::objectsView_onMousePress(QMouseEvent *event) {
this->ui->toolButton_Paint->setChecked(false);
this->ui->toolButton_Select->setChecked(true);
}
bool multiSelect = event->modifiers() & Qt::ControlModifier;
if (!selectingEvent && !multiSelect && selected_events->length() > 1) {
DraggablePixmapItem *first = selected_events->first();

View File

@@ -11,6 +11,7 @@
#include "customattributestable.h"
#include "scripting.h"
#include "adjustingstackedwidget.h"
#include "draggablepixmapitem.h"
#include "editcommands.h"
#include <QFileDialog>

View File

@@ -0,0 +1,115 @@
#include "draggablepixmapitem.h"
#include "editor.h"
#include "editcommands.h"
static unsigned currentActionId = 0;
void DraggablePixmapItem::updatePosition() {
int x = event->getPixelX();
int y = event->getPixelY();
setX(x);
setY(y);
if (editor->selected_events && editor->selected_events->contains(this)) {
setZValue(event->y() + 1);
} else {
setZValue(event->y());
}
}
void DraggablePixmapItem::emitPositionChanged() {
emit xChanged(event->x());
emit yChanged(event->y());
emit elevationChanged(event->elevation());
}
void DraggablePixmapItem::updatePixmap() {
QList<Event*> objects;
objects.append(event);
event->pixmap = QPixmap();
editor->project->loadEventPixmaps(objects);
this->updatePosition();
editor->redrawObject(this);
emit spriteChanged(event->pixmap);
}
void DraggablePixmapItem::bind(QComboBox *combo, QString key) {
connect(combo, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::currentTextChanged),
this, [this, key](QString value){
this->event->put(key, value);
});
connect(this, &DraggablePixmapItem::onPropertyChanged,
this, [combo, key](QString key2, QString value){
if (key2 == key) {
combo->addItem(value);
combo->setCurrentText(value);
}
});
}
void DraggablePixmapItem::bindToUserData(QComboBox *combo, QString key) {
connect(combo, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
this, [this, combo, key](int index) {
this->event->put(key, combo->itemData(index).toString());
});
}
void DraggablePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *mouse) {
active = true;
last_x = static_cast<int>(mouse->pos().x() + this->pos().x()) / 16;
last_y = static_cast<int>(mouse->pos().y() + this->pos().y()) / 16;
this->editor->selectMapEvent(this, mouse->modifiers() & Qt::ControlModifier);
this->editor->selectingEvent = true;
}
void DraggablePixmapItem::move(int x, int y) {
event->setX(event->x() + x);
event->setY(event->y() + y);
updatePosition();
emitPositionChanged();
}
void DraggablePixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouse) {
if (active) {
int x = static_cast<int>(mouse->pos().x() + this->pos().x()) / 16;
int y = static_cast<int>(mouse->pos().y() + this->pos().y()) / 16;
this->editor->playerViewRect->updateLocation(x, y);
this->editor->cursorMapTileRect->updateLocation(x, y);
if (x != last_x || y != last_y) {
QList <Event *> selectedEvents;
if (editor->selected_events->contains(this)) {
for (DraggablePixmapItem *item : *editor->selected_events) {
selectedEvents.append(item->event);
}
} else {
selectedEvents.append(this->event);
}
editor->map->editHistory.push(new EventMove(selectedEvents, x - last_x, y - last_y, currentActionId));
last_x = x;
last_y = y;
}
}
}
void DraggablePixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *) {
active = false;
currentActionId++;
}
void DraggablePixmapItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *) {
if (this->event->get("event_type") == EventType::Warp) {
QString destMap = this->event->get("destination_map_name");
if (destMap != NONE_MAP_NAME) {
emit editor->warpEventDoubleClicked(this->event->get("destination_map_name"), this->event->get("destination_warp"));
}
}
else if (this->event->get("event_type") == EventType::SecretBase) {
QString baseId = this->event->get("secret_base_id");
QString destMap = editor->project->mapConstantsToMapNames->value("MAP_" + baseId.left(baseId.lastIndexOf("_")));
if (destMap != NONE_MAP_NAME) {
emit editor->warpEventDoubleClicked(destMap, "0");
}
}
}