2020-06-30 15:32:16 +00:00
|
|
|
extends Node
|
|
|
|
# Code taken and modified from https://github.com/Pukkah/HTML5-File-Exchange-for-Godot
|
|
|
|
# Thanks to Pukkah from GitHub for providing the original code
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
signal in_focus
|
2020-06-30 15:32:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
func _ready() -> void:
|
2021-11-25 12:48:30 +00:00
|
|
|
if OS.get_name() == "HTML5" and OS.has_feature("JavaScript"):
|
2020-06-30 15:32:16 +00:00
|
|
|
_define_js()
|
|
|
|
|
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
func _notification(notification: int) -> void:
|
2020-06-30 15:32:16 +00:00
|
|
|
if notification == MainLoop.NOTIFICATION_WM_FOCUS_IN:
|
2021-11-25 12:48:30 +00:00
|
|
|
emit_signal("in_focus")
|
2020-06-30 15:32:16 +00:00
|
|
|
|
2020-07-01 00:57:27 +00:00
|
|
|
|
2020-06-30 15:32:16 +00:00
|
|
|
func _define_js() -> void:
|
|
|
|
# Define JS script
|
2021-11-25 12:48:30 +00:00
|
|
|
JavaScript.eval(
|
|
|
|
"""
|
2020-06-30 15:32:16 +00:00
|
|
|
var fileData;
|
|
|
|
var fileType;
|
|
|
|
var fileName;
|
|
|
|
var canceled;
|
|
|
|
function upload_image() {
|
|
|
|
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();
|
|
|
|
fileType = file.type;
|
|
|
|
fileName = file.name;
|
|
|
|
reader.readAsArrayBuffer(file);
|
|
|
|
reader.onloadend = function (evt) {
|
|
|
|
if (evt.target.readyState == FileReader.DONE) {
|
|
|
|
fileData = evt.target.result;
|
|
|
|
}
|
|
|
|
}
|
2021-11-25 12:48:30 +00:00
|
|
|
});
|
2020-06-30 15:32:16 +00:00
|
|
|
}
|
2020-07-25 13:26:57 +00:00
|
|
|
function upload_shader() {
|
|
|
|
canceled = true;
|
|
|
|
var input = document.createElement('INPUT');
|
|
|
|
input.setAttribute("type", "file");
|
|
|
|
input.setAttribute("accept", ".shader");
|
|
|
|
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;
|
|
|
|
reader.readAsText(file);
|
|
|
|
reader.onloadend = function (evt) {
|
|
|
|
if (evt.target.readyState == FileReader.DONE) {
|
|
|
|
fileData = evt.target.result;
|
|
|
|
}
|
|
|
|
}
|
2021-11-25 12:48:30 +00:00
|
|
|
});
|
2020-07-25 13:26:57 +00:00
|
|
|
}
|
2021-11-25 12:48:30 +00:00
|
|
|
""",
|
|
|
|
true
|
|
|
|
)
|
2020-06-30 15:32:16 +00:00
|
|
|
|
|
|
|
|
2020-06-30 19:29:24 +00:00
|
|
|
func load_image() -> void:
|
2021-11-25 12:48:30 +00:00
|
|
|
if OS.get_name() != "HTML5" or !OS.has_feature("JavaScript"):
|
2020-06-30 15:32:16 +00:00
|
|
|
return
|
|
|
|
|
2020-07-01 00:57:27 +00:00
|
|
|
# Execute JS function
|
2021-11-25 12:48:30 +00:00
|
|
|
JavaScript.eval("upload_image();", true) # Opens prompt for choosing file
|
2020-06-30 15:32:16 +00:00
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
yield(self, "in_focus") # Wait until JS prompt is closed
|
2020-06-30 15:32:16 +00:00
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
yield(get_tree().create_timer(0.5), "timeout") # Give some time for async JS data load
|
2020-06-30 15:32:16 +00:00
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
if JavaScript.eval("canceled;", true): # If File Dialog closed w/o file
|
2020-06-30 15:32:16 +00:00
|
|
|
return
|
|
|
|
|
2020-07-01 00:57:27 +00:00
|
|
|
# Use data from png data
|
2020-07-01 01:48:18 +00:00
|
|
|
var image_data
|
2020-06-30 15:32:16 +00:00
|
|
|
while true:
|
2020-07-01 01:48:18 +00:00
|
|
|
image_data = JavaScript.eval("fileData;", true)
|
|
|
|
if image_data != null:
|
2020-06-30 15:32:16 +00:00
|
|
|
break
|
2021-11-25 12:48:30 +00:00
|
|
|
yield(get_tree().create_timer(1.0), "timeout") # Need more time to load data
|
2020-06-30 15:32:16 +00:00
|
|
|
|
2020-06-30 19:29:24 +00:00
|
|
|
var image_type = JavaScript.eval("fileType;", true)
|
|
|
|
var image_name = JavaScript.eval("fileName;", true)
|
2020-06-30 15:32:16 +00:00
|
|
|
|
|
|
|
var image = Image.new()
|
|
|
|
var image_error
|
2020-06-30 19:29:24 +00:00
|
|
|
match image_type:
|
2020-06-30 15:32:16 +00:00
|
|
|
"image/png":
|
2020-07-01 01:48:18 +00:00
|
|
|
image_error = image.load_png_from_buffer(image_data)
|
2020-06-30 15:32:16 +00:00
|
|
|
"image/jpeg":
|
2020-07-01 01:48:18 +00:00
|
|
|
image_error = image.load_jpg_from_buffer(image_data)
|
2020-06-30 15:32:16 +00:00
|
|
|
"image/webp":
|
2020-07-01 01:48:18 +00:00
|
|
|
image_error = image.load_webp_from_buffer(image_data)
|
2020-06-30 15:32:16 +00:00
|
|
|
var invalid_type:
|
2020-07-01 01:48:18 +00:00
|
|
|
print("Invalid type: " + invalid_type)
|
2020-06-30 15:32:16 +00:00
|
|
|
return
|
|
|
|
if image_error:
|
|
|
|
print("An error occurred while trying to display the image.")
|
|
|
|
return
|
|
|
|
else:
|
2020-06-30 19:29:24 +00:00
|
|
|
OpenSave.handle_loading_image(image_name, image)
|
2020-06-30 15:32:16 +00:00
|
|
|
|
|
|
|
|
2020-07-25 13:26:57 +00:00
|
|
|
func load_shader() -> void:
|
2021-11-25 12:48:30 +00:00
|
|
|
if OS.get_name() != "HTML5" or !OS.has_feature("JavaScript"):
|
2020-07-25 13:26:57 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# Execute JS function
|
2021-11-25 12:48:30 +00:00
|
|
|
JavaScript.eval("upload_shader();", true) # Opens prompt for choosing file
|
2020-07-25 13:26:57 +00:00
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
yield(self, "in_focus") # Wait until JS prompt is closed
|
2020-07-25 13:26:57 +00:00
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
yield(get_tree().create_timer(0.5), "timeout") # Give some time for async JS data load
|
2020-07-25 13:26:57 +00:00
|
|
|
|
2021-11-25 12:48:30 +00:00
|
|
|
if JavaScript.eval("canceled;", true): # If File Dialog closed w/o file
|
2020-07-25 13:26:57 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# Use data from png data
|
|
|
|
var file_data
|
|
|
|
while true:
|
|
|
|
file_data = JavaScript.eval("fileData;", true)
|
|
|
|
if file_data != null:
|
|
|
|
break
|
2021-11-25 12:48:30 +00:00
|
|
|
yield(get_tree().create_timer(1.0), "timeout") # Need more time to load data
|
2020-07-25 13:26:57 +00:00
|
|
|
|
|
|
|
# var file_type = JavaScript.eval("fileType;", true)
|
|
|
|
var file_name = JavaScript.eval("fileName;", true)
|
|
|
|
|
|
|
|
var shader = Shader.new()
|
|
|
|
shader.code = file_data
|
|
|
|
|
|
|
|
var shader_effect_dialog = Global.control.get_node("Dialogs/ImageEffects/ShaderEffect")
|
2020-07-28 01:11:06 +00:00
|
|
|
shader_effect_dialog.change_shader(shader, file_name.get_basename())
|