Fix tileset editor desyncing with API changes

This commit is contained in:
GriffinR 2024-01-02 00:54:54 -05:00
parent ecfc521a87
commit 6682233079
3 changed files with 25 additions and 3 deletions

View file

@ -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<uint16_t> metatileReloadQueue;
signals:
void tilesetsSaved(QString, QString);

View file

@ -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)
// 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) {

View file

@ -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());