2021-11-25 14:48:30 +02:00
|
|
|
class_name Guide
|
|
|
|
extends Line2D
|
2019-11-21 00:11:21 +02:00
|
|
|
|
2021-11-25 14:48:30 +02:00
|
|
|
enum Types { HORIZONTAL, VERTICAL }
|
2019-11-21 00:11:21 +02:00
|
|
|
|
2020-05-05 03:53:58 +03:00
|
|
|
var font := preload("res://assets/fonts/Roboto-Regular.tres")
|
2019-11-21 00:11:21 +02:00
|
|
|
var has_focus := true
|
|
|
|
var mouse_pos := Vector2.ZERO
|
2020-02-11 00:06:24 +02:00
|
|
|
var type = Types.HORIZONTAL
|
2020-07-16 04:25:59 +03:00
|
|
|
var project = Global.current_project
|
2019-11-21 00:11:21 +02:00
|
|
|
|
2020-05-01 20:47:10 +03:00
|
|
|
|
2019-11-21 00:11:21 +02:00
|
|
|
func _ready() -> void:
|
2021-08-31 03:00:04 +05:00
|
|
|
width = Global.camera.zoom.x * 2
|
2019-12-27 02:28:36 +02:00
|
|
|
default_color = Global.guide_color
|
2020-07-16 04:25:59 +03:00
|
|
|
project.guides.append(self)
|
2021-11-16 18:48:21 -05:00
|
|
|
if outside_canvas():
|
|
|
|
modulate.a = 0.5
|
2019-11-21 00:11:21 +02:00
|
|
|
|
2020-04-20 18:52:05 +03:00
|
|
|
|
2021-11-25 14:48:30 +02:00
|
|
|
func _input(_event: InputEvent) -> void:
|
2021-11-22 21:32:39 +02:00
|
|
|
if !visible:
|
|
|
|
return
|
2021-09-27 01:30:53 +03:00
|
|
|
var tmp_transform = get_canvas_transform().affine_inverse()
|
|
|
|
var tmp_position = Global.main_viewport.get_local_mouse_position()
|
|
|
|
mouse_pos = tmp_transform.basis_xform(tmp_position) + tmp_transform.origin
|
|
|
|
|
2019-11-21 00:11:21 +02:00
|
|
|
var point0 := points[0]
|
|
|
|
var point1 := points[1]
|
2020-02-11 00:06:24 +02:00
|
|
|
if type == Types.HORIZONTAL:
|
2019-11-21 00:11:21 +02:00
|
|
|
point0.y -= width * 3
|
|
|
|
point1.y += width * 3
|
|
|
|
else:
|
|
|
|
point0.x -= width * 3
|
|
|
|
point1.x += width * 3
|
2021-11-25 14:48:30 +02:00
|
|
|
if (
|
|
|
|
Global.can_draw
|
|
|
|
and Global.has_focus
|
|
|
|
and point_in_rectangle(mouse_pos, point0, point1)
|
|
|
|
and Input.is_action_just_pressed("left_mouse")
|
|
|
|
):
|
2021-01-06 16:11:50 +01:00
|
|
|
if !point_in_rectangle(Global.canvas.current_pixel, Vector2.ZERO, project.size):
|
2019-11-21 00:11:21 +02:00
|
|
|
has_focus = true
|
|
|
|
Global.has_focus = false
|
|
|
|
update()
|
2021-11-22 21:32:39 +02:00
|
|
|
if has_focus:
|
2019-11-23 01:02:53 +02:00
|
|
|
if Input.is_action_pressed("left_mouse"):
|
2020-02-11 00:06:24 +02:00
|
|
|
if type == Types.HORIZONTAL:
|
2020-09-30 13:12:01 +03:00
|
|
|
var yy = stepify(mouse_pos.y, 0.5)
|
|
|
|
points[0].y = yy
|
|
|
|
points[1].y = yy
|
2019-11-21 00:11:21 +02:00
|
|
|
else:
|
2020-09-30 13:12:01 +03:00
|
|
|
var xx = stepify(mouse_pos.x, 0.5)
|
|
|
|
points[0].x = xx
|
|
|
|
points[1].x = xx
|
2021-11-16 18:48:21 -05:00
|
|
|
if outside_canvas():
|
|
|
|
modulate.a = 0.5
|
|
|
|
else:
|
|
|
|
modulate.a = 1
|
2019-11-23 01:02:53 +02:00
|
|
|
if Input.is_action_just_released("left_mouse"):
|
|
|
|
Global.has_focus = true
|
|
|
|
has_focus = false
|
2021-11-16 18:48:21 -05:00
|
|
|
if outside_canvas():
|
|
|
|
project.guides.erase(self)
|
|
|
|
queue_free()
|
|
|
|
else:
|
2019-11-23 01:02:53 +02:00
|
|
|
update()
|
|
|
|
|
2019-11-21 00:11:21 +02:00
|
|
|
|
|
|
|
func _draw() -> void:
|
|
|
|
if has_focus:
|
2019-12-27 19:00:24 -03:00
|
|
|
var viewport_size: Vector2 = Global.main_viewport.rect_size
|
|
|
|
var zoom: Vector2 = Global.camera.zoom
|
2021-11-13 13:43:37 -05:00
|
|
|
|
|
|
|
# viewport_poly is an array of the points that make up the corners of the viewport
|
2021-11-25 14:48:30 +02:00
|
|
|
var viewport_poly := [
|
|
|
|
Vector2.ZERO, Vector2(viewport_size.x, 0), viewport_size, Vector2(0, viewport_size.y)
|
|
|
|
]
|
2021-11-13 13:43:37 -05:00
|
|
|
# Adjusting viewport_poly to take into account the camera offset, zoom, and rotation
|
|
|
|
for p in range(viewport_poly.size()):
|
2021-11-25 14:48:30 +02:00
|
|
|
viewport_poly[p] = (
|
|
|
|
viewport_poly[p].rotated(Global.camera.rotation) * zoom
|
|
|
|
+ Vector2(
|
|
|
|
(
|
|
|
|
Global.camera.offset.x
|
|
|
|
- (viewport_size.rotated(Global.camera.rotation).x / 2) * zoom.x
|
|
|
|
),
|
|
|
|
(
|
|
|
|
Global.camera.offset.y
|
|
|
|
- (viewport_size.rotated(Global.camera.rotation).y / 2) * zoom.y
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
var string := (
|
|
|
|
"%spx"
|
|
|
|
% str(stepify(mouse_pos.y if type == Types.HORIZONTAL else mouse_pos.x, 0.5))
|
|
|
|
)
|
2021-11-16 18:48:21 -05:00
|
|
|
var color: Color = Global.control.theme.get_color("font_color", "Label")
|
2021-11-13 13:43:37 -05:00
|
|
|
# X and Y offsets for nicer looking spacing
|
|
|
|
var x_offset := 5
|
2021-11-25 14:48:30 +02:00
|
|
|
var y_offset := -7 # Only used where the string is above the guide
|
2021-11-13 13:43:37 -05:00
|
|
|
|
2021-11-16 00:04:50 +02:00
|
|
|
# Draw the string where the guide intersects with the viewport poly
|
2021-11-13 13:43:37 -05:00
|
|
|
# Priority is top edge, then left, then right
|
2021-11-25 14:48:30 +02:00
|
|
|
var intersection = Geometry.segment_intersects_segment_2d(
|
|
|
|
points[0], points[1], viewport_poly[0], viewport_poly[1]
|
|
|
|
)
|
2021-11-13 13:43:37 -05:00
|
|
|
if intersection:
|
|
|
|
draw_set_transform(intersection, Global.camera.rotation, zoom * 2)
|
2021-11-25 14:48:30 +02:00
|
|
|
if (
|
|
|
|
intersection.distance_squared_to(viewport_poly[0])
|
|
|
|
< intersection.distance_squared_to(viewport_poly[1])
|
|
|
|
):
|
2021-11-16 18:48:21 -05:00
|
|
|
draw_string(font, Vector2(x_offset, font.get_height()), string, color)
|
2021-11-13 13:43:37 -05:00
|
|
|
else:
|
2021-11-25 14:48:30 +02:00
|
|
|
draw_string(
|
|
|
|
font,
|
|
|
|
Vector2(-font.get_string_size(string).x - x_offset, font.get_height()),
|
|
|
|
string,
|
|
|
|
color
|
|
|
|
)
|
2021-11-13 13:43:37 -05:00
|
|
|
return
|
2021-11-25 14:48:30 +02:00
|
|
|
intersection = Geometry.segment_intersects_segment_2d(
|
|
|
|
points[0], points[1], viewport_poly[3], viewport_poly[0]
|
|
|
|
)
|
2021-11-13 13:43:37 -05:00
|
|
|
if intersection:
|
|
|
|
draw_set_transform(intersection, Global.camera.rotation, zoom * 2)
|
2021-11-25 14:48:30 +02:00
|
|
|
if (
|
|
|
|
intersection.distance_squared_to(viewport_poly[3])
|
|
|
|
< intersection.distance_squared_to(viewport_poly[0])
|
|
|
|
):
|
2021-11-16 18:48:21 -05:00
|
|
|
draw_string(font, Vector2(x_offset, y_offset), string, color)
|
2021-11-13 13:43:37 -05:00
|
|
|
else:
|
2021-11-16 18:48:21 -05:00
|
|
|
draw_string(font, Vector2(x_offset, font.get_height()), string, color)
|
2021-11-13 13:43:37 -05:00
|
|
|
return
|
2021-11-25 14:48:30 +02:00
|
|
|
intersection = Geometry.segment_intersects_segment_2d(
|
|
|
|
points[0], points[1], viewport_poly[1], viewport_poly[2]
|
|
|
|
)
|
2021-11-13 13:43:37 -05:00
|
|
|
if intersection:
|
|
|
|
draw_set_transform(intersection, Global.camera.rotation, zoom * 2)
|
2021-11-25 14:48:30 +02:00
|
|
|
if (
|
|
|
|
intersection.distance_squared_to(viewport_poly[1])
|
|
|
|
< intersection.distance_squared_to(viewport_poly[2])
|
|
|
|
):
|
|
|
|
draw_string(
|
|
|
|
font,
|
|
|
|
Vector2(-font.get_string_size(string).x - x_offset, font.get_height()),
|
|
|
|
string,
|
|
|
|
color
|
|
|
|
)
|
2021-11-13 13:43:37 -05:00
|
|
|
else:
|
2021-11-25 14:48:30 +02:00
|
|
|
draw_string(
|
|
|
|
font,
|
|
|
|
Vector2(-font.get_string_size(string).x - x_offset, y_offset),
|
|
|
|
string,
|
|
|
|
color
|
|
|
|
)
|
2021-11-13 13:43:37 -05:00
|
|
|
return
|
|
|
|
|
|
|
|
# If there's no intersection with a viewport edge, show string in top left corner
|
|
|
|
draw_set_transform(viewport_poly[0], Global.camera.rotation, zoom * 2)
|
2021-11-16 18:48:21 -05:00
|
|
|
draw_string(font, Vector2(x_offset, font.get_height()), string, color)
|
2019-11-21 00:11:21 +02:00
|
|
|
|
2020-04-21 21:01:45 +03:00
|
|
|
|
|
|
|
func outside_canvas() -> bool:
|
2020-02-11 00:06:24 +02:00
|
|
|
if type == Types.HORIZONTAL:
|
2021-11-16 18:48:21 -05:00
|
|
|
return points[0].y < 0 || points[0].y > project.size.y
|
2019-11-23 01:02:53 +02:00
|
|
|
else:
|
2021-11-16 18:48:21 -05:00
|
|
|
return points[0].x < 0 || points[0].x > project.size.x
|
2019-11-23 01:02:53 +02:00
|
|
|
|
2020-04-21 21:01:45 +03:00
|
|
|
|
2021-11-25 14:48:30 +02:00
|
|
|
func point_in_rectangle(p: Vector2, coord1: Vector2, coord2: Vector2) -> bool:
|
2019-12-27 19:00:24 -03:00
|
|
|
return p.x > coord1.x && p.y > coord1.y && p.x < coord2.x && p.y < coord2.y
|