Debian Codegraph @

editor/scene/2d/tiles/atlas_merging_dialog.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* atlas_merging_dialog.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "atlas_merging_dialog.h"
32
33 #include "editor/editor_undo_redo_manager.h"
34 #include "editor/gui/editor_file_dialog.h"
35 #include "editor/themes/editor_scale.h"
36 #include "scene/gui/control.h"
37 #include "scene/gui/split_container.h"
38 #include "scene/resources/image_texture.h"
39
40 void AtlasMergingDialog::_property_changed(const StringName &p_property, const Variant &p_value, const String &p_field, bool p_changing) {
41 _set(p_property, p_value);
42 }
43
44 void AtlasMergingDialog::_generate_merged(const Vector<Ref<TileSetAtlasSource>> &p_atlas_sources, int p_max_columns) {
45 merged.instantiate();
46 merged_mapping.clear();
47
48 if (p_atlas_sources.size() >= 2) {
49 Ref<Image> output_image = Image::create_empty(1, 1, false, Image::FORMAT_RGBA8);
50
51 // Compute the new texture region size.
52 Vector2i new_texture_region_size;
53 for (int source_index = 0; source_index < p_atlas_sources.size(); source_index++) {
54 const Ref<TileSetAtlasSource> &atlas_source = p_atlas_sources[source_index];
55 new_texture_region_size = new_texture_region_size.max(atlas_source->get_texture_region_size());
56 }
57
58 // Generate the new texture.
59 Vector2i atlas_offset;
60 int line_height = 0;
61 for (int source_index = 0; source_index < p_atlas_sources.size(); source_index++) {
62 const Ref<TileSetAtlasSource> &atlas_source = p_atlas_sources[source_index];
63 Ref<Image> input_image = atlas_source->get_texture()->get_image();
64 if (input_image->get_format() != Image::FORMAT_RGBA8) {
65 input_image->convert(Image::FORMAT_RGBA8);
66 }
67 merged_mapping.push_back(HashMap<Vector2i, Vector2i>());
68
69 // Layout the tiles.
70 Vector2i atlas_size;
71
72 for (int tile_index = 0; tile_index < atlas_source->get_tiles_count(); tile_index++) {
73 Vector2i tile_id = atlas_source->get_tile_id(tile_index);
74 atlas_size = atlas_size.max(tile_id + atlas_source->get_tile_size_in_atlas(tile_id));
75
76 Rect2i new_tile_rect_in_atlas = Rect2i(atlas_offset + tile_id, atlas_source->get_tile_size_in_atlas(tile_id));
77
78 int columns = atlas_source->get_tile_animation_columns(tile_id);
79 Vector2i anim_separation = atlas_source->get_tile_animation_separation(tile_id);
80 Vector2i size_in_atlas = atlas_source->get_tile_size_in_atlas(tile_id);
81
82 // Copy the texture.
83 for (int frame = 0; frame < atlas_source->get_tile_animation_frames_count(tile_id); frame++) {
84 Rect2i src_rect = atlas_source->get_tile_texture_region(tile_id, frame);
85 Vector2i new_position = new_tile_rect_in_atlas.position * new_texture_region_size;
86
87 if (frame > 0) {
88 Vector2i frame_coords;
89 if (columns > 0) {
90 // Clamp x frame coordinate by number of max columns( `frame` % `columns`).
91 // Set y frame coordinate to the whole number of times a complete
92 // row of columns can be made( `frame` / `column` ).
93 // These two steps combined convert a 1D index(`frame`) into a
94 // 2D coordinate(`frame_coords`).
95 frame_coords = new_tile_rect_in_atlas.position + (size_in_atlas + anim_separation) * Vector2i(frame % columns, frame / columns);
96 } else {
97 // Godot lays frames out horizontally(`Vector2i(frame,0)`) if columns are set to 0.
98 frame_coords = new_tile_rect_in_atlas.position + (size_in_atlas + anim_separation) * Vector2i(frame, 0);
99 }
100 // Enlarge the atlas offset if new frame_coords fall outside its current dimensions.
101 atlas_size.x = MAX(frame_coords.x + 1, atlas_size.x);
102 atlas_size.y = MAX(frame_coords.y + 1, atlas_size.y);
103
104 new_position = frame_coords * new_texture_region_size;
105 }
106 Rect2 dst_rect_wide = Rect2i(new_position, new_tile_rect_in_atlas.size * new_texture_region_size);
107 // Enlarge image if the destination boundary falls outside its current dimensions.
108 if (dst_rect_wide.get_end().x > output_image->get_width() || dst_rect_wide.get_end().y > output_image->get_height()) {
109 output_image->crop(MAX(dst_rect_wide.get_end().x, output_image->get_width()), MAX(dst_rect_wide.get_end().y, output_image->get_height()));
110 }
111 output_image->blit_rect(input_image, src_rect, dst_rect_wide.get_center() - src_rect.size / 2);
112 }
113
114 // Add to the mapping.
115 merged_mapping[source_index][tile_id] = new_tile_rect_in_atlas.position;
116 }
117
118 // Compute the atlas offset.
119 line_height = MAX(atlas_size.y, line_height);
120 atlas_offset.x += atlas_size.x;
121 if (atlas_offset.x >= p_max_columns) {
122 atlas_offset.x = 0;
123 atlas_offset.y += line_height;
124 line_height = 0;
125 }
126 }
127
128 merged->set_name(p_atlas_sources[0]->get_name());
129 merged->set_texture(ImageTexture::create_from_image(output_image));
130 merged->set_texture_region_size(new_texture_region_size);
131
132 // Copy the tiles to the merged TileSetAtlasSource.
133 for (int source_index = 0; source_index < p_atlas_sources.size(); source_index++) {
134 const Ref<TileSetAtlasSource> &atlas_source = p_atlas_sources[source_index];
135 for (KeyValue<Vector2i, Vector2i> tile_mapping : merged_mapping[source_index]) {
136 // Create tiles and alternatives, then copy their properties.
137 for (int alternative_index = 0; alternative_index < atlas_source->get_alternative_tiles_count(tile_mapping.key); alternative_index++) {
138 int alternative_id = atlas_source->get_alternative_tile_id(tile_mapping.key, alternative_index);
139 int changed_id = -1;
140 if (alternative_id == 0) {
141 merged->create_tile(tile_mapping.value, atlas_source->get_tile_size_in_atlas(tile_mapping.key));
142
143 // Copies are done in order as seen inside the editor
144 // - for cross-referencing and organizational purposes.
145
146 // Copy over `columns`.
147 int columns = atlas_source->get_tile_animation_columns(tile_mapping.key);
148 merged->set_tile_animation_columns(tile_mapping.value, columns);
149
150 // Copy over `separation`.
151 Vector2i separation = atlas_source->get_tile_animation_separation(tile_mapping.key);
152 merged->set_tile_animation_separation(tile_mapping.value, separation);
153
154 // Copy over speed.
155 merged->set_tile_animation_speed(tile_mapping.value, atlas_source->get_tile_animation_speed(tile_mapping.key));
156
157 // Copy over mode.
158 merged->set_tile_animation_mode(tile_mapping.value, atlas_source->get_tile_animation_mode(tile_mapping.key));
159
160 // Copy over `count` and frame durations.
161 int count = atlas_source->get_tile_animation_frames_count(tile_mapping.key);
162 merged->set_tile_animation_frames_count(tile_mapping.value, count);
163 for (int i = 0; i < count; i++) {
164 merged->set_tile_animation_frame_duration(tile_mapping.value, i, atlas_source->get_tile_animation_frame_duration(tile_mapping.key, i));
165 }
166 merged->set_tile_animation_speed(tile_mapping.value, atlas_source->get_tile_animation_speed(tile_mapping.key));
167 } else {
168 changed_id = merged->create_alternative_tile(tile_mapping.value, alternative_index);
169 }
170
171 // Copy the properties.
172 TileData *src_tile_data = atlas_source->get_tile_data(tile_mapping.key, alternative_id);
173 List<PropertyInfo> properties;
174 src_tile_data->get_property_list(&properties);
175
176 TileData *dst_tile_data = merged->get_tile_data(tile_mapping.value, changed_id == -1 ? alternative_id : changed_id);
177 for (PropertyInfo property : properties) {
178 // Only copy over properties that are used for storage.
179 if (!(property.usage & PROPERTY_USAGE_STORAGE)) {
180 continue;
181 }
182
183 // Only copy over properties that are not default.
184 Variant value = src_tile_data->get(property.name);
185 Variant default_value = ClassDB::class_get_default_property_value("TileData", property.name);
186 if (default_value.get_type() != Variant::NIL && bool(Variant::evaluate(Variant::OP_EQUAL, value, default_value))) {
187 continue;
188 }
189
190 // DO NOT try to copy "script" properties that are null
191 // as this causes a crash - see issue #101132.
192 // I believe this *should* be covered by the default
193 // check that is done above but the call to
194 // Variant::evaluate(Variant::OP_EQUAL, <null>, <null>) returns false.
195 // I do not know if this is intended behavior or a bug.
196 if (property.name == "script" && value.is_null()) {
197 continue;
198 }
199 dst_tile_data->set(property.name, value);
200 }
201 }
202 }
203 }
204 }
205 }
206
207 void AtlasMergingDialog::_update_texture() {
208 Vector<int> selected = atlas_merging_atlases_list->get_selected_items();
209 if (selected.size() >= 2) {
210 Vector<Ref<TileSetAtlasSource>> to_merge;
211 for (int i = 0; i < selected.size(); i++) {
212 int source_id = atlas_merging_atlases_list->get_item_metadata(selected[i]);
213 to_merge.push_back(tile_set->get_source(source_id));
214 }
215 _generate_merged(to_merge, next_line_after_column);
216 preview->set_texture(merged->get_texture());
217 preview->show();
218 select_2_atlases_label->hide();
219 get_ok_button()->set_disabled(false);
220 merge_button->set_disabled(false);
221 } else {
222 _generate_merged(Vector<Ref<TileSetAtlasSource>>(), next_line_after_column);
223 preview->set_texture(Ref<Texture2D>());
224 preview->hide();
225 select_2_atlases_label->show();
226 get_ok_button()->set_disabled(true);
227 merge_button->set_disabled(true);
228 }
229 }
230
231 void AtlasMergingDialog::_merge_confirmed(const String &p_path) {
232 ERR_FAIL_COND(merged.is_null());
233
234 Ref<ImageTexture> output_image_texture = merged->get_texture();
235 output_image_texture->get_image()->save_png(p_path);
236
237 ResourceLoader::import(p_path);
238
239 Ref<Texture2D> new_texture_resource = ResourceLoader::load(p_path, "Texture2D");
240 merged->set_texture(new_texture_resource);
241
242 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
243 undo_redo->create_action(TTR("Merge TileSetAtlasSource"));
244 int next_id = tile_set->get_next_source_id();
245 undo_redo->add_do_method(*tile_set, "add_source", merged, next_id);
246 undo_redo->add_undo_method(*tile_set, "remove_source", next_id);
247
248 if (delete_original_atlases) {
249 // Delete originals if needed.
250 Vector<int> selected = atlas_merging_atlases_list->get_selected_items();
251 for (int i = 0; i < selected.size(); i++) {
252 int source_id = atlas_merging_atlases_list->get_item_metadata(selected[i]);
253 Ref<TileSetAtlasSource> tas = tile_set->get_source(source_id);
254 undo_redo->add_do_method(*tile_set, "remove_source", source_id);
255 undo_redo->add_undo_method(*tile_set, "add_source", tas, source_id);
256
257 // Add the tile proxies.
258 for (int tile_index = 0; tile_index < tas->get_tiles_count(); tile_index++) {
259 Vector2i tile_id = tas->get_tile_id(tile_index);
260 undo_redo->add_do_method(*tile_set, "set_coords_level_tile_proxy", source_id, tile_id, next_id, merged_mapping[i][tile_id]);
261 if (tile_set->has_coords_level_tile_proxy(source_id, tile_id)) {
262 Array a = tile_set->get_coords_level_tile_proxy(source_id, tile_id);
263 undo_redo->add_undo_method(*tile_set, "set_coords_level_tile_proxy", a[0], a[1]);
264 } else {
265 undo_redo->add_undo_method(*tile_set, "remove_coords_level_tile_proxy", source_id, tile_id);
266 }
267 }
268 }
269 }
270 undo_redo->commit_action();
271 committed_actions_count++;
272
273 hide();
274 }
275
276 void AtlasMergingDialog::ok_pressed() {
277 delete_original_atlases = false;
278 editor_file_dialog->popup_file_dialog();
279 }
280
281 void AtlasMergingDialog::cancel_pressed() {
282 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
283 for (int i = 0; i < committed_actions_count; i++) {
284 undo_redo->undo();
285 }
286 committed_actions_count = 0;
287 }
288
289 void AtlasMergingDialog::custom_action(const String &p_action) {
290 if (p_action == "merge") {
291 delete_original_atlases = true;
292 editor_file_dialog->popup_file_dialog();
293 }
294 }
295
296 bool AtlasMergingDialog::_set(const StringName &p_name, const Variant &p_value) {
297 if (p_name == "next_line_after_column" && p_value.get_type() == Variant::INT) {
298 next_line_after_column = p_value;
299 _update_texture();
300 return true;
301 }
302 return false;
303 }
304
305 bool AtlasMergingDialog::_get(const StringName &p_name, Variant &r_ret) const {
306 if (p_name == "next_line_after_column") {
307 r_ret = next_line_after_column;
308 return true;
309 }
310 return false;
311 }
312
313 void AtlasMergingDialog::_notification(int p_what) {
314 switch (p_what) {
315 case NOTIFICATION_VISIBILITY_CHANGED: {
316 if (is_visible()) {
317 _update_texture();
318 }
319 } break;
320 }
321 }
322
323 void AtlasMergingDialog::update_tile_set(Ref<TileSet> p_tile_set) {
324 ERR_FAIL_COND(p_tile_set.is_null());
325 tile_set = p_tile_set;
326
327 atlas_merging_atlases_list->clear();
328 for (int i = 0; i < p_tile_set->get_source_count(); i++) {
329 int source_id = p_tile_set->get_source_id(i);
330 Ref<TileSetAtlasSource> atlas_source = p_tile_set->get_source(source_id);
331 if (atlas_source.is_valid()) {
332 Ref<Texture2D> texture = atlas_source->get_texture();
333 if (texture.is_valid()) {
334 String item_text = vformat(TTR("%s (ID: %d)"), texture->get_path().get_file(), source_id);
335 atlas_merging_atlases_list->add_item(item_text, texture);
336 atlas_merging_atlases_list->set_item_metadata(-1, source_id);
337 }
338 }
339 }
340
341 get_ok_button()->set_disabled(true);
342 merge_button->set_disabled(true);
343
344 committed_actions_count = 0;
345 }
346
347 AtlasMergingDialog::AtlasMergingDialog() {
348 // Atlas merging window.
349 set_title(TTR("Atlas Merging"));
350 set_hide_on_ok(false);
351
352 // Ok buttons
353 set_ok_button_text(TTR("Merge (Keep original Atlases)"));
354 get_ok_button()->set_disabled(true);
355 merge_button = add_button(TTR("Merge"), true, "merge");
356 merge_button->set_disabled(true);
357
358 HSplitContainer *atlas_merging_h_split_container = memnew(HSplitContainer);
359 atlas_merging_h_split_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);
360 atlas_merging_h_split_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);
361 add_child(atlas_merging_h_split_container);
362
363 // Atlas sources item list.
364 atlas_merging_atlases_list = memnew(ItemList);
365 atlas_merging_atlases_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
366 atlas_merging_atlases_list->set_fixed_icon_size(Size2(60, 60) * EDSCALE);
367 atlas_merging_atlases_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
368 atlas_merging_atlases_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
369 atlas_merging_atlases_list->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
370 atlas_merging_atlases_list->set_custom_minimum_size(Size2(100, 200));
371 atlas_merging_atlases_list->set_select_mode(ItemList::SELECT_MULTI);
372 atlas_merging_atlases_list->set_theme_type_variation("ItemListSecondary");
373 atlas_merging_atlases_list->connect("multi_selected", callable_mp(this, &AtlasMergingDialog::_update_texture).unbind(2));
374 atlas_merging_h_split_container->add_child(atlas_merging_atlases_list);
375
376 VBoxContainer *atlas_merging_right_panel = memnew(VBoxContainer);
377 atlas_merging_right_panel->set_h_size_flags(Control::SIZE_EXPAND_FILL);
378 atlas_merging_right_panel->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
379 atlas_merging_h_split_container->add_child(atlas_merging_right_panel);
380
381 // Settings.
382 Label *settings_label = memnew(Label);
383 settings_label->set_text(TTR("Settings:"));
384 atlas_merging_right_panel->add_child(settings_label);
385
386 columns_editor_property = memnew(EditorPropertyInteger);
387 columns_editor_property->set_label(TTR("Next Line After Column"));
388 columns_editor_property->set_object_and_property(this, "next_line_after_column");
389 columns_editor_property->update_property();
390 columns_editor_property->connect("property_changed", callable_mp(this, &AtlasMergingDialog::_property_changed));
391 atlas_merging_right_panel->add_child(columns_editor_property);
392
393 // Preview.
394 Label *preview_label = memnew(Label);
395 preview_label->set_text(TTR("Preview:"));
396 atlas_merging_right_panel->add_child(preview_label);
397
398 preview = memnew(TextureRect);
399 preview->set_h_size_flags(Control::SIZE_EXPAND_FILL);
400 preview->set_v_size_flags(Control::SIZE_EXPAND_FILL);
401 preview->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
402 preview->hide();
403 preview->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
404 atlas_merging_right_panel->add_child(preview);
405
406 select_2_atlases_label = memnew(Label);
407 select_2_atlases_label->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
408 select_2_atlases_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
409 select_2_atlases_label->set_v_size_flags(Control::SIZE_EXPAND_FILL);
410 select_2_atlases_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
411 select_2_atlases_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
412 select_2_atlases_label->set_text(TTR("Please select two atlases or more."));
413 atlas_merging_right_panel->add_child(select_2_atlases_label);
414
415 // The file dialog to choose the texture path.
416 editor_file_dialog = memnew(EditorFileDialog);
417 editor_file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
418 editor_file_dialog->add_filter("*.png");
419 editor_file_dialog->connect("file_selected", callable_mp(this, &AtlasMergingDialog::_merge_confirmed));
420 add_child(editor_file_dialog);
421 }