38 lines
896 B
GDScript
38 lines
896 B
GDScript
class_name InventoryUi extends Control
|
|
|
|
const INVENTORY_SLOT = preload("res://gui/pause/inventory/inventory_slot.tscn")
|
|
|
|
@export var data: InventoryData
|
|
|
|
var focus_index := 1
|
|
|
|
func _ready() -> void:
|
|
PauseMenu.shown.connect(update)
|
|
PauseMenu.hidden.connect(clear)
|
|
clear()
|
|
data.changed.connect(on_inventory_changed)
|
|
|
|
func clear() -> void:
|
|
for child in get_children():
|
|
child.queue_free()
|
|
|
|
func update() -> void:
|
|
for slot in data.slots:
|
|
var new_slot = INVENTORY_SLOT.instantiate()
|
|
add_child(new_slot)
|
|
new_slot.slot_data = slot
|
|
new_slot.focus_entered.connect(on_item_focused)
|
|
if !data.slots.is_empty():
|
|
await get_tree().process_frame
|
|
get_child(focus_index).grab_focus()
|
|
|
|
func on_inventory_changed() -> void:
|
|
clear()
|
|
update()
|
|
|
|
func on_item_focused() -> void:
|
|
for child_index in get_child_count():
|
|
if get_child(child_index).has_focus():
|
|
focus_index = child_index
|
|
return
|