Support custom fields for top-level map attributes

This commit is contained in:
Marcus Huderle 2019-02-03 10:38:45 -06:00 committed by huderlem
parent d70145d89a
commit a894bea71b
8 changed files with 455 additions and 197 deletions

View file

@ -14,8 +14,8 @@
<string>porymap</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QSplitter" name="splitter_main">
<property name="orientation">
<enum>Qt::Horizontal</enum>
@ -1856,21 +1856,36 @@
</layout>
</widget>
<widget class="QWidget" name="tab_Attributes">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<attribute name="title">
<string>Header</string>
</attribute>
<layout class="QVBoxLayout" name="gridLayout_Attributes">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QFrame" name="frame_3">
<property name="enabled">
<bool>false</bool>
</property>
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>381</width>
<height>311</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
@ -1884,9 +1899,15 @@
<enum>QFrame::Raised</enum>
</property>
<layout class="QFormLayout" name="formLayout_2">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::FieldsStayAtSizeHint</enum>
</property>
<property name="verticalSpacing">
<number>12</number>
</property>
<property name="bottomMargin">
<number>9</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
@ -2059,6 +2080,113 @@
</item>
</layout>
</widget>
</item>
<item>
<widget class="QFrame" name="frame_keyValue">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout_10">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Custom Fields</string>
</property>
</widget>
</item>
<item>
<widget class="QFrame" name="frame">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_8">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QPushButton" name="pushButton_AddCustomHeaderField">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_DeleteCustomHeaderField">
<property name="text">
<string>Delete</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QTableWidget" name="tableWidget_CustomHeaderFields">
<property name="toolTip">
<string>Custom fields will be added to the map.json file for the current map.</string>
</property>
<attribute name="horizontalHeaderVisible">
<bool>true</bool>
</attribute>
<attribute name="horizontalHeaderCascadingSectionResizes">
<bool>false</bool>
</attribute>
<attribute name="horizontalHeaderHighlightSections">
<bool>true</bool>
</attribute>
<attribute name="horizontalHeaderShowSortIndicator" stdset="0">
<bool>false</bool>
</attribute>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string>Key</string>
</property>
</column>
<column>
<property name="text">
<string>Value</string>
</property>
</column>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_Connections">
<attribute name="title">

View file

@ -38,6 +38,7 @@ public:
QString battle_scene;
QString sharedEventsMap = "";
QString sharedScriptsMap = "";
QMap<QString, QString> customHeaders;
MapLayout *layout;
bool isPersistedToFile = true;
bool needsLayoutDir = true;

View file

@ -74,6 +74,7 @@ public:
void updatePrimaryTileset(QString tilesetLabel, bool forceLoad = false);
void updateSecondaryTileset(QString tilesetLabel, bool forceLoad = false);
void toggleBorderVisibility(bool visible);
void updateCustomMapHeaderValues(QTableWidget *);
Tileset *getCurrentMapPrimaryTileset();
DraggablePixmapItem *addMapEvent(Event *event);

View file

@ -152,12 +152,12 @@ private slots:
void selectedEventIndexChanged(int index);
void on_horizontalSlider_CollisionTransparency_valueChanged(int value);
void on_toolButton_ExpandAll_clicked();
void on_toolButton_CollapseAll_clicked();
void on_actionAbout_Porymap_triggered();
void on_pushButton_AddCustomHeaderField_clicked();
void on_pushButton_DeleteCustomHeaderField_clicked();
void on_tableWidget_CustomHeaderFields_cellChanged(int row, int column);
private:
Ui::MainWindow *ui;

View file

@ -70,6 +70,7 @@ public:
QList<QStringList>* getLabelMacros(QList<QStringList>*, QString);
QStringList* getLabelValues(QList<QStringList>*, QString);
QMap<QString, bool> getTopLevelMapFields();
bool loadMapData(Map*);
void readMapLayouts();
void loadMapLayout(Map*);

View file

