From 8c5aba0083e8c5f1aeda80024f0d7088b28a5bcb Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Sat, 25 May 2024 15:02:13 +0300 Subject: [PATCH] Implement dragging and dropping images directly from the browser to download them and import them --- src/Autoload/OpenSave.gd | 19 ++++++++++++++++++- src/Main.gd | 11 +++++++++++ src/Main.tscn | 3 +++ .../ExtensionExplorer/Entry/ExtensionEntry.gd | 15 +++------------ 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/Autoload/OpenSave.gd b/src/Autoload/OpenSave.gd index 7d3237e30..f1f67b6de 100644 --- a/src/Autoload/OpenSave.gd +++ b/src/Autoload/OpenSave.gd @@ -53,7 +53,8 @@ func handle_loading_file(file: String) -> void: var apng_res := AImgIOAPNGImporter.load_from_file(file) if apng_res[0] == null: # No error - this is an APNG! - handle_loading_aimg(file, apng_res[1]) + if typeof(apng_res[1]) == TYPE_ARRAY: + handle_loading_aimg(file, apng_res[1]) return # Attempt to load as a regular image. var image := Image.load_from_file(file) @@ -91,6 +92,22 @@ func add_import_option(import_name: StringName, import_scene: PackedScene) -> in return id +## Mostly used for downloading images from the Internet. Tries multiple file extensions +## in case the extension of the file is wrong, which is common for images on the Internet. +func load_image_from_buffer(buffer: PackedByteArray) -> Image: + var image := Image.new() + var err := image.load_png_from_buffer(buffer) + if err != OK: + err = image.load_jpg_from_buffer(buffer) + if err != OK: + err = image.load_webp_from_buffer(buffer) + if err != OK: + err = image.load_tga_from_buffer(buffer) + if err != OK: + image.load_bmp_from_buffer(buffer) + return image + + func handle_loading_image(file: String, image: Image) -> void: if Global.projects.size() <= 1 and Global.current_project.is_empty(): open_image_as_new_tab(file, image) diff --git a/src/Main.gd b/src/Main.gd index 2d3546c7d..9129343ae 100644 --- a/src/Main.gd +++ b/src/Main.gd @@ -384,6 +384,8 @@ func _notification(what: int) -> void: func _on_files_dropped(files: PackedStringArray) -> void: for file in files: + if not FileAccess.file_exists(file): + $ImageRequest.request(file) OpenSave.handle_loading_file(file) if splash_dialog.visible: splash_dialog.hide() @@ -575,3 +577,12 @@ func _exit_tree() -> void: Global.config_cache.set_value("view_menu", "show_guides", Global.show_guides) Global.config_cache.set_value("view_menu", "show_mouse_guides", Global.show_mouse_guides) Global.config_cache.save(Global.CONFIG_PATH) + + +func _on_image_request_request_completed( + _result: int, _response_code: int, _headers: PackedStringArray, body: PackedByteArray +) -> void: + var image := OpenSave.load_image_from_buffer(body) + if image.is_empty(): + return + OpenSave.handle_loading_image(OS.get_system_dir(OS.SYSTEM_DIR_DESKTOP), image) diff --git a/src/Main.tscn b/src/Main.tscn index ce1cbadc8..f1d71657a 100644 --- a/src/Main.tscn +++ b/src/Main.tscn @@ -102,6 +102,8 @@ visible = false [node name="RightCursor" type="Sprite2D" parent="."] visible = false +[node name="ImageRequest" type="HTTPRequest" parent="."] + [connection signal="files_selected" from="Dialogs/OpenSprite" to="." method="_on_OpenSprite_files_selected"] [connection signal="visibility_changed" from="Dialogs/OpenSprite" to="." method="_on_open_sprite_visibility_changed"] [connection signal="file_selected" from="Dialogs/SaveSprite" to="." method="_on_SaveSprite_file_selected"] @@ -117,3 +119,4 @@ visible = false [connection signal="visibility_changed" from="Dialogs/QuitAndSaveDialog" to="." method="_can_draw_true"] [connection signal="visibility_changed" from="Dialogs/ErrorDialog" to="." method="_can_draw_true"] [connection signal="visibility_changed" from="Dialogs/BackupConfirmation" to="." method="_on_backup_confirmation_visibility_changed"] +[connection signal="request_completed" from="ImageRequest" to="." method="_on_image_request_request_completed"] diff --git a/src/UI/ExtensionExplorer/Entry/ExtensionEntry.gd b/src/UI/ExtensionExplorer/Entry/ExtensionEntry.gd index d392b6f57..85f38db1c 100644 --- a/src/UI/ExtensionExplorer/Entry/ExtensionEntry.gd +++ b/src/UI/ExtensionExplorer/Entry/ExtensionEntry.gd @@ -56,18 +56,9 @@ func _on_ImageRequest_request_completed( ) -> void: # Update the received image thumbnail_request.queue_free() - var image := Image.new() - # for images on internet there is a hagh chance that extension is wrong - # so check all of them even if they give error - var err := image.load_png_from_buffer(body) - if err != OK: - var err_a := image.load_jpg_from_buffer(body) - if err_a != OK: - var err_b := image.load_webp_from_buffer(body) - if err_b != OK: - var err_c := image.load_tga_from_buffer(body) - if err_c != OK: - image.load_bmp_from_buffer(body) + var image := OpenSave.load_image_from_buffer(body) + if image.is_empty(): + return var texture := ImageTexture.create_from_image(image) small_picture.texture_normal = texture small_picture.pressed.connect(enlarge_thumbnail.bind(texture))