diff --git a/forms/customscriptseditor.ui b/forms/customscriptseditor.ui
index 8166ed8d..2469339d 100644
--- a/forms/customscriptseditor.ui
+++ b/forms/customscriptseditor.ui
@@ -6,7 +6,7 @@
0
0
- 374
+ 535
355
@@ -49,20 +49,40 @@
-
-
+
+
+ Create a new Porymap script file with a default template
+
- Add New Script...
+ Create New Script...
- :/icons/add.ico:/icons/add.ico
+ :/icons/file_add.ico:/icons/file_add.ico
-
-
+
+
+ Add an existing script file to the list below
+
- Reload Scripts
+ Load Script...
+
+
+
+ :/icons/file_put.ico:/icons/file_put.ico
+
+
+
+ -
+
+
+ Refresh all loaded scripts to account for any recent edits
+
+
+ Refresh Scripts
@@ -128,7 +148,7 @@
false
- QAbstractItemView::NoDragDrop
+ QAbstractItemView::DragOnly
Qt::IgnoreAction
diff --git a/forms/customscriptslistitem.ui b/forms/customscriptslistitem.ui
index 7c7e8794..3a34e954 100644
--- a/forms/customscriptslistitem.ui
+++ b/forms/customscriptslistitem.ui
@@ -6,7 +6,7 @@
0
0
- 129
+ 151
34
@@ -24,16 +24,12 @@
4
-
-
+
- Choose a new filepath for this script
+ If unchecked this script will be ignored
- ...
-
-
-
- :/icons/folder.ico:/icons/folder.ico
+
@@ -51,12 +47,16 @@
-
-
+
- If unchecked this script will be ignored
+ Choose a new filepath for this script
-
+ ...
+
+
+
+ :/icons/folder.ico:/icons/folder.ico
@@ -70,7 +70,7 @@
- :/icons/edit_document.ico:/icons/edit_document.ico
+ :/icons/file_edit.ico:/icons/file_edit.ico
diff --git a/include/ui/customscriptseditor.h b/include/ui/customscriptseditor.h
index d54a7cc3..27238c4e 100644
--- a/include/ui/customscriptseditor.h
+++ b/include/ui/customscriptseditor.h
@@ -31,10 +31,11 @@ private:
Ui::CustomScriptsEditor *ui;
bool hasUnsavedChanges = false;
- QString importDir;
+ QString fileDialogDir;
const QString baseDir;
void displayScript(const QString &filepath, bool enabled);
+ void displayNewScript(QString filepath);
QString chooseScript(QString dir);
void removeScript(QListWidgetItem * item);
void replaceScript(QListWidgetItem * item);
@@ -52,8 +53,9 @@ private:
private slots:
void dialogButtonClicked(QAbstractButton *button);
- void addNewScript();
- void reloadScripts();
+ void createNewScript();
+ void loadScript();
+ void refreshScripts();
void removeSelectedScripts();
void openSelectedScripts();
};
diff --git a/resources/icons/edit_document.ico b/resources/icons/edit_document.ico
deleted file mode 100755
index cd4bb026..00000000
Binary files a/resources/icons/edit_document.ico and /dev/null differ
diff --git a/resources/icons/file_add.ico b/resources/icons/file_add.ico
new file mode 100755
index 00000000..d33fc2bd
Binary files /dev/null and b/resources/icons/file_add.ico differ
diff --git a/resources/icons/file_edit.ico b/resources/icons/file_edit.ico
new file mode 100755
index 00000000..d197d1d6
Binary files /dev/null and b/resources/icons/file_edit.ico differ
diff --git a/resources/icons/file_put.ico b/resources/icons/file_put.ico
new file mode 100755
index 00000000..8359c0e5
Binary files /dev/null and b/resources/icons/file_put.ico differ
diff --git a/resources/images.qrc b/resources/images.qrc
index 2399cbe9..8afb0df4 100644
--- a/resources/images.qrc
+++ b/resources/images.qrc
@@ -4,8 +4,10 @@
icons/collapse_all.ico
icons/cursor.ico
icons/delete.ico
- icons/edit_document.ico
icons/expand_all.ico
+ icons/file_add.ico
+ icons/file_edit.ico
+ icons/file_put.ico
icons/fill_color_cursor.ico
icons/fill_color.ico
icons/folder_closed_map.ico
diff --git a/resources/text.qrc b/resources/text.qrc
index 8567f8b5..b1c47e58 100644
--- a/resources/text.qrc
+++ b/resources/text.qrc
@@ -6,6 +6,7 @@
text/prefabs_default_emerald.json
text/prefabs_default_firered.json
text/prefabs_default_ruby.json
+ text/script_template.js
../CHANGELOG.md
diff --git a/resources/text/script_template.js b/resources/text/script_template.js
new file mode 100644
index 00000000..fdd1949f
--- /dev/null
+++ b/resources/text/script_template.js
@@ -0,0 +1,71 @@
+// Called when Porymap successfully opens a project.
+export function onProjectOpened(projectPath) {
+
+}
+
+// Called when Porymap closes a project. For example, this is called when opening a different project.
+export function onProjectClosed(projectPath) {
+
+}
+
+// Called when a map is opened.
+export function onMapOpened(mapName) {
+
+}
+
+// Called when a block is changed on the map. For example, this is called when a user paints a new tile or changes the collision property of a block.
+export function onBlockChanged(x, y, prevBlock, newBlock) {
+
+}
+
+// Called when a border metatile is changed.
+export function onBorderMetatileChanged(x, y, prevMetatileId, newMetatileId) {
+
+}
+
+// Called when the mouse enters a new map block.
+export function onBlockHoverChanged(x, y) {
+
+}
+
+// Called when the mouse exits the map.
+export function onBlockHoverCleared() {
+
+}
+
+// Called when the dimensions of the map are changed.
+export function onMapResized(oldWidth, oldHeight, newWidth, newHeight) {
+
+}
+
+// Called when the dimensions of the border are changed.
+export function onBorderResized(oldWidth, oldHeight, newWidth, newHeight) {
+
+}
+
+// Called when the map is updated by use of the Map Shift tool.
+export function onMapShifted(xDelta, yDelta) {
+
+}
+
+// Called when the currently loaded tileset is changed by switching to a new one or by saving changes to it in the Tileset Editor.
+export function onTilesetUpdated(tilesetName) {
+
+}
+
+// Called when the selected tab in the main tab bar is changed.
+// Tabs are indexed from left to right, starting at 0 (0: Map, 1: Events, 2: Header, 3: Connections, 4: Wild Pokemon).
+export function onMainTabChanged(oldTab, newTab) {
+
+}
+
+// Called when the selected tab in the map view tab bar is changed.
+// Tabs are indexed from left to right, starting at 0 (0: Metatiles, 1: Collision, 2: Prefabs).
+export function onMapViewTabChanged(oldTab, newTab) {
+
+}
+
+// Called when the visibility of the border and connecting maps is toggled on or off.
+export function onBorderVisibilityToggled(visible) {
+
+}
diff --git a/src/ui/customscriptseditor.cpp b/src/ui/customscriptseditor.cpp
index 6daeb0eb..accc9c89 100644
--- a/src/ui/customscriptseditor.cpp
+++ b/src/ui/customscriptseditor.cpp
@@ -23,10 +23,11 @@ CustomScriptsEditor::CustomScriptsEditor(QWidget *parent) :
for (int i = 0; i < paths.length(); i++)
this->displayScript(paths.at(i), enabled.at(i));
- this->importDir = userConfig.getProjectDir();
+ this->fileDialogDir = userConfig.getProjectDir();
- connect(ui->button_AddNewScript, &QAbstractButton::clicked, this, &CustomScriptsEditor::addNewScript);
- connect(ui->button_ReloadScripts, &QAbstractButton::clicked, this, &CustomScriptsEditor::reloadScripts);
+ connect(ui->button_CreateNewScript, &QAbstractButton::clicked, this, &CustomScriptsEditor::createNewScript);
+ connect(ui->button_LoadScript, &QAbstractButton::clicked, this, &CustomScriptsEditor::loadScript);
+ connect(ui->button_RefreshScripts, &QAbstractButton::clicked, this, &CustomScriptsEditor::refreshScripts);
connect(ui->buttonBox, &QDialogButtonBox::clicked, this, &CustomScriptsEditor::dialogButtonClicked);
this->initShortcuts();
@@ -48,13 +49,17 @@ void CustomScriptsEditor::initShortcuts() {
shortcut_open->setObjectName("shortcut_open");
shortcut_open->setWhatsThis("Open Selected Scripts");
- auto *shortcut_addNew = new Shortcut(QKeySequence(), this, SLOT(addNewScript()));
- shortcut_addNew->setObjectName("shortcut_addNew");
- shortcut_addNew->setWhatsThis("Add New Script...");
+ auto *shortcut_createNew = new Shortcut(QKeySequence(), this, SLOT(createNewScript()));
+ shortcut_createNew->setObjectName("shortcut_createNew");
+ shortcut_createNew->setWhatsThis("Create New Script...");
- auto *shortcut_reload = new Shortcut(QKeySequence(), this, SLOT(reloadScripts()));
- shortcut_reload->setObjectName("shortcut_reload");
- shortcut_reload->setWhatsThis("Reload Scripts");
+ auto *shortcut_load = new Shortcut(QKeySequence(), this, SLOT(loadScript()));
+ shortcut_load->setObjectName("shortcut_load");
+ shortcut_load->setWhatsThis("Load Script...");
+
+ auto *shortcut_refresh = new Shortcut(QKeySequence(), this, SLOT(refreshScripts()));
+ shortcut_refresh->setObjectName("shortcut_refresh");
+ shortcut_refresh->setWhatsThis("Refresh Scripts");
shortcutsConfig.load();
shortcutsConfig.setDefaultShortcuts(shortcutableObjects());
@@ -145,13 +150,49 @@ QString CustomScriptsEditor::chooseScript(QString dir) {
return QFileDialog::getOpenFileName(this, "Choose Custom Script File", dir, "JavaScript Files (*.js)");
}
-void CustomScriptsEditor::addNewScript() {
- QString filepath = this->chooseScript(this->importDir);
+void CustomScriptsEditor::createNewScript() {
+ QString filepath = QFileDialog::getSaveFileName(this, "Create New Script File", this->fileDialogDir + "/new_script.js", "JavaScript Files (*.js)");
if (filepath.isEmpty())
return;
- this->importDir = filepath;
+ this->fileDialogDir = filepath;
+
+ QFile scriptFile(filepath);
+ if (!scriptFile.open(QIODevice::WriteOnly)) {
+ logError(QString("Error: Could not open %1 for writing").arg(filepath));
+ QMessageBox messageBox(this);
+ messageBox.setText("Failed to create new script file!");
+ messageBox.setInformativeText(QString("Could not open \"%1\" for writing").arg(filepath));
+ messageBox.setIcon(QMessageBox::Warning);
+ messageBox.exec();
+ return;
+ }
+ ParseUtil parser;
+ scriptFile.write(parser.readTextFile(":/text/script_template.js").toUtf8());
+ scriptFile.close();
+ // TODO: When this dialog closes, focus returns to the editor window
+ this->displayNewScript(filepath);
+}
+
+void CustomScriptsEditor::loadScript() {
+ QString filepath = this->chooseScript(this->fileDialogDir);
+ if (filepath.isEmpty())
+ return;
+ this->fileDialogDir = filepath;
+ this->displayNewScript(filepath);
+}
+
+void CustomScriptsEditor::displayNewScript(QString filepath) {
if (filepath.startsWith(this->baseDir))
filepath.remove(0, this->baseDir.length());
+
+ // Verify new script path is not already in list
+ for (int i = 0; i < ui->list->count(); i++) {
+ if (filepath == this->getScriptFilepath(ui->list->item(i), false)) {
+ QMessageBox::information(this, "", QString("The script '%1' is already loaded").arg(filepath));
+ return;
+ }
+ }
+
this->displayScript(filepath, true);
this->markEdited();
}
@@ -192,7 +233,7 @@ void CustomScriptsEditor::openSelectedScripts() {
this->openScript(item);
}
-void CustomScriptsEditor::reloadScripts() {
+void CustomScriptsEditor::refreshScripts() {
if (this->hasUnsavedChanges) {
if (this->prompt("Scripts have been modified, save changes and reload the script engine?", QMessageBox::Yes) == QMessageBox::No)
return;
@@ -218,7 +259,7 @@ void CustomScriptsEditor::save() {
userConfig.setCustomScripts(paths, enabledStates);
this->hasUnsavedChanges = false;
- this->reloadScripts();
+ this->refreshScripts();
}
int CustomScriptsEditor::prompt(const QString &text, QMessageBox::StandardButton defaultButton) {