Skip to content

Commit 9929a18

Browse files
committed
Scale blocks when dragged over canvas
1 parent 0af8b52 commit 9929a18

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

addons/block_code/drag_manager/drag_manager.gd

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,14 @@ class Drag:
5757
func add_delete_area(delete_area: Rect2):
5858
_delete_areas.append(delete_area)
5959

60-
func update_drag_position():
60+
func update_drag_state():
6161
global_position = get_global_mouse_position()
6262

63+
if _block_canvas.is_mouse_over():
64+
scale = Vector2(_block_canvas.zoom, _block_canvas.zoom)
65+
else:
66+
scale = Vector2(1, 1)
67+
6368
for rect in _delete_areas:
6469
if rect.has_point(get_global_mouse_position()):
6570
action = DragAction.REMOVE
@@ -71,7 +76,7 @@ class Drag:
7176
target_snap_point = _find_closest_snap_point()
7277

7378
func apply_drag() -> Block:
74-
update_drag_position()
79+
update_drag_state()
7580

7681
if action == DragAction.PLACE:
7782
_place_block()
@@ -147,7 +152,7 @@ class Drag:
147152
var closest_distance: int
148153
for snap_point in _snap_points:
149154
var distance = _get_distance_to_snap_point(snap_point)
150-
if distance > Constants.MINIMUM_SNAP_DISTANCE:
155+
if distance > Constants.MINIMUM_SNAP_DISTANCE * _block_canvas.zoom:
151156
continue
152157
elif closest_snap_point == null or distance < closest_distance:
153158
closest_snap_point = snap_point
@@ -202,7 +207,7 @@ func _ready():
202207

203208
func _process(_delta):
204209
if drag:
205-
drag.update_drag_position()
210+
drag.update_drag_state()
206211

207212

208213
func drag_block(block: Block, copied_from: Block = null):
@@ -215,6 +220,9 @@ func drag_block(block: Block, copied_from: Block = null):
215220
else:
216221
offset = Vector2.ZERO
217222

223+
if _block_canvas.is_ancestor_of(block):
224+
offset /= _block_canvas.zoom
225+
218226
var parent = block.get_parent()
219227

220228
if parent:

addons/block_code/ui/block_canvas/block_canvas.gd

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ const ZOOM_FACTOR: float = 1.1
2828

2929
var _block_scenes_by_class = {}
3030
var _panning := false
31+
var zoom: float:
32+
set(value):
33+
_window.scale = Vector2(value, value)
34+
get:
35+
return _window.scale.x
3136

3237
signal reconnect_block(block: Block)
3338
signal add_block_code
@@ -82,7 +87,7 @@ func bsd_selected(bsd: BlockScriptData):
8287
var edited_node = EditorInterface.get_inspector().get_edited_object() as Node
8388

8489
_window.position = Vector2(0, 0)
85-
_window.scale = Vector2(1, 1)
90+
zoom = 1
8691
_zoom_label.visible = false
8792

8893
_empty_box.visible = false
@@ -112,6 +117,7 @@ func bsd_selected(bsd: BlockScriptData):
112117
_selected_node_label.text = _selected_node_label_format.format({"node": edited_node.name})
113118
_add_block_code_button.disabled = false
114119

120+
115121
func _load_bsd(bsd: BlockScriptData):
116122
for tree in bsd.block_trees.array:
117123
load_tree(_window, tree)
@@ -235,14 +241,14 @@ func _input(event):
235241
if is_mouse_over():
236242
var old_mouse_window_pos := canvas_to_window(relative_mouse_pos)
237243

238-
if event.button_index == MOUSE_BUTTON_WHEEL_UP and _window.scale.x < 2:
239-
_window.scale *= ZOOM_FACTOR
240-
if event.button_index == MOUSE_BUTTON_WHEEL_DOWN and _window.scale.x > 0.2:
241-
_window.scale /= ZOOM_FACTOR
244+
if event.button_index == MOUSE_BUTTON_WHEEL_UP and zoom < 2:
245+
zoom *= ZOOM_FACTOR
246+
if event.button_index == MOUSE_BUTTON_WHEEL_DOWN and zoom > 0.2:
247+
zoom /= ZOOM_FACTOR
242248

243-
_zoom_label.text = "%.1fx" % _window.scale.x
249+
_zoom_label.text = "%.1fx" % zoom
244250

245-
_window.position -= (old_mouse_window_pos - canvas_to_window(relative_mouse_pos)) * _window.scale.x
251+
_window.position -= (old_mouse_window_pos - canvas_to_window(relative_mouse_pos)) * zoom
246252

247253
if event is InputEventMouseMotion:
248254
if (Input.is_key_pressed(KEY_SHIFT) and _panning) or (Input.is_mouse_button_pressed(MOUSE_BUTTON_MIDDLE) and _panning):

0 commit comments

Comments
 (0)