porymap/src/ui/projectsettingseditor.cpp

358 lines
17 KiB
C++
Raw Normal View History

2023-08-23 07:32:32 +01:00
#include "projectsettingseditor.h"
#include "ui_projectsettingseditor.h"
#include "config.h"
#include "noscrollcombobox.h"
2023-08-31 18:47:13 +01:00
#include "prefab.h"
2023-08-23 07:32:32 +01:00
#include <QAbstractButton>
#include <QFormLayout>
/*
2023-09-01 19:00:09 +01:00
Editor for the settings in a user's porymap.project.cfg file (and 'use_encounter_json' in porymap.user.cfg).
2023-08-23 07:32:32 +01:00
*/
ProjectSettingsEditor::ProjectSettingsEditor(QWidget *parent, Project *project) :
QMainWindow(parent),
ui(new Ui::ProjectSettingsEditor),
project(project),
baseDir(userConfig.getProjectDir() + QDir::separator())
2023-08-23 07:32:32 +01:00
{
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
2023-08-29 02:02:52 +01:00
this->initUi();
2023-09-07 04:33:13 +01:00
this->createProjectPathsTable();
2023-08-29 02:02:52 +01:00
this->connectSignals();
this->refresh();
this->restoreWindowState();
2023-08-23 07:32:32 +01:00
}
ProjectSettingsEditor::~ProjectSettingsEditor()
{
delete ui;
}
void ProjectSettingsEditor::connectSignals() {
connect(ui->buttonBox, &QDialogButtonBox::clicked, this, &ProjectSettingsEditor::dialogButtonClicked);
2023-08-29 02:02:52 +01:00
connect(ui->button_ChoosePrefabs, &QAbstractButton::clicked, this, &ProjectSettingsEditor::choosePrefabsFileClicked);
2023-08-31 18:47:13 +01:00
connect(ui->button_ImportDefaultPrefabs, &QAbstractButton::clicked, this, &ProjectSettingsEditor::importDefaultPrefabsClicked);
2023-08-29 02:02:52 +01:00
connect(ui->comboBox_BaseGameVersion, &QComboBox::currentTextChanged, this, &ProjectSettingsEditor::promptRestoreDefaults);
connect(ui->comboBox_AttributesSize, &QComboBox::currentTextChanged, this, &ProjectSettingsEditor::updateAttributeLimits);
2023-09-01 19:00:09 +01:00
// Record that there are unsaved changes if any of the settings are modified
for (auto combo : ui->centralwidget->findChildren<NoScrollComboBox *>())
connect(combo, &QComboBox::currentTextChanged, this, &ProjectSettingsEditor::markEdited);
for (auto checkBox : ui->centralwidget->findChildren<QCheckBox *>())
connect(checkBox, &QCheckBox::stateChanged, this, &ProjectSettingsEditor::markEdited);
for (auto lineEdit : ui->centralwidget->findChildren<QLineEdit *>())
connect(lineEdit, &QLineEdit::textEdited, this, &ProjectSettingsEditor::markEdited);
2023-09-11 17:44:15 +01:00
for (auto spinBox : ui->centralwidget->findChildren<NoScrollSpinBox *>())
connect(spinBox, &QSpinBox::textChanged, this, &ProjectSettingsEditor::markEdited);
for (auto spinBox : ui->centralwidget->findChildren<UIntSpinBox *>())
connect(spinBox, &UIntSpinBox::textChanged, this, &ProjectSettingsEditor::markEdited);
}
void ProjectSettingsEditor::markEdited() {
2023-08-29 02:02:52 +01:00
// Don't treat signals emitted while the UI is refreshing as edits
if (!this->refreshing)
this->hasUnsavedChanges = true;
}
2023-08-23 07:32:32 +01:00
void ProjectSettingsEditor::initUi() {
2023-08-29 02:02:52 +01:00
// Populate combo boxes
if (project) ui->comboBox_DefaultPrimaryTileset->addItems(project->primaryTilesetLabels);
if (project) ui->comboBox_DefaultSecondaryTileset->addItems(project->secondaryTilesetLabels);
ui->comboBox_BaseGameVersion->addItems(ProjectConfig::versionStrings);
ui->comboBox_AttributesSize->addItems({"1", "2", "4"});
// Validate that the border metatiles text is a comma-separated list of metatile values
const QString regex_Hex = "(0[xX])?[A-Fa-f0-9]+";
static const QRegularExpression expression(QString("^(%1,)*%1$").arg(regex_Hex)); // Comma-separated list of hex values
QRegularExpressionValidator *validator = new QRegularExpressionValidator(expression);
ui->lineEdit_BorderMetatiles->setValidator(validator);
ui->spinBox_Elevation->setMaximum(15);
ui->spinBox_FillMetatile->setMaximum(Project::getNumMetatilesTotal() - 1);
}
void ProjectSettingsEditor::updateAttributeLimits(const QString &attrSize) {
QMap<QString, uint32_t> limits {
{"1", 0xFF},
{"2", 0xFFFF},
{"4", 0xFFFFFFFF},
};
uint32_t max = limits.value(attrSize, UINT_MAX);
ui->spinBox_BehaviorMask->setMaximum(max);
ui->spinBox_EncounterTypeMask->setMaximum(max);
ui->spinBox_LayerTypeMask->setMaximum(max);
ui->spinBox_TerrainTypeMask->setMaximum(max);
2023-08-29 02:02:52 +01:00
}
2023-09-07 04:33:13 +01:00
void ProjectSettingsEditor::createProjectPathsTable() {
auto pathPairs = ProjectConfig::defaultPaths.values();
for (auto pathPair : pathPairs) {
// Name of the path
auto name = new QLabel();
2023-09-08 18:05:38 +01:00
name->setAlignment(Qt::AlignBottom);
2023-09-07 04:33:13 +01:00
name->setText(pathPair.first);
// Filepath line edit
auto lineEdit = new QLineEdit();
lineEdit->setObjectName(pathPair.first); // Used when saving the paths
lineEdit->setPlaceholderText(pathPair.second);
lineEdit->setClearButtonEnabled(true);
// "Choose file" button
2023-09-07 04:33:13 +01:00
auto button = new QToolButton();
button->setIcon(QIcon(":/icons/folder.ico"));
connect(button, &QAbstractButton::clicked, [this, lineEdit](bool) {
const QString path = this->chooseProjectFile(lineEdit->placeholderText());
if (!path.isEmpty()) {
lineEdit->setText(path);
this->markEdited();
}
});
2023-09-07 04:33:13 +01:00
// Add to list
2023-09-08 18:05:38 +01:00
auto editArea = new QWidget();
auto layout = new QHBoxLayout(editArea);
layout->addWidget(lineEdit);
2023-09-07 04:33:13 +01:00
layout->addWidget(button);
2023-09-08 18:05:38 +01:00
ui->layout_ProjectPaths->addRow(name, editArea);
2023-09-07 04:33:13 +01:00
}
}
QString ProjectSettingsEditor::chooseProjectFile(const QString &defaultFilepath) {
const QString startDir = this->baseDir + defaultFilepath;
QString path;
if (defaultFilepath.endsWith("/")){
// Default filepath is a folder, choose a new folder
path = QFileDialog::getExistingDirectory(this, "Choose Project File Folder", startDir) + QDir::separator();
} else{
// Default filepath is not a folder, choose a new file
path = QFileDialog::getOpenFileName(this, "Choose Project File", startDir);
}
if (!path.startsWith(this->baseDir)){
// Most of Porymap's file-parsing code for project files will assume that filepaths
// are relative to the root project folder, so we enforce that here.
QMessageBox::warning(this, "Failed to set custom filepath",
QString("Custom filepaths must be inside the root project folder '%1'").arg(this->baseDir));
return QString();
}
return path.remove(0, this->baseDir.length());
}
2023-08-29 02:02:52 +01:00
void ProjectSettingsEditor::restoreWindowState() {
logInfo("Restoring project settings editor geometry from previous session.");
const QMap<QString, QByteArray> geometry = porymapConfig.getProjectSettingsEditorGeometry();
this->restoreGeometry(geometry.value("project_settings_editor_geometry"));
this->restoreState(geometry.value("project_settings_editor_state"));
}
// Set UI states using config data
void ProjectSettingsEditor::refresh() {
2023-08-29 02:02:52 +01:00
this->refreshing = true; // Block signals
2023-08-23 07:32:32 +01:00
// Set combo box texts
2023-08-29 02:02:52 +01:00
ui->comboBox_DefaultPrimaryTileset->setTextItem(projectConfig.getDefaultPrimaryTileset());
ui->comboBox_DefaultSecondaryTileset->setTextItem(projectConfig.getDefaultSecondaryTileset());
ui->comboBox_BaseGameVersion->setTextItem(projectConfig.getBaseGameVersionString());
ui->comboBox_AttributesSize->setTextItem(QString::number(projectConfig.getMetatileAttributesSize()));
this->updateAttributeLimits(ui->comboBox_AttributesSize->currentText());
2023-08-23 07:32:32 +01:00
// Set check box states
2023-08-23 07:32:32 +01:00
ui->checkBox_UsePoryscript->setChecked(projectConfig.getUsePoryScript());
ui->checkBox_ShowWildEncounterTables->setChecked(userConfig.getEncounterJsonActive());
ui->checkBox_CreateTextFile->setChecked(projectConfig.getCreateMapTextFileEnabled());
ui->checkBox_EnableTripleLayerMetatiles->setChecked(projectConfig.getTripleLayerMetatilesEnabled());
ui->checkBox_EnableRequiresItemfinder->setChecked(projectConfig.getHiddenItemRequiresItemfinderEnabled());
ui->checkBox_EnableQuantity->setChecked(projectConfig.getHiddenItemQuantityEnabled());
ui->checkBox_EnableCloneObjects->setChecked(projectConfig.getEventCloneObjectEnabled());
ui->checkBox_EnableWeatherTriggers->setChecked(projectConfig.getEventWeatherTriggerEnabled());
ui->checkBox_EnableSecretBases->setChecked(projectConfig.getEventSecretBaseEnabled());
ui->checkBox_EnableRespawn->setChecked(projectConfig.getHealLocationRespawnDataEnabled());
ui->checkBox_EnableAllowFlags->setChecked(projectConfig.getMapAllowFlagsEnabled());
ui->checkBox_EnableFloorNumber->setChecked(projectConfig.getFloorNumberEnabled());
ui->checkBox_EnableCustomBorderSize->setChecked(projectConfig.getUseCustomBorderSize());
ui->checkBox_OutputCallback->setChecked(projectConfig.getTilesetsHaveCallback());
ui->checkBox_OutputIsCompressed->setChecked(projectConfig.getTilesetsHaveIsCompressed());
// Set spin box values
2023-08-23 07:32:32 +01:00
ui->spinBox_Elevation->setValue(projectConfig.getNewMapElevation());
ui->spinBox_FillMetatile->setValue(projectConfig.getNewMapMetatileId());
ui->spinBox_BehaviorMask->setValue(projectConfig.getMetatileBehaviorMask());
ui->spinBox_EncounterTypeMask->setValue(projectConfig.getMetatileEncounterTypeMask());
ui->spinBox_LayerTypeMask->setValue(projectConfig.getMetatileLayerTypeMask());
ui->spinBox_TerrainTypeMask->setValue(projectConfig.getMetatileTerrainTypeMask());
2023-08-23 07:32:32 +01:00
// Set line edit texts
2023-08-23 07:32:32 +01:00
ui->lineEdit_BorderMetatiles->setText(projectConfig.getNewMapBorderMetatileIdsString());
2023-08-31 18:47:13 +01:00
ui->lineEdit_PrefabsPath->setText(projectConfig.getPrefabFilepath());
2023-09-07 04:33:13 +01:00
for (auto lineEdit : ui->scrollAreaContents_ProjectPaths->findChildren<QLineEdit*>())
lineEdit->setText(projectConfig.getFilePath(lineEdit->objectName(), true));
2023-08-23 07:32:32 +01:00
2023-08-29 02:02:52 +01:00
this->refreshing = false; // Allow signals
}
2023-08-29 02:02:52 +01:00
void ProjectSettingsEditor::save() {
if (!this->hasUnsavedChanges)
return;
// Prevent a call to save() for each of the config settings
projectConfig.setSaveDisabled(true);
2023-08-29 02:02:52 +01:00
projectConfig.setDefaultPrimaryTileset(ui->comboBox_DefaultPrimaryTileset->currentText());
projectConfig.setDefaultSecondaryTileset(ui->comboBox_DefaultSecondaryTileset->currentText());
projectConfig.setBaseGameVersion(projectConfig.stringToBaseGameVersion(ui->comboBox_BaseGameVersion->currentText()));
projectConfig.setMetatileAttributesSize(ui->comboBox_AttributesSize->currentText().toInt());
projectConfig.setUsePoryScript(ui->checkBox_UsePoryscript->isChecked());
userConfig.setEncounterJsonActive(ui->checkBox_ShowWildEncounterTables->isChecked());
projectConfig.setCreateMapTextFileEnabled(ui->checkBox_CreateTextFile->isChecked());
projectConfig.setTripleLayerMetatilesEnabled(ui->checkBox_EnableTripleLayerMetatiles->isChecked());
projectConfig.setHiddenItemRequiresItemfinderEnabled(ui->checkBox_EnableRequiresItemfinder->isChecked());
projectConfig.setHiddenItemQuantityEnabled(ui->checkBox_EnableQuantity->isChecked());
projectConfig.setEventCloneObjectEnabled(ui->checkBox_EnableCloneObjects->isChecked());
projectConfig.setEventWeatherTriggerEnabled(ui->checkBox_EnableWeatherTriggers->isChecked());
projectConfig.setEventSecretBaseEnabled(ui->checkBox_EnableSecretBases->isChecked());
projectConfig.setHealLocationRespawnDataEnabled(ui->checkBox_EnableRespawn->isChecked());
projectConfig.setMapAllowFlagsEnabled(ui->checkBox_EnableAllowFlags->isChecked());
projectConfig.setFloorNumberEnabled(ui->checkBox_EnableFloorNumber->isChecked());
projectConfig.setUseCustomBorderSize(ui->checkBox_EnableCustomBorderSize->isChecked());
projectConfig.setTilesetsHaveCallback(ui->checkBox_OutputCallback->isChecked());
projectConfig.setTilesetsHaveIsCompressed(ui->checkBox_OutputIsCompressed->isChecked());
projectConfig.setNewMapElevation(ui->spinBox_Elevation->value());
projectConfig.setNewMapMetatileId(ui->spinBox_FillMetatile->value());
projectConfig.setMetatileBehaviorMask(ui->spinBox_BehaviorMask->value());
2023-08-29 02:02:52 +01:00
projectConfig.setMetatileTerrainTypeMask(ui->spinBox_TerrainTypeMask->value());
projectConfig.setMetatileEncounterTypeMask(ui->spinBox_EncounterTypeMask->value());
projectConfig.setMetatileLayerTypeMask(ui->spinBox_LayerTypeMask->value());
projectConfig.setPrefabFilepath(ui->lineEdit_PrefabsPath->text());
2023-09-07 04:33:13 +01:00
for (auto lineEdit : ui->scrollAreaContents_ProjectPaths->findChildren<QLineEdit*>())
projectConfig.setFilePath(lineEdit->objectName(), lineEdit->text());
2023-09-07 04:33:13 +01:00
// Parse and save border metatile list
QList<uint16_t> metatileIds;
2023-09-07 04:33:13 +01:00
for (auto s : ui->lineEdit_BorderMetatiles->text().split(",")) {
uint16_t metatileId = s.toUInt(nullptr, 0);
metatileIds.append(qMin(metatileId, static_cast<uint16_t>(Project::getNumMetatilesTotal() - 1)));
}
projectConfig.setNewMapBorderMetatileIds(metatileIds);
projectConfig.setSaveDisabled(false);
projectConfig.save();
this->hasUnsavedChanges = false;
2023-08-29 02:02:52 +01:00
// Technically, a reload is not required for several of the config settings.
// For simplicity we prompt the user to reload when any setting is changed regardless.
this->projectNeedsReload = true;
}
2023-09-06 19:45:46 +01:00
// Pick a file to use as the new prefabs file path
2023-08-29 02:02:52 +01:00
void ProjectSettingsEditor::choosePrefabsFileClicked(bool) {
QString startPath = this->project->importExportPath;
QFileInfo fileInfo(ui->lineEdit_PrefabsPath->text());
if (fileInfo.exists() && fileInfo.isFile() && fileInfo.suffix() == "json") {
// Current setting is a valid JSON file. Start the file dialog there
startPath = fileInfo.dir().absolutePath();
}
QString filepath = QFileDialog::getOpenFileName(this, "Choose Prefabs File", startPath, "JSON Files (*.json)");
if (filepath.isEmpty())
return;
this->project->setImportExportPath(filepath);
2023-09-06 19:45:46 +01:00
// Display relative path if this file is in the project folder
if (filepath.startsWith(this->baseDir))
filepath.remove(0, this->baseDir.length());
2023-08-29 02:02:52 +01:00
ui->lineEdit_PrefabsPath->setText(filepath);
this->hasUnsavedChanges = true;
2023-08-23 07:32:32 +01:00
}
2023-08-31 18:47:13 +01:00
void ProjectSettingsEditor::importDefaultPrefabsClicked(bool) {
// If the prompt is accepted the prefabs file will be created and its filepath will be saved in the config.
// No need to set hasUnsavedChanges here.
BaseGameVersion version = projectConfig.stringToBaseGameVersion(ui->comboBox_BaseGameVersion->currentText());
if (prefab.tryImportDefaultPrefabs(this, version, ui->lineEdit_PrefabsPath->text())) {
ui->lineEdit_PrefabsPath->setText(projectConfig.getPrefabFilepath()); // Refresh with new filepath
this->projectNeedsReload = true;
}
}
2023-08-29 02:02:52 +01:00
int ProjectSettingsEditor::prompt(const QString &text, QMessageBox::StandardButton defaultButton) {
QMessageBox messageBox(this);
messageBox.setText(text);
messageBox.setIcon(QMessageBox::Question);
2023-08-29 02:02:52 +01:00
messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No | defaultButton);
messageBox.setDefaultButton(defaultButton);
return messageBox.exec();
}
bool ProjectSettingsEditor::promptSaveChanges() {
if (!this->hasUnsavedChanges)
return true;
int result = this->prompt("Settings have been modified, save changes?", QMessageBox::Cancel);
if (result == QMessageBox::Cancel)
return false;
if (result == QMessageBox::Yes)
this->save();
else if (result == QMessageBox::No)
this->hasUnsavedChanges = false; // Discarding changes
return true;
}
bool ProjectSettingsEditor::promptRestoreDefaults() {
if (this->refreshing)
return false;
const QString versionText = ui->comboBox_BaseGameVersion->currentText();
if (this->prompt(QString("Restore default config settings for %1?").arg(versionText)) == QMessageBox::No)
return false;
// Restore defaults by resetting config in memory, refreshing the UI, then restoring the config.
// Don't want to save changes until user accepts them.
ProjectConfig tempProject = projectConfig;
projectConfig.reset(projectConfig.stringToBaseGameVersion(versionText));
this->refresh();
projectConfig = tempProject;
this->hasUnsavedChanges = true;
return true;
}
2023-08-23 07:32:32 +01:00
void ProjectSettingsEditor::dialogButtonClicked(QAbstractButton *button) {
auto buttonRole = ui->buttonBox->buttonRole(button);
if (buttonRole == QDialogButtonBox::AcceptRole) {
2023-08-29 02:02:52 +01:00
// "OK" button
this->save();
2023-08-23 07:32:32 +01:00
close();
} else if (buttonRole == QDialogButtonBox::RejectRole) {
2023-08-29 02:02:52 +01:00
// "Cancel" button
if (!this->promptSaveChanges())
return;
2023-08-23 07:32:32 +01:00
close();
2023-08-29 02:02:52 +01:00
} else if (buttonRole == QDialogButtonBox::ResetRole) {
// "Restore Defaults" button
this->promptRestoreDefaults();
2023-08-23 07:32:32 +01:00
}
2023-08-29 02:02:52 +01:00
}
void ProjectSettingsEditor::closeEvent(QCloseEvent* event) {
if (!this->promptSaveChanges()) {
event->ignore();
return;
}
if (this->projectNeedsReload) {
if (this->prompt("Settings changed, reload project to apply changes?") == QMessageBox::Yes)
emit this->reloadProject();
}
porymapConfig.setProjectSettingsEditorGeometry(
this->saveGeometry(),
this->saveState()
);
2023-08-23 07:32:32 +01:00
}