From ced402a4c6da1d83bb6cc1a35c92746636a32783 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Fri, 29 Dec 2023 21:54:37 -0500 Subject: [PATCH] Add labels from scripts file to Script dropdowns --- include/core/map.h | 5 ++++- include/project.h | 3 +-- src/core/map.cpp | 22 ++++++++++++++++++++-- src/editor.cpp | 5 ++--- src/project.cpp | 12 +----------- src/ui/eventframes.cpp | 38 +++++++++++++++++++++----------------- 6 files changed, 49 insertions(+), 36 deletions(-) diff --git a/include/core/map.h b/include/core/map.h index 1668ac08..c4268ef9 100644 --- a/include/core/map.h +++ b/include/core/map.h @@ -53,6 +53,7 @@ public: QString battle_scene; QString sharedEventsMap = ""; QString sharedScriptsMap = ""; + QStringList scriptsFileLabels; QMap customHeaders; MapLayout *layout; bool isPersistedToFile = true; @@ -70,6 +71,7 @@ public: QList connections; QList metatileLayerOrder; QList metatileLayerOpacity; + void setName(QString mapName); static QString mapConstantFromName(QString mapName, bool includePrefix = true); int getWidth(); @@ -92,7 +94,7 @@ public: void _floodFillCollisionElevation(int x, int y, uint16_t collision, uint16_t elevation); void magicFillCollisionElevation(int x, int y, uint16_t collision, uint16_t elevation); QList getAllEvents() const; - QStringList eventScriptLabels(Event::Group group = Event::Group::None) const; + QStringList getScriptLabels(Event::Group group = Event::Group::None) const; void removeEvent(Event *); void addEvent(Event *); QPixmap renderConnection(MapConnection, MapLayout *); @@ -105,6 +107,7 @@ public: bool isWithinBounds(int x, int y); bool isWithinBorderBounds(int x, int y); void openScript(QString label); + QString getScriptsFilePath() const; MapPixmapItem *mapItem = nullptr; void setMapItem(MapPixmapItem *item) { mapItem = item; } diff --git a/include/project.h b/include/project.h index cf9fcd73..fc5aef28 100644 --- a/include/project.h +++ b/include/project.h @@ -204,9 +204,8 @@ public: QString fixPalettePath(QString path); QString fixGraphicPath(QString path); - QString getScriptFileExtension(bool usePoryScript) const; + static QString getScriptFileExtension(bool usePoryScript); QString getScriptDefaultString(bool usePoryScript, QString mapName) const; - QString getMapScriptsFilePath(const QString &mapName) const; QStringList getEventScriptsFilePaths() const; QCompleter *getEventScriptLabelCompleter(QStringList additionalScriptLabels); QStringList getGlobalScriptLabels(); diff --git a/src/core/map.cpp b/src/core/map.cpp index 800991e5..e076949b 100644 --- a/src/core/map.cpp +++ b/src/core/map.cpp @@ -27,6 +27,7 @@ Map::~Map() { void Map::setName(QString mapName) { name = mapName; constantName = mapConstantFromName(mapName); + scriptsFileLabels = ParseUtil::getGlobalScriptLabels(this->getScriptsFilePath()); } QString Map::mapConstantFromName(QString mapName, bool includePrefix) { @@ -462,9 +463,10 @@ QList Map::getAllEvents() const { return all_events; } -QStringList Map::eventScriptLabels(Event::Group group) const { +QStringList Map::getScriptLabels(Event::Group group) const { QStringList scriptLabels; + // Get script labels currently in-use by the map's events if (group == Event::Group::None) { ScriptTracker scriptTracker; for (Event *event : this->getAllEvents()) { @@ -479,14 +481,30 @@ QStringList Map::eventScriptLabels(Event::Group group) const { scriptLabels = scriptTracker.getScripts(); } - scriptLabels.removeAll(""); + // Add scripts from map's scripts file, and empty names. + scriptLabels.append(scriptsFileLabels); scriptLabels.prepend("0x0"); scriptLabels.prepend("NULL"); + + scriptLabels.removeAll(""); scriptLabels.removeDuplicates(); return scriptLabels; } +QString Map::getScriptsFilePath() const { + const bool usePoryscript = projectConfig.getUsePoryScript(); + auto path = QDir::cleanPath(QString("%1/%2/%3/scripts") + .arg(projectConfig.getProjectDir()) + .arg(projectConfig.getFilePath(ProjectFilePath::data_map_folders)) + .arg(this->name)); + auto extension = Project::getScriptFileExtension(usePoryscript); + if (usePoryscript && !QFile::exists(path + extension)) + extension = Project::getScriptFileExtension(false); + path += extension; + return path; +} + void Map::removeEvent(Event *event) { for (Event::Group key : events.keys()) { events[key].removeAll(event); diff --git a/src/editor.cpp b/src/editor.cpp index 4c61b5b2..1cd80291 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -2167,13 +2167,12 @@ bool Editor::eventLimitReached(Event::Type event_type) { } void Editor::openMapScripts() const { - const QString scriptPath = project->getMapScriptsFilePath(map->name); - openInTextEditor(scriptPath); + openInTextEditor(map->getScriptsFilePath()); } void Editor::openScript(const QString &scriptLabel) const { // Find the location of scriptLabel. - QStringList scriptPaths(project->getMapScriptsFilePath(map->name)); + QStringList scriptPaths(map->getScriptsFilePath()); scriptPaths << project->getEventScriptsFilePaths(); int lineNum = 0; QString scriptPath = scriptPaths.first(); diff --git a/src/project.cpp b/src/project.cpp index d3db3d86..b6710d83 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -2403,7 +2403,7 @@ QString Project::fixGraphicPath(QString path) { return path; } -QString Project::getScriptFileExtension(bool usePoryScript) const { +QString Project::getScriptFileExtension(bool usePoryScript) { if(usePoryScript) { return ".pory"; } else { @@ -2418,16 +2418,6 @@ QString Project::getScriptDefaultString(bool usePoryScript, QString mapName) con return QString("%1_MapScripts::\n\t.byte 0\n").arg(mapName); } -QString Project::getMapScriptsFilePath(const QString &mapName) const { - const bool usePoryscript = projectConfig.getUsePoryScript(); - auto path = QDir::cleanPath(root + "/" + projectConfig.getFilePath(ProjectFilePath::data_map_folders) + "/" + mapName + "/scripts"); - auto extension = getScriptFileExtension(usePoryscript); - if (usePoryscript && !QFile::exists(path + extension)) - extension = getScriptFileExtension(false); - path += extension; - return path; -} - QStringList Project::getEventScriptsFilePaths() const { QStringList filePaths(QDir::cleanPath(root + "/" + projectConfig.getFilePath(ProjectFilePath::data_event_scripts))); const QString scriptsDir = QDir::cleanPath(root + "/" + projectConfig.getFilePath(ProjectFilePath::data_scripts_folders)); diff --git a/src/ui/eventframes.cpp b/src/ui/eventframes.cpp index 3002569a..348045d4 100644 --- a/src/ui/eventframes.cpp +++ b/src/ui/eventframes.cpp @@ -367,14 +367,16 @@ void ObjectFrame::populate(Project *project) { this->combo_flag->addItems(project->flagNames); this->combo_trainer_type->addItems(project->trainerTypes); - QStringList scriptLabels = this->object->getMap()->eventScriptLabels() + project->getGlobalScriptLabels(); - scriptLabels.removeDuplicates(); - + // The script dropdown is populated with scripts used by the map's events and from its scripts file. + QStringList scriptLabels; if (this->object->getMap()) { - const auto localScriptLabels = this->object->getMap()->eventScriptLabels(); - this->combo_script->addItems(localScriptLabels); + scriptLabels.append(this->object->getMap()->getScriptLabels()); + this->combo_script->addItems(scriptLabels); } + // The dropdown's autocomplete has all script labels across the full project. + scriptLabels.append(project->getGlobalScriptLabels()); + scriptLabels.removeDuplicates(); this->scriptCompleter = new QCompleter(scriptLabels, this); this->scriptCompleter->setCaseSensitivity(Qt::CaseInsensitive); this->scriptCompleter->setFilterMode(Qt::MatchContains); @@ -635,15 +637,16 @@ void TriggerFrame::populate(Project *project) { // var combo this->combo_var->addItems(project->varNames); - // script - QStringList scriptLabels = this->trigger->getMap()->eventScriptLabels() + project->getGlobalScriptLabels(); - scriptLabels.removeDuplicates(); - + // The script dropdown is populated with scripts used by the map's events and from its scripts file. + QStringList scriptLabels; if (this->trigger->getMap()) { - const auto localScriptLabels = this->trigger->getMap()->eventScriptLabels(); - this->combo_script->addItems(localScriptLabels); + scriptLabels.append(this->trigger->getMap()->getScriptLabels()); + this->combo_script->addItems(scriptLabels); } + // The dropdown's autocomplete has all script labels across the full project. + scriptLabels.append(project->getGlobalScriptLabels()); + scriptLabels.removeDuplicates(); this->scriptCompleter = new QCompleter(scriptLabels, this); this->scriptCompleter->setCaseSensitivity(Qt::CaseInsensitive); this->scriptCompleter->setFilterMode(Qt::MatchContains); @@ -769,15 +772,16 @@ void SignFrame::populate(Project *project) { // facing dir this->combo_facing_dir->addItems(project->bgEventFacingDirections); - // script - QStringList scriptLabels = this->sign->getMap()->eventScriptLabels() + project->getGlobalScriptLabels(); - scriptLabels.removeDuplicates(); - + // The script dropdown is populated with scripts used by the map's events and from its scripts file. + QStringList scriptLabels; if (this->sign->getMap()) { - const auto localScriptLabels = this->sign->getMap()->eventScriptLabels(); - this->combo_script->addItems(localScriptLabels); + scriptLabels.append(this->sign->getMap()->getScriptLabels()); + this->combo_script->addItems(scriptLabels); } + // The dropdown's autocomplete has all script labels across the full project. + scriptLabels.append(project->getGlobalScriptLabels()); + scriptLabels.removeDuplicates(); this->scriptCompleter = new QCompleter(scriptLabels, this); this->scriptCompleter->setCaseSensitivity(Qt::CaseInsensitive); this->scriptCompleter->setFilterMode(Qt::MatchContains);