diff options
author | Daniel Weipert <git@mail.dweipert.de> | 2024-05-17 14:18:16 +0200 |
---|---|---|
committer | Daniel Weipert <git@mail.dweipert.de> | 2024-05-17 14:18:16 +0200 |
commit | 07f373ef408f7701b8bfa543ab3c189465f1eed9 (patch) | |
tree | 48d5dffb507ff29bbff940c1baae48c78bf3ebec | |
parent | f5593483ba906b66779cb4466a7f2c5b2ed004a8 (diff) |
rotation
-rw-r--r-- | Objects/BuilderObject.gd | 45 | ||||
-rw-r--r-- | Objects/BuilderObject.tscn | 12 | ||||
-rw-r--r-- | project.godot | 5 |
3 files changed, 54 insertions, 8 deletions
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): diff --git a/Objects/BuilderObject.tscn b/Objects/BuilderObject.tscn index 749ad2a..3d85ff3 100644 --- a/Objects/BuilderObject.tscn +++ b/Objects/BuilderObject.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=6 format=3 uid="uid://p0ay1mp7v772"] +[gd_scene load_steps=7 format=3 uid="uid://p0ay1mp7v772"] [ext_resource type="Script" path="res://Objects/BuilderObject.gd" id="1_wjk1m"] [ext_resource type="Texture2D" uid="uid://dfjrmruj2mk0n" path="res://Assets/wooden_bench/32x32/bench-wood.png" id="2_hvvj8"] +[ext_resource type="Texture2D" uid="uid://djasmoqj87h1r" path="res://icon.svg" id="3_ios8i"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_vqo4b"] size = Vector2(80, 64) @@ -33,6 +34,15 @@ texture = ExtResource("2_hvvj8") region_enabled = true region_rect = Rect2(2, 13, 60, 49) +[node name="Rotate" type="TextureRect" parent="."] +offset_left = -40.0 +offset_top = -32.0 +offset_right = 88.0 +offset_bottom = 96.0 +scale = Vector2(0.08, 0.08) +texture = ExtResource("3_ios8i") + [connection signal="area_entered" from="Area2D" to="." method="_on_area_2d_area_entered"] [connection signal="area_exited" from="Area2D" to="." method="_on_area_2d_area_exited"] [connection signal="input_event" from="Area2D" to="." method="_on_area_2d_input_event"] +[connection signal="gui_input" from="Rotate" to="." method="_on_rotate_gui_input"] diff --git a/project.godot b/project.godot index 8c671f6..88afd2d 100644 --- a/project.godot +++ b/project.godot @@ -28,6 +28,11 @@ drag_screen={ , Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":4,"position":Vector2(275, 14),"global_position":Vector2(279, 55),"factor":1.0,"button_index":3,"canceled":false,"pressed":true,"double_click":false,"script":null) ] } +rotate_start={ +"deadzone": 0.5, +"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":1,"position":Vector2(115, 18),"global_position":Vector2(119, 59),"factor":1.0,"button_index":1,"canceled":false,"pressed":true,"double_click":false,"script":null) +] +} [rendering] |