From 0a15dfbf4c964d073ada2f9e1524d465a0b54b1d Mon Sep 17 00:00:00 2001 From: BigBahss Date: Mon, 16 Nov 2020 06:46:21 -0500 Subject: [PATCH 01/16] Add preferred text editor to PorymapConfig --- include/config.h | 3 +++ src/config.cpp | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/include/config.h b/include/config.h index 008eeeb7..a680c55d 100644 --- a/include/config.h +++ b/include/config.h @@ -61,6 +61,7 @@ public: void setMonitorFiles(bool monitor); void setRegionMapDimensions(int width, int height); void setTheme(QString theme); + void setTextEditorCommand(const QString &command); QString getRecentProject(); QString getRecentMap(); MapSortOrder getMapSortOrder(); @@ -76,6 +77,7 @@ public: bool getMonitorFiles(); QSize getRegionMapDimensions(); QString getTheme(); + QString getTextEditorCommand(); protected: virtual QString getConfigFilepath() override; virtual void parseConfigKeyValue(QString key, QString value) override; @@ -107,6 +109,7 @@ private: bool monitorFiles; QSize regionMapDimensions; QString theme; + QString textEditorCommand; }; extern PorymapConfig porymapConfig; diff --git a/src/config.cpp b/src/config.cpp index 3c9fe33d..2a1b16c7 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -187,6 +187,8 @@ void PorymapConfig::parseConfigKeyValue(QString key, QString value) { } } else if (key == "theme") { this->theme = value; + } else if (key == "text_editor") { + this->textEditorCommand = value; } else { logWarn(QString("Invalid config key found in config file %1: '%2'").arg(this->getConfigFilepath()).arg(key)); } @@ -216,6 +218,7 @@ QMap PorymapConfig::getKeyValueMap() { map.insert("region_map_dimensions", QString("%1x%2").arg(this->regionMapDimensions.width()) .arg(this->regionMapDimensions.height())); map.insert("theme", this->theme); + map.insert("text_editor", this->textEditorCommand); return map; } @@ -316,6 +319,11 @@ void PorymapConfig::setTheme(QString theme) { this->theme = theme; } +void PorymapConfig::setTextEditorCommand(const QString &command) { + this->textEditorCommand = command; + this->save(); +} + QString PorymapConfig::getRecentProject() { return this->recentProject; } @@ -398,6 +406,10 @@ QString PorymapConfig::getTheme() { return this->theme; } +QString PorymapConfig::getTextEditorCommand() { + return this->textEditorCommand; +} + const QMap baseGameVersionMap = { {BaseGameVersion::pokeruby, "pokeruby"}, {BaseGameVersion::pokefirered, "pokefirered"}, From ea9cfa47e55d9c432898a6895b7ddae9341c918f Mon Sep 17 00:00:00 2001 From: BigBahss Date: Mon, 16 Nov 2020 07:39:42 -0500 Subject: [PATCH 02/16] Add basic prefences window with text editor command field --- forms/mainwindow.ui | 7 ++++ forms/preferenceeditor.ui | 75 +++++++++++++++++++++++++++++++++++ include/mainwindow.h | 3 ++ include/ui/preferenceeditor.h | 31 +++++++++++++++ porymap.pro | 5 ++- src/mainwindow.cpp | 15 +++++++ src/ui/preferenceeditor.cpp | 42 ++++++++++++++++++++ 7 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 forms/preferenceeditor.ui create mode 100644 include/ui/preferenceeditor.h create mode 100644 src/ui/preferenceeditor.cpp diff --git a/forms/mainwindow.ui b/forms/mainwindow.ui index f1061bdb..fd97121e 100644 --- a/forms/mainwindow.ui +++ b/forms/mainwindow.ui @@ -2639,6 +2639,8 @@ + + @@ -2905,6 +2907,11 @@ Export Map Stitch Image... + + + Edit Preferences... + + diff --git a/forms/preferenceeditor.ui b/forms/preferenceeditor.ui new file mode 100644 index 00000000..78b6da04 --- /dev/null +++ b/forms/preferenceeditor.ui @@ -0,0 +1,75 @@ + + + PreferenceEditor + + + + 0 + 0 + 480 + 320 + + + + Preferences + + + + + + + Preferred Text Editor + + + + + + Command + + + + + + + The command (including the necessary parameters) to perform the action. See the command parameter legend below for a list of supported parameter variables, which will be substituted when launching the command. When upper-case letters (e.g. %F, $D, %N) are used, the action will be applicable even if more than one item is selected. Esle the action will only be applicable f exactly one item is selected. + + + e.g. code --goto %F:%L + + + true + + + + + + + <html><head/><body><p>The command that will be executed when clicking the &quot;Open Map Scripts&quot; button. You may optionally include '%F' and '%L' in the command. '%F' will be substituted with the scripts file path and '%L' will be substituted with a line number. If '%L' is specified then the scripts file will be opened to map script cooresponding to the currently selected event (If the script can be found). If '%F' is <span style=" font-weight:600;">not</span> specified then the map scripts file path will be appended to the end of the command.</p></body></html> + + + false + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + true + + + + + + + + + + QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + diff --git a/include/mainwindow.h b/include/mainwindow.h index a9180402..7cc74e6a 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -21,6 +21,7 @@ #include "filterchildrenproxymodel.h" #include "newmappopup.h" #include "newtilesetdialog.h" +#include "preferenceeditor.h" namespace Ui { class MainWindow; @@ -226,6 +227,7 @@ private slots: void on_pushButton_ConfigureEncountersJSON_clicked(); void on_actionRegion_Map_Editor_triggered(); + void on_actionEdit_Preferences_triggered(); private: Ui::MainWindow *ui; @@ -234,6 +236,7 @@ private: MapImageExporter *mapImageExporter = nullptr; FilterChildrenProxyModel *mapListProxyModel; NewMapPopup *newmapprompt = nullptr; + PreferenceEditor *preferenceEditor = nullptr; QStandardItemModel *mapListModel; QList *mapGroupItemsList; QMap mapListIndexes; diff --git a/include/ui/preferenceeditor.h b/include/ui/preferenceeditor.h new file mode 100644 index 00000000..74085b6a --- /dev/null +++ b/include/ui/preferenceeditor.h @@ -0,0 +1,31 @@ +#ifndef PREFERENCES_H +#define PREFERENCES_H + +#include + +class QAbstractButton; + + +namespace Ui { +class PreferenceEditor; +} + +class PreferenceEditor : public QMainWindow +{ + Q_OBJECT + +public: + explicit PreferenceEditor(QWidget *parent = nullptr); + ~PreferenceEditor(); + +private: + Ui::PreferenceEditor *ui; + + void populateFields(); + void saveFields(); + +private slots: + void dialogButtonClicked(QAbstractButton *button); +}; + +#endif // PREFERENCES_H diff --git a/porymap.pro b/porymap.pro index 0da4a14b..806f3b5b 100644 --- a/porymap.pro +++ b/porymap.pro @@ -71,6 +71,7 @@ SOURCES += src/core/block.cpp \ src/ui/newtilesetdialog.cpp \ src/ui/flowlayout.cpp \ src/ui/mapruler.cpp \ + src/ui/preferenceeditor.cpp \ src/config.cpp \ src/editor.cpp \ src/main.cpp \ @@ -140,6 +141,7 @@ HEADERS += include/core/block.h \ include/ui/overlay.h \ include/ui/flowlayout.h \ include/ui/mapruler.h \ + include/ui/preferenceeditor.h \ include/config.h \ include/editor.h \ include/mainwindow.h \ @@ -156,7 +158,8 @@ FORMS += forms/mainwindow.ui \ forms/newmappopup.ui \ forms/aboutporymap.ui \ forms/newtilesetdialog.ui \ - forms/mapimageexporter.ui + forms/mapimageexporter.ui \ + forms/preferenceeditor.ui RESOURCES += \ resources/images.qrc \ diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index f239d4d6..31ad9da1 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -2560,6 +2560,21 @@ void MainWindow::on_actionThemes_triggered() themeSelectorWindow.exec(); } +void MainWindow::on_actionEdit_Preferences_triggered() { + if (!preferenceEditor) { + preferenceEditor = new PreferenceEditor(this); + connect(preferenceEditor, &QObject::destroyed, [=](QObject *) { preferenceEditor = nullptr; }); + } + + if (!preferenceEditor->isVisible()) { + preferenceEditor->show(); + } else if (preferenceEditor->isMinimized()) { + preferenceEditor->showNormal(); + } else { + preferenceEditor->activateWindow(); + } +} + void MainWindow::on_pushButton_AddCustomHeaderField_clicked() { int rowIndex = this->ui->tableWidget_CustomHeaderFields->rowCount(); diff --git a/src/ui/preferenceeditor.cpp b/src/ui/preferenceeditor.cpp new file mode 100644 index 00000000..25476dfe --- /dev/null +++ b/src/ui/preferenceeditor.cpp @@ -0,0 +1,42 @@ +#include "preferenceeditor.h" +#include "ui_preferenceeditor.h" +#include "config.h" + +#include + + +PreferenceEditor::PreferenceEditor(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::PreferenceEditor) +{ + ui->setupUi(this); + setAttribute(Qt::WA_DeleteOnClose); + connect(ui->buttonBox, &QDialogButtonBox::clicked, + this, &PreferenceEditor::dialogButtonClicked); + populateFields(); +} + +PreferenceEditor::~PreferenceEditor() +{ + delete ui; +} + +void PreferenceEditor::populateFields() { + ui->lineEdit_TextEditor->setText(porymapConfig.getTextEditorCommand()); +} + +void PreferenceEditor::saveFields() { + porymapConfig.setTextEditorCommand(ui->lineEdit_TextEditor->text()); +} + +void PreferenceEditor::dialogButtonClicked(QAbstractButton *button) { + auto buttonRole = ui->buttonBox->buttonRole(button); + if (buttonRole == QDialogButtonBox::AcceptRole) { + saveFields(); + close(); + } else if (buttonRole == QDialogButtonBox::ApplyRole) { + saveFields(); + } else if (buttonRole == QDialogButtonBox::RejectRole) { + close(); + } +} From 662fb2a367333c8a54c61a80088d4665b3bb39bd Mon Sep 17 00:00:00 2001 From: BigBahss Date: Mon, 16 Nov 2020 09:35:55 -0500 Subject: [PATCH 03/16] Move themes to PreferenceEditor --- forms/mainwindow.ui | 7 ------- forms/preferenceeditor.ui | 15 +++++++++++++- include/editor.h | 4 +++- include/mainwindow.h | 1 - include/ui/preferenceeditor.h | 6 ++++++ src/mainwindow.cpp | 38 ++++------------------------------- src/ui/preferenceeditor.cpp | 28 +++++++++++++++++++++++++- 7 files changed, 54 insertions(+), 45 deletions(-) diff --git a/forms/mainwindow.ui b/forms/mainwindow.ui index fd97121e..b3179047 100644 --- a/forms/mainwindow.ui +++ b/forms/mainwindow.ui @@ -2607,8 +2607,6 @@ - - @@ -2897,11 +2895,6 @@ Ctrl+Shift+N - - - Themes... - - Export Map Stitch Image... diff --git a/forms/preferenceeditor.ui b/forms/preferenceeditor.ui index 78b6da04..d9e7fa72 100644 --- a/forms/preferenceeditor.ui +++ b/forms/preferenceeditor.ui @@ -16,7 +16,7 @@ - + Preferred Text Editor @@ -60,6 +60,19 @@ + + + + + 0 + 0 + + + + Application Theme + + + diff --git a/include/editor.h b/include/editor.h index fc8cfe26..748759b6 100644 --- a/include/editor.h +++ b/include/editor.h @@ -65,7 +65,6 @@ public: void displayMapBorder(); void displayMapGrid(); void displayWildMonTables(); - void maskNonVisibleConnectionTiles(); void updateMapBorder(); void updateMapConnections(); @@ -154,6 +153,9 @@ public: void shouldReselectEvents(); void scaleMapView(int); +public slots: + void maskNonVisibleConnectionTiles(); + private: void setConnectionItemsVisible(bool); void setBorderItemsVisible(bool, qreal = 1); diff --git a/include/mainwindow.h b/include/mainwindow.h index 7cc74e6a..6293c627 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -218,7 +218,6 @@ private slots: void on_toolButton_ExpandAll_clicked(); void on_toolButton_CollapseAll_clicked(); void on_actionAbout_Porymap_triggered(); - void on_actionThemes_triggered(); void on_pushButton_AddCustomHeaderField_clicked(); void on_pushButton_DeleteCustomHeaderField_clicked(); void on_tableWidget_CustomHeaderFields_cellChanged(int row, int column); diff --git a/include/ui/preferenceeditor.h b/include/ui/preferenceeditor.h index 74085b6a..c16716d9 100644 --- a/include/ui/preferenceeditor.h +++ b/include/ui/preferenceeditor.h @@ -3,6 +3,7 @@ #include +class NoScrollComboBox; class QAbstractButton; @@ -18,8 +19,13 @@ public: explicit PreferenceEditor(QWidget *parent = nullptr); ~PreferenceEditor(); +signals: + void preferencesSaved(); + void themeChanged(const QString &theme); + private: Ui::PreferenceEditor *ui; + NoScrollComboBox *themeSelector; void populateFields(); void saveFields(); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 31ad9da1..7f674ed3 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -2526,43 +2526,13 @@ void MainWindow::on_actionAbout_Porymap_triggered() window->show(); } -void MainWindow::on_actionThemes_triggered() -{ - QStringList themes; - QRegularExpression re(":/themes/([A-z0-9_-]+).qss"); - themes.append("default"); - QDirIterator it(":/themes", QDirIterator::Subdirectories); - while (it.hasNext()) { - QString themeName = re.match(it.next()).captured(1); - themes.append(themeName); - } - - QDialog themeSelectorWindow(this); - QFormLayout form(&themeSelectorWindow); - - NoScrollComboBox *themeSelector = new NoScrollComboBox(); - themeSelector->addItems(themes); - themeSelector->setCurrentText(porymapConfig.getTheme()); - form.addRow(new QLabel("Themes"), themeSelector); - - QDialogButtonBox buttonBox(QDialogButtonBox::Apply | QDialogButtonBox::Close, Qt::Horizontal, &themeSelectorWindow); - form.addRow(&buttonBox); - connect(&buttonBox, &QDialogButtonBox::clicked, [&buttonBox, themeSelector, this](QAbstractButton *button){ - if (button == buttonBox.button(QDialogButtonBox::Apply)) { - QString theme = themeSelector->currentText(); - porymapConfig.setTheme(theme); - this->setTheme(theme); - editor->maskNonVisibleConnectionTiles(); - } - }); - connect(&buttonBox, SIGNAL(rejected()), &themeSelectorWindow, SLOT(reject())); - - themeSelectorWindow.exec(); -} - void MainWindow::on_actionEdit_Preferences_triggered() { if (!preferenceEditor) { preferenceEditor = new PreferenceEditor(this); + connect(preferenceEditor, &PreferenceEditor::themeChanged, + this, &MainWindow::setTheme); + connect(preferenceEditor, &PreferenceEditor::themeChanged, + editor, &Editor::maskNonVisibleConnectionTiles); connect(preferenceEditor, &QObject::destroyed, [=](QObject *) { preferenceEditor = nullptr; }); } diff --git a/src/ui/preferenceeditor.cpp b/src/ui/preferenceeditor.cpp index 25476dfe..42edd313 100644 --- a/src/ui/preferenceeditor.cpp +++ b/src/ui/preferenceeditor.cpp @@ -1,15 +1,23 @@ #include "preferenceeditor.h" #include "ui_preferenceeditor.h" #include "config.h" +#include "noscrollcombobox.h" #include +#include +#include +#include PreferenceEditor::PreferenceEditor(QWidget *parent) : QMainWindow(parent), - ui(new Ui::PreferenceEditor) + ui(new Ui::PreferenceEditor), + themeSelector(nullptr) { ui->setupUi(this); + auto *formLayout = new QFormLayout(ui->groupBox_Themes); + themeSelector = new NoScrollComboBox(ui->groupBox_Themes); + formLayout->addRow("Themes", themeSelector); setAttribute(Qt::WA_DeleteOnClose); connect(ui->buttonBox, &QDialogButtonBox::clicked, this, &PreferenceEditor::dialogButtonClicked); @@ -23,10 +31,28 @@ PreferenceEditor::~PreferenceEditor() void PreferenceEditor::populateFields() { ui->lineEdit_TextEditor->setText(porymapConfig.getTextEditorCommand()); + + QStringList themes = { "default" }; + QRegularExpression re(":/themes/([A-z0-9_-]+).qss"); + QDirIterator it(":/themes", QDirIterator::Subdirectories); + while (it.hasNext()) { + QString themeName = re.match(it.next()).captured(1); + themes.append(themeName); + } + themeSelector->addItems(themes); + themeSelector->setCurrentText(porymapConfig.getTheme()); } void PreferenceEditor::saveFields() { porymapConfig.setTextEditorCommand(ui->lineEdit_TextEditor->text()); + + if (themeSelector->currentText() != porymapConfig.getTheme()) { + const auto theme = themeSelector->currentText(); + porymapConfig.setTheme(theme); + emit themeChanged(theme); + } + + emit preferencesSaved(); } void PreferenceEditor::dialogButtonClicked(QAbstractButton *button) { From 3478846b6019efff0d8b8d5d4f0d7cbeba91ee3a Mon Sep 17 00:00:00 2001 From: BigBahss Date: Sat, 21 Nov 2020 17:33:16 -0500 Subject: [PATCH 04/16] Add Project::getMapScriptsFilePath() and rename text editor config members --- include/config.h | 6 +++--- include/project.h | 5 +++-- src/config.cpp | 12 ++++++------ src/project.cpp | 14 ++++++++++++-- src/ui/preferenceeditor.cpp | 4 ++-- 5 files changed, 26 insertions(+), 15 deletions(-) diff --git a/include/config.h b/include/config.h index a680c55d..bb740d41 100644 --- a/include/config.h +++ b/include/config.h @@ -61,7 +61,7 @@ public: void setMonitorFiles(bool monitor); void setRegionMapDimensions(int width, int height); void setTheme(QString theme); - void setTextEditorCommand(const QString &command); + void setTextEditorCommandTemplate(const QString &commandTemplate); QString getRecentProject(); QString getRecentMap(); MapSortOrder getMapSortOrder(); @@ -77,7 +77,7 @@ public: bool getMonitorFiles(); QSize getRegionMapDimensions(); QString getTheme(); - QString getTextEditorCommand(); + QString getTextEditorCommandTemplate(); protected: virtual QString getConfigFilepath() override; virtual void parseConfigKeyValue(QString key, QString value) override; @@ -109,7 +109,7 @@ private: bool monitorFiles; QSize regionMapDimensions; QString theme; - QString textEditorCommand; + QString textEditorCommandTemplate; }; extern PorymapConfig porymapConfig; diff --git a/include/project.h b/include/project.h index 179a5a93..2dcaa345 100644 --- a/include/project.h +++ b/include/project.h @@ -172,8 +172,9 @@ public: QString fixPalettePath(QString path); QString fixGraphicPath(QString path); - QString getScriptFileExtension(bool usePoryScript); - QString getScriptDefaultString(bool usePoryScript, QString mapName); + QString getScriptFileExtension(bool usePoryScript) const; + QString getScriptDefaultString(bool usePoryScript, QString mapName) const; + QString getMapScriptsFilePath(const QString &mapName) const; bool loadMapBorder(Map *map); diff --git a/src/config.cpp b/src/config.cpp index 2a1b16c7..24798f7c 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -188,7 +188,7 @@ void PorymapConfig::parseConfigKeyValue(QString key, QString value) { } else if (key == "theme") { this->theme = value; } else if (key == "text_editor") { - this->textEditorCommand = value; + this->textEditorCommandTemplate = value; } else { logWarn(QString("Invalid config key found in config file %1: '%2'").arg(this->getConfigFilepath()).arg(key)); } @@ -218,7 +218,7 @@ QMap PorymapConfig::getKeyValueMap() { map.insert("region_map_dimensions", QString("%1x%2").arg(this->regionMapDimensions.width()) .arg(this->regionMapDimensions.height())); map.insert("theme", this->theme); - map.insert("text_editor", this->textEditorCommand); + map.insert("text_editor", this->textEditorCommandTemplate); return map; } @@ -319,8 +319,8 @@ void PorymapConfig::setTheme(QString theme) { this->theme = theme; } -void PorymapConfig::setTextEditorCommand(const QString &command) { - this->textEditorCommand = command; +void PorymapConfig::setTextEditorCommandTemplate(const QString &commandTemplate) { + this->textEditorCommandTemplate = commandTemplate; this->save(); } @@ -406,8 +406,8 @@ QString PorymapConfig::getTheme() { return this->theme; } -QString PorymapConfig::getTextEditorCommand() { - return this->textEditorCommand; +QString PorymapConfig::getTextEditorCommandTemplate() { + return this->textEditorCommandTemplate; } const QMap baseGameVersionMap = { diff --git a/src/project.cpp b/src/project.cpp index 270e9346..6cbd7c92 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -2410,7 +2410,7 @@ QString Project::fixGraphicPath(QString path) { return path; } -QString Project::getScriptFileExtension(bool usePoryScript) { +QString Project::getScriptFileExtension(bool usePoryScript) const { if(usePoryScript) { return ".pory"; } else { @@ -2418,13 +2418,23 @@ QString Project::getScriptFileExtension(bool usePoryScript) { } } -QString Project::getScriptDefaultString(bool usePoryScript, QString mapName) { +QString Project::getScriptDefaultString(bool usePoryScript, QString mapName) const { if(usePoryScript) return QString("mapscripts %1_MapScripts {}").arg(mapName); else 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 + "/data/maps/" + mapName + "/scripts"); + auto extension = getScriptFileExtension(usePoryscript); + if (usePoryscript && !QFile::exists(path + extension)) + extension = getScriptFileExtension(false); + path += extension; + return path; +} + void Project::loadEventPixmaps(QList objects) { bool needs_update = false; for (Event *object : objects) { diff --git a/src/ui/preferenceeditor.cpp b/src/ui/preferenceeditor.cpp index 42edd313..9a22a250 100644 --- a/src/ui/preferenceeditor.cpp +++ b/src/ui/preferenceeditor.cpp @@ -30,7 +30,7 @@ PreferenceEditor::~PreferenceEditor() } void PreferenceEditor::populateFields() { - ui->lineEdit_TextEditor->setText(porymapConfig.getTextEditorCommand()); + ui->lineEdit_TextEditor->setText(porymapConfig.getTextEditorCommandTemplate()); QStringList themes = { "default" }; QRegularExpression re(":/themes/([A-z0-9_-]+).qss"); @@ -44,7 +44,7 @@ void PreferenceEditor::populateFields() { } void PreferenceEditor::saveFields() { - porymapConfig.setTextEditorCommand(ui->lineEdit_TextEditor->text()); + porymapConfig.setTextEditorCommandTemplate(ui->lineEdit_TextEditor->text()); if (themeSelector->currentText() != porymapConfig.getTheme()) { const auto theme = themeSelector->currentText(); From 4aaae1a264bd427d06fe276b1f0d6891d3e26f25 Mon Sep 17 00:00:00 2001 From: BigBahss Date: Sun, 22 Nov 2020 01:04:46 -0500 Subject: [PATCH 05/16] 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) From a4528fb0d9482fb63392ddefd4b2f860a1754539 Mon Sep 17 00:00:00 2001 From: BigBahss Date: Thu, 26 Nov 2020 06:09:58 -0500 Subject: [PATCH 06/16] Add support for opening .pory scripts to the selected event script --- include/core/parseutil.h | 9 +++-- src/core/parseutil.cpp | 75 ++++++++++++++++++++++++++++++++-------- src/editor.cpp | 14 ++++---- 3 files changed, 74 insertions(+), 24 deletions(-) diff --git a/include/core/parseutil.h b/include/core/parseutil.h index 4bc5301e..b65dbf76 100644 --- a/include/core/parseutil.h +++ b/include/core/parseutil.h @@ -41,7 +41,7 @@ public: ParseUtil(); void set_root(QString); static QString readTextFile(QString); - static void strip_comment(QString*); + void strip_comment(QString*); QList* parseAsm(QString); int evaluateDefine(QString, QMap*); QStringList readCArray(QString text, QString label); @@ -56,7 +56,12 @@ public: // 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); + static int getScriptLineNumber(const QString &filePath, const QString &scriptLabel); + static int getRawScriptLineNumber(QString text, const QString &scriptLabel); + static int getPoryScriptLineNumber(QString text, const QString &scriptLabel); + static QString &removeStringLiterals(QString &text); + static QString &removeLineComments(QString &text, const QString &commentSymbol); + static QString &removeLineComments(QString &text, const QStringList &commentSymbols); private: QString root; diff --git a/src/core/parseutil.cpp b/src/core/parseutil.cpp index 431d7ba3..5dc4f176 100644 --- a/src/core/parseutil.cpp +++ b/src/core/parseutil.cpp @@ -421,21 +421,66 @@ 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")) { +int ParseUtil::getScriptLineNumber(const QString &filePath, const QString &scriptLabel) { + if (filePath.endsWith(".inc")) + return getRawScriptLineNumber(readTextFile(filePath), scriptLabel); + else if (filePath.endsWith(".pory")) + return getPoryScriptLineNumber(readTextFile(filePath), scriptLabel); - } return 0; } + +int ParseUtil::getRawScriptLineNumber(QString text, const QString &scriptLabel) { + removeStringLiterals(text); + removeLineComments(text, "@"); + + static const QRegularExpression re_incScriptLabel("(?