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, tool_context: SummerDayToolContext):
	if event is InputEventMouseButton:
		if event.button_index == BUTTON_LEFT:
			if event.is_pressed() and event.control and event.alt:
				initial_transform = tool_context.canvas_state.canvas_transform
				var canvas_center = tool_context.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, tool_context: SummerDayToolContext):
	var canvas_center = tool_context.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
	tool_context.canvas_state.canvas_transform = canvas_transform
