From 07f373ef408f7701b8bfa543ab3c189465f1eed9 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Fri, 17 May 2024 14:18:16 +0200 Subject: rotation --- Objects/BuilderObject.gd | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) (limited to 'Objects/BuilderObject.gd') diff --git a/Objects/BuilderObject.gd b/Objects/BuilderObject.gd index 2be50da..91465bc 100644 --- a/Objects/BuilderObject.gd +++ b/Objects/BuilderObject.gd @@ -3,19 +3,28 @@ extends Node2D var is_colliding = false -var is_dragged = false +var is_dragging = false +var is_rotating = false var mouse_offset = Vector2(0,0) -var last_position = Vector2(0,0) +var last_position = null +var rotation_offset_angle = 0 +var last_rotation = 0 + +# todo: if is focused via click, show rotation picker in edge func _ready(): $CollisionShape.texture.width = $Area2D/CollisionShape2D.shape.size.x func _process(_delta): - if is_dragged: + if is_dragging: position = get_global_mouse_position() + mouse_offset + + if is_rotating: + var distance = position - get_global_mouse_position() + rotation = atan2(distance.y, distance.x) - rotation_offset_angle func _on_area_2d_input_event(_viewport, event: InputEvent, _shape_idx): @@ -30,16 +39,38 @@ func drag_start(): mouse_offset = position - get_global_mouse_position() last_position = position - is_dragged = true + is_dragging = true func drag_end(): - if is_dragged and is_colliding: - if last_position != Vector2(0,0): + if is_colliding: + if last_position: position = last_position else: queue_free() - is_dragged = false + is_dragging = false + + +func _on_rotate_gui_input(event: InputEvent): + get_viewport().set_input_as_handled() + + if event.is_action_pressed("rotate_start"): + rotate_start() + if event.is_action_released("rotate_start"): + rotate_end() + + +func rotate_start(): + var distance = position - get_global_mouse_position() + rotation_offset_angle = atan2(distance.y, distance.x) - rotation + last_rotation = rotation + is_rotating = true + +func rotate_end(): + if is_colliding: + rotation = last_rotation + + is_rotating = false func _on_area_2d_area_entered(_area): -- cgit v1.2.3