Skip to content

Commit

Permalink
Fix regression for dragging multiple events
Browse files Browse the repository at this point in the history
  • Loading branch information
GriffinRichards committed Aug 31, 2024
1 parent 1a456bc commit 0b4f027
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 41 deletions.
3 changes: 1 addition & 2 deletions include/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ class Editor : public QObject
Tileset *getCurrentMapPrimaryTileset();

DraggablePixmapItem *addMapEvent(Event *event);
void selectMapEvent(DraggablePixmapItem *object);
void selectMapEvent(DraggablePixmapItem *object, bool toggle);
void selectMapEvent(DraggablePixmapItem *object, bool toggle = false);
DraggablePixmapItem *addNewEvent(Event::Type type);
void updateSelectedEvents();
void duplicateSelectedEvents();
Expand Down
12 changes: 6 additions & 6 deletions include/ui/draggablepixmapitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ class DraggablePixmapItem : public QObject, public QGraphicsPixmapItem {
updatePosition();
}

Editor *editor = nullptr;
Event *event = nullptr;
QGraphicsItemAnimation *pos_anim = nullptr;

bool active;
int last_x;
int last_y;

void updatePosition();
void move(int dx, int dy);
void moveTo(const QPoint &pos);
void emitPositionChanged();
void updatePixmap();

private:
Editor *editor = nullptr;
QPoint lastPos;
bool active = false;
bool releaseSelectionQueued = false;

signals:
void positionChanged(Event *event);
void xChanged(int);
Expand Down
2 changes: 1 addition & 1 deletion src/core/editcommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ void EventCreate::redo() {

// select this event
editor->selected_events->clear();
editor->selectMapEvent(event->getPixmapItem(), false);
editor->selectMapEvent(event->getPixmapItem());
}

void EventCreate::undo() {
Expand Down
12 changes: 3 additions & 9 deletions src/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,7 @@ void Editor::mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item
if (newEvent) {
newEvent->move(pos.x(), pos.y());
emit objectsChanged();
selectMapEvent(newEvent, false);
selectMapEvent(newEvent);
}
}
}
Expand Down Expand Up @@ -1994,10 +1994,6 @@ void Editor::updateSelectedEvents() {
emit objectsChanged();
}

void Editor::selectMapEvent(DraggablePixmapItem *object) {
selectMapEvent(object, false);
}

void Editor::selectMapEvent(DraggablePixmapItem *object, bool toggle) {
if (!selected_events || !object)
return;
Expand Down Expand Up @@ -2228,10 +2224,8 @@ void Editor::objectsView_onMousePress(QMouseEvent *event) {

bool multiSelect = event->modifiers() & Qt::ControlModifier;
if (!selectingEvent && !multiSelect && selected_events->length() > 1) {
DraggablePixmapItem *first = selected_events->first();
selected_events->clear();
selected_events->append(first);
updateSelectedEvents();
// User is clearing group selection by clicking on the background
this->selectMapEvent(selected_events->first());
}
selectingEvent = false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2014,7 +2014,7 @@ void MainWindow::addNewEvent(Event::Type type) {
auto centerPos = ui->graphicsView_Map->mapToScene(halfSize.width(), halfSize.height());
object->moveTo(Metatile::coordFromPixmapCoord(centerPos));
updateObjects();
editor->selectMapEvent(object, false);
editor->selectMapEvent(object);
} else {
QMessageBox msgBox(this);
msgBox.setText("Failed to add new event");
Expand Down
70 changes: 48 additions & 22 deletions src/ui/draggablepixmapitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,26 @@ void DraggablePixmapItem::updatePixmap() {
}

void DraggablePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *mouse) {
active = true;
QPoint pos = Metatile::coordFromPixmapCoord(mouse->scenePos());
last_x = pos.x();
last_y = pos.y();
this->editor->selectMapEvent(this, mouse->modifiers() & Qt::ControlModifier);
if (this->active)
return;
this->active = true;
this->lastPos = Metatile::coordFromPixmapCoord(mouse->scenePos());

bool selectionToggle = mouse->modifiers() & Qt::ControlModifier;
if (selectionToggle || !editor->selected_events->contains(this)) {
// User is either toggling this selection on/off as part of a group selection,
// or they're newly selecting just this item.
this->editor->selectMapEvent(this, selectionToggle);
} else {
// This item is already selected and the user isn't toggling the selection, so there are 4 possibilities:
// 1. This is the only selected event, and the selection is pointless.
// 2. This is the only selected event, and they want to drag the item around.
// 3. There's a group selection, and they want to start a new selection with just this item.
// 4. There's a group selection, and they want to drag the group around.
// 'selectMapEvent' will immediately clear the rest of the selection, which supports #1-3 but prevents #4.
// To support #4 we set the flag below, and we only call 'selectMapEvent' on mouse release if no move occurred.
this->releaseSelectionQueued = true;
}
this->editor->selectingEvent = true;
}

Expand All @@ -57,28 +72,39 @@ void DraggablePixmapItem::moveTo(const QPoint &pos) {
}

void DraggablePixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouse) {
if (active) {
QPoint pos = Metatile::coordFromPixmapCoord(mouse->scenePos());
if (pos.x() != last_x || pos.y() != last_y) {
emit this->editor->map_item->hoveredMapMetatileChanged(pos);
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, pos.x() - last_x, pos.y() - last_y, currentActionId));
last_x = pos.x();
last_y = pos.y();
if (!this->active)
return;

QPoint pos = Metatile::coordFromPixmapCoord(mouse->scenePos());
if (pos == this->lastPos)
return;

QPoint moveDistance = pos - this->lastPos;
this->lastPos = pos;
emit this->editor->map_item->hoveredMapMetatileChanged(pos);

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, moveDistance.x(), moveDistance.y(), currentActionId));
this->releaseSelectionQueued = false;
}

void DraggablePixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *) {
active = false;
void DraggablePixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouse) {
if (!this->active)
return;
this->active = false;
currentActionId++;
if (this->releaseSelectionQueued) {
this->releaseSelectionQueued = false;
if (Metatile::coordFromPixmapCoord(mouse->scenePos()) == this->lastPos)
this->editor->selectMapEvent(this);
}
}

void DraggablePixmapItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *) {
Expand Down

0 comments on commit 0b4f027

Please sign in to comment.