ChocolateBird/widgets/FileSelect.gd

247 lines
8.3 KiB
GDScript3
Raw Normal View History

2023-08-01 23:22:31 +09:30
extends PanelContainer
#warning-ignore-all:return_value_discarded
2023-08-01 23:22:31 +09:30
signal continue_pressed
signal savefile_loaded(dicts)
const globals = preload('res://globals.gd') # Make LSP shut up about non-const
2023-12-06 18:19:19 +10:30
const FOLDER_ICON := globals.FOLDER_ICON
const ALLOWED_EXTS := globals.ALLOWED_EXTS
const CD_EXTS := globals.CD_EXTS
const EXT_ICONS := globals.EXT_ICONS
const TYPE_DESCS := globals.TYPE_DESCS
enum {SELECT_ROM, SELECT_SAVE, COMPLETE}
var current_mode = SELECT_ROM
var cached_cd_bin_paths := {}
2023-08-01 23:22:31 +09:30
var dir := Directory.new()
var home_path := ''
onready var itemlist: ItemList = $VBoxContainer/HBoxContainer/PanelContainer/ItemList
onready var vscroller: VScrollBar = itemlist.get_child(0)
onready var folder_buttons_scroller: ScrollContainer = $VBoxContainer/ScrollContainer
onready var folder_buttons: HBoxContainer = $VBoxContainer/ScrollContainer/folder_buttons
onready var btn_ok: Button = $'%btn_ok'
onready var btn_continue: Button = $'%btn_continue'
2023-09-21 15:26:10 +09:30
onready var lbl_filename_header: Label = $'%lbl_filename_header'
onready var lbl_filename_content: Label = $'%lbl_filename_content'
onready var lbl_filetype_header: Label = $'%lbl_filetype_header'
onready var lbl_filetype_content: Label = $'%lbl_filetype_content'
onready var lbl_filesize_header: Label = $'%lbl_filesize_header'
onready var lbl_filesize_content: Label = $'%lbl_filesize_content'
onready var lbl_fileinfo_header: Label = $'%lbl_fileinfo_header'
onready var lbl_fileinfo_content: Label = $'%lbl_fileinfo_content'
const inactive_modulate_color := Color(0.5, 0.5, 0.5)
const active_modulate_color := Color.white
var last_dir := ''
2023-08-01 23:22:31 +09:30
static func get_human_size(n: int) -> String:
if n > 0x100000000:
return '%.2f GiB' % (n/float(0x40000000))
if n > 0x400000:
return '%.1f MiB' % (n/float(0x100000))
if n > 0x1000:
return '%.1f KiB' % (n/float(0x400))
return '%d B' % n
static func tokenize_path(path: String) -> PoolStringArray:
return path.rstrip('/').split('/', true) # Allow empty means '/' will return ['']
static func join_path(tokens) -> String:
var path = '/'.join(tokens)
return path if path else '/'
2023-08-01 23:22:31 +09:30
func update_view():
var error = dir.list_dir_begin(true, true)
if error != OK:
print_debug(error)
return
for child in folder_buttons.get_children():
child.queue_free()
var path = ProjectSettings.globalize_path(dir.get_current_dir())
var cur_path: Array = tokenize_path(path)
2023-08-01 23:22:31 +09:30
var l := len(cur_path)
for i in l:
var btn := Button.new()
btn.connect('pressed', self, 'activate_entry', [join_path(cur_path.slice(0, i))])
2023-08-01 23:22:31 +09:30
btn.text = cur_path[i]
folder_buttons.add_child(btn)
if i == 0:
btn.text += '/'
if i == l-1:
btn.icon = FOLDER_ICON
if last_dir.begins_with(path): # i.e. we jumped up a level or more
var last_path: Array = tokenize_path(last_dir)
var l2 := len(last_path)
for i in range(l, l2):
var btn := Button.new()
btn.connect('pressed', self, 'activate_entry', [join_path(last_path.slice(0, i))])
btn.text = last_path[i]
btn.modulate = inactive_modulate_color
folder_buttons.add_child(btn)
# Doesn't work thanks to frame delay
# folder_buttons_scroller.set_h_scroll(9999)
# folder_buttons_scroller.ensure_control_visible(folder_buttons_scroller.get_h_scrollbar())
# folder_buttons_scroller.get_h_scrollbar().update()
2023-08-01 23:22:31 +09:30
itemlist.clear()
var directories := PoolStringArray()
var current_dir := dir.get_current_dir()
2023-08-01 23:22:31 +09:30
var files := []
while true:
var entry := dir.get_next()
if len(entry) == 0:
break
var filename := current_dir + '/' + entry
if filename in cached_cd_bin_paths:
files.append([entry, EXT_ICONS.iso])
elif dir.current_is_dir():
2023-08-01 23:22:31 +09:30
directories.append(entry)
elif '.' in entry:
var extension = entry.rsplit('.', true, 1)[1]
if extension in ALLOWED_EXTS:
files.append([entry, EXT_ICONS.get(extension)])
2023-08-01 23:22:31 +09:30
directories.sort()
files.sort()
for entry in directories:
itemlist.add_item(entry, FOLDER_ICON)
2023-08-01 23:22:31 +09:30
for entry in files:
itemlist.add_item(entry[0], entry[1])
func load_file(entry: String):
var filename := dir.get_current_dir() + '/' + entry
var ext = entry.rsplit('.', true, 1)[1].to_lower()
match ext:
2023-12-06 18:19:19 +10:30
'sfc', 'smc':
if current_mode == SELECT_ROM:
RomLoader.load_snes_rom(filename)
'srm':
if current_mode == SELECT_ROM:
return
var file := File.new()
match file.open(filename, File.READ):
OK:
emit_signal('savefile_loaded', SaveLoader.load_save_dicts_from_file(file))
_set_mode(COMPLETE)
var err:
print_debug('Error loading savefile: %s - %d'%[filename, err])
func activate_entry(entry: String, _index: int = -1):
var curr_dir := dir.get_current_dir()
if not self.last_dir.begins_with(curr_dir):
self.last_dir = curr_dir
2023-08-01 23:22:31 +09:30
if dir.dir_exists(entry):
var error := dir.change_dir(entry)
if error == OK:
update_view()
init_labels()
2023-08-01 23:22:31 +09:30
else:
print_debug(error)
elif dir.file_exists(entry):
load_file(entry)
func activate_current_entry():
var selected := itemlist.get_selected_items()
if len(selected) > 0:
activate_entry(itemlist.get_item_text(selected[0]))
2023-08-01 23:22:31 +09:30
func view_entry(entry: String, index: int = -1):
init_labels()
lbl_filename_header.modulate = active_modulate_color
lbl_filename_content.text = entry
lbl_filetype_header.modulate = active_modulate_color
2023-08-01 23:22:31 +09:30
if dir.dir_exists(entry):
lbl_filetype_content.text = 'Folder'
btn_ok.disabled = false
2023-08-01 23:22:31 +09:30
elif dir.file_exists(entry):
var file := File.new()
var filename := dir.get_current_dir() + '/' + entry
var error := file.open(filename, File.READ)
2023-08-01 23:22:31 +09:30
if error != OK:
print_debug(error)
return
var size = file.get_len()
var human_size = get_human_size(size)
var ext = entry.rsplit('.', true, 1)[1].to_lower()
var type = TYPE_DESCS.get(ext, 'Unknown')
2023-08-01 23:22:31 +09:30
var prodcode := ''
if ext in CD_EXTS:
2023-08-01 23:22:31 +09:30
var cd := RomLoader.loader_cd_image.new(file)
for key in cd.directory:
var s = key.trim_prefix('./')
var re_match := RomLoader.psx_productcode_regex.search(s)
if re_match:
2023-08-02 19:50:50 +09:30
prodcode = '%s\n%s\n%s' % [cd.pvd.system_identifier.strip_edges(), cd.pvd.volume_identifier.strip_edges(), re_match.get_string(1)]
2023-08-01 23:22:31 +09:30
type = 'PSX CD-ROM Image'
cached_cd_bin_paths[filename] = prodcode
if index >= 0:
itemlist.set_item_icon(index, EXT_ICONS.iso)
2023-08-01 23:22:31 +09:30
break
var info = ('Info:\n %s' % prodcode) if prodcode else ''
lbl_filetype_content.text = type
lbl_filesize_header.modulate = active_modulate_color
lbl_filesize_content.text = human_size
lbl_fileinfo_header.modulate = active_modulate_color if prodcode else inactive_modulate_color
lbl_fileinfo_content.text = prodcode
match current_mode:
SELECT_ROM:
btn_ok.disabled = (ext != 'sfc' and ext != 'smc') # Only allow opening snes roms for now
SELECT_SAVE, COMPLETE:
btn_ok.disabled = (ext != 'srm')
func init_labels():
lbl_filename_header.modulate = inactive_modulate_color
lbl_filename_content.text = ''
lbl_filetype_header.modulate = inactive_modulate_color
lbl_filetype_content.text = ''
lbl_filesize_header.modulate = inactive_modulate_color
lbl_filesize_content.text = ''
lbl_fileinfo_header.modulate = inactive_modulate_color
lbl_fileinfo_content.text = ''
btn_ok.disabled = true
2023-08-01 23:22:31 +09:30
func _ready() -> void:
init_labels()
2023-08-01 23:22:31 +09:30
if OS.has_feature('windows'):
home_path = OS.get_environment('USERPROFILE')
else:
home_path = OS.get_environment('HOME')
# dir.open(OS.get_system_dir(OS.SYSTEM_DIR_DOWNLOADS))
var error = dir.open(home_path)
if error == OK:
update_view()
btn_ok.connect('pressed', self, 'activate_current_entry')
btn_continue.connect('pressed', self, '_on_continue_pressed')
RomLoader.connect('rom_loaded', self, '_on_rom_loaded')
2023-08-01 23:22:31 +09:30
print(ProjectSettings.globalize_path('user://'))
print(ProjectSettings.globalize_path(dir.get_current_dir()))
func _set_mode(new_mode) -> void:
match new_mode:
SELECT_ROM:
$'%lbl_prompt'.text = "Select a ROM from your device's storage"
btn_continue.disabled = true
SELECT_SAVE:
$'%lbl_prompt'.text = "Select a save file from your device's storage"
btn_continue.disabled = false
COMPLETE:
$'%lbl_prompt'.text = "Save file loaded, press Continue"
btn_continue.disabled = false
current_mode = new_mode
update_view()
init_labels()
2023-08-01 23:22:31 +09:30
func _on_ItemList_item_activated(index: int) -> void:
activate_entry(itemlist.get_item_text(index), index)
2023-08-01 23:22:31 +09:30
func _on_ItemList_item_selected(index: int) -> void:
view_entry(itemlist.get_item_text(index), index)
func _on_rom_loaded() -> void:
_set_mode(SELECT_SAVE)
func _on_continue_pressed() -> void:
emit_signal('continue_pressed')