mirror of
https://github.com/huderlem/porymap.git
synced 2026-09-25 09:15:34 -05:00
Add support for opening .inc scripts to the selected event script
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user