diff --git a/include/ui/metatilelayersitem.h b/include/ui/metatilelayersitem.h index fbd769e0..ef05a77d 100644 --- a/include/ui/metatilelayersitem.h +++ b/include/ui/metatilelayersitem.h @@ -14,25 +14,33 @@ public: this->primaryTileset = primaryTileset; this->secondaryTileset = secondaryTileset; this->clearLastModifiedCoords(); + this->clearLastHoveredCoords(); + setAcceptHoverEvents(true); } void draw(); void setTilesets(Tileset*, Tileset*); void setMetatile(Metatile*); void clearLastModifiedCoords(); + void clearLastHoveredCoords(); bool showGrid; private: Metatile* metatile; Tileset *primaryTileset; Tileset *secondaryTileset; - QPoint prevChangedTile; - void getBoundedCoords(QPointF, int*, int*); + QPoint prevChangedPos; + QPoint prevHoveredPos; + QPoint getBoundedPos(const QPointF &); signals: void tileChanged(int, int); void selectedTilesChanged(QPoint, int, int); + void hoveredTileChanged(uint16_t); + void hoveredTileCleared(); protected: void mousePressEvent(QGraphicsSceneMouseEvent*); void mouseMoveEvent(QGraphicsSceneMouseEvent*); void mouseReleaseEvent(QGraphicsSceneMouseEvent*); + void hoverMoveEvent(QGraphicsSceneHoverEvent*); + void hoverLeaveEvent(QGraphicsSceneHoverEvent*); }; #endif // METATILELAYERSITEM_H diff --git a/src/ui/metatilelayersitem.cpp b/src/ui/metatilelayersitem.cpp index d113d098..e4a1e74e 100644 --- a/src/ui/metatilelayersitem.cpp +++ b/src/ui/metatilelayersitem.cpp @@ -3,22 +3,22 @@ #include "imageproviders.h" #include -void MetatileLayersItem::draw() { - static const QList tileCoords = QList{ - QPoint(0, 0), - QPoint(16, 0), - QPoint(0, 16), - QPoint(16, 16), - QPoint(32, 0), - QPoint(48, 0), - QPoint(32, 16), - QPoint(48, 16), - QPoint(64, 0), - QPoint(80, 0), - QPoint(64, 16), - QPoint(80, 16), - }; +static const QList tilePositions = { + QPoint(0, 0), + QPoint(1, 0), + QPoint(0, 1), + QPoint(1, 1), + QPoint(2, 0), + QPoint(3, 0), + QPoint(2, 1), + QPoint(3, 1), + QPoint(4, 0), + QPoint(5, 0), + QPoint(4, 1), + QPoint(5, 1), +}; +void MetatileLayersItem::draw() { const int numLayers = projectConfig.getNumLayersInMetatile(); QPixmap pixmap(numLayers * 32, 32); QPainter painter(&pixmap); @@ -30,7 +30,7 @@ void MetatileLayersItem::draw() { QImage tileImage = getPalettedTileImage(tile.tileId, this->primaryTileset, this->secondaryTileset, tile.palette, true) .mirrored(tile.xflip, tile.yflip) .scaled(16, 16); - painter.drawImage(tileCoords.at(i), tileImage); + painter.drawImage(tilePositions.at(i) * 16, tileImage); } if (this->showGrid) { // Draw grid @@ -47,6 +47,7 @@ void MetatileLayersItem::draw() { void MetatileLayersItem::setMetatile(Metatile *metatile) { this->metatile = metatile; this->clearLastModifiedCoords(); + this->clearLastHoveredCoords(); } void MetatileLayersItem::setTilesets(Tileset *primaryTileset, Tileset *secondaryTileset) { @@ -54,6 +55,7 @@ void MetatileLayersItem::setTilesets(Tileset *primaryTileset, Tileset *secondary this->secondaryTileset = secondaryTileset; this->draw(); this->clearLastModifiedCoords(); + this->clearLastHoveredCoords(); } void MetatileLayersItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { @@ -64,11 +66,10 @@ void MetatileLayersItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { emit this->selectedTilesChanged(selectionOrigin, dimensions.x(), dimensions.y()); this->drawSelection(); } else { - int x, y; - this->getBoundedCoords(event->pos(), &x, &y); - this->prevChangedTile.setX(x); - this->prevChangedTile.setY(y); - emit this->tileChanged(x, y); + const QPoint pos = this->getBoundedPos(event->pos()); + this->prevChangedPos = pos; + this->clearLastHoveredCoords(); + emit this->tileChanged(pos.x(), pos.y()); } } @@ -80,12 +81,11 @@ void MetatileLayersItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { emit this->selectedTilesChanged(selectionOrigin, dimensions.x(), dimensions.y()); this->drawSelection(); } else { - int x, y; - this->getBoundedCoords(event->pos(), &x, &y); - if (prevChangedTile.x() != x || prevChangedTile.y() != y) { - this->prevChangedTile.setX(x); - this->prevChangedTile.setY(y); - emit this->tileChanged(x, y); + const QPoint pos = this->getBoundedPos(event->pos()); + if (prevChangedPos != pos) { + this->prevChangedPos = pos; + this->clearLastHoveredCoords(); + emit this->tileChanged(pos.x(), pos.y()); } } } @@ -101,17 +101,40 @@ void MetatileLayersItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { this->draw(); } -void MetatileLayersItem::clearLastModifiedCoords() { - this->prevChangedTile.setX(-1); - this->prevChangedTile.setY(-1); +void MetatileLayersItem::hoverMoveEvent(QGraphicsSceneHoverEvent * event) { + const QPoint pos = this->getBoundedPos(event->pos()); + if (pos == this->prevHoveredPos) + return; + this->prevHoveredPos = pos; + + int tileIndex = tilePositions.indexOf(pos); + if (tileIndex < 0 || tileIndex >= this->metatile->tiles.length()) + return; + + emit this->hoveredTileChanged(this->metatile->tiles.at(tileIndex).tileId); } -void MetatileLayersItem::getBoundedCoords(QPointF pos, int *x, int *y) { - int maxX = (projectConfig.getNumLayersInMetatile() * 2) - 1; - *x = static_cast(pos.x()) / 16; - *y = static_cast(pos.y()) / 16; - if (*x < 0) *x = 0; - if (*y < 0) *y = 0; - if (*x > maxX) *x = maxX; - if (*y > 1) *y = 1; +void MetatileLayersItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *) { + this->clearLastHoveredCoords(); + emit this->hoveredTileCleared(); +} + +void MetatileLayersItem::clearLastModifiedCoords() { + this->prevChangedPos = QPoint(-1, -1); +} + +void MetatileLayersItem::clearLastHoveredCoords() { + this->prevHoveredPos = QPoint(-1, -1); +} + +QPoint MetatileLayersItem::getBoundedPos(const QPointF &pos) { + int x, y; + int maxX = (projectConfig.getNumLayersInMetatile() * 2) - 1; + x = static_cast(pos.x()) / 16; + y = static_cast(pos.y()) / 16; + if (x < 0) x = 0; + if (y < 0) y = 0; + if (x > maxX) x = maxX; + if (y > 1) y = 1; + return QPoint(x, y); } diff --git a/src/ui/tileseteditor.cpp b/src/ui/tileseteditor.cpp index 0032264e..1d8f74fd 100644 --- a/src/ui/tileseteditor.cpp +++ b/src/ui/tileseteditor.cpp @@ -206,6 +206,10 @@ void TilesetEditor::initMetatileLayersItem() { this, &TilesetEditor::onMetatileLayerTileChanged); connect(this->metatileLayersItem, &MetatileLayersItem::selectedTilesChanged, this, &TilesetEditor::onMetatileLayerSelectionChanged); + connect(this->metatileLayersItem, &MetatileLayersItem::hoveredTileChanged, + this, &TilesetEditor::onHoveredTileChanged); + connect(this->metatileLayersItem, &MetatileLayersItem::hoveredTileCleared, + this, &TilesetEditor::onHoveredTileCleared); bool showGrid = porymapConfig.getShowTilesetEditorLayerGrid(); this->ui->actionLayer_Grid->setChecked(showGrid); @@ -861,6 +865,7 @@ bool TilesetEditor::replaceMetatile(uint16_t metatileId, const Metatile * src, Q this->metatileSelector->draw(); this->metatileLayersItem->draw(); this->metatileLayersItem->clearLastModifiedCoords(); + this->metatileLayersItem->clearLastHoveredCoords(); return true; }