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->primaryTileset = map->layout->tileset_primary;
this->secondaryTileset = map->layout->tileset_secondary; this->secondaryTileset = map->layout->tileset_secondary;
this->selection = MetatileSelection{}; this->selection = MetatileSelection{};
this->cellPos = QPoint(-1, -1);
setAcceptHoverEvents(true); setAcceptHoverEvents(true);
} }
QPoint getSelectionDimensions(); QPoint getSelectionDimensions();
@ -68,13 +69,15 @@ private:
int externalSelectionHeight; int externalSelectionHeight;
QList<uint16_t> externalSelectedMetatiles; QList<uint16_t> externalSelectedMetatiles;
MetatileSelection selection; MetatileSelection selection;
QPoint cellPos;
void updateSelectedMetatiles(); void updateSelectedMetatiles();
void updateExternalSelectedMetatiles(); void updateExternalSelectedMetatiles();
uint16_t getMetatileId(int x, int y); uint16_t getMetatileId(int x, int y) const;
QPoint getMetatileIdCoords(uint16_t); QPoint getMetatileIdCoords(uint16_t);
bool shouldAcceptEvent(QGraphicsSceneMouseEvent*); bool positionIsValid(const QPoint &pos) const;
bool selectionIsValid(); bool selectionIsValid();
void hoverChanged();
signals: signals:
void hoveredMetatileSelectionChanged(uint16_t); void hoveredMetatileSelectionChanged(uint16_t);

View file

@ -114,41 +114,55 @@ void MetatileSelector::setPrefabSelection(MetatileSelection selection) {
emit selectedMetatilesChanged(); emit selectedMetatilesChanged();
} }
bool MetatileSelector::shouldAcceptEvent(QGraphicsSceneMouseEvent *event) { bool MetatileSelector::positionIsValid(const QPoint &pos) const {
QPoint pos = this->getCellPos(event->pos());
return Tileset::metatileIsValid(getMetatileId(pos.x(), pos.y()), this->primaryTileset, this->secondaryTileset); return Tileset::metatileIsValid(getMetatileId(pos.x(), pos.y()), this->primaryTileset, this->secondaryTileset);
} }
void MetatileSelector::mousePressEvent(QGraphicsSceneMouseEvent *event) { 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); SelectablePixmapItem::mousePressEvent(event);
this->updateSelectedMetatiles(); this->updateSelectedMetatiles();
} }
void MetatileSelector::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { 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); SelectablePixmapItem::mouseMoveEvent(event);
this->updateSelectedMetatiles(); this->updateSelectedMetatiles();
this->hoverChanged();
QPoint pos = this->getCellPos(event->pos());
uint16_t metatileId = this->getMetatileId(pos.x(), pos.y());
emit this->hoveredMetatileSelectionChanged(metatileId);
} }
void MetatileSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { void MetatileSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
if (!shouldAcceptEvent(event)) return; QPoint pos = this->getCellPos(event->pos());
if (!positionIsValid(pos))
return;
SelectablePixmapItem::mouseReleaseEvent(event); SelectablePixmapItem::mouseReleaseEvent(event);
this->updateSelectedMetatiles();
} }
void MetatileSelector::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { void MetatileSelector::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
QPoint pos = this->getCellPos(event->pos()); 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); emit this->hoveredMetatileSelectionChanged(metatileId);
} }
void MetatileSelector::hoverLeaveEvent(QGraphicsSceneHoverEvent*) { void MetatileSelector::hoverLeaveEvent(QGraphicsSceneHoverEvent*) {
emit this->hoveredMetatileSelectionCleared(); emit this->hoveredMetatileSelectionCleared();
this->cellPos = QPoint(-1, -1);
} }
void MetatileSelector::updateSelectedMetatiles() { void MetatileSelector::updateSelectedMetatiles() {
@ -182,7 +196,7 @@ void MetatileSelector::updateExternalSelectedMetatiles() {
emit selectedMetatilesChanged(); 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; int index = y * this->numMetatilesWide + x;
if (index < this->primaryTileset->metatiles.length()) { if (index < this->primaryTileset->metatiles.length()) {
return static_cast<uint16_t>(index); return static_cast<uint16_t>(index);