2024-01-27 16:13:43 +02:00
|
|
|
extends Entity
|
|
|
|
|
2024-04-17 17:49:58 +03:00
|
|
|
const Entity = preload ("../entity.gd")
|
2023-10-31 23:15:33 +02:00
|
|
|
|
|
|
|
@onready var sprite: AnimatedSprite3D = $Icon
|
2024-05-09 13:40:41 +03:00
|
|
|
@onready var snap_sound = $SnapSound
|
2023-10-31 23:15:33 +02:00
|
|
|
|
2024-04-18 13:35:57 +03:00
|
|
|
var active = R.state(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()
|
2023-11-24 02:36:31 +02:00
|
|
|
var stateInfo = await HomeApi.get_state(entity_id)
|
2023-11-05 14:59:33 +02:00
|
|
|
|
2024-04-18 13:35:57 +03:00
|
|
|
set_state(stateInfo)
|
2024-04-17 17:49:58 +03:00
|
|
|
|
2023-11-24 02:36:31 +02:00
|
|
|
await HomeApi.watch_state(entity_id, func(new_state):
|
2024-04-18 13:35:57 +03:00
|
|
|
set_state(new_state)
|
|
|
|
)
|
2023-11-03 23:00:05 +02:00
|
|
|
|
2024-04-18 13:35:57 +03:00
|
|
|
R.effect(func(_arg):
|
|
|
|
sprite.set_frame(1 if active.value else 0)
|
2024-04-17 17:49:58 +03:00
|
|
|
)
|
2023-10-31 23:15:33 +02:00
|
|
|
|
2024-04-18 13:35:57 +03:00
|
|
|
func set_state(stateInfo):
|
|
|
|
if stateInfo == null:
|
|
|
|
return
|
|
|
|
|
|
|
|
active.value = stateInfo["state"] == "on"
|
|
|
|
icon.value = "toggle_" + stateInfo["state"]
|
2023-10-31 23:15:33 +02:00
|
|
|
|
2024-04-18 13:35:57 +03:00
|
|
|
func _on_click(_event):
|
2024-05-09 13:40:41 +03:00
|
|
|
snap_sound.play()
|
2024-04-18 13:35:57 +03:00
|
|
|
_toggle()
|
2024-04-17 22:27:09 +03:00
|
|
|
|
|
|
|
func quick_action():
|
2024-04-18 13:35:57 +03:00
|
|
|
_toggle()
|
|
|
|
|
|
|
|
func _toggle():
|
|
|
|
HomeApi.set_state(entity_id, "off" if active.value else "on")
|