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

Make snapping distance change based on the zoom

Should work similarly to GIMP now
This commit is contained in:
Emmanouil Papadeas 2023-04-16 21:15:40 +03:00
parent 9cf82e7f71
commit 5c8cf56f8f
2 changed files with 27 additions and 27 deletions

View file

@ -138,7 +138,7 @@ var draw_pixel_grid := false
var show_rulers := true
var show_guides := true
var show_mouse_guides := false
var snapping_distance := 10.0
var snapping_distance := 32.0
var snap_to_rectangular_grid := false
var snap_to_guides := false
var snap_to_perspective_guides := false

View file

@ -122,7 +122,7 @@ func draw_preview() -> void:
func snap_position(position: Vector2) -> Vector2:
var snap_distance := Global.snapping_distance * Vector2.ONE
var snapping_distance := Global.snapping_distance * Global.camera.zoom.x
if Global.snap_to_rectangular_grid:
var grid_size := Vector2(Global.grid_width, Global.grid_height)
var grid_offset := Vector2(Global.grid_offset_x, Global.grid_offset_y)
@ -144,9 +144,9 @@ func snap_position(position: Vector2) -> Vector2:
if vec.distance_to(position) < grid_pos.distance_to(position):
grid_pos = vec
var closest_point_grid := _get_closest_point_to_grid(position, snap_distance, grid_pos)
if closest_point_grid != Vector2.INF:
position = closest_point_grid.floor()
var grid_point := _get_closest_point_to_grid(position, snapping_distance, grid_pos)
if grid_point != Vector2.INF:
position = grid_point.floor()
var snap_to := Vector2.INF
if Global.snap_to_guides:
@ -155,35 +155,35 @@ func snap_position(position: Vector2) -> Vector2:
continue
var s1: Vector2 = guide.points[0]
var s2: Vector2 = guide.points[1]
var snap := _snap_to_guide(snap_to, position, snap_distance, s1, s2)
var snap := _snap_to_guide(snap_to, position, snapping_distance, s1, s2)
if snap == Vector2.INF:
continue
snap_to = snap
if Global.snap_to_perspective_guides:
for point in Global.current_project.vanishing_points:
if point.has("pos_x") and point.has("pos_y"): # Sanity check
for i in point.lines.size():
if point.lines[i].has("angle") and point.lines[i].has("length"): # Sanity check
var angle: float = deg2rad(point.lines[i].angle)
var length: float = point.lines[i].length
var start = Vector2(point.pos_x, point.pos_y)
var s1: Vector2 = start
var s2 := s1 + Vector2(length * cos(angle), length * sin(angle))
var snap := _snap_to_guide(snap_to, position, snap_distance, s1, s2)
if snap == Vector2.INF:
continue
snap_to = snap
if not (point.has("pos_x") and point.has("pos_y")): # Sanity check
continue
for i in point.lines.size():
if point.lines[i].has("angle") and point.lines[i].has("length"): # Sanity check
var angle: float = deg2rad(point.lines[i].angle)
var length: float = point.lines[i].length
var start = Vector2(point.pos_x, point.pos_y)
var s1: Vector2 = start
var s2 := s1 + Vector2(length * cos(angle), length * sin(angle))
var snap := _snap_to_guide(snap_to, position, snapping_distance, s1, s2)
if snap == Vector2.INF:
continue
snap_to = snap
if snap_to != Vector2.INF:
position = snap_to.floor()
return position
func _get_closest_point_to_grid(
position: Vector2, snap_distance: Vector2, grid_pos: Vector2
) -> Vector2:
func _get_closest_point_to_grid(position: Vector2, distance: float, grid_pos: Vector2) -> Vector2:
# If the cursor is close to the start/origin of a grid cell, snap to that
var snap_distance := distance * Vector2.ONE
var closest_point := Vector2.INF
var rect := Rect2()
rect.position = position - (snap_distance / 4.0)
@ -196,13 +196,13 @@ func _get_closest_point_to_grid(
var grid_start_hor := Vector2(0, grid_pos.y)
var grid_end_hor := Vector2(Global.current_project.size.x, grid_pos.y)
var closest_point_hor := _get_closest_point_to_segment(
position, snap_distance, grid_start_hor, grid_end_hor
position, distance, grid_start_hor, grid_end_hor
)
# Look for a point close to a vertical grid line
var grid_start_ver := Vector2(grid_pos.x, 0)
var grid_end_ver := Vector2(grid_pos.x, Global.current_project.size.y)
var closest_point_ver := _get_closest_point_to_segment(
position, snap_distance, grid_start_ver, grid_end_ver
position, distance, grid_start_ver, grid_end_ver
)
# Snap to the closest point to the closest grid line
var horizontal_distance := (closest_point_hor - position).length()
@ -217,11 +217,11 @@ func _get_closest_point_to_grid(
func _get_closest_point_to_segment(
position: Vector2, distance: Vector2, s1: Vector2, s2: Vector2
position: Vector2, distance: float, s1: Vector2, s2: Vector2
) -> Vector2:
var test_line := (s2 - s1).rotated(deg2rad(90)).normalized()
var from_a := position - test_line * distance.length()
var from_b := position + test_line * distance.length()
var from_a := position - test_line * distance
var from_b := position + test_line * distance
var closest_point := Vector2.INF
if Geometry.segment_intersects_segment_2d(from_a, from_b, s1, s2):
closest_point = Geometry.get_closest_point_to_segment_2d(position, s1, s2)
@ -229,7 +229,7 @@ func _get_closest_point_to_segment(
func _snap_to_guide(
snap_to: Vector2, position: Vector2, distance: Vector2, s1: Vector2, s2: Vector2
snap_to: Vector2, position: Vector2, distance: float, s1: Vector2, s2: Vector2
) -> Vector2:
var closest_point := _get_closest_point_to_segment(position, distance, s1, s2)
if closest_point == Vector2.INF: # Is not close to a guide