Skip to content

Commit

Permalink
Add button to import default prefabs
Browse files Browse the repository at this point in the history
  • Loading branch information
GriffinRichards committed Aug 31, 2023
1 parent 871a6ed commit 143e5cf
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 58 deletions.
34 changes: 17 additions & 17 deletions forms/projectsettingseditor.ui
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<x>0</x>
<y>0</y>
<width>544</width>
<height>1096</height>
<height>1107</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_5">
Expand Down Expand Up @@ -185,31 +185,31 @@
<string>Prefabs</string>
</property>
<layout class="QGridLayout" name="gridLayout_4">
<item row="0" column="1">
<widget class="QLineEdit" name="lineEdit_PrefabsPath"/>
</item>
<item row="1" column="0" colspan="2">
<widget class="QCheckBox" name="checkBox_PrefabImportPrompted">
<item row="0" column="2">
<widget class="QToolButton" name="button_ChoosePrefabs">
<property name="text">
<string>Prefab import prompted</string>
<string>...</string>
</property>
<property name="icon">
<iconset resource="../resources/images.qrc">
<normaloff>:/icons/folder.ico</normaloff>:/icons/folder.ico</iconset>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_PrefabsPath">
<item row="1" column="0">
<widget class="QPushButton" name="button_ImportDefaultPrefabs">
<property name="text">
<string>Prefabs Path</string>
<string>Import Defaults</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QToolButton" name="button_ChoosePrefabs">
<item row="0" column="1">
<widget class="QLineEdit" name="lineEdit_PrefabsPath"/>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_PrefabsPath">
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset resource="../resources/images.qrc">
<normaloff>:/icons/folder.ico</normaloff>:/icons/folder.ico</iconset>
<string>Prefabs Path</string>
</property>
</widget>
</item>
Expand Down
3 changes: 2 additions & 1 deletion include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ class ProjectConfig: public KeyValueConfigBase
void setBaseGameVersion(BaseGameVersion baseGameVersion);
BaseGameVersion getBaseGameVersion();
QString getBaseGameVersionString();
QString getBaseGameVersionString(BaseGameVersion version);
BaseGameVersion stringToBaseGameVersion(QString string, bool * ok = nullptr);
void setUsePoryScript(bool usePoryScript);
bool getUsePoryScript();
Expand Down Expand Up @@ -269,7 +270,7 @@ class ProjectConfig: public KeyValueConfigBase
void setFilePath(ProjectFilePath pathId, QString path);
QString getFilePath(ProjectFilePath pathId);
void setPrefabFilepath(QString filepath);
QString getPrefabFilepath(bool setIfEmpty);
QString getPrefabFilepath();
void setPrefabImportPrompted(bool prompted);
bool getPrefabImportPrompted();
void setTilesetsHaveCallback(bool has);
Expand Down
2 changes: 1 addition & 1 deletion include/ui/prefab.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Prefab
void initPrefabUI(MetatileSelector *selector, QWidget *prefabWidget, QLabel *emptyPrefabLabel, Map *map);
void addPrefab(MetatileSelection selection, Map *map, QString name);
void updatePrefabUi(Map *map);
void tryImportDefaultPrefabs(Map *map);
bool tryImportDefaultPrefabs(QWidget * parent, BaseGameVersion version, QString filepath = "");

private:
MetatileSelector *selector;
Expand Down
1 change: 1 addition & 0 deletions include/ui/projectsettingseditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ProjectSettingsEditor : public QMainWindow
private slots:
void dialogButtonClicked(QAbstractButton *button);
void choosePrefabsFileClicked(bool);
void importDefaultPrefabsClicked(bool);
void markEdited();
};

Expand Down
14 changes: 9 additions & 5 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,8 +801,15 @@ BaseGameVersion ProjectConfig::getBaseGameVersion() {
return this->baseGameVersion;
}

QString ProjectConfig::getBaseGameVersionString(BaseGameVersion version) {
if (!baseGameVersionMap.contains(version)) {
version = BaseGameVersion::pokeemerald;
}
return baseGameVersionMap.value(version);
}

QString ProjectConfig::getBaseGameVersionString() {
return baseGameVersionMap.value(this->baseGameVersion);
return this->getBaseGameVersionString(this->baseGameVersion);
}

