47 lines
1.1 KiB
GDScript
47 lines
1.1 KiB
GDScript
@tool
|
|
extends OptionButton
|
|
class_name CustomOptionButton
|
|
|
|
@export_dir var resourcePath : String
|
|
@export var regexPattern : String
|
|
@export var reloadOnOpen : bool = false
|
|
|
|
var selectedValue : String
|
|
|
|
func _ready() -> void:
|
|
loadData(true)
|
|
item_selected.connect(itemHasBeenSelected)
|
|
pressed.connect(loadData)
|
|
allow_reselect = true
|
|
|
|
func loadData(force : bool = false) -> void:
|
|
if !force && !reloadOnOpen:
|
|
return
|
|
|
|
var regex := RegEx.create_from_string(regexPattern)
|
|
var dir := DirAccess.open(resourcePath)
|
|
clear()
|
|
for file in dir.get_files():
|
|
var fileMatch := regex.search(file)
|
|
if fileMatch:
|
|
add_item(fileMatch.strings[1])
|
|
if selectedValue == fileMatch.strings[1]:
|
|
selected = item_count - 1
|
|
|
|
if !selectedValue && item_count > 0:
|
|
selected = 0
|
|
|
|
func itemHasBeenSelected(index : int) -> void:
|
|
if selectedValue != get_item_text(index):
|
|
selectedValue = get_item_text(index)
|
|
onValueChanged.emit(selectedValue)
|
|
|
|
|
|
func selectItemByName(name : String) -> void:
|
|
for i in item_count:
|
|
if get_item_text(i) == name:
|
|
selected = i
|
|
return
|
|
|
|
|
|
signal onValueChanged(value : String)
|