Path: blob/master/editor/scene/2d/tiles/atlas_merging_dialog.cpp
9912 views
/**************************************************************************/1/* atlas_merging_dialog.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "atlas_merging_dialog.h"3132#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"3839void AtlasMergingDialog::_property_changed(const StringName &p_property, const Variant &p_value, const String &p_field, bool p_changing) {40_set(p_property, p_value);41}4243void AtlasMergingDialog::_generate_merged(const Vector<Ref<TileSetAtlasSource>> &p_atlas_sources, int p_max_columns) {44merged.instantiate();45merged_mapping.clear();4647if (p_atlas_sources.size() >= 2) {48Ref<Image> output_image = Image::create_empty(1, 1, false, Image::FORMAT_RGBA8);4950// Compute the new texture region size.51Vector2i new_texture_region_size;52for (int source_index = 0; source_index < p_atlas_sources.size(); source_index++) {53const Ref<TileSetAtlasSource> &atlas_source = p_atlas_sources[source_index];54new_texture_region_size = new_texture_region_size.max(atlas_source->get_texture_region_size());55}5657// Generate the new texture.58Vector2i atlas_offset;59int line_height = 0;60for (int source_index = 0; source_index < p_atlas_sources.size(); source_index++) {61const Ref<TileSetAtlasSource> &atlas_source = p_atlas_sources[source_index];62Ref<Image> input_image = atlas_source->get_texture()->get_image();63if (input_image->get_format() != Image::FORMAT_RGBA8) {64input_image->convert(Image::FORMAT_RGBA8);65}66merged_mapping.push_back(HashMap<Vector2i, Vector2i>());6768// Layout the tiles.69Vector2i atlas_size;7071for (int tile_index = 0; tile_index < atlas_source->get_tiles_count(); tile_index++) {72Vector2i tile_id = atlas_source->get_tile_id(tile_index);73atlas_size = atlas_size.max(tile_id + atlas_source->get_tile_size_in_atlas(tile_id));7475Rect2i new_tile_rect_in_atlas = Rect2i(atlas_offset + tile_id, atlas_source->get_tile_size_in_atlas(tile_id));7677// Copy the texture.78for (int frame = 0; frame < atlas_source->get_tile_animation_frames_count(tile_id); frame++) {79Rect2i src_rect = atlas_source->get_tile_texture_region(tile_id, frame);80Vector2i new_position = new_tile_rect_in_atlas.position * new_texture_region_size;81if (frame > 0) {82new_position += src_rect.size * Vector2i(frame, 0);83atlas_size.x = MAX(frame + 1, atlas_size.x);84}85Rect2 dst_rect_wide = Rect2i(new_position, new_tile_rect_in_atlas.size * new_texture_region_size);86if (dst_rect_wide.get_end().x > output_image->get_width() || dst_rect_wide.get_end().y > output_image->get_height()) {87output_image->crop(MAX(dst_rect_wide.get_end().x, output_image->get_width()), MAX(dst_rect_wide.get_end().y, output_image->get_height()));88}89output_image->blit_rect(input_image, src_rect, dst_rect_wide.get_center() - src_rect.size / 2);90}9192// Add to the mapping.93merged_mapping[source_index][tile_id] = new_tile_rect_in_atlas.position;94}9596// Compute the atlas offset.97line_height = MAX(atlas_size.y, line_height);98atlas_offset.x += atlas_size.x;99if (atlas_offset.x >= p_max_columns) {100atlas_offset.x = 0;101atlas_offset.y += line_height;102line_height = 0;103}104}105106merged->set_name(p_atlas_sources[0]->get_name());107merged->set_texture(ImageTexture::create_from_image(output_image));108merged->set_texture_region_size(new_texture_region_size);109110// Copy the tiles to the merged TileSetAtlasSource.111for (int source_index = 0; source_index < p_atlas_sources.size(); source_index++) {112const Ref<TileSetAtlasSource> &atlas_source = p_atlas_sources[source_index];113for (KeyValue<Vector2i, Vector2i> tile_mapping : merged_mapping[source_index]) {114// Create tiles and alternatives, then copy their properties.115for (int alternative_index = 0; alternative_index < atlas_source->get_alternative_tiles_count(tile_mapping.key); alternative_index++) {116int alternative_id = atlas_source->get_alternative_tile_id(tile_mapping.key, alternative_index);117int changed_id = -1;118if (alternative_id == 0) {119merged->create_tile(tile_mapping.value, atlas_source->get_tile_size_in_atlas(tile_mapping.key));120int count = atlas_source->get_tile_animation_frames_count(tile_mapping.key);121merged->set_tile_animation_frames_count(tile_mapping.value, count);122for (int i = 0; i < count; i++) {123merged->set_tile_animation_frame_duration(tile_mapping.value, i, atlas_source->get_tile_animation_frame_duration(tile_mapping.key, i));124}125merged->set_tile_animation_speed(tile_mapping.value, atlas_source->get_tile_animation_speed(tile_mapping.key));126merged->set_tile_animation_mode(tile_mapping.value, atlas_source->get_tile_animation_mode(tile_mapping.key));127} else {128changed_id = merged->create_alternative_tile(tile_mapping.value, alternative_index);129}130131// Copy the properties.132TileData *src_tile_data = atlas_source->get_tile_data(tile_mapping.key, alternative_id);133List<PropertyInfo> properties;134src_tile_data->get_property_list(&properties);135136TileData *dst_tile_data = merged->get_tile_data(tile_mapping.value, changed_id == -1 ? alternative_id : changed_id);137for (PropertyInfo property : properties) {138if (!(property.usage & PROPERTY_USAGE_STORAGE)) {139continue;140}141Variant value = src_tile_data->get(property.name);142Variant default_value = ClassDB::class_get_default_property_value("TileData", property.name);143if (default_value.get_type() != Variant::NIL && bool(Variant::evaluate(Variant::OP_EQUAL, value, default_value))) {144continue;145}146dst_tile_data->set(property.name, value);147}148}149}150}151}152}153154void AtlasMergingDialog::_update_texture() {155Vector<int> selected = atlas_merging_atlases_list->get_selected_items();156if (selected.size() >= 2) {157Vector<Ref<TileSetAtlasSource>> to_merge;158for (int i = 0; i < selected.size(); i++) {159int source_id = atlas_merging_atlases_list->get_item_metadata(selected[i]);160to_merge.push_back(tile_set->get_source(source_id));161}162_generate_merged(to_merge, next_line_after_column);163preview->set_texture(merged->get_texture());164preview->show();165select_2_atlases_label->hide();166get_ok_button()->set_disabled(false);167merge_button->set_disabled(false);168} else {169_generate_merged(Vector<Ref<TileSetAtlasSource>>(), next_line_after_column);170preview->set_texture(Ref<Texture2D>());171preview->hide();172select_2_atlases_label->show();173get_ok_button()->set_disabled(true);174merge_button->set_disabled(true);175}176}177178void AtlasMergingDialog::_merge_confirmed(const String &p_path) {179ERR_FAIL_COND(merged.is_null());180181Ref<ImageTexture> output_image_texture = merged->get_texture();182output_image_texture->get_image()->save_png(p_path);183184ResourceLoader::import(p_path);185186Ref<Texture2D> new_texture_resource = ResourceLoader::load(p_path, "Texture2D");187merged->set_texture(new_texture_resource);188189EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();190undo_redo->create_action(TTR("Merge TileSetAtlasSource"));191int next_id = tile_set->get_next_source_id();192undo_redo->add_do_method(*tile_set, "add_source", merged, next_id);193undo_redo->add_undo_method(*tile_set, "remove_source", next_id);194195if (delete_original_atlases) {196// Delete originals if needed.197Vector<int> selected = atlas_merging_atlases_list->get_selected_items();198for (int i = 0; i < selected.size(); i++) {199int source_id = atlas_merging_atlases_list->get_item_metadata(selected[i]);200Ref<TileSetAtlasSource> tas = tile_set->get_source(source_id);201undo_redo->add_do_method(*tile_set, "remove_source", source_id);202undo_redo->add_undo_method(*tile_set, "add_source", tas, source_id);203204// Add the tile proxies.205for (int tile_index = 0; tile_index < tas->get_tiles_count(); tile_index++) {206Vector2i tile_id = tas->get_tile_id(tile_index);207undo_redo->add_do_method(*tile_set, "set_coords_level_tile_proxy", source_id, tile_id, next_id, merged_mapping[i][tile_id]);208if (tile_set->has_coords_level_tile_proxy(source_id, tile_id)) {209Array a = tile_set->get_coords_level_tile_proxy(source_id, tile_id);210undo_redo->add_undo_method(*tile_set, "set_coords_level_tile_proxy", a[0], a[1]);211} else {212undo_redo->add_undo_method(*tile_set, "remove_coords_level_tile_proxy", source_id, tile_id);213}214}215}216}217undo_redo->commit_action();218committed_actions_count++;219220hide();221}222223void AtlasMergingDialog::ok_pressed() {224delete_original_atlases = false;225editor_file_dialog->popup_file_dialog();226}227228void AtlasMergingDialog::cancel_pressed() {229EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();230for (int i = 0; i < committed_actions_count; i++) {231undo_redo->undo();232}233committed_actions_count = 0;234}235236void AtlasMergingDialog::custom_action(const String &p_action) {237if (p_action == "merge") {238delete_original_atlases = true;239editor_file_dialog->popup_file_dialog();240}241}242243bool AtlasMergingDialog::_set(const StringName &p_name, const Variant &p_value) {244if (p_name == "next_line_after_column" && p_value.get_type() == Variant::INT) {245next_line_after_column = p_value;246_update_texture();247return true;248}249return false;250}251252bool AtlasMergingDialog::_get(const StringName &p_name, Variant &r_ret) const {253if (p_name == "next_line_after_column") {254r_ret = next_line_after_column;255return true;256}257return false;258}259260void AtlasMergingDialog::_notification(int p_what) {261switch (p_what) {262case NOTIFICATION_VISIBILITY_CHANGED: {263if (is_visible()) {264_update_texture();265}266} break;267}268}269270void AtlasMergingDialog::update_tile_set(Ref<TileSet> p_tile_set) {271ERR_FAIL_COND(p_tile_set.is_null());272tile_set = p_tile_set;273274atlas_merging_atlases_list->clear();275for (int i = 0; i < p_tile_set->get_source_count(); i++) {276int source_id = p_tile_set->get_source_id(i);277Ref<TileSetAtlasSource> atlas_source = p_tile_set->get_source(source_id);278if (atlas_source.is_valid()) {279Ref<Texture2D> texture = atlas_source->get_texture();280if (texture.is_valid()) {281String item_text = vformat(TTR("%s (ID: %d)"), texture->get_path().get_file(), source_id);282atlas_merging_atlases_list->add_item(item_text, texture);283atlas_merging_atlases_list->set_item_metadata(-1, source_id);284}285}286}287288get_ok_button()->set_disabled(true);289merge_button->set_disabled(true);290291committed_actions_count = 0;292}293294AtlasMergingDialog::AtlasMergingDialog() {295// Atlas merging window.296set_title(TTR("Atlas Merging"));297set_hide_on_ok(false);298299// Ok buttons300set_ok_button_text(TTR("Merge (Keep original Atlases)"));301get_ok_button()->set_disabled(true);302merge_button = add_button(TTR("Merge"), true, "merge");303merge_button->set_disabled(true);304305HSplitContainer *atlas_merging_h_split_container = memnew(HSplitContainer);306atlas_merging_h_split_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);307atlas_merging_h_split_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);308add_child(atlas_merging_h_split_container);309310// Atlas sources item list.311atlas_merging_atlases_list = memnew(ItemList);312atlas_merging_atlases_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);313atlas_merging_atlases_list->set_fixed_icon_size(Size2(60, 60) * EDSCALE);314atlas_merging_atlases_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);315atlas_merging_atlases_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);316atlas_merging_atlases_list->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);317atlas_merging_atlases_list->set_custom_minimum_size(Size2(100, 200));318atlas_merging_atlases_list->set_select_mode(ItemList::SELECT_MULTI);319atlas_merging_atlases_list->set_theme_type_variation("ItemListSecondary");320atlas_merging_atlases_list->connect("multi_selected", callable_mp(this, &AtlasMergingDialog::_update_texture).unbind(2));321atlas_merging_h_split_container->add_child(atlas_merging_atlases_list);322323VBoxContainer *atlas_merging_right_panel = memnew(VBoxContainer);324atlas_merging_right_panel->set_h_size_flags(Control::SIZE_EXPAND_FILL);325atlas_merging_right_panel->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);326atlas_merging_h_split_container->add_child(atlas_merging_right_panel);327328// Settings.329Label *settings_label = memnew(Label);330settings_label->set_text(TTR("Settings:"));331atlas_merging_right_panel->add_child(settings_label);332333columns_editor_property = memnew(EditorPropertyInteger);334columns_editor_property->set_label(TTR("Next Line After Column"));335columns_editor_property->set_object_and_property(this, "next_line_after_column");336columns_editor_property->update_property();337columns_editor_property->connect("property_changed", callable_mp(this, &AtlasMergingDialog::_property_changed));338atlas_merging_right_panel->add_child(columns_editor_property);339340// Preview.341Label *preview_label = memnew(Label);342preview_label->set_text(TTR("Preview:"));343atlas_merging_right_panel->add_child(preview_label);344345preview = memnew(TextureRect);346preview->set_h_size_flags(Control::SIZE_EXPAND_FILL);347preview->set_v_size_flags(Control::SIZE_EXPAND_FILL);348preview->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);349preview->hide();350preview->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);351atlas_merging_right_panel->add_child(preview);352353select_2_atlases_label = memnew(Label);354select_2_atlases_label->set_focus_mode(Control::FOCUS_ACCESSIBILITY);355select_2_atlases_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);356select_2_atlases_label->set_v_size_flags(Control::SIZE_EXPAND_FILL);357select_2_atlases_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);358select_2_atlases_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);359select_2_atlases_label->set_text(TTR("Please select two atlases or more."));360atlas_merging_right_panel->add_child(select_2_atlases_label);361362// The file dialog to choose the texture path.363editor_file_dialog = memnew(EditorFileDialog);364editor_file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);365editor_file_dialog->add_filter("*.png");366editor_file_dialog->connect("file_selected", callable_mp(this, &AtlasMergingDialog::_merge_confirmed));367add_child(editor_file_dialog);368}369370371