From 9ad16eaecef73e9a4c26d69aa7ab09726de239cd Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Mon, 8 Jul 2024 15:35:38 +0930 Subject: [PATCH] [Web] Allow dropping files onto the window to load them. --- widgets/FileSelectWeb.gd | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/widgets/FileSelectWeb.gd b/widgets/FileSelectWeb.gd index fa2fbeb..e455feb 100644 --- a/widgets/FileSelectWeb.gd +++ b/widgets/FileSelectWeb.gd @@ -145,6 +145,24 @@ func _upload_file(filename: String, file_type: String, data): # data is Javascr print('uploaded "%s" with filesize %d bytes'%[filename, len(data)]) update_view() +func _files_dropped(filenames: PoolStringArray, screen_idx: int) -> void: + print('Files Dropped signal: screen=%d, files='%screen_idx, filenames) + for filename in filenames: + var ext: String = filename.rsplit('.', true, 1)[1] + if ext in ALLOWED_EXTS: + var file := File.new() + match file.open(filename, File.READ): + OK: + var rom_size := file.get_len() + var data := file.get_buffer(rom_size) + uploaded_files[filename] = data + uploaded_files_types[filename] = ext + print('uploaded "%s" with filesize %d bytes'%[filename, len(data)]) + update_view() + load_file(filename) + var error: + print_debug('Error %d loading filename:'%error, filename) + func _ready() -> void: if OS.get_name() != 'HTML5' or !OS.has_feature('JavaScript'): return @@ -155,6 +173,7 @@ func _ready() -> void: RomLoader.connect('rom_loaded', self, '_on_rom_loaded') RomLoader.connect('loading_stage_updated', self, '_on_loading_stage_updated') HTML5.connect('uploaded_file', self, '_upload_file') + get_tree().connect('files_dropped', self, '_files_dropped') func _set_mode(new_mode) -> void: match new_mode: