From f7022fc3d286f8e89dbd481aa57b272ac7612ffa Mon Sep 17 00:00:00 2001 From: garak Date: Wed, 26 Jun 2019 15:15:50 -0400 Subject: [PATCH] allow saving new encounter groups to maps --- include/core/wildmoninfo.h | 2 +- include/project.h | 1 + src/editor.cpp | 152 +++++++++++++++++++++++++++++++++---- src/mainwindow.cpp | 6 ++ src/project.cpp | 1 + src/ui/montabwidget.cpp | 5 ++ 6 files changed, 153 insertions(+), 14 deletions(-) diff --git a/include/core/wildmoninfo.h b/include/core/wildmoninfo.h index 414e0f35..728fe689 100644 --- a/include/core/wildmoninfo.h +++ b/include/core/wildmoninfo.h @@ -12,7 +12,7 @@ struct WildPokemon { struct WildMonInfo { // bool active = false; - int encounterRate; + int encounterRate = 0; QVector wildPokemon; }; diff --git a/include/project.h b/include/project.h index 6c0655bd..cec5ee8d 100644 --- a/include/project.h +++ b/include/project.h @@ -89,6 +89,7 @@ public: void readWildMonData(); QMap> wildMonData;// "MAP_CONSTANT": {base_label: wild_encounter_json QMap encounterMapToBaseLabel;// delete this? + QVector encounterGroupLabels; // when saving, preserve the extra fields for gBattlePyramidWildMonHeaders, gBattlePikeWildMonHeaders void readSpeciesIconPaths(); QMap speciesToIconPath; diff --git a/src/editor.cpp b/src/editor.cpp index ffe3bc43..4cf4bf41 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -184,9 +184,9 @@ void Editor::displayWildMonTables() { } // do I have to disconnect this signal? or set it up only once? - connect(labelCombo, QOverload::of(&QComboBox::activated), [=](int index){ - stack->setCurrentIndex(index);//setCurrentWidget(tabWidget); - }); + //connect(labelCombo, QOverload::of(&QComboBox::activated), [=](int index){ + // stack->setCurrentIndex(index);//setCurrentWidget(tabWidget); + //}); labelCombo->addItems(project->wildMonData[map->constantName].keys()); labelCombo->setCurrentText(project->wildMonData[map->constantName].firstKey()); @@ -206,7 +206,7 @@ void Editor::displayWildMonTables() { //QVector unusedIndices; int tabIndex = 0; - for (QPair> monField : project->wildMonFields) { + for (Field monField : project->wildMonFields) { QString fieldName = monField.first; // tabWidget_WildMons @@ -238,10 +238,133 @@ void Editor::displayWildMonTables() { stack->setCurrentIndex(0); } -// TODO: move this funciton +// TODO: move this funciton? void Editor::addNewWildMonGroup() { - // + // copy the currently displayed group? qDebug() << "new wild encounter group to map" << map->name; + + static bool earlyExit = false; + + if (earlyExit) return; + + // QStackedWidget *stack = ui->stackedWidget_WildMons + QStackedWidget *stack = ui->stackedWidget_WildMons; + QComboBox *labelCombo = ui->comboBox_EncounterGroupLabel; + + QDialog dialog(nullptr, Qt::WindowTitleHint | Qt::WindowCloseButtonHint); + dialog.setWindowTitle("New Wild Encounter Group Label"); + dialog.setWindowModality(Qt::NonModal); + + QFormLayout form(&dialog); + + QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &dialog); + + QLineEdit *lineEdit = new QLineEdit(); + form.addRow(new QLabel("Group Base Label:"), lineEdit); + QRegularExpressionValidator *validator = new QRegularExpressionValidator(QRegularExpression("[_A-Za-z0-9]*")); + lineEdit->setValidator(validator); + connect(lineEdit, &QLineEdit::textChanged, [this, &lineEdit, &buttonBox](QString text){ + if (this->project->encounterGroupLabels.contains(text)) { + QPalette palette = lineEdit->palette(); + QColor color = Qt::red; + color.setAlpha(25); + palette.setColor(QPalette::Base, color); + lineEdit->setPalette(palette); + buttonBox.button(QDialogButtonBox::Ok)->setDisabled(true); + } else { + lineEdit->setPalette(QPalette()); + buttonBox.button(QDialogButtonBox::Ok)->setEnabled(true); + } + }); + // give a default value to the label + lineEdit->setText(QString("g%1%2").arg(map->name).arg(project->wildMonData.value(map->constantName).size())); + + //WildPokemonHeader header; + //QString label; + //if (project->wildMonData.contains(map->constantName)) { + // label = project->wildMonData.value(map->constantName).keys().at(0);// copy the first one + //} + //if (!label.isEmpty()) { + // header = project->wildMonData.value(map->constantName).value(label); + //} else { + // TODO: change this to default or null + //WildPokemonHeader header;// = project->wildMonData.value(map->constantName).value(label); + //} + // Fields [x] copy from existing + QLabel *fieldsLabel = new QLabel("Fields:"); + form.addRow(fieldsLabel); + QCheckBox *copyCheckbox = new QCheckBox; + copyCheckbox->setEnabled(stack->count());//project->wildMonData.contains(map->constantName));// disable if there are no + form.addRow(new QLabel("Copy from current group"), copyCheckbox); + QVector fieldCheckboxes; + for (Field monField : project->wildMonFields) { + QCheckBox *fieldCheckbox = new QCheckBox; + fieldCheckboxes.append(fieldCheckbox); + form.addRow(new QLabel(monField.first), fieldCheckbox); + } + // TODO: fix this + connect(copyCheckbox, &QCheckBox::stateChanged, [=](int state){ + if (state == Qt::Checked) { + int fieldIndex = 0; + MonTabWidget *monWidget = static_cast(stack->widget(stack->currentIndex())); + for (Field monField : project->wildMonFields) { + fieldCheckboxes[fieldIndex]->setChecked(monWidget->isTabEnabled(fieldIndex));//header.wildMons.value(monField.first).active); + fieldIndex++; + } + } + }); + + connect(&buttonBox, &QDialogButtonBox::accepted, [&dialog, &lineEdit, this](){ + QString newLabel = lineEdit->text(); + if (!newLabel.isEmpty()) { + qDebug() << "new label:" << newLabel; + this->project->encounterGroupLabels.append(newLabel); + dialog.accept(); + } + }); + connect(&buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject())); + form.addRow(&buttonBox); + + if (dialog.exec() == QDialog::Accepted) { + qDebug() << "new encounter group dialog accepted"; + + WildPokemonHeader header; + for (Field monField : project->wildMonFields) { + // + QString fieldName = monField.first; + header.wildMons[fieldName].active = false; + header.wildMons[fieldName].encounterRate = 0; + } + + // TODO: check for this signal, or set it up somewhere else + //connect(labelCombo, QOverload::of(&QComboBox::currentIndexChanged), [=](int index){ + // stack->setCurrentIndex(index);//setCurrentWidget(tabWidget); + //}); + + MonTabWidget *tabWidget = new MonTabWidget(project); + stack->insertWidget(stack->count(), tabWidget);//project->wildMonData[map->constantName].keys().size(), tabWidget); + + labelCombo->addItem(lineEdit->text()); + labelCombo->setCurrentIndex(labelCombo->count() - 1); + + // populate it -- check for copyFrom checked + int tabIndex = 0; + for (Field monField : project->wildMonFields) { + QString fieldName = monField.first; + //header.wildMons[fieldName].active = fieldCheckboxes[tabIndex]->isChecked(); + tabWidget->clearTableAt(tabIndex); + if (fieldCheckboxes[tabIndex]->isChecked()) {//(header.wildMons[fieldName].active) { + qDebug() << "checked" << fieldName; + header.wildMons.insert(fieldName, getDefaultMonInfo(monField)); + tabWidget->populateTab(tabIndex, header.wildMons[fieldName], fieldName); + } else { + tabWidget->setTabActive(tabIndex, false); + } + tabIndex++; + } + } + + //earlyExit = true; } void Editor::configureEncounterJSON() { @@ -251,22 +374,25 @@ void Editor::configureEncounterJSON() { // TODO: move this? void Editor::saveEncounterTabData() { - if (!project->wildMonData.contains(map->constantName)) { - qDebug() << "don't save encounters for this map"; - return; - } + //if (!project->wildMonData.contains(map->constantName)) { + // qDebug() << "don't save encounters for this map"; + // return; + //} // QStackedWidget *stack = ui->stackedWidget_WildMons; QComboBox *labelCombo = ui->comboBox_EncounterGroupLabel; + if (!stack->count()) return; + // TODO: verify that this exists before accsessing so no seg fault QMap &encounterMap = project->wildMonData[map->constantName]; - for (int groupIndex = 0; groupIndex < encounterMap.keys().size(); groupIndex++) { + //for (int groupIndex = 0; groupIndex < encounterMap.keys().size(); groupIndex++) { + for (int groupIndex = 0; groupIndex < stack->count(); groupIndex++) { // MonTabWidget *tabWidget = static_cast(stack->widget(groupIndex)); - // TODO: verify this exists before so no segfault + // TODO: verify this exists before so no segfault actually no WildPokemonHeader &encounterHeader = encounterMap[labelCombo->itemText(groupIndex)]; //qDebug() << encounterHeader.wildMons.keys(); @@ -275,7 +401,7 @@ void Editor::saveEncounterTabData() { // QTableWidget *monTable = //} int fieldIndex = 0; - for (QPair> monField : project->wildMonFields) { + for (Field monField : project->wildMonFields) { QString fieldName = monField.first; //if (!encounterHeader.wildMons.contains(fieldName) // || encounterHeader.wildMons[fieldName].wildPokemon.empty() diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index a236dc65..c5ee7634 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -80,6 +80,12 @@ void MainWindow::initCustomUI() { ui->mapList->setContextMenuPolicy(Qt::CustomContextMenu); connect(ui->mapList, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(onOpenMapListContextMenu(const QPoint &))); + + QStackedWidget *stack = ui->stackedWidget_WildMons; + QComboBox *labelCombo = ui->comboBox_EncounterGroupLabel; + connect(labelCombo, QOverload::of(&QComboBox::currentIndexChanged), [=](int index){ + stack->setCurrentIndex(index); + }); } void MainWindow::initExtraSignals() { diff --git a/src/project.cpp b/src/project.cpp index e925a412..a9925e9d 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -1446,6 +1446,7 @@ void Project::readWildMonData() { // wildMonData.insert(mapConstant, header); //else wildMonData[mapConstant].insert(encounter["base_label"].toString(), header); + encounterGroupLabels.append(encounter["base_label"].toString()); } } diff --git a/src/ui/montabwidget.cpp b/src/ui/montabwidget.cpp index 8a6cbb6c..b5126a4a 100644 --- a/src/ui/montabwidget.cpp +++ b/src/ui/montabwidget.cpp @@ -159,6 +159,11 @@ void MonTabWidget::createSpeciesTableRow(QTableWidget *table, WildPokemon mon, i minLevel->setValue(mon.minLevel); maxLevel->setValue(mon.maxLevel); + // connect level spinboxes so max is never less than min + connect(minLevel, QOverload::of(&QSpinBox::valueChanged), [maxLevel](int min){ + maxLevel->setMinimum(min); + }); + int fieldIndex = 0; for (auto field : project->wildMonFields) { if (field.first == fieldName) break;