Skip to content

Commit fb3a4c9

Browse files
committed
In block_canvas, snap EntryBlock blocks to a grid
1 parent 5d57c2e commit fb3a4c9

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

addons/block_code/ui/block_canvas/block_canvas.gd

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ class_name BlockCanvas
33
extends MarginContainer
44

55
const EXTEND_MARGIN: float = 800
6-
const BLOCK_AUTO_PLACE_MARGIN: Vector2 = Vector2(16, 8)
7-
const DEFAULT_WINDOW_MARGIN: Vector2 = Vector2(24, 24)
6+
const BLOCK_AUTO_PLACE_MARGIN: Vector2 = Vector2(25, 8)
7+
const DEFAULT_WINDOW_MARGIN: Vector2 = Vector2(25, 25)
8+
const SNAP_GRID: Vector2 = Vector2(25, 25)
89
const ZOOM_FACTOR: float = 1.1
910

1011
@onready var _window: Control = %Window
@@ -27,6 +28,7 @@ const ZOOM_FACTOR: float = 1.1
2728
@onready var _mouse_override: Control = %MouseOverride
2829
@onready var _zoom_label: Label = %ZoomLabel
2930

31+
var _current_bsd: BlockScriptData
3032
var _block_scenes_by_class = {}
3133
var _panning := false
3234
var zoom: float:
@@ -59,7 +61,11 @@ func _populate_block_scenes_by_class():
5961

6062

6163
func add_block(block: Block, position: Vector2 = Vector2.ZERO) -> void:
62-
block.position = canvas_to_window(position)
64+
if block is EntryBlock:
65+
block.position = canvas_to_window(position).snapped(SNAP_GRID)
66+
else:
67+
block.position = canvas_to_window(position)
68+
6369
_window.add_child(block)
6470

6571

@@ -74,7 +80,9 @@ func get_blocks() -> Array[Block]:
7480

7581
func arrange_block(block: Block, nearby_block: Block) -> void:
7682
add_block(block)
77-
block.global_position = (nearby_block.global_position + (nearby_block.get_size() * Vector2.RIGHT) + BLOCK_AUTO_PLACE_MARGIN)
83+
var rect = nearby_block.get_global_rect()
84+
rect.position += (rect.size * Vector2.RIGHT) + BLOCK_AUTO_PLACE_MARGIN
85+
block.global_position = rect.position
7886

7987

8088
func set_child(n: Node):
@@ -105,7 +113,9 @@ func bsd_selected(bsd: BlockScriptData):
105113
_load_bsd(bsd)
106114
_window.visible = true
107115
_zoom_label.visible = true
108-
reset_window_position()
116+
117+
if bsd != _current_bsd:
118+
reset_window_position()
109119
elif edited_node == null:
110120
_empty_box.visible = true
111121
elif BlockCodePlugin.node_has_block_code(edited_node):
@@ -123,6 +133,8 @@ func bsd_selected(bsd: BlockScriptData):
123133
_selected_node_label.text = _selected_node_label_format.format({"node": edited_node.name})
124134
_add_block_code_button.disabled = false
125135

136+
_current_bsd = bsd
137+
126138

127139
func _load_bsd(bsd: BlockScriptData):
128140
for tree in bsd.block_trees.array:

0 commit comments

Comments
 (0)