void ProjectConfig::setUsePoryScript(bool usePoryScript) {
Expand Down Expand Up @@ -970,10 +977,7 @@ void ProjectConfig::setPrefabFilepath(QString filepath) {
this->save();
}

QString ProjectConfig::getPrefabFilepath(bool setIfEmpty) {
if (setIfEmpty && this->prefabFilepath.isEmpty()) {
this->setPrefabFilepath("prefabs.json");
}
QString ProjectConfig::getPrefabFilepath() {
return this->prefabFilepath;
}

Expand Down
7 changes: 6 additions & 1 deletion src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,12 @@ void MainWindow::on_mapViewTab_tabBarClicked(int index)
editor->setEditingCollision();
} else if (index == 2) {
editor->setEditingMap();
prefab.tryImportDefaultPrefabs(this->editor->map);
if (projectConfig.getPrefabFilepath().isEmpty() && !projectConfig.getPrefabImportPrompted()) {
// User hasn't set up prefabs and hasn't been prompted before.
// Ask if they'd like to import the default prefabs file.
if (prefab.tryImportDefaultPrefabs(this, projectConfig.getBaseGameVersion()))
prefab.updatePrefabUi(this->editor->map);
}
}
editor->setCursorRectVisible(false);
}
Expand Down
73 changes: 44 additions & 29 deletions src/ui/prefab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
using OrderedJson = poryjson::Json;
using OrderedJsonDoc = poryjson::JsonDoc;

const QString defaultFilepath = "prefabs.json";

