Skip to content

Commit 5fa9380

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

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

addons/block_code/ui/block_canvas/block_canvas.gd

Lines changed: 27 additions & 3 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

@@ -88,6 +90,8 @@ func bsd_selected(bsd: BlockScriptData):
8890

8991
_window.position = Vector2(0, 0)
9092
zoom = 1
93+
94+
_window.visible = false
9195
_zoom_label.visible = false
9296

9397
_empty_box.visible = false
@@ -99,7 +103,9 @@ func bsd_selected(bsd: BlockScriptData):
99103

100104
if bsd != null:
101105
_load_bsd(bsd)
106+
_window.visible = true
102107
_zoom_label.visible = true
108+
reset_window_position()
103109
elif edited_node == null:
104110
_empty_box.visible = true
105111
elif BlockCodePlugin.node_has_block_code(edited_node):
@@ -132,6 +138,7 @@ func scene_has_bsd_nodes() -> bool:
132138

133139
func clear_canvas():
134140
for child in _window.get_children():
141+
_window.remove_child(child)
135142
child.queue_free()
136143

137144

@@ -140,7 +147,6 @@ func load_tree(parent: Node, node: SerializedBlockTreeNode):
140147
var scene: Block = load(_block_scene_path).instantiate()
141148
for prop_pair in node.serialized_block.serialized_props:
142149
scene.set(prop_pair[0], prop_pair[1])
143-
144150
parent.add_child(scene)
145151

146152
var scene_block: Block = scene as Block
@@ -227,6 +233,10 @@ func _input(event):
227233
set_mouse_override(event.pressed)
228234

229235
if event is InputEventMouseButton:
236+
if event.button_index == MOUSE_BUTTON_RIGHT and event.is_pressed():
237+
reset_window_position()
238+
return
239+
230240
if event.button_index == MOUSE_BUTTON_LEFT or event.button_index == MOUSE_BUTTON_MIDDLE:
231241
if event.pressed and is_mouse_over():
232242
_panning = true
@@ -246,15 +256,29 @@ func _input(event):
246256
if event.button_index == MOUSE_BUTTON_WHEEL_DOWN and zoom > 0.2:
247257
zoom /= ZOOM_FACTOR
248258

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

253261
if event is InputEventMouseMotion:
254262
if (Input.is_key_pressed(KEY_SHIFT) and _panning) or (Input.is_mouse_button_pressed(MOUSE_BUTTON_MIDDLE) and _panning):
255263
_window.position += event.relative
256264

257265

266+
func reset_window_position():
267+
var blocks = get_blocks()
268+
var top_left: Vector2 = Vector2.INF
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+
if top_left == Vector2.INF:
277+
top_left = Vector2.ZERO
278+
279+
_window.position = (-top_left + DEFAULT_WINDOW_MARGIN) * zoom
280+
281+
258282
func canvas_to_window(v: Vector2) -> Vector2:
259283
return _window.get_transform().affine_inverse() * v
260284

addons/block_code/ui/block_canvas/block_canvas.tscn

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[ext_resource type="Script" path="res://addons/block_code/ui/block_canvas/block_canvas.gd" id="1_tk8h2"]
44
[ext_resource type="Texture2D" uid="uid://cmusxj1ppspnp" path="res://addons/block_code/block_code_node/block_code_node.svg" id="2_710vn"]
55

6-
[sub_resource type="Image" id="Image_0g48l"]
6+
[sub_resource type="Image" id="Image_0aray"]
77
data = {
88
"data": PackedByteArray(255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 94, 94, 127, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 93, 93, 255, 255, 94, 94, 127, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 94, 94, 127, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 94, 94, 127, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 231, 255, 94, 94, 54, 255, 94, 94, 57, 255, 93, 93, 233, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 231, 255, 94, 94, 54, 255, 94, 94, 57, 255, 93, 93, 233, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 97, 97, 42, 255, 255, 255, 0, 255, 255, 255, 0, 255, 97, 97, 42, 255, 93, 93, 233, 255, 93, 93, 232, 255, 93, 93, 41, 255, 255, 255, 0, 255, 255, 255, 0, 255, 97, 97, 42, 255, 93, 93, 233, 255, 93, 93, 232, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 96, 96, 45, 255, 93, 93, 44, 255, 255, 255, 0, 255, 97, 97, 42, 255, 97, 97, 42, 255, 255, 255, 0, 255, 96, 96, 45, 255, 93, 93, 44, 255, 255, 255, 0, 255, 97, 97, 42, 255, 97, 97, 42, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 96, 96, 45, 255, 93, 93, 235, 255, 94, 94, 234, 255, 95, 95, 43, 255, 255, 255, 0, 255, 255, 255, 0, 255, 96, 96, 45, 255, 93, 93, 235, 255, 94, 94, 234, 255, 95, 95, 43, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 235, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 233, 255, 95, 95, 59, 255, 96, 96, 61, 255, 93, 93, 235, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 233, 255, 95, 95, 59, 255, 96, 96, 61, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0),
99
"format": "RGBA8",
@@ -13,7 +13,7 @@ data = {
1313
}
1414

1515
[sub_resource type="ImageTexture" id="ImageTexture_jgo72"]
16-
image = SubResource("Image_0g48l")
16+
image = SubResource("Image_0aray")
1717

1818
[node name="BlockCanvas" type="MarginContainer"]
1919
anchors_preset = 15
@@ -35,7 +35,6 @@ layout_mode = 2
3535
[node name="Window" type="Control" parent="WindowContainer"]
3636
unique_name_in_owner = true
3737
layout_mode = 2
38-
size_flags_horizontal = 3
3938
mouse_filter = 1
4039

4140
[node name="Overlay" type="Control" parent="WindowContainer"]

0 commit comments

Comments
 (0)