Disable invalid metatile selection in tileset editor

This commit is contained in:
GriffinR 2020-06-29 18:07:26 -04:00 committed by huderlem
parent 2e29e19605
commit 1747a4bf2e
2 changed files with 19 additions and 14 deletions

View file

@ -33,7 +33,7 @@ private:
void updateSelectedMetatile();
uint16_t getMetatileId(int x, int y);
QPoint getMetatileIdCoords(uint16_t);
uint16_t getValidMetatileId(uint16_t);
bool shouldAcceptEvent(QGraphicsSceneMouseEvent*);
signals:
void hoveredMetatileChanged(uint16_t);

View file

@ -16,6 +16,7 @@ void TilesetEditorMetatileSelector::draw() {
height_++;
}
QImage image(this->numMetatilesWide * 32, height_ * 32, QImage::Format_RGBA8888);
image.fill(Qt::magenta);
QPainter painter(&image);
for (int i = 0; i < length_; i++) {
int tile = i;
@ -35,7 +36,7 @@ void TilesetEditorMetatileSelector::draw() {
}
void TilesetEditorMetatileSelector::select(uint16_t metatileId) {
metatileId = this->getValidMetatileId(metatileId);
if (!Tileset::metatileIsValid(metatileId, this->primaryTileset, this->secondaryTileset)) return;
QPoint coords = this->getMetatileIdCoords(metatileId);
SelectablePixmapItem::select(coords.x(), coords.y(), 0, 0);
this->selectedMetatile = metatileId;
@ -50,7 +51,9 @@ void TilesetEditorMetatileSelector::setTilesets(Tileset *primaryTileset, Tileset
void TilesetEditorMetatileSelector::updateSelectedMetatile() {
QPoint origin = this->getSelectionStart();
this->selectedMetatile = this->getValidMetatileId(this->getMetatileId(origin.x(), origin.y()));
uint16_t metatileId = this->getMetatileId(origin.x(), origin.y());
if (Tileset::metatileIsValid(metatileId, this->primaryTileset, this->secondaryTileset))
this->selectedMetatile = metatileId;
}
uint16_t TilesetEditorMetatileSelector::getSelectedMetatile() {
@ -66,13 +69,20 @@ uint16_t TilesetEditorMetatileSelector::getMetatileId(int x, int y) {
}
}
bool TilesetEditorMetatileSelector::shouldAcceptEvent(QGraphicsSceneMouseEvent *event) {
QPoint pos = this->getCellPos(event->pos());
return Tileset::metatileIsValid(getMetatileId(pos.x(), pos.y()), this->primaryTileset, this->secondaryTileset);
}
void TilesetEditorMetatileSelector::mousePressEvent(QGraphicsSceneMouseEvent *event) {
if (!shouldAcceptEvent(event)) return;
SelectablePixmapItem::mousePressEvent(event);
this->updateSelectedMetatile();
emit selectedMetatileChanged(this->selectedMetatile);
}
void TilesetEditorMetatileSelector::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
if (!shouldAcceptEvent(event)) return;
SelectablePixmapItem::mouseMoveEvent(event);
this->updateSelectedMetatile();
emit hoveredMetatileChanged(this->selectedMetatile);
@ -80,6 +90,7 @@ void TilesetEditorMetatileSelector::mouseMoveEvent(QGraphicsSceneMouseEvent *eve
}
void TilesetEditorMetatileSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
if (!shouldAcceptEvent(event)) return;
SelectablePixmapItem::mouseReleaseEvent(event);
this->updateSelectedMetatile();
emit selectedMetatileChanged(this->selectedMetatile);
@ -96,19 +107,13 @@ void TilesetEditorMetatileSelector::hoverLeaveEvent(QGraphicsSceneHoverEvent*) {
}
QPoint TilesetEditorMetatileSelector::getMetatileIdCoords(uint16_t metatileId) {
if (!Tileset::metatileIsValid(metatileId, this->primaryTileset, this->secondaryTileset))
{
// Invalid metatile id.
return QPoint(0, 0);
}
int index = metatileId < Project::getNumMetatilesPrimary()
? metatileId
: metatileId - Project::getNumMetatilesPrimary() + this->primaryTileset->metatiles->length();
return QPoint(index % this->numMetatilesWide, index / this->numMetatilesWide);
}
uint16_t TilesetEditorMetatileSelector::getValidMetatileId(uint16_t metatileId) {
if (metatileId >= Project::getNumMetatilesTotal()
|| (metatileId < Project::getNumMetatilesPrimary() && metatileId >= this->primaryTileset->metatiles->length())
|| (metatileId < Project::getNumMetatilesTotal() && metatileId >= Project::getNumMetatilesPrimary() + this->secondaryTileset->metatiles->length()))
{
return 0;
}
return metatileId;
}