Skip to content

Commit 93cbeab

Browse files
committed
Scale blocks when dragged over canvas
1 parent d49b949 commit 93cbeab

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-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: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ const ZOOM_FACTOR: float = 1.1
1414

1515
var _block_scenes_by_class = {}
1616
var _panning := false
17+
var zoom: float:
18+
set(value):
19+
_window.scale = Vector2(value, value)
20+
get:
21+
return _window.scale.x
1722

1823
signal reconnect_block(block: Block)
1924

@@ -61,7 +66,7 @@ func bsd_selected(bsd: BlockScriptData):
6166
clear_canvas()
6267

6368
_window.position = Vector2(0, 0)
64-
_window.scale = Vector2(1, 1)
69+
zoom = 1
6570

6671
_zoom_label.visible = true
6772
_choose_block_code_label.visible = false
@@ -180,14 +185,14 @@ func _input(event):
180185
if is_mouse_over():
181186
var old_mouse_window_pos := canvas_to_window(relative_mouse_pos)
182187

183-
if event.button_index == MOUSE_BUTTON_WHEEL_UP and _window.scale.x < 2:
184-
_window.scale *= ZOOM_FACTOR
185-
if event.button_index == MOUSE_BUTTON_WHEEL_DOWN and _window.scale.x > 0.2:
186-
_window.scale /= ZOOM_FACTOR
188+
if event.button_index == MOUSE_BUTTON_WHEEL_UP and zoom < 2:
189+
zoom *= ZOOM_FACTOR
190+
if event.button_index == MOUSE_BUTTON_WHEEL_DOWN and zoom > 0.2:
191+
zoom /= ZOOM_FACTOR
187192

188-
_zoom_label.text = "%.1fx" % _window.scale.x
193+
_zoom_label.text = "%.1fx" % zoom
189194

190-
_window.position -= (old_mouse_window_pos - canvas_to_window(relative_mouse_pos)) * _window.scale.x
195+
_window.position -= (old_mouse_window_pos - canvas_to_window(relative_mouse_pos)) * zoom
191196

192197
if event is InputEventMouseMotion:
193198
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)