FunkPanion/scripts/config/config_window_presets.gd

76 lines
2.3 KiB
GDScript

extends ScrollContainer
signal reset
signal preset_use
signal close
var dir
var conf_reference:Config
@onready var tree = $"MarginContainer/VBoxContainer/Tree"
var tree_root: TreeItem
@onready var addnew_name = $"MarginContainer/VBoxContainer/AddNew/name"
@onready var addnew_switch = $"MarginContainer/VBoxContainer/AddNew/switch"
@onready var close_icon = load("res://assets/icons/close.png")
@onready var preset_use_icon = load("res://assets/icons/preset_use.png")
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
tree.set_column_title(0, "Name")
tree.set_column_title(1, "Created")
tree_root = tree.create_item()
dir = DirAccess.open("user://presets/")
tree.button_clicked.connect(func(item:TreeItem, column:int, id:int, _mbi:int):
if column == 1:
if id == 0:
GlobalConfig.load_from_file(item.get_metadata(0))
GlobalConfig.save_to_file()
preset_use.emit()
if id == 1:
dir.remove(item.get_metadata(0))
refresh_tree()
)
addnew_switch.pressed.connect(func():
if addnew_name.text.length() > 0 and addnew_name.text.is_valid_filename():
addnew_name.text = addnew_name.text.simplify_path()
if addnew_name.text.get_extension() != "cfg":
addnew_name.text = addnew_name.text.get_basename() + ".cfg"
conf_reference.save_to_file(dir.get_current_dir().path_join(addnew_name.text))
addnew_name.text = ""
refresh_tree()
)
refresh_tree()
func refresh_tree() -> void:
for child in tree_root.get_children():
child.free()
if not dir:
DirAccess.make_dir_absolute("user://presets/")
dir = DirAccess.open("user://presets/")
dir.list_dir_begin()
var file_name = dir.get_next()
while file_name != "":
if file_name.get_extension() == "cfg":
var child:TreeItem = tree.create_item(tree_root)
var abs_file_name = dir.get_current_dir().path_join(file_name)
child.set_cell_mode(0, TreeItem.CELL_MODE_STRING)
child.set_text(0, file_name.get_basename())
var mod_time = FileAccess.get_modified_time(abs_file_name)
child.set_text(1, Time.get_datetime_string_from_unix_time(mod_time, true))
child.add_button(1, preset_use_icon)
child.add_button(1, close_icon)
child.set_metadata(0, abs_file_name)
file_name = dir.get_next()
func _on_close_button_pressed() -> void:
close.emit()
func _on_reset_button_pressed() -> void:
reset.emit()