Merge pull request #19 from Nitwel/testing

Improve Interaction and Entities
This commit is contained in:
Nitwel 2023-11-14 00:52:33 +01:00 committed by GitHub
commit 6bac3640c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 93 additions and 53 deletions

View File

@ -1,13 +1,15 @@
extends StaticBody3D extends StaticBody3D
@export var entity_id = "switch.plug_printer_2" @export var entity_id = "switch.plug_printer_2"
@onready var sprite: AnimatedSprite3D = $Icon @export var color_off = Color(0.23, 0.23, 0.23)
@onready var animation: AnimationPlayer = $AnimationPlayer @export var color_on = Color(1.0, 0.85, 0.0)
@onready var shape = $CSGCombiner3D @onready var shape = $CSGCombiner3D
@onready var rod_top = $RodTop @onready var rod_top = $RodTop
@onready var rod_bottom = $RodBottom @onready var rod_bottom = $RodBottom
@onready var slider_knob = $Knob @onready var slider_knob = $Knob
var state = false var state = false
var brightness = 0 # 0-255
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
@ -20,20 +22,38 @@ func _ready():
set_state(new_state["state"] == "on") set_state(new_state["state"] == "on")
) )
func set_state(state: bool): func set_state(state: bool, brightness = null):
print("set_state ", state, brightness)
self.state = state self.state = state
self.brightness = brightness
if state: if state:
animation.play_backwards("light") if brightness == null:
shape.material_override.albedo_color = color_on
else: else:
animation.play("light") shape.material_override.albedo_color = color_off.lerp(color_on, brightness / 255.0)
else:
shape.material_override.albedo_color = color_off
func _on_click(event): func _on_click(event):
if event.target == self: if event.target == self:
HomeAdapters.adapter.set_state(entity_id, "on" if !state else "off") var attributes = {}
set_state(!state)
if !state && brightness != null:
attributes["brightness"] = int(brightness)
HomeAdapters.adapter.set_state(entity_id, "on" if !state else "off", attributes)
set_state(!state, brightness)
else: else:
_on_clickable_on_click(event) _on_clickable_on_click(event)
func _on_press_move(event):
if event.target != self:
_on_clickable_on_click(event)
func _on_request_completed(): func _on_request_completed():
pass pass
@ -51,4 +71,5 @@ func _on_clickable_on_click(event):
slider_knob.position = new_pos slider_knob.position = new_pos
HomeAdapters.adapter.set_state(entity_id, "on" if state else "off", {"brightness_pct": int(ratio * 100)}) HomeAdapters.adapter.set_state(entity_id, "on" if state else "off", {"brightness": int(ratio * 255)})
set_state(state, ratio * 255)

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=14 format=3 uid="uid://cw86rc42dv2d8"] [gd_scene load_steps=15 format=3 uid="uid://cw86rc42dv2d8"]
[ext_resource type="Script" path="res://content/entities/light/light.gd" id="1_ykxy3"] [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://b72vsbcvqqxg7" path="res://assets/materials/swich_on.png" id="2_6gn2e"]
@ -7,7 +7,7 @@
[ext_resource type="Material" uid="uid://vce66e7sbc3n" path="res://content/entities/light/light_on.tres" id="5_50gph"] [ext_resource type="Material" uid="uid://vce66e7sbc3n" path="res://content/entities/light/light_on.tres" id="5_50gph"]
[sub_resource type="SphereShape3D" id="SphereShape3D_ukj14"] [sub_resource type="SphereShape3D" id="SphereShape3D_ukj14"]
radius = 0.06 radius = 0.05
[sub_resource type="SpriteFrames" id="SpriteFrames_ldpuo"] [sub_resource type="SpriteFrames" id="SpriteFrames_ldpuo"]
animations = [{ animations = [{
@ -23,22 +23,6 @@ animations = [{
"speed": 5.0 "speed": 5.0
}] }]
[sub_resource type="Animation" id="Animation_7o31s"]
resource_name = "light"
length = 0.3
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("CSGCombiner3D:material_override:albedo_color")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0, 0.3),
"transitions": PackedFloat32Array(1, 1),
"update": 0,
"values": [Color(1, 0.85098, 0, 1), Color(0.231373, 0.231373, 0.231373, 1)]
}
[sub_resource type="Animation" id="Animation_afofi"] [sub_resource type="Animation" id="Animation_afofi"]
length = 0.001 length = 0.001
tracks/0/type = "value" tracks/0/type = "value"
@ -66,6 +50,22 @@ tracks/1/keys = {
"values": [Vector3(0, 0.0492394, -0.0903599)] "values": [Vector3(0, 0.0492394, -0.0903599)]
} }
[sub_resource type="Animation" id="Animation_7o31s"]
resource_name = "light"
length = 0.3
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("CSGCombiner3D:material_override:albedo_color")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0, 0.3),
"transitions": PackedFloat32Array(1, 1),
"update": 0,
"values": [Color(1, 0.85098, 0, 1), Color(0.231373, 0.231373, 0.231373, 1)]
}
[sub_resource type="AnimationLibrary" id="AnimationLibrary_8a76q"] [sub_resource type="AnimationLibrary" id="AnimationLibrary_8a76q"]
_data = { _data = {
"RESET": SubResource("Animation_afofi"), "RESET": SubResource("Animation_afofi"),
@ -73,18 +73,22 @@ _data = {
} }
[sub_resource type="CylinderMesh" id="CylinderMesh_j3pn3"] [sub_resource type="CylinderMesh" id="CylinderMesh_j3pn3"]
top_radius = 0.002 top_radius = 0.004
bottom_radius = 0.002 bottom_radius = 0.004
height = 0.1 height = 0.1
[sub_resource type="CylinderShape3D" id="CylinderShape3D_tysib"] [sub_resource type="CylinderShape3D" id="CylinderShape3D_tysib"]
height = 0.1 height = 0.1
radius = 0.006 radius = 0.01
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_13uml"]
albedo_color = Color(0.231373, 0.239216, 0.231373, 1)
[sub_resource type="CylinderMesh" id="CylinderMesh_s8215"] [sub_resource type="CylinderMesh" id="CylinderMesh_s8215"]
top_radius = 0.004 material = SubResource("StandardMaterial3D_13uml")
bottom_radius = 0.004 top_radius = 0.006
height = 0.002 bottom_radius = 0.006
height = 0.004
[node name="Light" type="StaticBody3D"] [node name="Light" type="StaticBody3D"]
script = ExtResource("1_ykxy3") script = ExtResource("1_ykxy3")

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:9a91a5382c57467807e4afe3a1d52a1ce762977812c0ed7822b73bf440f32b35 oid sha256:ebf5147aa65e1f7aad1fee303f502e0ffce7e8863187b0e2a68377f38a0f3997
size 263 size 248

View File

@ -6,11 +6,19 @@ extends StaticBody3D
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
var stateInfo = await HomeAdapters.adapter.get_state(entity_id) var stateInfo = await HomeAdapters.adapter.get_state(entity_id)
label.text = stateInfo["state"] set_text(stateInfo)
await HomeAdapters.adapter.watch_state(entity_id, func(new_state): await HomeAdapters.adapter.watch_state(entity_id, func(new_state):
label.text = new_state["state"] set_text(new_state)
) )
func _on_click(event): func set_text(stateInfo):
pass var text = stateInfo["state"]
if stateInfo["attributes"]["friendly_name"] != null:
text = stateInfo["attributes"]["friendly_name"] + "\n" + text
if stateInfo["attributes"].has("unit_of_measurement") && stateInfo["attributes"]["unit_of_measurement"] != null:
text += " " + stateInfo["attributes"]["unit_of_measurement"]
label.text = text

View File

@ -13,7 +13,11 @@ script = ExtResource("1_57ac8")
shape = SubResource("SphereShape3D_r20gc") shape = SubResource("SphereShape3D_r20gc")
[node name="Label" type="Label3D" parent="."] [node name="Label" type="Label3D" parent="."]
text = "some text" transform = Transform3D(0.4, 0, 0, 0, 0.4, 0, 0, 0, 0.4, 0, 0, 0)
pixel_size = 0.001
text = "some text
"
font_size = 80
[node name="Movable" type="Node" parent="."] [node name="Movable" type="Node" parent="."]
script = ExtResource("2_fpq5q") script = ExtResource("2_fpq5q")

View File

@ -6,11 +6,13 @@ var hit_node := Node3D.new()
func _on_grab_down(event): func _on_grab_down(event):
event.controller.add_child(hit_node) event.controller.add_child(hit_node)
hit_node.global_position = get_parent().global_position hit_node.global_transform = get_parent().global_transform
func _on_grab_move(event): func _on_grab_move(event):
get_parent().global_position = hit_node.global_position get_parent().global_transform = hit_node.global_transform
get_parent().global_rotation = hit_node.global_rotation
func _on_grab_up(event):
event.controller.remove_child(hit_node)
func _get_configuration_warnings() -> PackedStringArray: func _get_configuration_warnings() -> PackedStringArray:
var warnings := PackedStringArray() var warnings := PackedStringArray()

View File

@ -23,20 +23,19 @@ func _handle_move():
return return
var distance = ray.get_collision_point().distance_to(_click_point) var distance = ray.get_collision_point().distance_to(_click_point)
var collider = ray.get_collider()
if distance > 0.02: if _moved || distance > 0.02:
if _is_pressed: if _is_pressed:
_call_fn(collider, "_on_press_move") _call_fn(_last_collided, "_on_press_move")
_moved = true _moved = true
if _is_grabbed: if _is_grabbed:
_call_fn(collider, "_on_grab_move") _call_fn(_last_collided, "_on_grab_move")
_moved = true _moved = true
func _handle_enter_leave(): func _handle_enter_leave():
var collider = ray.get_collider() var collider = ray.get_collider()
if collider == _last_collided: if collider == _last_collided || _is_grabbed || _is_pressed:
return return
_call_fn(collider, "_on_ray_enter") _call_fn(collider, "_on_ray_enter")
@ -61,22 +60,20 @@ func _on_button_pressed(button):
_call_fn(collider, "_on_grab_down") _call_fn(collider, "_on_grab_down")
func _on_button_released(button): func _on_button_released(button):
var collider = ray.get_collider() if _last_collided == null:
if collider == null:
return return
match button: match button:
"trigger_click": "trigger_click":
if _is_pressed: if _is_pressed:
if _moved == false: if _moved == false:
_call_fn(collider, "_on_click") _call_fn(_last_collided, "_on_click")
_call_fn(collider, "_on_press_up") _call_fn(_last_collided, "_on_press_up")
_is_pressed = false _is_pressed = false
_moved = false _moved = false
"grip_click": "grip_click":
if _is_grabbed: if _is_grabbed:
_call_fn(collider, "_on_grab_up") _call_fn(_last_collided, "_on_grab_up")
_is_grabbed = false _is_grabbed = false
_moved = false _moved = false

View File

@ -7,6 +7,10 @@ var request_timeout := 10.0
var url := "ws://192.168.33.33:8123/api/websocket" var url := "ws://192.168.33.33:8123/api/websocket"
var token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIzZjQ0ZGM2N2Y3YzY0MDc1OGZlMWI2ZjJlNmIxZjRkNSIsImlhdCI6MTY5ODAxMDcyOCwiZXhwIjoyMDEzMzcwNzI4fQ.K6ydLUC-4Q7BNIRCU1nWlI2s6sg9UCiOu-Lpedw2zJc" var token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIzZjQ0ZGM2N2Y3YzY0MDc1OGZlMWI2ZjJlNmIxZjRkNSIsImlhdCI6MTY5ODAxMDcyOCwiZXhwIjoyMDEzMzcwNzI4fQ.K6ydLUC-4Q7BNIRCU1nWlI2s6sg9UCiOu-Lpedw2zJc"
# var url := "wss://8ybjhqcinfcdyvzu.myfritz.net:8123/api/websocket"
# var token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjNjU0ZDE3NDc2ZGM0NzU1OGY5NjIzMmM5ZjdjYzE2YSIsImlhdCI6MTY5OTgyMzcxOCwiZXhwIjoyMDE1MTgzNzE4fQ.XHlfnXUd16HaV9XjYrxzuNg23nFFeoEsIsaMVXwRkd8"
var LOG_MESSAGES := false var LOG_MESSAGES := false
var authenticated := false var authenticated := false