Move script utility functions to own object

This commit is contained in:
GriffinR 2022-09-05 11:53:30 -04:00
parent 31b22bfcc1
commit bd5446a40b
8 changed files with 164 additions and 143 deletions

54
include/api_util.h Normal file
View file

@ -0,0 +1,54 @@
#pragma once
#ifndef APIUTIL_H
#define APIUTIL_H
#include "mainwindow.h"
class ScriptUtility : public QObject
{
Q_OBJECT
public:
ScriptUtility(MainWindow *mainWindow);
void clearActions();
QString getActionFunctionName(QString actionName);
Q_INVOKABLE void registerAction(QString functionName, QString actionName, QString shortcut = "");
Q_INVOKABLE void setTimeout(QJSValue callback, int milliseconds);
Q_INVOKABLE void log(QString message);
Q_INVOKABLE void warn(QString message);
Q_INVOKABLE void error(QString message);
Q_INVOKABLE void showMessage(QString text, QString informativeText = "", QString detailedText = "");
Q_INVOKABLE void showWarning(QString text, QString informativeText = "", QString detailedText = "");
Q_INVOKABLE void showError(QString text, QString informativeText = "", QString detailedText = "");
Q_INVOKABLE bool showQuestion(QString text, QString informativeText = "", QString detailedText = "");
Q_INVOKABLE QJSValue getInputText(QString title, QString label, QString defaultValue = "");
Q_INVOKABLE QJSValue getInputNumber(QString title, QString label, double defaultValue = 0, double min = INT_MIN, double max = INT_MAX, int decimals = 0, double step = 1);
Q_INVOKABLE QJSValue getInputItem(QString title, QString label, QStringList items, int defaultValue = 0, bool editable = false);
Q_INVOKABLE int getMainTab();
Q_INVOKABLE void setMainTab(int index);
Q_INVOKABLE int getMapViewTab();
Q_INVOKABLE void setMapViewTab(int index);
Q_INVOKABLE void setGridVisibility(bool visible);
Q_INVOKABLE bool getGridVisibility();
Q_INVOKABLE void setBorderVisibility(bool visible);
Q_INVOKABLE bool getBorderVisibility();
Q_INVOKABLE void setSmartPathsEnabled(bool visible);
Q_INVOKABLE bool getSmartPathsEnabled();
Q_INVOKABLE QList<QString> getCustomScripts();
Q_INVOKABLE QList<int> getMetatileLayerOrder();
Q_INVOKABLE void setMetatileLayerOrder(QList<int> order);
Q_INVOKABLE QList<float> getMetatileLayerOpacity();
Q_INVOKABLE void setMetatileLayerOpacity(QList<float> order);
Q_INVOKABLE bool isPrimaryTileset(QString tilesetName);
Q_INVOKABLE bool isSecondaryTileset(QString tilesetName);
private:
void callTimeoutFunction(QJSValue callback);
void runMessageBox(QString text, QString informativeText, QString detailedText, QMessageBox::Icon icon);
MainWindow *window;
QList<QAction *> registeredActions;
QMap<QString, QString> actionMap;
};
#endif // APIUTIL_H

View file

