Add copy/paste to tileset editor

This commit is contained in:
GriffinR 2022-03-30 18:03:13 -04:00 committed by t
parent 418f7ffc81
commit 304b00b0f3
6 changed files with 79 additions and 36 deletions

View file

@ -540,6 +540,8 @@
<property name="title">
<string>Edit</string>
</property>
<addaction name="actionCopy"/>
<addaction name="actionPaste"/>
<addaction name="actionUndo"/>
<addaction name="actionRedo"/>
</widget>
@ -636,6 +638,22 @@
<string>Import Secondary Metatiles from Advance Map 1.92...</string>
</property>
</action>
<action name="actionCopy">
<property name="text">
<string>Copy</string>
</property>
<property name="shortcut">
<string>Ctrl+C</string>
</property>
</action>
<action name="actionPaste">
<property name="text">
<string>Paste</string>
</property>
<property name="shortcut">
<string>Ctrl+V</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>

View file

@ -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<MetatileHistoryItem*> metatileHistory;
@ -137,6 +142,7 @@ private:
Project *project = nullptr;
Map *map = nullptr;
Metatile *metatile = nullptr;
Metatile *copiedMetatile = nullptr;
int paletteId;
bool tileXFlip;
bool tileYFlip;

View file

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

View file

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

View file

@ -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<uint16_t>(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<uint8_t>(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<uint8_t>(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<uint8_t>(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()

View file

@ -78,7 +78,7 @@ void TilesetEditorMetatileSelector::updateSelectedMetatile() {
emit selectedMetatileChanged(this->selectedMetatile);
}
uint16_t TilesetEditorMetatileSelector::getSelectedMetatile() {
uint16_t TilesetEditorMetatileSelector::getSelectedMetatileId() {
return this->selectedMetatile;
}