diff --git a/content/entities/light/light.gd b/content/entities/light/light.gd index 1ee6b1f..e3a768b 100644 --- a/content/entities/light/light.gd +++ b/content/entities/light/light.gd @@ -1,13 +1,15 @@ extends StaticBody3D @export var entity_id = "switch.plug_printer_2" -@onready var sprite: AnimatedSprite3D = $Icon -@onready var animation: AnimationPlayer = $AnimationPlayer +@export var color_off = Color(0.23, 0.23, 0.23) +@export var color_on = Color(1.0, 0.85, 0.0) + @onready var shape = $CSGCombiner3D @onready var rod_top = $RodTop @onready var rod_bottom = $RodBottom @onready var slider_knob = $Knob var state = false +var brightness = 0 # 0-255 # Called when the node enters the scene tree for the first time. func _ready(): @@ -20,20 +22,38 @@ func _ready(): 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.brightness = brightness + if state: - animation.play_backwards("light") + if brightness == null: + shape.material_override.albedo_color = color_on + else: + shape.material_override.albedo_color = color_off.lerp(color_on, brightness / 255.0) else: - animation.play("light") + shape.material_override.albedo_color = color_off + + func _on_click(event): if event.target == self: - HomeAdapters.adapter.set_state(entity_id, "on" if !state else "off") - set_state(!state) + var attributes = {} + + 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: _on_clickable_on_click(event) +func _on_press_move(event): + if event.target != self: + _on_clickable_on_click(event) + + func _on_request_completed(): pass @@ -51,4 +71,5 @@ func _on_clickable_on_click(event): 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) diff --git a/content/entities/light/light.tscn b/content/entities/light/light.tscn index e3d21a6..a444e4a 100644 --- a/content/entities/light/light.tscn +++ b/content/entities/light/light.tscn @@ -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="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"] [sub_resource type="SphereShape3D" id="SphereShape3D_ukj14"] -radius = 0.06 +radius = 0.05 [sub_resource type="SpriteFrames" id="SpriteFrames_ldpuo"] animations = [{ @@ -73,18 +73,22 @@ _data = { } [sub_resource type="CylinderMesh" id="CylinderMesh_j3pn3"] -top_radius = 0.002 -bottom_radius = 0.002 +top_radius = 0.004 +bottom_radius = 0.004 height = 0.1 [sub_resource type="CylinderShape3D" id="CylinderShape3D_tysib"] 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"] -top_radius = 0.004 -bottom_radius = 0.004 -height = 0.002 +material = SubResource("StandardMaterial3D_13uml") +top_radius = 0.006 +bottom_radius = 0.006 +height = 0.004 [node name="Light" type="StaticBody3D"] script = ExtResource("1_ykxy3") diff --git a/content/entities/light/light_on.tres b/content/entities/light/light_on.tres index 88fc350..2a5fbb8 100644 --- a/content/entities/light/light_on.tres +++ b/content/entities/light/light_on.tres @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:9a91a5382c57467807e4afe3a1d52a1ce762977812c0ed7822b73bf440f32b35 -size 263 +oid sha256:ebf5147aa65e1f7aad1fee303f502e0ffce7e8863187b0e2a68377f38a0f3997 +size 248 diff --git a/content/entities/sensor/sensor.gd b/content/entities/sensor/sensor.gd index 78716bc..149d7bd 100644 --- a/content/entities/sensor/sensor.gd +++ b/content/entities/sensor/sensor.gd @@ -6,11 +6,19 @@ extends StaticBody3D # Called when the node enters the scene tree for the first time. func _ready(): 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): - label.text = new_state["state"] + set_text(new_state) ) -func _on_click(event): - pass \ No newline at end of file +func set_text(stateInfo): + 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 diff --git a/content/entities/sensor/sensor.tscn b/content/entities/sensor/sensor.tscn index 37ef248..c7b6b96 100644 --- a/content/entities/sensor/sensor.tscn +++ b/content/entities/sensor/sensor.tscn @@ -13,7 +13,11 @@ script = ExtResource("1_57ac8") shape = SubResource("SphereShape3D_r20gc") [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="."] script = ExtResource("2_fpq5q") diff --git a/content/functions/movable.gd b/content/functions/movable.gd index a729fe7..e0b1cad 100644 --- a/content/functions/movable.gd +++ b/content/functions/movable.gd @@ -6,11 +6,13 @@ var hit_node := Node3D.new() func _on_grab_down(event): 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): - get_parent().global_position = hit_node.global_position - get_parent().global_rotation = hit_node.global_rotation + get_parent().global_transform = hit_node.global_transform + +func _on_grab_up(event): + event.controller.remove_child(hit_node) func _get_configuration_warnings() -> PackedStringArray: var warnings := PackedStringArray() diff --git a/content/raycast.gd b/content/raycast.gd index 7ffae18..18dfbfd 100644 --- a/content/raycast.gd +++ b/content/raycast.gd @@ -60,22 +60,20 @@ func _on_button_pressed(button): _call_fn(collider, "_on_grab_down") func _on_button_released(button): - var collider = ray.get_collider() - - if collider == null: + if _last_collided == null: return match button: "trigger_click": if _is_pressed: if _moved == false: - _call_fn(collider, "_on_click") - _call_fn(collider, "_on_press_up") + _call_fn(_last_collided, "_on_click") + _call_fn(_last_collided, "_on_press_up") _is_pressed = false _moved = false "grip_click": if _is_grabbed: - _call_fn(collider, "_on_grab_up") + _call_fn(_last_collided, "_on_grab_up") _is_grabbed = false _moved = false