@tool
extends Control

var path:String = "res://scenes/nodes/rooms/";
var label_template:PackedScene;
var scenes_ref:Array[NodePath] = [];

@export var selected_label:Label;

func _enter_tree() -> void:
	label_template = load("res://addons/discretized_loader/templates/discrete_element_panel.tscn");
	var dir:DirAccess = DirAccess.open(path);
	if dir:
		dir.list_dir_begin();
		var file_name:String = dir.get_next();
		while file_name != "":
			print(file_name);
			if dir.current_is_dir():
				file_name = dir.get_next();
				continue;
			
			var label:Control = label_template.instantiate();
			label.setup(file_name);
			#label.button.pressed.connect(focus_selection.bind(file_name));
			add_child(label);
			scenes_ref.append(label.get_path());
			file_name = dir.get_next();
		
		dir.list_dir_end();
	else:
		print("Could not find folder");

func focus_selection(what) -> void:
	selected_label.text = what;

func _exit_tree() -> void:
	for lab:NodePath in scenes_ref:
		get_node(lab).free();
