do not allow selection of invalid metatiles

- also display invalid metatiles as magenta to stand out more
This commit is contained in:
garakmon 2020-04-09 15:12:52 -04:00 committed by huderlem
parent 3a52c8680c
commit ef5ba968b1
5 changed files with 27 additions and 5 deletions

View file

@ -35,6 +35,7 @@ public:
static Metatile* getMetatile(int, Tileset*, Tileset*);
static QList<QList<QRgb>> getBlockPalettes(Tileset*, Tileset*);
static QList<QRgb> getPalette(int, Tileset*, Tileset*);
static bool metatileIsValid(uint16_t metatileId, Tileset *, Tileset *);
bool appendToHeaders(QString headerFile, QString friendlyName);
bool appendToGraphics(QString graphicsFile, QString friendlyName, bool primary);

View file

@ -48,6 +48,7 @@ private:
void updateSelectedMetatiles();
uint16_t getMetatileId(int x, int y);
QPoint getMetatileIdCoords(uint16_t);
bool shouldAcceptEvent(QGraphicsSceneMouseEvent*);
signals:
void hoveredMetatileSelectionChanged(uint16_t);

View file

@ -66,6 +66,19 @@ Metatile* Tileset::getMetatile(int index, Tileset *primaryTileset, Tileset *seco
return metatile;
}
bool Tileset::metatileIsValid(uint16_t metatileId, Tileset *primaryTileset, Tileset *secondaryTileset) {
if (metatileId >= Project::getNumMetatilesTotal())
return false;
if (metatileId < Project::getNumMetatilesPrimary() && metatileId >= primaryTileset->metatiles->length())
return false;
if (metatileId < Project::getNumMetatilesTotal() && metatileId >= Project::getNumMetatilesPrimary() + secondaryTileset->metatiles->length())
return false;
return true;
}
QList<QList<QRgb>> Tileset::getBlockPalettes(Tileset *primaryTileset, Tileset *secondaryTileset) {
QList<QList<QRgb>> palettes;
for (int i = 0; i < Project::getNumPalettesPrimary(); i++) {

View file

@ -18,13 +18,13 @@ QImage getMetatileImage(uint16_t tile, Tileset *primaryTileset, Tileset *seconda
Metatile* metatile = Tileset::getMetatile(tile, primaryTileset, secondaryTileset);
if (!metatile || !metatile->tiles) {
metatile_image.fill(0xffffffff);
metatile_image.fill(Qt::magenta);
return metatile_image;
}
Tileset* blockTileset = Tileset::getBlockTileset(tile, primaryTileset, secondaryTileset);
if (!blockTileset) {
metatile_image.fill(0xffffffff);
metatile_image.fill(Qt::magenta);
return metatile_image;
}
QList<QList<QRgb>> palettes = Tileset::getBlockPalettes(primaryTileset, secondaryTileset);

View file

@ -24,6 +24,7 @@ void MetatileSelector::draw() {
height_++;
}
QImage image(this->numMetatilesWide * 16, height_ * 16, QImage::Format_RGBA8888);
image.fill(Qt::magenta);
QPainter painter(&image);
for (int i = 0; i < length_; i++) {
int tile = i;
@ -91,13 +92,20 @@ void MetatileSelector::setExternalSelection(int width, int height, QList<uint16_
emit selectedMetatilesChanged();
}
bool MetatileSelector::shouldAcceptEvent(QGraphicsSceneMouseEvent *event) {
QPoint pos = this->getCellPos(event->pos());
return Tileset::metatileIsValid(getMetatileId(pos.x(), pos.y()), this->primaryTileset, this->secondaryTileset);
}
void MetatileSelector::mousePressEvent(QGraphicsSceneMouseEvent *event) {
if (!shouldAcceptEvent(event)) return;
SelectablePixmapItem::mousePressEvent(event);
this->updateSelectedMetatiles();
emit selectedMetatilesChanged();
}
void MetatileSelector::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
if (!shouldAcceptEvent(event)) return;
SelectablePixmapItem::mouseMoveEvent(event);
this->updateSelectedMetatiles();
@ -108,6 +116,7 @@ void MetatileSelector::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
}
void MetatileSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
if (!shouldAcceptEvent(event)) return;
SelectablePixmapItem::mouseReleaseEvent(event);
this->updateSelectedMetatiles();
emit selectedMetatilesChanged();
@ -147,9 +156,7 @@ uint16_t MetatileSelector::getMetatileId(int x, int y) {
}
QPoint MetatileSelector::getMetatileIdCoords(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()))
if (!Tileset::metatileIsValid(metatileId, this->primaryTileset, this->secondaryTileset))
{
// Invalid metatile id.
return QPoint(0, 0);