From 1abdb301c1946785669078aa1b9325ce59abb538 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Mon, 3 Jan 2022 21:35:15 -0500 Subject: [PATCH] Fix 'getBlock' functions use for tiles, add getTilePixels to API --- include/core/metatile.h | 2 +- include/core/tile.h | 17 ++++------------- include/core/tileset.h | 3 ++- include/mainwindow.h | 1 + porymap.pro | 1 + src/core/metatile.cpp | 8 ++++---- src/core/tile.cpp | 24 ++++++++++++++++++++++++ src/core/tileset.cpp | 26 +++++++++++++++++++------- src/mainwindow_scriptapi.cpp | 22 ++++++++++++++++++---- src/ui/imageproviders.cpp | 8 ++++---- 10 files changed, 78 insertions(+), 34 deletions(-) create mode 100644 src/core/tile.cpp diff --git a/include/core/metatile.h b/include/core/metatile.h index 0b2af276..e258087f 100644 --- a/include/core/metatile.h +++ b/include/core/metatile.h @@ -44,7 +44,7 @@ public: uint8_t terrainType; // FRLG only QString label; - static int getBlockIndex(int); + static int getIndexInTileset(int); static QPoint coordFromPixmapCoord(const QPointF &pixelCoord); }; diff --git a/include/core/tile.h b/include/core/tile.h index dff5cd58..8d225c50 100644 --- a/include/core/tile.h +++ b/include/core/tile.h @@ -6,25 +6,16 @@ class Tile { public: - Tile() : - tileId(0), - xflip(false), - yflip(false), - palette(0) - { } - - Tile(int tileId, bool xflip, bool yflip, int palette) : - tileId(tileId), - xflip(xflip), - yflip(yflip), - palette(palette) - { } + Tile(); + Tile(int tileId, bool xflip, bool yflip, int palette); public: int tileId; bool xflip; bool yflip; int palette; + + static int getIndexInTileset(int); }; #endif // TILE_H diff --git a/include/core/tileset.h b/include/core/tileset.h index 9a02aefc..ea89c602 100644 --- a/include/core/tileset.h +++ b/include/core/tileset.h @@ -34,7 +34,8 @@ public: QList> palettes; QList> 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 QList> getBlockPalettes(Tileset*, Tileset*, bool useTruePalettes = false); static QList getPalette(int, Tileset*, Tileset*, bool useTruePalettes = false); diff --git a/include/mainwindow.h b/include/mainwindow.h index 57449f6f..043d421e 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -173,6 +173,7 @@ public: 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, 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 getNumMetatileLayers(); Q_INVOKABLE QString getBaseGameVersion(); diff --git a/porymap.pro b/porymap.pro index b6863c3c..b73c4ee6 100644 --- a/porymap.pro +++ b/porymap.pro @@ -27,6 +27,7 @@ SOURCES += src/core/block.cpp \ src/core/metatileparser.cpp \ src/core/paletteutil.cpp \ src/core/parseutil.cpp \ + src/core/tile.cpp \ src/core/tileset.cpp \ src/core/regionmap.cpp \ src/core/wildmoninfo.cpp \ diff --git a/src/core/metatile.cpp b/src/core/metatile.cpp index 4c97418d..b6f25905 100644 --- a/src/core/metatile.cpp +++ b/src/core/metatile.cpp @@ -9,11 +9,11 @@ Metatile::Metatile() : terrainType(0) { } -int Metatile::getBlockIndex(int index) { - if (index < Project::getNumMetatilesPrimary()) { - return index; +int Metatile::getIndexInTileset(int metatileId) { + if (metatileId < Project::getNumMetatilesPrimary()) { + return metatileId; } else { - return index - Project::getNumMetatilesPrimary(); + return metatileId - Project::getNumMetatilesPrimary(); } } diff --git a/src/core/tile.cpp b/src/core/tile.cpp new file mode 100644 index 00000000..e36ebb37 --- /dev/null +++ b/src/core/tile.cpp @@ -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(); + } +} diff --git a/src/core/tileset.cpp b/src/core/tileset.cpp index a119efd3..1a93f66d 100644 --- a/src/core/tileset.cpp +++ b/src/core/tileset.cpp @@ -59,21 +59,33 @@ Tileset &Tileset::operator=(const Tileset &other) { return *this; } -Tileset* Tileset::getBlockTileset(int metatile_index, Tileset *primaryTileset, Tileset *secondaryTileset) { - if (metatile_index < Project::getNumMetatilesPrimary()) { +Tileset* Tileset::getTileTileset(int tileId, Tileset *primaryTileset, Tileset *secondaryTileset) { + if (tileId < Project::getNumTilesPrimary()) { return primaryTileset; - } else { + } else if (tileId < Project::getNumTilesTotal()) { return secondaryTileset; + } else { + return nullptr; } } -Metatile* Tileset::getMetatile(int index, Tileset *primaryTileset, Tileset *secondaryTileset) { - Tileset *tileset = Tileset::getBlockTileset(index, primaryTileset, secondaryTileset); - int local_index = Metatile::getBlockIndex(index); +Tileset* Tileset::getMetatileTileset(int metatileId, Tileset *primaryTileset, Tileset *secondaryTileset) { + if (metatileId < Project::getNumMetatilesPrimary()) { + 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) { 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) { diff --git a/src/mainwindow_scriptapi.cpp b/src/mainwindow_scriptapi.cpp index 27d1c3bb..95dff4d4 100644 --- a/src/mainwindow_scriptapi.cpp +++ b/src/mainwindow_scriptapi.cpp @@ -812,8 +812,8 @@ void MainWindow::setMetatileLayerOpacity(QList order) { } void MainWindow::saveMetatilesByMetatileId(int metatileId) { - Tileset * tileset = Tileset::getBlockTileset(metatileId, this->editor->map->layout->tileset_primary, this->editor->map->layout->tileset_secondary); - if (this->editor->project) + Tileset * tileset = Tileset::getMetatileTileset(metatileId, this->editor->map->layout->tileset_primary, this->editor->map->layout->tileset_secondary); + if (this->editor->project && tileset) this->editor->project->saveTilesetMetatiles(tileset); // 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) { - Tileset * tileset = Tileset::getBlockTileset(metatileId, this->editor->map->layout->tileset_primary, this->editor->map->layout->tileset_secondary); - if (this->editor->project) + Tileset * tileset = Tileset::getMetatileTileset(metatileId, this->editor->map->layout->tileset_primary, this->editor->map->layout->tileset_secondary); + if (this->editor->project && tileset) this->editor->project->saveTilesetMetatileAttributes(tileset); // 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); } +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() { return projectConfig.getTripleLayerMetatilesEnabled() ? 12 : 8; } diff --git a/src/ui/imageproviders.cpp b/src/ui/imageproviders.cpp index 827e129b..8254ece5 100644 --- a/src/ui/imageproviders.cpp +++ b/src/ui/imageproviders.cpp @@ -23,7 +23,7 @@ QImage getMetatileImage( bool useTruePalettes) { Metatile* metatile = Tileset::getMetatile(metatileId, primaryTileset, secondaryTileset); - Tileset* blockTileset = Tileset::getBlockTileset(metatileId, primaryTileset, secondaryTileset); + Tileset* blockTileset = Tileset::getMetatileTileset(metatileId, primaryTileset, secondaryTileset); if (!metatile || !blockTileset) { QImage metatile_image(16, 16, QImage::Format_RGBA8888); metatile_image.fill(Qt::magenta); @@ -106,12 +106,12 @@ QImage getMetatileImage( } QImage getTileImage(uint16_t tileId, Tileset *primaryTileset, Tileset *secondaryTileset) { - Tileset *tileset = Tileset::getBlockTileset(tileId, primaryTileset, secondaryTileset); - int local_index = Metatile::getBlockIndex(tileId); + Tileset *tileset = Tileset::getTileTileset(tileId, primaryTileset, secondaryTileset); + int index = Tile::getIndexInTileset(tileId); if (!tileset) { 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 palette) {