void Prefab::loadPrefabs() {
this->items.clear();
QString filepath = projectConfig.getPrefabFilepath(false);
QString filepath = projectConfig.getPrefabFilepath();
if (filepath.isEmpty()) return;

ParseUtil parser;
Expand Down Expand Up @@ -85,8 +87,11 @@ void Prefab::loadPrefabs() {
}

void Prefab::savePrefabs() {
QString filepath = projectConfig.getPrefabFilepath(true);
if (filepath.isEmpty()) return;
QString filepath = projectConfig.getPrefabFilepath();
if (filepath.isEmpty()) {
filepath = defaultFilepath;
projectConfig.setPrefabFilepath(filepath);
}

QFileInfo info(filepath);
if (info.isRelative()) {
Expand Down Expand Up @@ -269,48 +274,58 @@ void Prefab::addPrefab(MetatileSelection selection, Map *map, QString name) {
this->updatePrefabUi(map);
}

void Prefab::tryImportDefaultPrefabs(Map *map) {
BaseGameVersion version = projectConfig.getBaseGameVersion();
bool Prefab::tryImportDefaultPrefabs(QWidget * parent, BaseGameVersion version, QString filepath) {
// Ensure we have default prefabs for the project's game version.
if (version != BaseGameVersion::pokeruby && version != BaseGameVersion::pokeemerald && version != BaseGameVersion::pokefirered)
return;

// Exit early if the user has already setup prefabs.
if (!projectConfig.getPrefabFilepath(false).isEmpty())
return;
return false;

if (filepath.isEmpty())
filepath = defaultFilepath;

// Get the absolute filepath for writing/warnings
QString absFilepath;
QFileInfo fileInfo(filepath);
if (fileInfo.suffix().isEmpty())
filepath += ".json";
if (fileInfo.isRelative()) {
absFilepath = QDir::cleanPath(projectConfig.getProjectDir() + QDir::separator() + filepath);
} else {
absFilepath = filepath;
}

// Exit early if the user has already gone through this import prompt before.
if (projectConfig.getPrefabImportPrompted())
return;
// The warning message when importing defaults changes if there's a pre-existing file.
QString fileWarning;
if (!QFileInfo::exists(absFilepath)) {
fileWarning = QString("This will create a file called '%1'").arg(absFilepath);
} else {
fileWarning = QString("This will overwrite any existing prefabs in '%1'").arg(absFilepath);
}

// Display a dialog box to the user, asking if the default prefabs should be imported
// into their project.
QMessageBox::StandardButton prompt =
QMessageBox::question(nullptr,
QMessageBox::question(parent,
"Import Default Prefabs",
QString("Would you like to import the default prefabs for %1? This will create a file called 'prefabs.json' in your project directory.")
.arg(projectConfig.getBaseGameVersionString()),
QString("Would you like to import the default prefabs for %1? %2.")
.arg(projectConfig.getBaseGameVersionString(version))
.arg(fileWarning),
QMessageBox::Yes | QMessageBox::No);

if (prompt == QMessageBox::Yes) {
bool acceptedImport = (prompt == QMessageBox::Yes);
if (acceptedImport) {
// Sets up the default prefabs.json filepath.
QString filepath = projectConfig.getPrefabFilepath(true);

QFileInfo info(filepath);
if (info.isRelative()) {
filepath = QDir::cleanPath(projectConfig.getProjectDir() + QDir::separator() + filepath);
}
QFile prefabsFile(filepath);
projectConfig.setPrefabFilepath(filepath);
QFile prefabsFile(absFilepath);
if (!prefabsFile.open(QIODevice::WriteOnly)) {
projectConfig.setPrefabFilepath(QString());

logError(QString("Error: Could not open %1 for writing").arg(filepath));
QMessageBox messageBox;
logError(QString("Error: Could not open %1 for writing").arg(absFilepath));
QMessageBox messageBox(parent);
messageBox.setText("Failed to import default prefabs file!");
messageBox.setInformativeText(QString("Could not open \"%1\" for writing").arg(filepath));
messageBox.setInformativeText(QString("Could not open \"%1\" for writing").arg(absFilepath));
messageBox.setIcon(QMessageBox::Warning);
messageBox.exec();
return;
return false;
}

ParseUtil parser;
Expand All @@ -330,10 +345,10 @@ void Prefab::tryImportDefaultPrefabs(Map *map) {
prefabsFile.write(content.toUtf8());
prefabsFile.close();
this->loadPrefabs();
this->updatePrefabUi(map);
}

projectConfig.setPrefabImportPrompted(true);
return acceptedImport;
}

Prefab prefab;
18 changes: 14 additions & 4 deletions src/ui/projectsettingseditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "ui_projectsettingseditor.h"
#include "config.h"
#include "noscrollcombobox.h"
#include "prefab.h"

#include <QAbstractButton>
#include <QFormLayout>
Expand Down Expand Up @@ -31,8 +32,10 @@ ProjectSettingsEditor::~ProjectSettingsEditor()
// TODO: Move tool tips to editable areas

void ProjectSettingsEditor::connectSignals() {
// Connect buttons
connect(ui->buttonBox, &QDialogButtonBox::clicked, this, &ProjectSettingsEditor::dialogButtonClicked);
connect(ui->button_ChoosePrefabs, &QAbstractButton::clicked, this, &ProjectSettingsEditor::choosePrefabsFileClicked);
connect(ui->button_ImportDefaultPrefabs, &QAbstractButton::clicked, this, &ProjectSettingsEditor::importDefaultPrefabsClicked);

// Connect combo boxes
QList<NoScrollComboBox *> combos = ui->centralwidget->findChildren<NoScrollComboBox *>();
Expand Down Expand Up @@ -106,7 +109,6 @@ void ProjectSettingsEditor::refresh() {
ui->checkBox_UsePoryscript->setChecked(projectConfig.getUsePoryScript());
ui->checkBox_ShowWildEncounterTables->setChecked(userConfig.getEncounterJsonActive());
ui->checkBox_CreateTextFile->setChecked(projectConfig.getCreateMapTextFileEnabled());
ui->checkBox_PrefabImportPrompted->setChecked(projectConfig.getPrefabImportPrompted());
ui->checkBox_EnableTripleLayerMetatiles->setChecked(projectConfig.getTripleLayerMetatilesEnabled());
ui->checkBox_EnableRequiresItemfinder->setChecked(projectConfig.getHiddenItemRequiresItemfinderEnabled());
ui->checkBox_EnableQuantity->setChecked(projectConfig.getHiddenItemQuantityEnabled());
Expand All @@ -130,7 +132,7 @@ void ProjectSettingsEditor::refresh() {

// Set line edit texts
ui->lineEdit_BorderMetatiles->setText(projectConfig.getNewMapBorderMetatileIdsString());
ui->lineEdit_PrefabsPath->setText(projectConfig.getPrefabFilepath(false));
ui->lineEdit_PrefabsPath->setText(projectConfig.getPrefabFilepath());

this->refreshing = false; // Allow signals
}
Expand All @@ -150,7 +152,6 @@ void ProjectSettingsEditor::save() {
projectConfig.setUsePoryScript(ui->checkBox_UsePoryscript->isChecked());
userConfig.setEncounterJsonActive(ui->checkBox_ShowWildEncounterTables->isChecked());
projectConfig.setCreateMapTextFileEnabled(ui->checkBox_CreateTextFile->isChecked());
projectConfig.setPrefabImportPrompted(ui->checkBox_PrefabImportPrompted->isChecked());
projectConfig.setTripleLayerMetatilesEnabled(ui->checkBox_EnableTripleLayerMetatiles->isChecked());
projectConfig.setHiddenItemRequiresItemfinderEnabled(ui->checkBox_EnableRequiresItemfinder->isChecked());
projectConfig.setHiddenItemQuantityEnabled(ui->checkBox_EnableQuantity->isChecked());
Expand Down Expand Up @@ -203,10 +204,19 @@ void ProjectSettingsEditor::choosePrefabsFileClicked(bool) {
return;
this->project->setImportExportPath(filepath);
ui->lineEdit_PrefabsPath->setText(filepath);
ui->checkBox_PrefabImportPrompted->setChecked(true);
this->hasUnsavedChanges = true;
}

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;
}
}

int ProjectSettingsEditor::prompt(const QString &text, QMessageBox::StandardButton defaultButton) {
QMessageBox messageBox(this);
messageBox.setText(text);
Expand Down

0 comments on commit 143e5cf

Please sign in to comment.