-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbbcode_edit_main.gd
More file actions
225 lines (174 loc) · 7.37 KB
/
bbcode_edit_main.gd
File metadata and controls
225 lines (174 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
@tool
extends EditorPlugin
const BBCodeEdit: GDScript = preload("res://addons/bbcode_edit/doc_comment.editor/bbcode_edit.gd")
const Completions = preload("res://addons/bbcode_edit/doc_comment.editor/completions_db/completions.gd")
const Scraper = preload("res://addons/bbcode_edit/doc_comment.editor/editor_interface_scraper.gd")
const ADDON_NAME = "BBCode Editor"
const ACTION_OPEN_DOC = &"bbcode_edit/editor/open_current_file_documentation"
const ACTION_SETTINGS: Array[StringName] = [
"input/" + ACTION_OPEN_DOC,
"input/" + BBCodeEdit.ACTION_TOGGLE_BOLD,
"input/" + BBCodeEdit.ACTION_TOGGLE_ITALIC,
"input/" + BBCodeEdit.ACTION_TOGGLE_UNDERLINE,
"input/" + BBCodeEdit.ACTION_TOGGLE_STRIKE,
]
func _enable_plugin() -> void:
print("Enabling ", ADDON_NAME)
add_keybinds()
_on_editor_startup.call_deferred()
print("Enabled ", ADDON_NAME)
func _disable_plugin() -> void:
print("Disabling ", ADDON_NAME)
for editor in EditorInterface.get_script_editor().get_open_script_editors():
editor.get_base_editor().set_script(null)
remove_keybinds()
for setting in ACTION_SETTINGS:
InputMap.erase_action(setting.substr(6))
ProjectSettings.set_setting(Completions.DONT_ASK_TO_FETCH_SETTING_PATH, null)
print("Disabled ", ADDON_NAME)
func _enter_tree() -> void:
if not EditorInterface.has_meta(&"bbcode_edit_saved_once"):
EditorInterface.set_meta(&"bbcode_edit_saved_once", PackedStringArray())
_on_editor_startup.call_deferred()
add_tool_menu_item("BBCodeEdit: Fetch Builtin Classes", Completions.fetch_builtin_classes)
func _exit_tree() -> void:
remove_tool_menu_item("BBCodeEdit: Fetch Builtin Classes")
var started_up: bool = false
func _on_editor_startup() -> void:
if started_up:
return
started_up = true
# TODO check if InputMap.load_from_project_settings() is better
for setting in ACTION_SETTINGS:
if !ProjectSettings.has_setting(setting):
continue
var action_dict: Dictionary = ProjectSettings.get_setting(setting)
var action_name: StringName = setting.substr(6)
InputMap.add_action(action_name, action_dict["deadzone"])
for event in action_dict["events"]:
InputMap.action_add_event(action_name, event)
EditorInterface.get_script_editor().editor_script_changed.connect(check_current.unbind(1))
check_current.call_deferred()
func add_keybinds() -> void:
var toggle_bold := InputEventKey.new()
toggle_bold.alt_pressed = true
toggle_bold.keycode = 66
ProjectSettings.set_setting(
&"input/bbcode_edit/toggle_bold",
{
"deadzone": 0.5,
"events": [toggle_bold],
}
)
var toggle_italic := InputEventKey.new()
toggle_italic.alt_pressed = true
toggle_italic.keycode = 73
ProjectSettings.set_setting(
&"input/bbcode_edit/toggle_italic",
{
"deadzone": 0.5,
"events": [toggle_italic],
}
)
var toggle_underline := InputEventKey.new()
toggle_underline.alt_pressed = true
toggle_underline.keycode = 85
ProjectSettings.set_setting(
&"input/bbcode_edit/toggle_underline",
{
"deadzone": 0.5,
"events": [toggle_underline],
}
)
var toggle_strike := InputEventKey.new()
toggle_strike.alt_pressed = true
toggle_strike.keycode = _get_striketrough_keycode()
ProjectSettings.set_setting(
&"input/bbcode_edit/toggle_strike",
{
"deadzone": 0.5,
"events": [toggle_strike],
}
)
if Engine.is_editor_hint():
add_editor_keybinds()
ProjectSettings.save()
print_rich("[color=orange]If you don't see the keybinds in the InputMap, please reload the Project.[/color]")
func _get_striketrough_keycode() -> int:
var editor_shortcuts: Variant = EditorInterface.get_editor_settings().get(&"shortcuts")
if editor_shortcuts == null:
return KEY_C
for shortcut: Dictionary in editor_shortcuts:
if shortcut["name"] == "bottom_panels/toggle_shader_editor_bottom_panel":
var input_events: Array = shortcut.get("shortcuts")
for input_event: InputEvent in input_events:
if input_event.keycode == KEY_S:
return KEY_C
return KEY_S
return KEY_C
func add_editor_keybinds() -> void:
var open_current_file_documentation := InputEventKey.new()
open_current_file_documentation.shift_pressed = true
open_current_file_documentation.keycode = 4194332
ProjectSettings.set_setting(
"input/" + ACTION_OPEN_DOC,
{
"deadzone": 0.5,
"events": [open_current_file_documentation],
}
)
# NB: Initial values DON'T work, see [url]https://github.com/godotengine/godot/issues/56598[/url]
#ProjectSettings.set_initial_value(&"input/bbcode_edit/editor/open_current_file_documentation", open_current_file_documentation.duplicate())
func remove_keybinds() -> void:
ProjectSettings.set_setting("input/" + BBCodeEdit.ACTION_TOGGLE_BOLD, null)
ProjectSettings.set_setting("input/" + BBCodeEdit.ACTION_TOGGLE_ITALIC, null)
ProjectSettings.set_setting("input/" + BBCodeEdit.ACTION_TOGGLE_UNDERLINE, null)
ProjectSettings.set_setting("input/" + BBCodeEdit.ACTION_TOGGLE_STRIKE, null)
# This calls ProjectSettings.save(), so please call it last
remove_editor_keybinds()
func remove_editor_keybinds() -> void:
ProjectSettings.set_setting("input/" + ACTION_OPEN_DOC, null)
ProjectSettings.save()
func check_current() -> void:
var current_editor := EditorInterface.get_script_editor().get_current_editor()
if current_editor == null:
return
check_bbcode_pretendant(current_editor.get_base_editor())
func check_bbcode_pretendant(pretendant: Control) -> void:
if pretendant is CodeEdit and not pretendant.has_meta(&"BBCode_utilities"):
add_bbcode_handling(pretendant)
func add_bbcode_handling(code_edit: CodeEdit) -> void:
# TODO MAYBE implement automatic script inheritence if script is already overriden by another addon
code_edit.set_meta(&"never_changed", true)
code_edit.set_script(BBCodeEdit)
## [b]WARING:[/b] not fully implemented for non-current script
func open_doc(script: Script, code_edit: CodeEdit = null) -> void:
var class_name_: String = script.get_global_name()
if class_name_ == "":
class_name_ = '"' + script.resource_path.trim_prefix("res://") + '"'
var bbcode_edit_saved_once: PackedStringArray = EditorInterface.get_meta(&"bbcode_edit_saved_once", PackedStringArray())
if code_edit and class_name_ not in bbcode_edit_saved_once:
bbcode_edit_saved_once.append(class_name_)
print_rich("[color=orange]The script never changed since startup: brute-forcing documentation generation. (See [url=https://github.com/godotengine/godot/pull/95821]godot#95821[/url])[/color]")
code_edit.text = code_edit.text
EditorInterface.save_all_scenes()
elif Scraper.is_current_script_unsaved():
# TODO ↑ Fix this for non-current script
print_rich("[color=orange]Saving to make godot generate documentation.[/color]")
EditorInterface.save_all_scenes()
elif Scraper.is_current_script_unsaved():
print_rich("[color=orange]Saving to make godot generate documentation.[/color]")
EditorInterface.save_all_scenes()
EditorInterface.get_script_editor().get_current_editor().go_to_help.emit.call_deferred("class_name:"+class_name_)
func _unhandled_input(event: InputEvent) -> void:
if (
InputMap.has_action(ACTION_OPEN_DOC)
and InputMap.event_is_action(event, ACTION_OPEN_DOC, true)
):
# TODO find a workaround for the appearance delay of (*) to check unsaved status.
var current_editor := EditorInterface.get_script_editor().get_current_editor()
if current_editor == null:
return
var code_edit := current_editor.get_base_editor()
if code_edit is CodeEdit:
open_doc(EditorInterface.get_script_editor().get_current_script(), code_edit)