Skip to content

Commit

Permalink
Add labels from scripts file to Script dropdowns
Browse files Browse the repository at this point in the history
  • Loading branch information
GriffinRichards committed Dec 30, 2023
1 parent 6a8d3a8 commit ced402a
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 36 deletions.
5 changes: 4 additions & 1 deletion include/core/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Map : public QObject
QString battle_scene;
QString sharedEventsMap = "";
QString sharedScriptsMap = "";
QStringList scriptsFileLabels;
QMap<QString, QJsonValue> customHeaders;
MapLayout *layout;
bool isPersistedToFile = true;
Expand All @@ -70,6 +71,7 @@ class Map : public QObject
QList<MapConnection*> connections;
QList<int> metatileLayerOrder;
QList<float> metatileLayerOpacity;

void setName(QString mapName);
static QString mapConstantFromName(QString mapName, bool includePrefix = true);
int getWidth();
Expand All @@ -92,7 +94,7 @@ class Map : public QObject
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<Event *> 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 *);
Expand All @@ -105,6 +107,7 @@ class Map : public QObject
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; }
Expand Down
3 changes: 1 addition & 2 deletions include/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,8 @@ class Project : public QObject
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();
Expand Down
22 changes: 20 additions & 2 deletions src/core/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -462,9 +463,10 @@ QList<Event *> 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()) {
Expand All @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions src/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 1 addition & 11 deletions src/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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));
Expand Down
38 changes: 21 additions & 17 deletions src/ui/eventframes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ced402a

Please sign in to comment.