JHRGB/ToolSelect.gd
2023-12-08 16:50:57 +00:00

61 lines
1.9 KiB
GDScript

extends Control
# Called when the node enters the scene tree for the first time.
func _ready():
var first_child = true # Flag to identify the first child node
for child in $VBoxContainer.get_children():
if child != $VBoxContainer/OptionButton:
$VBoxContainer/OptionButton.add_item(child.name)
if not first_child:
child.visible = false # Hide all children except the first one
first_child = false # Set flag to false after processing the first child
func populate_paste():
var current_selection = null
if $VBoxContainer/Paste.selected >= 0 and $VBoxContainer/Paste.selected < $VBoxContainer/Paste.get_item_count():
current_selection = $VBoxContainer/Paste.get_item_text($VBoxContainer/Paste.selected)
$VBoxContainer/Paste.clear()
var dir = DirAccess.open("user://saves/")
var file_names = []
if dir:
dir.list_dir_begin()
var file_name = dir.get_next()
while file_name != "":
if !dir.current_is_dir():
file_names.append(file_name)
file_name = dir.get_next()
dir.list_dir_end()
file_names.sort() # Sort the file names
var index=0
for f in file_names:
$VBoxContainer/Paste.add_item(f)
if f==current_selection:
$VBoxContainer/Paste.selected=index
index+=1
else:
print("An error occurred when trying to access the path.")
func _on_option_button_item_selected(index):
populate_paste()
var children = $VBoxContainer.get_children()
for i in range(len(children)):
var child = children[i]
if child != $VBoxContainer/OptionButton:
if i - 1 == index: # Adjusted index because of the OptionButton
child.show()
else:
child.hide()
func get_selected():
var children = $VBoxContainer.get_children()
var index = $VBoxContainer/OptionButton.get_selected_id()
for i in range(len(children)):
var child = children[i]
if child != $VBoxContainer/OptionButton:
if i - 1 == index: # Adjusted index because of the OptionButton
return child
return null