From 0c7ecebe0baa4e3055f777750abe9105b5b8919c Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Tue, 2 Oct 2018 19:01:18 -0500 Subject: [PATCH] Keep tileset editor in sync with currently opened map --- include/mainwindow.h | 1 + include/ui/tileseteditor.h | 1 + include/ui/tileseteditormetatileselector.h | 1 + src/mainwindow.cpp | 7 +++++++ src/ui/tileseteditor.cpp | 15 +++++++++++++++ src/ui/tileseteditormetatileselector.cpp | 19 +++++++++++-------- 6 files changed, 36 insertions(+), 8 deletions(-) diff --git a/include/mainwindow.h b/include/mainwindow.h index a120644c..9ba488b8 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -157,6 +157,7 @@ private: void initEditor(); void loadUserSettings(); void openRecentProject(); + void updateTilesetEditor(); }; enum MapListUserRoles { diff --git a/include/ui/tileseteditor.h b/include/ui/tileseteditor.h index 2bef6227..c179bfd0 100644 --- a/include/ui/tileseteditor.h +++ b/include/ui/tileseteditor.h @@ -18,6 +18,7 @@ class TilesetEditor : public QMainWindow public: explicit TilesetEditor(Project*, QString, QString, QWidget *parent = nullptr); ~TilesetEditor(); + void setTilesets(QString, QString); private slots: void onHoveredMetatileChanged(uint16_t); diff --git a/include/ui/tileseteditormetatileselector.h b/include/ui/tileseteditormetatileselector.h index 4b1a0d49..3da82721 100644 --- a/include/ui/tileseteditormetatileselector.h +++ b/include/ui/tileseteditormetatileselector.h @@ -33,6 +33,7 @@ private: void updateSelectedMetatile(); uint16_t getMetatileId(int x, int y); QPoint getMetatileIdCoords(uint16_t); + uint16_t getValidMetatileId(uint16_t); signals: void hoveredMetatileChanged(uint16_t); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 280e770c..83e0dccb 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -196,6 +196,7 @@ void MainWindow::setMap(QString map_name, bool scrollTreeView) { setRecentMap(map_name); updateMapList(); + updateTilesetEditor(); } void MainWindow::redrawMapScene() @@ -529,6 +530,12 @@ void MainWindow::onTilesetChanged(QString mapName) setMap(mapName); } +void MainWindow::updateTilesetEditor() { + if (this->tilesetEditor) { + this->tilesetEditor->setTilesets(editor->ui->comboBox_PrimaryTileset->currentText(), editor->ui->comboBox_SecondaryTileset->currentText()); + } +} + void MainWindow::currentMetatilesSelectionChanged() { ui->graphicsView_currentMetatileSelection->setFixedSize(editor->scene_current_metatile_selection_item->pixmap().width() + 2, editor->scene_current_metatile_selection_item->pixmap().height() + 2); diff --git a/src/ui/tileseteditor.cpp b/src/ui/tileseteditor.cpp index 774e4a10..871c869b 100644 --- a/src/ui/tileseteditor.cpp +++ b/src/ui/tileseteditor.cpp @@ -41,6 +41,21 @@ TilesetEditor::~TilesetEditor() delete ui; } +void TilesetEditor::setTilesets(QString primaryTilesetLabel, QString secondaryTilesetLabel) { + delete this->primaryTileset; + delete this->secondaryTileset; + Tileset *primaryTileset = project->getTileset(primaryTilesetLabel); + Tileset *secondaryTileset = project->getTileset(secondaryTilesetLabel); + this->primaryTileset = primaryTileset->copy(); + this->secondaryTileset = secondaryTileset->copy(); + + this->metatileSelector->setTilesets(this->primaryTileset, this->secondaryTileset); + this->tileSelector->setTilesets(this->primaryTileset, this->secondaryTileset); + this->metatileLayersItem->setTilesets(this->primaryTileset, this->secondaryTileset); + this->metatileSelector->select(this->metatileSelector->getSelectedMetatile()); + this->drawSelectedTile(); +} + void TilesetEditor::initMetatileSelector() { this->metatileSelector = new TilesetEditorMetatileSelector(this->primaryTileset, this->secondaryTileset); diff --git a/src/ui/tileseteditormetatileselector.cpp b/src/ui/tileseteditormetatileselector.cpp index a5e607f7..852ab0e9 100644 --- a/src/ui/tileseteditormetatileselector.cpp +++ b/src/ui/tileseteditormetatileselector.cpp @@ -32,6 +32,7 @@ void TilesetEditorMetatileSelector::draw() { } void TilesetEditorMetatileSelector::select(uint16_t metatileId) { + metatileId = this->getValidMetatileId(metatileId); QPoint coords = this->getMetatileIdCoords(metatileId); SelectablePixmapItem::select(coords.x(), coords.y(), 0, 0); this->selectedMetatile = metatileId; @@ -95,17 +96,19 @@ void TilesetEditorMetatileSelector::hoverLeaveEvent(QGraphicsSceneHoverEvent*) { } QPoint TilesetEditorMetatileSelector::getMetatileIdCoords(uint16_t metatileId) { - if (metatileId >= Project::getNumMetatilesTotal() - || (metatileId < Project::getNumMetatilesPrimary() && metatileId >= this->primaryTileset->metatiles->length()) - || (metatileId < Project::getNumMetatilesTotal() && metatileId >= Project::getNumMetatilesPrimary() + this->secondaryTileset->metatiles->length())) - { - // Invalid metatile id. - return QPoint(0, 0); - } - int index = metatileId < Project::getNumMetatilesPrimary() ? metatileId : metatileId - Project::getNumMetatilesPrimary() + this->primaryTileset->metatiles->length(); return QPoint(index % this->numMetatilesWide, index / this->numMetatilesWide); } +uint16_t TilesetEditorMetatileSelector::getValidMetatileId(uint16_t metatileId) { + if (metatileId >= Project::getNumMetatilesTotal() + || (metatileId < Project::getNumMetatilesPrimary() && metatileId >= this->primaryTileset->metatiles->length()) + || (metatileId < Project::getNumMetatilesTotal() && metatileId >= Project::getNumMetatilesPrimary() + this->secondaryTileset->metatiles->length())) + { + return 0; + } + return metatileId; +} +