2023-10-31 23:15:33 +02:00
|
|
|
extends StaticBody3D
|
|
|
|
|
|
|
|
@export var entity_id = "switch.plug_printer_2"
|
|
|
|
@onready var sprite: AnimatedSprite3D = $Icon
|
2023-11-12 18:36:23 +02:00
|
|
|
@onready var animation: AnimationPlayer = $AnimationPlayer
|
|
|
|
@onready var shape = $CSGCombiner3D
|
|
|
|
@onready var rod_top = $RodTop
|
|
|
|
@onready var rod_bottom = $RodBottom
|
|
|
|
@onready var slider_knob = $Knob
|
|
|
|
var state = false
|
2023-10-31 23:15:33 +02:00
|
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
func _ready():
|
2023-11-05 17:36:13 +02:00
|
|
|
var stateInfo = await HomeAdapters.adapter.get_state(entity_id)
|
2023-11-12 18:36:23 +02:00
|
|
|
set_state(stateInfo["state"] == "on")
|
|
|
|
|
|
|
|
await HomeAdapters.adapter.watch_state(entity_id, func(new_state):
|
|
|
|
if (new_state["state"] == "on") == state:
|
|
|
|
return
|
|
|
|
set_state(new_state["state"] == "on")
|
|
|
|
)
|
2023-10-31 23:15:33 +02:00
|
|
|
|
2023-11-12 18:36:23 +02:00
|
|
|
func set_state(state: bool):
|
|
|
|
self.state = state
|
|
|
|
if state:
|
|
|
|
animation.play_backwards("light")
|
|
|
|
else:
|
|
|
|
animation.play("light")
|
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:
|
|
|
|
HomeAdapters.adapter.set_state(entity_id, "on" if !state else "off")
|
|
|
|
set_state(!state)
|
2023-10-31 23:15:33 +02:00
|
|
|
else:
|
2023-11-12 18:36:23 +02:00
|
|
|
_on_clickable_on_click(event)
|
2023-10-31 23:15:33 +02:00
|
|
|
|
|
|
|
func _on_request_completed():
|
|
|
|
pass
|
2023-11-12 18:36:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
func _on_clickable_on_click(event):
|
|
|
|
var click_pos: Vector3 = to_local(event.ray.get_collision_point())
|
|
|
|
var vec_bottom_to_top = rod_top.position - rod_bottom.position
|
|
|
|
|
|
|
|
var length_click = click_pos.y - rod_bottom.position.y
|
|
|
|
var length_total = vec_bottom_to_top.y
|
|
|
|
|
|
|
|
var ratio = length_click / length_total
|
|
|
|
|
|
|
|
var new_pos = rod_bottom.position.lerp(rod_top.position, ratio)
|
|
|
|
|
|
|
|
slider_knob.position = new_pos
|
|
|
|
|
|
|
|
HomeAdapters.adapter.set_state(entity_id, "on" if state else "off", {"brightness_pct": int(ratio * 100)})
|