diff --git a/src/Tools/BaseDraw.gd b/src/Tools/BaseDraw.gd index 730448963..3f7c09d6e 100644 --- a/src/Tools/BaseDraw.gd +++ b/src/Tools/BaseDraw.gd @@ -520,7 +520,7 @@ func draw_indicator(left: bool) -> void: if is_placing_tiles(): var tileset := (Global.current_project.get_current_cel() as CelTileMap).tileset var grid_size := tileset.tile_size - snapped_position = _snap_to_grid_center(snapped_position, grid_size, -1) + snapped_position = _snap_to_rectangular_grid_center(snapped_position, grid_size, -1) draw_indicator_at(snapped_position, Vector2i.ZERO, color) if ( Global.current_project.has_selection diff --git a/src/Tools/BaseTool.gd b/src/Tools/BaseTool.gd index e3de4759d..37764a59e 100644 --- a/src/Tools/BaseTool.gd +++ b/src/Tools/BaseTool.gd @@ -145,31 +145,10 @@ func draw_preview() -> void: func snap_position(pos: Vector2) -> Vector2: var snapping_distance := Global.snapping_distance / Global.camera.zoom.x if Global.snap_to_rectangular_grid_boundary: - var grid_pos := pos.snapped(Global.grids[0].grid_size) - grid_pos += Vector2(Global.grids[0].grid_offset) - # keeping grid_pos as is would have been fine but this adds extra accuracy as to - # which snap point (from the list below) is closest to mouse and occupy THAT point - # t_l is for "top left" and so on - var t_l := grid_pos + Vector2(-Global.grids[0].grid_size.x, -Global.grids[0].grid_size.y) - var t_c := grid_pos + Vector2(0, -Global.grids[0].grid_size.y) - var t_r := grid_pos + Vector2(Global.grids[0].grid_size.x, -Global.grids[0].grid_size.y) - var m_l := grid_pos + Vector2(-Global.grids[0].grid_size.x, 0) - var m_c := grid_pos - var m_r := grid_pos + Vector2(Global.grids[0].grid_size.x, 0) - var b_l := grid_pos + Vector2(-Global.grids[0].grid_size.x, Global.grids[0].grid_size.y) - var b_c := grid_pos + Vector2(0, Global.grids[0].grid_size.y) - var b_r := grid_pos + Vector2(Global.grids[0].grid_size) - var vec_arr: PackedVector2Array = [t_l, t_c, t_r, m_l, m_c, m_r, b_l, b_c, b_r] - for vec in vec_arr: - if vec.distance_to(pos) < grid_pos.distance_to(pos): - grid_pos = vec - - var grid_point := _get_closest_point_to_grid(pos, snapping_distance, grid_pos) - if grid_point != Vector2.INF: - pos = grid_point.floor() + pos = _snap_to_rectangular_grid_boundary(pos, Global.grids[0].grid_size, snapping_distance) if Global.snap_to_rectangular_grid_center: - pos = _snap_to_grid_center(pos, Global.grids[0].grid_size, snapping_distance) + pos = _snap_to_rectangular_grid_center(pos, Global.grids[0].grid_size, snapping_distance) var snap_to := Vector2.INF if Global.snap_to_guides: @@ -282,7 +261,37 @@ func _get_closest_point_to_segment( return closest_point -func _snap_to_grid_center(pos: Vector2, grid_size: Vector2i, snapping_distance: float) -> Vector2: +func _snap_to_rectangular_grid_boundary( + pos: Vector2, grid_size: Vector2i, snapping_distance: float +) -> Vector2: + var grid_pos := pos.snapped(Global.grids[0].grid_size) + grid_pos += Vector2(Global.grids[0].grid_offset) + # keeping grid_pos as is would have been fine but this adds extra accuracy as to + # which snap point (from the list below) is closest to mouse and occupy THAT point + # t_l is for "top left" and so on + var t_l := grid_pos + Vector2(-Global.grids[0].grid_size.x, -Global.grids[0].grid_size.y) + var t_c := grid_pos + Vector2(0, -Global.grids[0].grid_size.y) + var t_r := grid_pos + Vector2(Global.grids[0].grid_size.x, -Global.grids[0].grid_size.y) + var m_l := grid_pos + Vector2(-Global.grids[0].grid_size.x, 0) + var m_c := grid_pos + var m_r := grid_pos + Vector2(Global.grids[0].grid_size.x, 0) + var b_l := grid_pos + Vector2(-Global.grids[0].grid_size.x, Global.grids[0].grid_size.y) + var b_c := grid_pos + Vector2(0, Global.grids[0].grid_size.y) + var b_r := grid_pos + Vector2(Global.grids[0].grid_size) + var vec_arr: PackedVector2Array = [t_l, t_c, t_r, m_l, m_c, m_r, b_l, b_c, b_r] + for vec in vec_arr: + if vec.distance_to(pos) < grid_pos.distance_to(pos): + grid_pos = vec + + var grid_point := _get_closest_point_to_grid(pos, snapping_distance, grid_pos) + if grid_point != Vector2.INF: + pos = grid_point.floor() + return pos + + +func _snap_to_rectangular_grid_center( + pos: Vector2, grid_size: Vector2i, snapping_distance: float +) -> Vector2: var grid_center := pos.snapped(grid_size) + Vector2(grid_size / 2) grid_center += Vector2(Global.grids[0].grid_offset) # keeping grid_center as is would have been fine but this adds extra accuracy as to