Skip to content

Commit 5408311

Browse files
Update docs to: docs: Add more about action scripts (#85)
1 parent 3ce19a9 commit 5408311

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

docs/action_scripts.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Action Scripts in Text Forge
2+
3+
Action Scripts are a way to separate and manage possible actions in the Text Forge, helping them keep dozens of
4+
different capabilities separately and safely and add new capabilities in the shortest time.
5+
6+
## Design goals
7+
8+
- Modularity: Each action script is an independent unit with a specified output and input, almost all Action Scripts are
9+
detachable from the editor without making an error in its performance
10+
- Development: Developers can design new capabilities through simple Action Script APIs
11+
- Accessibility: Action Scripts are easily accessible through code, command palette, and shortcuts
12+
13+
## Structure
14+
15+
An action script must extend `ActionScript` or `MultiActionScript` class or another class that extends one of these
16+
classes. Each action script should be stored in `action_scripts/` directory and have snake_case file name of an option
17+
english name in menus. For example if we have a `Save As...` option in `File` menu, editor will load `action_scripts/save_as.gd`
18+
as its action script (will remove dots) and otherwise this action script will not be loaded.
19+
20+
Also, action scripts can have a shortcut in `shortcuts/` directory with same name. This file should be a `Shortcut`
21+
resource (`.tres` file) with a `InputEventKey` as shortcut. Loading shortcut will be done by `ActionScript` class.
22+
23+
All loaded action script will be children nodes of `Scripts` node in core, you can use `Global.get_scripts_node()` to
24+
access to it.
25+
26+
Each action script should override base class `Virtual` functions, you can see them in docstrings. There is a list of
27+
virtual functions of regular `ActionScript`:
28+
- `_initialize()`: For action script initializing
29+
- `_run_action()`: Main part of action script
30+
31+
## Features
32+
33+
### Access to menu option
34+
35+
Each action script have an item in menus, this item is accessible with `menu` and `index` property of action script.
36+
For checkbox and radio-checkbox options, editor will handle checking state itself.
37+
38+
### Automated shortcut loading
39+
40+
`ActionScript` and `MultiActionScript` class will load shortcuts from `shortcuts/` directory and call required functions
41+
in shortcut pressing.
42+
43+
### Define commands
44+
45+
An action script will define itself as a command for command palette, so command palettes can list action scripts for
46+
user and run them based on selections. If an action script has shortcut, it will add that shortcut in commands database
47+
so users can see shortcuts in command palette.
48+
49+
### Automated enabling / disabling
50+
51+
`requires_file` and `requires_saved_file` properties can be used to make smarter action scripts, must action scripts
52+
just available when there is an opened file, so `requires_file == true` can disable them other times.
53+
54+
## Create new action scripts
55+
56+
To add new action scripts (or edit existing one) see [here](create_action_scripts.md).

docs/create_action_scripts.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Create action scripts
2+
3+
!!! Note
4+
Reading [Action Scripts Guide](action_scripts.md) is recommended before creating actions scripts.
5+
6+
## Regular action scripts
7+
8+
There is some different types of action scripts based on their base classes, `ActionScript` class designed for regular
9+
action scripts and can handle a lot of tasks.
10+
11+
To add new action script you should make sure there is an item in menus for that action script, you can use `data/main_ui.ini`
12+
to add new options (or menus), to understand about structure of this file you can see [here](https://text-forge.github.io/docs/data_driven_ui/#menus).
13+
After select your option create a script in `action_scripts/` directory with same name (but in snake_case), it this case
14+
we will create **Copy Path** action script that will copy file path of opened file in clipboard, so we will create
15+
`action_scripts/copy_path.gd` and use this script as start point:
16+
17+
```gdscript
18+
extends ActionScript
19+
```
20+
21+
Just that! Run project and see option is enabled. But there is no action for now, so we have 3 steps to complete our
22+
action script:
23+
24+
### Add initializing
25+
26+
We needn't any special initializing for this action script, all we need is disabling it when there is no opened file,
27+
there is ready-made feature for this:
28+
29+
```gdscript
30+
extends ActionScript
31+
32+
func _initialize() -> void:
33+
requires_file = true
34+
requires_saved_file = true
35+
```
36+
37+
With this function `ActionScript` class will disable option when there isn't saved file, it means when you create a new
38+
file this option will keep disabled until that file saved, because before that there is no file path. Otherwise, (for
39+
example when user uses Open) option will be enabled.
40+
41+
### Add main action
42+
43+
We have initialized action script, but there is no action, so let's add it. To do this we use `_run_action()` function,
44+
this function will be called when user clicks in option in menus, presses shortcut (we will add shortcut later) or uses
45+
command palette. So we can complete our action script with this function:
46+
47+
```gdscript
48+
extends ActionScript
49+
50+
func _initialize() -> void:
51+
requires_file = true
52+
requires_saved_file = true
53+
54+
func _run_action() -> void:
55+
DisplayServer.clipboard_set(Global.get_file_path())
56+
```
57+
58+
Almost done! We have completed action script, and you can try it now in witch way you want.
59+
60+
### Add shortcut
61+
62+
There is an easy way for adding shortcuts to action scripts, so let's see how we can do it. To have shortcut we need a
63+
`Shortcut` resource stored in `shortcuts/` directory, so go to Godot's FileSystem dock and use RMB on `res://shortcuts/`,
64+
then click on Create New > Resource... and create a `Shortcut` resource and save it as `copy_path.tres`. Shortcut will
65+
be opened in Inspector, click on `Events` and add new element, then create a new `InputEventKey` in that element. Use
66+
`Configure` button and set your shortcut, then run project and try it. You can open command palette (Ctrl+P) and type `copy path`
67+
to see shortcut.

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ nav:
3333
- Contributing Guide: contributing.md
3434
- Contribution Types: contribution_types.md
3535
- Open In Godot (Build): build.md
36+
- Create Action Scripts: create_action_scripts.md
3637

3738
theme:
3839
name: readthedocs

0 commit comments

Comments
 (0)