clean up light and switch
This commit is contained in:
parent
90cfe87ae8
commit
8d1a380977
|
@ -15,9 +15,9 @@ const color_wheel_img := preload ("res://assets/canvas.png")
|
|||
@onready var mode_before = $Modes/Previous
|
||||
@onready var mode_label = $Modes/Label
|
||||
|
||||
var state = true
|
||||
var brightness = 0 # 0-255
|
||||
var color = color_on
|
||||
var active = R.state(false)
|
||||
var brightness = R.state(0) # 0-255
|
||||
var color = R.state(color_on)
|
||||
var color_supported = false
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
|
@ -26,7 +26,7 @@ func _ready():
|
|||
|
||||
icon.value = "lightbulb"
|
||||
var stateInfo = await HomeApi.get_state(entity_id)
|
||||
set_state(stateInfo["state"] == "on", stateInfo["attributes"])
|
||||
set_state(stateInfo)
|
||||
|
||||
if stateInfo.has("attributes")&&stateInfo["attributes"].has("effect_list")&&stateInfo["attributes"]["effect_list"].size() > 0:
|
||||
if stateInfo["attributes"].has("effect")&&stateInfo["attributes"]["effect"] != null:
|
||||
|
@ -66,78 +66,69 @@ func _ready():
|
|||
if delta.length() > 1:
|
||||
delta=delta.normalized()
|
||||
|
||||
print("delta", delta)
|
||||
var picked_color=color_wheel_img.get_image().get_pixel((delta.x * 0.5 + 0.5) * 1000, (delta.y * 0.5 + 0.5) * 1000)
|
||||
|
||||
var color=color_wheel_img.get_image().get_pixel((delta.x * 0.5 + 0.5) * 1000, (delta.y * 0.5 + 0.5) * 1000)
|
||||
|
||||
print("color", color)
|
||||
|
||||
color_puck.material_override.albedo_color=color
|
||||
color_puck.material_override.albedo_color=picked_color
|
||||
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(picked_color.r * 255), int(picked_color.g * 255), int(picked_color.b * 255)],
|
||||
}
|
||||
|
||||
HomeApi.set_state(entity_id, "on", attributes)
|
||||
set_state(state, attributes)
|
||||
)
|
||||
color_supported = true
|
||||
else:
|
||||
remove_child(color_wheel)
|
||||
|
||||
await HomeApi.watch_state(entity_id, func(new_state):
|
||||
if (new_state["state"] == "on") == state:
|
||||
return
|
||||
set_state(new_state["state"] == "on", new_state["attributes"])
|
||||
set_state(new_state)
|
||||
)
|
||||
|
||||
slider.on_value_changed.connect(func(new_value):
|
||||
var value=new_value / 100 * 255
|
||||
HomeApi.set_state(entity_id, "on" if state else "off", {"brightness": int(value)})
|
||||
set_state(state, {"brightness": value})
|
||||
HomeApi.set_state(entity_id, "on" if active.value else "off", {"brightness": int(value)})
|
||||
)
|
||||
|
||||
func set_state(new_state: bool, attributes={}):
|
||||
if state == false&&new_state == false:
|
||||
func set_state(stateInfo):
|
||||
if active.value == false&&stateInfo["state"] == "off":
|
||||
return
|
||||
|
||||
state = new_state
|
||||
var attributes = stateInfo["attributes"]
|
||||
|
||||
if attributes.has("brightness"):
|
||||
brightness = attributes["brightness"]
|
||||
active.value = stateInfo["state"] == "on"
|
||||
|
||||
if attributes.has("brightness")&&attributes["brightness"] != null:
|
||||
brightness.value = attributes["brightness"]
|
||||
slider.value = attributes["brightness"] / 255.0 * 100
|
||||
|
||||
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)
|
||||
color.value = 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
|
||||
|
||||
if state:
|
||||
if brightness == null:
|
||||
target_color = color if color_supported else color_on
|
||||
if active.value:
|
||||
if brightness.value == null:
|
||||
target_color = color.value if color_supported else color_on
|
||||
else:
|
||||
target_color = color_off.lerp(color if color_supported else color_on, brightness / 255.0)
|
||||
target_color = color_off.lerp(color.value if color_supported else color_on, brightness.value / 255.0)
|
||||
|
||||
icon_color.value = target_color
|
||||
tween.tween_property(lightbulb, "material_override:albedo_color", target_color, 0.3)
|
||||
|
||||
func _on_click(event):
|
||||
if event.target == self:
|
||||
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)
|
||||
_toggle()
|
||||
|
||||
func quick_action():
|
||||
_toggle()
|
||||
|
||||
func _toggle():
|
||||
var attributes = {}
|
||||
|
||||
if !state&&brightness != null:
|
||||
attributes["brightness"] = int(brightness)
|
||||
if !active.value&&brightness.value != null:
|
||||
attributes["brightness"] = int(brightness.value)
|
||||
|
||||
HomeApi.set_state(entity_id, "on" if !state else "off", attributes)
|
||||
set_state(!state, attributes)
|
||||
HomeApi.set_state(entity_id, "off" if active.value else "on", attributes)
|
|
@ -4,42 +4,35 @@ const Entity = preload ("../entity.gd")
|
|||
|
||||
@onready var sprite: AnimatedSprite3D = $Icon
|
||||
|
||||
var active = R.state(false)
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
super()
|
||||
var stateInfo = await HomeApi.get_state(entity_id)
|
||||
|
||||
set_state(stateInfo)
|
||||
|
||||
await HomeApi.watch_state(entity_id, func(new_state):
|
||||
set_state(new_state)
|
||||
)
|
||||
|
||||
R.effect(func(_arg):
|
||||
sprite.set_frame(1 if active.value else 0)
|
||||
)
|
||||
|
||||
func set_state(stateInfo):
|
||||
if stateInfo == null:
|
||||
return
|
||||
|
||||
if stateInfo["state"] == "on":
|
||||
sprite.set_frame(0)
|
||||
else:
|
||||
sprite.set_frame(1)
|
||||
|
||||
active.value = stateInfo["state"] == "on"
|
||||
icon.value = "toggle_" + stateInfo["state"]
|
||||
|
||||
await HomeApi.watch_state(entity_id, func(new_state):
|
||||
if new_state["state"] == "on":
|
||||
sprite.set_frame(0)
|
||||
else:
|
||||
sprite.set_frame(1)
|
||||
|
||||
icon.value="toggle_" + new_state["state"]
|
||||
)
|
||||
|
||||
func _on_click(_event):
|
||||
HomeApi.set_state(entity_id, "off" if sprite.get_frame() == 0 else "on")
|
||||
if sprite.get_frame() == 0:
|
||||
sprite.set_frame(1)
|
||||
else:
|
||||
sprite.set_frame(0)
|
||||
|
||||
func _on_request_completed():
|
||||
pass
|
||||
_toggle()
|
||||
|
||||
func quick_action():
|
||||
HomeApi.set_state(entity_id, "off" if sprite.get_frame() == 0 else "on")
|
||||
if sprite.get_frame() == 0:
|
||||
sprite.set_frame(1)
|
||||
else:
|
||||
sprite.set_frame(0)
|
||||
_toggle()
|
||||
|
||||
func _toggle():
|
||||
HomeApi.set_state(entity_id, "off" if active.value else "on")
|
|
@ -13,17 +13,17 @@ radius = 0.0482081
|
|||
animations = [{
|
||||
"frames": [{
|
||||
"duration": 1.0,
|
||||
"texture": ExtResource("1_w68gw")
|
||||
"texture": ExtResource("2_86ba1")
|
||||
}, {
|
||||
"duration": 1.0,
|
||||
"texture": ExtResource("2_86ba1")
|
||||
"texture": ExtResource("1_w68gw")
|
||||
}],
|
||||
"loop": true,
|
||||
"name": &"default",
|
||||
"speed": 5.0
|
||||
}]
|
||||
|
||||
[node name="Switch" type="StaticBody3D" ]
|
||||
[node name="Switch" type="StaticBody3D"]
|
||||
collision_mask = 0
|
||||
script = ExtResource("1_8ffhi")
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user