From 134867857b1b5786d17f732b6ab16d742d8721d8 Mon Sep 17 00:00:00 2001 From: Jaap Marsman Date: Sun, 13 Nov 2022 07:33:59 +0800 Subject: [PATCH] functional results export completed --- godot/Results.gd | 72 ++++++++++++++++++++++++++++++++++-------- godot/global_ints.gd | 4 +-- godot/main_screen.gd | 2 +- godot/main_screen.tscn | 2 -- 4 files changed, 62 insertions(+), 18 deletions(-) diff --git a/godot/Results.gd b/godot/Results.gd index e73196e..0f79ca5 100644 --- a/godot/Results.gd +++ b/godot/Results.gd @@ -3,9 +3,45 @@ extends CanvasLayer onready var global_ints = $"/root/GlobalInts" +var js_callback = JavaScript.create_callback(self, "load_handler"); +var js_interface; +var image : Image +var fileName : String = "results.png" func _ready(): - pass + if OS.get_name() == "HTML5" and OS.has_feature('JavaScript'): + _define_js() + js_interface = JavaScript.get_interface("_HTML5FileExchange"); + + +func _define_js()->void: + #Define JS script + JavaScript.eval(""" + var _HTML5FileExchange = {}; + _HTML5FileExchange.upload = function(gd_callback) { + 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; + reader.readAsArrayBuffer(file); + reader.onloadend = (evt) => { // Since here's it's arrow function, "this" still refers to _HTML5FileExchange + 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 + } + } + }); + } + """, true) + func _process(_delta): # $"%SaveReport".visible = true @@ -16,21 +52,31 @@ func _on_SaveReport_pressed(): $"%SaveReport".visible = false $"%BackMainMenu".visible = false - # WIP: Web Version -# var file = File.new() -# file.open("res://screenshot.png", File.READ) -# var base_64_data = Marshalls.raw_to_base64(file.get_buffer(file.get_len())) -# var url = "data:image/jpg;base64,"+base_64_data -# var comand = "var a = document.createElement('a'); a.href = '" + url + "'; a.setAttribute( 'download' , 'filename.jpg' ); a.click();" + print("I've disabled the buttons") + print("That means the screenshot SHOULD be button free") -# JavaScript.eval(comand, true) - - # WIP: Non-Web Version - var image = get_viewport().get_texture().get_data() - + image = get_viewport().get_texture().get_data() image.flip_y() - image.save_png("user://results.png") + + if OS.get_name() == "HTML5" or OS.has_feature('JavaScript'): + # We're on the web + print("We're on the web") + image.clear_mipmaps() + var buffer = image.save_png_to_buffer() + JavaScript.download_buffer(buffer, fileName) + + if OS.get_name() != "HTML5" or !OS.has_feature('JavaScript'): + # We're not on the web + print("We're not on the web") + + + var _error2 = image.save_png("user://results.png") + + var _error = OS.shell_open(OS.get_user_data_dir()) + + $"%SaveReport".visible = true + $"%BackMainMenu".visible = true func _on_BackMainMenu_pressed(): global_ints.reset_all_vars() diff --git a/godot/global_ints.gd b/godot/global_ints.gd index 405c0a4..779dc6c 100644 --- a/godot/global_ints.gd +++ b/godot/global_ints.gd @@ -8,9 +8,9 @@ var release_version = 0.10 var total_observed_time : int -var timer_duration : int = 6 +var timer_duration : int = 20 -var observation_minutes : int = 1 +var observation_minutes : int = 10 var observation_start_time : String var observation_end_time : String diff --git a/godot/main_screen.gd b/godot/main_screen.gd index 152db31..a55fe67 100644 --- a/godot/main_screen.gd +++ b/godot/main_screen.gd @@ -35,7 +35,7 @@ func _on_HTTPRequest_request_completed(_result, _response_code, _headers, body): $"%UpdateIntro".text = str("You are currently using version ",global_ints.release_version,". The latest version available is ",global_ints.web_release_version,".") -func _on_HTTPRequest2_request_completed(_result, _response_code, _headers, body): +func _on_HTTPRequest2_request_completed(_result, _response_code, _headers, _body): # Need to find a way to load .txt file contents from a URL into a label. This to show what the update changes are. # var json = JSON.parse(body.get_string_from_utf8()) # update_text = str(json.result) diff --git a/godot/main_screen.tscn b/godot/main_screen.tscn index 05343aa..f45fc91 100644 --- a/godot/main_screen.tscn +++ b/godot/main_screen.tscn @@ -47,7 +47,6 @@ bg_color = Color( 0.862745, 0.435294, 0.329412, 1 ) bg_color = Color( 1, 1, 1, 1 ) [sub_resource type="DynamicFont" id=9] -size = 14 font_data = ExtResource( 5 ) [sub_resource type="DynamicFont" id=12] @@ -1130,7 +1129,6 @@ unique_name_in_owner = true wait_time = 20.0 [node name="Results" type="CanvasLayer" parent="."] -visible = false script = ExtResource( 9 ) [node name="Panel" type="Panel" parent="Results"]