diff --git a/CHANGELOG.md b/CHANGELOG.md index ed364d63..7961c104 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project somewhat adheres to [Semantic Versioning](https://semver.org/sp The **"Breaking Changes"** listed below are changes that have been made in the decompilation projects (e.g. pokeemerald), which porymap requires in order to work properly. It also includes changes to the scripting API that may change the behavior of existing porymap scripts. If porymap is used with a project or API script that is not up-to-date with the breaking changes, then porymap will likely break or behave improperly. ## [Unreleased] +### Added +- Add `registerToggleAction` to the scripting API + ### Changed - Change encounter tab copy and paste behavior. - A warning will appear if a custom script fails to load or an action fails to run. diff --git a/docsrc/manual/scripting-capabilities.rst b/docsrc/manual/scripting-capabilities.rst index 903ab51c..3b397b72 100644 --- a/docsrc/manual/scripting-capabilities.rst +++ b/docsrc/manual/scripting-capabilities.rst @@ -1406,6 +1406,15 @@ All utility functions are callable via the global ``utility`` object. :param string actionName: name of the action that will be displayed in the ``Tools`` menu :param string shortcut: optional keyboard shortcut +.. js:function:: utility.registerToggleAction(functionName, actionName, shortcut = "", checked = false) + + Registers a JavaScript function to an action that can be manually triggered in Porymap's ``Tools`` menu. Optionally, a keyboard shortcut (e.g. ``"Ctrl+P"``) can also be specified, assuming it doesn't collide with any existing shortcuts used by Porymap. A check mark will be toggled next to the action name each time its activated. Whether the check mark is initially present can be set with ``checked``. The function specified by ``functionName`` must have the ``export`` keyword. + + :param string functionName: name of the JavaScript function + :param string actionName: name of the action that will be displayed in the ``Tools`` menu + :param string shortcut: optional keyboard shortcut + :param boolean checked: whether the action initially has a check mark. Defaults to ``false``. + .. js:function:: utility.setTimeout(func, delayMs) This behaves essentially the same as JavaScript's ``setTimeout()`` that is used in web browsers or NodeJS. The ``func`` argument is a JavaScript function (NOT the name of a function) which will be executed after a delay. This is useful for creating animations or refreshing the overlay at constant intervals. diff --git a/include/scripting.h b/include/scripting.h index 7a0b1a67..ce435236 100644 --- a/include/scripting.h +++ b/include/scripting.h @@ -33,8 +33,6 @@ public: static void init(MainWindow *mainWindow); static void populateGlobalObject(MainWindow *mainWindow); static QJSEngine *getEngine(); - static void registerAction(QString functionName, QString actionName); - static int numRegisteredActions(); static void invokeAction(int actionIndex); static void cb_ProjectOpened(QString projectPath); static void cb_ProjectClosed(QString projectPath); diff --git a/include/scriptutility.h b/include/scriptutility.h index 5b6882c6..639e71f6 100644 --- a/include/scriptutility.h +++ b/include/scriptutility.h @@ -12,7 +12,8 @@ public: ScriptUtility(MainWindow *mainWindow); void clearActions(); QString getActionFunctionName(int actionIndex); - Q_INVOKABLE void registerAction(QString functionName, QString actionName, QString shortcut = ""); + Q_INVOKABLE bool registerAction(QString functionName, QString actionName, QString shortcut = ""); + Q_INVOKABLE bool registerToggleAction(QString functionName, QString actionName, QString shortcut = "", bool checked = false); Q_INVOKABLE void setTimeout(QJSValue callback, int milliseconds); Q_INVOKABLE void log(QString message); Q_INVOKABLE void warn(QString message); diff --git a/src/scriptapi/apiutility.cpp b/src/scriptapi/apiutility.cpp index 0678a5e0..baeb2017 100644 --- a/src/scriptapi/apiutility.cpp +++ b/src/scriptapi/apiutility.cpp @@ -7,13 +7,13 @@ ScriptUtility::ScriptUtility(MainWindow *mainWindow) { this->window = mainWindow; } -void ScriptUtility::registerAction(QString functionName, QString actionName, QString shortcut) { +bool ScriptUtility::registerAction(QString functionName, QString actionName, QString shortcut) { if (!window || !window->ui || !window->ui->menuTools) - return; + return false; if (functionName.isEmpty() || actionName.isEmpty()) { logError("Failed to register script action. 'functionName' and 'actionName' must be non-empty."); - return; + return false; } if (this->registeredActions.size() == 0) { @@ -32,6 +32,16 @@ void ScriptUtility::registerAction(QString functionName, QString actionName, QSt this->actionMap.insert(actionIndex, functionName); this->registeredActions.append(action); + return true; +} + +bool ScriptUtility::registerToggleAction(QString functionName, QString actionName, QString shortcut, bool checked) { + if (!registerAction(functionName, actionName, shortcut)) + return false; + QAction *action = this->registeredActions.last(); + action->setCheckable(true); + action->setChecked(checked); + return true; } void ScriptUtility::clearActions() {