allow entries to be moved by dragging them around map

This commit is contained in:
garak 2019-04-13 14:25:12 -04:00 committed by huderlem
parent 92ea089876
commit e30ec429ab
4 changed files with 48 additions and 12 deletions

View file

@ -42,6 +42,7 @@ public:
void onRegionMapLayoutHoveredTileCleared();
void onRegionMapEntriesSelectedTileChanged(QString) {};
void onRegionMapEntryDragged(int, int);
void undo();
void redo();

View file

@ -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*);

View file

@ -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();

View file

@ -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) {