From 941174d0ead8476d68b6b5cd022d7f669c3afac9 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Thu, 4 Jan 2024 21:36:14 -0500 Subject: [PATCH] Reduce redundant work in metatile selector --- include/ui/metatileselector.h | 7 +++++-- src/ui/metatileselector.cpp | 38 ++++++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/include/ui/metatileselector.h b/include/ui/metatileselector.h index 0c0b779f..eaf357ca 100644 --- a/include/ui/metatileselector.h +++ b/include/ui/metatileselector.h @@ -39,6 +39,7 @@ public: this->primaryTileset = map->layout->tileset_primary; this->secondaryTileset = map->layout->tileset_secondary; this->selection = MetatileSelection{}; + this->cellPos = QPoint(-1, -1); setAcceptHoverEvents(true); } QPoint getSelectionDimensions(); @@ -68,13 +69,15 @@ private: int externalSelectionHeight; QList externalSelectedMetatiles; MetatileSelection selection; + QPoint cellPos; void updateSelectedMetatiles(); void updateExternalSelectedMetatiles(); - uint16_t getMetatileId(int x, int y); + uint16_t getMetatileId(int x, int y) const; QPoint getMetatileIdCoords(uint16_t); - bool shouldAcceptEvent(QGraphicsSceneMouseEvent*); + bool positionIsValid(const QPoint &pos) const; bool selectionIsValid(); + void hoverChanged(); signals: void hoveredMetatileSelectionChanged(uint16_t); diff --git a/src/ui/metatileselector.cpp b/src/ui/metatileselector.cpp index d72bb25d..2c4a5fb3 100644 --- a/src/ui/metatileselector.cpp +++ b/src/ui/metatileselector.cpp @@ -114,41 +114,55 @@ void MetatileSelector::setPrefabSelection(MetatileSelection selection) { emit selectedMetatilesChanged(); } -bool MetatileSelector::shouldAcceptEvent(QGraphicsSceneMouseEvent *event) { - QPoint pos = this->getCellPos(event->pos()); +bool MetatileSelector::positionIsValid(const QPoint &pos) const { return Tileset::metatileIsValid(getMetatileId(pos.x(), pos.y()), this->primaryTileset, this->secondaryTileset); } void MetatileSelector::mousePressEvent(QGraphicsSceneMouseEvent *event) { - if (!shouldAcceptEvent(event)) return; + QPoint pos = this->getCellPos(event->pos()); + if (!positionIsValid(pos)) + return; + + this->cellPos = pos; SelectablePixmapItem::mousePressEvent(event); this->updateSelectedMetatiles(); } void MetatileSelector::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { - if (!shouldAcceptEvent(event)) return; + QPoint pos = this->getCellPos(event->pos()); + if (!positionIsValid(pos) || this->cellPos == pos) + return; + + this->cellPos = pos; SelectablePixmapItem::mouseMoveEvent(event); this->updateSelectedMetatiles(); - - QPoint pos = this->getCellPos(event->pos()); - uint16_t metatileId = this->getMetatileId(pos.x(), pos.y()); - emit this->hoveredMetatileSelectionChanged(metatileId); + this->hoverChanged(); } void MetatileSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { - if (!shouldAcceptEvent(event)) return; + QPoint pos = this->getCellPos(event->pos()); + if (!positionIsValid(pos)) + return; SelectablePixmapItem::mouseReleaseEvent(event); - this->updateSelectedMetatiles(); } void MetatileSelector::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { QPoint pos = this->getCellPos(event->pos()); - uint16_t metatileId = this->getMetatileId(pos.x(), pos.y()); + if (!positionIsValid(pos) || this->cellPos == pos) + return; + + this->cellPos = pos; + this->hoverChanged(); +} + +void MetatileSelector::hoverChanged() { + uint16_t metatileId = this->getMetatileId(this->cellPos.x(), this->cellPos.y()); emit this->hoveredMetatileSelectionChanged(metatileId); } void MetatileSelector::hoverLeaveEvent(QGraphicsSceneHoverEvent*) { emit this->hoveredMetatileSelectionCleared(); + this->cellPos = QPoint(-1, -1); } void MetatileSelector::updateSelectedMetatiles() { @@ -182,7 +196,7 @@ void MetatileSelector::updateExternalSelectedMetatiles() { emit selectedMetatilesChanged(); } -uint16_t MetatileSelector::getMetatileId(int x, int y) { +uint16_t MetatileSelector::getMetatileId(int x, int y) const { int index = y * this->numMetatilesWide + x; if (index < this->primaryTileset->metatiles.length()) { return static_cast(index);