Fix 'getBlock' functions use for tiles, add getTilePixels to API

This commit is contained in:
GriffinR 2022-01-03 21:35:15 -05:00 committed by huderlem
parent 6f872b7d96
commit 1abdb301c1
10 changed files with 78 additions and 34 deletions

View file

@ -44,7 +44,7 @@ public:
uint8_t terrainType; // FRLG only uint8_t terrainType; // FRLG only
QString label; QString label;
static int getBlockIndex(int); static int getIndexInTileset(int);
static QPoint coordFromPixmapCoord(const QPointF &pixelCoord); static QPoint coordFromPixmapCoord(const QPointF &pixelCoord);
}; };

View file

@ -6,25 +6,16 @@
class Tile class Tile
{ {
public: public:
Tile() : Tile();
tileId(0), Tile(int tileId, bool xflip, bool yflip, int palette);
xflip(false),
yflip(false),
palette(0)
{ }
Tile(int tileId, bool xflip, bool yflip, int palette) :
tileId(tileId),
xflip(xflip),
yflip(yflip),
palette(palette)
{ }
public: public:
int tileId; int tileId;
bool xflip; bool xflip;
bool yflip; bool yflip;
int palette; int palette;
static int getIndexInTileset(int);
}; };
#endif // TILE_H #endif // TILE_H

View file

@ -34,7 +34,8 @@ public:
QList<QList<QRgb>> palettes; QList<QList<QRgb>> palettes;
QList<QList<QRgb>> palettePreviews; QList<QList<QRgb>> palettePreviews;
static Tileset* getBlockTileset(int, Tileset*, Tileset*); static Tileset* getMetatileTileset(int, Tileset*, Tileset*);
static Tileset* getTileTileset(int, Tileset*, Tileset*);
static Metatile* getMetatile(int, Tileset*, Tileset*); static Metatile* getMetatile(int, Tileset*, Tileset*);
static QList<QList<QRgb>> getBlockPalettes(Tileset*, Tileset*, bool useTruePalettes = false); static QList<QList<QRgb>> getBlockPalettes(Tileset*, Tileset*, bool useTruePalettes = false);
static QList<QRgb> getPalette(int, Tileset*, Tileset*, bool useTruePalettes = false); static QList<QRgb> getPalette(int, Tileset*, Tileset*, bool useTruePalettes = false);

View file

@ -173,6 +173,7 @@ public:
Q_INVOKABLE QJSValue getMetatileTiles(int metatileId, int tileStart = 0, int tileEnd = -1); Q_INVOKABLE QJSValue getMetatileTiles(int metatileId, int tileStart = 0, int tileEnd = -1);
Q_INVOKABLE void setMetatileTiles(int metatileId, QJSValue tilesObj, int tileStart = 0, int tileEnd = -1, bool forceRedraw = true); Q_INVOKABLE void setMetatileTiles(int metatileId, QJSValue tilesObj, int tileStart = 0, int tileEnd = -1, bool forceRedraw = true);
Q_INVOKABLE void setMetatileTiles(int metatileId, int tileId, bool xflip, bool yflip, int palette, int tileStart = 0, int tileEnd = -1, bool forceRedraw = true); Q_INVOKABLE void setMetatileTiles(int metatileId, int tileId, bool xflip, bool yflip, int palette, int tileStart = 0, int tileEnd = -1, bool forceRedraw = true);
Q_INVOKABLE QJSValue getTilePixels(int tileId);
Q_INVOKABLE int getNumTilesInMetatile(); Q_INVOKABLE int getNumTilesInMetatile();
Q_INVOKABLE int getNumMetatileLayers(); Q_INVOKABLE int getNumMetatileLayers();
Q_INVOKABLE QString getBaseGameVersion(); Q_INVOKABLE QString getBaseGameVersion();

View file

@ -27,6 +27,7 @@ SOURCES += src/core/block.cpp \
src/core/metatileparser.cpp \ src/core/metatileparser.cpp \
src/core/paletteutil.cpp \ src/core/paletteutil.cpp \
src/core/parseutil.cpp \ src/core/parseutil.cpp \
src/core/tile.cpp \
src/core/tileset.cpp \ src/core/tileset.cpp \
src/core/regionmap.cpp \ src/core/regionmap.cpp \
src/core/wildmoninfo.cpp \ src/core/wildmoninfo.cpp \

