mirror of
https://github.com/Orama-Interactive/Pixelorama.git
synced 2025-01-31 15:39:49 +00:00
Make snapping distance change based on the zoom
Should work similarly to GIMP now
This commit is contained in:
parent
9cf82e7f71
commit
5c8cf56f8f
|
@ -138,7 +138,7 @@ var draw_pixel_grid := false
|
||||||
var show_rulers := true
|
var show_rulers := true
|
||||||
var show_guides := true
|
var show_guides := true
|
||||||
var show_mouse_guides := false
|
var show_mouse_guides := false
|
||||||
var snapping_distance := 10.0
|
var snapping_distance := 32.0
|
||||||
var snap_to_rectangular_grid := false
|
var snap_to_rectangular_grid := false
|
||||||
var snap_to_guides := false
|
var snap_to_guides := false
|
||||||
var snap_to_perspective_guides := false
|
var snap_to_perspective_guides := false
|
||||||
|
|
|
@ -122,7 +122,7 @@ func draw_preview() -> void:
|
||||||
|
|
||||||
|
|
||||||
func snap_position(position: Vector2) -> Vector2:
|
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:
|
if Global.snap_to_rectangular_grid:
|
||||||
var grid_size := Vector2(Global.grid_width, Global.grid_height)
|
var grid_size := Vector2(Global.grid_width, Global.grid_height)
|
||||||
var grid_offset := Vector2(Global.grid_offset_x, Global.grid_offset_y)
|
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):
|
if vec.distance_to(position) < grid_pos.distance_to(position):
|
||||||
grid_pos = vec
|
grid_pos = vec
|
||||||
|
|
||||||
var closest_point_grid := _get_closest_point_to_grid(position, snap_distance, grid_pos)
|
var grid_point := _get_closest_point_to_grid(position, snapping_distance, grid_pos)
|
||||||
if closest_point_grid != Vector2.INF:
|
if grid_point != Vector2.INF:
|
||||||
position = closest_point_grid.floor()
|
position = grid_point.floor()
|
||||||
|
|
||||||
var snap_to := Vector2.INF
|
var snap_to := Vector2.INF
|
||||||
if Global.snap_to_guides:
|
if Global.snap_to_guides:
|
||||||
|
@ -155,14 +155,15 @@ func snap_position(position: Vector2) -> Vector2:
|
||||||
continue
|
continue
|
||||||
var s1: Vector2 = guide.points[0]
|
var s1: Vector2 = guide.points[0]
|
||||||
var s2: Vector2 = guide.points[1]
|
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:
|
if snap == Vector2.INF:
|
||||||
continue
|
continue
|
||||||
snap_to = snap
|
snap_to = snap
|
||||||
|
|
||||||
if Global.snap_to_perspective_guides:
|
if Global.snap_to_perspective_guides:
|
||||||
for point in Global.current_project.vanishing_points:
|
for point in Global.current_project.vanishing_points:
|
||||||
if point.has("pos_x") and point.has("pos_y"): # Sanity check
|
if not (point.has("pos_x") and point.has("pos_y")): # Sanity check
|
||||||
|
continue
|
||||||
for i in point.lines.size():
|
for i in point.lines.size():
|
||||||
if point.lines[i].has("angle") and point.lines[i].has("length"): # Sanity check
|
if point.lines[i].has("angle") and point.lines[i].has("length"): # Sanity check
|
||||||
var angle: float = deg2rad(point.lines[i].angle)
|
var angle: float = deg2rad(point.lines[i].angle)
|
||||||
|
@ -170,7 +171,7 @@ func snap_position(position: Vector2) -> Vector2:
|
||||||
var start = Vector2(point.pos_x, point.pos_y)
|
var start = Vector2(point.pos_x, point.pos_y)
|
||||||
var s1: Vector2 = start
|
var s1: Vector2 = start
|
||||||
var s2 := s1 + Vector2(length * cos(angle), length * sin(angle))
|
var s2 := s1 + Vector2(length * cos(angle), length * sin(angle))
|
||||||
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:
|
if snap == Vector2.INF:
|
||||||
continue
|
continue
|
||||||
snap_to = snap
|
snap_to = snap
|
||||||
|
@ -180,10 +181,9 @@ func snap_position(position: Vector2) -> Vector2:
|
||||||
return position
|
return position
|
||||||
|
|
||||||
|
|
||||||
func _get_closest_point_to_grid(
|
func _get_closest_point_to_grid(position: Vector2, distance: float, grid_pos: Vector2) -> Vector2:
|
||||||
position: Vector2, snap_distance: Vector2, grid_pos: Vector2
|
|
||||||
) -> Vector2:
|
|
||||||
# If the cursor is close to the start/origin of a grid cell, snap to that
|
# 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 closest_point := Vector2.INF
|
||||||
var rect := Rect2()
|
var rect := Rect2()
|
||||||
rect.position = position - (snap_distance / 4.0)
|
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_start_hor := Vector2(0, grid_pos.y)
|
||||||
var grid_end_hor := Vector2(Global.current_project.size.x, 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(
|
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
|
# Look for a point close to a vertical grid line
|
||||||
var grid_start_ver := Vector2(grid_pos.x, 0)
|
var grid_start_ver := Vector2(grid_pos.x, 0)
|
||||||
var grid_end_ver := Vector2(grid_pos.x, Global.current_project.size.y)
|
var grid_end_ver := Vector2(grid_pos.x, Global.current_project.size.y)
|
||||||
var closest_point_ver := _get_closest_point_to_segment(
|
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
|
# Snap to the closest point to the closest grid line
|
||||||
var horizontal_distance := (closest_point_hor - position).length()
|
var horizontal_distance := (closest_point_hor - position).length()
|
||||||
|
@ -217,11 +217,11 @@ func _get_closest_point_to_grid(
|
||||||
|
|
||||||
|
|
||||||
func _get_closest_point_to_segment(
|
func _get_closest_point_to_segment(
|
||||||
position: Vector2, distance: Vector2, s1: Vector2, s2: Vector2
|
position: Vector2, distance: float, s1: Vector2, s2: Vector2
|
||||||
) -> Vector2:
|
) -> Vector2:
|
||||||
var test_line := (s2 - s1).rotated(deg2rad(90)).normalized()
|
var test_line := (s2 - s1).rotated(deg2rad(90)).normalized()
|
||||||
var from_a := position - test_line * distance.length()
|
var from_a := position - test_line * distance
|
||||||
var from_b := position + test_line * distance.length()
|
var from_b := position + test_line * distance
|
||||||
var closest_point := Vector2.INF
|
var closest_point := Vector2.INF
|
||||||
if Geometry.segment_intersects_segment_2d(from_a, from_b, s1, s2):
|
if Geometry.segment_intersects_segment_2d(from_a, from_b, s1, s2):
|
||||||
closest_point = Geometry.get_closest_point_to_segment_2d(position, 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(
|
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:
|
) -> Vector2:
|
||||||
var closest_point := _get_closest_point_to_segment(position, distance, s1, s2)
|
var closest_point := _get_closest_point_to_segment(position, distance, s1, s2)
|
||||||
if closest_point == Vector2.INF: # Is not close to a guide
|
if closest_point == Vector2.INF: # Is not close to a guide
|
||||||
|
|
Loading…
Reference in a new issue