-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathblock_canvas.gd
406 lines (286 loc) · 11.7 KB
/
block_canvas.gd
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
@tool
extends MarginContainer
const ASTList = preload("res://addons/block_code/code_generation/ast_list.gd")
const BlockAST = preload("res://addons/block_code/code_generation/block_ast.gd")
const BlockCodePlugin = preload("res://addons/block_code/block_code_plugin.gd")
const BlockTreeUtil = preload("res://addons/block_code/ui/block_tree_util.gd")
const DragManager = preload("res://addons/block_code/drag_manager/drag_manager.gd")
const ScriptGenerator = preload("res://addons/block_code/code_generation/script_generator.gd")
const Util = preload("res://addons/block_code/ui/util.gd")
const EXTEND_MARGIN: float = 800
const BLOCK_AUTO_PLACE_MARGIN: Vector2 = Vector2(25, 8)
const DEFAULT_WINDOW_MARGIN: Vector2 = Vector2(25, 25)
const SNAP_GRID: Vector2 = Vector2(25, 25)
const ZOOM_FACTOR: float = 1.1
@onready var _context := BlockEditorContext.get_default()
@onready var _window: Control = %Window
@onready var _empty_box: BoxContainer = %EmptyBox
@onready var _selected_node_box: BoxContainer = %SelectedNodeBox
@onready var _selected_node_label: Label = %SelectedNodeBox/Label
@onready var _selected_node_label_format: String = _selected_node_label.text
@onready var _selected_node_with_block_code_box: BoxContainer = %SelectedNodeWithBlockCodeBox
@onready var _selected_node_with_block_code_label: Label = %SelectedNodeWithBlockCodeBox/Label
@onready var _selected_node_with_block_code_label_format: String = _selected_node_with_block_code_label.text
@onready var _add_block_code_button: Button = %AddBlockCodeButton
@onready var _open_scene_button: Button = %OpenSceneButton
@onready var _replace_block_code_button: Button = %ReplaceBlockCodeButton
@onready var _open_scene_icon = _open_scene_button.get_theme_icon("Load", "EditorIcons")
@onready var _mouse_override: Control = %MouseOverride
@onready var _zoom_button: Button = %ZoomButton
var _current_block_script: BlockScriptSerialization
var _current_ast_list: ASTList
var _panning := false
var zoom: float:
set(value):
_window.scale = Vector2(value, value)
_zoom_button.text = "%.1fx" % value
get:
return _window.scale.x
signal reconnect_block(block: Block)
signal add_block_code
signal open_scene
signal replace_block_code
func _ready():
_context.changed.connect(_on_context_changed)
if not _open_scene_button.icon and not Util.node_is_part_of_edited_scene(self):
_open_scene_button.icon = _open_scene_icon
func add_block(block: Block, position: Vector2 = Vector2.ZERO) -> void:
if block is EntryBlock:
block.position = canvas_to_window(position).snapped(SNAP_GRID)
else:
block.position = canvas_to_window(position)
_window.add_child(block)
func get_blocks() -> Array[Block]:
var blocks: Array[Block] = []
for child in _window.get_children():
var block = child as Block
if block:
blocks.append(block)
return blocks
func arrange_block(block: Block, nearby_block: Block) -> void:
add_block(block)
var rect = nearby_block.get_global_rect()
rect.position += (rect.size * Vector2.RIGHT) + BLOCK_AUTO_PLACE_MARGIN
block.global_position = rect.position
func set_child(n: Node):
n.owner = _window
for c in n.get_children():
set_child(c)
func _on_context_changed():
clear_canvas()
var edited_node: Node
if Engine.is_editor_hint():
edited_node = EditorInterface.get_inspector().get_edited_object() as Node
if _context.block_script != _current_block_script:
_window.position = Vector2(0, 0)
zoom = 1
_window.visible = false
_zoom_button.visible = false
_empty_box.visible = false
_selected_node_box.visible = false
_selected_node_with_block_code_box.visible = false
_add_block_code_button.disabled = true
_open_scene_button.disabled = true
_replace_block_code_button.disabled = true
if _context.block_script != null:
_load_block_script(_context.block_script)
_window.visible = true
_zoom_button.visible = true
if _context.block_script != _current_block_script:
reset_window_position()
elif edited_node == null:
_empty_box.visible = true
elif BlockCodePlugin.node_has_block_code(edited_node):
# If the selected node has a block code node, but BlockCodePlugin didn't
# provide it to block_script_selected, we assume the block code itself is not
# editable. In that case, provide options to either edit the node's
# scene file, or override the BlockCode node. This is mostly to avoid
# creating a situation where a node has multiple BlockCode nodes.
_selected_node_with_block_code_box.visible = true
_selected_node_with_block_code_label.text = _selected_node_with_block_code_label_format.format({"node": edited_node.name})
_open_scene_button.disabled = false if edited_node.scene_file_path else true
_replace_block_code_button.disabled = false
else:
_selected_node_box.visible = true
_selected_node_label.text = _selected_node_label_format.format({"node": edited_node.name})
_add_block_code_button.disabled = false
_current_block_script = _context.block_script
func _load_block_script(block_script: BlockScriptSerialization):
_current_ast_list = block_script.generate_ast_list()
reload_ui_from_ast_list()
func reload_ui_from_ast_list():
for ast_pair in _current_ast_list.array:
var root_block = ui_tree_from_ast_node(ast_pair.ast.root)
root_block.position = ast_pair.canvas_position
_window.add_child(root_block)
func ui_tree_from_ast_node(ast_node: BlockAST.ASTNode) -> Block:
var block: Block = _context.block_script.instantiate_block(ast_node.data)
# Args
var parameter_values: Dictionary
for arg_name in ast_node.arguments:
var argument = ast_node.arguments[arg_name]
if argument is BlockAST.ASTValueNode:
var value_block = ui_tree_from_ast_value_node(argument)
parameter_values[arg_name] = value_block
else: # Argument is not a node, but a user input value
parameter_values[arg_name] = argument
block.set_parameter_values_on_ready(parameter_values)
# Children
var current_block: Block = block
var i: int = 0
for c in ast_node.children:
var child_block: Block = ui_tree_from_ast_node(c)
if i == 0:
current_block.child_snap.add_child(child_block)
else:
current_block.bottom_snap.add_child(child_block)
current_block = child_block
i += 1
reconnect_block.emit(block)
return block
func ui_tree_from_ast_value_node(ast_value_node: BlockAST.ASTValueNode) -> Block:
var block: Block = _context.block_script.instantiate_block(ast_value_node.data)
# Args
var parameter_values: Dictionary
for arg_name in ast_value_node.arguments:
var argument = ast_value_node.arguments[arg_name]
if argument is BlockAST.ASTValueNode:
var value_block = ui_tree_from_ast_value_node(argument)
parameter_values[arg_name] = value_block
else: # Argument is not a node, but a user input value
parameter_values[arg_name] = argument
block.set_parameter_values_on_ready(parameter_values)
reconnect_block.emit(block)
return block
func clear_canvas():
for child in _window.get_children():
_window.remove_child(child)
child.queue_free()
func rebuild_ast_list():
_current_ast_list.clear()
for c in _window.get_children():
if c is StatementBlock:
var root: BlockAST.ASTNode = build_ast(c)
var ast: BlockAST = BlockAST.new()
ast.root = root
_current_ast_list.append(ast, c.position)
func build_ast(block: Block) -> BlockAST.ASTNode:
var ast_node := BlockAST.ASTNode.new()
ast_node.data = block.definition
var parameter_values := block.get_parameter_values()
for arg_name in parameter_values:
var arg_value = parameter_values[arg_name]
if arg_value is Block:
ast_node.arguments[arg_name] = build_value_ast(arg_value)
else:
ast_node.arguments[arg_name] = arg_value
var children: Array[BlockAST.ASTNode] = []
if block.child_snap:
var child: Block = block.child_snap.get_snapped_block()
while child != null:
var child_ast_node := build_ast(child)
child_ast_node.data = child.definition
children.append(child_ast_node)
if child.bottom_snap == null:
child = null
else:
child = child.bottom_snap.get_snapped_block()
ast_node.children = children
return ast_node
func build_value_ast(block: ParameterBlock) -> BlockAST.ASTValueNode:
var ast_node := BlockAST.ASTValueNode.new()
ast_node.data = block.definition
var parameter_values := block.get_parameter_values()
for arg_name in parameter_values:
var arg_value = parameter_values[arg_name]
if arg_value is Block:
ast_node.arguments[arg_name] = build_value_ast(arg_value)
else:
ast_node.arguments[arg_name] = arg_value
return ast_node
func rebuild_block_serialization_trees():
_context.block_script.update_from_ast_list(_current_ast_list)
func find_snaps(node: Node) -> Array[SnapPoint]:
var snaps: Array[SnapPoint]
if node.is_in_group("snap_point") and node is SnapPoint:
snaps.append(node)
else:
for c in node.get_children():
snaps.append_array(find_snaps(c))
return snaps
func set_scope(scope: String):
for block in _window.get_children():
var valid := false
if block is EntryBlock:
if scope == block.definition.code_template:
valid = true
else:
var tree_scope := BlockTreeUtil.get_tree_scope(block)
if tree_scope == "" or scope == tree_scope:
valid = true
if not valid:
block.modulate = Color(0.5, 0.5, 0.5, 1)
func release_scope():
for block in _window.get_children():
block.modulate = Color.WHITE
func _on_add_block_code_button_pressed():
_add_block_code_button.disabled = true
add_block_code.emit()
func _on_open_scene_button_pressed():
_open_scene_button.disabled = true
open_scene.emit()
func _on_replace_block_code_button_pressed():
_replace_block_code_button.disabled = true
replace_block_code.emit()
func _input(event):
if event is InputEventKey:
if event.keycode == KEY_SHIFT:
set_mouse_override(event.pressed)
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT or event.button_index == MOUSE_BUTTON_MIDDLE:
if event.pressed and is_mouse_over():
_panning = true
else:
_panning = false
if event.button_index == MOUSE_BUTTON_MIDDLE:
set_mouse_override(event.pressed)
var relative_mouse_pos := get_global_mouse_position() - get_global_rect().position
if is_mouse_over():
var old_mouse_window_pos := canvas_to_window(relative_mouse_pos)
if event.button_index == MOUSE_BUTTON_WHEEL_UP and zoom < 2:
zoom *= ZOOM_FACTOR
if event.button_index == MOUSE_BUTTON_WHEEL_DOWN and zoom > 0.2:
zoom /= ZOOM_FACTOR
_window.position -= (old_mouse_window_pos - canvas_to_window(relative_mouse_pos)) * zoom
if event is InputEventMouseMotion:
if (Input.is_key_pressed(KEY_SHIFT) and _panning) or (Input.is_mouse_button_pressed(MOUSE_BUTTON_MIDDLE) and _panning):
_window.position += event.relative
func reset_window_position():
var blocks = get_blocks()
var top_left: Vector2 = Vector2.INF
for block in blocks:
if block.position.x < top_left.x:
top_left.x = block.position.x
if block.position.y < top_left.y:
top_left.y = block.position.y
if top_left == Vector2.INF:
top_left = Vector2.ZERO
_window.position = (-top_left + DEFAULT_WINDOW_MARGIN) * zoom
func canvas_to_window(v: Vector2) -> Vector2:
return _window.get_transform().affine_inverse() * v
func window_to_canvas(v: Vector2) -> Vector2:
return _window.get_transform() * v
func is_mouse_over() -> bool:
return get_global_rect().has_point(get_global_mouse_position())
func set_mouse_override(override: bool):
if override:
_mouse_override.mouse_filter = Control.MOUSE_FILTER_PASS
_mouse_override.mouse_default_cursor_shape = Control.CURSOR_MOVE
else:
_mouse_override.mouse_filter = Control.MOUSE_FILTER_IGNORE
_mouse_override.mouse_default_cursor_shape = Control.CURSOR_ARROW
func generate_script_from_current_window() -> String:
return ScriptGenerator.generate_script(_current_ast_list, _context.block_script)
func _on_zoom_button_pressed():
zoom = 1.0
reset_window_position()