From 1747a4bf2e9f9cfe8fa68584602e35ebbb9cc433 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Mon, 29 Jun 2020 18:07:26 -0400 Subject: [PATCH] Disable invalid metatile selection in tileset editor --- include/ui/tileseteditormetatileselector.h | 2 +- src/ui/tileseteditormetatileselector.cpp | 31 +++++++++++++--------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/include/ui/tileseteditormetatileselector.h b/include/ui/tileseteditormetatileselector.h index 3da82721..2bc8e250 100644 --- a/include/ui/tileseteditormetatileselector.h +++ b/include/ui/tileseteditormetatileselector.h @@ -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); diff --git a/src/ui/tileseteditormetatileselector.cpp b/src/ui/tileseteditormetatileselector.cpp index b5732c99..ba4cf84a 100644 --- a/src/ui/tileseteditormetatileselector.cpp +++ b/src/ui/tileseteditormetatileselector.cpp @@ -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; -} -