Reduce redundant work in metatile selector

This commit is contained in:
GriffinR 2024-01-04 21:36:14 -05:00
parent 8c85209d53
commit 941174d0ea
2 changed files with 31 additions and 14 deletions

View file

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

View file

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