From 0ba940c886c2a6a9af2eaffe00221ebc9651b9f8 Mon Sep 17 00:00:00 2001 From: Manolis Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Tue, 4 May 2021 02:45:14 +0300 Subject: [PATCH] Implement snap to grid while moving selections and using the Move tool Mouse click + Ctrl to snap to grid while moving. --- src/Tools/Move.gd | 3 +++ src/Tools/SelectionTools/RectSelect.gd | 1 + src/Tools/SelectionTools/SelectionTool.gd | 17 +++++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/src/Tools/Move.gd b/src/Tools/Move.gd index 12b68b3bf..ca7d3dda2 100644 --- a/src/Tools/Move.gd +++ b/src/Tools/Move.gd @@ -19,6 +19,9 @@ func draw_move(position : Vector2) -> void: position.y = _start_pos.y else: position.x = _start_pos.x + if Tools.control: # Snap to grid + position = position.snapped(Vector2(Global.grid_width, Global.grid_height)) + if Global.current_project.has_selection: Global.canvas.selection.move_content(position - _offset) _offset = position diff --git a/src/Tools/SelectionTools/RectSelect.gd b/src/Tools/SelectionTools/RectSelect.gd index 8966a5b3c..0dfdc45fd 100644 --- a/src/Tools/SelectionTools/RectSelect.gd +++ b/src/Tools/SelectionTools/RectSelect.gd @@ -9,6 +9,7 @@ var _displace_origin = false # Mouse Click + Alt func _input(event : InputEvent) -> void: + ._input(event) if !_move and !_rect.has_no_area(): if event.is_action_pressed("shift"): _square = true diff --git a/src/Tools/SelectionTools/SelectionTool.gd b/src/Tools/SelectionTools/SelectionTool.gd index e0d46e43b..59c18ea20 100644 --- a/src/Tools/SelectionTools/SelectionTool.gd +++ b/src/Tools/SelectionTools/SelectionTool.gd @@ -9,6 +9,7 @@ var _offset := Vector2.ZERO var _add := false # Shift + Mouse Click var _subtract := false # Ctrl + Mouse Click var _intersect := false # Shift + Ctrl + Mouse Click +var _snap_to_grid := false # Mouse Click + Ctrl var undo_data : Dictionary @@ -27,6 +28,19 @@ func _ready() -> void: hspinbox.value = select_rect.size.y +func _input(event : InputEvent) -> void: + if _move: + if event.is_action_pressed("ctrl"): + _snap_to_grid = true + var grid_size := Vector2(Global.grid_width, Global.grid_height) + _offset = _offset.snapped(grid_size) + var prev_pos = selection_node.big_bounding_rectangle.position + selection_node.big_bounding_rectangle.position = selection_node.big_bounding_rectangle.position.snapped(grid_size) + selection_node.marching_ants_outline.offset += selection_node.big_bounding_rectangle.position - prev_pos + elif event.is_action_released("ctrl"): + _snap_to_grid = false + + func draw_start(position : Vector2) -> void: var project : Project = Global.current_project undo_data = selection_node._get_undo_data(false) @@ -70,6 +84,8 @@ func draw_move(position : Vector2) -> void: position.y = _start_pos.y else: position.x = _start_pos.x + if _snap_to_grid: + position = position.snapped(Vector2(Global.grid_width, Global.grid_height)) if _move_content: selection_node.move_content(position - _offset) @@ -87,6 +103,7 @@ func draw_end(_position : Vector2) -> void: apply_selection(_position) _move = false + _snap_to_grid = false cursor_text = ""