From a0c7dd452761a077ff2f07cd4489248c3ae4e752 Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Thu, 5 Sep 2024 04:50:36 +0300 Subject: [PATCH] Optimize the export dialog by caching all of the blended frames No more slowness when changing the export settings. The only setting that causes the frames to be blended again is changing the layers. --- src/Autoload/Export.gd | 18 ++++++++++++++++-- src/UI/Dialogs/ExportDialog.gd | 2 ++ src/UI/Dialogs/ExportDialog.tscn | 2 +- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Autoload/Export.gd b/src/Autoload/Export.gd index 89ec8c49e..bb10a3f1c 100644 --- a/src/Autoload/Export.gd +++ b/src/Autoload/Export.gd @@ -46,6 +46,9 @@ var custom_exporter_generators := {} var current_tab := ExportTab.IMAGE ## All frames and their layers processed/blended into images var processed_images: Array[ProcessedImage] = [] +## Dictionary of [Frame] and [Image] that contains all of the blended frames. +## Changes when [method cache_blended_frames] is called. +var blended_frames := {} var export_json := false var split_layers := false var trim_sprite := false @@ -137,6 +140,7 @@ func remove_custom_file_format(id: int) -> void: func external_export(project := Global.current_project) -> void: + cache_blended_frames(project) process_data(project) export_processed_images(true, Global.export_dialog, project) @@ -149,6 +153,15 @@ func process_data(project := Global.current_project) -> void: process_spritesheet(project) +func cache_blended_frames(project := Global.current_project) -> void: + blended_frames.clear() + var frames := _calculate_frames(project) + for frame in frames: + var image := Image.create(project.size.x, project.size.y, false, Image.FORMAT_RGBA8) + _blend_layers(image, frame) + blended_frames[frame] = image + + func process_spritesheet(project := Global.current_project) -> void: processed_images.clear() # Range of frames determined by tags @@ -252,7 +265,8 @@ func process_spritesheet(project := Global.current_project) -> void: origin.y = project.size.y * tag_origins[0] origin.x = 0 tag_origins[0] += 1 - _blend_layers(whole_image, frame, origin) + whole_image.blend_rect(blended_frames[frame], Rect2i(Vector2i.ZERO, project.size), origin) + #_blend_layers(whole_image, frame, origin) processed_images.append(ProcessedImage.new(whole_image, 0)) @@ -271,7 +285,7 @@ func process_animation(project := Global.current_project) -> void: ) else: var image := Image.create(project.size.x, project.size.y, false, Image.FORMAT_RGBA8) - _blend_layers(image, frame) + image.copy_from(blended_frames[frame]) if trim_sprite: image = image.get_region(image.get_used_rect()) var duration := frame.duration * (1.0 / project.fps) diff --git a/src/UI/Dialogs/ExportDialog.gd b/src/UI/Dialogs/ExportDialog.gd index 8924584e3..5683294f6 100644 --- a/src/UI/Dialogs/ExportDialog.gd +++ b/src/UI/Dialogs/ExportDialog.gd @@ -290,6 +290,7 @@ func _on_ExportDialog_about_to_show() -> void: path_dialog_popup.current_dir = project.export_directory_path file_line_edit.text = project.file_name file_format_options.selected = project.file_format + Export.cache_blended_frames() show_tab() # Set the size of the preview checker @@ -472,6 +473,7 @@ func _on_Frames_item_selected(id: int) -> void: func _on_Layers_item_selected(id: int) -> void: Export.export_layers = id + Export.cache_blended_frames() Export.process_data() set_preview() diff --git a/src/UI/Dialogs/ExportDialog.tscn b/src/UI/Dialogs/ExportDialog.tscn index b61e2446e..925094e25 100644 --- a/src/UI/Dialogs/ExportDialog.tscn +++ b/src/UI/Dialogs/ExportDialog.tscn @@ -82,8 +82,8 @@ mouse_default_cursor_shape = 2 selected = 0 item_count = 4 popup/item_0/text = "Columns" -popup/item_0/id = 1 popup/item_1/text = "Rows" +popup/item_1/id = 1 popup/item_2/text = "Tags by column" popup/item_2/id = 2 popup/item_3/text = "Tags by row"