SummerDay/addons/summer_day/tools/canvas/rotation.gd
2022-01-03 23:53:28 +01:00

42 lines
1.2 KiB
GDScript

tool
extends SummerDayTool
class_name SummerDayRotationCanvasTool
var rotation_activated := false
var initial_transform: Transform2D
var initial_angle: float
func _init():
priority = 1
modifier_layer = 2
func _check_for_use(event: InputEvent):
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT:
if event.is_pressed() and event.control and event.alt:
initial_transform = SummerDay.canvas_state.canvas_transform
var canvas_center = SummerDay.canvas_state.rect_size * 0.5
initial_angle = event.position.angle_to_point(canvas_center)
rotation_activated = true
else:
rotation_activated = false
if event is InputEventMouseMotion:
return rotation_activated
return false
func _run(event: InputEvent):
var canvas_center = SummerDay.canvas_state.rect_size * 0.5
var canvas_transform = initial_transform
canvas_transform.origin -= canvas_center
var current_angle = event.position.angle_to_point(canvas_center)
var rotation_angle = current_angle - initial_angle
canvas_transform = canvas_transform.rotated(rotation_angle)
canvas_transform.origin += canvas_center
SummerDay.canvas_state.canvas_transform = canvas_transform