Skip to content

Commit 5d57c2e

Browse files
authored
Merge pull request #128 from endlessm/block-canvas-initial-position
Always scroll block canvas to the top-left block on open
2 parents 387f5ca + 96691a9 commit 5d57c2e

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

addons/block_code/ui/block_canvas/block_canvas.gd

Lines changed: 23 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
@@ -246,15 +252,29 @@ func _input(event):
246252
if event.button_index == MOUSE_BUTTON_WHEEL_DOWN and zoom > 0.2:
247253
zoom /= ZOOM_FACTOR
248254

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

253257
if event is InputEventMouseMotion:
254258
if (Input.is_key_pressed(KEY_SHIFT) and _panning) or (Input.is_mouse_button_pressed(MOUSE_BUTTON_MIDDLE) and _panning):
255259
_window.position += event.relative
256260

257261

262+
func reset_window_position():
263+
var blocks = get_blocks()
264+
var top_left: Vector2 = Vector2.INF
265+
266+
for block in blocks:
267+
if block.position.x < top_left.x:
268+
top_left.x = block.position.x
269+
if block.position.y < top_left.y:
270+
top_left.y = block.position.y
271+
272+
if top_left == Vector2.INF:
273+
top_left = Vector2.ZERO
274+
275+
_window.position = (-top_left + DEFAULT_WINDOW_MARGIN) * zoom
276+
277+
258278
func canvas_to_window(v: Vector2) -> Vector2:
259279
return _window.get_transform().affine_inverse() * v
260280

addons/block_code/ui/block_canvas/block_canvas.tscn

Lines changed: 16 additions & 5 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
@@ -28,18 +28,29 @@ script = ExtResource("1_tk8h2")
2828
[node name="Panel" type="Panel" parent="."]
2929
layout_mode = 2
3030

31-
[node name="WindowContainer" type="MarginContainer" parent="."]
31+
[node name="WindowContainer" type="Control" parent="."]
3232
clip_contents = true
3333
layout_mode = 2
3434

3535
[node name="Window" type="Control" parent="WindowContainer"]
3636
unique_name_in_owner = true
3737
layout_mode = 2
38-
size_flags_horizontal = 3
38+
anchors_preset = 0
39+
offset_right = 1152.0
40+
offset_bottom = 648.0
3941
mouse_filter = 1
4042

4143
[node name="Overlay" type="Control" parent="WindowContainer"]
42-
layout_mode = 2
44+
layout_mode = 1
45+
anchors_preset = 3
46+
anchor_left = 1.0
47+
anchor_top = 1.0
48+
anchor_right = 1.0
49+
anchor_bottom = 1.0
50+
offset_left = -1152.0
51+
offset_top = -648.0
52+
grow_horizontal = 0
53+
grow_vertical = 0
4354
mouse_filter = 2
4455

4556
[node name="MarginContainer" type="MarginContainer" parent="WindowContainer/Overlay"]

0 commit comments

Comments
 (0)