Skip to content

Commit

Permalink
Fix move tool grid offset snapping, follow-up to #712
Browse files Browse the repository at this point in the history
Also re-formatted SelectionTool's draw_move()
  • Loading branch information
OverloadedOrama committed Jul 2, 2022
1 parent dcb963d commit 8a922a2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 49 deletions.
41 changes: 20 additions & 21 deletions src/Tools/Move.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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
Expand Down
56 changes: 28 additions & 28 deletions src/Tools/SelectionTools/SelectionTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 8a922a2

Please sign in to comment.