Add prompt to import default prefabs for each game version

This commit is contained in:
Marcus Huderle 2022-09-24 15:28:31 -05:00
parent a081af85c4
commit 71a34c6b22
8 changed files with 42964 additions and 5 deletions

View file

@ -158,6 +158,7 @@ public:
this->createMapTextFile = false;
this->enableTripleLayerMetatiles = false;
this->prefabFilepath = QString();
this->prefabImportPrompted = false;
this->customScripts.clear();
this->readKeys.clear();
}
@ -195,7 +196,9 @@ public:
void setCustomScripts(QList<QString> scripts);
QList<QString> getCustomScripts();
void setPrefabFilepath(QString filepath);
QString getPrefabFilepath();
QString getPrefabFilepath(bool setIfEmpty);
void setPrefabImportPrompted(bool prompted);
bool getPrefabImportPrompted();
protected:
virtual QString getConfigFilepath() override;
virtual void parseConfigKeyValue(QString key, QString value) override;
@ -221,6 +224,7 @@ private:
QList<QString> customScripts;
QStringList readKeys;
QString prefabFilepath;
bool prefabImportPrompted;
};
extern ProjectConfig projectConfig;

View file

@ -23,6 +23,7 @@ public:
void initPrefabUI(MetatileSelector *selector, QWidget *prefabWidget, QLabel *emptyPrefabLabel, Map *map);
void addPrefab(MetatileSelection selection, Map *map, QString name);
void updatePrefabUi(Map *map);
void handlePrefabImport();
private:
MetatileSelector *selector;

View file

@ -3,5 +3,8 @@
<file>text/region_map_default_emerald.json</file>
<file>text/region_map_default_firered.json</file>
<file>text/region_map_default_ruby.json</file>
<file>text/prefabs_default_emerald.json</file>
<file>text/prefabs_default_firered.json</file>
<file>text/prefabs_default_ruby.json</file>
</qresource>
</RCC>

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -505,6 +505,8 @@ void ProjectConfig::parseConfigKeyValue(QString key, QString value) {
}
} else if (key == "prefabs_filepath") {
this->prefabFilepath = value;
} else if (key == "prefabs_import_prompted") {
setConfigBool(key, &this->prefabImportPrompted, value);
} else {
logWarn(QString("Invalid config key found in config file %1: '%2'").arg(this->getConfigFilepath()).arg(key));
}
@ -543,6 +545,7 @@ QMap<QString, QString> ProjectConfig::getKeyValueMap() {
map.insert("enable_triple_layer_metatiles", QString::number(this->enableTripleLayerMetatiles));
map.insert("custom_scripts", this->customScripts.join(","));
map.insert("prefabs_filepath", this->prefabFilepath);
map.insert("prefabs_import_prompted", QString::number(this->prefabImportPrompted));
return map;
}
@ -740,13 +743,22 @@ void ProjectConfig::setPrefabFilepath(QString filepath) {
this->save();
}
QString ProjectConfig::getPrefabFilepath() {
if (this->prefabFilepath.isEmpty()) {
QString ProjectConfig::getPrefabFilepath(bool setIfEmpty) {
if (setIfEmpty && this->prefabFilepath.isEmpty()) {
this->setPrefabFilepath("prefabs.json");
}
return this->prefabFilepath;
}
void ProjectConfig::setPrefabImportPrompted(bool prompted) {
this->prefabImportPrompted = prompted;
this->save();
}
bool ProjectConfig::getPrefabImportPrompted() {
return this->prefabImportPrompted;
}
ShortcutsConfig shortcutsConfig;
QString ShortcutsConfig::getConfigFilepath() {

View file

@ -21,7 +21,7 @@ using OrderedJsonDoc = poryjson::JsonDoc;
void Prefab::loadPrefabs() {
this->items.clear();
QString filepath = projectConfig.getPrefabFilepath();
QString filepath = projectConfig.getPrefabFilepath(false);
if (filepath.isEmpty()) return;
ParseUtil parser;
@ -85,7 +85,7 @@ void Prefab::loadPrefabs() {
}
void Prefab::savePrefabs() {
QString filepath = projectConfig.getPrefabFilepath();
QString filepath = projectConfig.getPrefabFilepath(true);
if (filepath.isEmpty()) return;
QFileInfo info(filepath);
@ -159,6 +159,7 @@ void Prefab::initPrefabUI(MetatileSelector *selector, QWidget *prefabWidget, QLa
this->selector = selector;
this->prefabWidget = prefabWidget;
this->emptyPrefabLabel = emptyPrefabLabel;
this->handlePrefabImport();
this->loadPrefabs();
this->updatePrefabUi(map);
}
@ -268,4 +269,69 @@ void Prefab::addPrefab(MetatileSelection selection, Map *map, QString name) {
this->updatePrefabUi(map);
}
void Prefab::handlePrefabImport() {
BaseGameVersion version = projectConfig.getBaseGameVersion();
// 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;
// Exit early if the user has already gone through this import prompt before.
if (projectConfig.getPrefabImportPrompted())
return;
// Display a dialog box to the user, asking if the default prefabs should be imported
// into their project.
QMessageBox::StandardButton prompt =
QMessageBox::question(nullptr,
"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()),
QMessageBox::Yes | QMessageBox::No);
if (prompt == QMessageBox::Yes) {
// 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);
if (!prefabsFile.open(QIODevice::WriteOnly)) {
projectConfig.setPrefabFilepath(QString());
logError(QString("Error: Could not open %1 for writing").arg(filepath));
QMessageBox messageBox;
messageBox.setText("Failed to import default prefabs file!");
messageBox.setInformativeText(QString("Could not open \"%1\" for writing").arg(filepath));
messageBox.setIcon(QMessageBox::Warning);
messageBox.exec();
return;
}
ParseUtil parser;
QString content;
switch (version) {
case BaseGameVersion::pokeruby:
content = parser.readTextFile(":/text/prefabs_default_ruby.json");
break;
case BaseGameVersion::pokefirered:
content = parser.readTextFile(":/text/prefabs_default_firered.json");
break;
case BaseGameVersion::pokeemerald:
content = parser.readTextFile(":/text/prefabs_default_emerald.json");
break;
}
prefabsFile.write(content.toUtf8());
prefabsFile.close();
}
projectConfig.setPrefabImportPrompted(true);
}
Prefab prefab;