From 3bf2f5d7611344a7f1e68bcbf4d0d5fd8f5f2887 Mon Sep 17 00:00:00 2001 From: Nordup Date: Sun, 24 Mar 2024 04:24:21 +0400 Subject: [PATCH] change RenderResult size, don't show gate thumbnail if pack cached --- project/resources/gate_events.res | Bin 212 -> 205 bytes project/scripts/loading/file_downloader.gd | 8 +++- project/scripts/loading/gate_loader.gd | 3 +- project/scripts/resources/gate_events.gd | 6 +-- project/scripts/sandbox/render_result.gd | 46 +++++++++++++-------- project/scripts/ui/menu/star.gd | 2 +- project/scripts/ui/world/gate_info.gd | 2 +- project/scripts/ui/world/loading_status.gd | 2 +- 8 files changed, 42 insertions(+), 27 deletions(-) diff --git a/project/resources/gate_events.res b/project/resources/gate_events.res index 06dc50120ce8461a3ba8b22b26fed893baaa24d2..822ad51c02e924b25695a186f161bde5134683c6 100644 GIT binary patch literal 205 zcmV;;05bnlQ$s@n000005C8x~0RRBC0000ewJ-f(L;zg{00PJxCXg$Q0sp!GKh^*L zGqO!u_}itC$!$-IXcrn<@tcr5cOqpZ8LU zPXEDaY0q%JbjskesW~{Y_#b{q_hkl>+aeAG2hvD3lfQ7fh=H2xC;WmR@CQB@080xP zTENK)xCy|4B48>|sZ?mKD?$i(>X9{`g@=d2^VB#ntjx>);b6p>%#mf3_7|H1WDKAU HQd2`i2@YDn literal 212 zcmV;_04x7eQ$s@n000005C8y90RRBJ0000ewJ-f(P5_k!0AffTCXg$Q0rzv=-9hf| zjuL9p!Y3&#GP&Tpu|v{l{s9fjE;6 void: FileTools.remove_recursive(DOWNLOAD_FOLDER) +func is_cached(url: String) -> bool: + var save_path = DOWNLOAD_FOLDER + "/" + url.md5_text() + "." + url.get_file().get_extension() + return FileAccess.file_exists(save_path) + + func download(url: String, timeout: float = 0) -> String: var save_path = DOWNLOAD_FOLDER + "/" + url.md5_text() + "." + url.get_file().get_extension() @@ -31,7 +36,6 @@ func download(url: String, timeout: float = 0) -> String: DirAccess.make_dir_recursive_absolute(save_path.get_base_dir()) var result = await create_request(url, save_path, timeout) - if result == 200: return save_path else: @@ -50,7 +54,6 @@ func download_shared_lib(url: String, gate_url: String) -> String: DirAccess.make_dir_recursive_absolute(dir) var result = await create_request(url, save_path) - if result == 200: return dir else: @@ -108,4 +111,5 @@ func stop_all() -> void: func _exit_tree() -> void: + FileDownloader.stop_all() FileTools.remove_recursive(DOWNLOAD_FOLDER) diff --git a/project/scripts/loading/gate_loader.gd b/project/scripts/loading/gate_loader.gd index fd6b856..b1b5340 100644 --- a/project/scripts/loading/gate_loader.gd +++ b/project/scripts/loading/gate_loader.gd @@ -21,7 +21,8 @@ func load_gate(config_url: String) -> void: var image_path = await FileDownloader.download(c_gate.image_url) var gate = Gate.create(config_url, c_gate.title, c_gate.description, image_path, "", "") - gate_events.gate_info_loaded_emit(gate) + var is_cached = FileDownloader.is_cached(c_gate.resource_pack_url) + gate_events.gate_info_loaded_emit(gate, is_cached) gate.resource_pack = await FileDownloader.download(c_gate.resource_pack_url) if gate.resource_pack.is_empty(): return error(GateEvents.GateError.MISSING_PACK) diff --git a/project/scripts/resources/gate_events.gd b/project/scripts/resources/gate_events.gd index 45da0d0..c8070ac 100644 --- a/project/scripts/resources/gate_events.gd +++ b/project/scripts/resources/gate_events.gd @@ -4,7 +4,7 @@ class_name GateEvents signal search(query: String) signal open_gate(url: String) signal gate_config_loaded(url: String, config: ConfigGate) -signal gate_info_loaded(gate: Gate) +signal gate_info_loaded(gate: Gate, is_cached: bool) signal gate_loaded(gate: Gate) signal gate_entered signal exit_gate @@ -42,9 +42,9 @@ func gate_config_loaded_emit(url: String, config: ConfigGate) -> void: gate_config_loaded.emit(url, config) -func gate_info_loaded_emit(gate: Gate) -> void: +func gate_info_loaded_emit(gate: Gate, is_cached: bool) -> void: current_gate = gate - gate_info_loaded.emit(gate) + gate_info_loaded.emit(gate, is_cached) func gate_loaded_emit(gate: Gate) -> void: diff --git a/project/scripts/sandbox/render_result.gd b/project/scripts/sandbox/render_result.gd index fde39a7..52fcd78 100644 --- a/project/scripts/sandbox/render_result.gd +++ b/project/scripts/sandbox/render_result.gd @@ -14,29 +14,35 @@ var texture_rid: RID func _ready() -> void: - gate_events.gate_entered.connect(create_external_texture) gate_events.gate_info_loaded.connect(initialize) + gate_events.gate_entered.connect(create_external_texture) command_events.send_filehandle.connect(send_filehandle) + + # Change size + var image = resize_and_convert(splash_screen.get_image(), Image.FORMAT_RGB8) + self.texture = ImageTexture.create_from_image(image) -func initialize(gate: Gate) -> void: +func initialize(gate: Gate, is_cached: bool) -> void: rd = RenderingServer.get_rendering_device() - var image: Image - var tex = FileTools.load_external_tex(gate.image) - if tex != null: - image = tex.get_image() - image.resize(width, height) - image.convert(Image.FORMAT_RGB8) - image.clear_mipmaps() - else: - image = Image.create(width, height, false, Image.FORMAT_RGB8) + if not is_cached: # Show thumbnail image + self.texture = create_gate_image(gate) - self.texture = ImageTexture.create_from_image(image) texture_rid = RenderingServer.texture_get_rd_texture(self.texture.get_rid()) if not texture_rid.is_valid(): Debug.logerr("Cannot create ImageTexture") +func create_gate_image(gate: Gate) -> ImageTexture: + var tex = FileTools.load_external_tex(gate.image) + + var image: Image + if tex != null: image = resize_and_convert(tex.get_image(), Image.FORMAT_RGB8) + else: image = Image.create(width, height, false, Image.FORMAT_RGB8) + + return ImageTexture.create_from_image(image) + + func create_external_texture() -> void: var t_format: RDTextureFormat = RDTextureFormat.new() t_format.format = RenderingDevice.DATA_FORMAT_R8G8B8A8_UNORM @@ -47,11 +53,6 @@ func create_external_texture() -> void: t_format.depth = 1 var t_view: RDTextureView = RDTextureView.new() - var image = splash_screen.get_image() - image.resize(width, height) - image.convert(Image.FORMAT_RGBA8) - image.clear_mipmaps() - # For some reason when switching scene something is not freed # So need to wait to free that up await get_tree().process_frame @@ -59,11 +60,19 @@ func create_external_texture() -> void: await get_tree().process_frame ext_texure = ExternalTexture.new() + var image = resize_and_convert(splash_screen.get_image(), Image.FORMAT_RGBA8) var err = ext_texure.create(t_format, t_view, [image.get_data()]) - if err: Debug.logerr("Cannot create external texture"); return + if err: Debug.logerr("Cannot create external texture") else: Debug.logclr("External texture created", Color.AQUAMARINE) +func resize_and_convert(image: Image, format: Image.Format) -> Image: + image.resize(width, height) + image.convert(format) + image.clear_mipmaps() + return image + + func send_filehandle(filehandle_path: String) -> void: Debug.logr("Sending filehandle...") var sent = false @@ -76,4 +85,5 @@ func send_filehandle(filehandle_path: String) -> void: func _process(_delta: float) -> void: if ext_texure == null or not ext_texure.get_rid().is_valid(): return if not texture_rid.is_valid(): return + ext_texure.copy_to(texture_rid) diff --git a/project/scripts/ui/menu/star.gd b/project/scripts/ui/menu/star.gd index 996c527..2111176 100644 --- a/project/scripts/ui/menu/star.gd +++ b/project/scripts/ui/menu/star.gd @@ -38,7 +38,7 @@ func hide_buttons() -> void: unstar.visible = false -func update_info(_gate: Gate) -> void: +func update_info(_gate: Gate, _is_cached: bool) -> void: gate = _gate if bookmarks.gates.has(gate.url): bookmarks.update(gate) diff --git a/project/scripts/ui/world/gate_info.gd b/project/scripts/ui/world/gate_info.gd index cf46be0..ac5b424 100644 --- a/project/scripts/ui/world/gate_info.gd +++ b/project/scripts/ui/world/gate_info.gd @@ -14,7 +14,7 @@ func _ready() -> void: gate_events.gate_error.connect(on_gate_error) -func display_info(_gate: Gate) -> void: +func display_info(_gate: Gate, _is_cached: bool) -> void: gate = _gate title.text = "Unnamed" if gate.title.is_empty() else gate.title description.text = "No description" if gate.description.is_empty() else gate.description diff --git a/project/scripts/ui/world/loading_status.gd b/project/scripts/ui/world/loading_status.gd index 5ff30dc..86b463f 100644 --- a/project/scripts/ui/world/loading_status.gd +++ b/project/scripts/ui/world/loading_status.gd @@ -4,7 +4,7 @@ extends Label func _ready() -> void: - gate_events.gate_info_loaded.connect(func(_gate): on_gate_info_loaded()) + gate_events.gate_info_loaded.connect(func(_gate, _is_cached): on_gate_info_loaded()) gate_events.gate_entered.connect(on_gate_entered) gate_events.gate_error.connect(on_gate_error) set_text("Connecting...")