extends Control

@onready
var character_grid_container := $ScrollContainer2

@onready
var character_grid := $ScrollContainer2/GridContainer

signal character_selected(file_name)

# Called when the node enters the scene tree for the first time.
func _ready():
	var characters := DirAccess.open("res://characters/")
	if characters:
		characters.list_dir_begin()
		var file_name = characters.get_next()
		while file_name != "":
			file_name = file_name.replace('.import', '') # <--- remove the .import
			if file_name.ends_with(".png"):

				var texture = load("res://characters/" + file_name)

				var texture_btn := TextureButton.new()
			
				character_grid.add_child(texture_btn)
				texture_btn.custom_minimum_size = Vector2(80, 40)
				texture_btn.ignore_texture_size = true
				texture_btn.stretch_mode = TextureButton.STRETCH_KEEP_ASPECT
				texture_btn.texture_normal = texture
				
				texture_btn.pressed.connect(select_character.bind(file_name))
			
			file_name = characters.get_next()
			
func show_grid():
	character_grid_container.visible = true

func select_character(file_name: String):
	print("select_character")
	character_selected.emit(file_name)
