diff --git a/include/config.h b/include/config.h index 179c9cab..a8e9ffb8 100644 --- a/include/config.h +++ b/include/config.h @@ -238,6 +238,8 @@ public: bool getCreateMapTextFileEnabled(); void setTripleLayerMetatilesEnabled(bool enable); bool getTripleLayerMetatilesEnabled(); + int getNumLayersInMetatile(); + int getNumTilesInMetatile(); void setNewMapMetatileId(int metatileId); int getNewMapMetatileId(); void setNewMapElevation(int elevation); diff --git a/include/core/metatile.h b/include/core/metatile.h index ef8ce0eb..eb315ab8 100644 --- a/include/core/metatile.h +++ b/include/core/metatile.h @@ -36,6 +36,7 @@ public: Metatile(); Metatile(const Metatile &other) = default; Metatile &operator=(const Metatile &other) = default; + Metatile(const int numTiles); public: QList tiles; diff --git a/include/ui/tileseteditortileselector.h b/include/ui/tileseteditortileselector.h index 4d270a40..bfbc4946 100644 --- a/include/ui/tileseteditortileselector.h +++ b/include/ui/tileseteditortileselector.h @@ -7,8 +7,8 @@ class TilesetEditorTileSelector: public SelectablePixmapItem { Q_OBJECT public: - TilesetEditorTileSelector(Tileset *primaryTileset, Tileset *secondaryTileset, bool isTripleLayer) - : SelectablePixmapItem(16, 16, isTripleLayer ? 6 : 4, 2) { + TilesetEditorTileSelector(Tileset *primaryTileset, Tileset *secondaryTileset, int numLayers) + : SelectablePixmapItem(16, 16, numLayers * 2, 2) { this->primaryTileset = primaryTileset; this->secondaryTileset = secondaryTileset; this->numTilesWide = 16; diff --git a/src/config.cpp b/src/config.cpp index 9aff518d..671b84c6 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -795,6 +795,14 @@ bool ProjectConfig::getTripleLayerMetatilesEnabled() { return this->enableTripleLayerMetatiles; } +int ProjectConfig::getNumLayersInMetatile() { + return this->enableTripleLayerMetatiles ? 3 : 2; +} + +int ProjectConfig::getNumTilesInMetatile() { + return this->enableTripleLayerMetatiles ? 12 : 8; +} + void ProjectConfig::setNewMapMetatileId(int metatileId) { this->newMapMetatileId = metatileId; this->save(); diff --git a/src/core/metatile.cpp b/src/core/metatile.cpp index 99739a81..6264fda2 100644 --- a/src/core/metatile.cpp +++ b/src/core/metatile.cpp @@ -10,6 +10,19 @@ Metatile::Metatile() : unusedAttributes(0) { } +Metatile::Metatile(const int numTiles) : + behavior(0), + layerType(0), + encounterType(0), + terrainType(0), + unusedAttributes(0) +{ + Tile tile = Tile(); + for (int i = 0; i < numTiles; i++) { + this->tiles.append(tile); + } +} + int Metatile::getIndexInTileset(int metatileId) { if (metatileId < Project::getNumMetatilesPrimary()) { return metatileId; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index b3eeb023..ea9bade6 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1307,8 +1307,8 @@ void MainWindow::on_actionNew_Tileset_triggered() { int numMetaTiles = createTilesetDialog->isSecondary ? (Project::getNumTilesTotal() - Project::getNumTilesPrimary()) : Project::getNumTilesPrimary(); QImage tilesImage(":/images/blank_tileset.png"); editor->project->loadTilesetTiles(&newSet, tilesImage); + int tilesPerMetatile = projectConfig.getNumTilesInMetatile(); for(int i = 0; i < numMetaTiles; ++i) { - int tilesPerMetatile = projectConfig.getTripleLayerMetatilesEnabled() ? 12 : 8; Metatile *mt = new Metatile(); for(int j = 0; j < tilesPerMetatile; ++j){ Tile tile(0, false, false, 0); diff --git a/src/project.cpp b/src/project.cpp index fb55cead..0ea8e28d 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -1079,7 +1079,7 @@ void Project::saveTilesetMetatiles(Tileset *tileset) { QFile metatiles_file(tileset->metatiles_path); if (metatiles_file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { QByteArray data; - int numTiles = projectConfig.getTripleLayerMetatilesEnabled() ? 12 : 8; + int numTiles = projectConfig.getNumTilesInMetatile(); for (Metatile *metatile : tileset->metatiles) { for (int i = 0; i < numTiles; i++) { uint16_t tile = metatile->tiles.at(i).rawValue(); @@ -1591,7 +1591,7 @@ void Project::loadTilesetMetatiles(Tileset* tileset) { QFile metatiles_file(tileset->metatiles_path); if (metatiles_file.open(QIODevice::ReadOnly)) { QByteArray data = metatiles_file.readAll(); - int tilesPerMetatile = projectConfig.getTripleLayerMetatilesEnabled() ? 12 : 8; + int tilesPerMetatile = projectConfig.getNumTilesInMetatile(); int bytesPerMetatile = 2 * tilesPerMetatile; int num_metatiles = data.length() / bytesPerMetatile; QList metatiles; diff --git a/src/scriptapi/apimap.cpp b/src/scriptapi/apimap.cpp index 602be5a4..3bb68caa 100644 --- a/src/scriptapi/apimap.cpp +++ b/src/scriptapi/apimap.cpp @@ -691,7 +691,7 @@ void MainWindow::setMetatileAttributes(int metatileId, int attributes) { } int MainWindow::calculateTileBounds(int * tileStart, int * tileEnd) { - int maxNumTiles = projectConfig.getTripleLayerMetatilesEnabled() ? 12 : 8; + int maxNumTiles = projectConfig.getNumTilesInMetatile(); if (*tileEnd >= maxNumTiles || *tileEnd < 0) *tileEnd = maxNumTiles - 1; if (*tileStart >= maxNumTiles || *tileStart < 0) diff --git a/src/scriptapi/scripting.cpp b/src/scriptapi/scripting.cpp index 12b549f0..97e0eaf9 100644 --- a/src/scriptapi/scripting.cpp +++ b/src/scriptapi/scripting.cpp @@ -70,7 +70,6 @@ void Scripting::populateGlobalObject(MainWindow *mainWindow) { int numTilesTotal = Project::getNumTilesTotal(); int numMetatilesPrimary = Project::getNumMetatilesPrimary(); int numMetatilesTotal = Project::getNumMetatilesTotal(); - bool tripleLayerEnabled = projectConfig.getTripleLayerMetatilesEnabled(); // Invisibly create an "About" window to read Porymap version AboutPorymap *about = new AboutPorymap(mainWindow); @@ -85,8 +84,8 @@ void Scripting::populateGlobalObject(MainWindow *mainWindow) { constants.setProperty("max_secondary_tiles", numTilesTotal - numTilesPrimary); constants.setProperty("max_primary_metatiles", numMetatilesPrimary); constants.setProperty("max_secondary_metatiles", numMetatilesTotal - numMetatilesPrimary); - constants.setProperty("layers_per_metatile", tripleLayerEnabled ? 3 : 2); - constants.setProperty("tiles_per_metatile", tripleLayerEnabled ? 12 : 8); + constants.setProperty("layers_per_metatile", projectConfig.getNumLayersInMetatile()); + constants.setProperty("tiles_per_metatile", projectConfig.getNumTilesInMetatile()); constants.setProperty("base_game_version", projectConfig.getBaseGameVersionString()); instance->engine->globalObject().setProperty("constants", constants); diff --git a/src/ui/metatilelayersitem.cpp b/src/ui/metatilelayersitem.cpp index 26c8c257..2b67b9a4 100644 --- a/src/ui/metatilelayersitem.cpp +++ b/src/ui/metatilelayersitem.cpp @@ -19,11 +19,9 @@ void MetatileLayersItem::draw() { QPoint(80, 16), }; - bool isTripleLayerMetatile = projectConfig.getTripleLayerMetatilesEnabled(); - int width = isTripleLayerMetatile ? 96 : 64; - QPixmap pixmap(width, 32); + int numTiles = projectConfig.getNumTilesInMetatile(); + QPixmap pixmap(numTiles * 8, 32); QPainter painter(&pixmap); - int numTiles = isTripleLayerMetatile ? 12 : 8; for (int i = 0; i < numTiles; i++) { Tile tile = this->metatile->tiles.at(i); QImage tileImage = getPalettedTileImage(tile.tileId, this->primaryTileset, this->secondaryTileset, tile.palette, true) @@ -98,8 +96,7 @@ void MetatileLayersItem::clearLastModifiedCoords() { } void MetatileLayersItem::getBoundedCoords(QPointF pos, int *x, int *y) { - bool isTripleLayerMetatile = projectConfig.getTripleLayerMetatilesEnabled(); - int maxX = isTripleLayerMetatile ? 5 : 3; + int maxX = (projectConfig.getNumLayersInMetatile() * 2) - 1; *x = static_cast(pos.x()) / 16; *y = static_cast(pos.y()) / 16; if (*x < 0) *x = 0; diff --git a/src/ui/tileseteditor.cpp b/src/ui/tileseteditor.cpp index 75e7d693..0f4611c5 100644 --- a/src/ui/tileseteditor.cpp +++ b/src/ui/tileseteditor.cpp @@ -192,8 +192,7 @@ void TilesetEditor::initMetatileLayersItem() { void TilesetEditor::initTileSelector() { - this->tileSelector = new TilesetEditorTileSelector(this->primaryTileset, this->secondaryTileset, - projectConfig.getTripleLayerMetatilesEnabled()); + this->tileSelector = new TilesetEditorTileSelector(this->primaryTileset, this->secondaryTileset, projectConfig.getNumLayersInMetatile()); connect(this->tileSelector, &TilesetEditorTileSelector::hoveredTileChanged, this, &TilesetEditor::onHoveredTileChanged); connect(this->tileSelector, &TilesetEditorTileSelector::hoveredTileCleared, @@ -398,8 +397,7 @@ void TilesetEditor::onMetatileLayerTileChanged(int x, int y) { QPoint dimensions = this->tileSelector->getSelectionDimensions(); QList tiles = this->tileSelector->getSelectedTiles(); int selectedTileIndex = 0; - bool isTripleLayerMetatile = projectConfig.getTripleLayerMetatilesEnabled(); - int maxTileIndex = isTripleLayerMetatile ? 12: 8; + int maxTileIndex = projectConfig.getNumTilesInMetatile(); for (int j = 0; j < dimensions.y(); j++) { for (int i = 0; i < dimensions.x(); i++) { int tileIndex = ((x + i) / 2 * 4) + ((y + j) * 2) + ((x + i) % 2); @@ -430,8 +428,7 @@ void TilesetEditor::onMetatileLayerSelectionChanged(QPoint selectionOrigin, int QList tileIdxs; int x = selectionOrigin.x(); int y = selectionOrigin.y(); - bool isTripleLayerMetatile = projectConfig.getTripleLayerMetatilesEnabled(); - int maxTileIndex = isTripleLayerMetatile ? 12: 8; + int maxTileIndex = projectConfig.getNumTilesInMetatile(); for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { int tileIndex = ((x + i) / 2 * 4) + ((y + j) * 2) + ((x + i) % 2); @@ -759,28 +756,18 @@ void TilesetEditor::on_actionChange_Metatiles_Count_triggered() if (dialog.exec() == QDialog::Accepted) { int numPrimaryMetatiles = primarySpinBox->value(); int numSecondaryMetatiles = secondarySpinBox->value(); - int numTiles = projectConfig.getTripleLayerMetatilesEnabled() ? 12 : 8; + int numTiles = projectConfig.getNumTilesInMetatile(); while (this->primaryTileset->metatiles.length() > numPrimaryMetatiles) { delete this->primaryTileset->metatiles.takeLast(); } while (this->primaryTileset->metatiles.length() < numPrimaryMetatiles) { - Tile tile(0, false, false, 0); - Metatile *metatile = new Metatile(); - for (int i = 0; i < numTiles; i++) { - metatile->tiles.append(tile); - } - this->primaryTileset->metatiles.append(metatile); + this->primaryTileset->metatiles.append(new Metatile(numTiles)); } while (this->secondaryTileset->metatiles.length() > numSecondaryMetatiles) { delete this->secondaryTileset->metatiles.takeLast(); } while (this->secondaryTileset->metatiles.length() < numSecondaryMetatiles) { - Tile tile(0, false, false, 0); - Metatile *metatile = new Metatile(); - for (int i = 0; i < numTiles; i++) { - metatile->tiles.append(tile); - } - this->secondaryTileset->metatiles.append(metatile); + this->secondaryTileset->metatiles.append(new Metatile(numTiles)); } this->metatileSelector->updateSelectedMetatile(); @@ -852,10 +839,7 @@ void TilesetEditor::on_actionRedo_triggered() void TilesetEditor::on_actionCut_triggered() { - int numTiles = projectConfig.getTripleLayerMetatilesEnabled() ? 12 : 8; - Metatile * empty = new Metatile(); - for (int i = 0; i < numTiles; i++) - empty->tiles.append(Tile()); + Metatile * empty = new Metatile(projectConfig.getNumTilesInMetatile()); this->copyMetatile(true); this->pasteMetatile(empty); delete empty;