Skip to content

Commit

Permalink
Reduce redundant work in metatile selector
Browse files Browse the repository at this point in the history
  • Loading branch information
GriffinRichards committed Jan 5, 2024
1 parent 8c85209 commit 941174d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
7 changes: 5 additions & 2 deletions include/ui/metatileselector.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class MetatileSelector: public SelectablePixmapItem {
this->primaryTileset = map->layout->tileset_primary;
this->secondaryTileset = map->layout->tileset_secondary;
this->selection = MetatileSelection{};
this->cellPos = QPoint(-1, -1);
setAcceptHoverEvents(true);
}
QPoint getSelectionDimensions();
Expand Down Expand Up @@ -68,13 +69,15 @@ class MetatileSelector: public SelectablePixmapItem {
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);
Expand Down
38 changes: 26 additions & 12 deletions src/ui/metatileselector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 941174d

Please sign in to comment.