From e30ec429abee1bf79bc4609395533bdaae8c6ebb Mon Sep 17 00:00:00 2001 From: garak Date: Sat, 13 Apr 2019 14:25:12 -0400 Subject: [PATCH] allow entries to be moved by dragging them around map --- include/ui/regionmapeditor.h | 1 + include/ui/regionmapentriespixmapitem.h | 5 ++- src/ui/regionmapeditor.cpp | 11 +++++-- src/ui/regionmapentriespixmapitem.cpp | 43 +++++++++++++++++++------ 4 files changed, 48 insertions(+), 12 deletions(-) diff --git a/include/ui/regionmapeditor.h b/include/ui/regionmapeditor.h index 831c0fe5..d5dc00d2 100644 --- a/include/ui/regionmapeditor.h +++ b/include/ui/regionmapeditor.h @@ -42,6 +42,7 @@ public: void onRegionMapLayoutHoveredTileCleared(); void onRegionMapEntriesSelectedTileChanged(QString) {}; + void onRegionMapEntryDragged(int, int); void undo(); void redo(); diff --git a/include/ui/regionmapentriespixmapitem.h b/include/ui/regionmapentriespixmapitem.h index 26f6b245..12808c33 100644 --- a/include/ui/regionmapentriespixmapitem.h +++ b/include/ui/regionmapentriespixmapitem.h @@ -16,10 +16,12 @@ public: QString currentSection = QString(); int selectedTile; int highlightedTile; + int pressedX; + int pressedY; void draw(); void select(int, int); void select(int); - void highlight(int, int, int); + bool draggingEntry = false; private: void updateSelectedTile(); @@ -29,6 +31,7 @@ signals: void hoveredTileChanged(int); void hoveredTileCleared(); void selectedTileChanged(QString); + void entryPositionChanged(int, int); protected: void hoverMoveEvent(QGraphicsSceneHoverEvent*); diff --git a/src/ui/regionmapeditor.cpp b/src/ui/regionmapeditor.cpp index b61598a5..ba33921b 100644 --- a/src/ui/regionmapeditor.cpp +++ b/src/ui/regionmapeditor.cpp @@ -181,6 +181,9 @@ void RegionMapEditor::displayRegionMapEntriesImage() { this->region_map_entries_item = new RegionMapEntriesPixmapItem(this->region_map, this->mapsquare_selector_item); + connect(this->region_map_entries_item, &RegionMapEntriesPixmapItem::entryPositionChanged, + this, &RegionMapEditor::onRegionMapEntryDragged); + if (entriesFirstDraw) { QString first = this->project->mapSectionValueToName.first(); this->region_map_entries_item->currentSection = first; @@ -221,8 +224,6 @@ void RegionMapEditor::updateRegionMapEntryOptions(QString section) { this->ui->spinBox_RM_Entry_width->setEnabled(enabled); this->ui->spinBox_RM_Entry_height->setEnabled(enabled); - // if the key is not in the entries map, add it - this->ui->comboBox_RM_Entry_MapSection->setCurrentText(section); this->activeEntry = section; this->region_map_entries_item->currentSection = section; @@ -365,6 +366,12 @@ void RegionMapEditor::onRegionMapTileSelectorHoveredTileCleared() { this->ui->statusbar->clearMessage(); } +void RegionMapEditor::onRegionMapEntryDragged(int new_x, int new_y) { + on_spinBox_RM_Entry_x_valueChanged(new_x); + on_spinBox_RM_Entry_y_valueChanged(new_y); + updateRegionMapEntryOptions(activeEntry); +} + void RegionMapEditor::onRegionMapLayoutSelectedTileChanged(int index) { setCurrentSquareOptions(); QString message = QString(); diff --git a/src/ui/regionmapentriespixmapitem.cpp b/src/ui/regionmapentriespixmapitem.cpp index b5f1c31d..08f07aa8 100644 --- a/src/ui/regionmapentriespixmapitem.cpp +++ b/src/ui/regionmapentriespixmapitem.cpp @@ -76,22 +76,47 @@ void RegionMapEntriesPixmapItem::select(int index) { emit selectedTileChanged(this->region_map->map_squares[index].mapsec); } -void RegionMapEntriesPixmapItem::highlight(int x, int y, int red) { - this->highlightedTile = red; - SelectablePixmapItem::select(x + this->region_map->padLeft, y + this->region_map->padTop, 0, 0); - draw(); -} - void RegionMapEntriesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { - event->ignore(); + QPoint pos = this->getCellPos(event->pos()); + int x = pos.x() - this->region_map->padLeft; + int y = pos.y() - this->region_map->padTop; + + RegionMapEntry entry = this->region_map->mapSecToMapEntry.value(currentSection); + pressedX = x - entry.x; + pressedY = y - entry.y; + if (entry.x == x && entry.y == y) { + this->draggingEntry = true; + } + else if (pressedX < entry.width && x >= entry.x && pressedY < entry.height && y >= entry.y) { + this->draggingEntry = true; + } } void RegionMapEntriesPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { - event->ignore(); + if (!draggingEntry) { + event->ignore(); + return; + } + + QPoint pos = this->getCellPos(event->pos()); + int new_x = pos.x() - this->region_map->padLeft - pressedX; + int new_y = pos.y() - this->region_map->padTop - pressedY; + + RegionMapEntry entry = this->region_map->mapSecToMapEntry.value(currentSection); + + // check to make sure not moving out of bounds + if (new_x + entry.width > this->region_map->width() - this->region_map->padLeft - this->region_map->padRight + || new_y + entry.height > this->region_map->height() - this->region_map->padTop - this->region_map->padBottom + || new_x < 0 || new_y < 0) + return; + + if (new_x != entry.x || new_y != entry.y) { + emit entryPositionChanged(new_x, new_y); + } } void RegionMapEntriesPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { - event->ignore(); + this->draggingEntry = false; } void RegionMapEntriesPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {