From c2e37bc8d8192689df051a25b9278164beb49611 Mon Sep 17 00:00:00 2001 From: Nitwel Date: Sun, 5 Nov 2023 21:32:50 +0100 Subject: [PATCH 1/2] make entities movable --- content/entities/light/light.gd | 2 +- content/entities/light/light.tscn | 6 ++++- content/entities/sensor/sensor.gd | 2 +- content/entities/switch/switch.gd | 2 +- content/functions/function.gd | 2 ++ content/functions/movable.gd | 40 +++++++++++++++++++++++++++++++ content/raycast.gd | 18 +++++++++++--- content/ui/device/device.gd | 2 +- content/ui/entity/entity.gd | 2 +- lib/home_adapters/hass_ws/hass.gd | 2 +- project.godot | 1 + 11 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 content/functions/function.gd create mode 100644 content/functions/movable.gd diff --git a/content/entities/light/light.gd b/content/entities/light/light.gd index 5f77c50..c46a3e4 100644 --- a/content/entities/light/light.gd +++ b/content/entities/light/light.gd @@ -12,7 +12,7 @@ func _ready(): sprite.set_frame(1) -func _on_toggle(): +func _on_click(event): HomeAdapters.adapter.set_state(entity_id, "off" if sprite.get_frame() == 0 else "on") if sprite.get_frame() == 0: sprite.set_frame(1) diff --git a/content/entities/light/light.tscn b/content/entities/light/light.tscn index c8ee172..ba9725e 100644 --- a/content/entities/light/light.tscn +++ b/content/entities/light/light.tscn @@ -1,8 +1,9 @@ -[gd_scene load_steps=6 format=3 uid="uid://cw86rc42dv2d8"] +[gd_scene load_steps=7 format=3 uid="uid://cw86rc42dv2d8"] [ext_resource type="Script" path="res://content/entities/light/light.gd" id="1_ykxy3"] [ext_resource type="Texture2D" uid="uid://b72vsbcvqqxg7" path="res://assets/materials/swich_on.png" id="2_6gn2e"] [ext_resource type="Texture2D" uid="uid://cvc0o6dsktnvl" path="res://assets/materials/switch_off.png" id="3_qlm62"] +[ext_resource type="Script" path="res://content/functions/movable.gd" id="4_4sfxb"] [sub_resource type="SphereShape3D" id="SphereShape3D_ukj14"] radius = 0.1 @@ -31,3 +32,6 @@ shape = SubResource("SphereShape3D_ukj14") pixel_size = 0.0005 billboard = 1 sprite_frames = SubResource("SpriteFrames_ldpuo") + +[node name="Movable" type="Node" parent="."] +script = ExtResource("4_4sfxb") diff --git a/content/entities/sensor/sensor.gd b/content/entities/sensor/sensor.gd index f3da193..78716bc 100644 --- a/content/entities/sensor/sensor.gd +++ b/content/entities/sensor/sensor.gd @@ -12,5 +12,5 @@ func _ready(): label.text = new_state["state"] ) -func _on_toggle(): +func _on_click(event): pass \ No newline at end of file diff --git a/content/entities/switch/switch.gd b/content/entities/switch/switch.gd index 7c41724..3239121 100644 --- a/content/entities/switch/switch.gd +++ b/content/entities/switch/switch.gd @@ -22,7 +22,7 @@ func _ready(): ) -func _on_toggle(): +func _on_click(event): HomeAdapters.adapter.set_state(entity_id, "off" if sprite.get_frame() == 0 else "on") if sprite.get_frame() == 0: sprite.set_frame(1) diff --git a/content/functions/function.gd b/content/functions/function.gd new file mode 100644 index 0000000..41e24be --- /dev/null +++ b/content/functions/function.gd @@ -0,0 +1,2 @@ +extends Node +class_name Function diff --git a/content/functions/movable.gd b/content/functions/movable.gd new file mode 100644 index 0000000..0f022b2 --- /dev/null +++ b/content/functions/movable.gd @@ -0,0 +1,40 @@ +@tool +extends Function +class_name Movable + +var start_pos:Vector3 +var start_rot:Vector3 + +var grab_pos:Vector3 +var grab_rot:Vector3 + +func _on_grab_down(event): + print("grab down movable") + start_pos = get_parent().position + start_rot = get_parent().rotation + + grab_pos = event.controller.position + grab_rot = event.controller.rotation + + +func _on_grab_move(event): + print("grab move movable") + var delta_pos = event.controller.position - grab_pos + var delta_rot = event.controller.rotation - grab_rot + + print(delta_pos, delta_rot) + + get_parent().position = start_pos + delta_pos + get_parent().rotation = start_rot + delta_rot + +func _on_grab_up(event): + print("grab up movable") + +func _get_configuration_warnings() -> PackedStringArray: + var warnings := PackedStringArray() + + if get_parent() is StaticBody3D == false: + warnings.append("Movable requires a StaticBody3D as parent.") + + + return warnings \ No newline at end of file diff --git a/content/raycast.gd b/content/raycast.gd index d8b634e..6d7d3bb 100644 --- a/content/raycast.gd +++ b/content/raycast.gd @@ -25,10 +25,15 @@ func _get_event_data(): } func _handle_move(): + if _is_pressed == false && _is_grabbed == false: + return + var distance = ray.get_collision_point().distance_to(_click_point) var collider = ray.get_collider() - if distance > 0.01: + print(distance) + + if distance > 0.02: if _is_pressed: _call_fn(collider, "_on_press_move") _moved = true @@ -84,5 +89,12 @@ func _on_button_released(button): _moved = false func _call_fn(collider: Object, fn_name: String): - if collider != null && collider.has_method(fn_name): - collider.call(fn_name, _get_event_data()) + if collider != null: + if collider.has_method(fn_name): + collider.call(fn_name, _get_event_data()) + + for child in collider.get_children(): + print("child", child) + if child is Function: + print("child is function!") + _call_fn(child, fn_name) diff --git a/content/ui/device/device.gd b/content/ui/device/device.gd index e11ab14..dba05ab 100644 --- a/content/ui/device/device.gd +++ b/content/ui/device/device.gd @@ -5,7 +5,7 @@ extends StaticBody3D signal click(id: String) -func _on_toggle(): +func _on_click(event): click.emit(id) func set_device_name(text): diff --git a/content/ui/entity/entity.gd b/content/ui/entity/entity.gd index e324331..aa7a3ad 100644 --- a/content/ui/entity/entity.gd +++ b/content/ui/entity/entity.gd @@ -5,7 +5,7 @@ extends StaticBody3D signal click(name: String) -func _on_toggle(): +func _on_click(event): click.emit(text) func set_entity_name(text): diff --git a/lib/home_adapters/hass_ws/hass.gd b/lib/home_adapters/hass_ws/hass.gd index 60e80cf..0197059 100644 --- a/lib/home_adapters/hass_ws/hass.gd +++ b/lib/home_adapters/hass_ws/hass.gd @@ -7,7 +7,7 @@ var request_timeout := 10.0 var url := "ws://192.168.33.33:8123/api/websocket" var token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIzZjQ0ZGM2N2Y3YzY0MDc1OGZlMWI2ZjJlNmIxZjRkNSIsImlhdCI6MTY5ODAxMDcyOCwiZXhwIjoyMDEzMzcwNzI4fQ.K6ydLUC-4Q7BNIRCU1nWlI2s6sg9UCiOu-Lpedw2zJc" -var LOG_MESSAGES := true +var LOG_MESSAGES := false var authenticated := false var loading := true diff --git a/project.godot b/project.godot index 79aa4f7..232d0bb 100644 --- a/project.godot +++ b/project.godot @@ -31,6 +31,7 @@ renderer/rendering_method="mobile" textures/vram_compression/import_etc2_astc=true lights_and_shadows/directional_shadow/soft_shadow_filter_quality=4 lights_and_shadows/directional_shadow/soft_shadow_filter_quality.mobile=4 +anti_aliasing/quality/msaa_3d=2 [xr] From 6985c7585de14d8baf3d9657287198a336d52d7d Mon Sep 17 00:00:00 2001 From: Nitwel Date: Mon, 6 Nov 2023 15:39:53 +0100 Subject: [PATCH 2/2] implement movable entities --- assets/design.afdesign | 4 ++-- content/entities/sensor/sensor.tscn | 6 +++++- content/entities/switch/switch.tscn | 6 +++++- content/functions/movable.gd | 30 ++++++----------------------- content/main.tscn | 2 +- content/raycast.gd | 4 ---- project.godot | 1 - 7 files changed, 19 insertions(+), 34 deletions(-) diff --git a/assets/design.afdesign b/assets/design.afdesign index c0044da..5e102a7 100644 --- a/assets/design.afdesign +++ b/assets/design.afdesign @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:065527a0fe822f47a1f45bed61a0dcca10f73004f9c012fb3539021ca6bd2fbd -size 2451944 +oid sha256:034fb07ad84872c283141a5f2ae34989b349cdf77328cab5458a9263f58e4967 +size 2984138 diff --git a/content/entities/sensor/sensor.tscn b/content/entities/sensor/sensor.tscn index 0cf6030..37ef248 100644 --- a/content/entities/sensor/sensor.tscn +++ b/content/entities/sensor/sensor.tscn @@ -1,6 +1,7 @@ -[gd_scene load_steps=3 format=3 uid="uid://xsiy71rsqulj"] +[gd_scene load_steps=4 format=3 uid="uid://xsiy71rsqulj"] [ext_resource type="Script" path="res://content/entities/sensor/sensor.gd" id="1_57ac8"] +[ext_resource type="Script" path="res://content/functions/movable.gd" id="2_fpq5q"] [sub_resource type="SphereShape3D" id="SphereShape3D_r20gc"] radius = 0.1 @@ -13,3 +14,6 @@ shape = SubResource("SphereShape3D_r20gc") [node name="Label" type="Label3D" parent="."] text = "some text" + +[node name="Movable" type="Node" parent="."] +script = ExtResource("2_fpq5q") diff --git a/content/entities/switch/switch.tscn b/content/entities/switch/switch.tscn index 4babc8f..a6cedf4 100644 --- a/content/entities/switch/switch.tscn +++ b/content/entities/switch/switch.tscn @@ -1,8 +1,9 @@ -[gd_scene load_steps=6 format=3 uid="uid://cscl5k7lhopj5"] +[gd_scene load_steps=7 format=3 uid="uid://cscl5k7lhopj5"] [ext_resource type="Script" path="res://content/entities/switch/switch.gd" id="1_8ffhi"] [ext_resource type="Texture2D" uid="uid://b72vsbcvqqxg7" path="res://assets/materials/swich_on.png" id="1_w68gw"] [ext_resource type="Texture2D" uid="uid://cvc0o6dsktnvl" path="res://assets/materials/switch_off.png" id="2_86ba1"] +[ext_resource type="Script" path="res://content/functions/movable.gd" id="4_6xr03"] [sub_resource type="SphereShape3D" id="SphereShape3D_ukj14"] radius = 0.1 @@ -31,3 +32,6 @@ shape = SubResource("SphereShape3D_ukj14") pixel_size = 0.0005 billboard = 1 sprite_frames = SubResource("SpriteFrames_ldpuo") + +[node name="Movable" type="Node" parent="."] +script = ExtResource("4_6xr03") diff --git a/content/functions/movable.gd b/content/functions/movable.gd index 0f022b2..a729fe7 100644 --- a/content/functions/movable.gd +++ b/content/functions/movable.gd @@ -2,33 +2,15 @@ extends Function class_name Movable -var start_pos:Vector3 -var start_rot:Vector3 - -var grab_pos:Vector3 -var grab_rot:Vector3 +var hit_node := Node3D.new() func _on_grab_down(event): - print("grab down movable") - start_pos = get_parent().position - start_rot = get_parent().rotation - - grab_pos = event.controller.position - grab_rot = event.controller.rotation - + event.controller.add_child(hit_node) + hit_node.global_position = get_parent().global_position func _on_grab_move(event): - print("grab move movable") - var delta_pos = event.controller.position - grab_pos - var delta_rot = event.controller.rotation - grab_rot - - print(delta_pos, delta_rot) - - get_parent().position = start_pos + delta_pos - get_parent().rotation = start_rot + delta_rot - -func _on_grab_up(event): - print("grab up movable") + get_parent().global_position = hit_node.global_position + get_parent().global_rotation = hit_node.global_rotation func _get_configuration_warnings() -> PackedStringArray: var warnings := PackedStringArray() @@ -37,4 +19,4 @@ func _get_configuration_warnings() -> PackedStringArray: warnings.append("Movable requires a StaticBody3D as parent.") - return warnings \ No newline at end of file + return warnings diff --git a/content/main.tscn b/content/main.tscn index 5a04593..1da5ecb 100644 --- a/content/main.tscn +++ b/content/main.tscn @@ -57,7 +57,7 @@ script = ExtResource("1_tsqxc") ray = NodePath("RayCast3D") [node name="RayCast3D" type="RayCast3D" parent="XROrigin3D/XRControllerRight/Raycast"] -transform = Transform3D(1.91069e-15, 4.37114e-08, 1, 1, -4.37114e-08, 0, 4.37114e-08, 1, -4.37114e-08, 0, 0, 0) +transform = Transform3D(-2.58078e-11, 4.3714e-08, 1, 1, -4.37117e-08, 9.27469e-12, 4.37112e-08, 1, -4.3714e-08, 0, 0, 0) target_position = Vector3(0, -5, 0) [node name="StartXR" parent="." instance=ExtResource("1_i4c04")] diff --git a/content/raycast.gd b/content/raycast.gd index 6d7d3bb..03a3c32 100644 --- a/content/raycast.gd +++ b/content/raycast.gd @@ -31,8 +31,6 @@ func _handle_move(): var distance = ray.get_collision_point().distance_to(_click_point) var collider = ray.get_collider() - print(distance) - if distance > 0.02: if _is_pressed: _call_fn(collider, "_on_press_move") @@ -94,7 +92,5 @@ func _call_fn(collider: Object, fn_name: String): collider.call(fn_name, _get_event_data()) for child in collider.get_children(): - print("child", child) if child is Function: - print("child is function!") _call_fn(child, fn_name) diff --git a/project.godot b/project.godot index 232d0bb..79aa4f7 100644 --- a/project.godot +++ b/project.godot @@ -31,7 +31,6 @@ renderer/rendering_method="mobile" textures/vram_compression/import_etc2_astc=true lights_and_shadows/directional_shadow/soft_shadow_filter_quality=4 lights_and_shadows/directional_shadow/soft_shadow_filter_quality.mobile=4 -anti_aliasing/quality/msaa_3d=2 [xr]