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

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
This commit is contained in:
OverloadedOrama 2020-06-30 22:29:24 +03:00
parent 637a60d9ee
commit 7e3436d3ba
7 changed files with 39 additions and 28 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,7 +1,6 @@
[gd_scene format=2]
[node name="SaveSpriteHTML5" type="ConfirmationDialog"]
visible = true
margin_right = 200.0
margin_bottom = 70.0

View file

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