@ -42,8 +42,6 @@ public:
MainWindow(const MainWindow &) = delete; MainWindow(const MainWindow &) = delete;
MainWindow & operator = (const MainWindow &) = delete; MainWindow & operator = (const MainWindow &) = delete;
MapView *getMapView();
// Scripting API // Scripting API
Q_INVOKABLE QJSValue getBlock(int x, int y); Q_INVOKABLE QJSValue getBlock(int x, int y);
void tryRedrawMapArea(bool forceRedraw); void tryRedrawMapArea(bool forceRedraw);
@ -104,36 +102,10 @@ public:
Q_INVOKABLE int getNumSecondaryTilesetMetatiles(); Q_INVOKABLE int getNumSecondaryTilesetMetatiles();
Q_INVOKABLE int getNumPrimaryTilesetTiles(); Q_INVOKABLE int getNumPrimaryTilesetTiles();
Q_INVOKABLE int getNumSecondaryTilesetTiles(); Q_INVOKABLE int getNumSecondaryTilesetTiles();
Q_INVOKABLE bool isPrimaryTileset(QString tilesetName);
Q_INVOKABLE bool isSecondaryTileset(QString tilesetName);
Q_INVOKABLE QString getPrimaryTileset(); Q_INVOKABLE QString getPrimaryTileset();
Q_INVOKABLE QString getSecondaryTileset(); Q_INVOKABLE QString getSecondaryTileset();
Q_INVOKABLE void setPrimaryTileset(QString tileset); Q_INVOKABLE void setPrimaryTileset(QString tileset);
Q_INVOKABLE void setSecondaryTileset(QString tileset); Q_INVOKABLE void setSecondaryTileset(QString tileset);
Q_INVOKABLE void setGridVisibility(bool visible);
Q_INVOKABLE bool getGridVisibility();
Q_INVOKABLE void setBorderVisibility(bool visible);
Q_INVOKABLE bool getBorderVisibility();
Q_INVOKABLE void setSmartPathsEnabled(bool visible);
Q_INVOKABLE bool getSmartPathsEnabled();
Q_INVOKABLE void registerAction(QString functionName, QString actionName, QString shortcut = "");
Q_INVOKABLE void setTimeout(QJSValue callback, int milliseconds);
void invokeCallback(QJSValue callback);
Q_INVOKABLE void log(QString message);
Q_INVOKABLE void warn(QString message);
Q_INVOKABLE void error(QString message);
void runMessageBox(QString text, QString informativeText, QString detailedText, QMessageBox::Icon icon);
Q_INVOKABLE void showMessage(QString text, QString informativeText = "", QString detailedText = "");
Q_INVOKABLE void showWarning(QString text, QString informativeText = "", QString detailedText = "");
Q_INVOKABLE void showError(QString text, QString informativeText = "", QString detailedText = "");
Q_INVOKABLE bool showQuestion(QString text, QString informativeText = "", QString detailedText = "");
Q_INVOKABLE QJSValue getInputText(QString title, QString label, QString defaultValue = "");
Q_INVOKABLE QJSValue getInputNumber(QString title, QString label, double defaultValue = 0, double min = INT_MIN, double max = INT_MAX, int decimals = 0, double step = 1);
Q_INVOKABLE QJSValue getInputItem(QString title, QString label, QStringList items, int defaultValue = 0, bool editable = false);
Q_INVOKABLE QList<int> getMetatileLayerOrder();
Q_INVOKABLE void setMetatileLayerOrder(QList<int> order);
Q_INVOKABLE QList<float> getMetatileLayerOpacity();
Q_INVOKABLE void setMetatileLayerOpacity(QList<float> order);
void saveMetatilesByMetatileId(int metatileId); void saveMetatilesByMetatileId(int metatileId);
void saveMetatileAttributesByMetatileId(int metatileId); void saveMetatileAttributesByMetatileId(int metatileId);
Metatile * getMetatile(int metatileId); Metatile * getMetatile(int metatileId);
@ -157,11 +129,6 @@ public:
Q_INVOKABLE void setMetatileTiles(int metatileId, QJSValue tilesObj, int tileStart = 0, int tileEnd = -1, bool forceRedraw = true); Q_INVOKABLE void setMetatileTiles(int metatileId, QJSValue tilesObj, int tileStart = 0, int tileEnd = -1, bool forceRedraw = true);
Q_INVOKABLE void setMetatileTiles(int metatileId, int tileId, bool xflip, bool yflip, int palette, int tileStart = 0, int tileEnd = -1, bool forceRedraw = true); Q_INVOKABLE void setMetatileTiles(int metatileId, int tileId, bool xflip, bool yflip, int palette, int tileStart = 0, int tileEnd = -1, bool forceRedraw = true);
Q_INVOKABLE QJSValue getTilePixels(int tileId); Q_INVOKABLE QJSValue getTilePixels(int tileId);
Q_INVOKABLE QList<QString> getCustomScripts();
Q_INVOKABLE int getMainTab();
Q_INVOKABLE void setMainTab(int index);
Q_INVOKABLE int getMapViewTab();
Q_INVOKABLE void setMapViewTab(int index);
bool gameStringToBool(QString s); bool gameStringToBool(QString s);
Q_INVOKABLE QString getSong(); Q_INVOKABLE QString getSong();
Q_INVOKABLE void setSong(QString song); Q_INVOKABLE void setSong(QString song);
@ -186,6 +153,10 @@ public:
Q_INVOKABLE int getFloorNumber(); Q_INVOKABLE int getFloorNumber();
Q_INVOKABLE void setFloorNumber(int floorNumber); Q_INVOKABLE void setFloorNumber(int floorNumber);
public slots:
void on_mainTabBar_tabBarClicked(int index);
void on_mapViewTab_tabBarClicked(int index);
private slots: private slots:
void on_action_Open_Project_triggered(); void on_action_Open_Project_triggered();
void on_action_Reload_Project_triggered(); void on_action_Reload_Project_triggered();
@ -232,9 +203,6 @@ private slots:
void on_actionOpen_Recent_Project_On_Launch_triggered(bool checked); void on_actionOpen_Recent_Project_On_Launch_triggered(bool checked);
void on_actionEdit_Shortcuts_triggered(); void on_actionEdit_Shortcuts_triggered();
void on_mainTabBar_tabBarClicked(int index);
void on_mapViewTab_tabBarClicked(int index);
void on_actionZoom_In_triggered(); void on_actionZoom_In_triggered();
void on_actionZoom_Out_triggered(); void on_actionZoom_Out_triggered();
void on_actionBetter_Cursors_triggered(); void on_actionBetter_Cursors_triggered();
@ -318,8 +286,11 @@ private slots:
void on_actionEdit_Preferences_triggered(); void on_actionEdit_Preferences_triggered();
void togglePreferenceSpecificUi(); void togglePreferenceSpecificUi();
private: public:
Ui::MainWindow *ui; Ui::MainWindow *ui;
Editor *editor = nullptr;
private:
QLabel *label_MapRulerStatus = nullptr; QLabel *label_MapRulerStatus = nullptr;
QPointer<TilesetEditor> tilesetEditor = nullptr; QPointer<TilesetEditor> tilesetEditor = nullptr;
QPointer<RegionMapEditor> regionMapEditor = nullptr; QPointer<RegionMapEditor> regionMapEditor = nullptr;
@ -331,7 +302,6 @@ private:
QStandardItemModel *mapListModel; QStandardItemModel *mapListModel;
QList<QStandardItem*> *mapGroupItemsList; QList<QStandardItem*> *mapGroupItemsList;
QMap<QString, QModelIndex> mapListIndexes; QMap<QString, QModelIndex> mapListIndexes;
Editor *editor = nullptr;
QIcon* mapIcon; QIcon* mapIcon;
QIcon* mapEditedIcon; QIcon* mapEditedIcon;
QIcon* mapOpenedIcon; QIcon* mapOpenedIcon;
@ -355,7 +325,6 @@ private:
DraggablePixmapItem *selectedBG; DraggablePixmapItem *selectedBG;
DraggablePixmapItem *selectedHealspot; DraggablePixmapItem *selectedHealspot;
QList<QAction *> registeredActions;
QVector<QToolButton *> openScriptButtons; QVector<QToolButton *> openScriptButtons;
bool isProgrammaticEventTabChange; bool isProgrammaticEventTabChange;