@ -1109,6 +1109,21 @@ void Editor::toggleBorderVisibility(bool visible)
this->setConnectionsVisibility(visible);
}
void Editor::updateCustomMapHeaderValues(QTableWidget *table)
{
QMap<QString, QString> fields;
for (int row = 0; row < table->rowCount(); row++) {
QString keyStr = "";
QString valueStr = "";
QTableWidgetItem *key = table->item(row, 0);
QTableWidgetItem *value = table->item(row, 1);
if (key) keyStr = key->text();
if (value) valueStr = value->text();
fields[keyStr] = valueStr;
}
map->customHeaders = fields;
}
Tileset* Editor::getCurrentMapPrimaryTileset()
{
QString tilesetLabel = map->layout->tileset_primary_label;

View file

@ -486,6 +486,17 @@ void MainWindow::displayMapProperties() {
ui->checkBox_AllowRunning->setChecked(map->allowRunning.toInt() > 0 || map->allowRunning == "TRUE");
ui->checkBox_AllowBiking->setChecked(map->allowBiking.toInt() > 0 || map->allowBiking == "TRUE");
ui->checkBox_AllowEscapeRope->setChecked(map->allowEscapeRope.toInt() > 0 || map->allowEscapeRope == "TRUE");
// Custom fields table.
ui->tableWidget_CustomHeaderFields->blockSignals(true);
ui->tableWidget_CustomHeaderFields->setRowCount(0);
for (auto it = map->customHeaders.begin(); it != map->customHeaders.end(); it++) {
int rowIndex = ui->tableWidget_CustomHeaderFields->rowCount();
ui->tableWidget_CustomHeaderFields->insertRow(rowIndex);
ui->tableWidget_CustomHeaderFields->setItem(rowIndex, 0, new QTableWidgetItem(it.key()));
ui->tableWidget_CustomHeaderFields->setItem(rowIndex, 1, new QTableWidgetItem(it.value()));
}
ui->tableWidget_CustomHeaderFields->blockSignals(false);
}
void MainWindow::on_comboBox_Song_activated(const QString &song)
@ -1903,6 +1914,42 @@ void MainWindow::on_actionAbout_Porymap_triggered()
window->show();
}
void MainWindow::on_pushButton_AddCustomHeaderField_clicked()
{
int rowIndex = this->ui->tableWidget_CustomHeaderFields->rowCount();
this->ui->tableWidget_CustomHeaderFields->insertRow(rowIndex);
this->ui->tableWidget_CustomHeaderFields->selectRow(rowIndex);
this->editor->updateCustomMapHeaderValues(this->ui->tableWidget_CustomHeaderFields);
}
void MainWindow::on_pushButton_DeleteCustomHeaderField_clicked()
{
int rowCount = this->ui->tableWidget_CustomHeaderFields->rowCount();
if (rowCount > 0) {
QModelIndexList indexList = ui->tableWidget_CustomHeaderFields->selectionModel()->selectedIndexes();
QList<QPersistentModelIndex> persistentIndexes;
for (QModelIndex index : indexList) {
QPersistentModelIndex persistentIndex(index);
persistentIndexes.append(persistentIndex);
}
for (QPersistentModelIndex index : persistentIndexes) {
this->ui->tableWidget_CustomHeaderFields->removeRow(index.row());
}
if (this->ui->tableWidget_CustomHeaderFields->rowCount() > 0) {
this->ui->tableWidget_CustomHeaderFields->selectRow(0);
}
this->editor->updateCustomMapHeaderValues(this->ui->tableWidget_CustomHeaderFields);
}
}
void MainWindow::on_tableWidget_CustomHeaderFields_cellChanged(int row, int column)
{
this->editor->updateCustomMapHeaderValues(this->ui->tableWidget_CustomHeaderFields);
}
void MainWindow::closeEvent(QCloseEvent *event) {
porymapConfig.setGeometry(
this->saveGeometry(),

View file

@ -128,6 +128,58 @@ QStringList* Project::getLabelValues(QList<QStringList> *list, QString label) {
return values;
}
QMap<QString, bool> Project::getTopLevelMapFields() {
if (projectConfig.getBaseGameVersion() == BaseGameVersion::pokeemerald) {
return QMap<QString, bool>
{
{"id", true},
{"name", true},
{"layout", true},
{"music", true},
{"region_map_section", true},
{"requires_flash", true},
{"weather", true},
{"map_type", true},
{"allow_bike", true},
{"allow_escape_rope", true},
{"allow_running", true},
{"show_map_name", true},
{"battle_scene", true},
{"connections", true},
{"object_events", true},
{"warp_events", true},
{"coord_events", true},
{"bg_events", true},
{"shared_events_map", true},
{"shared_scripts_map", true},
};
} else if (projectConfig.getBaseGameVersion() == BaseGameVersion::pokeruby) {
return QMap<QString, bool>
{
{"id", true},
{"name", true},
{"layout", true},
{"music", true},
{"region_map_section", true},
{"requires_flash", true},
{"weather", true},
{"map_type", true},
{"show_map_name", true},
{"battle_scene", true},
{"connections", true},
{"object_events", true},
{"warp_events", true},
{"coord_events", true},
{"bg_events", true},
{"shared_events_map", true},
{"shared_scripts_map", true},
};
} else {
logError("Invalid game version");
return QMap<QString, bool>();
}
}
bool Project::loadMapData(Map* map) {
if (!map->isPersistedToFile) {
return true;
@ -322,6 +374,14 @@ bool Project::loadMapData(Map* map) {
}
}
// Check for custom fields
QMap<QString, bool> baseFields = this->getTopLevelMapFields();
for (QString key : mapObj.keys()) {
if (!baseFields.contains(key)) {
map->customHeaders.insert(key, mapObj[key].toString());
}
}
return true;
}
@ -954,6 +1014,11 @@ void Project::saveMap(Map *map) {
mapObj["shared_scripts_map"] = map->sharedScriptsMap;
}
// Custom header fields.
for (QString key : map->customHeaders.keys()) {
mapObj[key] = map->customHeaders[key];
}
QJsonDocument mapDoc(mapObj);
mapFile.write(mapDoc.toJson());