From 1fc6abb5f38cc99c0d2edc0e7a563ffc8546d2d1 Mon Sep 17 00:00:00 2001 From: Nitwel Date: Sun, 28 Jan 2024 17:30:01 +0100 Subject: [PATCH] add modes to light --- content/entities/light/light.gd | 38 +++++++++++++++++++++++++++++-- content/entities/light/light.tscn | 25 +++++++++++++++++++- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/content/entities/light/light.gd b/content/entities/light/light.gd index 7ceec8a..7535a73 100644 --- a/content/entities/light/light.gd +++ b/content/entities/light/light.gd @@ -10,6 +10,10 @@ const color_wheel_img := preload("res://assets/canvas.png") @onready var slider: Slider3D = $Slider @onready var color_wheel = $ColorWheel @onready var color_puck = $ColorWheel/Puck +@onready var modes = $Modes +@onready var mode_next = $Modes/Next +@onready var mode_before = $Modes/Previous +@onready var mode_label = $Modes/Label var state = true var brightness = 0 # 0-255 @@ -21,6 +25,36 @@ func _ready(): var stateInfo = await HomeApi.get_state(entity_id) set_state(stateInfo["state"] == "on") + if stateInfo.has("attributes") && stateInfo["attributes"].has("effect_list") && stateInfo["attributes"]["effect_list"].size() > 0: + mode_label.text = stateInfo["attributes"]["effect"] + + mode_next.on_button_down.connect(func(): + var index = stateInfo["attributes"]["effect_list"].find(stateInfo["attributes"]["effect"]) + if index == -1: + index = 0 + else: + index = (index + 1) % stateInfo["attributes"]["effect_list"].size() + + mode_label.text = stateInfo["attributes"]["effect_list"][index] + + HomeApi.set_state(entity_id, "on", {"effect": stateInfo["attributes"]["effect_list"][index]}) + ) + + mode_before.on_button_down.connect(func(): + var index = stateInfo["attributes"]["effect_list"].find(stateInfo["attributes"]["effect"]) + if index == -1: + index = 0 + else: + index = (index - 1) % stateInfo["attributes"]["effect_list"].size() + + mode_label.text = stateInfo["attributes"]["effect_list"][index] + + HomeApi.set_state(entity_id, "on", {"effect": stateInfo["attributes"]["effect_list"][index]}) + ) + else: + remove_child(modes) + + if stateInfo.has("attributes") && stateInfo["attributes"].has("supported_color_modes") && stateInfo["attributes"]["supported_color_modes"].has("rgb"): color_wheel.get_node("Clickable").on_press_down.connect(func(event: EventPointer): var target_point = color_wheel.to_local(event.ray.get_collision_point()) @@ -35,7 +69,7 @@ func _ready(): color_puck.position = Vector3(target_point.x, color_puck.position.y, target_point.z) var attributes = { - "rgb_color": [int(color.r * 255), int(color.g * 255), int(color.b * 255)] + "rgb_color": [int(color.r * 255), int(color.g * 255), int(color.b * 255)], } HomeApi.set_state(entity_id, "on", attributes) @@ -83,4 +117,4 @@ func _on_click(event): attributes["brightness"] = int(brightness) HomeApi.set_state(entity_id, "on" if !state else "off", attributes) - set_state(!state, brightness) \ No newline at end of file + set_state(!state, brightness) diff --git a/content/entities/light/light.tscn b/content/entities/light/light.tscn index 2cfb355..33f586e 100644 --- a/content/entities/light/light.tscn +++ b/content/entities/light/light.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=17 format=3 uid="uid://cw86rc42dv2d8"] +[gd_scene load_steps=18 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/functions/movable.gd" id="4_4sfxb"] @@ -7,6 +7,7 @@ [ext_resource type="PackedScene" uid="uid://pk5k1q8bx0rj" path="res://content/ui/components/slider/slider.tscn" id="6_mhjlm"] [ext_resource type="Texture2D" uid="uid://hy2f6is7qjyv" path="res://assets/canvas.png" id="7_ximu1"] [ext_resource type="Script" path="res://content/functions/clickable.gd" id="8_1sfll"] +[ext_resource type="PackedScene" uid="uid://bsjqdvkt0u87c" path="res://content/ui/components/button/button.tscn" id="8_nhcff"] [sub_resource type="SphereShape3D" id="SphereShape3D_ukj14"] radius = 0.05 @@ -59,6 +60,7 @@ bottom_radius = 0.08 height = 0.01 [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_1jd14"] +resource_local_to_scene = true [sub_resource type="CylinderMesh" id="CylinderMesh_x68ys"] top_radius = 0.01 @@ -68,6 +70,8 @@ height = 0.005 [node name="Light" type="StaticBody3D" groups=["entity"]] collision_mask = 0 script = ExtResource("1_ykxy3") +color_off = null +color_on = null [node name="CollisionShape3D" type="CollisionShape3D" parent="."] shape = SubResource("SphereShape3D_ukj14") @@ -128,3 +132,22 @@ script = ExtResource("8_1sfll") transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -4.65661e-10, 0.01, 0) material_override = SubResource("StandardMaterial3D_1jd14") mesh = SubResource("CylinderMesh_x68ys") + +[node name="Modes" type="Node3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.11, 0) + +[node name="Next" parent="Modes" instance=ExtResource("8_nhcff")] +transform = Transform3D(1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, -0.04, 0, 0) +label = "navigate_next" +icon = true + +[node name="Previous" parent="Modes" instance=ExtResource("8_nhcff")] +transform = Transform3D(1, 0, 0, 0, 2.98023e-08, -1, 0, 1, 2.98023e-08, -0.1, 0, 0) +label = "navigate_before" +icon = true + +[node name="Label" type="Label3D" parent="Modes"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.01, 0, 0) +pixel_size = 0.001 +text = "Default" +horizontal_alignment = 0