From ad2fcf48912062af74f2b8b7925d3cf0d98e5f87 Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Tue, 3 Dec 2024 17:55:36 +0200 Subject: [PATCH] Add a grid_offset parameter for the rectangular grid snap methods --- src/Tools/BaseDraw.gd | 4 +++- src/Tools/BaseTool.gd | 34 +++++++++++++++++++--------------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/Tools/BaseDraw.gd b/src/Tools/BaseDraw.gd index 3f7c09d6e..9b2ddf0f2 100644 --- a/src/Tools/BaseDraw.gd +++ b/src/Tools/BaseDraw.gd @@ -520,7 +520,9 @@ 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_rectangular_grid_center(snapped_position, grid_size, -1) + snapped_position = _snap_to_rectangular_grid_center( + snapped_position, grid_size, Vector2i.ZERO, -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 37764a59e..51fa3221d 100644 --- a/src/Tools/BaseTool.gd +++ b/src/Tools/BaseTool.gd @@ -145,10 +145,14 @@ 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: - pos = _snap_to_rectangular_grid_boundary(pos, Global.grids[0].grid_size, snapping_distance) + pos = _snap_to_rectangular_grid_boundary( + pos, Global.grids[0].grid_size, Global.grids[0].grid_offset, snapping_distance + ) if Global.snap_to_rectangular_grid_center: - pos = _snap_to_rectangular_grid_center(pos, Global.grids[0].grid_size, snapping_distance) + pos = _snap_to_rectangular_grid_center( + pos, Global.grids[0].grid_size, Global.grids[0].grid_offset, snapping_distance + ) var snap_to := Vector2.INF if Global.snap_to_guides: @@ -262,22 +266,22 @@ func _get_closest_point_to_segment( func _snap_to_rectangular_grid_boundary( - pos: Vector2, grid_size: Vector2i, snapping_distance: float + pos: Vector2, grid_size: Vector2i, grid_offset: Vector2i, snapping_distance: float ) -> Vector2: - var grid_pos := pos.snapped(Global.grids[0].grid_size) - grid_pos += Vector2(Global.grids[0].grid_offset) + var grid_pos := pos.snapped(grid_size) + grid_pos += Vector2(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 t_l := grid_pos + Vector2(-grid_size.x, -grid_size.y) + var t_c := grid_pos + Vector2(0, -grid_size.y) + var t_r := grid_pos + Vector2(grid_size.x, -grid_size.y) + var m_l := grid_pos + Vector2(-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 m_r := grid_pos + Vector2(grid_size.x, 0) + var b_l := grid_pos + Vector2(-grid_size.x, grid_size.y) + var b_c := grid_pos + Vector2(0, grid_size.y) + var b_r := grid_pos + Vector2(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): @@ -290,10 +294,10 @@ func _snap_to_rectangular_grid_boundary( func _snap_to_rectangular_grid_center( - pos: Vector2, grid_size: Vector2i, snapping_distance: float + pos: Vector2, grid_size: Vector2i, grid_offset: Vector2i, snapping_distance: float ) -> Vector2: var grid_center := pos.snapped(grid_size) + Vector2(grid_size / 2) - grid_center += Vector2(Global.grids[0].grid_offset) + grid_center += Vector2(grid_offset) # keeping grid_center 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