From 8a922a255cda787702586eb467bc5f326bd1e128 Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas Date: Sun, 3 Jul 2022 00:04:01 +0300 Subject: [PATCH] Fix move tool grid offset snapping, follow-up to #712 Also re-formatted SelectionTool's draw_move() --- src/Tools/Move.gd | 41 ++++++++--------- src/Tools/SelectionTools/SelectionTool.gd | 56 +++++++++++------------ 2 files changed, 48 insertions(+), 49 deletions(-) diff --git a/src/Tools/Move.gd b/src/Tools/Move.gd index 3382f68c3182..0874430f7dbc 100644 --- a/src/Tools/Move.gd +++ b/src/Tools/Move.gd @@ -49,15 +49,7 @@ func draw_move(position: Vector2) -> void: # while the content is being moved if _content_transformation_check != selection_node.is_moving_content: return - if Input.is_action_pressed("transform_snap_axis"): - var angle := position.angle_to_point(_start_pos) - if abs(angle) <= PI / 4 or abs(angle) >= 3 * PI / 4: - position.y = _start_pos.y - else: - position.x = _start_pos.x - if _snap_to_grid: # Snap to grid - position = position.snapped(Vector2(Global.grid_width, Global.grid_height)) - position += Vector2(Global.grid_offset_x, Global.grid_offset_y) + position = _snap_position(position) if Global.current_project.has_selection: selection_node.move_content(position - _offset) @@ -74,23 +66,13 @@ func draw_end(position: Vector2) -> void: _start_pos != Vector2.INF and _content_transformation_check == selection_node.is_moving_content ): - if Input.is_action_pressed("transform_snap_axis"): - var angle := position.angle_to_point(_start_pos) - if abs(angle) <= PI / 4 or abs(angle) >= 3 * PI / 4: - position.y = _start_pos.y - else: - position.x = _start_pos.x - - if _snap_to_grid: # Snap to grid - position = position.snapped(Vector2(Global.grid_width, Global.grid_height)) - position += Vector2(Global.grid_offset_x, Global.grid_offset_y) - - var pixel_diff: Vector2 = position - _start_pos + position = _snap_position(position) var project: Project = Global.current_project if project.has_selection: selection_node.move_borders_end() else: + var pixel_diff: Vector2 = position - _start_pos Global.canvas.move_preview_location = Vector2.ZERO var images := _get_selected_draw_images() for image in images: @@ -105,6 +87,23 @@ func draw_end(position: Vector2) -> void: _snap_to_grid = false +func _snap_position(position: Vector2) -> Vector2: + if Input.is_action_pressed("transform_snap_axis"): + var angle := position.angle_to_point(_start_pos) + if abs(angle) <= PI / 4 or abs(angle) >= 3 * PI / 4: + position.y = _start_pos.y + else: + position.x = _start_pos.x + if _snap_to_grid: # Snap to grid + var grid_size := Vector2(Global.grid_width, Global.grid_height) + position = position.snapped(grid_size) + var grid_offset = Vector2(Global.grid_offset_x, Global.grid_offset_y) + grid_offset = Vector2(fmod(grid_offset.x, grid_size.x), fmod(grid_offset.y, grid_size.y)) + position += grid_offset + + return position + + func commit_undo(action: String) -> void: var redo_data := _get_undo_data() var project: Project = Global.current_project diff --git a/src/Tools/SelectionTools/SelectionTool.gd b/src/Tools/SelectionTools/SelectionTool.gd index 95b23c111c0a..34719f861f56 100644 --- a/src/Tools/SelectionTools/SelectionTool.gd +++ b/src/Tools/SelectionTools/SelectionTool.gd @@ -125,36 +125,36 @@ func draw_move(position: Vector2) -> void: # while the content is being moved if _content_transformation_check != selection_node.is_moving_content: return - if _move: - if Input.is_action_pressed("transform_snap_axis"): # Snap to axis - var angle := position.angle_to_point(_start_pos) - if abs(angle) <= PI / 4 or abs(angle) >= 3 * PI / 4: - position.y = _start_pos.y - else: - position.x = _start_pos.x - if Input.is_action_pressed("transform_snap_grid"): - 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 = prev_pos.snapped(grid_size) - selection_node.marching_ants_outline.offset += ( - selection_node.big_bounding_rectangle.position - - prev_pos - ) - position = position.snapped(grid_size) - var grid_offset = Vector2(Global.grid_offset_x, Global.grid_offset_y) - grid_offset = Vector2( - fmod(grid_offset.x, grid_size.x), fmod(grid_offset.y, grid_size.y) - ) - position += grid_offset - - if _move_content: - selection_node.move_content(position - _offset) + if not _move: + return + + if Input.is_action_pressed("transform_snap_axis"): # Snap to axis + var angle := position.angle_to_point(_start_pos) + if abs(angle) <= PI / 4 or abs(angle) >= 3 * PI / 4: + position.y = _start_pos.y else: - selection_node.move_borders(position - _offset) + position.x = _start_pos.x + if Input.is_action_pressed("transform_snap_grid"): + 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 = prev_pos.snapped(grid_size) + selection_node.marching_ants_outline.offset += ( + selection_node.big_bounding_rectangle.position + - prev_pos + ) + position = position.snapped(grid_size) + var grid_offset = Vector2(Global.grid_offset_x, Global.grid_offset_y) + grid_offset = Vector2(fmod(grid_offset.x, grid_size.x), fmod(grid_offset.y, grid_size.y)) + position += grid_offset + + if _move_content: + selection_node.move_content(position - _offset) + else: + selection_node.move_borders(position - _offset) - _offset = position - _set_cursor_text(selection_node.big_bounding_rectangle) + _offset = position + _set_cursor_text(selection_node.big_bounding_rectangle) func draw_end(position: Vector2) -> void: