SummerDay/addons/summer_day/tools/canvas/rotation.gd

42 lines
1.2 KiB
GDScript3
Raw Normal View History

2022-01-03 23:53:28 +01:00
tool
2021-11-27 11:27:42 +01:00
extends SummerDayTool
class_name SummerDayRotationCanvasTool
var rotation_activated := false
var initial_transform: Transform2D
var initial_angle: float
func _init():
priority = 1
modifier_layer = 2
2022-01-02 16:42:55 +01:00
func _check_for_use(event: InputEvent):
2021-11-27 11:27:42 +01:00
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT:
if event.is_pressed() and event.control and event.alt:
2022-01-02 16:42:55 +01:00
initial_transform = SummerDay.canvas_state.canvas_transform
var canvas_center = SummerDay.canvas_state.rect_size * 0.5
2021-11-27 11:27:42 +01:00
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
2022-01-02 16:42:55 +01:00
func _run(event: InputEvent):
var canvas_center = SummerDay.canvas_state.rect_size * 0.5
2021-11-27 11:27:42 +01:00
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
2022-01-02 16:42:55 +01:00
SummerDay.canvas_state.canvas_transform = canvas_transform