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

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