mirror of
https://github.com/huderlem/porymap.git
synced 2026-08-14 12:57:06 -05:00
Add ability to register custom actions
This commit is contained in:
@@ -348,6 +348,10 @@ bool MainWindow::openProject(QString dir) {
|
||||
|
||||
}
|
||||
|
||||
if (success) {
|
||||
Scripting::cb_ProjectOpened(dir);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
@@ -2844,3 +2848,16 @@ void MainWindow::setSecondaryTilesetPalettePreview(int paletteIndex, QList<QList
|
||||
return;
|
||||
this->setTilesetPalettePreview(this->editor->map->layout->tileset_secondary, paletteIndex, colors);
|
||||
}
|
||||
|
||||
void MainWindow::registerAction(QString functionName, QString actionName) {
|
||||
if (!this->ui || !this->ui->menuTools)
|
||||
return;
|
||||
|
||||
Scripting::registerAction(functionName, actionName);
|
||||
if (Scripting::numRegisteredActions() == 1) {
|
||||
this->ui->menuTools->addSeparator();
|
||||
}
|
||||
this->ui->menuTools->addAction(actionName, [actionName](){
|
||||
Scripting::invokeAction(actionName);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "log.h"
|
||||
|
||||
QMap<CallbackType, QString> callbackFunctions = {
|
||||
{OnProjectOpened, "on_project_opened"},
|
||||
{OnBlockChanged, "on_block_changed"},
|
||||
{OnMapOpened, "on_map_opened"},
|
||||
};
|
||||
@@ -54,6 +55,44 @@ 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) {
|
||||
if (!instance) return;
|
||||
if (!instance->registeredActions.contains(actionName)) return;
|
||||
|
||||
QString functionName = instance->registeredActions.value(actionName);
|
||||
for (QJSValue module : instance->modules) {
|
||||
QJSValue callbackFunction = module.property(functionName);
|
||||
if (callbackFunction.isError()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
QJSValue result = callbackFunction.call(QJSValueList());
|
||||
if (result.isError()) {
|
||||
logError(QString("Module %1 encountered an error when calling '%2'").arg(module.toString()).arg(functionName));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Scripting::cb_ProjectOpened(QString projectPath) {
|
||||
if (!instance) return;
|
||||
|
||||
QJSValueList args {
|
||||
projectPath,
|
||||
};
|
||||
instance->invokeCallback(OnProjectOpened, args);
|
||||
}
|
||||
|
||||
void Scripting::cb_MetatileChanged(int x, int y, Block prevBlock, Block newBlock) {
|
||||
if (!instance) return;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user