immersive-home/app/content/ui/menu/edit/devices.gd

59 lines
1.5 KiB
GDScript3
Raw Normal View History

extends Node3D
signal on_select_device(device_id)
const ButtonScene = preload ("res://content/ui/components/button/button.tscn")
@onready var grid_container = $GridContainer3D
@onready var pagination = $Pagination3D
@onready var search_input: Input3D = $Input
var page = R.state(0)
var page_size = 28.0
var search = R.state("")
func _ready():
var devices = R.computed(func(_arg):
var devices=Store.devices.state.devices
if search.value != "":
return devices.filter(func(device):
2024-05-06 13:42:45 +03:00
return device["name"].to_lower().find(search.value.to_lower()) != - 1||device["id"].to_lower().find(search.value.to_lower()) != - 1
)
return devices
)
var pages = R.computed(func(_arg):
return ceil(devices.value.size() / page_size)
)
var visible_devices = R.computed(func(_arg):
return devices.value.slice(page.value * page_size, page.value * page_size + page_size)
)
R.bind(pagination, "pages", pages)
R.bind(pagination, "page", page, pagination.on_page_changed)
R.bind(search_input, "text", search, search_input.on_text_changed)
search_input.on_text_changed.connect(func(_arg):
page.value=0
)
R.effect(func(_arg):
for child in grid_container.get_children():
grid_container.remove_child(child)
child.free()
for device in visible_devices.value:
var button_instance=ButtonScene.instantiate()
2024-05-06 13:42:45 +03:00
button_instance.label=device["name"]
2024-04-25 14:19:12 +03:00
button_instance.font_size=8
2024-04-25 17:31:55 +03:00
button_instance.on_button_up.connect(func():
2024-05-06 13:42:45 +03:00
on_select_device.emit(device["id"])
)
grid_container.add_child(button_instance)
)