HTML5-File-Exchange-for-Godot/addons/HTML5FileExchange/HTML5FileExchange.gd

81 lines
2.3 KiB
GDScript3
Raw Normal View History

2020-06-03 17:03:57 +00:00
extends Node
signal read_completed
signal load_completed(image)
2023-09-10 03:45:00 +00:00
var js_callback = JavaScriptBridge.create_callback(load_handler);
var js_interface;
2020-06-03 17:03:57 +00:00
func _ready():
2023-09-10 03:45:00 +00:00
if OS.get_name() == "Web":
2020-06-03 17:03:57 +00:00
_define_js()
2023-09-10 02:19:26 +00:00
js_interface = JavaScriptBridge.get_interface("_HTML5FileExchange");
2020-06-03 17:03:57 +00:00
func _define_js()->void:
#Define JS script
2023-09-10 02:19:26 +00:00
JavaScriptBridge.eval("""
var _HTML5FileExchange = {};
_HTML5FileExchange.upload = function(gd_callback) {
2020-06-03 17:03:57 +00:00
canceled = true;
var input = document.createElement('INPUT');
input.setAttribute("type", "file");
input.setAttribute("accept", "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();
this.fileType = file.type;
// var fileName = file.name;
2020-06-03 17:03:57 +00:00
reader.readAsArrayBuffer(file);
reader.onloadend = (evt) => { // Since here's it's arrow function, "this" still refers to _HTML5FileExchange
2020-06-03 17:03:57 +00:00
if (evt.target.readyState == FileReader.DONE) {
this.result = evt.target.result;
gd_callback(); // It's hard to retrieve value from callback argument, so it's just for notification
2020-06-03 17:03:57 +00:00
}
}
});
}
""", true)
func load_handler(_args):
emit_signal("read_completed")
2020-06-03 17:03:57 +00:00
func load_image():
2023-09-10 03:45:00 +00:00
if OS.get_name() != "Web" or !OS.has_feature('JavaScript'):
2020-06-03 17:03:57 +00:00
return
js_interface.upload(js_callback);
2023-09-10 03:45:00 +00:00
await self.read_completed
2020-06-03 17:03:57 +00:00
var imageType = js_interface.fileType;
2023-09-10 02:19:26 +00:00
var imageData = JavaScriptBridge.eval("_HTML5FileExchange.result", true) # interface doesn't work as expected for some reason
2020-06-03 17:03:57 +00:00
var image = Image.new()
var image_error
match imageType:
"image/png":
image_error = image.load_png_from_buffer(imageData)
"image/jpeg":
image_error = image.load_jpg_from_buffer(imageData)
"image/webp":
image_error = image.load_webp_from_buffer(imageData)
var invalidType:
print("Unsupported file format - %s." % invalidType)
2020-06-03 17:03:57 +00:00
return
2020-06-03 17:03:57 +00:00
if image_error:
print("An error occurred while trying to display the image.")
emit_signal("load_completed", image)
2020-06-03 17:03:57 +00:00
func save_image(image:Image, fileName:String = "export.png")->void:
2023-09-10 03:45:00 +00:00
if OS.get_name() != "Web" or !OS.has_feature('JavaScript'):
2020-06-03 17:03:57 +00:00
return
2020-06-03 17:03:57 +00:00
image.clear_mipmaps()
var buffer = image.save_png_to_buffer()
2023-09-10 02:19:26 +00:00
JavaScriptBridge.download_buffer(buffer, fileName)