1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-31 07:29:49 +00:00

Compare commits

...

2 commits

Author SHA1 Message Date
Emmanouil Papadeas 6a0ad2e3db Add a grid_offset parameter for the rectangular grid snap methods 2024-12-03 17:55:36 +02:00
Emmanouil Papadeas 8bdad90c30 Add a _snap_to_rectangular_grid_boundary() method to BaseTool
And rename _snap_to_grid_center() to _snap_to_rectangular_grid_center()
2024-12-03 17:42:23 +02:00
2 changed files with 41 additions and 26 deletions

View file

@ -520,7 +520,9 @@ func draw_indicator(left: bool) -> void:
if is_placing_tiles(): if is_placing_tiles():
var tileset := (Global.current_project.get_current_cel() as CelTileMap).tileset var tileset := (Global.current_project.get_current_cel() as CelTileMap).tileset
var grid_size := tileset.tile_size 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, Vector2i.ZERO, -1
)
draw_indicator_at(snapped_position, Vector2i.ZERO, color) draw_indicator_at(snapped_position, Vector2i.ZERO, color)
if ( if (
Global.current_project.has_selection Global.current_project.has_selection

View file

@ -145,31 +145,14 @@ func draw_preview() -> void:
func snap_position(pos: Vector2) -> Vector2: func snap_position(pos: Vector2) -> Vector2:
var snapping_distance := Global.snapping_distance / Global.camera.zoom.x var snapping_distance := Global.snapping_distance / Global.camera.zoom.x
if Global.snap_to_rectangular_grid_boundary: if Global.snap_to_rectangular_grid_boundary:
var grid_pos := pos.snapped(Global.grids[0].grid_size) pos = _snap_to_rectangular_grid_boundary(
grid_pos += Vector2(Global.grids[0].grid_offset) pos, Global.grids[0].grid_size, Global.grids[0].grid_offset, snapping_distance
# 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()
if Global.snap_to_rectangular_grid_center: 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, Global.grids[0].grid_offset, snapping_distance
)
var snap_to := Vector2.INF var snap_to := Vector2.INF
if Global.snap_to_guides: if Global.snap_to_guides:
@ -282,9 +265,39 @@ func _get_closest_point_to_segment(
return closest_point 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, grid_offset: Vector2i, snapping_distance: float
) -> Vector2:
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(-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(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):
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, grid_offset: Vector2i, snapping_distance: float
) -> Vector2:
var grid_center := pos.snapped(grid_size) + Vector2(grid_size / 2) 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 # 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 # which snap point (from the list below) is closest to mouse and occupy THAT point
# t_l is for "top left" and so on # t_l is for "top left" and so on