View file

@ -4,6 +4,7 @@
#include "mainwindow.h" #include "mainwindow.h"
#include "block.h" #include "block.h"
#include "api_util.h"
#include <QStringList> #include <QStringList>
#include <QJSEngine> #include <QJSEngine>
@ -63,8 +64,8 @@ private:
QJSEngine *engine; QJSEngine *engine;
QStringList filepaths; QStringList filepaths;
QList<QJSValue> modules; QList<QJSValue> modules;
QMap<QString, QString> registeredActions;
QMap<QString, const QImage*> imageCache; QMap<QString, const QImage*> imageCache;
ScriptUtility *scriptUtility;
void loadModules(QStringList moduleFiles); void loadModules(QStringList moduleFiles);
void invokeCallback(CallbackType type, QJSValueList args); void invokeCallback(CallbackType type, QJSValueList args);

View file

@ -169,6 +169,7 @@ HEADERS += include/core/block.h \
include/ui/preferenceeditor.h \ include/ui/preferenceeditor.h \
include/ui/regionmappropertiesdialog.h \ include/ui/regionmappropertiesdialog.h \
include/ui/colorpicker.h \ include/ui/colorpicker.h \
include/api_util.h \
include/config.h \ include/config.h \
include/editor.h \ include/editor.h \
include/mainwindow.h \ include/mainwindow.h \

