@@ -8,7 +8,7 @@ signal was_unloaded
88signal active_changed
99
1010var source_path : String
11- var processed_path : String
11+ var processed_path : String = ""
1212var is_currently_loaded := false
1313var was_already_exported := false
1414var relative_path : String
@@ -19,7 +19,8 @@ var preview_texture : Texture2D = null
1919var index : int = 0
2020var total_rotations : int = 0
2121
22- var is_loading_image : bool = false
22+ var is_loading_images : int = 0
23+ var is_exporting_image : bool = false
2324
2425var exported_section : Rect2i = Rect2i ()
2526
@@ -38,18 +39,32 @@ func load_image() -> void:
3839 if original_texture != null and preview_texture != null :
3940 return
4041
41- if is_loading_image :
42+ if is_loading_images != 0 :
4243 return
4344
44- is_loading_image = true
45+ if processed_path != "" :
46+ is_loading_images = 2
47+ else :
48+ is_loading_images = 1
4549
4650 Application .threaded_load_image (
4751 source_path ,
4852 func (image : ImageTexture ):
4953 original_texture = image
50- was_loaded .emit ()
51- is_loading_image = false
54+ is_loading_images -= 1
55+ if is_loading_images == 0 :
56+ was_loaded .emit ()
5257 )
58+
59+ if processed_path != "" :
60+ Application .threaded_load_image (
61+ processed_path ,
62+ func (image : ImageTexture ):
63+ preview_texture = image
64+ is_loading_images -= 1
65+ if is_loading_images == 0 :
66+ was_loaded .emit ()
67+ )
5368
5469func rotate (direction : int ) -> void :
5570 if original_texture == null :
@@ -75,18 +90,58 @@ func unload_image() -> bool:
7590
7691func export_image (position : Rect2i ) -> void :
7792
78- exported_section = position
93+ # If the crop region didn't change, we do not need to do anything
94+ if position == exported_section :
95+ return
96+
97+ if is_exporting_image :
98+ return
7999
80100 # We now have our image section, where we extract a new image
81101 if original_texture != null :
82102
103+ is_exporting_image = true
104+
83105 Application .threaded_crop_image (
84106 original_texture .get_image (),
85107 position ,
86108 func (image : Image ):
87109 preview_texture = ImageTexture .create_from_image (image )
88- was_already_exported = true
89- was_exported .emit ()
110+
111+ # Create a formatted file name for the image
112+ var file_name : String = Application .output_template .format ({
113+ "directory_tree" : relative_path .get_base_dir ().path_join ("" ),
114+ "file" : relative_path .get_file ().trim_suffix ("." + relative_path .get_extension ()),
115+ "extension" : relative_path .get_extension () if Application .current_file_format == 0 else ["png" , "jpg" , "webp" ][Application .current_file_format - 1 ],
116+ "index" : index ,
117+ "width" : Application .crop_to_size .x ,
118+ "height" : Application .crop_to_size .x ,
119+ "rotations" : total_rotations ,
120+ "rect.x" : exported_section .position .x ,
121+ "rect.y" : exported_section .position .y ,
122+ "rect.width" : exported_section .size .x ,
123+ "rect.height" : exported_section .size .y ,
124+ })
125+
126+ # Remove leading / from the filename and join the path with the output directory
127+ var path := Application .output_path .path_join (file_name .trim_prefix ("/" ))
128+
129+ exported_section = position
130+
131+ Application .threaded_save_image (
132+ image ,
133+ path ,
134+ func (status : int ):
135+
136+ is_exporting_image = false
137+
138+ if status == OK :
139+ processed_path = path
140+ was_already_exported = true
141+ was_exported .emit ()
142+ else :
143+ print ("File - " + path + " - was exported unsuccessfully - status: " + error_string (status ))
144+ )
90145 )
91146
92147
0 commit comments