Add registerToggleAction

This commit is contained in:
GriffinR 2023-02-10 11:46:19 -05:00
parent bf0ec3395d
commit bc256053d0
5 changed files with 27 additions and 6 deletions

View file

@ -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.

View file

@ -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.

View file

@ -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);

View file

@ -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);

View file

@ -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() {