keep track of encounter data when navigating porymap

This commit is contained in:
garak 2019-09-30 22:24:39 -04:00 committed by huderlem
parent 9e03ac5e14
commit 55573099b5
5 changed files with 50 additions and 36 deletions

View file

@ -6,14 +6,14 @@
#include <QtWidgets> #include <QtWidgets>
#include <QVector> #include <QVector>
class Project; class Editor;
class MonTabWidget : public QTabWidget { class MonTabWidget : public QTabWidget {
Q_OBJECT Q_OBJECT
public: public:
explicit MonTabWidget(Project *project = nullptr, QWidget *parent = nullptr); explicit MonTabWidget(Editor *editor = nullptr, QWidget *parent = nullptr);
~MonTabWidget(){}; ~MonTabWidget(){};
void populate(); void populate();
@ -35,7 +35,7 @@ private:
QVector<bool> activeTabs; QVector<bool> activeTabs;
Project *project; Editor *editor;
}; };
#endif // MONTABWIDGET_H #endif // MONTABWIDGET_H

View file

@ -1,6 +1,5 @@
#include "wildmoninfo.h" #include "wildmoninfo.h"
#include "montabwidget.h" #include "montabwidget.h"
#include "project.h"

View file

@ -184,7 +184,7 @@ void Editor::displayWildMonTables() {
WildPokemonHeader header = project->wildMonData.value(map->constantName).value(label); WildPokemonHeader header = project->wildMonData.value(map->constantName).value(label);
MonTabWidget *tabWidget = new MonTabWidget(project); MonTabWidget *tabWidget = new MonTabWidget(this);
stack->insertWidget(labelIndex, tabWidget); stack->insertWidget(labelIndex, tabWidget);
int tabIndex = 0; int tabIndex = 0;
@ -281,20 +281,20 @@ void Editor::addNewWildMonGroup(QWidget *window) {
if (dialog.exec() == QDialog::Accepted) { if (dialog.exec() == QDialog::Accepted) {
WildPokemonHeader header; WildPokemonHeader header;
for (EncounterField monField : project->wildMonFields) { for (EncounterField& monField : project->wildMonFields) {
QString fieldName = monField.name; QString fieldName = monField.name;
header.wildMons[fieldName].active = false; header.wildMons[fieldName].active = false;
header.wildMons[fieldName].encounterRate = 0; header.wildMons[fieldName].encounterRate = 0;
} }
MonTabWidget *tabWidget = new MonTabWidget(project); MonTabWidget *tabWidget = new MonTabWidget(this);
stack->insertWidget(stack->count(), tabWidget); stack->insertWidget(stack->count(), tabWidget);
labelCombo->addItem(lineEdit->text()); labelCombo->addItem(lineEdit->text());
labelCombo->setCurrentIndex(labelCombo->count() - 1); labelCombo->setCurrentIndex(labelCombo->count() - 1);
int tabIndex = 0; int tabIndex = 0;
for (EncounterField monField : project->wildMonFields) { for (EncounterField &monField : project->wildMonFields) {
QString fieldName = monField.name; QString fieldName = monField.name;
tabWidget->clearTableAt(tabIndex); tabWidget->clearTableAt(tabIndex);
if (fieldCheckboxes[tabIndex]->isChecked()) { if (fieldCheckboxes[tabIndex]->isChecked()) {
@ -313,6 +313,7 @@ void Editor::addNewWildMonGroup(QWidget *window) {
} }
tabIndex++; tabIndex++;
} }
saveEncounterTabData();
} }
} }
@ -544,6 +545,7 @@ void Editor::configureEncounterJSON(QWidget *window) {
} }
void Editor::saveEncounterTabData() { void Editor::saveEncounterTabData() {
// This function does not save to disk so it is safe to use before user clicks Save.
QStackedWidget *stack = ui->stackedWidget_WildMons; QStackedWidget *stack = ui->stackedWidget_WildMons;
QComboBox *labelCombo = ui->comboBox_EncounterGroupLabel; QComboBox *labelCombo = ui->comboBox_EncounterGroupLabel;

View file

@ -1096,6 +1096,9 @@ void MainWindow::on_tabWidget_currentChanged(int index)
} else if (index == 3) { } else if (index == 3) {
editor->setEditingConnections(); editor->setEditingConnections();
} }
if (index != 4) {
editor->saveEncounterTabData();
}
} }
void MainWindow::on_actionUndo_triggered() void MainWindow::on_actionUndo_triggered()

