From 304b00b0f38ff08192c18e2f26e9bd1a3be5fb22 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Wed, 30 Mar 2022 18:03:13 -0400 Subject: [PATCH] Add copy/paste to tileset editor --- forms/tileseteditor.ui | 18 +++++ include/ui/tileseteditor.h | 8 ++- include/ui/tileseteditormetatileselector.h | 2 +- src/mainwindow_scriptapi.cpp | 4 +- src/ui/tileseteditor.cpp | 81 +++++++++++++--------- src/ui/tileseteditormetatileselector.cpp | 2 +- 6 files changed, 79 insertions(+), 36 deletions(-) diff --git a/forms/tileseteditor.ui b/forms/tileseteditor.ui index 960142d5..98dfcc81 100644 --- a/forms/tileseteditor.ui +++ b/forms/tileseteditor.ui @@ -540,6 +540,8 @@ Edit + + @@ -636,6 +638,22 @@ Import Secondary Metatiles from Advance Map 1.92... + + + Copy + + + Ctrl+C + + + + + Paste + + + Ctrl+V + + diff --git a/include/ui/tileseteditor.h b/include/ui/tileseteditor.h index 1088d27e..9e433025 100644 --- a/include/ui/tileseteditor.h +++ b/include/ui/tileseteditor.h @@ -41,7 +41,7 @@ public: void updateMap(Map *map); void updateTilesets(QString primaryTilsetLabel, QString secondaryTilesetLabel); bool selectMetatile(uint16_t metatileId); - uint16_t getSelectedMetatile(); + uint16_t getSelectedMetatileId(); void setMetatileLabel(QString label); QObjectList shortcutableObjects() const; @@ -103,6 +103,10 @@ private slots: void on_actionImport_Secondary_Metatiles_triggered(); + void on_actionCopy_triggered(); + + void on_actionPaste_triggered(); + private: void initUi(); void setMetatileBehaviors(); @@ -127,6 +131,7 @@ private: void closeEvent(QCloseEvent*); void countMetatileUsage(); void countTileUsage(); + bool replaceMetatile(uint16_t metatileId, Metatile * src); Ui::TilesetEditor *ui; History metatileHistory; @@ -137,6 +142,7 @@ private: Project *project = nullptr; Map *map = nullptr; Metatile *metatile = nullptr; + Metatile *copiedMetatile = nullptr; int paletteId; bool tileXFlip; bool tileYFlip; diff --git a/include/ui/tileseteditormetatileselector.h b/include/ui/tileseteditormetatileselector.h index 432241e7..e306727d 100644 --- a/include/ui/tileseteditormetatileselector.h +++ b/include/ui/tileseteditormetatileselector.h @@ -13,7 +13,7 @@ public: void draw(); bool select(uint16_t metatileId); void setTilesets(Tileset*, Tileset*, bool draw = true); - uint16_t getSelectedMetatile(); + uint16_t getSelectedMetatileId(); void updateSelectedMetatile(); QPoint getMetatileIdCoordsOnWidget(uint16_t metatileId); diff --git a/src/mainwindow_scriptapi.cpp b/src/mainwindow_scriptapi.cpp index 7ce31b4e..cb37560e 100644 --- a/src/mainwindow_scriptapi.cpp +++ b/src/mainwindow_scriptapi.cpp @@ -833,7 +833,7 @@ void MainWindow::saveMetatileAttributesByMetatileId(int metatileId) { this->editor->project->saveTilesetMetatileAttributes(tileset); // If the Tileset Editor is currently displaying the updated metatile, refresh it - if (this->tilesetEditor && this->tilesetEditor->getSelectedMetatile() == metatileId) + if (this->tilesetEditor && this->tilesetEditor->getSelectedMetatileId() == metatileId) this->tilesetEditor->updateTilesets(this->editor->map->layout->tileset_primary_label, this->editor->map->layout->tileset_secondary_label); } @@ -863,7 +863,7 @@ void MainWindow::setMetatileLabel(int metatileId, QString label) { return; } - if (this->tilesetEditor && this->tilesetEditor->getSelectedMetatile() == metatileId) { + if (this->tilesetEditor && this->tilesetEditor->getSelectedMetatileId() == metatileId) { this->tilesetEditor->setMetatileLabel(label); } else if (metatile->label != label) { metatile->label = label; diff --git a/src/ui/tileseteditor.cpp b/src/ui/tileseteditor.cpp index 540ef66c..0ff44c7b 100644 --- a/src/ui/tileseteditor.cpp +++ b/src/ui/tileseteditor.cpp @@ -40,6 +40,7 @@ TilesetEditor::~TilesetEditor() delete selectedTilePixmapItem; delete selectedTileScene; delete metatileLayersScene; + delete copiedMetatile; } void TilesetEditor::update(Map *map, QString primaryTilesetLabel, QString secondaryTilesetLabel) { @@ -76,8 +77,8 @@ bool TilesetEditor::selectMetatile(uint16_t metatileId) { return true; } -uint16_t TilesetEditor::getSelectedMetatile() { - return this->metatileSelector->getSelectedMetatile(); +uint16_t TilesetEditor::getSelectedMetatileId() { + return this->metatileSelector->getSelectedMetatileId(); } void TilesetEditor::setTilesets(QString primaryTilesetLabel, QString secondaryTilesetLabel) { @@ -177,7 +178,7 @@ void TilesetEditor::initMetatileSelector() } void TilesetEditor::initMetatileLayersItem() { - Metatile *metatile = Tileset::getMetatile(this->getSelectedMetatile(), this->primaryTileset, this->secondaryTileset); + Metatile *metatile = Tileset::getMetatile(this->getSelectedMetatileId(), this->primaryTileset, this->secondaryTileset); this->metatileLayersItem = new MetatileLayersItem(metatile, this->primaryTileset, this->secondaryTileset); connect(this->metatileLayersItem, &MetatileLayersItem::tileChanged, this, &TilesetEditor::onMetatileLayerTileChanged); @@ -282,7 +283,7 @@ void TilesetEditor::refresh() { this->metatileLayersItem->setTilesets(this->primaryTileset, this->secondaryTileset); this->tileSelector->setTilesets(this->primaryTileset, this->secondaryTileset); this->metatileSelector->setTilesets(this->primaryTileset, this->secondaryTileset); - this->metatileSelector->select(this->getSelectedMetatile()); + this->metatileSelector->select(this->getSelectedMetatileId()); this->drawSelectedTiles(); if (metatileSelector) { @@ -419,7 +420,7 @@ void TilesetEditor::onMetatileLayerTileChanged(int x, int y) { this->metatileLayersItem->draw(); this->hasUnsavedChanges = true; - MetatileHistoryItem *commit = new MetatileHistoryItem(this->getSelectedMetatile(), + MetatileHistoryItem *commit = new MetatileHistoryItem(this->getSelectedMetatileId(), prevMetatile, new Metatile(*this->metatile)); metatileHistory.push(commit); } @@ -486,7 +487,7 @@ void TilesetEditor::on_comboBox_metatileBehaviors_textActivated(const QString &m if (this->metatile) { Metatile *prevMetatile = new Metatile(*this->metatile); this->metatile->behavior = static_cast(project->metatileBehaviorMap[metatileBehavior]); - MetatileHistoryItem *commit = new MetatileHistoryItem(this->getSelectedMetatile(), + MetatileHistoryItem *commit = new MetatileHistoryItem(this->getSelectedMetatileId(), prevMetatile, new Metatile(*this->metatile)); metatileHistory.push(commit); this->hasUnsavedChanges = true; @@ -510,7 +511,7 @@ void TilesetEditor::saveMetatileLabel() if (this->metatile && this->metatile->label != this->ui->lineEdit_metatileLabel->text()) { Metatile *prevMetatile = new Metatile(*this->metatile); this->metatile->label = this->ui->lineEdit_metatileLabel->text(); - MetatileHistoryItem *commit = new MetatileHistoryItem(this->getSelectedMetatile(), + MetatileHistoryItem *commit = new MetatileHistoryItem(this->getSelectedMetatileId(), prevMetatile, new Metatile(*this->metatile)); metatileHistory.push(commit); this->hasUnsavedChanges = true; @@ -522,7 +523,7 @@ void TilesetEditor::on_comboBox_layerType_activated(int layerType) if (this->metatile) { Metatile *prevMetatile = new Metatile(*this->metatile); this->metatile->layerType = static_cast(layerType); - MetatileHistoryItem *commit = new MetatileHistoryItem(this->getSelectedMetatile(), + MetatileHistoryItem *commit = new MetatileHistoryItem(this->getSelectedMetatileId(), prevMetatile, new Metatile(*this->metatile)); metatileHistory.push(commit); this->hasUnsavedChanges = true; @@ -534,7 +535,7 @@ void TilesetEditor::on_comboBox_encounterType_activated(int encounterType) if (this->metatile) { Metatile *prevMetatile = new Metatile(*this->metatile); this->metatile->encounterType = static_cast(encounterType); - MetatileHistoryItem *commit = new MetatileHistoryItem(this->getSelectedMetatile(), + MetatileHistoryItem *commit = new MetatileHistoryItem(this->getSelectedMetatileId(), prevMetatile, new Metatile(*this->metatile)); metatileHistory.push(commit); this->hasUnsavedChanges = true; @@ -546,7 +547,7 @@ void TilesetEditor::on_comboBox_terrainType_activated(int terrainType) if (this->metatile) { Metatile *prevMetatile = new Metatile(*this->metatile); this->metatile->terrainType = static_cast(terrainType); - MetatileHistoryItem *commit = new MetatileHistoryItem(this->getSelectedMetatile(), + MetatileHistoryItem *commit = new MetatileHistoryItem(this->getSelectedMetatileId(), prevMetatile, new Metatile(*this->metatile)); metatileHistory.push(commit); this->hasUnsavedChanges = true; @@ -811,6 +812,21 @@ void TilesetEditor::onPaletteEditorChangedPalette(int paletteId) { this->on_spinBox_paletteSelector_valueChanged(paletteId); } +bool TilesetEditor::replaceMetatile(uint16_t metatileId, Metatile * src) +{ + Metatile * dest = Tileset::getMetatile(metatileId, this->primaryTileset, this->secondaryTileset); + if (!dest || !src) + return false; + + this->metatile = dest; + *this->metatile = *src; + this->metatileSelector->select(metatileId); + this->metatileSelector->draw(); + this->metatileLayersItem->draw(); + this->metatileLayersItem->clearLastModifiedCoords(); + return true; +} + void TilesetEditor::on_actionUndo_triggered() { MetatileHistoryItem *commit = this->metatileHistory.current(); @@ -818,34 +834,37 @@ void TilesetEditor::on_actionUndo_triggered() Metatile *prev = commit->prevMetatile; if (!prev) return; this->metatileHistory.back(); - - Metatile *temp = Tileset::getMetatile(commit->metatileId, this->primaryTileset, this->secondaryTileset); - if (temp) { - this->metatile = temp; - *this->metatile = *prev; - this->metatileSelector->select(commit->metatileId); - this->metatileSelector->draw(); - this->metatileLayersItem->draw(); - this->metatileLayersItem->clearLastModifiedCoords(); - } + this->replaceMetatile(commit->metatileId, prev); } void TilesetEditor::on_actionRedo_triggered() { MetatileHistoryItem *commit = this->metatileHistory.next(); if (!commit) return; - Metatile *next = commit->newMetatile; - if (!next) return; + this->replaceMetatile(commit->metatileId, commit->newMetatile); +} - Metatile *temp = Tileset::getMetatile(commit->metatileId, this->primaryTileset, this->secondaryTileset); - if (temp) { - this->metatile = temp; - *this->metatile = *next; - this->metatileSelector->select(commit->metatileId); - this->metatileSelector->draw(); - this->metatileLayersItem->draw(); - this->metatileLayersItem->clearLastModifiedCoords(); - } +void TilesetEditor::on_actionCopy_triggered() +{ + Metatile * toCopy = Tileset::getMetatile(this->getSelectedMetatileId(), this->primaryTileset, this->secondaryTileset); + if (!toCopy) return; + + if (!this->copiedMetatile) + this->copiedMetatile = new Metatile(*toCopy); + else + *this->copiedMetatile = *toCopy; + this->copiedMetatile->label = ""; // Don't copy the label, these should be unique to each metatile +} + +void TilesetEditor::on_actionPaste_triggered() +{ + Metatile *prevMetatile = new Metatile(*this->metatile); + uint16_t metatileId = this->getSelectedMetatileId(); + if (!this->replaceMetatile(metatileId, this->copiedMetatile)) + return; + + MetatileHistoryItem *commit = new MetatileHistoryItem(metatileId, prevMetatile, new Metatile(*this->metatile)); + metatileHistory.push(commit); } void TilesetEditor::on_actionExport_Primary_Tiles_Image_triggered() diff --git a/src/ui/tileseteditormetatileselector.cpp b/src/ui/tileseteditormetatileselector.cpp index 178d0a63..9ea218be 100644 --- a/src/ui/tileseteditormetatileselector.cpp +++ b/src/ui/tileseteditormetatileselector.cpp @@ -78,7 +78,7 @@ void TilesetEditorMetatileSelector::updateSelectedMetatile() { emit selectedMetatileChanged(this->selectedMetatile); } -uint16_t TilesetEditorMetatileSelector::getSelectedMetatile() { +uint16_t TilesetEditorMetatileSelector::getSelectedMetatileId() { return this->selectedMetatile; }