Skip to content

Commit 25e14ab

Browse files
committed
Always scroll block canvas to the top-left block on open
1 parent 387f5ca commit 25e14ab

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

addons/block_code/ui/block_canvas/block_canvas.gd

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ extends MarginContainer
44

55
const EXTEND_MARGIN: float = 800
66
const BLOCK_AUTO_PLACE_MARGIN: Vector2 = Vector2(16, 8)
7+
const DEFAULT_WINDOW_MARGIN: Vector2 = Vector2(24, 24)
78
const ZOOM_FACTOR: float = 1.1
89

910
@onready var _window: Control = %Window
@@ -31,6 +32,7 @@ var _panning := false
3132
var zoom: float:
3233
set(value):
3334
_window.scale = Vector2(value, value)
35+
_zoom_label.text = "%.1fx" % value
3436
get:
3537
return _window.scale.x
3638

@@ -117,6 +119,8 @@ func bsd_selected(bsd: BlockScriptData):
117119
_selected_node_label.text = _selected_node_label_format.format({"node": edited_node.name})
118120
_add_block_code_button.disabled = false
119121

122+
reset_window_position()
123+
120124

121125
func _load_bsd(bsd: BlockScriptData):
122126
for tree in bsd.block_trees.array:
@@ -227,6 +231,10 @@ func _input(event):
227231
set_mouse_override(event.pressed)
228232

229233
if event is InputEventMouseButton:
234+
if event.button_index == MOUSE_BUTTON_RIGHT and event.is_pressed():
235+
reset_window_position()
236+
return
237+
230238
if event.button_index == MOUSE_BUTTON_LEFT or event.button_index == MOUSE_BUTTON_MIDDLE:
231239
if event.pressed and is_mouse_over():
232240
_panning = true
@@ -246,15 +254,28 @@ func _input(event):
246254
if event.button_index == MOUSE_BUTTON_WHEEL_DOWN and zoom > 0.2:
247255
zoom /= ZOOM_FACTOR
248256

249-
_zoom_label.text = "%.1fx" % zoom
250-
251257
_window.position -= (old_mouse_window_pos - canvas_to_window(relative_mouse_pos)) * zoom
252258

253259
if event is InputEventMouseMotion:
254260
if (Input.is_key_pressed(KEY_SHIFT) and _panning) or (Input.is_mouse_button_pressed(MOUSE_BUTTON_MIDDLE) and _panning):
255261
_window.position += event.relative
256262

257263

264+
func reset_window_position():
265+
# Generate a bounding box from all of the blocks in the canvas
266+
267+
var blocks = get_blocks()
268+
var top_left: Vector2 = blocks.pop_front().position if blocks.size() > 0 else Vector2(0, 0)
269+
270+
for block in blocks:
271+
if block.position.x < top_left.x:
272+
top_left.x = block.position.x
273+
if block.position.y < top_left.y:
274+
top_left.y = block.position.y
275+
276+
_window.position = -top_left + DEFAULT_WINDOW_MARGIN
277+
278+
258279
func canvas_to_window(v: Vector2) -> Vector2:
259280
return _window.get_transform().affine_inverse() * v
260281

@@ -274,3 +295,7 @@ func set_mouse_override(override: bool):
274295
else:
275296
_mouse_override.mouse_filter = Control.MOUSE_FILTER_IGNORE
276297
_mouse_override.mouse_default_cursor_shape = Control.CURSOR_ARROW
298+
299+
300+
func _on_window_resized():
301+
pass

0 commit comments

Comments
 (0)