Fix exported metatile image scaling, remove hardcoded cell sizes

This commit is contained in:
GriffinR 2025-06-26 14:07:04 -04:00
parent bbe34e4983
commit 56d1a0d570
11 changed files with 62 additions and 59 deletions

View File

@ -6,7 +6,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>733</width> <width>748</width>
<height>784</height> <height>784</height>
</rect> </rect>
</property> </property>
@ -267,16 +267,10 @@
<verstretch>0</verstretch> <verstretch>0</verstretch>
</sizepolicy> </sizepolicy>
</property> </property>
<property name="minimumSize">
<size>
<width>66</width>
<height>34</height>
</size>
</property>
<property name="maximumSize"> <property name="maximumSize">
<size> <size>
<width>96</width> <width>1</width>
<height>34</height> <height>1</height>
</size> </size>
</property> </property>
<property name="verticalScrollBarPolicy"> <property name="verticalScrollBarPolicy">
@ -561,8 +555,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>445</width> <width>499</width>
<height>237</height> <height>241</height>
</rect> </rect>
</property> </property>
<layout class="QGridLayout" name="gridLayout_2"> <layout class="QGridLayout" name="gridLayout_2">
@ -623,7 +617,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>733</width> <width>748</width>
<height>37</height> <height>37</height>
</rect> </rect>
</property> </property>

View File

@ -82,7 +82,7 @@ public:
this->collisionOpacity = 50; this->collisionOpacity = 50;
this->collisionZoom = 30; this->collisionZoom = 30;
this->metatilesZoom = 30; this->metatilesZoom = 30;
this->tilesetEditorMetatilesZoom = 30; this->tilesetEditorMetatilesZoom = 45;
this->tilesetEditorTilesZoom = 30; this->tilesetEditorTilesZoom = 30;
this->showPlayerView = false; this->showPlayerView = false;
this->showCursorTile = true; this->showCursorTile = true;

View File

@ -9,14 +9,7 @@
class MetatileLayersItem: public SelectablePixmapItem { class MetatileLayersItem: public SelectablePixmapItem {
Q_OBJECT Q_OBJECT
public: public:
MetatileLayersItem(Metatile *metatile, Tileset *primaryTileset, Tileset *secondaryTileset): SelectablePixmapItem(16, 16, 6, 2) { MetatileLayersItem(Metatile *metatile, Tileset *primaryTileset, Tileset *secondaryTileset);
this->metatile = metatile;
this->primaryTileset = primaryTileset;
this->secondaryTileset = secondaryTileset;
this->clearLastModifiedCoords();
this->clearLastHoveredCoords();
setAcceptHoverEvents(true);
}
void draw(); void draw();
void setTilesets(Tileset*, Tileset*); void setTilesets(Tileset*, Tileset*);
void setMetatile(Metatile*); void setMetatile(Metatile*);

View File

@ -136,7 +136,7 @@ public:
this->palette = PaletteUtil::parse(palFilepath, &err); this->palette = PaletteUtil::parse(palFilepath, &err);
} }
this->setPixmap(QPixmap::fromImage(this->tileset)); this->setPixmap(QPixmap::fromImage(this->tileset));
this->numTilesWide = this->tileset.width() / 8; this->numTilesWide = this->tileset.width() / this->cellWidth;
this->selectedTile = 0x00; this->selectedTile = 0x00;
setAcceptHoverEvents(true); setAcceptHoverEvents(true);
} }

View File

