forked from gaea-godot/gaea
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcreate_node_tree.gd
More file actions
196 lines (153 loc) · 5.65 KB
/
create_node_tree.gd
File metadata and controls
196 lines (153 loc) · 5.65 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
@tool
extends Tree
signal node_selected_for_creation(resource: GaeaNodeResource)
signal special_node_selected_for_creation(id: StringName)
const NODES_FOLDER_PATH: String = "res://addons/gaea/graph/graph_nodes/root/"
@export var description_label: RichTextLabel
var tree_dictionary: Dictionary
func _ready() -> void:
if is_part_of_edited_scene():
return
populate()
func _unhandled_key_input(event: InputEvent) -> void:
if event is InputEventKey and event.is_pressed():
if event.is_command_or_control_pressed() and event.keycode == KEY_TAB:
_select_next_visible(
get_selected() if get_selected() != null else get_root(),
1 if not event.is_shift_pressed() else -1
)
func populate() -> void:
clear()
var root: TreeItem = create_item()
hide_root = true
tree_dictionary = _populate_dict_with_files(NODES_FOLDER_PATH, {})
tree_dictionary["Special"] = {
"Frame": &"frame"
}
if not GaeaProjectSettings.get_custom_nodes_path().is_empty():
tree_dictionary = _populate_dict_with_files(GaeaProjectSettings.get_custom_nodes_path(), tree_dictionary)
_populate_from_dictionary(tree_dictionary, root)
root.set_collapsed_recursive(true)
root.set_collapsed(false)
func _populate_from_dictionary(dictionary: Dictionary, parent_item: TreeItem) -> void:
parent_item.set_selectable(0, false)
for key: String in dictionary:
var tree_item: TreeItem = create_item(parent_item)
tree_item.set_text(0, key)
if dictionary.get(key) is Dictionary:
_populate_from_dictionary(dictionary.get(key), tree_item)
else:
var value: Variant = dictionary.get(key)
tree_item.set_metadata(0, value)
if value is GaeaNodeResource:
tree_item.set_text(0, value.get_tree_name())
tree_item.set_icon(0, value.get_icon())
tree_item.set_icon_max_width(0, 16)
func _populate_dict_with_files(folder_path: String, dict: Dictionary) -> Dictionary:
folder_path += ("/" if not folder_path.ends_with("/") else "")
var dir := DirAccess.open(folder_path)
if dir == null:
push_error(error_string(DirAccess.get_open_error()))
return {}
dir.list_dir_begin()
var file_name := dir.get_next()
var idx: int = 0
while file_name != "":
if not dir.current_is_dir() and not file_name.ends_with(".gd"):
file_name = dir.get_next()
continue
idx += 1
var tree_name: String = file_name.get_basename().capitalize()
var file_path = folder_path + file_name
if dir.current_is_dir():
_populate_dict_with_files(file_path + "/", dict.get_or_add(tree_name, {}))
if file_name.ends_with(".gd"):
var script := load(file_path)
if script is GDScript:
var is_valid_node_resource := false
var base_script: GDScript = script
while is_instance_valid(base_script):
base_script = base_script.get_base_script()
if base_script == GaeaNodeResource:
is_valid_node_resource = true
break
if is_valid_node_resource:
var resource: GaeaNodeResource = script.new()
if resource.is_available():
var sub_idx: int = 0
for item in resource.get_tree_items():
sub_idx += 1
dict.get_or_add(item.get_tree_name() + str(idx) + str(sub_idx), item)
file_name = dir.get_next()
dict.sort()
return dict
func _on_item_activated() -> void:
var item: TreeItem = get_selected()
if not is_instance_valid(item):
return
if item.get_metadata(0) is GaeaNodeResource:
node_selected_for_creation.emit(item.get_metadata(0))
elif item.get_metadata(0) is StringName:
special_node_selected_for_creation.emit(item.get_metadata(0))
func _on_create_button_pressed() -> void:
_on_item_activated()
func _on_item_selected() -> void:
var item: TreeItem = get_selected()
if item.get_metadata(0) is GaeaNodeResource:
description_label.set_text(GaeaNodeResource.get_formatted_text(item.get_metadata(0).get_description()))
elif item.get_metadata(0) is StringName:
match item.get_metadata(0):
&"frame": description_label.set_text("A rectangular area for better organziation.")
func _on_nothing_selected() -> void:
description_label.set_text("")
func _on_search_bar_text_changed(new_text: String) -> void:
if new_text.is_empty():
get_root().set_collapsed_recursive(true)
get_root().collapsed = false
deselect_all()
else:
get_root().set_collapsed_recursive(false)
var item: TreeItem = get_root()
var first_item_found: TreeItem = null
while item.get_next_in_tree() != null:
item = item.get_next_in_tree()
if new_text.is_empty():
item.visible = true
else:
var item_matched = new_text.is_subsequence_ofn(item.get_text(0))
if item_matched and item.is_selectable(0):
if first_item_found == null:
first_item_found = item
item.visible = true
_show_parents_recursive(item)
else:
item.visible = false
if first_item_found:
scroll_to_item(first_item_found, true)
first_item_found.select(0)
ensure_cursor_is_visible()
else:
scroll_to_item(get_root(), true)
deselect_all()
func _select_next_visible(from: TreeItem, direction: int = 1) -> void:
if not is_instance_valid(from):
return
var item: TreeItem = (from.get_next_visible(true) if direction == 1 else from.get_prev_visible(true))
if not is_instance_valid(item):
return
while not item.is_selectable(0):
item.set_collapsed_recursive(false)
item = (item.get_next_visible(true) if direction == 1 else item.get_prev_visible(true))
_show_parents_recursive(item)
scroll_to_item(item)
item.select(0)
grab_focus()
func _show_parents_recursive(item: TreeItem) -> void:
var parent_item: TreeItem = item.get_parent()
while parent_item != null:
parent_item.visible = true
parent_item.set_collapsed_recursive(false)
parent_item = parent_item.get_parent()
func _on_search_bar_text_submitted(_new_text: String) -> void:
if get_selected() != null:
_on_item_activated()