From a2d230666b1997d91eb805bec0d1b23baa8cf896 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Tue, 19 Dec 2023 21:21:16 -0500 Subject: [PATCH] Add grids to tileset editor --- CHANGELOG.md | 1 + forms/tileseteditor.ui | 153 +++++++++++++-------- include/config.h | 8 ++ include/ui/metatilelayersitem.h | 1 + include/ui/tileseteditor.h | 2 + include/ui/tileseteditormetatileselector.h | 5 +- src/config.cpp | 24 ++++ src/ui/metatilelayersitem.cpp | 15 +- src/ui/tileseteditor.cpp | 20 +++ src/ui/tileseteditormetatileselector.cpp | 35 ++++- 10 files changed, 196 insertions(+), 68 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7254c16..b25dfca4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ The **"Breaking Changes"** listed below are changes that have been made in the d - Add settings for custom images, including the collision graphics, default event icons, and pokémon icons. - Add settings to override any symbol or macro names Porymap expects to find. - Add a zoom slider to the Collision tab. +- Add toggleable grids to the Tileset Editor. - Support for custom metatile ID, collision, and elevation data sizes. - Support for 8bpp tileset tile images. diff --git a/forms/tileseteditor.ui b/forms/tileseteditor.ui index 6a2a4c69..2fb717e6 100644 --- a/forms/tileseteditor.ui +++ b/forms/tileseteditor.ui @@ -201,83 +201,41 @@ false - - - - Bottom/Top - - - - - - - - - - Encounter Type - - - - - - - Terrain Type - - - - - - - - - - Metatile Label (Optional) - - - - + - - - - <html><head/><body><p>Copies the full metatile label to the clipboard.</p></body></html> - - - ... - - - - :/icons/clipboard.ico:/icons/clipboard.ico - - + + - + true - - + + - Layer Type + Metatile Label (Optional) - + Metatile Behavior - - - + + + 0 + 0 + + 66 @@ -298,6 +256,54 @@ + + + + Bottom/Top + + + + + + + Encounter Type + + + + + + + Terrain Type + + + + + + + + + + Layer Type + + + + + + + <html><head/><body><p>Copies the full metatile label to the clipboard.</p></body></html> + + + ... + + + + :/icons/clipboard.ico:/icons/clipboard.ico + + + + + + @@ -565,10 +571,6 @@ - - - - @@ -584,8 +586,20 @@ + + + View + + + + + + + + + @@ -711,6 +725,25 @@ Ctrl+V + + + true + + + Layer Grid + + + + + true + + + Metatile Grid + + + Ctrl+G + + diff --git a/include/config.h b/include/config.h index c338ab34..bc847b35 100644 --- a/include/config.h +++ b/include/config.h @@ -62,6 +62,8 @@ public: this->showCursorTile = true; this->showBorder = true; this->showGrid = false; + this->showTilesetEditorMetatileGrid = false; + this->showTilesetEditorLayerGrid = true; this->monitorFiles = true; this->tilesetCheckerboardFill = true; this->theme = "default"; @@ -88,6 +90,8 @@ public: void setShowCursorTile(bool enabled); void setShowBorder(bool enabled); void setShowGrid(bool enabled); + void setShowTilesetEditorMetatileGrid(bool enabled); + void setShowTilesetEditorLayerGrid(bool enabled); void setMonitorFiles(bool monitor); void setTilesetCheckerboardFill(bool checkerboard); void setTheme(QString theme); @@ -113,6 +117,8 @@ public: bool getShowCursorTile(); bool getShowBorder(); bool getShowGrid(); + bool getShowTilesetEditorMetatileGrid(); + bool getShowTilesetEditorLayerGrid(); bool getMonitorFiles(); bool getTilesetCheckerboardFill(); QString getTheme(); @@ -156,6 +162,8 @@ private: bool showCursorTile; bool showBorder; bool showGrid; + bool showTilesetEditorMetatileGrid; + bool showTilesetEditorLayerGrid; bool monitorFiles; bool tilesetCheckerboardFill; QString theme; diff --git a/include/ui/metatilelayersitem.h b/include/ui/metatilelayersitem.h index 87c6f066..fbd769e0 100644 --- a/include/ui/metatilelayersitem.h +++ b/include/ui/metatilelayersitem.h @@ -19,6 +19,7 @@ public: void setTilesets(Tileset*, Tileset*); void setMetatile(Metatile*); void clearLastModifiedCoords(); + bool showGrid; private: Metatile* metatile; Tileset *primaryTileset; diff --git a/include/ui/tileseteditor.h b/include/ui/tileseteditor.h index 5a46075c..7de67568 100644 --- a/include/ui/tileseteditor.h +++ b/include/ui/tileseteditor.h @@ -84,6 +84,8 @@ private slots: void on_actionShow_Unused_toggled(bool checked); void on_actionShow_Counts_toggled(bool checked); void on_actionShow_UnusedTiles_toggled(bool checked); + void on_actionMetatile_Grid_triggered(bool checked); + void on_actionLayer_Grid_triggered(bool checked); void on_actionUndo_triggered(); diff --git a/include/ui/tileseteditormetatileselector.h b/include/ui/tileseteditormetatileselector.h index ef5255dc..50de1c56 100644 --- a/include/ui/tileseteditormetatileselector.h +++ b/include/ui/tileseteditormetatileselector.h @@ -22,6 +22,7 @@ public: QVector usedMetatiles; bool selectorShowUnused = false; bool selectorShowCounts = false; + bool showGrid; protected: void mousePressEvent(QGraphicsSceneMouseEvent*); @@ -35,10 +36,12 @@ private: Tileset *secondaryTileset = nullptr; uint16_t selectedMetatile; int numMetatilesWide; + int numMetatilesHigh; uint16_t getMetatileId(int x, int y); QPoint getMetatileIdCoords(uint16_t); bool shouldAcceptEvent(QGraphicsSceneMouseEvent*); - + int numRows(int numMetatiles); + int numRows(); void drawFilters(); void drawUnused(); void drawCounts(); diff --git a/src/config.cpp b/src/config.cpp index e2616f39..f9e64987 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -354,6 +354,10 @@ void PorymapConfig::parseConfigKeyValue(QString key, QString value) { this->showBorder = getConfigBool(key, value); } else if (key == "show_grid") { this->showGrid = getConfigBool(key, value); + } else if (key == "show_tileset_editor_metatile_grid") { + this->showTilesetEditorMetatileGrid = getConfigBool(key, value); + } else if (key == "show_tileset_editor_layer_grid") { + this->showTilesetEditorLayerGrid = getConfigBool(key, value); } else if (key == "monitor_files") { this->monitorFiles = getConfigBool(key, value); } else if (key == "tileset_checkerboard_fill") { @@ -405,6 +409,8 @@ QMap PorymapConfig::getKeyValueMap() { map.insert("show_cursor_tile", this->showCursorTile ? "1" : "0"); map.insert("show_border", this->showBorder ? "1" : "0"); map.insert("show_grid", this->showGrid ? "1" : "0"); + map.insert("show_tileset_editor_metatile_grid", this->showTilesetEditorMetatileGrid ? "1" : "0"); + map.insert("show_tileset_editor_layer_grid", this->showTilesetEditorLayerGrid ? "1" : "0"); map.insert("monitor_files", this->monitorFiles ? "1" : "0"); map.insert("tileset_checkerboard_fill", this->tilesetCheckerboardFill ? "1" : "0"); map.insert("theme", this->theme); @@ -538,6 +544,16 @@ void PorymapConfig::setShowGrid(bool enabled) { this->save(); } +void PorymapConfig::setShowTilesetEditorMetatileGrid(bool enabled) { + this->showTilesetEditorMetatileGrid = enabled; + this->save(); +} + +void PorymapConfig::setShowTilesetEditorLayerGrid(bool enabled) { + this->showTilesetEditorLayerGrid = enabled; + this->save(); +} + void PorymapConfig::setTheme(QString theme) { this->theme = theme; } @@ -662,6 +678,14 @@ bool PorymapConfig::getShowGrid() { return this->showGrid; } +bool PorymapConfig::getShowTilesetEditorMetatileGrid() { + return this->showTilesetEditorMetatileGrid; +} + +bool PorymapConfig::getShowTilesetEditorLayerGrid() { + return this->showTilesetEditorLayerGrid; +} + bool PorymapConfig::getMonitorFiles() { return this->monitorFiles; } diff --git a/src/ui/metatilelayersitem.cpp b/src/ui/metatilelayersitem.cpp index 5147ad1c..d113d098 100644 --- a/src/ui/metatilelayersitem.cpp +++ b/src/ui/metatilelayersitem.cpp @@ -4,7 +4,7 @@ #include void MetatileLayersItem::draw() { - const QList tileCoords = QList{ + static const QList tileCoords = QList{ QPoint(0, 0), QPoint(16, 0), QPoint(0, 16), @@ -19,8 +19,11 @@ void MetatileLayersItem::draw() { QPoint(80, 16), }; - QPixmap pixmap(projectConfig.getNumLayersInMetatile() * 32, 32); + const int numLayers = projectConfig.getNumLayersInMetatile(); + QPixmap pixmap(numLayers * 32, 32); QPainter painter(&pixmap); + + // Draw tile images int numTiles = projectConfig.getNumTilesInMetatile(); for (int i = 0; i < numTiles; i++) { Tile tile = this->metatile->tiles.at(i); @@ -29,6 +32,14 @@ void MetatileLayersItem::draw() { .scaled(16, 16); painter.drawImage(tileCoords.at(i), tileImage); } + if (this->showGrid) { + // Draw grid + painter.setPen(Qt::white); + for (int i = 1; i < numLayers; i++) { + int x = i * 32; + painter.drawLine(x, 0, x, 32); + } + } this->setPixmap(pixmap); } diff --git a/src/ui/tileseteditor.cpp b/src/ui/tileseteditor.cpp index 77844ffa..62ac20c5 100644 --- a/src/ui/tileseteditor.cpp +++ b/src/ui/tileseteditor.cpp @@ -186,6 +186,10 @@ void TilesetEditor::initMetatileSelector() connect(this->metatileSelector, &TilesetEditorMetatileSelector::selectedMetatileChanged, this, &TilesetEditor::onSelectedMetatileChanged); + bool showGrid = porymapConfig.getShowTilesetEditorMetatileGrid(); + this->ui->actionMetatile_Grid->setChecked(showGrid); + this->metatileSelector->showGrid = showGrid; + this->metatilesScene = new QGraphicsScene; this->metatilesScene->addItem(this->metatileSelector); this->metatileSelector->draw(); @@ -202,6 +206,10 @@ void TilesetEditor::initMetatileLayersItem() { connect(this->metatileLayersItem, &MetatileLayersItem::selectedTilesChanged, this, &TilesetEditor::onMetatileLayerSelectionChanged); + bool showGrid = porymapConfig.getShowTilesetEditorLayerGrid(); + this->ui->actionLayer_Grid->setChecked(showGrid); + this->metatileLayersItem->showGrid = showGrid; + this->metatileLayersScene = new QGraphicsScene; this->metatileLayersScene->addItem(this->metatileLayersItem); this->ui->graphicsView_metatileLayers->setScene(this->metatileLayersScene); @@ -1036,6 +1044,18 @@ void TilesetEditor::on_actionShow_UnusedTiles_toggled(bool checked) { this->tileSelector->draw(); } +void TilesetEditor::on_actionMetatile_Grid_triggered(bool checked) { + this->metatileSelector->showGrid = checked; + this->metatileSelector->draw(); + porymapConfig.setShowTilesetEditorMetatileGrid(checked); +} + +void TilesetEditor::on_actionLayer_Grid_triggered(bool checked) { + this->metatileLayersItem->showGrid = checked; + this->metatileLayersItem->draw(); + porymapConfig.setShowTilesetEditorLayerGrid(checked); +} + void TilesetEditor::countMetatileUsage() { // do not double count metatileSelector->usedMetatiles.fill(0); diff --git a/src/ui/tileseteditormetatileselector.cpp b/src/ui/tileseteditormetatileselector.cpp index e89e94da..d46edcc3 100644 --- a/src/ui/tileseteditormetatileselector.cpp +++ b/src/ui/tileseteditormetatileselector.cpp @@ -12,6 +12,19 @@ TilesetEditorMetatileSelector::TilesetEditorMetatileSelector(Tileset *primaryTil this->usedMetatiles.resize(Project::getNumMetatilesTotal()); } +int TilesetEditorMetatileSelector::numRows(int numMetatiles) { + int numMetatilesHigh = numMetatiles / this->numMetatilesWide; + if (numMetatiles % this->numMetatilesWide != 0) { + // Round up height for incomplete last row + numMetatilesHigh++; + } + return numMetatilesHigh; +} + +int TilesetEditorMetatileSelector::numRows() { + return this->numRows(this->primaryTileset->metatiles.length() + this->secondaryTileset->metatiles.length()); +} + QImage TilesetEditorMetatileSelector::buildAllMetatilesImage() { return this->buildImage(0, this->primaryTileset->metatiles.length() + this->secondaryTileset->metatiles.length()); } @@ -25,11 +38,7 @@ QImage TilesetEditorMetatileSelector::buildSecondaryMetatilesImage() { } QImage TilesetEditorMetatileSelector::buildImage(int metatileIdStart, int numMetatiles) { - int numMetatilesHigh = numMetatiles / this->numMetatilesWide; - if (numMetatiles % this->numMetatilesWide != 0) { - // Round up height for incomplete last row - numMetatilesHigh++; - } + int numMetatilesHigh = this->numRows(numMetatiles); int numPrimary = this->primaryTileset->metatiles.length(); int maxPrimary = Project::getNumMetatilesPrimary(); bool includesPrimary = metatileIdStart < maxPrimary; @@ -158,6 +167,22 @@ QPoint TilesetEditorMetatileSelector::getMetatileIdCoordsOnWidget(uint16_t metat } void TilesetEditorMetatileSelector::drawFilters() { + if (this->showGrid) { + QPixmap pixmap = this->pixmap(); + QPainter painter(&pixmap); + const int numColumns = this->numMetatilesWide; + const int numRows = this->numRows(); + for (int column = 1; column < numColumns; column++) { + int x = column * 32; + painter.drawLine(x, 0, x, numRows * 32); + } + for (int row = 1; row < numRows; row++) { + int y = row * 32; + painter.drawLine(0, y, numColumns * 32, y); + } + painter.end(); + this->setPixmap(pixmap); + } if (selectorShowUnused) { drawUnused(); }