TinyAdventure/ui/inventory_dialog.gd

69 lines
1.8 KiB
GDScript

class_name InventoryDialog
extends MarginContainer
# Crafting
@onready var recipe_list:ItemList = %RecipeList
@onready var ingredients_container:ItemGrid = %IngredientsContainer
@onready var results_container:ItemGrid = %ResultsContainer
@onready var craft_button = %CraftButton
# Backpack
@onready var inventory_container:ItemGrid = %InventoryContainer
var _inventory:Inventory = null
var _selected_recipe:RecipeResource
func open(recipes:Array[RecipeResource], inventory:Inventory):
_inventory = inventory
show()
recipe_list.clear()
for recipe in recipes:
var index = recipe_list.add_item(recipe.name)
recipe_list.set_item_metadata(index, recipe)
inventory_container.displayStacks(inventory.get_item_stacks())
recipe_list.select(0)
_on_recipe_list_item_selected(0)
func _on_close_button_pressed():
hide()
func _unhandled_key_input(event:InputEvent):
var key_event:InputEventKey = event as InputEventKey
if visible and key_event.pressed and key_event.get_keycode_with_modifiers() == KEY_ESCAPE:
get_viewport().set_input_as_handled()
hide()
func update_crafting_widgets(recipe:RecipeResource):
ingredients_container.display(recipe.ingredients)
results_container.display(recipe.results)
craft_button.disabled = not _inventory.has_all(recipe.ingredients)
func _on_recipe_list_item_selected(index):
var recipe:RecipeResource = recipe_list.get_item_metadata(index)
_selected_recipe = recipe
print("Selected recipe is: " + str(recipe) + " with index " + str(index))
update_crafting_widgets(_selected_recipe)
func _on_craft_button_pressed():
for item in _selected_recipe.ingredients:
_inventory.remove_item(item)
for item in _selected_recipe.results:
_inventory.add_item(item)
inventory_container.displayStacks(_inventory.get_item_stacks())
update_crafting_widgets(_selected_recipe)