Add support for opening .inc scripts to the selected event script

This commit is contained in:
BigBahss 2020-11-22 01:04:46 -05:00
parent 3478846b60
commit 4aaae1a264
6 changed files with 67 additions and 20 deletions

View file

@ -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<QStringList>* parseAsm(QString);
int evaluateDefine(QString, QMap<QString, int>*);
QStringList readCArray(QString text, QString label);
@ -54,6 +54,10 @@ public:
bool tryParseJsonFile(QJsonDocument *out, QString filepath);
bool ensureFieldsExist(QJsonObject obj, QList<QString> 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;

View file

@ -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);

View file

@ -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();

View file

@ -420,3 +420,22 @@ bool ParseUtil::ensureFieldsExist(QJsonObject obj, QList<QString> 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;
}

View file

@ -9,6 +9,7 @@
#include "metatile.h"
#include "montabwidget.h"
#include "editcommands.h"
#include "config.h"
#include <QCheckBox>
#include <QPainter>
#include <QMouseEvent>
@ -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

View file

@ -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)