View file

@ -540,6 +540,7 @@ bool MainWindow::openProject(QString dir) {
if (success) { if (success) {
showWindowTitle(); showWindowTitle();
this->statusBar()->showMessage(QString("Opened project %1").arg(nativeDir)); this->statusBar()->showMessage(QString("Opened project %1").arg(nativeDir));
Scripting::cb_ProjectOpened(dir);
} else { } else {
this->statusBar()->showMessage(QString("Failed to open project %1").arg(nativeDir)); this->statusBar()->showMessage(QString("Failed to open project %1").arg(nativeDir));
QMessageBox msgBox(this); QMessageBox msgBox(this);
@ -550,14 +551,6 @@ bool MainWindow::openProject(QString dir) {
msgBox.critical(nullptr, "Error Opening Project", errorMsg); msgBox.critical(nullptr, "Error Opening Project", errorMsg);
} }
if (success) {
for (auto action : this->registeredActions) {
this->ui->menuTools->removeAction(action);
}
Scripting::cb_ProjectOpened(dir);
}
projectOpenFailure = !success; projectOpenFailure = !success;
return success; return success;
} }
@ -3238,7 +3231,3 @@ void MainWindow::closeEvent(QCloseEvent *event) {
QMainWindow::closeEvent(event); QMainWindow::closeEvent(event);
} }
MapView *MainWindow::getMapView() {
return this->ui->graphicsView_Map;
}

View file

