atlas_merging_dialog.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. #include "atlas_merging_dialog.h"
  31. #include "editor/editor_properties_vector.h"
  32. #include "editor/editor_undo_redo_manager.h"
  33. #include "editor/gui/editor_file_dialog.h"
  34. #include "editor/themes/editor_scale.h"
  35. #include "scene/gui/control.h"
  36. #include "scene/gui/split_container.h"
  37. #include "scene/resources/image_texture.h"
  38. void AtlasMergingDialog::_property_changed(const StringName &p_property, const Variant &p_value, const String &p_field, bool p_changing) {
  39. _set(p_property, p_value);
  40. }
  41. void AtlasMergingDialog::_generate_merged(const Vector<Ref<TileSetAtlasSource>> &p_atlas_sources, int p_max_columns) {
  42. merged.instantiate();
  43. merged_mapping.clear();
  44. if (p_atlas_sources.size() >= 2) {
  45. Ref<Image> output_image = Image::create_empty(1, 1, false, Image::FORMAT_RGBA8);
  46. // Compute the new texture region size.
  47. Vector2i new_texture_region_size;
  48. for (int source_index = 0; source_index < p_atlas_sources.size(); source_index++) {
  49. const Ref<TileSetAtlasSource> &atlas_source = p_atlas_sources[source_index];
  50. new_texture_region_size = new_texture_region_size.max(atlas_source->get_texture_region_size());
  51. }
  52. // Generate the new texture.
  53. Vector2i atlas_offset;
  54. int line_height = 0;
  55. for (int source_index = 0; source_index < p_atlas_sources.size(); source_index++) {
  56. const Ref<TileSetAtlasSource> &atlas_source = p_atlas_sources[source_index];
  57. Ref<Image> input_image = atlas_source->get_texture()->get_image();
  58. if (input_image->get_format() != Image::FORMAT_RGBA8) {
  59. input_image->convert(Image::FORMAT_RGBA8);
  60. }
  61. merged_mapping.push_back(HashMap<Vector2i, Vector2i>());
  62. // Layout the tiles.
  63. Vector2i atlas_size;
  64. for (int tile_index = 0; tile_index < atlas_source->get_tiles_count(); tile_index++) {
  65. Vector2i tile_id = atlas_source->get_tile_id(tile_index);
  66. atlas_size = atlas_size.max(tile_id + atlas_source->get_tile_size_in_atlas(tile_id));
  67. Rect2i new_tile_rect_in_atlas = Rect2i(atlas_offset + tile_id, atlas_source->get_tile_size_in_atlas(tile_id));
  68. // Copy the texture.
  69. for (int frame = 0; frame < atlas_source->get_tile_animation_frames_count(tile_id); frame++) {
  70. Rect2i src_rect = atlas_source->get_tile_texture_region(tile_id, frame);
  71. Vector2i new_position = new_tile_rect_in_atlas.position * new_texture_region_size;
  72. if (frame > 0) {
  73. new_position += src_rect.size * Vector2i(frame, 0);
  74. atlas_size.x = MAX(frame + 1, atlas_size.x);
  75. }
  76. Rect2 dst_rect_wide = Rect2i(new_position, new_tile_rect_in_atlas.size * new_texture_region_size);
  77. if (dst_rect_wide.get_end().x > output_image->get_width() || dst_rect_wide.get_end().y > output_image->get_height()) {
  78. 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()));
  79. }
  80. output_image->blit_rect(input_image, src_rect, dst_rect_wide.get_center() - src_rect.size / 2);
  81. }
  82. // Add to the mapping.
  83. merged_mapping[source_index][tile_id] = new_tile_rect_in_atlas.position;
  84. }
  85. // Compute the atlas offset.
  86. line_height = MAX(atlas_size.y, line_height);
  87. atlas_offset.x += atlas_size.x;
  88. if (atlas_offset.x >= p_max_columns) {
  89. atlas_offset.x = 0;
  90. atlas_offset.y += line_height;
  91. line_height = 0;
  92. }
  93. }
  94. merged->set_name(p_atlas_sources[0]->get_name());
  95. merged->set_texture(ImageTexture::create_from_image(output_image));
  96. merged->set_texture_region_size(new_texture_region_size);
  97. // Copy the tiles to the merged TileSetAtlasSource.
  98. for (int source_index = 0; source_index < p_atlas_sources.size(); source_index++) {
  99. const Ref<TileSetAtlasSource> &atlas_source = p_atlas_sources[source_index];
  100. for (KeyValue<Vector2i, Vector2i> tile_mapping : merged_mapping[source_index]) {
  101. // Create tiles and alternatives, then copy their properties.
  102. for (int alternative_index = 0; alternative_index < atlas_source->get_alternative_tiles_count(tile_mapping.key); alternative_index++) {
  103. int alternative_id = atlas_source->get_alternative_tile_id(tile_mapping.key, alternative_index);
  104. int changed_id = -1;
  105. if (alternative_id == 0) {
  106. merged->create_tile(tile_mapping.value, atlas_source->get_tile_size_in_atlas(tile_mapping.key));
  107. int count = atlas_source->get_tile_animation_frames_count(tile_mapping.key);
  108. merged->set_tile_animation_frames_count(tile_mapping.value, count);
  109. for (int i = 0; i < count; i++) {
  110. merged->set_tile_animation_frame_duration(tile_mapping.value, i, atlas_source->get_tile_animation_frame_duration(tile_mapping.key, i));
  111. }
  112. merged->set_tile_animation_speed(tile_mapping.value, atlas_source->get_tile_animation_speed(tile_mapping.key));
  113. merged->set_tile_animation_mode(tile_mapping.value, atlas_source->get_tile_animation_mode(tile_mapping.key));
  114. } else {
  115. changed_id = merged->create_alternative_tile(tile_mapping.value, alternative_index);
  116. }
  117. // Copy the properties.
  118. TileData *src_tile_data = atlas_source->get_tile_data(tile_mapping.key, alternative_id);
  119. List<PropertyInfo> properties;
  120. src_tile_data->get_property_list(&properties);
  121. TileData *dst_tile_data = merged->get_tile_data(tile_mapping.value, changed_id == -1 ? alternative_id : changed_id);
  122. for (PropertyInfo property : properties) {
  123. if (!(property.usage & PROPERTY_USAGE_STORAGE)) {
  124. continue;
  125. }
  126. Variant value = src_tile_data->get(property.name);
  127. Variant default_value = ClassDB::class_get_default_property_value("TileData", property.name);
  128. if (default_value.get_type() != Variant::NIL && bool(Variant::evaluate(Variant::OP_EQUAL, value, default_value))) {
  129. continue;
  130. }
  131. dst_tile_data->set(property.name, value);
  132. }
  133. }
  134. }
  135. }
  136. }
  137. }
  138. void AtlasMergingDialog::_update_texture() {
  139. Vector<int> selected = atlas_merging_atlases_list->get_selected_items();
  140. if (selected.size() >= 2) {
  141. Vector<Ref<TileSetAtlasSource>> to_merge;
  142. for (int i = 0; i < selected.size(); i++) {
  143. int source_id = atlas_merging_atlases_list->get_item_metadata(selected[i]);
  144. to_merge.push_back(tile_set->get_source(source_id));
  145. }
  146. _generate_merged(to_merge, next_line_after_column);
  147. preview->set_texture(merged->get_texture());
  148. preview->show();
  149. select_2_atlases_label->hide();
  150. get_ok_button()->set_disabled(false);
  151. merge_button->set_disabled(false);
  152. } else {
  153. _generate_merged(Vector<Ref<TileSetAtlasSource>>(), next_line_after_column);
  154. preview->set_texture(Ref<Texture2D>());
  155. preview->hide();
  156. select_2_atlases_label->show();
  157. get_ok_button()->set_disabled(true);
  158. merge_button->set_disabled(true);
  159. }
  160. }
  161. void AtlasMergingDialog::_merge_confirmed(const String &p_path) {
  162. ERR_FAIL_COND(!merged.is_valid());
  163. Ref<ImageTexture> output_image_texture = merged->get_texture();
  164. output_image_texture->get_image()->save_png(p_path);
  165. ResourceLoader::import(p_path);
  166. Ref<Texture2D> new_texture_resource = ResourceLoader::load(p_path, "Texture2D");
  167. merged->set_texture(new_texture_resource);
  168. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  169. undo_redo->create_action(TTR("Merge TileSetAtlasSource"));
  170. int next_id = tile_set->get_next_source_id();
  171. undo_redo->add_do_method(*tile_set, "add_source", merged, next_id);
  172. undo_redo->add_undo_method(*tile_set, "remove_source", next_id);
  173. if (delete_original_atlases) {
  174. // Delete originals if needed.
  175. Vector<int> selected = atlas_merging_atlases_list->get_selected_items();
  176. for (int i = 0; i < selected.size(); i++) {
  177. int source_id = atlas_merging_atlases_list->get_item_metadata(selected[i]);
  178. Ref<TileSetAtlasSource> tas = tile_set->get_source(source_id);
  179. undo_redo->add_do_method(*tile_set, "remove_source", source_id);
  180. undo_redo->add_undo_method(*tile_set, "add_source", tas, source_id);
  181. // Add the tile proxies.
  182. for (int tile_index = 0; tile_index < tas->get_tiles_count(); tile_index++) {
  183. Vector2i tile_id = tas->get_tile_id(tile_index);
  184. undo_redo->add_do_method(*tile_set, "set_coords_level_tile_proxy", source_id, tile_id, next_id, merged_mapping[i][tile_id]);
  185. if (tile_set->has_coords_level_tile_proxy(source_id, tile_id)) {
  186. Array a = tile_set->get_coords_level_tile_proxy(source_id, tile_id);
  187. undo_redo->add_undo_method(*tile_set, "set_coords_level_tile_proxy", a[0], a[1]);
  188. } else {
  189. undo_redo->add_undo_method(*tile_set, "remove_coords_level_tile_proxy", source_id, tile_id);
  190. }
  191. }
  192. }
  193. }
  194. undo_redo->commit_action();
  195. commited_actions_count++;
  196. hide();
  197. }
  198. void AtlasMergingDialog::ok_pressed() {
  199. delete_original_atlases = false;
  200. editor_file_dialog->popup_file_dialog();
  201. }
  202. void AtlasMergingDialog::cancel_pressed() {
  203. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  204. for (int i = 0; i < commited_actions_count; i++) {
  205. undo_redo->undo();
  206. }
  207. commited_actions_count = 0;
  208. }
  209. void AtlasMergingDialog::custom_action(const String &p_action) {
  210. if (p_action == "merge") {
  211. delete_original_atlases = true;
  212. editor_file_dialog->popup_file_dialog();
  213. }
  214. }
  215. bool AtlasMergingDialog::_set(const StringName &p_name, const Variant &p_value) {
  216. if (p_name == "next_line_after_column" && p_value.get_type() == Variant::INT) {
  217. next_line_after_column = p_value;
  218. _update_texture();
  219. return true;
  220. }
  221. return false;
  222. }
  223. bool AtlasMergingDialog::_get(const StringName &p_name, Variant &r_ret) const {
  224. if (p_name == "next_line_after_column") {
  225. r_ret = next_line_after_column;
  226. return true;
  227. }
  228. return false;
  229. }
  230. void AtlasMergingDialog::_notification(int p_what) {
  231. switch (p_what) {
  232. case NOTIFICATION_VISIBILITY_CHANGED: {
  233. if (is_visible()) {
  234. _update_texture();
  235. }
  236. } break;
  237. }
  238. }
  239. void AtlasMergingDialog::update_tile_set(Ref<TileSet> p_tile_set) {
  240. ERR_FAIL_COND(!p_tile_set.is_valid());
  241. tile_set = p_tile_set;
  242. atlas_merging_atlases_list->clear();
  243. for (int i = 0; i < p_tile_set->get_source_count(); i++) {
  244. int source_id = p_tile_set->get_source_id(i);
  245. Ref<TileSetAtlasSource> atlas_source = p_tile_set->get_source(source_id);
  246. if (atlas_source.is_valid()) {
  247. Ref<Texture2D> texture = atlas_source->get_texture();
  248. if (texture.is_valid()) {
  249. String item_text = vformat(TTR("%s (ID: %d)"), texture->get_path().get_file(), source_id);
  250. atlas_merging_atlases_list->add_item(item_text, texture);
  251. atlas_merging_atlases_list->set_item_metadata(-1, source_id);
  252. }
  253. }
  254. }
  255. get_ok_button()->set_disabled(true);
  256. merge_button->set_disabled(true);
  257. commited_actions_count = 0;
  258. }
  259. AtlasMergingDialog::AtlasMergingDialog() {
  260. // Atlas merging window.
  261. set_title(TTR("Atlas Merging"));
  262. set_hide_on_ok(false);
  263. // Ok buttons
  264. set_ok_button_text(TTR("Merge (Keep original Atlases)"));
  265. get_ok_button()->set_disabled(true);
  266. merge_button = add_button(TTR("Merge"), true, "merge");
  267. merge_button->set_disabled(true);
  268. HSplitContainer *atlas_merging_h_split_container = memnew(HSplitContainer);
  269. atlas_merging_h_split_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  270. atlas_merging_h_split_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  271. add_child(atlas_merging_h_split_container);
  272. // Atlas sources item list.
  273. atlas_merging_atlases_list = memnew(ItemList);
  274. atlas_merging_atlases_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  275. atlas_merging_atlases_list->set_fixed_icon_size(Size2(60, 60) * EDSCALE);
  276. atlas_merging_atlases_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  277. atlas_merging_atlases_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  278. atlas_merging_atlases_list->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
  279. atlas_merging_atlases_list->set_custom_minimum_size(Size2(100, 200));
  280. atlas_merging_atlases_list->set_select_mode(ItemList::SELECT_MULTI);
  281. atlas_merging_atlases_list->connect("multi_selected", callable_mp(this, &AtlasMergingDialog::_update_texture).unbind(2));
  282. atlas_merging_h_split_container->add_child(atlas_merging_atlases_list);
  283. VBoxContainer *atlas_merging_right_panel = memnew(VBoxContainer);
  284. atlas_merging_right_panel->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  285. atlas_merging_right_panel->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
  286. atlas_merging_h_split_container->add_child(atlas_merging_right_panel);
  287. // Settings.
  288. Label *settings_label = memnew(Label);
  289. settings_label->set_text(TTR("Settings:"));
  290. atlas_merging_right_panel->add_child(settings_label);
  291. columns_editor_property = memnew(EditorPropertyInteger);
  292. columns_editor_property->set_label(TTR("Next Line After Column"));
  293. columns_editor_property->set_object_and_property(this, "next_line_after_column");
  294. columns_editor_property->update_property();
  295. columns_editor_property->connect("property_changed", callable_mp(this, &AtlasMergingDialog::_property_changed));
  296. atlas_merging_right_panel->add_child(columns_editor_property);
  297. // Preview.
  298. Label *preview_label = memnew(Label);
  299. preview_label->set_text(TTR("Preview:"));
  300. atlas_merging_right_panel->add_child(preview_label);
  301. preview = memnew(TextureRect);
  302. preview->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  303. preview->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  304. preview->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  305. preview->hide();
  306. preview->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  307. atlas_merging_right_panel->add_child(preview);
  308. select_2_atlases_label = memnew(Label);
  309. select_2_atlases_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  310. select_2_atlases_label->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  311. select_2_atlases_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  312. select_2_atlases_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  313. select_2_atlases_label->set_text(TTR("Please select two atlases or more."));
  314. atlas_merging_right_panel->add_child(select_2_atlases_label);
  315. // The file dialog to choose the texture path.
  316. editor_file_dialog = memnew(EditorFileDialog);
  317. editor_file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  318. editor_file_dialog->add_filter("*.png");
  319. editor_file_dialog->connect("file_selected", callable_mp(this, &AtlasMergingDialog::_merge_confirmed));
  320. add_child(editor_file_dialog);
  321. }