mirror of
https://github.com/Orama-Interactive/Pixelorama.git
synced 2025-01-31 07:29:49 +00:00
Importing palettes is now possible in HTML5
This commit is contained in:
parent
d02bb52d48
commit
9464c7a953
|
@ -43,6 +43,32 @@ func _define_js() -> void:
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
function upload_palette() {
|
||||||
|
canceled = true;
|
||||||
|
var input = document.createElement('INPUT');
|
||||||
|
input.setAttribute("type", "file");
|
||||||
|
input.setAttribute("accept", "application/json, .gpl, image/png, image/jpeg, image/webp");
|
||||||
|
input.click();
|
||||||
|
input.addEventListener('change', event => {
|
||||||
|
if (event.target.files.length > 0){
|
||||||
|
canceled = false;}
|
||||||
|
var file = event.target.files[0];
|
||||||
|
var reader = new FileReader();
|
||||||
|
fileType = file.type;
|
||||||
|
fileName = file.name;
|
||||||
|
if (fileType == "image/png" || fileType == "image/jpeg" || fileType == "image/webp"){
|
||||||
|
reader.readAsArrayBuffer(file);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
reader.readAsText(file);
|
||||||
|
}
|
||||||
|
reader.onloadend = function (evt) {
|
||||||
|
if (evt.target.readyState == FileReader.DONE) {
|
||||||
|
fileData = evt.target.result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
function download(fileName, byte, type) {
|
function download(fileName, byte, type) {
|
||||||
var buffer = Uint8Array.from(byte);
|
var buffer = Uint8Array.from(byte);
|
||||||
var blob = new Blob([buffer], { type: type});
|
var blob = new Blob([buffer], { type: type});
|
||||||
|
@ -69,10 +95,10 @@ func load_image() -> void:
|
||||||
return
|
return
|
||||||
|
|
||||||
# Use data from png data
|
# Use data from png data
|
||||||
var imageData
|
var image_data
|
||||||
while true:
|
while true:
|
||||||
imageData = JavaScript.eval("fileData;", true)
|
image_data = JavaScript.eval("fileData;", true)
|
||||||
if imageData != null:
|
if image_data != null:
|
||||||
break
|
break
|
||||||
yield(get_tree().create_timer(1.0), "timeout") # Need more time to load data
|
yield(get_tree().create_timer(1.0), "timeout") # Need more time to load data
|
||||||
|
|
||||||
|
@ -83,13 +109,13 @@ func load_image() -> void:
|
||||||
var image_error
|
var image_error
|
||||||
match image_type:
|
match image_type:
|
||||||
"image/png":
|
"image/png":
|
||||||
image_error = image.load_png_from_buffer(imageData)
|
image_error = image.load_png_from_buffer(image_data)
|
||||||
"image/jpeg":
|
"image/jpeg":
|
||||||
image_error = image.load_jpg_from_buffer(imageData)
|
image_error = image.load_jpg_from_buffer(image_data)
|
||||||
"image/webp":
|
"image/webp":
|
||||||
image_error = image.load_webp_from_buffer(imageData)
|
image_error = image.load_webp_from_buffer(image_data)
|
||||||
var invalid_type:
|
var invalid_type:
|
||||||
print(invalid_type)
|
print("Invalid type: " + invalid_type)
|
||||||
return
|
return
|
||||||
if image_error:
|
if image_error:
|
||||||
print("An error occurred while trying to display the image.")
|
print("An error occurred while trying to display the image.")
|
||||||
|
@ -98,6 +124,50 @@ func load_image() -> void:
|
||||||
OpenSave.handle_loading_image(image_name, image)
|
OpenSave.handle_loading_image(image_name, image)
|
||||||
|
|
||||||
|
|
||||||
|
func load_palette() -> void:
|
||||||
|
if OS.get_name() != "HTML5" or !OS.has_feature('JavaScript'):
|
||||||
|
return
|
||||||
|
|
||||||
|
# Execute JS function
|
||||||
|
JavaScript.eval("upload_palette();", true) # Opens prompt for choosing file
|
||||||
|
|
||||||
|
yield(self, "InFocus") # Wait until JS prompt is closed
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
# Use data from palette file data
|
||||||
|
var palette_data
|
||||||
|
while true:
|
||||||
|
palette_data = JavaScript.eval("fileData;", true)
|
||||||
|
if palette_data != null:
|
||||||
|
break
|
||||||
|
yield(get_tree().create_timer(1.0), "timeout") # Need more time to load data
|
||||||
|
|
||||||
|
var file_type = JavaScript.eval("fileType;", true)
|
||||||
|
var file_name = JavaScript.eval("fileName;", true)
|
||||||
|
if file_name.ends_with(".gpl"):
|
||||||
|
var palette := Palette.new()
|
||||||
|
palette = Import.import_gpl(file_name, palette_data)
|
||||||
|
Global.palette_container.attempt_to_import_palette(palette)
|
||||||
|
else:
|
||||||
|
match file_type:
|
||||||
|
"image/png":
|
||||||
|
var image := Image.new()
|
||||||
|
var err = image.load_png_from_buffer(palette_data)
|
||||||
|
if !err:
|
||||||
|
Global.palette_container.import_image_palette(file_name, image)
|
||||||
|
"application/json":
|
||||||
|
var palette : Palette = Palette.new().deserialize(palette_data)
|
||||||
|
palette.source_path = file_name
|
||||||
|
Global.palette_container.attempt_to_import_palette(palette)
|
||||||
|
var invalid_type:
|
||||||
|
print("Invalid type: " + invalid_type)
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
func save_image(image : Image, file_name : String = "export") -> void:
|
func save_image(image : Image, file_name : String = "export") -> void:
|
||||||
if OS.get_name() != "HTML5" or !OS.has_feature('JavaScript'):
|
if OS.get_name() != "HTML5" or !OS.has_feature('JavaScript'):
|
||||||
return
|
return
|
||||||
|
|
|
@ -256,29 +256,22 @@ func import_patterns(priority_ordered_search_path: Array) -> void:
|
||||||
Global.fill_pattern_containers[1].get_child(3).get_child(1).max_value = image_size.y - 1
|
Global.fill_pattern_containers[1].get_child(3).get_child(1).max_value = image_size.y - 1
|
||||||
|
|
||||||
|
|
||||||
func import_gpl(path : String) -> Palette:
|
func import_gpl(path : String, text : String) -> Palette:
|
||||||
# Refer to app/core/gimppalette-load.c of the GIMP for the "living spec"
|
# Refer to app/core/gimppalette-load.c of the GIMP for the "living spec"
|
||||||
var result : Palette = null
|
var result : Palette = null
|
||||||
var file = File.new()
|
|
||||||
if file.file_exists(path):
|
|
||||||
file.open(path, File.READ)
|
|
||||||
var text = file.get_as_text()
|
|
||||||
var lines = text.split('\n')
|
var lines = text.split('\n')
|
||||||
var line_number := 0
|
var line_number := 0
|
||||||
var comments := ""
|
var comments := ""
|
||||||
for line in lines:
|
for line in lines:
|
||||||
# Check if valid Gimp Palette Library file
|
# Check if valid Gimp Palette Library file
|
||||||
if line_number == 0:
|
if line_number == 0:
|
||||||
if line != "GIMP Palette":
|
if not "GIMP Palette" in line:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
result = Palette.new()
|
result = Palette.new()
|
||||||
# Use filename as palette name in case reading old
|
# Use filename as palette name in case reading old
|
||||||
# palette format (must read more to determine)
|
# palette format (must read more to determine)
|
||||||
var name_start = path.find_last('/') + 1
|
result.name = path.get_basename().get_file()
|
||||||
var name_end = path.find_last('.')
|
|
||||||
if name_end > name_start:
|
|
||||||
result.name = path.substr(name_start, name_end - name_start)
|
|
||||||
|
|
||||||
# Comments
|
# Comments
|
||||||
if line.begins_with('#'):
|
if line.begins_with('#'):
|
||||||
|
@ -308,7 +301,6 @@ func import_gpl(path : String) -> Palette:
|
||||||
|
|
||||||
if result:
|
if result:
|
||||||
result.comments = comments
|
result.comments = comments
|
||||||
file.close()
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,9 @@ func on_new_empty_palette() -> void:
|
||||||
|
|
||||||
|
|
||||||
func on_import_palette() -> void:
|
func on_import_palette() -> void:
|
||||||
|
if OS.get_name() == "HTML5":
|
||||||
|
Html5FileExchange.load_palette()
|
||||||
|
else:
|
||||||
Global.palette_import_file_dialog.popup_centered()
|
Global.palette_import_file_dialog.popup_centered()
|
||||||
Global.dialog_open(true)
|
Global.dialog_open(true)
|
||||||
|
|
||||||
|
@ -49,7 +52,12 @@ func on_palette_import_file_selected(path : String) -> void:
|
||||||
if path.to_lower().ends_with("json"):
|
if path.to_lower().ends_with("json"):
|
||||||
palette = Palette.new().load_from_file(path)
|
palette = Palette.new().load_from_file(path)
|
||||||
elif path.to_lower().ends_with("gpl"):
|
elif path.to_lower().ends_with("gpl"):
|
||||||
palette = Import.import_gpl(path)
|
var file = File.new()
|
||||||
|
if file.file_exists(path):
|
||||||
|
file.open(path, File.READ)
|
||||||
|
var text = file.get_as_text()
|
||||||
|
file.close()
|
||||||
|
palette = Import.import_gpl(path, text)
|
||||||
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"):
|
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"):
|
||||||
var image := Image.new()
|
var image := Image.new()
|
||||||
var err := image.load(path)
|
var err := image.load(path)
|
||||||
|
|
|
@ -11,5 +11,5 @@ resizable = true
|
||||||
mode = 0
|
mode = 0
|
||||||
access = 2
|
access = 2
|
||||||
filters = PoolStringArray( "*.json ; JavaScript Object Notation", "*.gpl ; Gimp Palette Library", "*.png; Portable Network Graphics" )
|
filters = PoolStringArray( "*.json ; JavaScript Object Notation", "*.gpl ; Gimp Palette Library", "*.png; Portable Network Graphics" )
|
||||||
current_dir = "C:/Users"
|
current_dir = "/Users"
|
||||||
current_path = "C:/Users/"
|
current_path = "/Users/"
|
||||||
|
|
Loading…
Reference in a new issue