From 9cc55ef2f75bc20ebf97b5b8a293479b4f1ba71f Mon Sep 17 00:00:00 2001 From: garak Date: Mon, 16 Jan 2023 23:23:41 -0500 Subject: [PATCH] add option to copy mon info from another tab [closes #469] --- CHANGELOG.md | 1 + include/core/wildmoninfo.h | 1 + src/core/wildmoninfo.cpp | 14 +++++++++ src/mainwindow.cpp | 1 + src/project.cpp | 4 ++- src/ui/montabwidget.cpp | 59 +++++++++++++++++++++++++++----------- 6 files changed, 62 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb83064c..209a0df3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ The **"Breaking Changes"** listed below are changes that have been made in the d - Add new config options for reorganizing metatile attributes. - Add `setScale` to the scripting API. - Add option to turn off the checkerboard fill for new tilesets. +- Add option (via right-click) to copy wild encounters from another tab. ### Changed - Double-clicking on a connecting map on the Map or Events tabs will now open that map. diff --git a/include/core/wildmoninfo.h b/include/core/wildmoninfo.h index f0421948..d436ff1f 100644 --- a/include/core/wildmoninfo.h +++ b/include/core/wildmoninfo.h @@ -30,5 +30,6 @@ struct EncounterField { typedef QVector EncounterFields; WildMonInfo getDefaultMonInfo(EncounterField field); +void combineEncounters(WildMonInfo &to, WildMonInfo from); #endif // GUARD_WILDMONINFO_H diff --git a/src/core/wildmoninfo.cpp b/src/core/wildmoninfo.cpp index 91422e5b..3e884c84 100644 --- a/src/core/wildmoninfo.cpp +++ b/src/core/wildmoninfo.cpp @@ -14,3 +14,17 @@ WildMonInfo getDefaultMonInfo(EncounterField field) { return newInfo; } + +void combineEncounters(WildMonInfo &to, WildMonInfo from) { + to.encounterRate = from.encounterRate; + + if (to.wildPokemon.size() == from.wildPokemon.size()) { + to.wildPokemon = from.wildPokemon; + } + else if (to.wildPokemon.size() > from.wildPokemon.size()) { + to.wildPokemon = from.wildPokemon + to.wildPokemon.mid(from.wildPokemon.size()); + } + else { + to.wildPokemon = from.wildPokemon.mid(0, to.wildPokemon.size()); + } +} diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 8a6a5ec2..8fb5d519 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -2376,6 +2376,7 @@ void MainWindow::onTilesetsSaved(QString primaryTilesetLabel, QString secondaryT } void MainWindow::onWildMonDataChanged() { + editor->saveEncounterTabData(); markMapEdited(); } diff --git a/src/project.cpp b/src/project.cpp index 2526712b..faa61802 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -1563,7 +1563,9 @@ void Project::saveTextFile(QString path, QString text) { QFile file(path); if (file.open(QIODevice::WriteOnly)) { file.write(text.toUtf8()); - gFileCache[path] = text; + if (gFileCache.contains(path)) { + gFileCache[path] = text; + } } else { logError(QString("Could not open '%1' for writing: ").arg(path) + file.errorString()); } diff --git a/src/ui/montabwidget.cpp b/src/ui/montabwidget.cpp index 458a50e8..a18b516c 100644 --- a/src/ui/montabwidget.cpp +++ b/src/ui/montabwidget.cpp @@ -41,27 +41,52 @@ void MonTabWidget::populate() { } void MonTabWidget::askActivateTab(int tabIndex, QPoint menuPos) { - if (activeTabs[tabIndex]) return; + if (activeTabs[tabIndex]) { + // copy from another tab + QMenu contextMenu(this); - QMenu contextMenu(this); + for (int i = 0; i < this->tabBar()->count(); i++) { + if (tabIndex == i) continue; + if (!activeTabs[i]) continue; - QString tabText = tabBar()->tabText(tabIndex); - QAction actionActivateTab(QString("Add %1 data for this map...").arg(tabText), this); - connect(&actionActivateTab, &QAction::triggered, [=](){ - clearTableAt(tabIndex); - populateTab(tabIndex, getDefaultMonInfo(editor->project->wildMonFields.at(tabIndex))); - editor->saveEncounterTabData(); - setCurrentIndex(tabIndex); - emit editor->wildMonDataChanged(); - }); - contextMenu.addAction(&actionActivateTab); - contextMenu.exec(mapToGlobal(menuPos)); + QString tabText = this->tabBar()->tabText(i); + QAction *actionCopyFrom = new QAction(QString("Copy encounters from %1").arg(tabText), &contextMenu); + + connect(actionCopyFrom, &QAction::triggered, [=](){ + EncounterTableModel *model = static_cast(this->tableAt(i)->model()); + WildMonInfo copyInfo = model->encounterData(); + clearTableAt(tabIndex); + WildMonInfo newInfo = getDefaultMonInfo(editor->project->wildMonFields.at(tabIndex)); + combineEncounters(newInfo, copyInfo); + populateTab(tabIndex, newInfo); + emit editor->wildMonDataChanged(); + }); + + contextMenu.addAction(actionCopyFrom); + } + contextMenu.exec(mapToGlobal(menuPos)); + } + else { + QMenu contextMenu(this); + + QString tabText = tabBar()->tabText(tabIndex); + QAction actionActivateTab(QString("Add %1 data for this map...").arg(tabText), this); + connect(&actionActivateTab, &QAction::triggered, [=](){ + clearTableAt(tabIndex); + populateTab(tabIndex, getDefaultMonInfo(editor->project->wildMonFields.at(tabIndex))); + editor->saveEncounterTabData(); + setCurrentIndex(tabIndex); + emit editor->wildMonDataChanged(); + }); + contextMenu.addAction(&actionActivateTab); + contextMenu.exec(mapToGlobal(menuPos)); + } } void MonTabWidget::clearTableAt(int tabIndex) { QTableView *table = tableAt(tabIndex); if (table) { - table->setModel(nullptr); + table->reset(); table->horizontalHeader()->hide(); } } @@ -78,12 +103,12 @@ void MonTabWidget::populateTab(int tabIndex, WildMonInfo monInfo) { speciesTable->setItemDelegateForColumn(EncounterTableModel::ColumnType::MaxLevel, new SpinBoxDelegate(editor->project, this)); speciesTable->setItemDelegateForColumn(EncounterTableModel::ColumnType::EncounterRate, new SpinBoxDelegate(editor->project, this)); - speciesTable->horizontalHeader()->setSectionResizeMode(EncounterTableModel::ColumnType::Slot, QHeaderView::ResizeToContents ); - speciesTable->horizontalHeader()->setSectionResizeMode(EncounterTableModel::ColumnType::Group, QHeaderView::ResizeToContents ); + speciesTable->horizontalHeader()->setSectionResizeMode(EncounterTableModel::ColumnType::Slot, QHeaderView::ResizeToContents); + speciesTable->horizontalHeader()->setSectionResizeMode(EncounterTableModel::ColumnType::Group, QHeaderView::ResizeToContents); speciesTable->horizontalHeader()->setSectionResizeMode(EncounterTableModel::ColumnType::Species, QHeaderView::Stretch); speciesTable->horizontalHeader()->setSectionResizeMode(EncounterTableModel::ColumnType::MinLevel, QHeaderView::Stretch); speciesTable->horizontalHeader()->setSectionResizeMode(EncounterTableModel::ColumnType::MaxLevel, QHeaderView::Stretch); - speciesTable->horizontalHeader()->setSectionResizeMode(EncounterTableModel::ColumnType::SlotRatio, QHeaderView::Stretch); + speciesTable->horizontalHeader()->setSectionResizeMode(EncounterTableModel::ColumnType::SlotRatio, QHeaderView::ResizeToContents); speciesTable->horizontalHeader()->setSectionResizeMode(EncounterTableModel::ColumnType::EncounterChance, QHeaderView::ResizeToContents); speciesTable->horizontalHeader()->setSectionResizeMode(EncounterTableModel::ColumnType::EncounterRate, QHeaderView::ResizeToContents);