View file

@ -9,11 +9,11 @@ Metatile::Metatile() :
terrainType(0) terrainType(0)
{ } { }
int Metatile::getBlockIndex(int index) { int Metatile::getIndexInTileset(int metatileId) {
if (index < Project::getNumMetatilesPrimary()) { if (metatileId < Project::getNumMetatilesPrimary()) {
return index; return metatileId;
} else { } else {
return index - Project::getNumMetatilesPrimary(); return metatileId - Project::getNumMetatilesPrimary();
} }
} }

24
src/core/tile.cpp Normal file
View file

@ -0,0 +1,24 @@
#include "tile.h"
#include "project.h"
Tile::Tile() :
tileId(0),
xflip(false),
yflip(false),
palette(0)
{ }
Tile::Tile(int tileId, bool xflip, bool yflip, int palette) :
tileId(tileId),
xflip(xflip),
yflip(yflip),
palette(palette)
{ }
int Tile::getIndexInTileset(int tileId) {
if (tileId < Project::getNumTilesPrimary()) {
return tileId;
} else {
return tileId - Project::getNumTilesPrimary();
}
}

View file

@ -59,21 +59,33 @@ Tileset &Tileset::operator=(const Tileset &other) {
return *this; return *this;
} }
Tileset* Tileset::getBlockTileset(int metatile_index, Tileset *primaryTileset, Tileset *secondaryTileset) { Tileset* Tileset::getTileTileset(int tileId, Tileset *primaryTileset, Tileset *secondaryTileset) {
if (metatile_index < Project::getNumMetatilesPrimary()) { if (tileId < Project::getNumTilesPrimary()) {
return primaryTileset; return primaryTileset;
} else { } else if (tileId < Project::getNumTilesTotal()) {
return secondaryTileset; return secondaryTileset;
} else {
return nullptr;
} }
} }
Metatile* Tileset::getMetatile(int index, Tileset *primaryTileset, Tileset *secondaryTileset) { Tileset* Tileset::getMetatileTileset(int metatileId, Tileset *primaryTileset, Tileset *secondaryTileset) {
Tileset *tileset = Tileset::getBlockTileset(index, primaryTileset, secondaryTileset); if (metatileId < Project::getNumMetatilesPrimary()) {
int local_index = Metatile::getBlockIndex(index); return primaryTileset;
} else if (metatileId < Project::getNumMetatilesTotal()) {
return secondaryTileset;
} else {
return nullptr;
}
}
Metatile* Tileset::getMetatile(int metatileId, Tileset *primaryTileset, Tileset *secondaryTileset) {
Tileset *tileset = Tileset::getMetatileTileset(metatileId, primaryTileset, secondaryTileset);
int index = Metatile::getIndexInTileset(metatileId);
if (!tileset) { if (!tileset) {
return nullptr; return nullptr;
} }
return tileset->metatiles.value(local_index, nullptr); return tileset->metatiles.value(index, nullptr);
} }
bool Tileset::metatileIsValid(uint16_t metatileId, Tileset *primaryTileset, Tileset *secondaryTileset) { bool Tileset::metatileIsValid(uint16_t metatileId, Tileset *primaryTileset, Tileset *secondaryTileset) {

View file

@ -812,8 +812,8 @@ void MainWindow::setMetatileLayerOpacity(QList<float> order) {
} }
void MainWindow::saveMetatilesByMetatileId(int metatileId) { void MainWindow::saveMetatilesByMetatileId(int metatileId) {
Tileset * tileset = Tileset::getBlockTileset(metatileId, this->editor->map->layout->tileset_primary, this->editor->map->layout->tileset_secondary); Tileset * tileset = Tileset::getMetatileTileset(metatileId, this->editor->map->layout->tileset_primary, this->editor->map->layout->tileset_secondary);
if (this->editor->project) if (this->editor->project && tileset)
this->editor->project->saveTilesetMetatiles(tileset); this->editor->project->saveTilesetMetatiles(tileset);
// Refresh anything that can display metatiles (except the actual map view) // Refresh anything that can display metatiles (except the actual map view)
@ -828,8 +828,8 @@ void MainWindow::saveMetatilesByMetatileId(int metatileId) {
} }
void MainWindow::saveMetatileAttributesByMetatileId(int metatileId) { void MainWindow::saveMetatileAttributesByMetatileId(int metatileId) {
Tileset * tileset = Tileset::getBlockTileset(metatileId, this->editor->map->layout->tileset_primary, this->editor->map->layout->tileset_secondary); Tileset * tileset = Tileset::getMetatileTileset(metatileId, this->editor->map->layout->tileset_primary, this->editor->map->layout->tileset_secondary);
if (this->editor->project) if (this->editor->project && tileset)
this->editor->project->saveTilesetMetatileAttributes(tileset); this->editor->project->saveTilesetMetatileAttributes(tileset);
// If the Tileset Editor is currently displaying the updated metatile, refresh it // If the Tileset Editor is currently displaying the updated metatile, refresh it
@ -1008,6 +1008,20 @@ void MainWindow::setMetatileTile(int metatileId, int tileIndex, QJSValue tileObj
this->setMetatileTiles(metatileId, tile.tileId, tile.xflip, tile.yflip, tile.palette, tileIndex, tileIndex, forceRedraw); this->setMetatileTiles(metatileId, tile.tileId, tile.xflip, tile.yflip, tile.palette, tileIndex, tileIndex, forceRedraw);
} }
QJSValue MainWindow::getTilePixels(int tileId) {
if (tileId < 0 || !this->editor || !this->editor->project || !this->editor->map || !this->editor->map->layout)
return QJSValue();
QImage tileImage = getTileImage(tileId, this->editor->map->layout->tileset_primary, this->editor->map->layout->tileset_secondary);
if (tileImage.isNull() || tileImage.sizeInBytes() < 64)
return QJSValue();
const uchar * pixels = tileImage.constBits();
QJSValue pixelArray = Scripting::getEngine()->newArray(64);
for (int i = 0; i < 64; i++) {
pixelArray.setProperty(i, pixels[i]);
}
return pixelArray;
}
int MainWindow::getNumTilesInMetatile() { int MainWindow::getNumTilesInMetatile() {
return projectConfig.getTripleLayerMetatilesEnabled() ? 12 : 8; return projectConfig.getTripleLayerMetatilesEnabled() ? 12 : 8;
} }

View file

@ -23,7 +23,7 @@ QImage getMetatileImage(
bool useTruePalettes) bool useTruePalettes)
{ {
Metatile* metatile = Tileset::getMetatile(metatileId, primaryTileset, secondaryTileset); Metatile* metatile = Tileset::getMetatile(metatileId, primaryTileset, secondaryTileset);
Tileset* blockTileset = Tileset::getBlockTileset(metatileId, primaryTileset, secondaryTileset); Tileset* blockTileset = Tileset::getMetatileTileset(metatileId, primaryTileset, secondaryTileset);
if (!metatile || !blockTileset) { if (!metatile || !blockTileset) {
QImage metatile_image(16, 16, QImage::Format_RGBA8888); QImage metatile_image(16, 16, QImage::Format_RGBA8888);
metatile_image.fill(Qt::magenta); metatile_image.fill(Qt::magenta);
@ -106,12 +106,12 @@ QImage getMetatileImage(
} }
QImage getTileImage(uint16_t tileId, Tileset *primaryTileset, Tileset *secondaryTileset) { QImage getTileImage(uint16_t tileId, Tileset *primaryTileset, Tileset *secondaryTileset) {
Tileset *tileset = Tileset::getBlockTileset(tileId, primaryTileset, secondaryTileset); Tileset *tileset = Tileset::getTileTileset(tileId, primaryTileset, secondaryTileset);
int local_index = Metatile::getBlockIndex(tileId); int index = Tile::getIndexInTileset(tileId);
if (!tileset) { if (!tileset) {
return QImage(); return QImage();
} }
return tileset->tiles.value(local_index, QImage()); return tileset->tiles.value(index, QImage());
} }
QImage getColoredTileImage(uint16_t tileId, Tileset *primaryTileset, Tileset *secondaryTileset, QList<QRgb> palette) { QImage getColoredTileImage(uint16_t tileId, Tileset *primaryTileset, Tileset *secondaryTileset, QList<QRgb> palette) {