From 020890512387a9421d31c0068370a0054d94d193 Mon Sep 17 00:00:00 2001 From: Nitwel Date: Sun, 12 Nov 2023 17:36:23 +0100 Subject: [PATCH] add light model and brightness slider --- assets/design.afdesign | 4 +- assets/materials/interface.tres | 7 +- content/entities/light/light.gd | 47 ++++++++++-- content/entities/light/light.tscn | 109 ++++++++++++++++++++++++++- content/entities/light/light_on.tres | 3 + content/main.gd | 8 +- content/main.tscn | 2 +- lib/home_adapters/hass_ws/hass.gd | 2 +- 8 files changed, 163 insertions(+), 19 deletions(-) create mode 100644 content/entities/light/light_on.tres diff --git a/assets/design.afdesign b/assets/design.afdesign index 88cc857..bbf0e2e 100644 --- a/assets/design.afdesign +++ b/assets/design.afdesign @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:d9423ef5121871fe9f60a673f76728265d2618f318a14e73affb4257cb42f73d -size 3906893 +oid sha256:991ebe1738a0621dd536436b99a7b5149d10e6b185008ec533eb2c5ca79fec0c +size 3087839 diff --git a/assets/materials/interface.tres b/assets/materials/interface.tres index 34ab9af..3af2e71 100644 --- a/assets/materials/interface.tres +++ b/assets/materials/interface.tres @@ -1,4 +1,3 @@ -[gd_resource type="StandardMaterial3D" format=3 uid="uid://bertj8bp8b5l1"] - -[resource] -albedo_color = Color(0.286275, 0.309804, 0.439216, 1) +version https://git-lfs.github.com/spec/v1 +oid sha256:55d5f30db336a8f8f7633743c23e41077d5c1a1b900c475a6d3811746eba3d1b +size 141 diff --git a/content/entities/light/light.gd b/content/entities/light/light.gd index c46a3e4..1ee6b1f 100644 --- a/content/entities/light/light.gd +++ b/content/entities/light/light.gd @@ -2,22 +2,53 @@ extends StaticBody3D @export var entity_id = "switch.plug_printer_2" @onready var sprite: AnimatedSprite3D = $Icon +@onready var animation: AnimationPlayer = $AnimationPlayer +@onready var shape = $CSGCombiner3D +@onready var rod_top = $RodTop +@onready var rod_bottom = $RodBottom +@onready var slider_knob = $Knob +var state = false # Called when the node enters the scene tree for the first time. func _ready(): var stateInfo = await HomeAdapters.adapter.get_state(entity_id) - if stateInfo["state"] == "on": - sprite.set_frame(0) - else: - sprite.set_frame(1) + set_state(stateInfo["state"] == "on") + await HomeAdapters.adapter.watch_state(entity_id, func(new_state): + if (new_state["state"] == "on") == state: + return + set_state(new_state["state"] == "on") + ) + +func set_state(state: bool): + self.state = state + if state: + animation.play_backwards("light") + else: + animation.play("light") 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) + if event.target == self: + HomeAdapters.adapter.set_state(entity_id, "on" if !state else "off") + set_state(!state) else: - sprite.set_frame(0) + _on_clickable_on_click(event) func _on_request_completed(): pass + + +func _on_clickable_on_click(event): + var click_pos: Vector3 = to_local(event.ray.get_collision_point()) + var vec_bottom_to_top = rod_top.position - rod_bottom.position + + var length_click = click_pos.y - rod_bottom.position.y + var length_total = vec_bottom_to_top.y + + var ratio = length_click / length_total + + var new_pos = rod_bottom.position.lerp(rod_top.position, ratio) + + slider_knob.position = new_pos + + HomeAdapters.adapter.set_state(entity_id, "on" if state else "off", {"brightness_pct": int(ratio * 100)}) diff --git a/content/entities/light/light.tscn b/content/entities/light/light.tscn index ba9725e..e5b27b2 100644 --- a/content/entities/light/light.tscn +++ b/content/entities/light/light.tscn @@ -1,12 +1,13 @@ -[gd_scene load_steps=7 format=3 uid="uid://cw86rc42dv2d8"] +[gd_scene load_steps=14 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"] +[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"] -radius = 0.1 +radius = 0.06 [sub_resource type="SpriteFrames" id="SpriteFrames_ldpuo"] animations = [{ @@ -22,6 +23,69 @@ animations = [{ "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"] +length = 0.001 +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), +"transitions": PackedFloat32Array(1), +"update": 0, +"values": [Color(1, 0.85098, 0, 1)] +} +tracks/1/type = "value" +tracks/1/imported = false +tracks/1/enabled = true +tracks/1/path = NodePath("Knob:position") +tracks/1/interp = 1 +tracks/1/loop_wrap = true +tracks/1/keys = { +"times": PackedFloat32Array(0), +"transitions": PackedFloat32Array(1), +"update": 0, +"values": [Vector3(0, 0.0492394, -0.0903599)] +} + +[sub_resource type="AnimationLibrary" id="AnimationLibrary_8a76q"] +_data = { +"RESET": SubResource("Animation_afofi"), +"light": SubResource("Animation_7o31s") +} + +[sub_resource type="CylinderMesh" id="CylinderMesh_j3pn3"] +top_radius = 0.002 +bottom_radius = 0.002 +height = 0.1 + +[sub_resource type="CylinderShape3D" id="CylinderShape3D_tysib"] +height = 0.1 +radius = 0.006 + +[sub_resource type="CylinderMesh" id="CylinderMesh_s8215"] +top_radius = 0.004 +bottom_radius = 0.004 +height = 0.002 + [node name="Light" type="StaticBody3D"] script = ExtResource("1_ykxy3") @@ -29,9 +93,50 @@ script = ExtResource("1_ykxy3") shape = SubResource("SphereShape3D_ukj14") [node name="Icon" type="AnimatedSprite3D" parent="."] +visible = false pixel_size = 0.0005 billboard = 1 sprite_frames = SubResource("SpriteFrames_ldpuo") [node name="Movable" type="Node" parent="."] script = ExtResource("4_4sfxb") + +[node name="CSGCombiner3D" type="CSGCombiner3D" parent="."] +transform = Transform3D(1, 9.69949e-05, 0.000589194, -9.77749e-05, 0.999999, 0.00135802, -0.000589065, -0.00135808, 0.999999, 0, 0, 0) +material_override = ExtResource("5_50gph") + +[node name="CSGSphere3D" type="CSGSphere3D" parent="CSGCombiner3D"] +radius = 0.05 +radial_segments = 36 +rings = 12 + +[node name="CSGCylinder3D" type="CSGCylinder3D" parent="CSGCombiner3D"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.0485739, 0) +radius = 0.02 +height = 0.03 +sides = 36 + +[node name="AnimationPlayer" type="AnimationPlayer" parent="."] +libraries = { +"": SubResource("AnimationLibrary_8a76q") +} + +[node name="Rod" type="StaticBody3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -0.0903599) + +[node name="MeshInstance3D" type="MeshInstance3D" parent="Rod"] +mesh = SubResource("CylinderMesh_j3pn3") +skeleton = NodePath("../..") + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Rod"] +shape = SubResource("CylinderShape3D_tysib") + +[node name="Knob" type="MeshInstance3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.0492394, -0.0903599) +mesh = SubResource("CylinderMesh_s8215") + +[node name="RodBottom" type="Marker3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.0501399, -0.090675) + +[node name="RodTop" type="Marker3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.0498752, -0.090942) diff --git a/content/entities/light/light_on.tres b/content/entities/light/light_on.tres new file mode 100644 index 0000000..88fc350 --- /dev/null +++ b/content/entities/light/light_on.tres @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9a91a5382c57467807e4afe3a1d52a1ce762977812c0ed7822b73bf440f32b35 +size 263 diff --git a/content/main.gd b/content/main.gd index b58aa06..ce68db4 100644 --- a/content/main.gd +++ b/content/main.gd @@ -5,6 +5,8 @@ var sky_passthrough = preload("res://assets/materials/sky_passthrough.material") @onready var environment: WorldEnvironment = $WorldEnvironment @onready var camera: XRCamera3D = $XROrigin3D/XRCamera3D +@onready var controller_left = $XROrigin3D/XRControllerLeft +@onready var controller_right = $XROrigin3D/XRControllerRight @onready var house = $House func _ready(): @@ -26,7 +28,11 @@ func _process(delta): camera_basis.x = camera_basis.x.normalized() camera_basis.z = camera_basis.z.normalized() - camera.position += camera_basis * vector_key_mapping(KEY_D, KEY_A, KEY_S, KEY_W) * delta + var movement = camera_basis * vector_key_mapping(KEY_D, KEY_A, KEY_S, KEY_W) * delta + + camera.position += movement + controller_left.position += movement + controller_right.position += movement func vector_key_mapping(key_positive_x: int, key_negative_x: int, key_positive_y: int, key_negative_y: int): diff --git a/content/main.tscn b/content/main.tscn index 53c53cd..11ea585 100644 --- a/content/main.tscn +++ b/content/main.tscn @@ -81,4 +81,4 @@ shadow_enabled = true xr_origin = NodePath("../XROrigin3D") [node name="House" parent="." instance=ExtResource("8_qkrg7")] -transform = Transform3D(0.999999, -1.39634e-11, 0, 9.48053e-12, 1, 0, 0, 0, 1, -1.83323, 0.000545101, 0.477467) +transform = Transform3D(0.404247, 0.000180645, 0.914648, 0.00017221, 0.999999, -0.000273614, -0.914648, 0.00026812, 0.404247, -0.343479, 0.000110551, 1.91547) diff --git a/lib/home_adapters/hass_ws/hass.gd b/lib/home_adapters/hass_ws/hass.gd index 0197059..3d30bef 100644 --- a/lib/home_adapters/hass_ws/hass.gd +++ b/lib/home_adapters/hass_ws/hass.gd @@ -113,7 +113,7 @@ func start_subscriptions(): if packet.event.c[entity]["+"].has("s"): entities[entity]["state"] = packet.event.c[entity]["+"]["s"] if packet.event.c[entity]["+"].has("a"): - entities[entity]["attributes"].merge(packet.event.c[entity]["+"]["a"]) + entities[entity]["attributes"].merge(packet.event.c[entity]["+"]["a"], true) entitiy_callbacks.call_key(entity, [entities[entity]]) )