diff --git a/forms/regionmapeditor.ui b/forms/regionmapeditor.ui index 8e598760..68f31641 100644 --- a/forms/regionmapeditor.ui +++ b/forms/regionmapeditor.ui @@ -1100,6 +1100,7 @@ + @@ -1138,7 +1139,7 @@ - Swap... + Swap Layout Sections... @@ -1176,6 +1177,11 @@ Update Config... + + + Replace Layout Section... + + diff --git a/include/core/regionmap.h b/include/core/regionmap.h index 3501cc28..6116849a 100644 --- a/include/core/regionmap.h +++ b/include/core/regionmap.h @@ -70,6 +70,7 @@ public: void clearLayout(); void clearImage(); void replaceSection(QString oldSection, QString newSection); + void swapSections(QString secA, QString secB); unsigned getTileId(int index); shared_ptr getTile(int index); diff --git a/include/ui/regionmapeditor.h b/include/ui/regionmapeditor.h index f349f9b2..eeca49a0 100644 --- a/include/ui/regionmapeditor.h +++ b/include/ui/regionmapeditor.h @@ -126,6 +126,7 @@ private slots: void on_action_RegionMap_ClearLayout_triggered(); void on_action_RegionMap_ClearEntries_triggered(); void on_action_Swap_triggered(); + void on_action_Replace_triggered(); void on_action_Configure_triggered(); void on_tabWidget_Region_Map_currentChanged(int); void on_pushButton_RM_Options_delete_clicked(); diff --git a/src/core/regionmap.cpp b/src/core/regionmap.cpp index 18b877d1..483b2719 100644 --- a/src/core/regionmap.cpp +++ b/src/core/regionmap.cpp @@ -415,6 +415,19 @@ void RegionMap::replaceSection(QString oldSection, QString newSection) { } } +void RegionMap::swapSections(QString secA, QString secB) { + for (auto &square : this->layouts[this->current_layer]) { + if (square.map_section == secA) { + square.map_section = secB; + square.has_map = (square.map_section != "MAPSEC_NONE"); + } + else if (square.map_section == secB) { + square.map_section = secA; + square.has_map = (square.map_section != "MAPSEC_NONE"); + } + } +} + void RegionMap::resizeTilemap(int newWidth, int newHeight, bool update) { auto tilemapCopy = this->tilemap; int oldWidth = this->tilemap_width; diff --git a/src/ui/regionmapeditor.cpp b/src/ui/regionmapeditor.cpp index 3a749ebf..443f0cfc 100644 --- a/src/ui/regionmapeditor.cpp +++ b/src/ui/regionmapeditor.cpp @@ -83,6 +83,10 @@ void RegionMapEditor::initShortcuts() { shortcutsConfig.load(); shortcutsConfig.setDefaultShortcuts(shortcutableObjects()); applyUserShortcuts(); + + connect(&(this->history), &QUndoGroup::indexChanged, [this](int) { + on_tabWidget_Region_Map_currentChanged(this->ui->tabWidget_Region_Map->currentIndex()); + }); } QObjectList RegionMapEditor::shortcutableObjects() const { @@ -421,9 +425,11 @@ void RegionMapEditor::clear() { // except the config json auto stacks = this->history.stacks(); + this->history.blockSignals(true); for (auto *stack : stacks) { this->history.removeStack(stack); } + this->history.blockSignals(false); for (auto p : this->region_maps) { delete p.second; } @@ -497,10 +503,6 @@ bool RegionMapEditor::setup() { setRegionMap(region_maps.begin()->second); } - connect(&(this->history), &QUndoGroup::indexChanged, [this](int) { - on_tabWidget_Region_Map_currentChanged(this->ui->tabWidget_Region_Map->currentIndex()); - }); - this->show(); this->setWindowState(Qt::WindowState::WindowActive); this->activateWindow(); @@ -555,7 +557,6 @@ bool RegionMapEditor::load() { void RegionMapEditor::setRegionMap(RegionMap *map) { this->region_map = map; this->currIndex = this->region_map->firstLayoutIndex(); - this->region_map->editHistory.setActive(); if (this->region_map->layoutEnabled()) { this->ui->tabWidget_Region_Map->setTabEnabled(1, true); @@ -567,6 +568,7 @@ void RegionMapEditor::setRegionMap(RegionMap *map) { } displayRegionMap(); + this->region_map->editHistory.setActive(); } bool RegionMapEditor::saveRegionMap(RegionMap *map) { @@ -1215,6 +1217,45 @@ void RegionMapEditor::on_action_Swap_triggered() { QFormLayout form(&popup); + QComboBox *oldSecBox = new QComboBox(); + oldSecBox->addItems(this->project->mapSectionValueToName.values()); + form.addRow(new QLabel("Map Section 1:"), oldSecBox); + QComboBox *newSecBox = new QComboBox(); + newSecBox->addItems(this->project->mapSectionValueToName.values()); + form.addRow(new QLabel("Map Section 2:"), newSecBox); + + QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &popup); + form.addRow(&buttonBox); + + QString beforeSection, afterSection; + connect(&buttonBox, &QDialogButtonBox::rejected, &popup, &QDialog::reject); + connect(&buttonBox, &QDialogButtonBox::accepted, [&popup, &oldSecBox, &newSecBox, &beforeSection, &afterSection](){ + beforeSection = oldSecBox->currentText(); + afterSection = newSecBox->currentText(); + if (!beforeSection.isEmpty() && !afterSection.isEmpty()) { + popup.accept(); + } + }); + + if (popup.exec() == QDialog::Accepted) { + QList oldLayout = this->region_map->getLayout(this->region_map->getLayer()); + this->region_map->swapSections(beforeSection, afterSection); + QList newLayout = this->region_map->getLayout(this->region_map->getLayer()); + EditLayout *commit = new EditLayout(this->region_map, this->region_map->getLayer(), -5, oldLayout, newLayout); + commit->setText("Swap Layout Sections " + beforeSection + " <<>> " + afterSection); + this->region_map->editHistory.push(commit); + displayRegionMapLayout(); + this->region_map_layout_item->select(this->region_map_layout_item->selectedTile); + } +} + +void RegionMapEditor::on_action_Replace_triggered() { + QDialog popup(this, Qt::WindowTitleHint | Qt::WindowCloseButtonHint); + popup.setWindowTitle("Replace Map Section"); + popup.setWindowModality(Qt::NonModal); + + QFormLayout form(&popup); + QComboBox *oldSecBox = new QComboBox(); oldSecBox->addItems(this->project->mapSectionValueToName.values()); form.addRow(new QLabel("Old Map Section:"), oldSecBox); @@ -1240,7 +1281,7 @@ void RegionMapEditor::on_action_Swap_triggered() { this->region_map->replaceSection(beforeSection, afterSection); QList newLayout = this->region_map->getLayout(this->region_map->getLayer()); EditLayout *commit = new EditLayout(this->region_map, this->region_map->getLayer(), -2, oldLayout, newLayout); - commit->setText("Swap Layout Sections " + beforeSection + " >> " + afterSection); + commit->setText("Replace Layout Section " + beforeSection + " >> " + afterSection); this->region_map->editHistory.push(commit); displayRegionMapLayout(); this->region_map_layout_item->select(this->region_map_layout_item->selectedTile);