View file

@ -1,9 +1,9 @@
#include "montabwidget.h" #include "montabwidget.h"
#include "noscrollcombobox.h" #include "noscrollcombobox.h"
#include "project.h" #include "editor.h"
MonTabWidget::MonTabWidget(Project *project, QWidget *parent) : QTabWidget(parent) { MonTabWidget::MonTabWidget(Editor *editor, QWidget *parent) : QTabWidget(parent) {
this->project = project; this->editor = editor;
populate(); populate();
installEventFilter(this); installEventFilter(this);
} }
@ -22,7 +22,7 @@ bool MonTabWidget::eventFilter(QObject *, QEvent *event) {
} }
void MonTabWidget::populate() { void MonTabWidget::populate() {
EncounterFields fields = project->wildMonFields; EncounterFields fields = editor->project->wildMonFields;
activeTabs = QVector<bool>(fields.size(), false); activeTabs = QVector<bool>(fields.size(), false);
for (EncounterField field : fields) { for (EncounterField field : fields) {
@ -44,7 +44,8 @@ void MonTabWidget::askActivateTab(int tabIndex, QPoint menuPos) {
QAction actionActivateTab(QString("Add %1 data for this map...").arg(tabText), this); QAction actionActivateTab(QString("Add %1 data for this map...").arg(tabText), this);
connect(&actionActivateTab, &QAction::triggered, [=](){ connect(&actionActivateTab, &QAction::triggered, [=](){
clearTableAt(tabIndex); clearTableAt(tabIndex);
populateTab(tabIndex, getDefaultMonInfo(project->wildMonFields.at(tabIndex)), tabText); populateTab(tabIndex, getDefaultMonInfo(editor->project->wildMonFields.at(tabIndex)), tabText);
editor->saveEncounterTabData();
setCurrentIndex(tabIndex); setCurrentIndex(tabIndex);
}); });
contextMenu.addAction(&actionActivateTab); contextMenu.addAction(&actionActivateTab);
@ -63,12 +64,12 @@ void MonTabWidget::populateTab(int tabIndex, WildMonInfo monInfo, QString fieldN
QTableWidget *speciesTable = tableAt(tabIndex); QTableWidget *speciesTable = tableAt(tabIndex);
int fieldIndex = 0; int fieldIndex = 0;
for (EncounterField field : project->wildMonFields) { for (EncounterField field : editor->project->wildMonFields) {
if (field.name == fieldName) break; if (field.name == fieldName) break;
fieldIndex++; fieldIndex++;
} }
bool insertGroupLabel = false; bool insertGroupLabel = false;
if (!project->wildMonFields[fieldIndex].groups.isEmpty()) insertGroupLabel = true; if (!editor->project->wildMonFields[fieldIndex].groups.isEmpty()) insertGroupLabel = true;
speciesTable->setRowCount(monInfo.wildPokemon.size()); speciesTable->setRowCount(monInfo.wildPokemon.size());
speciesTable->setColumnCount(insertGroupLabel ? 8 : 7); speciesTable->setColumnCount(insertGroupLabel ? 8 : 7);
@ -93,6 +94,9 @@ void MonTabWidget::populateTab(int tabIndex, WildMonInfo monInfo, QString fieldN
encounterRate->setMinimum(0); encounterRate->setMinimum(0);
encounterRate->setMaximum(180); encounterRate->setMaximum(180);
encounterRate->setValue(monInfo.encounterRate); encounterRate->setValue(monInfo.encounterRate);
connect(encounterRate, QOverload<int>::of(&QSpinBox::valueChanged), [this](int) {
editor->saveEncounterTabData();
});
encounterLayout->addWidget(encounterRate); encounterLayout->addWidget(encounterRate);
encounterFrame->setLayout(encounterLayout); encounterFrame->setLayout(encounterLayout);
speciesTable->setCellWidget(0, insertGroupLabel? 7 : 6, encounterFrame); speciesTable->setCellWidget(0, insertGroupLabel? 7 : 6, encounterFrame);
@ -105,7 +109,7 @@ void MonTabWidget::populateTab(int tabIndex, WildMonInfo monInfo, QString fieldN
} }
void MonTabWidget::createSpeciesTableRow(QTableWidget *table, WildPokemon mon, int index, QString fieldName) { void MonTabWidget::createSpeciesTableRow(QTableWidget *table, WildPokemon mon, int index, QString fieldName) {
QPixmap monIcon = QPixmap(project->speciesToIconPath.value(mon.species)).copy(0, 0, 32, 32); QPixmap monIcon = QPixmap(editor->project->speciesToIconPath.value(mon.species)).copy(0, 0, 32, 32);
QLabel *monNum = new QLabel(QString("%1.").arg(QString::number(index))); QLabel *monNum = new QLabel(QString("%1.").arg(QString::number(index)));
@ -113,56 +117,62 @@ void MonTabWidget::createSpeciesTableRow(QTableWidget *table, WildPokemon mon, i
monLabel->setPixmap(monIcon); monLabel->setPixmap(monIcon);
NoScrollComboBox *monSelector = new NoScrollComboBox; NoScrollComboBox *monSelector = new NoScrollComboBox;
monSelector->addItems(project->speciesToIconPath.keys()); monSelector->addItems(editor->project->speciesToIconPath.keys());
monSelector->setCurrentText(mon.species); monSelector->setCurrentText(mon.species);
monSelector->setEditable(true); monSelector->setEditable(true);
QObject::connect(monSelector, &QComboBox::currentTextChanged, [=](QString newSpecies){ QObject::connect(monSelector, &QComboBox::currentTextChanged, [=](QString newSpecies) {
QPixmap monIcon = QPixmap(project->speciesToIconPath.value(newSpecies)).copy(0, 0, 32, 32); QPixmap monIcon = QPixmap(editor->project->speciesToIconPath.value(newSpecies)).copy(0, 0, 32, 32);
monLabel->setPixmap(monIcon); monLabel->setPixmap(monIcon);
if (!monIcon.isNull()) editor->saveEncounterTabData();
}); });
QSpinBox *minLevel = new QSpinBox; QSpinBox *minLevel = new QSpinBox;
QSpinBox *maxLevel = new QSpinBox; QSpinBox *maxLevel = new QSpinBox;
minLevel->setMinimum(project->miscConstants.value("min_level_define").toInt()); minLevel->setMinimum(editor->project->miscConstants.value("min_level_define").toInt());
minLevel->setMaximum(project->miscConstants.value("max_level_define").toInt()); minLevel->setMaximum(editor->project->miscConstants.value("max_level_define").toInt());
maxLevel->setMinimum(project->miscConstants.value("min_level_define").toInt()); maxLevel->setMinimum(editor->project->miscConstants.value("min_level_define").toInt());
maxLevel->setMaximum(project->miscConstants.value("max_level_define").toInt()); maxLevel->setMaximum(editor->project->miscConstants.value("max_level_define").toInt());
minLevel->setValue(mon.minLevel); minLevel->setValue(mon.minLevel);
maxLevel->setValue(mon.maxLevel); maxLevel->setValue(mon.maxLevel);
// Connect level spinboxes so max is never less than min. // Connect level spinboxes so max is never less than min.
connect(minLevel, QOverload<int>::of(&QSpinBox::valueChanged), [maxLevel](int min){ connect(minLevel, QOverload<int>::of(&QSpinBox::valueChanged), [maxLevel, this](int min) {
maxLevel->setMinimum(min); maxLevel->setMinimum(min);
editor->saveEncounterTabData();
});
connect(maxLevel, QOverload<int>::of(&QSpinBox::valueChanged), [maxLevel, this](int) {
editor->saveEncounterTabData();
}); });
int fieldIndex = 0; int fieldIndex = 0;
for (EncounterField field : project->wildMonFields) { for (EncounterField field : editor->project->wildMonFields) {
if (field.name == fieldName) break; if (field.name == fieldName) break;
fieldIndex++; fieldIndex++;
} }
double slotChanceTotal = 0.0; double slotChanceTotal = 0.0;
if (!project->wildMonFields[fieldIndex].groups.isEmpty()) { if (!editor->project->wildMonFields[fieldIndex].groups.isEmpty()) {
for (QString groupKey : project->wildMonFields[fieldIndex].groups.keys()) { for (QString groupKey : editor->project->wildMonFields[fieldIndex].groups.keys()) {
if (project->wildMonFields[fieldIndex].groups[groupKey].contains(index)) { if (editor->project->wildMonFields[fieldIndex].groups[groupKey].contains(index)) {
for (int chanceIndex : project->wildMonFields[fieldIndex].groups[groupKey]) { for (int chanceIndex : editor->project->wildMonFields[fieldIndex].groups[groupKey]) {
slotChanceTotal += static_cast<double>(project->wildMonFields[fieldIndex].encounterRates[chanceIndex]); slotChanceTotal += static_cast<double>(editor->project->wildMonFields[fieldIndex].encounterRates[chanceIndex]);
} }
break; break;
} }
} }
} else { } else {
for (auto chance : project->wildMonFields[fieldIndex].encounterRates) { for (auto chance : editor->project->wildMonFields[fieldIndex].encounterRates) {
slotChanceTotal += static_cast<double>(chance); slotChanceTotal += static_cast<double>(chance);
} }
} }
QLabel *percentLabel = new QLabel(QString("%1%").arg( QLabel *percentLabel = new QLabel(QString("%1%").arg(
QString::number(project->wildMonFields[fieldIndex].encounterRates[index] / slotChanceTotal * 100.0, 'f', 2) QString::number(editor->project->wildMonFields[fieldIndex].encounterRates[index] / slotChanceTotal * 100.0, 'f', 2)
)); ));
QLabel *ratioLabel = new QLabel(QString("%1").arg( QLabel *ratioLabel = new QLabel(QString("%1").arg(
QString::number(project->wildMonFields[fieldIndex].encounterRates[index] QString::number(editor->project->wildMonFields[fieldIndex].encounterRates[index]
))); )));
QFrame *speciesSelector = new QFrame; QFrame *speciesSelector = new QFrame;
@ -182,12 +192,12 @@ void MonTabWidget::createSpeciesTableRow(QTableWidget *table, WildPokemon mon, i
maxLevelFrame->setLayout(maxLevelSpinboxLayout); maxLevelFrame->setLayout(maxLevelSpinboxLayout);
bool insertGroupLabel = false; bool insertGroupLabel = false;
if (!project->wildMonFields[fieldIndex].groups.isEmpty()) insertGroupLabel = true; if (!editor->project->wildMonFields[fieldIndex].groups.isEmpty()) insertGroupLabel = true;
table->setCellWidget(index, 0, monNum); table->setCellWidget(index, 0, monNum);
if (insertGroupLabel) { if (insertGroupLabel) {
QString groupName = QString(); QString groupName = QString();
for (QString groupKey : project->wildMonFields[fieldIndex].groups.keys()) { for (QString groupKey : editor->project->wildMonFields[fieldIndex].groups.keys()) {
if (project->wildMonFields[fieldIndex].groups[groupKey].contains(index)) { if (editor->project->wildMonFields[fieldIndex].groups[groupKey].contains(index)) {
groupName = groupKey; groupName = groupKey;
break; break;
} }