Allow script actions with the same name

This commit is contained in:
GriffinR 2023-02-09 13:55:25 -05:00
parent 880dabade3
commit 314e6fff53
4 changed files with 20 additions and 11 deletions

View file

@ -35,7 +35,7 @@ public:
static QJSEngine *getEngine();
static void registerAction(QString functionName, QString actionName);
static int numRegisteredActions();
static void invokeAction(QString actionName);
static void invokeAction(int actionIndex);
static void cb_ProjectOpened(QString projectPath);
static void cb_ProjectClosed(QString projectPath);
static void cb_MetatileChanged(int x, int y, Block prevBlock, Block newBlock);

View file

@ -11,7 +11,7 @@ class ScriptUtility : public QObject
public:
ScriptUtility(MainWindow *mainWindow);
void clearActions();
QString getActionFunctionName(QString actionName);
QString getActionFunctionName(int actionIndex);
Q_INVOKABLE void registerAction(QString functionName, QString actionName, QString shortcut = "");
Q_INVOKABLE void setTimeout(QJSValue callback, int milliseconds);
Q_INVOKABLE void log(QString message);
@ -58,7 +58,7 @@ private:
MainWindow *window;
QList<QAction *> registeredActions;
QMap<QString, QString> actionMap;
QHash<int, QString> actionMap;
};
#endif // SCRIPTUTILITY_H

View file

@ -11,17 +11,26 @@ void ScriptUtility::registerAction(QString functionName, QString actionName, QSt
if (!window || !window->ui || !window->ui->menuTools)
return;
this->actionMap.insert(actionName, functionName);
if (this->actionMap.size() == 1) {
if (functionName.isEmpty() || actionName.isEmpty()) {
logError("Failed to register script action. 'functionName' and 'actionName' must be non-empty.");
return;
}
if (this->registeredActions.size() == 0) {
QAction *section = window->ui->menuTools->addSection("Custom Actions");
this->registeredActions.append(section);
}
QAction *action = window->ui->menuTools->addAction(actionName, [actionName](){
Scripting::invokeAction(actionName);
const int actionIndex = this->registeredActions.size();
QAction *action = window->ui->menuTools->addAction(actionName, [actionIndex](){
Scripting::invokeAction(actionIndex);
});
if (!shortcut.isEmpty()) {
action->setShortcut(QKeySequence(shortcut));
}
this->actionMap.insert(actionIndex, functionName);
this->registeredActions.append(action);
}
@ -31,8 +40,8 @@ void ScriptUtility::clearActions() {
}
}
QString ScriptUtility::getActionFunctionName(QString actionName) {
return this->actionMap.value(actionName);
QString ScriptUtility::getActionFunctionName(int actionIndex) {
return this->actionMap.value(actionIndex);
}
void ScriptUtility::setTimeout(QJSValue callback, int milliseconds) {

View file

@ -142,9 +142,9 @@ void Scripting::invokeCallback(CallbackType type, QJSValueList args) {
}
}
void Scripting::invokeAction(QString actionName) {
void Scripting::invokeAction(int actionIndex) {
if (!instance || !instance->scriptUtility) return;
QString functionName = instance->scriptUtility->getActionFunctionName(actionName);
QString functionName = instance->scriptUtility->getActionFunctionName(actionIndex);
if (functionName.isEmpty()) return;
bool foundFunction = false;