2024-01-27 16:13:43 +02:00
|
|
|
extends Entity
|
|
|
|
|
2024-04-17 17:49:58 +03:00
|
|
|
const Entity = preload ("../entity.gd")
|
|
|
|
const color_wheel_img := preload ("res://assets/canvas.png")
|
2023-10-31 23:15:33 +02:00
|
|
|
|
2023-11-14 01:51:48 +02:00
|
|
|
@export var color_off = Color(0.23, 0.23, 0.23)
|
|
|
|
@export var color_on = Color(1.0, 0.85, 0.0)
|
|
|
|
|
2024-04-17 17:49:58 +03:00
|
|
|
@onready var lightbulb = $Lightbulb
|
2023-11-29 01:16:25 +02:00
|
|
|
@onready var slider: Slider3D = $Slider
|
2024-01-28 18:13:32 +02:00
|
|
|
@onready var color_wheel = $ColorWheel
|
|
|
|
@onready var color_puck = $ColorWheel/Puck
|
2024-01-28 18:30:01 +02:00
|
|
|
@onready var modes = $Modes
|
|
|
|
@onready var mode_next = $Modes/Next
|
|
|
|
@onready var mode_before = $Modes/Previous
|
|
|
|
@onready var mode_label = $Modes/Label
|
2023-11-29 01:16:25 +02:00
|
|
|
|
|
|
|
var state = true
|
2023-11-14 01:51:48 +02:00
|
|
|
var brightness = 0 # 0-255
|
2024-04-17 17:49:58 +03:00
|
|
|
var color = color_on
|
|
|
|
var color_supported = false
|
2023-10-31 23:15:33 +02:00
|
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
func _ready():
|
2024-01-27 16:13:43 +02:00
|
|
|
super()
|
2024-04-17 17:49:58 +03:00
|
|
|
|
|
|
|
icon.value = "lightbulb"
|
2023-11-24 02:36:31 +02:00
|
|
|
var stateInfo = await HomeApi.get_state(entity_id)
|
2024-04-17 17:49:58 +03:00
|
|
|
set_state(stateInfo["state"] == "on", stateInfo["attributes"])
|
2023-11-12 18:36:23 +02:00
|
|
|
|
2024-04-17 17:49:58 +03:00
|
|
|
if stateInfo.has("attributes")&&stateInfo["attributes"].has("effect_list")&&stateInfo["attributes"]["effect_list"].size() > 0:
|
|
|
|
if stateInfo["attributes"].has("effect")&&stateInfo["attributes"]["effect"] != null:
|
2024-01-29 13:37:25 +02:00
|
|
|
mode_label.text = stateInfo["attributes"]["effect"]
|
2024-01-28 18:30:01 +02:00
|
|
|
|
|
|
|
mode_next.on_button_down.connect(func():
|
2024-04-17 17:49:58 +03:00
|
|
|
var index=stateInfo["attributes"]["effect_list"].find(stateInfo["attributes"]["effect"])
|
|
|
|
if index == - 1:
|
|
|
|
index=0
|
2024-01-28 18:30:01 +02:00
|
|
|
else:
|
2024-04-17 17:49:58 +03:00
|
|
|
index=(index + 1) % stateInfo["attributes"]["effect_list"].size()
|
2024-01-28 18:30:01 +02:00
|
|
|
|
2024-04-17 17:49:58 +03:00
|
|
|
mode_label.text=stateInfo["attributes"]["effect_list"][index]
|
2024-01-28 18:30:01 +02:00
|
|
|
|
|
|
|
HomeApi.set_state(entity_id, "on", {"effect": stateInfo["attributes"]["effect_list"][index]})
|
|
|
|
)
|
|
|
|
|
|
|
|
mode_before.on_button_down.connect(func():
|
2024-04-17 17:49:58 +03:00
|
|
|
var index=stateInfo["attributes"]["effect_list"].find(stateInfo["attributes"]["effect"])
|
|
|
|
if index == - 1:
|
|
|
|
index=0
|
2024-01-28 18:30:01 +02:00
|
|
|
else:
|
2024-04-17 17:49:58 +03:00
|
|
|
index=(index - 1) % stateInfo["attributes"]["effect_list"].size()
|
2024-01-28 18:30:01 +02:00
|
|
|
|
2024-04-17 17:49:58 +03:00
|
|
|
mode_label.text=stateInfo["attributes"]["effect_list"][index]
|
2024-01-28 18:30:01 +02:00
|
|
|
|
|
|
|
HomeApi.set_state(entity_id, "on", {"effect": stateInfo["attributes"]["effect_list"][index]})
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
remove_child(modes)
|
|
|
|
|
2024-04-17 17:49:58 +03:00
|
|
|
if stateInfo.has("attributes")&&stateInfo["attributes"].has("supported_color_modes")&&stateInfo["attributes"]["supported_color_modes"].has("rgb"):
|
2024-01-28 18:13:32 +02:00
|
|
|
color_wheel.get_node("Clickable").on_press_down.connect(func(event: EventPointer):
|
2024-04-17 17:49:58 +03:00
|
|
|
var target_point=color_wheel.to_local(event.ray.get_collision_point())
|
2024-01-28 18:13:32 +02:00
|
|
|
|
2024-04-17 17:49:58 +03:00
|
|
|
var delta=Vector2(target_point.x, target_point.z) * (1.0 / 0.08)
|
2024-01-28 18:13:32 +02:00
|
|
|
if delta.length() > 1:
|
2024-04-17 17:49:58 +03:00
|
|
|
delta=delta.normalized()
|
|
|
|
|
|
|
|
print("delta", delta)
|
2024-01-28 18:13:32 +02:00
|
|
|
|
2024-04-17 17:49:58 +03:00
|
|
|
var color=color_wheel_img.get_image().get_pixel((delta.x * 0.5 + 0.5) * 1000, (delta.y * 0.5 + 0.5) * 1000)
|
2024-01-28 18:13:32 +02:00
|
|
|
|
2024-04-17 17:49:58 +03:00
|
|
|
print("color", color)
|
2024-01-28 18:13:32 +02:00
|
|
|
|
2024-04-17 17:49:58 +03:00
|
|
|
color_puck.material_override.albedo_color=color
|
|
|
|
color_puck.position=Vector3(target_point.x, color_puck.position.y, target_point.z)
|
|
|
|
|
|
|
|
var attributes={
|
2024-01-28 18:30:01 +02:00
|
|
|
"rgb_color": [int(color.r * 255), int(color.g * 255), int(color.b * 255)],
|
2024-01-28 18:13:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HomeApi.set_state(entity_id, "on", attributes)
|
2024-04-17 17:49:58 +03:00
|
|
|
set_state(state, attributes)
|
2024-01-28 18:13:32 +02:00
|
|
|
)
|
2024-04-17 17:49:58 +03:00
|
|
|
color_supported = true
|
2024-01-28 18:13:32 +02:00
|
|
|
else:
|
|
|
|
remove_child(color_wheel)
|
|
|
|
|
2023-11-24 02:36:31 +02:00
|
|
|
await HomeApi.watch_state(entity_id, func(new_state):
|
2023-11-12 18:36:23 +02:00
|
|
|
if (new_state["state"] == "on") == state:
|
|
|
|
return
|
2024-04-17 17:49:58 +03:00
|
|
|
set_state(new_state["state"] == "on", new_state["attributes"])
|
2023-11-12 18:36:23 +02:00
|
|
|
)
|
2023-10-31 23:15:33 +02:00
|
|
|
|
2023-11-29 01:16:25 +02:00
|
|
|
slider.on_value_changed.connect(func(new_value):
|
2024-04-17 17:49:58 +03:00
|
|
|
var value=new_value / 100 * 255
|
2023-11-29 01:16:25 +02:00
|
|
|
HomeApi.set_state(entity_id, "on" if state else "off", {"brightness": int(value)})
|
2024-04-17 17:49:58 +03:00
|
|
|
set_state(state, {"brightness": value})
|
2023-11-29 01:16:25 +02:00
|
|
|
)
|
|
|
|
|
2024-04-17 17:49:58 +03:00
|
|
|
func set_state(new_state: bool, attributes={}):
|
|
|
|
if state == false&&new_state == false:
|
2023-11-29 01:16:25 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
state = new_state
|
2024-04-17 17:49:58 +03:00
|
|
|
|
|
|
|
if attributes.has("brightness"):
|
|
|
|
brightness = attributes["brightness"]
|
|
|
|
|
|
|
|
if attributes.has("rgb_color")&&attributes["rgb_color"] != null:
|
|
|
|
color = Color(attributes["rgb_color"][0] / 255.0, attributes["rgb_color"][1] / 255.0, attributes["rgb_color"][2] / 255.0, 1)
|
|
|
|
|
|
|
|
var tween = create_tween()
|
|
|
|
|
|
|
|
var target_color = color_off
|
2023-11-14 01:51:48 +02:00
|
|
|
|
2023-11-12 18:36:23 +02:00
|
|
|
if state:
|
2023-11-14 01:51:48 +02:00
|
|
|
if brightness == null:
|
2024-04-17 17:49:58 +03:00
|
|
|
target_color = color if color_supported else color_on
|
2023-11-14 01:51:48 +02:00
|
|
|
else:
|
2024-04-17 17:49:58 +03:00
|
|
|
target_color = color_off.lerp(color if color_supported else color_on, brightness / 255.0)
|
2023-11-14 01:51:48 +02:00
|
|
|
|
2024-04-17 17:49:58 +03:00
|
|
|
icon_color.value = target_color
|
|
|
|
tween.tween_property(lightbulb, "material_override:albedo_color", target_color, 0.3)
|
2023-10-31 23:15:33 +02:00
|
|
|
|
2023-11-05 22:32:50 +02:00
|
|
|
func _on_click(event):
|
2023-11-12 18:36:23 +02:00
|
|
|
if event.target == self:
|
2023-11-14 01:51:48 +02:00
|
|
|
var attributes = {}
|
|
|
|
|
2024-04-17 17:49:58 +03:00
|
|
|
if !state&&brightness != null:
|
2023-11-14 01:51:48 +02:00
|
|
|
attributes["brightness"] = int(brightness)
|
|
|
|
|
2023-11-24 02:36:31 +02:00
|
|
|
HomeApi.set_state(entity_id, "on" if !state else "off", attributes)
|
2024-04-17 17:49:58 +03:00
|
|
|
set_state(!state, attributes)
|
2024-04-17 22:27:09 +03:00
|
|
|
|
|
|
|
func quick_action():
|
|
|
|
var attributes = {}
|
|
|
|
|
|
|
|
if !state&&brightness != null:
|
|
|
|
attributes["brightness"] = int(brightness)
|
|
|
|
|
|
|
|
HomeApi.set_state(entity_id, "on" if !state else "off", attributes)
|
|
|
|
set_state(!state, attributes)
|