continue work on wild encounter editor

This commit is contained in:
garak 2019-06-15 18:49:30 -04:00
parent 1369113a1b
commit 4b0e0b0bff
5 changed files with 82 additions and 21 deletions

View file

@ -29,9 +29,9 @@ struct WildPokemonHeader {
class Project;
QWidget *newSpeciesTableEntry(Project *project, WildPokemon mon, int index);
void createSpeciesTableRow(Project *, QTableWidget *, WildPokemon, int);
void createSpeciesTableRow(Project *, QTableWidget *, WildPokemon, int, QString);
void clearTabWidget(QLayout *tab);
void clearTable(QTableWidget *table);
void populateWildMonTabWidget(QTabWidget *tabWidget, QVector<QString> fields);
void populateWildMonTabWidget(QTabWidget *tabWidget, QVector<QPair<QString, QVector<int>>> fields);
#endif // GUARD_WILDMONINFO_H

View file

@ -10,6 +10,8 @@
#include <QStringList>
#include <QList>
#include <QVector>
#include <QPair>
#include <QStandardItem>
static QString NONE_MAP_CONSTANT = "MAP_NONE";
@ -86,10 +88,14 @@ public:
void readWildMonData();
QMap<QString, WildPokemonHeader> wildMonData;// "MAP_CONSTANT": wild_encounter_json
QMap<QString, QString> encounterMapToBaseLabel;
// when saving, preserve the extra fields for gBattlePyramidWildMonHeaders, gBattlePikeWildMonHeaders
void readSpeciesIconPaths();
QMap<QString, QString> speciesToIconPath;
QVector<QString> wildMonFields;
//QVector<QString> wildMonFields;
QVector<QPair<QString, QVector<int>>> wildMonFields;
// temporary?
QMap<QString, QJsonObject> extraEncounterGroups;
QMap<QString, bool> getTopLevelMapFields();
bool loadMapData(Map*);
@ -109,6 +115,7 @@ public:
void saveAllDataStructures();
void saveMapLayouts();
void saveMapGroups();
void saveWildMonData();
void saveMapConstantsHeader();
void saveHealLocationStruct(Map*);
void saveTilesets(Tileset*, Tileset*);

View file

@ -20,14 +20,13 @@ void clearTabWidget(QLayout *tab) {
}
void clearTable(QTableWidget *table) {
//
if (table) {
table->clear();
table->horizontalHeader()->hide();
}
}
void createSpeciesTableRow(Project *project, QTableWidget *table, WildPokemon mon, int index) {
void createSpeciesTableRow(Project *project, QTableWidget *table, WildPokemon mon, int index, QString fieldName) {
//
QPixmap monIcon = QPixmap(project->speciesToIconPath.value(mon.species)).copy(0, 0, 32, 32);
@ -57,8 +56,14 @@ void createSpeciesTableRow(Project *project, QTableWidget *table, WildPokemon mo
minLevel->setValue(mon.minLevel);
maxLevel->setValue(mon.maxLevel);
// percentage -- add to json settings
QLabel *percentLabel = new QLabel(landPercentages[index]);
int fieldIndex = 0;
for (auto field : project->wildMonFields) {
if (field.first == fieldName) break;
fieldIndex++;
}
QLabel *percentLabel = new QLabel(QString("%1%").arg(
QString::number(project->wildMonFields[fieldIndex].second[index - 1]
)));
QFrame *speciesSelector = new QFrame;
QHBoxLayout *speciesSelectorLayout = new QHBoxLayout;
@ -84,7 +89,7 @@ void createSpeciesTableRow(Project *project, QTableWidget *table, WildPokemon mo
table->setCellWidget(index - 1, 4, percentLabel);
}
void populateWildMonTabWidget(QTabWidget *tabWidget, QVector<QString> fields) {
void populateWildMonTabWidget(QTabWidget *tabWidget, QVector<QPair<QString, QVector<int>>> fields) {
QPushButton *newTabButton = new QPushButton("Configure JSON...");
QObject::connect(newTabButton, &QPushButton::clicked, [=](){
// TODO
@ -92,12 +97,12 @@ void populateWildMonTabWidget(QTabWidget *tabWidget, QVector<QString> fields) {
});
tabWidget->setCornerWidget(newTabButton);
for (QString field : fields) {
for (QPair<QString, QVector<int>> field : fields) {
QTableWidget *table = new QTableWidget;
table->setEditTriggers(QAbstractItemView::NoEditTriggers);
table->setFocusPolicy(Qt::NoFocus);
table->setSelectionMode(QAbstractItemView::NoSelection);
table->clearFocus();
tabWidget->addTab(table, field);
tabWidget->addTab(table, field.first);
}
}

View file

@ -158,7 +158,8 @@ void Editor::displayWildMonTables() {
WildPokemonHeader header = project->wildMonData.value(map->constantName);
int tabIndex = 0;
for (QString field : project->wildMonFields) {
for (QPair<QString, QVector<int>> monField : project->wildMonFields) {
QString field = monField.first;
QTableWidget *speciesTable = static_cast<QTableWidget *>(ui->tabWidget_WildMons->widget(tabIndex++));
clearTable(speciesTable);
//speciesTable->horizontalHeader()->hide();
@ -170,7 +171,7 @@ void Editor::displayWildMonTables() {
speciesTable->setColumnCount(6);// TODO: stretch last column?
QStringList landMonTableHeaders;
landMonTableHeaders << "Index" << "Species" << "Min Level" << "Max Level" << "Catch Percentage" << "Encounter Rate";
landMonTableHeaders << "Index" << "Species" << "Min Level" << "Max Level" << "Index Percentage" << "Encounter Rate";
speciesTable->setHorizontalHeaderLabels(landMonTableHeaders);
speciesTable->horizontalHeader()->show();
speciesTable->verticalHeader()->hide();
@ -202,7 +203,7 @@ void Editor::displayWildMonTables() {
speciesTable->setCellWidget(0, 5, encounterFrame);
for (WildPokemon mon : header.wildMons[field].wildPokemon) {
createSpeciesTableRow(project, speciesTable, mon, i);
createSpeciesTableRow(project, speciesTable, mon, i, field);
i++;
}
} else {

View file

@ -520,6 +520,48 @@ void Project::saveMapGroups() {
mapGroupsFile.write(mapGroupsDoc.toJson());
}
void Project::saveWildMonData() {
//
//QString wildEncountersJsonFilepath = QString("%1/src/data/wild_encounters.json").arg(root);
QString wildEncountersJsonFilepath = QString("%1/src/data/wild_encounters_test.json").arg(root);
QFile wildEncountersFile(wildEncountersJsonFilepath);
if (!wildEncountersFile.open(QIODevice::WriteOnly)) {
logError(QString("Error: Could not open %1 for writing").arg(wildEncountersJsonFilepath));
return;
}
QJsonObject wildEncountersObject;
QJsonArray wildEncounterGroups = QJsonArray();
// gWildMonHeaders
QJsonObject monHeadersObject;
monHeadersObject["label"] = "gWildMonHeaders";
monHeadersObject["for_maps"] = true;
QJsonArray encountersArray = QJsonArray();
for (QString key : wildMonData.keys()) {
QJsonObject encounterObject;
encounterObject["map"] = key;
encounterObject["base_label"] = encounterMapToBaseLabel[key];
//
//text += key + "\n";
// ["base_label"] = encounterMapToBaseLabel[mapConstant]
encountersArray.append(encounterObject);
}
monHeadersObject["encounters"] = encountersArray;
wildEncounterGroups.append(monHeadersObject);
// add extra Json objects that are not associated with maps to the file
for (QString label : extraEncounterGroups.keys()) {
qDebug() << "extra label:" << label;
wildEncounterGroups.append(extraEncounterGroups[label]);
}
wildEncountersObject["wild_encounter_groups"] = wildEncounterGroups;
QJsonDocument wildEncountersDoc(wildEncountersObject);
wildEncountersFile.write(wildEncountersDoc.toJson());
wildEncountersFile.close();
}
void Project::saveMapConstantsHeader() {
QString text = QString("#ifndef GUARD_CONSTANTS_MAP_GROUPS_H\n");
text += QString("#define GUARD_CONSTANTS_MAP_GROUPS_H\n");
@ -1066,6 +1108,7 @@ void Project::saveAllDataStructures() {
saveMapLayouts();
saveMapGroups();
saveMapConstantsHeader();
saveWildMonData();
}
void Project::loadTilesetAssets(Tileset* tileset) {
@ -1306,7 +1349,6 @@ void Project::deleteFile(QString path) {
}
void Project::readWildMonData() {
qDebug() << "Project::readWildMonData";// speed testing
//
QString wildMonJsonFilepath = QString("%1/src/data/wild_encounters.json").arg(root);
QJsonDocument wildMonsJsonDoc;
@ -1319,24 +1361,30 @@ void Project::readWildMonData() {
for (auto subObjectRef : wildMonObj["wild_encounter_groups"].toArray()) {
QJsonObject subObject = subObjectRef.toObject();
if (!subObject["for_maps"].toBool()) continue;
if (!subObject["for_maps"].toBool()) {
extraEncounterGroups.insert(subObject["label"].toString(), subObject);
continue;
}
// fill wildMonFields
for (auto field : subObject["fields"].toArray()) {
wildMonFields.append(field.toString());
QPair<QString, QVector<int>> encounterField;
encounterField.first = field.toObject()["type"].toString();
for (auto val : field.toObject()["encounter_rates"].toArray())
encounterField.second.append(val.toInt());
wildMonFields.append(encounterField);
}
QJsonArray encounters = subObject["encounters"].toArray();
for (QJsonValue encounter : encounters) {
//
//qDebug() << encounter["map"].toString();
QString mapConstant = encounter["map"].toString();
//QString mapName = mapConstantsToMapNames->value(mapConstant);
encounterMapToBaseLabel.insert(mapConstant, encounter["base_label"].toString());
WildPokemonHeader header;
for (QString field : wildMonFields) {
for (QPair<QString, QVector<int>> monField : wildMonFields) {
//
QString field = monField.first;
if (encounter[field] != QJsonValue::Undefined) {
header.wildMons[field].active = true;
header.wildMons[field].encounterRate = encounter[field]["encounter_rate"].toInt();