From 6682233079c0a91c70b71ec6afa7b64a6a622dcd Mon Sep 17 00:00:00 2001 From: GriffinR Date: Tue, 2 Jan 2024 00:54:54 -0500 Subject: [PATCH] Fix tileset editor desyncing with API changes --- include/ui/tileseteditor.h | 2 ++ src/scriptapi/apimap.cpp | 12 +++++++++--- src/ui/tileseteditor.cpp | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/include/ui/tileseteditor.h b/include/ui/tileseteditor.h index 7de67568..a904a150 100644 --- a/include/ui/tileseteditor.h +++ b/include/ui/tileseteditor.h @@ -47,6 +47,7 @@ public: bool selectMetatile(uint16_t metatileId); uint16_t getSelectedMetatileId(); void setMetatileLabel(QString label); + void queueMetatileReload(uint16_t metatileId); QObjectList shortcutableObjects() const; @@ -166,6 +167,7 @@ private: QGraphicsPixmapItem *selectedTilePixmapItem = nullptr; QGraphicsScene *metatileLayersScene = nullptr; bool lockSelection = false; + QSet metatileReloadQueue; signals: void tilesetsSaved(QString, QString); diff --git a/src/scriptapi/apimap.cpp b/src/scriptapi/apimap.cpp index 14a6a558..6fb436d2 100644 --- a/src/scriptapi/apimap.cpp +++ b/src/scriptapi/apimap.cpp @@ -585,9 +585,15 @@ void MainWindow::saveMetatileAttributesByMetatileId(int metatileId) { if (this->editor->project && tileset) this->editor->project->saveTilesetMetatileAttributes(tileset); - // If the Tileset Editor is currently displaying the updated metatile, refresh it - if (this->tilesetEditor && this->tilesetEditor->getSelectedMetatileId() == metatileId) - this->tilesetEditor->onSelectedMetatileChanged(metatileId); + // If the tileset editor is open it needs to be refreshed with the new changes. + // Rather than do a full refresh (which is costly) we tell the editor it will need + // to reload the metatile from the project next time it's displayed. + // If it's currently being displayed, trigger this reload immediately. + if (this->tilesetEditor) { + this->tilesetEditor->queueMetatileReload(metatileId); + if (this->tilesetEditor->getSelectedMetatileId() == metatileId) + this->tilesetEditor->onSelectedMetatileChanged(metatileId); + } } Metatile * MainWindow::getMetatile(int metatileId) { diff --git a/src/ui/tileseteditor.cpp b/src/ui/tileseteditor.cpp index 62ac20c5..0032264e 100644 --- a/src/ui/tileseteditor.cpp +++ b/src/ui/tileseteditor.cpp @@ -81,6 +81,7 @@ uint16_t TilesetEditor::getSelectedMetatileId() { } void TilesetEditor::setTilesets(QString primaryTilesetLabel, QString secondaryTilesetLabel) { + this->metatileReloadQueue.clear(); Tileset *primaryTileset = project->getTileset(primaryTilesetLabel); Tileset *secondaryTileset = project->getTileset(secondaryTilesetLabel); if (this->primaryTileset) delete this->primaryTileset; @@ -373,6 +374,15 @@ void TilesetEditor::onHoveredMetatileCleared() { void TilesetEditor::onSelectedMetatileChanged(uint16_t metatileId) { this->metatile = Tileset::getMetatile(metatileId, this->primaryTileset, this->secondaryTileset); + + // The scripting API allows users to change metatiles in the project, and these changes are saved to disk. + // The Tileset Editor (if open) needs to reflect these changes when the metatile is next displayed. + if (this->metatileReloadQueue.contains(metatileId)) { + this->metatileReloadQueue.remove(metatileId); + Metatile *updatedMetatile = Tileset::getMetatile(metatileId, this->map->layout->tileset_primary, this->map->layout->tileset_secondary); + if (updatedMetatile) *this->metatile = *updatedMetatile; + } + this->metatileLayersItem->setMetatile(metatile); this->metatileLayersItem->draw(); this->ui->graphicsView_metatileLayers->setFixedSize(this->metatileLayersItem->pixmap().width() + 2, this->metatileLayersItem->pixmap().height() + 2); @@ -387,6 +397,10 @@ void TilesetEditor::onSelectedMetatileChanged(uint16_t metatileId) { this->ui->comboBox_terrainType->setHexItem(this->metatile->terrainType()); } +void TilesetEditor::queueMetatileReload(uint16_t metatileId) { + this->metatileReloadQueue.insert(metatileId); +} + void TilesetEditor::onHoveredTileChanged(uint16_t tile) { QString message = QString("Tile: 0x%1") .arg(QString("%1").arg(tile, 3, 16, QChar('0')).toUpper());