From c4a4bd4602a59d9d0144cbfd09c16cb072caef20 Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas Date: Sun, 11 Feb 2024 16:01:59 +0200 Subject: [PATCH] Implement lazy loading for some dialogs to make the initial loading of Pixelorama faster Only load dialogs when they are needed and not at the start of the program. Right now only the new image, about, manage layouts and image effects dialogs are being loaded on demand, but more dialogs can be handled this way in future commits. --- src/Main.tscn | 16 +--- src/UI/Dialogs/CreateNewImage.gd | 5 ++ src/UI/Dialogs/CreateNewImage.tscn | 1 + src/UI/Dialogs/ImageEffects/ImageEffects.tscn | 49 ----------- src/UI/TopMenuContainer/TopMenuContainer.gd | 83 ++++++++++++++----- 5 files changed, 69 insertions(+), 85 deletions(-) delete mode 100644 src/UI/Dialogs/ImageEffects/ImageEffects.tscn diff --git a/src/Main.tscn b/src/Main.tscn index e814aa116..f5cae27e7 100644 --- a/src/Main.tscn +++ b/src/Main.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=21 format=3 uid="uid://dbylw5k04ulp8"] +[gd_scene load_steps=17 format=3 uid="uid://dbylw5k04ulp8"] [ext_resource type="Theme" uid="uid://cngbvfpwjoimv" path="res://assets/themes/dark/theme.tres" id="1"] [ext_resource type="Script" path="res://src/Main.gd" id="2"] @@ -6,19 +6,15 @@ [ext_resource type="PackedScene" uid="uid://c8dsi6ggkqa7a" path="res://src/UI/UI.tscn" id="4"] [ext_resource type="PackedScene" uid="uid://d1jyt8wleg8ft" path="res://src/UI/Buttons/PatternsPopup.tscn" id="5"] [ext_resource type="PackedScene" uid="uid://c5x85ysdouh4t" path="res://src/UI/Buttons/BrushesPopup.tscn" id="6"] -[ext_resource type="PackedScene" uid="uid://cfihuvhy4ocx7" path="res://src/UI/Dialogs/ImageEffects/ImageEffects.tscn" id="7"] [ext_resource type="PackedScene" uid="uid://cltlo2whtiejd" path="res://src/UI/Dialogs/SaveSpriteHTML5.tscn" id="8"] [ext_resource type="PackedScene" uid="uid://bs3dnnvnxyp68" path="res://src/UI/Timeline/FrameProperties.tscn" id="9"] [ext_resource type="PackedScene" uid="uid://bcdt0pa7rojy5" path="res://src/UI/Dialogs/WindowOpacityDialog.tscn" id="10"] [ext_resource type="PackedScene" uid="uid://d4euwo633u33b" path="res://src/UI/Dialogs/SaveSprite.tscn" id="11"] [ext_resource type="PackedScene" uid="uid://b3aeqj2k58wdk" path="res://src/UI/Dialogs/OpenSprite.tscn" id="12"] -[ext_resource type="PackedScene" uid="uid://d1phd84dk4t5e" path="res://src/UI/Dialogs/ManageLayouts.tscn" id="13"] [ext_resource type="PackedScene" uid="uid://c0nuukjakmai2" path="res://src/UI/Dialogs/TileModeOffsetsDialog.tscn" id="14"] [ext_resource type="PackedScene" uid="uid://clbjfkdupw52l" path="res://src/UI/Timeline/CelProperties.tscn" id="17_ucs64"] [ext_resource type="PackedScene" uid="uid://bnpb1aip3wdvw" path="res://src/UI/Dialogs/SplashDialog.tscn" id="27"] -[ext_resource type="PackedScene" uid="uid://btjf3lj873q1t" path="res://src/UI/Dialogs/CreateNewImage.tscn" id="28"] [ext_resource type="PackedScene" uid="uid://b3hkjj3s6pe4x" path="res://src/Preferences/PreferencesDialog.tscn" id="32"] -[ext_resource type="PackedScene" uid="uid://w1v6plnv6ppt" path="res://src/UI/Dialogs/AboutDialog.tscn" id="34"] [ext_resource type="PackedScene" uid="uid://clgu8wb5o6oup" path="res://src/UI/Dialogs/ExportDialog.tscn" id="39"] [node name="Control" type="Control"] @@ -47,16 +43,12 @@ layout_mode = 2 anchors_preset = 0 mouse_filter = 2 -[node name="ImageEffects" parent="Dialogs" instance=ExtResource("7")] - [node name="BrushesPopup" parent="Dialogs" instance=ExtResource("6")] [node name="PatternsPopup" parent="Dialogs" instance=ExtResource("5")] [node name="SplashDialog" parent="Dialogs" instance=ExtResource("27")] -[node name="CreateNewImage" parent="Dialogs" instance=ExtResource("28")] - [node name="OpenSprite" parent="Dialogs" instance=ExtResource("12")] [node name="SaveSprite" parent="Dialogs" instance=ExtResource("11")] @@ -67,11 +59,6 @@ mouse_filter = 2 [node name="PreferencesDialog" parent="Dialogs" instance=ExtResource("32")] -[node name="ManageLayouts" parent="Dialogs" instance=ExtResource("13")] - -[node name="AboutDialog" parent="Dialogs" instance=ExtResource("34")] -position = Vector2i(0, 24) - [node name="UnsavedCanvasDialog" type="ConfirmationDialog" parent="Dialogs"] title = "Unsaved Image" exclusive = false @@ -118,7 +105,6 @@ visible = false visible = false [connection signal="visibility_changed" from="Dialogs/SplashDialog" to="." method="_can_draw_true"] -[connection signal="visibility_changed" from="Dialogs/CreateNewImage" to="." method="_can_draw_true"] [connection signal="files_selected" from="Dialogs/OpenSprite" to="." method="_on_OpenSprite_files_selected"] [connection signal="visibility_changed" from="Dialogs/OpenSprite" to="." method="_on_open_sprite_visibility_changed"] [connection signal="file_selected" from="Dialogs/SaveSprite" to="." method="_on_SaveSprite_file_selected"] diff --git a/src/UI/Dialogs/CreateNewImage.gd b/src/UI/Dialogs/CreateNewImage.gd index d4809a948..f97aa3440 100644 --- a/src/UI/Dialogs/CreateNewImage.gd +++ b/src/UI/Dialogs/CreateNewImage.gd @@ -219,3 +219,8 @@ func switch_width_height() -> void: width_value.value_changed.connect(_on_SizeValue_value_changed) height_value.value_changed.connect(_on_SizeValue_value_changed) + + +func _on_visibility_changed() -> void: + if not visible: + Global.dialog_open(false) diff --git a/src/UI/Dialogs/CreateNewImage.tscn b/src/UI/Dialogs/CreateNewImage.tscn index 4b750a914..eb3d5e6d3 100644 --- a/src/UI/Dialogs/CreateNewImage.tscn +++ b/src/UI/Dialogs/CreateNewImage.tscn @@ -218,6 +218,7 @@ color = Color(0, 0, 0, 0) [connection signal="about_to_popup" from="." to="." method="_on_CreateNewImage_about_to_show"] [connection signal="confirmed" from="." to="." method="_on_CreateNewImage_confirmed"] +[connection signal="visibility_changed" from="." to="." method="_on_visibility_changed"] [connection signal="item_selected" from="VBoxContainer/VBoxContainer/Templates/TemplatesContainer/TemplatesOptions" to="." method="_on_TemplatesOptions_item_selected"] [connection signal="value_changed" from="VBoxContainer/VBoxContainer/Templates/SizeContainer/VBoxContainer/WidthContainer/WidthValue" to="." method="_on_SizeValue_value_changed"] [connection signal="value_changed" from="VBoxContainer/VBoxContainer/Templates/SizeContainer/VBoxContainer/HeightContainer/HeightValue" to="." method="_on_SizeValue_value_changed"] diff --git a/src/UI/Dialogs/ImageEffects/ImageEffects.tscn b/src/UI/Dialogs/ImageEffects/ImageEffects.tscn deleted file mode 100644 index c05245db4..000000000 --- a/src/UI/Dialogs/ImageEffects/ImageEffects.tscn +++ /dev/null @@ -1,49 +0,0 @@ -[gd_scene load_steps=15 format=3 uid="uid://cfihuvhy4ocx7"] - -[ext_resource type="PackedScene" uid="uid://c7hnynip21y6u" path="res://src/UI/Dialogs/ImageEffects/FlipImageDialog.tscn" id="1"] -[ext_resource type="PackedScene" uid="uid://bl1t33abu7kl8" path="res://src/UI/Dialogs/ImageEffects/InvertColorsDialog.tscn" id="2"] -[ext_resource type="PackedScene" uid="uid://5gqka7tkg17k" path="res://src/UI/Dialogs/ImageEffects/DesaturateDialog.tscn" id="3"] -[ext_resource type="PackedScene" uid="uid://duse1w3q7whmm" path="res://src/UI/Dialogs/ImageEffects/DropShadowDialog.tscn" id="4"] -[ext_resource type="PackedScene" uid="uid://cjd5sjemyea4a" path="res://src/UI/Dialogs/ImageEffects/GradientMapDialog.tscn" id="5"] -[ext_resource type="PackedScene" uid="uid://cnryslyvxv4ye" path="res://src/UI/Dialogs/ImageEffects/Posterize.tscn" id="6"] -[ext_resource type="PackedScene" uid="uid://q355qqjvuajs" path="res://src/UI/Dialogs/ImageEffects/OffsetImage.tscn" id="7"] -[ext_resource type="PackedScene" uid="uid://birggxam3fjnb" path="res://src/UI/Dialogs/ImageEffects/ResizeCanvas.tscn" id="8"] -[ext_resource type="PackedScene" uid="uid://bvr6j8sohxob3" path="res://src/UI/Dialogs/ImageEffects/RotateImage.tscn" id="9"] -[ext_resource type="PackedScene" uid="uid://bkr47ocij684y" path="res://src/UI/Dialogs/ImageEffects/ShaderEffect.tscn" id="10"] -[ext_resource type="PackedScene" uid="uid://oq036vr10yvk" path="res://src/UI/Dialogs/ImageEffects/HSVDialog.tscn" id="11"] -[ext_resource type="PackedScene" uid="uid://u0y5xogg636e" path="res://src/UI/Dialogs/ImageEffects/GradientDialog.tscn" id="12"] -[ext_resource type="PackedScene" uid="uid://ci2qpf6t3dyyr" path="res://src/UI/Dialogs/ImageEffects/OutlineDialog.tscn" id="13"] -[ext_resource type="PackedScene" uid="uid://omm7xmtf5kbj" path="res://src/UI/Dialogs/ImageEffects/ScaleImage.tscn" id="14"] - -[node name="ImageEffects" type="Control"] -layout_mode = 3 -anchors_preset = 0 -mouse_filter = 2 - -[node name="OffsetImage" parent="." instance=ExtResource("7")] - -[node name="ScaleImage" parent="." instance=ExtResource("14")] - -[node name="ResizeCanvas" parent="." instance=ExtResource("8")] - -[node name="RotateImage" parent="." instance=ExtResource("9")] - -[node name="FlipImageDialog" parent="." instance=ExtResource("1")] - -[node name="InvertColorsDialog" parent="." instance=ExtResource("2")] - -[node name="DesaturateDialog" parent="." instance=ExtResource("3")] - -[node name="OutlineDialog" parent="." instance=ExtResource("13")] - -[node name="DropShadowDialog" parent="." instance=ExtResource("4")] - -[node name="HSVDialog" parent="." instance=ExtResource("11")] - -[node name="GradientDialog" parent="." instance=ExtResource("12")] - -[node name="GradientMapDialog" parent="." instance=ExtResource("5")] - -[node name="Posterize" parent="." instance=ExtResource("6")] - -[node name="ShaderEffect" parent="." instance=ExtResource("10")] diff --git a/src/UI/TopMenuContainer/TopMenuContainer.gd b/src/UI/TopMenuContainer/TopMenuContainer.gd index 7fceb2f03..db88691e2 100644 --- a/src/UI/TopMenuContainer/TopMenuContainer.gd +++ b/src/UI/TopMenuContainer/TopMenuContainer.gd @@ -13,6 +13,25 @@ var recent_projects := [] var selected_layout := 0 var zen_mode := false +# Dialogs +var new_image_dialog := Dialog.new("res://src/UI/Dialogs/CreateNewImage.tscn") +var offset_image_dialog := Dialog.new("res://src/UI/Dialogs/ImageEffects/OffsetImage.tscn") +var scale_image_dialog := Dialog.new("res://src/UI/Dialogs/ImageEffects/ScaleImage.tscn") +var resize_canvas_dialog := Dialog.new("res://src/UI/Dialogs/ImageEffects/ResizeCanvas.tscn") +var mirror_image_dialog := Dialog.new("res://src/UI/Dialogs/ImageEffects/FlipImageDialog.tscn") +var rotate_image_dialog := Dialog.new("res://src/UI/Dialogs/ImageEffects/RotateImage.tscn") +var invert_colors_dialog := Dialog.new("res://src/UI/Dialogs/ImageEffects/InvertColorsDialog.tscn") +var desaturate_dialog := Dialog.new("res://src/UI/Dialogs/ImageEffects/DesaturateDialog.tscn") +var outline_dialog := Dialog.new("res://src/UI/Dialogs/ImageEffects/OutlineDialog.tscn") +var drop_shadow_dialog := Dialog.new("res://src/UI/Dialogs/ImageEffects/DropShadowDialog.tscn") +var hsv_dialog := Dialog.new("res://src/UI/Dialogs/ImageEffects/HSVDialog.tscn") +var gradient_dialog := Dialog.new("res://src/UI/Dialogs/ImageEffects/GradientDialog.tscn") +var gradient_map_dialog := Dialog.new("res://src/UI/Dialogs/ImageEffects/GradientMapDialog.tscn") +var posterize_dialog := Dialog.new("res://src/UI/Dialogs/ImageEffects/Posterize.tscn") +var shader_effect_dialog := Dialog.new("res://src/UI/Dialogs/ImageEffects/ShaderEffect.tscn") +var manage_layouts_dialog := Dialog.new("res://src/UI/Dialogs/ManageLayouts.tscn") +var about_dialog := Dialog.new("res://src/UI/Dialogs/AboutDialog.tscn") + @onready var main_ui := Global.control.find_child("DockableContainer") as DockableContainer @onready var ui_elements := main_ui.get_children() @onready var file_menu: PopupMenu = $MenuBar/File @@ -24,7 +43,6 @@ var zen_mode := false @onready var help_menu: PopupMenu = $MenuBar/Help @onready var greyscale_vision: ColorRect = main_ui.find_child("GreyscaleVision") -@onready var new_image_dialog: ConfirmationDialog = Global.control.find_child("CreateNewImage") @onready var window_opacity_dialog: AcceptDialog = Global.control.find_child("WindowOpacityDialog") @onready var tile_mode_submenu := PopupMenu.new() @onready var snap_to_submenu := PopupMenu.new() @@ -33,6 +51,30 @@ var zen_mode := false @onready var recent_projects_submenu := PopupMenu.new() +class Dialog: + ## This class is used to help with lazy loading dialog scenes in order to + ## reduce Pixelorama's initial loading time, by only loading each dialog + ## scene when it's actually needed. + var scene_path := "" + var node: Window + + func _init(_scene_path: String) -> void: + scene_path = _scene_path + + func popup(dialog_size := Vector2i.ZERO) -> void: + if not is_instance_valid(node): + var scene := load(scene_path) + if not scene is PackedScene: + return + node = scene.instantiate() + if not is_instance_valid(node): + return + Global.control.get_node("Dialogs").add_child(node) + node.popup_centered(dialog_size) + var is_file_dialog := node is FileDialog + Global.dialog_open(true, is_file_dialog) + + func _ready() -> void: _setup_file_menu() _setup_edit_menu() @@ -383,7 +425,7 @@ func _popup_dialog(dialog: Window, dialog_size := Vector2i.ZERO) -> void: func file_menu_id_pressed(id: int) -> void: match id: Global.FileMenu.NEW: - _popup_dialog(new_image_dialog) + new_image_dialog.popup() Global.FileMenu.OPEN: _open_project_file() Global.FileMenu.OPEN_LAST_PROJECT: @@ -466,7 +508,7 @@ func edit_menu_id_pressed(id: int) -> void: func view_menu_id_pressed(id: int) -> void: match id: Global.ViewMenu.TILE_MODE_OFFSETS: - _popup_dialog(Global.control.get_node("Dialogs/TileModeOffsetsDialog")) + _popup_dialog(Global.tile_mode_offset_dialog) Global.ViewMenu.GREYSCALE_VIEW: _toggle_greyscale_view() Global.ViewMenu.MIRROR_VIEW: @@ -540,7 +582,7 @@ func _panels_submenu_id_pressed(id: int) -> void: func _layouts_submenu_id_pressed(id: int) -> void: if id == 0: - _popup_dialog(Global.control.get_node("Dialogs/ManageLayouts")) + manage_layouts_dialog.popup() else: set_layout(id - 1) @@ -665,38 +707,37 @@ func _toggle_fullscreen() -> void: func image_menu_id_pressed(id: int) -> void: match id: Global.ImageMenu.SCALE_IMAGE: - _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/ScaleImage")) + scale_image_dialog.popup() Global.ImageMenu.OFFSET_IMAGE: - _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/OffsetImage")) + offset_image_dialog.popup() Global.ImageMenu.CROP_TO_SELECTION: DrawingAlgos.crop_to_selection() Global.ImageMenu.CROP_TO_CONTENT: DrawingAlgos.crop_to_content() Global.ImageMenu.RESIZE_CANVAS: - _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/ResizeCanvas")) + resize_canvas_dialog.popup() Global.ImageMenu.FLIP: - _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/FlipImageDialog")) + mirror_image_dialog.popup() Global.ImageMenu.ROTATE: - _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/RotateImage")) + rotate_image_dialog.popup() Global.ImageMenu.INVERT_COLORS: - _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/InvertColorsDialog")) + invert_colors_dialog.popup() Global.ImageMenu.DESATURATION: - _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/DesaturateDialog")) + desaturate_dialog.popup() Global.ImageMenu.OUTLINE: - _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/OutlineDialog")) + outline_dialog.popup() Global.ImageMenu.DROP_SHADOW: - _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/DropShadowDialog")) + drop_shadow_dialog.popup() Global.ImageMenu.HSV: - _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/HSVDialog")) + hsv_dialog.popup() Global.ImageMenu.GRADIENT: - _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/GradientDialog")) + gradient_dialog.popup() Global.ImageMenu.GRADIENT_MAP: - _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/GradientMapDialog")) + gradient_map_dialog.popup() Global.ImageMenu.POSTERIZE: - _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/Posterize")) -# Global.ImageMenu.SHADER: -# _popup_dialog(Global.control.get_node("Dialogs/ImageEffects/ShaderEffect")) - + posterize_dialog.popup() + #Global.ImageMenu.SHADER: + #shader_effect_dialog.popup() _: _handle_metadata(id, image_menu) @@ -732,7 +773,7 @@ func help_menu_id_pressed(id: int) -> void: Global.HelpMenu.CHANGELOG: OS.shell_open(CHANGELOG_URL) Global.HelpMenu.ABOUT_PIXELORAMA: - _popup_dialog(Global.control.get_node("Dialogs/AboutDialog")) + about_dialog.popup() Global.HelpMenu.SUPPORT_PIXELORAMA: OS.shell_open(SUPPORT_URL) _: