immersive-home/content/ui/menu/menu.gd

94 lines
2.3 KiB
GDScript3
Raw Normal View History

2023-10-30 02:21:50 +02:00
extends Node3D
2023-11-05 16:16:07 +02:00
const Device = preload("res://content/ui/device/device.tscn")
const Entity = preload("res://content/ui/entity/entity.tscn")
const Switch = preload("res://content/entities/switch/switch.tscn")
const Light = preload("res://content/entities/light/light.tscn")
const Sensor = preload("res://content/entities/sensor/sensor.tscn")
2023-10-30 02:21:50 +02:00
2023-11-07 22:07:34 +02:00
@onready var devices_node: GridContainer3D = $Devices
2023-10-30 02:21:50 +02:00
var devices
var selected_device = null
# Called when the node enters the scene tree for the first time.
func _ready():
2023-11-05 17:36:13 +02:00
devices = await HomeAdapters.adapter.get_devices()
2023-10-30 02:21:50 +02:00
render_devices()
func render_devices():
for device in devices:
var info = device.values()[0]
var device_instance = Device.instantiate()
device_instance.click.connect(_on_device_click)
device_instance.id = device.keys()[0]
2023-10-30 02:21:50 +02:00
devices_node.add_child(device_instance)
device_instance.set_device_name(info["name"])
2023-11-07 22:07:34 +02:00
devices_node._update_container()
2023-10-30 02:21:50 +02:00
func render_entities():
var info
for device in devices:
if device.keys()[0] == selected_device:
2023-10-30 02:21:50 +02:00
info = device.values()[0]
break
if info == null:
return
var entities = info["entities"]
for entity in entities:
var entity_instance = Entity.instantiate()
entity_instance.click.connect(_on_entity_click)
devices_node.add_child(entity_instance)
entity_instance.set_entity_name(entity)
2023-11-07 22:07:34 +02:00
devices_node._update_container()
2023-10-30 02:21:50 +02:00
func _on_device_click(device_id):
selected_device = device_id
2023-10-30 02:21:50 +02:00
print(selected_device)
clear_menu()
render_entities()
func _on_entity_click(entity_name):
print(entity_name)
selected_device = null
clear_menu()
render_devices()
var type = entity_name.split(".")[0]
print(type)
if type == "switch":
var switch = Switch.instantiate()
switch.entity_id = entity_name
switch.set_position(global_position)
get_node("/root").add_child(switch)
if type == "light":
var light = Light.instantiate()
light.entity_id = entity_name
light.set_position(global_position)
get_node("/root").add_child(light)
if type == "sensor":
var sensor = Sensor.instantiate()
sensor.entity_id = entity_name
sensor.set_position(global_position)
get_node("/root").add_child(sensor)
2023-10-30 02:21:50 +02:00
func clear_menu():
for child in devices_node.get_children():
devices_node.remove_child(child)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass