From 7e3436d3ba8ca8b726b39703362a8c4b2db8d5f0 Mon Sep 17 00:00:00 2001 From: OverloadedOrama <35376950+OverloadedOrama@users.noreply.github.com> Date: Tue, 30 Jun 2020 22:29:24 +0300 Subject: [PATCH] Loading image files is now possible in HTML5 Had to move some palette png code around in order to make it possible to use these image files as palettes. Hopefully loading .pxo files should be next. Addresses #280 --- src/Autoload/HTML5FileExchange.gd | 12 ++++++------ src/Autoload/Import.gd | 12 ++---------- src/Autoload/OpenSave.gd | 15 +++++++++------ src/Palette/PaletteContainer.gd | 16 +++++++++++++++- src/UI/Dialogs/PreviewDialog.gd | 2 +- src/UI/Dialogs/SaveSpriteHTML5.tscn | 1 - src/UI/TopMenuContainer.gd | 9 ++++++--- 7 files changed, 39 insertions(+), 28 deletions(-) diff --git a/src/Autoload/HTML5FileExchange.gd b/src/Autoload/HTML5FileExchange.gd index 0328ed40d..8576178dd 100644 --- a/src/Autoload/HTML5FileExchange.gd +++ b/src/Autoload/HTML5FileExchange.gd @@ -53,7 +53,7 @@ func _define_js() -> void: """, true) -func load_image() -> Image: +func load_image() -> void: if OS.get_name() != "HTML5" or !OS.has_feature('JavaScript'): return @@ -62,7 +62,7 @@ func load_image() -> Image: yield(self, "InFocus") # wait until js prompt is closed - yield(get_tree().create_timer(0.1), "timeout") #give some time for async js data load + yield(get_tree().create_timer(0.5), "timeout") #give some time for async js data load if JavaScript.eval("canceled;", true): # if File Dialog closed w/o file return @@ -75,12 +75,12 @@ func load_image() -> Image: break yield(get_tree().create_timer(1.0), "timeout") # need more time to load data - var imageType = JavaScript.eval("fileType;", true) - var imageName = JavaScript.eval("fileName;", true) + var image_type = JavaScript.eval("fileType;", true) + var image_name = JavaScript.eval("fileName;", true) var image = Image.new() var image_error - match imageType: + match image_type: "image/png": image_error = image.load_png_from_buffer(imageData) "image/jpeg": @@ -94,7 +94,7 @@ func load_image() -> Image: print("An error occurred while trying to display the image.") return else: - return image + OpenSave.handle_loading_image(image_name, image) func save_image(image : Image, file_name : String = "export") -> void: diff --git a/src/Autoload/Import.gd b/src/Autoload/Import.gd index 88cdd1e25..1fd42c145 100644 --- a/src/Autoload/Import.gd +++ b/src/Autoload/Import.gd @@ -313,14 +313,9 @@ func import_gpl(path : String) -> Palette: return result -func import_png_palette(path: String) -> Palette: +func import_png_palette(path: String, image : Image) -> Palette: var result: Palette = null - var image := Image.new() - var err := image.load(path) - if err != OK: # An error occured - return null - var height: int = image.get_height() var width: int = image.get_width() @@ -335,9 +330,6 @@ func import_png_palette(path: String) -> Palette: result.add_color(color, "#" + color.to_html()) image.unlock() - var name_start = path.find_last('/') + 1 - var name_end = path.find_last('.') - if name_end > name_start: - result.name = path.substr(name_start, name_end - name_start) + result.name = path.get_basename().get_file() return result diff --git a/src/Autoload/OpenSave.gd b/src/Autoload/OpenSave.gd index d902e5a32..6117c9e27 100644 --- a/src/Autoload/OpenSave.gd +++ b/src/Autoload/OpenSave.gd @@ -34,13 +34,16 @@ func handle_loading_files(files : PoolStringArray) -> void: Global.error_dialog.popup_centered() Global.dialog_open(true) continue + handle_loading_image(file, image) - var preview_dialog : ConfirmationDialog = preload("res://src/UI/Dialogs/PreviewDialog.tscn").instance() - preview_dialog.path = file - preview_dialog.image = image - Global.control.add_child(preview_dialog) - preview_dialog.popup_centered() - Global.dialog_open(true) + +func handle_loading_image(file : String, image : Image) -> void: + var preview_dialog : ConfirmationDialog = preload("res://src/UI/Dialogs/PreviewDialog.tscn").instance() + preview_dialog.path = file + preview_dialog.image = image + Global.control.add_child(preview_dialog) + preview_dialog.popup_centered() + Global.dialog_open(true) func open_pxo_file(path : String, untitled_backup : bool = false) -> void: diff --git a/src/Palette/PaletteContainer.gd b/src/Palette/PaletteContainer.gd index 9f10dad46..ee9953228 100644 --- a/src/Palette/PaletteContainer.gd +++ b/src/Palette/PaletteContainer.gd @@ -51,8 +51,22 @@ func on_palette_import_file_selected(path : String) -> void: elif path.to_lower().ends_with("gpl"): palette = Import.import_gpl(path) elif path.to_lower().ends_with("png") or path.to_lower().ends_with("bmp") or path.to_lower().ends_with("hdr") or path.to_lower().ends_with("jpg") or path.to_lower().ends_with("svg") or path.to_lower().ends_with("tga") or path.to_lower().ends_with("webp"): - palette = Import.import_png_palette(path) + var image := Image.new() + var err := image.load(path) + if !err: + import_image_palette(path, image) + return + attempt_to_import_palette(palette) + + +func import_image_palette(path : String, image : Image) -> void: + var palette : Palette = null + palette = Import.import_png_palette(path, image) + attempt_to_import_palette(palette) + + +func attempt_to_import_palette(palette : Palette) -> void: if palette: palette.name = palette_name_replace(palette.name) Global.palettes[palette.name] = palette diff --git a/src/UI/Dialogs/PreviewDialog.gd b/src/UI/Dialogs/PreviewDialog.gd index 01444a5f4..77306463a 100644 --- a/src/UI/Dialogs/PreviewDialog.gd +++ b/src/UI/Dialogs/PreviewDialog.gd @@ -52,7 +52,7 @@ func _on_PreviewDialog_confirmed() -> void: OpenSave.open_image_as_new_layer(image, path.get_basename().get_file(), frame_index) elif current_import_option == ImageImportOptions.PALETTE: - Global.palette_container.on_palette_import_file_selected(path) + Global.palette_container.import_image_palette(path, image) elif current_import_option == ImageImportOptions.BRUSH: var file_name : String = path.get_basename().get_file() diff --git a/src/UI/Dialogs/SaveSpriteHTML5.tscn b/src/UI/Dialogs/SaveSpriteHTML5.tscn index 4491d26b9..1b762ef76 100644 --- a/src/UI/Dialogs/SaveSpriteHTML5.tscn +++ b/src/UI/Dialogs/SaveSpriteHTML5.tscn @@ -1,7 +1,6 @@ [gd_scene format=2] [node name="SaveSpriteHTML5" type="ConfirmationDialog"] -visible = true margin_right = 200.0 margin_bottom = 70.0 diff --git a/src/UI/TopMenuContainer.gd b/src/UI/TopMenuContainer.gd index 15120b069..7a4f8ccd5 100644 --- a/src/UI/TopMenuContainer.gd +++ b/src/UI/TopMenuContainer.gd @@ -145,9 +145,12 @@ func on_new_project_file_menu_option_pressed() -> void: func open_project_file() -> void: - Global.open_sprites_dialog.popup_centered() - Global.dialog_open(true) - Global.control.opensprite_file_selected = false + if OS.get_name() == "HTML5": + Html5FileExchange.load_image() + else: + Global.open_sprites_dialog.popup_centered() + Global.dialog_open(true) + Global.control.opensprite_file_selected = false func on_open_last_project_file_menu_option_pressed() -> void: