forked from gaea-godot/gaea
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathnode.gd
More file actions
389 lines (294 loc) · 13 KB
/
node.gd
File metadata and controls
389 lines (294 loc) · 13 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
@tool
class_name GaeaGraphNode
extends GraphNode
## The in-editor representation of a [GaeaNodeResource] to be used in the Gaea bottom panel.
const _PreviewTexture = preload("res://addons/gaea/graph/components/preview_texture.gd")
## Emitted when connections to this node are updated.
signal connections_updated
## Emitted when this node is removed from the graph.
signal removed
signal remove_invalid_connections_requested
## The [GaeaNodeResource] this acts as an editor of.
@export var resource: GaeaNodeResource
## The currently associated generator.
var generator: GaeaGenerator
## List of connections that goes to this node from other nodes.
## Used by the generator during runtime. This list is updated
## from [method update_connections] method.
var connections: Array[Dictionary]
# Holds a cache of the generated titlebar styleboxes for each [enum GaeaValue.Type].
# Updated if the type's color changes.
static var _titlebar_styleboxes: Dictionary[GaeaValue.Type, Dictionary]
var _preview: _PreviewTexture
var _preview_container: VBoxContainer
var _finished_loading: bool = false: set = set_finished_loading, get = has_finished_loading
var _finished_rebuilding: bool = true: get = has_finished_rebuilding
var _editors: Dictionary[StringName, GaeaGraphNodeArgumentEditor]
var _enum_editors: Array[OptionButton]
var _last_category: GaeaArgumentCategory
func _ready() -> void:
_on_added()
if is_instance_valid(resource):
set_tooltip_text("tooltip")
if Engine.get_version_info().hex >= 0x040500 and not resource is GaeaNodeReroute:
var script = resource.get_script()
if is_instance_valid(script):
var documentation_button := Button.new()
var editor_interface = Engine.get_singleton("EditorInterface")
documentation_button.icon = editor_interface.get_editor_theme().get_icon(&"HelpSearch", &"EditorIcons")
documentation_button.flat = true
get_titlebar_hbox().add_child(documentation_button)
documentation_button.pressed.connect(_open_node_documentation)
tree_exiting.connect(documentation_button.pressed.disconnect.bind(_open_node_documentation))
connections_updated.connect(_update_arguments_visibility)
removed.connect(_on_removed)
## Initializes the node with a preview if needed, a salt value and instantiates all the
## [GaeaGraphNodeArgumentEditor] and [GaeaGraphNodeOutput] nodes.
func _on_added() -> void:
if not is_instance_valid(resource) or is_part_of_edited_scene():
return
resource.node = self
resource.argument_list_changed.connect(_rebuild, CONNECT_DEFERRED)
for enum_idx in resource.get_enums_count():
var option_button: OptionButton = OptionButton.new()
for option in resource.get_enum_options(enum_idx).values():
option_button.add_item(resource.get_enum_option_display_name(enum_idx, option), option)
option_button.set_item_icon(
option_button.get_item_index(option),
resource.get_enum_option_icon(enum_idx, option)
)
option_button.select(option_button.get_item_index(resource.get_enum_selection(enum_idx)))
add_child(option_button)
option_button.item_selected.connect(_on_enum_value_changed.bind(enum_idx, option_button))
_enum_editors.append(option_button)
await _rebuild()
title = resource.get_title()
if resource.salt == 0:
resource.salt = randi()
generator.data.set_node_data_value(resource.id, &"salt", resource.salt)
func _rebuild() -> void:
if not has_finished_rebuilding():
return
_finished_rebuilding = false
var selected_preview: StringName = &""
if is_instance_valid(_preview):
selected_preview = _preview.selected_output
var saved_data := {}
if _finished_loading:
saved_data = generator.data.get_node_data(resource.id)
resource.enum_selections = saved_data.get("enums", [])
_editors.clear()
_preview_container = null
_preview = null
for child in get_children():
if child is OptionButton:
continue
child.queue_free()
await child.tree_exited
clear_all_slots()
_add_slots.call_deferred()
if _finished_loading:
load_save_data.call_deferred(saved_data)
_add_preview_container.call_deferred()
remove_invalid_connections_requested.emit.call_deferred()
_update_arguments_visibility.call_deferred()
if selected_preview.length() > 0:
_open_preview.call_deferred(selected_preview)
_finished_rebuilding = true
_set_titlebar()
_last_category = null
func _add_slots() -> void:
for argument in resource.get_arguments_list():
_editors.set(argument, _add_argument_editor(argument))
var preview_button_group: ButtonGroup = ButtonGroup.new()
preview_button_group.allow_unpress = true
for output in resource.get_output_ports_list():
var slot := _add_output_slot(output)
if is_instance_valid(slot):
slot.get_toggle_preview_button().button_group = preview_button_group
func _add_argument_editor(for_arg: StringName) -> GaeaGraphNodeArgumentEditor:
var type: GaeaValue.Type = resource.get_argument_type(for_arg)
var scene: PackedScene = GaeaValue.get_editor_for_type(type)
var node: GaeaGraphNodeArgumentEditor = scene.instantiate()
add_child(node)
if type == GaeaValue.Type.CATEGORY:
_last_category = node
elif is_instance_valid(_last_category):
_last_category.arguments.append(node)
node.initialize(
self,
resource.get_argument_type(for_arg),
resource.get_argument_display_name(for_arg),
resource.arguments.get(for_arg, resource.get_argument_default_value(for_arg)),
resource.get_argument_hint(for_arg)
)
if resource.has_input_slot(for_arg):
node.add_input_slot()
node.argument_value_changed.connect(_on_argument_value_changed.bind(node, for_arg))
return node
func _add_output_slot(for_output: StringName) -> GaeaGraphNodeOutput:
if resource.get_overridden_output_port_idx(for_output) >= 0:
var new_idx: int = resource.get_overridden_output_port_idx(for_output)
if get_child_count() > new_idx:
var type: GaeaValue.Type = resource.get_output_port_type(for_output)
set_slot_enabled_right(new_idx, true)
set_slot_type_right(new_idx, type)
set_slot_color_right(new_idx, GaeaValue.get_color(type))
set_slot_custom_icon_right(new_idx, GaeaValue.get_slot_icon(type))
return null
var node: GaeaGraphNodeOutput = preload("uid://cqpby5jyv71l0").instantiate()
add_child(node)
node.initialize(
self,
resource.get_output_port_type(for_output),
resource.get_output_port_display_name(for_output)
)
if GaeaValue.has_preview(resource.get_output_port_type(for_output)):
node.get_toggle_preview_button().show()
if not is_instance_valid(_preview):
_preview_container = VBoxContainer.new()
_preview = _PreviewTexture.new()
_preview.node = self
generator.generation_finished.connect(_preview.update.unbind(1))
node.get_toggle_preview_button().toggled.connect(_preview.toggle.bind(for_output).unbind(1))
return node
func _get_output_slot(for_output: StringName) -> GaeaGraphNodeOutput:
var overridden_idx: int = resource.get_overridden_output_port_idx(for_output)
if overridden_idx >= 0:
return get_child(overridden_idx)
var idx = resource.get_enums_count() + resource.get_arguments_list().size()
for output in resource.get_output_ports_list():
if output == for_output:
return get_child(idx)
if resource.get_overridden_output_port_idx(output) == -1:
idx += 1
return null
func _add_preview_container() -> void:
if is_instance_valid(_preview_container):
add_child(_preview_container)
_preview_container.add_child(_preview)
_preview_container.hide()
_preview.update()
func _open_preview(for_output: StringName) -> void:
var slot = _get_output_slot(for_output)
if is_instance_valid(slot):
slot.get_toggle_preview_button().set_pressed(true)
func _set_titlebar() -> void:
var type: GaeaValue.Type = resource.get_type()
var titlebar: StyleBoxFlat
var titlebar_selected: StyleBoxFlat
if type != GaeaValue.Type.NULL:
remove_theme_stylebox_override("titlebar")
remove_theme_stylebox_override("titlebar_selected")
if not _titlebar_styleboxes.has(type) or _titlebar_styleboxes.get(type).get("for_color", Color.TRANSPARENT) != resource.get_title_color():
titlebar = get_theme_stylebox("titlebar", "GraphNode").duplicate()
titlebar_selected = get_theme_stylebox("titlebar_selected", "GraphNode").duplicate()
titlebar.bg_color = titlebar.bg_color.blend(Color(resource.get_title_color(), 0.3))
titlebar_selected.bg_color = titlebar.bg_color
_titlebar_styleboxes.set(type, {"titlebar": titlebar, "selected": titlebar_selected, "for_color": resource.get_title_color()})
else:
titlebar = _titlebar_styleboxes.get(type).get("titlebar")
titlebar_selected = _titlebar_styleboxes.get(type).get("selected")
add_theme_stylebox_override("titlebar", titlebar)
add_theme_stylebox_override("titlebar_selected", titlebar_selected)
## Returns the current value set in the [GaeaGraphNodeArgumentEditor] for the argument of [param arg_name].
func get_arg_value(arg_name: StringName) -> Variant:
var editor: GaeaGraphNodeArgumentEditor = _editors.get(arg_name, null)
if is_instance_valid(editor):
return editor.get_arg_value()
return null
## Sets the [GaeaGraphNodeArgumentEditor] associated to the argument of [param arg_name] to [param value].
func _set_arg_value(arg_name: StringName, value: Variant) -> void:
var editor: GaeaGraphNodeArgumentEditor = _editors.get(arg_name, null)
if is_instance_valid(editor):
editor.set_arg_value(value)
func _on_argument_value_changed(value: Variant, _node: GaeaGraphNodeArgumentEditor, arg_name: String) -> void:
if _finished_loading:
resource.set_argument_value(arg_name, value)
generator.data.set_node_argument(resource.id, arg_name, value)
if is_instance_valid(_preview):
_preview.update()
func _on_enum_value_changed(option_idx: int, enum_idx: int, button: OptionButton) -> void:
var value := button.get_item_id(option_idx)
resource.set_enum_value(enum_idx, value)
generator.data.set_node_enum(resource.id, enum_idx, value)
if is_instance_valid(_preview):
_preview.update()
# Makes argument editors invisible if there's a wire connected to their input slot.
func _update_arguments_visibility() -> void:
var input_idx: int = -1
for child in get_children():
if not is_slot_enabled_left(child.get_index()):
continue
input_idx += 1
if child is not GaeaGraphNodeArgumentEditor:
continue
if is_zero_approx(child.size.y):
continue
child.set_editor_visible(not connections.any(_is_connected_to.bind(input_idx)))
auto_shrink()
func _on_removed() -> void:
pass
## Emit [signal connections_updated].
func notify_connections_updated() -> void:
connections_updated.emit()
func _is_connected_to(connection: Dictionary, idx: int) -> bool:
return connection.to_port == idx and connection.to_node == name
## Resizes the node to its minimum possible size, and updates wire display accordingly.
func auto_shrink() -> void:
size = get_combined_minimum_size()
# This is used to force the wire to redraw at the correct location
await get_tree().process_frame
for i: int in get_child_count():
slot_updated.emit.call_deferred(i)
## Loads data with the same format as seen in [method get_save_data].
func load_save_data(saved_data: Dictionary) -> void:
if saved_data.has(&"position"):
position_offset = saved_data.position
if saved_data.has(&"enums"):
for enum_idx: int in saved_data.get(&"enums").size():
_enum_editors[enum_idx].select(
_enum_editors[enum_idx].get_item_index(saved_data.get("enums")[enum_idx])
)
if saved_data.has(&"arguments"):
var arguments = saved_data.get(&"arguments")
for argument: StringName in resource.get_arguments_list():
var editor: GaeaGraphNodeArgumentEditor = _editors.get(argument)
if not is_instance_valid(editor):
break
if not arguments.has(argument):
arguments.set(argument, resource.get_argument_default_value(argument))
if arguments.get(argument) != null:
editor.set_arg_value(arguments.get(argument))
_finished_loading = true
func _get_tooltip(_at_position: Vector2) -> String:
return resource.get_description()
func _make_custom_tooltip(for_text: String) -> Object:
if for_text.length() == 0:
return null
var rich_text_label: RichTextLabel = RichTextLabel.new()
rich_text_label.autowrap_mode = TextServer.AUTOWRAP_WORD
rich_text_label.bbcode_enabled = true
rich_text_label.text = GaeaNodeResource.get_formatted_text(for_text)
rich_text_label.text += "\n[right][b]ID: %s[/b][/right]" % resource.id
rich_text_label.fit_content = true
rich_text_label.custom_minimum_size.x = 256.0
return rich_text_label
func _open_node_documentation():
var script = resource.get_script()
if not is_instance_valid(script):
return
var resource_class_name = (script as GDScript).get_global_name()
var editor_interface = Engine.get_singleton("EditorInterface")
var script_editor = editor_interface.get_script_editor()
script_editor.goto_help("class_name:%s" % resource_class_name)
## Sets whether or not this node has finished its loading process.
func set_finished_loading(value: bool) -> void:
_finished_loading = value
## Returns [code]true[/code] if this node has finished its loading process.
func has_finished_loading() -> bool:
return _finished_loading
func has_finished_rebuilding() -> bool:
return _finished_rebuilding
func _on_dragged(_from: Vector2, to: Vector2) -> void:
generator.data.set_node_position(resource.id, to)