@ -3,16 +3,20 @@
#include "scripting.h" #include "scripting.h"
#include "config.h" #include "config.h"
void MainWindow::registerAction(QString functionName, QString actionName, QString shortcut) { ScriptUtility::ScriptUtility(MainWindow *mainWindow) {
if (!this->ui || !this->ui->menuTools) this->window = mainWindow;
}
void ScriptUtility::registerAction(QString functionName, QString actionName, QString shortcut) {
if (!window || !window->ui || !window->ui->menuTools)
return; return;
Scripting::registerAction(functionName, actionName); this->actionMap.insert(actionName, functionName);
if (Scripting::numRegisteredActions() == 1) { if (this->actionMap.size() == 1) {
QAction *section = this->ui->menuTools->addSection("Custom Actions"); QAction *section = window->ui->menuTools->addSection("Custom Actions");
this->registeredActions.append(section); this->registeredActions.append(section);
} }
QAction *action = this->ui->menuTools->addAction(actionName, [actionName](){ QAction *action = window->ui->menuTools->addAction(actionName, [actionName](){
Scripting::invokeAction(actionName); Scripting::invokeAction(actionName);
}); });
if (!shortcut.isEmpty()) { if (!shortcut.isEmpty()) {
@ -21,37 +25,47 @@ void MainWindow::registerAction(QString functionName, QString actionName, QStrin
this->registeredActions.append(action); this->registeredActions.append(action);
} }
void MainWindow::setTimeout(QJSValue callback, int milliseconds) { void ScriptUtility::clearActions() {
for (auto action : this->registeredActions) {
window->ui->menuTools->removeAction(action);
}
}
QString ScriptUtility::getActionFunctionName(QString actionName) {
return this->actionMap.value(actionName);
}
void ScriptUtility::setTimeout(QJSValue callback, int milliseconds) {
if (!callback.isCallable() || milliseconds < 0) if (!callback.isCallable() || milliseconds < 0)
return; return;
QTimer *timer = new QTimer(0); QTimer *timer = new QTimer(0);
connect(timer, &QTimer::timeout, [=](){ connect(timer, &QTimer::timeout, [=](){
this->invokeCallback(callback); this->callTimeoutFunction(callback);
}); });
connect(timer, &QTimer::timeout, timer, &QTimer::deleteLater); connect(timer, &QTimer::timeout, timer, &QTimer::deleteLater);
timer->setSingleShot(true); timer->setSingleShot(true);
timer->start(milliseconds); timer->start(milliseconds);
} }
void MainWindow::invokeCallback(QJSValue callback) { void ScriptUtility::callTimeoutFunction(QJSValue callback) {
Scripting::tryErrorJS(callback.call()); Scripting::tryErrorJS(callback.call());
} }
void MainWindow::log(QString message) { void ScriptUtility::log(QString message) {
logInfo(message); logInfo(message);
} }
void MainWindow::warn(QString message) { void ScriptUtility::warn(QString message) {
logWarn(message); logWarn(message);
} }
void MainWindow::error(QString message) { void ScriptUtility::error(QString message) {
logError(message); logError(message);
} }
void MainWindow::runMessageBox(QString text, QString informativeText, QString detailedText, QMessageBox::Icon icon) { void ScriptUtility::runMessageBox(QString text, QString informativeText, QString detailedText, QMessageBox::Icon icon) {
QMessageBox messageBox(this); QMessageBox messageBox(window);
messageBox.setText(text); messageBox.setText(text);
messageBox.setInformativeText(informativeText); messageBox.setInformativeText(informativeText);
messageBox.setDetailedText(detailedText); messageBox.setDetailedText(detailedText);
@ -59,20 +73,20 @@ void MainWindow::runMessageBox(QString text, QString informativeText, QString de
messageBox.exec(); messageBox.exec();
} }
void MainWindow::showMessage(QString text, QString informativeText, QString detailedText) { void ScriptUtility::showMessage(QString text, QString informativeText, QString detailedText) {
this->runMessageBox(text, informativeText, detailedText, QMessageBox::Information); this->runMessageBox(text, informativeText, detailedText, QMessageBox::Information);
} }
void MainWindow::showWarning(QString text, QString informativeText, QString detailedText) { void ScriptUtility::showWarning(QString text, QString informativeText, QString detailedText) {
this->runMessageBox(text, informativeText, detailedText, QMessageBox::Warning); this->runMessageBox(text, informativeText, detailedText, QMessageBox::Warning);
} }
void MainWindow::showError(QString text, QString informativeText, QString detailedText) { void ScriptUtility::showError(QString text, QString informativeText, QString detailedText) {
this->runMessageBox(text, informativeText, detailedText, QMessageBox::Critical); this->runMessageBox(text, informativeText, detailedText, QMessageBox::Critical);
} }
bool MainWindow::showQuestion(QString text, QString informativeText, QString detailedText) { bool ScriptUtility::showQuestion(QString text, QString informativeText, QString detailedText) {
QMessageBox messageBox(this); QMessageBox messageBox(window);
messageBox.setText(text); messageBox.setText(text);
messageBox.setInformativeText(informativeText); messageBox.setInformativeText(informativeText);
messageBox.setDetailedText(detailedText); messageBox.setDetailedText(detailedText);
@ -81,83 +95,87 @@ bool MainWindow::showQuestion(QString text, QString informativeText, QString det
return messageBox.exec() == QMessageBox::Yes; return messageBox.exec() == QMessageBox::Yes;
} }
QJSValue MainWindow::getInputText(QString title, QString label, QString defaultValue) { QJSValue ScriptUtility::getInputText(QString title, QString label, QString defaultValue) {
bool ok; bool ok;
QString input = QInputDialog::getText(this, title, label, QLineEdit::Normal, defaultValue, &ok); QString input = QInputDialog::getText(window, title, label, QLineEdit::Normal, defaultValue, &ok);
return Scripting::dialogInput(input, ok); return Scripting::dialogInput(input, ok);
} }
QJSValue MainWindow::getInputNumber(QString title, QString label, double defaultValue, double min, double max, int decimals, double step) { QJSValue ScriptUtility::getInputNumber(QString title, QString label, double defaultValue, double min, double max, int decimals, double step) {
bool ok; bool ok;
double input = QInputDialog::getDouble(this, title, label, defaultValue, min, max, decimals, &ok, Qt::WindowFlags(), step); double input = QInputDialog::getDouble(window, title, label, defaultValue, min, max, decimals, &ok, Qt::WindowFlags(), step);
return Scripting::dialogInput(input, ok); return Scripting::dialogInput(input, ok);
} }
QJSValue MainWindow::getInputItem(QString title, QString label, QStringList items, int defaultValue, bool editable) { QJSValue ScriptUtility::getInputItem(QString title, QString label, QStringList items, int defaultValue, bool editable) {
bool ok; bool ok;
QString input = QInputDialog::getItem(this, title, label, items, defaultValue, editable, &ok); QString input = QInputDialog::getItem(window, title, label, items, defaultValue, editable, &ok);
return Scripting::dialogInput(input, ok); return Scripting::dialogInput(input, ok);
} }
int MainWindow::getMainTab() { int ScriptUtility::getMainTab() {
if (!this->ui || !this->ui->mainTabBar) if (!window || !window->ui || !window->ui->mainTabBar)
return -1; return -1;
return this->ui->mainTabBar->currentIndex(); return window->ui->mainTabBar->currentIndex();
} }
void MainWindow::setMainTab(int index) { void ScriptUtility::setMainTab(int index) {
if (!this->ui || !this->ui->mainTabBar || index < 0 || index >= this->ui->mainTabBar->count()) if (!window || !window->ui || !window->ui->mainTabBar || index < 0 || index >= window->ui->mainTabBar->count())
return; return;
// Can't select Wild Encounters tab if it's disabled // Can't select Wild Encounters tab if it's disabled
if (index == 4 && !projectConfig.getEncounterJsonActive()) if (index == 4 && !projectConfig.getEncounterJsonActive())
return; return;
this->on_mainTabBar_tabBarClicked(index); window->on_mainTabBar_tabBarClicked(index);
} }
int MainWindow::getMapViewTab() { int ScriptUtility::getMapViewTab() {
if (!this->ui || !this->ui->mapViewTab) if (!window || !window->ui || !window->ui->mapViewTab)
return -1; return -1;
return this->ui->mapViewTab->currentIndex(); return window->ui->mapViewTab->currentIndex();
} }
void MainWindow::setMapViewTab(int index) { void ScriptUtility::setMapViewTab(int index) {
if (this->getMainTab() != 0 || !this->ui->mapViewTab || index < 0 || index >= this->ui->mapViewTab->count()) if (this->getMainTab() != 0 || !window->ui->mapViewTab || index < 0 || index >= window->ui->mapViewTab->count())
return; return;
this->on_mapViewTab_tabBarClicked(index); window->on_mapViewTab_tabBarClicked(index);
} }
void MainWindow::setGridVisibility(bool visible) { void ScriptUtility::setGridVisibility(bool visible) {
this->ui->checkBox_ToggleGrid->setChecked(visible); window->ui->checkBox_ToggleGrid->setChecked(visible);
} }
bool MainWindow::getGridVisibility() { bool ScriptUtility::getGridVisibility() {
return this->ui->checkBox_ToggleGrid->isChecked(); return window->ui->checkBox_ToggleGrid->isChecked();
} }
void MainWindow::setBorderVisibility(bool visible) { void ScriptUtility::setBorderVisibility(bool visible) {
this->editor->toggleBorderVisibility(visible, false); window->editor->toggleBorderVisibility(visible, false);
} }
bool MainWindow::getBorderVisibility() { bool ScriptUtility::getBorderVisibility() {
return this->ui->checkBox_ToggleBorder->isChecked(); return window->ui->checkBox_ToggleBorder->isChecked();
} }
void MainWindow::setSmartPathsEnabled(bool visible) { void ScriptUtility::setSmartPathsEnabled(bool visible) {
this->ui->checkBox_smartPaths->setChecked(visible); window->ui->checkBox_smartPaths->setChecked(visible);
} }
bool MainWindow::getSmartPathsEnabled() { bool ScriptUtility::getSmartPathsEnabled() {
return this->ui->checkBox_smartPaths->isChecked(); return window->ui->checkBox_smartPaths->isChecked();
} }
QList<int> MainWindow::getMetatileLayerOrder() { QList<QString> ScriptUtility::getCustomScripts() {
if (!this->editor || !this->editor->map) return projectConfig.getCustomScripts();
}
QList<int> ScriptUtility::getMetatileLayerOrder() {
if (!window || !window->editor || !window->editor->map)
return QList<int>(); return QList<int>();
return this->editor->map->metatileLayerOrder; return window->editor->map->metatileLayerOrder;
} }
void MainWindow::setMetatileLayerOrder(QList<int> order) { void ScriptUtility::setMetatileLayerOrder(QList<int> order) {
if (!this->editor || !this->editor->map) if (!window || !window->editor || !window->editor->map)
return; return;
const int numLayers = 3; const int numLayers = 3;
@ -176,36 +194,31 @@ void MainWindow::setMetatileLayerOrder(QList<int> order) {
} }
if (invalid) return; if (invalid) return;
this->editor->map->metatileLayerOrder = order; window->editor->map->metatileLayerOrder = order;
this->refreshAfterPalettePreviewChange(); window->refreshAfterPalettePreviewChange();
} }
QList<float> MainWindow::getMetatileLayerOpacity() { QList<float> ScriptUtility::getMetatileLayerOpacity() {
if (!this->editor || !this->editor->map) if (!window || !window->editor || !window->editor->map)
return QList<float>(); return QList<float>();
return this->editor->map->metatileLayerOpacity; return window->editor->map->metatileLayerOpacity;
} }
void MainWindow::setMetatileLayerOpacity(QList<float> order) { void ScriptUtility::setMetatileLayerOpacity(QList<float> order) {
if (!this->editor || !this->editor->map) if (!window || !window->editor || !window->editor->map)
return; return;
this->editor->map->metatileLayerOpacity = order; window->editor->map->metatileLayerOpacity = order;
this->refreshAfterPalettePreviewChange(); window->refreshAfterPalettePreviewChange();
} }
bool MainWindow::isPrimaryTileset(QString tilesetName) { bool ScriptUtility::isPrimaryTileset(QString tilesetName) {
if (!this->editor || !this->editor->project) if (!window || !window->editor || !window->editor->project)
return false; return false;
return this->editor->project->tilesetLabels["primary"].contains(tilesetName); return window->editor->project->tilesetLabels["primary"].contains(tilesetName);
} }
bool MainWindow::isSecondaryTileset(QString tilesetName) { bool ScriptUtility::isSecondaryTileset(QString tilesetName) {
if (!this->editor || !this->editor->project) if (!window || !window->editor || !window->editor->project)
return false; return false;
return this->editor->project->tilesetLabels["secondary"].contains(tilesetName); return window->editor->project->tilesetLabels["secondary"].contains(tilesetName);
} }
QList<QString> MainWindow::getCustomScripts() {
return projectConfig.getCustomScripts();
}

View file

@ -25,6 +25,7 @@ Scripting *instance = nullptr;
void Scripting::init(MainWindow *mainWindow) { void Scripting::init(MainWindow *mainWindow) {
if (instance) { if (instance) {
instance->engine->setInterrupted(true); instance->engine->setInterrupted(true);
instance->scriptUtility->clearActions();
qDeleteAll(instance->imageCache); qDeleteAll(instance->imageCache);
delete instance; delete instance;
} }
@ -38,6 +39,7 @@ Scripting::Scripting(MainWindow *mainWindow) {
this->filepaths.append(script); this->filepaths.append(script);
} }
this->loadModules(this->filepaths); this->loadModules(this->filepaths);
this->scriptUtility = new ScriptUtility(mainWindow);
} }
void Scripting::loadModules(QStringList moduleFiles) { void Scripting::loadModules(QStringList moduleFiles) {
@ -58,7 +60,8 @@ void Scripting::populateGlobalObject(MainWindow *mainWindow) {
if (!instance || !instance->engine) return; if (!instance || !instance->engine) return;
instance->engine->globalObject().setProperty("map", instance->engine->newQObject(mainWindow)); instance->engine->globalObject().setProperty("map", instance->engine->newQObject(mainWindow));
instance->engine->globalObject().setProperty("overlay", instance->engine->newQObject(mainWindow->getMapView())); instance->engine->globalObject().setProperty("overlay", instance->engine->newQObject(mainWindow->ui->graphicsView_Map));
instance->engine->globalObject().setProperty("utility", instance->engine->newQObject(instance->scriptUtility));
QJSValue constants = instance->engine->newObject(); QJSValue constants = instance->engine->newObject();
@ -91,6 +94,7 @@ void Scripting::populateGlobalObject(MainWindow *mainWindow) {
// Prevent changes to the object properties of the global object // Prevent changes to the object properties of the global object
instance->engine->evaluate("Object.freeze(map);"); instance->engine->evaluate("Object.freeze(map);");
instance->engine->evaluate("Object.freeze(overlay);"); instance->engine->evaluate("Object.freeze(overlay);");
instance->engine->evaluate("Object.freeze(utility);");
instance->engine->evaluate("Object.freeze(constants.version);"); instance->engine->evaluate("Object.freeze(constants.version);");
instance->engine->evaluate("Object.freeze(constants);"); instance->engine->evaluate("Object.freeze(constants);");
} }
@ -125,22 +129,12 @@ void Scripting::invokeCallback(CallbackType type, QJSValueList args) {
} }
} }
void Scripting::registerAction(QString functionName, QString actionName) {
if (!instance) return;
instance->registeredActions.insert(actionName, functionName);
}
int Scripting::numRegisteredActions() {
if (!instance) return 0;
return instance->registeredActions.size();
}
void Scripting::invokeAction(QString actionName) { void Scripting::invokeAction(QString actionName) {
if (!instance) return; if (!instance || !instance->scriptUtility) return;
if (!instance->registeredActions.contains(actionName)) return; QString functionName = instance->scriptUtility->getActionFunctionName(actionName);
if (functionName.isEmpty()) return;
bool foundFunction = false; bool foundFunction = false;
QString functionName = instance->registeredActions.value(actionName);
for (QJSValue module : instance->modules) { for (QJSValue module : instance->modules) {
QJSValue callbackFunction = module.property(functionName); QJSValue callbackFunction = module.property(functionName);
if (callbackFunction.isUndefined() || !callbackFunction.isCallable()) if (callbackFunction.isUndefined() || !callbackFunction.isCallable())

View file

@ -42,9 +42,9 @@ void MapView::drawForeground(QPainter *painter, const QRectF&) {
} }
void MapView::clearOverlayMap() { void MapView::clearOverlayMap() {
foreach (Overlay * layer, this->overlayMap) { foreach (Overlay * overlay, this->overlayMap) {
layer->clearItems(); overlay->clearItems();
delete layer; delete overlay;
} }
this->overlayMap.clear(); this->overlayMap.clear();
} }