@ -3,6 +3,17 @@
#include "imageproviders.h" #include "imageproviders.h"
#include <QPainter> #include <QPainter>
MetatileLayersItem::MetatileLayersItem(Metatile *metatile, Tileset *primaryTileset, Tileset *secondaryTileset)
: SelectablePixmapItem(16, 16, 2 * projectConfig.getNumLayersInMetatile(), 2),
metatile(metatile),
primaryTileset(primaryTileset),
secondaryTileset(secondaryTileset)
{
clearLastModifiedCoords();
clearLastHoveredCoords();
setAcceptHoverEvents(true);
}
static const QList<QPoint> tilePositions = { static const QList<QPoint> tilePositions = {
QPoint(0, 0), QPoint(0, 0),
QPoint(1, 0), QPoint(1, 0),
@ -20,23 +31,31 @@ static const QList<QPoint> tilePositions = {
void MetatileLayersItem::draw() { void MetatileLayersItem::draw() {
const int numLayers = projectConfig.getNumLayersInMetatile(); const int numLayers = projectConfig.getNumLayersInMetatile();
QPixmap pixmap(numLayers * 32, 32); const int layerWidth = this->cellWidth * 2;
const int layerHeight = this->cellHeight * 2;
QPixmap pixmap(numLayers * layerWidth, layerHeight);
QPainter painter(&pixmap); QPainter painter(&pixmap);
// Draw tile images // Draw tile images
int numTiles = qMin(projectConfig.getNumTilesInMetatile(), this->metatile ? this->metatile->tiles.length() : 0); int numTiles = qMin(projectConfig.getNumTilesInMetatile(), this->metatile ? this->metatile->tiles.length() : 0);
for (int i = 0; i < numTiles; i++) { for (int i = 0; i < numTiles; i++) {
Tile tile = this->metatile->tiles.at(i); Tile tile = this->metatile->tiles.at(i);
QImage tileImage = getPalettedTileImage(tile.tileId, this->primaryTileset, this->secondaryTileset, tile.palette, true).scaled(16, 16); QImage tileImage = getPalettedTileImage(tile.tileId,
this->primaryTileset,
this->secondaryTileset,
tile.palette,
true
).scaled(this->cellWidth, this->cellHeight);
tile.flip(&tileImage); tile.flip(&tileImage);
painter.drawImage(tilePositions.at(i) * 16, tileImage); QPoint pos = tilePositions.at(i);
painter.drawImage(pos.x() * this->cellWidth, pos.y() * this->cellHeight, tileImage);
} }
if (this->showGrid) { if (this->showGrid) {
// Draw grid // Draw grid
painter.setPen(Qt::white); painter.setPen(Qt::white);
for (int i = 1; i < numLayers; i++) { for (int i = 1; i < numLayers; i++) {
int x = i * 32; int x = i * layerWidth;
painter.drawLine(x, 0, x, 32); painter.drawLine(x, 0, x, layerHeight);
} }
} }
@ -127,13 +146,8 @@ void MetatileLayersItem::clearLastHoveredCoords() {
} }
QPoint MetatileLayersItem::getBoundedPos(const QPointF &pos) { QPoint MetatileLayersItem::getBoundedPos(const QPointF &pos) {
int x, y; int x = static_cast<int>(pos.x()) / this->cellWidth;
int maxX = (projectConfig.getNumLayersInMetatile() * 2) - 1; int y = static_cast<int>(pos.y()) / this->cellHeight;
x = static_cast<int>(pos.x()) / 16; return QPoint( qMax(0, qMin(x, this->maxSelectionWidth - 1)),
y = static_cast<int>(pos.y()) / 16; qMax(0, qMin(y, this->maxSelectionHeight - 1)) );
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x > maxX) x = maxX;
if (y > 1) y = 1;
return QPoint(x, y);
} }

View File

@ -21,7 +21,7 @@ void MetatileSelector::updateBasePixmap() {
if (length_ % this->numMetatilesWide != 0) { if (length_ % this->numMetatilesWide != 0) {
height_++; height_++;
} }
QImage image(this->numMetatilesWide * 16, height_ * 16, QImage::Format_RGBA8888); QImage image(this->numMetatilesWide * this->cellWidth, height_ * this->cellHeight, QImage::Format_RGBA8888);
image.fill(Qt::magenta); image.fill(Qt::magenta);
QPainter painter(&image); QPainter painter(&image);
for (int i = 0; i < length_; i++) { for (int i = 0; i < length_; i++) {
@ -32,7 +32,7 @@ void MetatileSelector::updateBasePixmap() {
QImage metatile_image = getMetatileImage(tile, this->primaryTileset, this->secondaryTileset, layout->metatileLayerOrder, layout->metatileLayerOpacity); QImage metatile_image = getMetatileImage(tile, this->primaryTileset, this->secondaryTileset, layout->metatileLayerOrder, layout->metatileLayerOpacity);
int map_y = i / this->numMetatilesWide; int map_y = i / this->numMetatilesWide;
int map_x = i % this->numMetatilesWide; int map_x = i % this->numMetatilesWide;
QPoint metatile_origin = QPoint(map_x * 16, map_y * 16); QPoint metatile_origin = QPoint(map_x * this->cellWidth, map_y * this->cellHeight);
painter.drawImage(metatile_origin, metatile_image); painter.drawImage(metatile_origin, metatile_image);
} }
painter.end(); painter.end();

View File

@ -17,12 +17,12 @@ void RegionMapEntriesPixmapItem::draw() {
entry_w = entry.width, entry_h = entry.height; entry_w = entry.width, entry_h = entry.height;
} }
QImage image(region_map->tilemapWidth() * 8, region_map->tilemapHeight() * 8, QImage::Format_RGBA8888); QImage image(region_map->tilemapWidth() * this->cellWidth, region_map->tilemapHeight() * this->cellHeight, QImage::Format_RGBA8888);
QPainter painter(&image); QPainter painter(&image);
for (int i = 0; i < region_map->tilemapSize(); i++) { for (int i = 0; i < region_map->tilemapSize(); i++) {
QImage bottom_img = this->tile_selector->tileImg(region_map->getTile(i)); QImage bottom_img = this->tile_selector->tileImg(region_map->getTile(i));
QImage top_img(8, 8, QImage::Format_RGBA8888); QImage top_img(this->cellWidth, this->cellHeight, QImage::Format_RGBA8888);
int x = i % region_map->tilemapWidth(); int x = i % region_map->tilemapWidth();
int y = i / region_map->tilemapWidth(); int y = i / region_map->tilemapWidth();
bool insideEntry = false; bool insideEntry = false;
@ -40,7 +40,7 @@ void RegionMapEntriesPixmapItem::draw() {
} else { } else {
top_img.fill(Qt::black); top_img.fill(Qt::black);
} }
QPoint pos = QPoint(x * 8, y * 8); QPoint pos = QPoint(x * this->cellWidth, y * this->cellHeight);
painter.setOpacity(1); painter.setOpacity(1);
painter.drawImage(pos, bottom_img); painter.drawImage(pos, bottom_img);
painter.save(); painter.save();

View File

@ -8,7 +8,7 @@ void RegionMapLayoutPixmapItem::draw() {
QPainter painter(&image); QPainter painter(&image);
for (int i = 0; i < region_map->tilemapSize(); i++) { for (int i = 0; i < region_map->tilemapSize(); i++) {
QImage bottom_img = this->tile_selector->tileImg(region_map->getTile(i)); QImage bottom_img = this->tile_selector->tileImg(region_map->getTile(i));
QImage top_img(8, 8, QImage::Format_RGBA8888); QImage top_img(this->cellWidth, this->cellHeight, QImage::Format_RGBA8888);
if (region_map->squareHasMap(i)) { if (region_map->squareHasMap(i)) {
top_img.fill(Qt::gray); top_img.fill(Qt::gray);
} else { } else {
@ -16,7 +16,7 @@ void RegionMapLayoutPixmapItem::draw() {
} }
int x = i % region_map->tilemapWidth(); int x = i % region_map->tilemapWidth();
int y = i / region_map->tilemapWidth(); int y = i / region_map->tilemapWidth();
QPoint pos = QPoint(x * 8, y * 8); QPoint pos = QPoint(x * this->cellWidth, y * this->cellHeight);
painter.setOpacity(1); painter.setOpacity(1);
painter.drawImage(pos, bottom_img); painter.drawImage(pos, bottom_img);
painter.save(); painter.save();

View File

@ -7,9 +7,9 @@ void TilemapTileSelector::draw() {
this->pixelWidth = width_; this->pixelWidth = width_;
size_t height_ = this->tileset.height(); size_t height_ = this->tileset.height();
this->pixelHeight = height_; this->pixelHeight = height_;
size_t ntiles_ = (width_/8) * (height_/8); size_t ntiles_ = (width_/this->cellWidth) * (height_/this->cellHeight);
this->numTilesWide = width_ / 8; this->numTilesWide = width_ / this->cellWidth;
this->numTiles = ntiles_; this->numTiles = ntiles_;
this->setPixmap(QPixmap::fromImage(this->setPalette(this->tile_palette))); this->setPixmap(QPixmap::fromImage(this->setPalette(this->tile_palette)));
@ -92,7 +92,7 @@ QImage TilemapTileSelector::tileImg(shared_ptr<TilemapTile> tile) {
QImage tilesetImage = setPalette(tile->palette()); QImage tilesetImage = setPalette(tile->palette());
// take a tile from the tileset // take a tile from the tileset
QImage img = tilesetImage.copy(pos.x() * 8, pos.y() * 8, 8, 8); QImage img = tilesetImage.copy(pos.x() * this->cellWidth, pos.y() * this->cellHeight, this->cellWidth, this->cellHeight);
// QImage::flip was introduced in 6.9.0 to replace QImage::mirrored. // QImage::flip was introduced in 6.9.0 to replace QImage::mirrored.
#if (QT_VERSION >= QT_VERSION_CHECK(6, 9, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(6, 9, 0))

View File

@ -4,7 +4,7 @@
#include <QPainter> #include <QPainter>
TilesetEditorMetatileSelector::TilesetEditorMetatileSelector(Tileset *primaryTileset, Tileset *secondaryTileset, Layout *layout) TilesetEditorMetatileSelector::TilesetEditorMetatileSelector(Tileset *primaryTileset, Tileset *secondaryTileset, Layout *layout)
: SelectablePixmapItem(32, 32, 1, 1) { : SelectablePixmapItem(16, 16, 1, 1) {
this->primaryTileset = primaryTileset; this->primaryTileset = primaryTileset;
this->secondaryTileset = secondaryTileset; this->secondaryTileset = secondaryTileset;
this->numMetatilesWide = 8; this->numMetatilesWide = 8;

View File

@ -22,27 +22,27 @@ void TilesetEditorTileSelector::draw() {
int secondaryLength = this->secondaryTileset->tiles.length(); int secondaryLength = this->secondaryTileset->tiles.length();
int height = totalTiles / this->numTilesWide; int height = totalTiles / this->numTilesWide;
QList<QRgb> palette = Tileset::getPalette(this->paletteId, this->primaryTileset, this->secondaryTileset, true); QList<QRgb> palette = Tileset::getPalette(this->paletteId, this->primaryTileset, this->secondaryTileset, true);
QImage image(this->numTilesWide * 16, height * 16, QImage::Format_RGBA8888); QImage image(this->numTilesWide * this->cellWidth, height * this->cellHeight, QImage::Format_RGBA8888);
QPainter painter(&image); QPainter painter(&image);
for (uint16_t tile = 0; tile < totalTiles; tile++) { for (uint16_t tile = 0; tile < totalTiles; tile++) {
QImage tileImage; QImage tileImage;
if (tile < primaryLength) { if (tile < primaryLength) {
tileImage = getPalettedTileImage(tile, this->primaryTileset, this->secondaryTileset, this->paletteId, true).scaled(16, 16); tileImage = getPalettedTileImage(tile, this->primaryTileset, this->secondaryTileset, this->paletteId, true).scaled(this->cellWidth, this->cellHeight);
} else if (tile < Project::getNumTilesPrimary()) { } else if (tile < Project::getNumTilesPrimary()) {
tileImage = QImage(16, 16, QImage::Format_RGBA8888); tileImage = QImage(this->cellWidth, this->cellHeight, QImage::Format_RGBA8888);
tileImage.fill(palette.at(0)); tileImage.fill(palette.at(0));
} else if (tile < Project::getNumTilesPrimary() + secondaryLength) { } else if (tile < Project::getNumTilesPrimary() + secondaryLength) {
tileImage = getPalettedTileImage(tile, this->primaryTileset, this->secondaryTileset, this->paletteId, true).scaled(16, 16); tileImage = getPalettedTileImage(tile, this->primaryTileset, this->secondaryTileset, this->paletteId, true).scaled(this->cellWidth, this->cellHeight);
} else { } else {
tileImage = QImage(16, 16, QImage::Format_RGBA8888); tileImage = QImage(this->cellWidth, this->cellHeight, QImage::Format_RGBA8888);
QPainter painter(&tileImage); QPainter painter(&tileImage);
painter.fillRect(0, 0, 16, 16, palette.at(0)); painter.fillRect(0, 0, this->cellWidth, this->cellHeight, palette.at(0));
} }
int y = tile / this->numTilesWide; int y = tile / this->numTilesWide;
int x = tile % this->numTilesWide; int x = tile % this->numTilesWide;
QPoint origin = QPoint(x * 16, y * 16); QPoint origin = QPoint(x * this->cellWidth, y * this->cellHeight);
painter.drawImage(origin, tileImage); painter.drawImage(origin, tileImage);
} }
@ -52,9 +52,9 @@ void TilesetEditorTileSelector::draw() {
// Round up height for incomplete last row // Round up height for incomplete last row
row++; row++;
} }
const int y = row * 16; const int y = row * this->cellHeight;
painter.setPen(Qt::white); painter.setPen(Qt::white);
painter.drawLine(0, y, this->numTilesWide * 16, y); painter.drawLine(0, y, this->numTilesWide * this->cellWidth, y);
} }
painter.end(); painter.end();
@ -244,8 +244,10 @@ QImage TilesetEditorTileSelector::buildSecondaryTilesIndexedImage() {
} }
QImage TilesetEditorTileSelector::buildImage(int tileIdStart, int numTiles) { QImage TilesetEditorTileSelector::buildImage(int tileIdStart, int numTiles) {
const int tileWidth = 8;
const int tileHeight = 8;
int height = qCeil(numTiles / static_cast<double>(this->numTilesWide)); int height = qCeil(numTiles / static_cast<double>(this->numTilesWide));
QImage image(this->numTilesWide * 8, height * 8, QImage::Format_RGBA8888); QImage image(this->numTilesWide * tileWidth, height * tileHeight, QImage::Format_RGBA8888);
image.fill(0); image.fill(0);
QPainter painter(&image); QPainter painter(&image);
@ -253,7 +255,7 @@ QImage TilesetEditorTileSelector::buildImage(int tileIdStart, int numTiles) {
QImage tileImage = getGreyscaleTileImage(tileIdStart + i, this->primaryTileset, this->secondaryTileset); QImage tileImage = getGreyscaleTileImage(tileIdStart + i, this->primaryTileset, this->secondaryTileset);
int y = i / this->numTilesWide; int y = i / this->numTilesWide;
int x = i % this->numTilesWide; int x = i % this->numTilesWide;
QPoint origin = QPoint(x * 8, y * 8); QPoint origin = QPoint(x * tileWidth, y * tileHeight);
painter.drawImage(origin, tileImage); painter.drawImage(origin, tileImage);
} }
painter.end(); painter.end();
@ -305,7 +307,7 @@ void TilesetEditorTileSelector::drawUnused() {
for (int tile = 0; tile < this->usedTiles.size(); tile++) { for (int tile = 0; tile < this->usedTiles.size(); tile++) {
if (!this->usedTiles[tile]) { if (!this->usedTiles[tile]) {
unusedPainter.drawPixmap((tile % 16) * 16, (tile / 16) * 16, redX); unusedPainter.drawPixmap((tile % this->cellWidth) * this->cellWidth, (tile / this->cellWidth) * this->cellHeight, redX);
} }
} }