From 4aaae1a264bd427d06fe276b1f0d6891d3e26f25 Mon Sep 17 00:00:00 2001 From: BigBahss Date: Sun, 22 Nov 2020 01:04:46 -0500 Subject: [PATCH] Add support for opening .inc scripts to the selected event script --- include/core/parseutil.h | 8 ++++++-- include/editor.h | 2 ++ include/mainwindow.h | 3 --- src/core/parseutil.cpp | 19 +++++++++++++++++++ src/editor.cpp | 39 +++++++++++++++++++++++++++++++++++++++ src/mainwindow.cpp | 16 +--------------- 6 files changed, 67 insertions(+), 20 deletions(-) diff --git a/include/core/parseutil.h b/include/core/parseutil.h index 22abe367..4bc5301e 100644 --- a/include/core/parseutil.h +++ b/include/core/parseutil.h @@ -40,8 +40,8 @@ class ParseUtil public: ParseUtil(); void set_root(QString); - QString readTextFile(QString); - void strip_comment(QString*); + static QString readTextFile(QString); + static void strip_comment(QString*); QList* parseAsm(QString); int evaluateDefine(QString, QMap*); QStringList readCArray(QString text, QString label); @@ -54,6 +54,10 @@ public: bool tryParseJsonFile(QJsonDocument *out, QString filepath); bool ensureFieldsExist(QJsonObject obj, QList fields); + // Returns the 1-indexed line number for the definition of scriptLabel in the scripts file at filePath. + // Returns 0 if a definition for scriptLabel cannot be found. + static int getScriptLineNumber(const QString &scriptLabel, const QString &filePath); + private: QString root; QString text; diff --git a/include/editor.h b/include/editor.h index 748759b6..6e04262e 100644 --- a/include/editor.h +++ b/include/editor.h @@ -154,6 +154,7 @@ public: void scaleMapView(int); public slots: + void openMapScripts() const; void maskNonVisibleConnectionTiles(); private: @@ -183,6 +184,7 @@ private: QString getMovementPermissionText(uint16_t collision, uint16_t elevation); QString getMetatileDisplayMessage(uint16_t metatileId); bool eventLimitReached(Map *, QString); + QString constructTextEditorCommand(QString commandTemplate, const QString &path) const; private slots: void onMapStartPaint(QGraphicsSceneMouseEvent *event, MapPixmapItem *item); diff --git a/include/mainwindow.h b/include/mainwindow.h index 6293c627..92be8c07 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -119,8 +119,6 @@ private slots: void duplicate(); - void openInTextEditor(); - void onLoadMapRequested(QString, QString); void onMapChanged(Map *map); void onMapNeedsRedrawing(); @@ -164,7 +162,6 @@ private slots: void on_actionMap_Shift_triggered(); void on_toolButton_deleteObject_clicked(); - void on_toolButton_Open_Scripts_clicked(); void addNewEvent(QString); void updateSelectedObjects(); diff --git a/src/core/parseutil.cpp b/src/core/parseutil.cpp index 80a4cf6c..431d7ba3 100644 --- a/src/core/parseutil.cpp +++ b/src/core/parseutil.cpp @@ -420,3 +420,22 @@ bool ParseUtil::ensureFieldsExist(QJsonObject obj, QList fields) { } return true; } + +int ParseUtil::getScriptLineNumber(const QString &scriptLabel, const QString &filePath) { + if (filePath.endsWith(".inc")) { + const QString text = readTextFile(filePath); + const QStringList lines = text.split('\n'); + for (int lineNumber = 0; lineNumber < lines.count(); ++lineNumber) { + QString line = lines.at(lineNumber); + strip_comment(&line); + if (line.contains(':')) { + const QString parsedLabel = line.left(line.indexOf(':')).trimmed(); + if (parsedLabel == scriptLabel) + return lineNumber + 1; + } + } + } else if (filePath.endsWith(".pory")) { + + } + return 0; +} diff --git a/src/editor.cpp b/src/editor.cpp index 0b4eb6be7..47de6c1e 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -9,6 +9,7 @@ #include "metatile.h" #include "montabwidget.h" #include "editcommands.h" +#include "config.h" #include #include #include @@ -1999,6 +2000,44 @@ void Editor::deleteEvent(Event *event) { //updateSelectedObjects(); } +void Editor::openMapScripts() const { + const QString path = project->getMapScriptsFilePath(map->name); + const QString commandTemplate = porymapConfig.getTextEditorCommandTemplate(); + if (commandTemplate.isEmpty()) { + // Open map scripts in the system's default editor. + QDesktopServices::openUrl(QUrl::fromLocalFile(path)); + } else { + const QString command = constructTextEditorCommand(commandTemplate, path); +#ifdef Q_OS_WIN + // On Windows, a QProcess command must be wrapped in a cmd.exe command. + const QString program = QProcessEnvironment::systemEnvironment().value("COMSPEC"); + QStringList arguments({ QString("/c"), command }); +#else + QStringList arguments = QProcess::splitCommand(command); + const QString program = arguments.takeFirst(); +#endif + static QProcess proc; + proc.setProgram(program); + proc.setArguments(arguments); + proc.start(); + } +} + +QString Editor::constructTextEditorCommand(QString commandTemplate, const QString &path) const { + if (commandTemplate.contains("%F")) { + if (commandTemplate.contains("%L")) { + const QString scriptLabel = selected_events->isEmpty() ? + QString() : selected_events->first()->event->get("script_label"); + const int lineNum = ParseUtil::getScriptLineNumber(scriptLabel, path); + commandTemplate.replace("%L", QString::number(lineNum)); + } + commandTemplate.replace("%F", path); + } else { + commandTemplate += path; + } + return commandTemplate; +} + // It doesn't seem to be possible to prevent the mousePress event // from triggering both event's DraggablePixmapItem and the background mousePress. // Since the DraggablePixmapItem's event fires first, we can set a temp diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 7f674ed3..cb72165d 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -154,6 +154,7 @@ void MainWindow::initEditor() { connect(this->editor, SIGNAL(warpEventDoubleClicked(QString,QString)), this, SLOT(openWarpMap(QString,QString))); connect(this->editor, SIGNAL(currentMetatilesSelectionChanged()), this, SLOT(currentMetatilesSelectionChanged())); connect(this->editor, SIGNAL(wildMonDataChanged()), this, SLOT(onWildMonDataChanged())); + connect(ui->toolButton_Open_Scripts, &QToolButton::clicked, this->editor, &Editor::openMapScripts); this->loadUserSettings(); @@ -1293,16 +1294,6 @@ void MainWindow::duplicate() { editor->duplicateSelectedEvents(); } -// Open current map scripts in system default editor for .inc files -void MainWindow::openInTextEditor() { - bool usePoryscript = projectConfig.getUsePoryScript(); - QString path = QDir::cleanPath("file://" + editor->project->root + QDir::separator() + "data/maps/" + editor->map->name + "/scripts"); - - // Try opening scripts file, if opening .pory failed try again with .inc - if (!QDesktopServices::openUrl(QUrl(path + editor->project->getScriptFileExtension(usePoryscript))) && usePoryscript) - QDesktopServices::openUrl(QUrl(path + editor->project->getScriptFileExtension(false))); -} - void MainWindow::on_action_Save_triggered() { editor->save(); updateMapList(); @@ -2093,11 +2084,6 @@ void MainWindow::on_toolButton_deleteObject_clicked() { } } -void MainWindow::on_toolButton_Open_Scripts_clicked() -{ - openInTextEditor(); -} - void MainWindow::on_toolButton_Paint_clicked() { if (ui->mainTabBar->currentIndex() == 0)