1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-19 09:39:48 +00:00
Pixelorama/Scripts/Guides.gd
OverloadedOrama 5613c3d7ef Overall code re-organizing & layer renaming changes
- When renaming a layer, the line edit grabs the focus. If enter is pressed, it gets invisible. Also fixed positioning and size
- Re-organized the variables in Global, now they are less messy and randomly placed
- Layer, frame & brush textures stretch mode has been changed to Keep Aspect Centered
2019-12-06 01:48:29 +02:00

86 lines
3 KiB
GDScript

extends Line2D
class_name Guide
enum TYPE {HORIZONTAL, VERTICAL}
var font := preload("res://Assets/Fonts/Roboto-Regular.tres")
var has_focus := true
var mouse_pos := Vector2.ZERO
var previous_points := points
var type = TYPE.HORIZONTAL
func _ready() -> void:
width = 0.1
# warning-ignore:unused_argument
func _process(delta : float) -> void:
width = Global.camera.zoom.x
mouse_pos = get_local_mouse_position()
var point0 := points[0]
var point1 := points[1]
if type == TYPE.HORIZONTAL:
point0.y -= width * 3
point1.y += width * 3
else:
point0.x -= width * 3
point1.x += width * 3
if point_in_rectangle(mouse_pos, point0, point1) && Input.is_action_just_pressed("left_mouse"):
if !point_in_rectangle(Global.canvas.current_pixel, Global.canvas.location, Global.canvas.location + Global.canvas.size):
has_focus = true
Global.has_focus = false
update()
if has_focus:
if Input.is_action_just_pressed("left_mouse"):
previous_points = points
if Input.is_action_pressed("left_mouse"):
if type == TYPE.HORIZONTAL:
points[0].y = round(mouse_pos.y)
points[1].y = round(mouse_pos.y)
else:
points[0].x = round(mouse_pos.x)
points[1].x = round(mouse_pos.x)
if Input.is_action_just_released("left_mouse"):
Global.has_focus = true
has_focus = false
if !outside_canvas():
Global.undos += 1
Global.undo_redo.create_action("Move Guide")
Global.undo_redo.add_do_method(self, "outside_canvas")
Global.undo_redo.add_do_property(self, "points", points)
Global.undo_redo.add_undo_property(self, "points", previous_points)
Global.undo_redo.add_undo_method(self, "outside_canvas", true)
Global.undo_redo.commit_action()
update()
func _draw() -> void:
if has_focus:
var viewport_size := Global.main_viewport.rect_size
var zoom := Global.camera.zoom
if type == TYPE.HORIZONTAL:
draw_set_transform(Vector2(Global.camera.offset.x - (viewport_size.x / 2) * zoom.x, points[0].y + font.get_height() * zoom.x * 2), rotation, zoom * 2)
draw_string(font, Vector2.ZERO, "%spx" % str(round(mouse_pos.y)))
else:
draw_set_transform(Vector2(points[0].x + font.get_height() * zoom.y, Global.camera.offset.y - (viewport_size.y / 2.25) * zoom.y), rotation, zoom * 2)
draw_string(font, Vector2.ZERO, "%spx" % str(round(mouse_pos.x)))
func outside_canvas(undo := false) -> bool:
if undo:
Global.undos -= 1
Global.notification_label("Move Guide")
if Global.control.redone:
if Global.undos < Global.undo_redo.get_version(): #If we did undo and then redo
Global.undos = Global.undo_redo.get_version()
Global.notification_label("Move Guide")
if type == TYPE.HORIZONTAL:
if points[0].y < 0 || points[0].y > Global.canvas.size.y:
queue_free()
return true
else:
if points[0].x < 0 || points[0].x > Global.canvas.size.x:
queue_free()
return true
return false
func point_in_rectangle(p : Vector2, coord1 : Vector2, coord2 : Vector2) -> bool:
return p.x > coord1.x && p.y > coord1.y && p.x < coord2.x && p.y < coord2.y