Skip to content

Commit c5ae7e6

Browse files
authored
Merge pull request #79 from endlessm/block-scope
Add scope to blocks
2 parents 2086301 + cae4ec7 commit c5ae7e6

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

addons/block_code/drag_manager/drag_manager.gd

+46-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ enum DragAction { NONE, PLACE, REMOVE }
1616
class Drag:
1717
extends Control
1818
var _block: Block
19+
var _block_scope: String
1920
var _block_canvas: BlockCanvas
2021
var _preview_block: Control
2122
var action: DragAction:
@@ -38,13 +39,14 @@ class Drag:
3839
get:
3940
return target_snap_point.block if target_snap_point else null
4041

41-
func _init(block: Block, offset: Vector2, block_canvas: BlockCanvas):
42+
func _init(block: Block, block_scope: String, offset: Vector2, block_canvas: BlockCanvas):
4243
assert(block.get_parent() == null)
4344

4445
add_child(block)
4546
block.position = -offset
4647

4748
_block = block
49+
_block_scope = block_scope
4850
_block_canvas = block_canvas
4951

5052
func apply_drag() -> Block:
@@ -108,12 +110,24 @@ class Drag:
108110

109111
# Check if any parent node is this node
110112
var parent = _snap_point
113+
var top_block
111114
while parent is SnapPoint:
112115
if parent.block == _block:
113116
return false
114117

118+
top_block = parent.block
115119
parent = parent.block.get_parent()
116120

121+
# Check if scope is valid
122+
if _block_scope != "":
123+
if top_block is EntryBlock:
124+
if _block_scope != top_block.get_entry_statement():
125+
return false
126+
else:
127+
var tree_scope := DragManager.get_tree_scope(top_block)
128+
if tree_scope != "" and _block_scope != tree_scope:
129+
return false
130+
117131
return true
118132

119133
func sort_snap_points_by_distance(a: SnapPoint, b: SnapPoint):
@@ -198,7 +212,11 @@ func drag_block(block: Block, copied_from: Block = null):
198212

199213
block.disconnect_signals()
200214

201-
drag = Drag.new(block, offset, _block_canvas)
215+
var block_scope := get_tree_scope(block)
216+
if block_scope != "":
217+
_block_canvas.set_scope(block_scope)
218+
219+
drag = Drag.new(block, block_scope, offset, _block_canvas)
202220
add_child(drag)
203221

204222

@@ -222,6 +240,8 @@ func drag_ended():
222240
if block:
223241
connect_block_canvas_signals(block)
224242

243+
_block_canvas.release_scope()
244+
225245
drag.queue_free()
226246
drag = null
227247

@@ -237,7 +257,27 @@ func connect_block_canvas_signals(block: Block):
237257
var statement_block := block as StatementBlock
238258
for pair in statement_block.param_name_input_pairs:
239259
var param_input: ParameterInput = pair[1]
240-
var b := param_input.get_snapped_block()
241-
if b:
242-
if b.drag_started.get_connections().size() == 0:
243-
b.drag_started.connect(copy_picked_block_and_drag)
260+
var copy_block := param_input.get_snapped_block()
261+
if copy_block == null:
262+
continue
263+
if copy_block.drag_started.get_connections().size() == 0:
264+
copy_block.drag_started.connect(func(b: Block): drag_copy_parameter(b, block))
265+
266+
267+
func drag_copy_parameter(block: Block, parent: Block):
268+
if parent is EntryBlock:
269+
block.scope = parent.get_entry_statement()
270+
copy_picked_block_and_drag(block)
271+
272+
273+
## Returns the scope of the first non-empty scope child block
274+
static func get_tree_scope(node: Node) -> String:
275+
if node is Block:
276+
if node.scope != "":
277+
return node.scope
278+
279+
for c in node.get_children():
280+
var scope := get_tree_scope(c)
281+
if scope != "":
282+
return scope
283+
return ""

addons/block_code/ui/block_canvas/block_canvas.gd

+21
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,24 @@ func find_snaps(node: Node) -> Array:
121121
snaps.append_array(find_snaps(c))
122122

123123
return snaps
124+
125+
126+
func set_scope(scope: String):
127+
for block in _window.get_children():
128+
var valid := false
129+
130+
if block is EntryBlock:
131+
if scope == block.get_entry_statement():
132+
valid = true
133+
else:
134+
var tree_scope := DragManager.get_tree_scope(block)
135+
if tree_scope == "" or scope == tree_scope:
136+
valid = true
137+
138+
if not valid:
139+
block.modulate = Color(0.5, 0.5, 0.5, 1)
140+
141+
142+
func release_scope():
143+
for block in _window.get_children():
144+
block.modulate = Color.WHITE

addons/block_code/ui/blocks/block/block.gd

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ signal modified
2323
## The next block in the line of execution (can be null if end)
2424
@export var bottom_snap_path: NodePath
2525

26+
## The scope of the block (statement of matching entry block)
27+
@export var scope: String = ""
28+
2629
var bottom_snap: SnapPoint
2730

2831

@@ -62,7 +65,7 @@ func get_instruction_node() -> InstructionTree.TreeNode:
6265

6366
# Override this method to add more serialized properties
6467
func get_serialized_props() -> Array:
65-
return serialize_props(["block_name", "label", "color", "block_type", "position"])
68+
return serialize_props(["block_name", "label", "color", "block_type", "position", "scope"])
6669

6770

6871
func serialize_props(prop_names: Array) -> Array:

0 commit comments

Comments
 (0)