1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-18 17:19:50 +00:00

Implement dragging and dropping images directly from the browser to download them and import them

This commit is contained in:
Emmanouil Papadeas 2024-05-25 15:02:13 +03:00
parent b25ae8b4cc
commit 8c5aba0083
4 changed files with 35 additions and 13 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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"]

View file

@ -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))