| 1 | /**************************************************************************/ |
| 2 | /* tile_set_scenes_collection_source_editor.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 "tile_set_scenes_collection_source_editor.h" |
| 32 | |
| 33 | #include "editor/editor_node.h" |
| 34 | #include "editor/editor_undo_redo_manager.h" |
| 35 | #include "editor/file_system/editor_file_system.h" |
| 36 | #include "editor/gui/editor_file_dialog.h" |
| 37 | #include "editor/inspector/editor_resource_preview.h" |
| 38 | #include "editor/scene/2d/tiles/tile_set_editor.h" |
| 39 | #include "editor/settings/editor_settings.h" |
| 40 | #include "editor/themes/editor_scale.h" |
| 41 | |
| 42 | #include "scene/gui/button.h" |
| 43 | #include "scene/gui/item_list.h" |
| 44 | #include "scene/gui/label.h" |
| 45 | #include "scene/gui/split_container.h" |
| 46 | |
| 47 | void TileSetScenesCollectionSourceEditor::TileSetScenesCollectionProxyObject::set_id(int p_id) { |
| 48 | ERR_FAIL_COND(p_id < 0); |
| 49 | if (source_id == p_id) { |
| 50 | return; |
| 51 | } |
| 52 | ERR_FAIL_COND_MSG(tile_set->has_source(p_id), vformat("Cannot change TileSet Scenes Collection source ID. Another TileSet source exists with id %d.", p_id)); |
| 53 | |
| 54 | int previous_source = source_id; |
| 55 | source_id = p_id; // source_id must be updated before, because it's used by the source list update. |
| 56 | tile_set->set_source_id(previous_source, p_id); |
| 57 | emit_signal(CoreStringName(changed), "id"); |
| 58 | } |
| 59 | |
| 60 | int TileSetScenesCollectionSourceEditor::TileSetScenesCollectionProxyObject::get_id() { |
| 61 | return source_id; |
| 62 | } |
| 63 | |
| 64 | bool TileSetScenesCollectionSourceEditor::TileSetScenesCollectionProxyObject::_set(const StringName &p_name, const Variant &p_value) { |
| 65 | String name = p_name; |
| 66 | if (name == "name") { |
| 67 | // Use the resource_name property to store the source's name. |
| 68 | name = "resource_name"; |
| 69 | } |
| 70 | bool valid = false; |
| 71 | tile_set_scenes_collection_source->set(name, p_value, &valid); |
| 72 | if (valid) { |
| 73 | emit_signal(CoreStringName(changed), String(name).utf8().get_data()); |
| 74 | } |
| 75 | return valid; |
| 76 | } |
| 77 | |
| 78 | bool TileSetScenesCollectionSourceEditor::TileSetScenesCollectionProxyObject::_get(const StringName &p_name, Variant &r_ret) const { |
| 79 | if (tile_set_scenes_collection_source.is_null()) { |
| 80 | return false; |
| 81 | } |
| 82 | String name = p_name; |
| 83 | if (name == "name") { |
| 84 | // Use the resource_name property to store the source's name. |
| 85 | name = "resource_name"; |
| 86 | } |
| 87 | bool valid = false; |
| 88 | r_ret = tile_set_scenes_collection_source->get(name, &valid); |
| 89 | return valid; |
| 90 | } |
| 91 | |
| 92 | void TileSetScenesCollectionSourceEditor::TileSetScenesCollectionProxyObject::_get_property_list(List<PropertyInfo> *p_list) const { |
| 93 | p_list->push_back(PropertyInfo(Variant::STRING, "name", PROPERTY_HINT_NONE, "")); |
| 94 | } |
| 95 | |
| 96 | void TileSetScenesCollectionSourceEditor::TileSetScenesCollectionProxyObject::_bind_methods() { |
| 97 | // -- Shape and layout -- |
| 98 | ClassDB::bind_method(D_METHOD("set_id", "id"), &TileSetScenesCollectionSourceEditor::TileSetScenesCollectionProxyObject::set_id); |
| 99 | ClassDB::bind_method(D_METHOD("get_id"), &TileSetScenesCollectionSourceEditor::TileSetScenesCollectionProxyObject::get_id); |
| 100 | |
| 101 | ADD_PROPERTY(PropertyInfo(Variant::INT, "id"), "set_id", "get_id"); |
| 102 | |
| 103 | ADD_SIGNAL(MethodInfo("changed", PropertyInfo(Variant::STRING, "what"))); |
| 104 | } |
| 105 | |
| 106 | void TileSetScenesCollectionSourceEditor::TileSetScenesCollectionProxyObject::edit(Ref<TileSet> p_tile_set, TileSetScenesCollectionSource *p_tile_set_scenes_collection_source, int p_source_id) { |
| 107 | ERR_FAIL_COND(p_tile_set.is_null()); |
| 108 | ERR_FAIL_NULL(p_tile_set_scenes_collection_source); |
| 109 | ERR_FAIL_COND(p_source_id < 0); |
| 110 | ERR_FAIL_COND(p_tile_set->get_source(p_source_id) != p_tile_set_scenes_collection_source); |
| 111 | |
| 112 | if (tile_set == p_tile_set && tile_set_scenes_collection_source == p_tile_set_scenes_collection_source && source_id == p_source_id) { |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | // Disconnect to changes. |
| 117 | if (tile_set_scenes_collection_source.is_valid()) { |
| 118 | tile_set_scenes_collection_source->disconnect(CoreStringName(property_list_changed), callable_mp((Object *)this, &Object::notify_property_list_changed)); |
| 119 | } |
| 120 | |
| 121 | tile_set = p_tile_set; |
| 122 | tile_set_scenes_collection_source = p_tile_set_scenes_collection_source; |
| 123 | source_id = p_source_id; |
| 124 | |
| 125 | // Connect to changes. |
| 126 | if (tile_set_scenes_collection_source.is_valid()) { |
| 127 | if (!tile_set_scenes_collection_source->is_connected(CoreStringName(property_list_changed), callable_mp((Object *)this, &Object::notify_property_list_changed))) { |
| 128 | tile_set_scenes_collection_source->connect(CoreStringName(property_list_changed), callable_mp((Object *)this, &Object::notify_property_list_changed)); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | notify_property_list_changed(); |
| 133 | } |
| 134 | |
| 135 | // -- Proxy object used by the tile inspector -- |
| 136 | bool TileSetScenesCollectionSourceEditor::SceneTileProxyObject::_set(const StringName &p_name, const Variant &p_value) { |
| 137 | if (tile_set_scenes_collection_source.is_null()) { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | if (p_name == "id") { |
| 142 | int as_int = int(p_value); |
| 143 | ERR_FAIL_COND_V(as_int < 0, false); |
| 144 | ERR_FAIL_COND_V(tile_set_scenes_collection_source->has_scene_tile_id(as_int), false); |
| 145 | tile_set_scenes_collection_source->set_scene_tile_id(scene_id, as_int); |
| 146 | scene_id = as_int; |
| 147 | emit_signal(CoreStringName(changed), "id"); |
| 148 | for (int i = 0; i < tile_set_scenes_collection_source_editor->scene_tiles_list->get_item_count(); i++) { |
| 149 | if (int(tile_set_scenes_collection_source_editor->scene_tiles_list->get_item_metadata(i)) == scene_id) { |
| 150 | tile_set_scenes_collection_source_editor->scene_tiles_list->select(i); |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | return true; |
| 155 | } else if (p_name == "scene") { |
| 156 | tile_set_scenes_collection_source->set_scene_tile_scene(scene_id, p_value); |
| 157 | emit_signal(CoreStringName(changed), "scene"); |
| 158 | return true; |
| 159 | } else if (p_name == "display_placeholder") { |
| 160 | tile_set_scenes_collection_source->set_scene_tile_display_placeholder(scene_id, p_value); |
| 161 | emit_signal(CoreStringName(changed), "display_placeholder"); |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | bool TileSetScenesCollectionSourceEditor::SceneTileProxyObject::_get(const StringName &p_name, Variant &r_ret) const { |
| 169 | if (tile_set_scenes_collection_source.is_null()) { |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | if (p_name == "id") { |
| 174 | r_ret = scene_id; |
| 175 | return true; |
| 176 | } else if (p_name == "scene") { |
| 177 | r_ret = tile_set_scenes_collection_source->get_scene_tile_scene(scene_id); |
| 178 | return true; |
| 179 | } else if (p_name == "display_placeholder") { |
| 180 | r_ret = tile_set_scenes_collection_source->get_scene_tile_display_placeholder(scene_id); |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | void TileSetScenesCollectionSourceEditor::SceneTileProxyObject::_get_property_list(List<PropertyInfo> *p_list) const { |
| 188 | if (tile_set_scenes_collection_source.is_null()) { |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | p_list->push_back(PropertyInfo(Variant::INT, "id", PROPERTY_HINT_NONE, "")); |
| 193 | p_list->push_back(PropertyInfo(Variant::OBJECT, "scene", PROPERTY_HINT_RESOURCE_TYPE, "PackedScene")); |
| 194 | p_list->push_back(PropertyInfo(Variant::BOOL, "display_placeholder", PROPERTY_HINT_NONE, "")); |
| 195 | } |
| 196 | |
| 197 | void TileSetScenesCollectionSourceEditor::SceneTileProxyObject::edit(TileSetScenesCollectionSource *p_tile_set_scenes_collection_source, int p_scene_id) { |
| 198 | ERR_FAIL_NULL(p_tile_set_scenes_collection_source); |
| 199 | ERR_FAIL_COND(!p_tile_set_scenes_collection_source->has_scene_tile_id(p_scene_id)); |
| 200 | |
| 201 | if (tile_set_scenes_collection_source == p_tile_set_scenes_collection_source && scene_id == p_scene_id) { |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | tile_set_scenes_collection_source = p_tile_set_scenes_collection_source; |
| 206 | scene_id = p_scene_id; |
| 207 | |
| 208 | notify_property_list_changed(); |
| 209 | } |
| 210 | |
| 211 | void TileSetScenesCollectionSourceEditor::SceneTileProxyObject::_bind_methods() { |
| 212 | ADD_SIGNAL(MethodInfo("changed", PropertyInfo(Variant::STRING, "what"))); |
| 213 | } |
| 214 | |
| 215 | void TileSetScenesCollectionSourceEditor::_scenes_collection_source_proxy_object_changed(const String &p_what) { |
| 216 | if (p_what == "id") { |
| 217 | emit_signal(SNAME("source_id_changed"), scenes_collection_source_proxy_object->get_id()); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void TileSetScenesCollectionSourceEditor::_tile_set_scenes_collection_source_changed() { |
| 222 | tile_set_scenes_collection_source_changed_needs_update = true; |
| 223 | } |
| 224 | |
| 225 | void TileSetScenesCollectionSourceEditor::_scene_thumbnail_done(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, int p_idx) { |
| 226 | if (p_idx >= 0 && p_idx < scene_tiles_list->get_item_count()) { |
| 227 | scene_tiles_list->set_item_icon(p_idx, p_preview); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | void TileSetScenesCollectionSourceEditor::_scenes_list_item_activated(int p_index) { |
| 232 | Ref<PackedScene> packed_scene = tile_set_scenes_collection_source->get_scene_tile_scene(scene_tiles_list->get_item_metadata(p_index)); |
| 233 | if (packed_scene.is_valid()) { |
| 234 | EditorNode::get_singleton()->load_scene(packed_scene->get_path()); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | void TileSetScenesCollectionSourceEditor::_source_add_pressed() { |
| 239 | if (!scene_select_dialog) { |
| 240 | scene_select_dialog = memnew(EditorFileDialog); |
| 241 | add_child(scene_select_dialog); |
| 242 | scene_select_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE); |
| 243 | scene_select_dialog->connect("file_selected", callable_mp(this, &TileSetScenesCollectionSourceEditor::_scene_file_selected)); |
| 244 | |
| 245 | for (const String &E : Vector<String>{ "tscn", "scn" }) { |
| 246 | scene_select_dialog->add_filter("*." + E, E.to_upper()); |
| 247 | } |
| 248 | } |
| 249 | scene_select_dialog->popup_file_dialog(); |
| 250 | } |
| 251 | |
| 252 | void TileSetScenesCollectionSourceEditor::_scene_file_selected(const String &p_path) { |
| 253 | Ref<PackedScene> scene = ResourceLoader::load(p_path); |
| 254 | |
| 255 | int scene_id = tile_set_scenes_collection_source->get_next_scene_tile_id(); |
| 256 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 257 | undo_redo->create_action(TTR("Add a Scene Tile")); |
| 258 | undo_redo->add_do_method(*tile_set_scenes_collection_source, "create_scene_tile", scene, scene_id); |
| 259 | undo_redo->add_do_method(this, "_update_all"); |
| 260 | undo_redo->add_undo_method(*tile_set_scenes_collection_source, "remove_scene_tile", scene_id); |
| 261 | undo_redo->add_undo_method(this, "_update_all"); |
| 262 | undo_redo->commit_action(); |
| 263 | } |
| 264 | |
| 265 | void TileSetScenesCollectionSourceEditor::_source_delete_pressed() { |
| 266 | Vector<int> selected_indices = scene_tiles_list->get_selected_items(); |
| 267 | ERR_FAIL_COND(selected_indices.is_empty()); |
| 268 | int scene_id = scene_tiles_list->get_item_metadata(selected_indices[0]); |
| 269 | |
| 270 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 271 | undo_redo->create_action(TTR("Remove a Scene Tile")); |
| 272 | undo_redo->add_do_method(*tile_set_scenes_collection_source, "remove_scene_tile", scene_id); |
| 273 | undo_redo->add_do_method(this, "_update_all"); |
| 274 | undo_redo->add_undo_method(*tile_set_scenes_collection_source, "create_scene_tile", tile_set_scenes_collection_source->get_scene_tile_scene(scene_id), scene_id); |
| 275 | undo_redo->add_undo_method(this, "_update_all"); |
| 276 | undo_redo->commit_action(); |
| 277 | } |
| 278 | |
| 279 | void TileSetScenesCollectionSourceEditor::_update_source_inspector() { |
| 280 | // Update the proxy object. |
| 281 | scenes_collection_source_proxy_object->edit(tile_set, *tile_set_scenes_collection_source, tile_set_source_id); |
| 282 | } |
| 283 | |
| 284 | void TileSetScenesCollectionSourceEditor::_update_tile_inspector() { |
| 285 | Vector<int> selected_indices = scene_tiles_list->get_selected_items(); |
| 286 | bool has_atlas_tile_selected = (selected_indices.size() > 0); |
| 287 | |
| 288 | // Update the proxy object. |
| 289 | if (has_atlas_tile_selected) { |
| 290 | int scene_id = scene_tiles_list->get_item_metadata(selected_indices[0]); |
| 291 | tile_proxy_object->edit(*tile_set_scenes_collection_source, scene_id); |
| 292 | } |
| 293 | |
| 294 | // Update visibility. |
| 295 | tile_inspector_label->set_visible(has_atlas_tile_selected); |
| 296 | tile_inspector->set_visible(has_atlas_tile_selected); |
| 297 | } |
| 298 | |
| 299 | void TileSetScenesCollectionSourceEditor::_update_action_buttons() { |
| 300 | Vector<int> selected_indices = scene_tiles_list->get_selected_items(); |
| 301 | scene_tile_delete_button->set_disabled(selected_indices.is_empty() || read_only); |
| 302 | } |
| 303 | |
| 304 | void TileSetScenesCollectionSourceEditor::_update_all() { |
| 305 | _update_scenes_list(); |
| 306 | _update_action_buttons(); |
| 307 | _update_tile_inspector(); |
| 308 | } |
| 309 | |
| 310 | void TileSetScenesCollectionSourceEditor::_update_scenes_list() { |
| 311 | if (tile_set_scenes_collection_source.is_null()) { |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | // Get the previously selected id. |
| 316 | Vector<int> selected_indices = scene_tiles_list->get_selected_items(); |
| 317 | int old_selected_scene_id = (selected_indices.size() > 0) ? int(scene_tiles_list->get_item_metadata(selected_indices[0])) : -1; |
| 318 | |
| 319 | // Clear the list. |
| 320 | scene_tiles_list->clear(); |
| 321 | |
| 322 | // Rebuild the list. |
| 323 | int to_reselect = -1; |
| 324 | for (int i = 0; i < tile_set_scenes_collection_source->get_scene_tiles_count(); i++) { |
| 325 | int scene_id = tile_set_scenes_collection_source->get_scene_tile_id(i); |
| 326 | |
| 327 | Ref<PackedScene> scene = tile_set_scenes_collection_source->get_scene_tile_scene(scene_id); |
| 328 | |
| 329 | int item_index = 0; |
| 330 | if (scene.is_valid()) { |
| 331 | item_index = scene_tiles_list->add_item(vformat("%s (path:%s id:%d)", scene->get_path().get_file().get_basename(), scene->get_path(), scene_id)); |
| 332 | EditorResourcePreview::get_singleton()->queue_edited_resource_preview(scene, callable_mp(this, &TileSetScenesCollectionSourceEditor::_scene_thumbnail_done).bind(i)); |
| 333 | } else { |
| 334 | item_index = scene_tiles_list->add_item(TTR("Tile with Invalid Scene"), get_editor_theme_icon(SNAME("PackedScene"))); |
| 335 | } |
| 336 | scene_tiles_list->set_item_metadata(item_index, scene_id); |
| 337 | |
| 338 | if (old_selected_scene_id >= 0 && scene_id == old_selected_scene_id) { |
| 339 | to_reselect = i; |
| 340 | } |
| 341 | } |
| 342 | if (scene_tiles_list->get_item_count() == 0) { |
| 343 | scene_tiles_list->add_item(TTR("Drag and drop scenes here or use the Add button.")); |
| 344 | scene_tiles_list->set_item_disabled(-1, true); |
| 345 | } |
| 346 | |
| 347 | // Reselect if needed. |
| 348 | if (to_reselect >= 0) { |
| 349 | scene_tiles_list->select(to_reselect); |
| 350 | } |
| 351 | |
| 352 | // Icon size update. |
| 353 | int int_size = int(EDITOR_GET("filesystem/file_dialog/thumbnail_size")) * EDSCALE; |
| 354 | scene_tiles_list->set_fixed_icon_size(Vector2(int_size, int_size)); |
| 355 | } |
| 356 | |
| 357 | void TileSetScenesCollectionSourceEditor::_notification(int p_what) { |
| 358 | switch (p_what) { |
| 359 | case NOTIFICATION_READY: { |
| 360 | scenes_collection_source_inspector->edit(scenes_collection_source_proxy_object); |
| 361 | scenes_collection_source_inspector->add_custom_property_description("TileSetScenesCollectionProxyObject", "id", TTR("The tile's unique identifier within this TileSet. Each tile stores its source ID, so changing one may make tiles invalid.")); |
| 362 | scenes_collection_source_inspector->add_custom_property_description("TileSetScenesCollectionProxyObject", "name", TTR("The human-readable name for the scene collection. Use a descriptive name here for organizational purposes (such as \"obstacles\", \"decoration\", etc.).")); |
| 363 | |
| 364 | tile_inspector->edit(tile_proxy_object); |
| 365 | tile_inspector->add_custom_property_description("SceneTileProxyObject", "id", TTR("ID of the scene tile in the collection. Each painted tile has associated ID, so changing this property may cause your TileMaps to not display properly.")); |
| 366 | tile_inspector->add_custom_property_description("SceneTileProxyObject", "scene", TTR("Absolute path to the scene associated with this tile.")); |
| 367 | tile_inspector->add_custom_property_description("SceneTileProxyObject", "display_placeholder", TTR("If [code]true[/code], a placeholder marker will be displayed on top of the scene's preview. The marker is displayed anyway if the scene has no valid preview.")); |
| 368 | } break; |
| 369 | |
| 370 | case NOTIFICATION_THEME_CHANGED: { |
| 371 | scene_tile_add_button->set_button_icon(get_editor_theme_icon(SNAME("Add"))); |
| 372 | scene_tile_delete_button->set_button_icon(get_editor_theme_icon(SNAME("Remove"))); |
| 373 | _update_scenes_list(); |
| 374 | } break; |
| 375 | |
| 376 | case NOTIFICATION_INTERNAL_PROCESS: { |
| 377 | if (tile_set_scenes_collection_source_changed_needs_update) { |
| 378 | read_only = false; |
| 379 | // Add the listener again and check for read-only status. |
| 380 | if (tile_set.is_valid()) { |
| 381 | read_only = EditorNode::get_singleton()->is_resource_read_only(tile_set); |
| 382 | } |
| 383 | |
| 384 | // Update everything. |
| 385 | _update_source_inspector(); |
| 386 | _update_scenes_list(); |
| 387 | _update_action_buttons(); |
| 388 | _update_tile_inspector(); |
| 389 | tile_set_scenes_collection_source_changed_needs_update = false; |
| 390 | } |
| 391 | } break; |
| 392 | |
| 393 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 394 | // Update things just in case. |
| 395 | _update_scenes_list(); |
| 396 | _update_action_buttons(); |
| 397 | } break; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | void TileSetScenesCollectionSourceEditor::edit(Ref<TileSet> p_tile_set, TileSetScenesCollectionSource *p_tile_set_scenes_collection_source, int p_source_id) { |
| 402 | ERR_FAIL_COND(p_tile_set.is_null()); |
| 403 | ERR_FAIL_NULL(p_tile_set_scenes_collection_source); |
| 404 | ERR_FAIL_COND(p_source_id < 0); |
| 405 | ERR_FAIL_COND(p_tile_set->get_source(p_source_id) != p_tile_set_scenes_collection_source); |
| 406 | |
| 407 | bool new_read_only_state = false; |
| 408 | if (p_tile_set.is_valid()) { |
| 409 | new_read_only_state = EditorNode::get_singleton()->is_resource_read_only(p_tile_set); |
| 410 | } |
| 411 | |
| 412 | if (p_tile_set == tile_set && p_tile_set_scenes_collection_source == *tile_set_scenes_collection_source && p_source_id == tile_set_source_id && new_read_only_state == read_only) { |
| 413 | return; |
| 414 | } |
| 415 | |
| 416 | // Remove listener for old objects. |
| 417 | if (tile_set_scenes_collection_source.is_valid()) { |
| 418 | tile_set_scenes_collection_source->disconnect_changed(callable_mp(this, &TileSetScenesCollectionSourceEditor::_tile_set_scenes_collection_source_changed)); |
| 419 | } |
| 420 | |
| 421 | // Change the edited object. |
| 422 | tile_set = p_tile_set; |
| 423 | tile_set_scenes_collection_source = p_tile_set_scenes_collection_source; |
| 424 | tile_set_source_id = p_source_id; |
| 425 | |
| 426 | // Read-only status is false by default |
| 427 | read_only = new_read_only_state; |
| 428 | |
| 429 | if (tile_set.is_valid()) { |
| 430 | scenes_collection_source_inspector->set_read_only(read_only); |
| 431 | tile_inspector->set_read_only(read_only); |
| 432 | |
| 433 | scene_tile_add_button->set_disabled(read_only); |
| 434 | } |
| 435 | |
| 436 | // Add the listener again. |
| 437 | if (tile_set_scenes_collection_source.is_valid()) { |
| 438 | tile_set_scenes_collection_source->connect_changed(callable_mp(this, &TileSetScenesCollectionSourceEditor::_tile_set_scenes_collection_source_changed)); |
| 439 | } |
| 440 | |
| 441 | // Update everything. |
| 442 | _update_source_inspector(); |
| 443 | _update_scenes_list(); |
| 444 | _update_action_buttons(); |
| 445 | _update_tile_inspector(); |
| 446 | } |
| 447 | |
| 448 | void TileSetScenesCollectionSourceEditor::_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) { |
| 449 | if (!_can_drop_data_fw(p_point, p_data, p_from)) { |
| 450 | return; |
| 451 | } |
| 452 | |
| 453 | if (p_from == scene_tiles_list) { |
| 454 | // Handle dropping a texture in the list of atlas resources. |
| 455 | Dictionary d = p_data; |
| 456 | const Vector<String> files = d["files"]; |
| 457 | for (const String &file : files) { |
| 458 | const Ref<PackedScene> resource = ResourceLoader::load(file); |
| 459 | if (resource.is_valid()) { |
| 460 | int scene_id = tile_set_scenes_collection_source->get_next_scene_tile_id(); |
| 461 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 462 | undo_redo->create_action(TTR("Add a Scene Tile")); |
| 463 | undo_redo->add_do_method(*tile_set_scenes_collection_source, "create_scene_tile", resource, scene_id); |
| 464 | undo_redo->add_do_method(this, "_update_all"); |
| 465 | undo_redo->add_undo_method(*tile_set_scenes_collection_source, "remove_scene_tile", scene_id); |
| 466 | undo_redo->add_undo_method(this, "_update_all"); |
| 467 | undo_redo->commit_action(); |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | bool TileSetScenesCollectionSourceEditor::_can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const { |
| 474 | if (p_from == scene_tiles_list) { |
| 475 | Dictionary d = p_data; |
| 476 | |
| 477 | if (!d.has("type")) { |
| 478 | return false; |
| 479 | } |
| 480 | |
| 481 | // Check if we have a Texture2D. |
| 482 | if (String(d["type"]) == "files") { |
| 483 | Vector<String> files = d["files"]; |
| 484 | |
| 485 | if (files.is_empty()) { |
| 486 | return false; |
| 487 | } |
| 488 | |
| 489 | for (int i = 0; i < files.size(); i++) { |
| 490 | String ftype = EditorFileSystem::get_singleton()->get_file_type(files[i]); |
| 491 | |
| 492 | if (!ClassDB::is_parent_class(ftype, "PackedScene")) { |
| 493 | return false; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | return true; |
| 498 | } |
| 499 | } |
| 500 | return false; |
| 501 | } |
| 502 | |
| 503 | void TileSetScenesCollectionSourceEditor::_bind_methods() { |
| 504 | ClassDB::bind_method(D_METHOD("_update_all"), &TileSetScenesCollectionSourceEditor::_update_all); |
| 505 | |
| 506 | ADD_SIGNAL(MethodInfo("source_id_changed", PropertyInfo(Variant::INT, "source_id"))); |
| 507 | } |
| 508 | |
| 509 | TileSetScenesCollectionSourceEditor::TileSetScenesCollectionSourceEditor() { |
| 510 | // -- Right side -- |
| 511 | HSplitContainer *split_container_right_side = memnew(HSplitContainer); |
| 512 | split_container_right_side->set_h_size_flags(SIZE_EXPAND_FILL); |
| 513 | add_child(split_container_right_side); |
| 514 | |
| 515 | // Middle panel. |
| 516 | ScrollContainer *middle_panel = memnew(ScrollContainer); |
| 517 | middle_panel->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED); |
| 518 | middle_panel->set_custom_minimum_size(Size2(200, 0) * EDSCALE); |
| 519 | split_container_right_side->add_child(middle_panel); |
| 520 | |
| 521 | VBoxContainer *middle_vbox_container = memnew(VBoxContainer); |
| 522 | middle_vbox_container->set_h_size_flags(SIZE_EXPAND_FILL); |
| 523 | middle_panel->add_child(middle_vbox_container); |
| 524 | |
| 525 | // Scenes collection source inspector. |
| 526 | scenes_collection_source_inspector_label = memnew(Label); |
| 527 | scenes_collection_source_inspector_label->set_focus_mode(FOCUS_ACCESSIBILITY); |
| 528 | scenes_collection_source_inspector_label->set_text(TTR("Scenes collection properties:")); |
| 529 | middle_vbox_container->add_child(scenes_collection_source_inspector_label); |
| 530 | |
| 531 | scenes_collection_source_proxy_object = memnew(TileSetScenesCollectionProxyObject()); |
| 532 | scenes_collection_source_proxy_object->connect(CoreStringName(changed), callable_mp(this, &TileSetScenesCollectionSourceEditor::_scenes_collection_source_proxy_object_changed)); |
| 533 | |
| 534 | scenes_collection_source_inspector = memnew(EditorInspector); |
| 535 | scenes_collection_source_inspector->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED); |
| 536 | scenes_collection_source_inspector->set_use_doc_hints(true); |
| 537 | middle_vbox_container->add_child(scenes_collection_source_inspector); |
| 538 | |
| 539 | // Tile inspector. |
| 540 | tile_inspector_label = memnew(Label); |
| 541 | tile_inspector_label->set_text(TTR("Tile properties:")); |
| 542 | tile_inspector_label->hide(); |
| 543 | middle_vbox_container->add_child(tile_inspector_label); |
| 544 | |
| 545 | tile_proxy_object = memnew(SceneTileProxyObject(this)); |
| 546 | tile_proxy_object->connect(CoreStringName(changed), callable_mp(this, &TileSetScenesCollectionSourceEditor::_update_scenes_list).unbind(1)); |
| 547 | tile_proxy_object->connect(CoreStringName(changed), callable_mp(this, &TileSetScenesCollectionSourceEditor::_update_action_buttons).unbind(1)); |
| 548 | |
| 549 | tile_inspector = memnew(EditorInspector); |
| 550 | tile_inspector->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED); |
| 551 | tile_inspector->set_use_doc_hints(true); |
| 552 | tile_inspector->set_use_folding(true); |
| 553 | middle_vbox_container->add_child(tile_inspector); |
| 554 | |
| 555 | // Scenes list. |
| 556 | VBoxContainer *right_vbox_container = memnew(VBoxContainer); |
| 557 | split_container_right_side->add_child(right_vbox_container); |
| 558 | |
| 559 | scene_tiles_list = memnew(ItemList); |
| 560 | scene_tiles_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 561 | scene_tiles_list->set_h_size_flags(SIZE_EXPAND_FILL); |
| 562 | scene_tiles_list->set_v_size_flags(SIZE_EXPAND_FILL); |
| 563 | scene_tiles_list->set_theme_type_variation("ItemListSecondary"); |
| 564 | SET_DRAG_FORWARDING_CDU(scene_tiles_list, TileSetScenesCollectionSourceEditor); |
| 565 | scene_tiles_list->connect(SceneStringName(item_selected), callable_mp(this, &TileSetScenesCollectionSourceEditor::_update_tile_inspector).unbind(1)); |
| 566 | scene_tiles_list->connect(SceneStringName(item_selected), callable_mp(this, &TileSetScenesCollectionSourceEditor::_update_action_buttons).unbind(1)); |
| 567 | scene_tiles_list->connect("item_activated", callable_mp(this, &TileSetScenesCollectionSourceEditor::_scenes_list_item_activated)); |
| 568 | scene_tiles_list->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST); |
| 569 | right_vbox_container->add_child(scene_tiles_list); |
| 570 | |
| 571 | HBoxContainer *scenes_bottom_actions = memnew(HBoxContainer); |
| 572 | right_vbox_container->add_child(scenes_bottom_actions); |
| 573 | |
| 574 | scene_tile_add_button = memnew(Button); |
| 575 | scene_tile_add_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 576 | scene_tile_add_button->connect(SceneStringName(pressed), callable_mp(this, &TileSetScenesCollectionSourceEditor::_source_add_pressed)); |
| 577 | scene_tile_add_button->set_accessibility_name(TTRC("Add")); |
| 578 | scenes_bottom_actions->add_child(scene_tile_add_button); |
| 579 | |
| 580 | scene_tile_delete_button = memnew(Button); |
| 581 | scene_tile_delete_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 582 | scene_tile_delete_button->set_disabled(true); |
| 583 | scene_tile_delete_button->connect(SceneStringName(pressed), callable_mp(this, &TileSetScenesCollectionSourceEditor::_source_delete_pressed)); |
| 584 | scene_tile_delete_button->set_accessibility_name(TTRC("Delete")); |
| 585 | scenes_bottom_actions->add_child(scene_tile_delete_button); |
| 586 | |
| 587 | EditorInspector::add_inspector_plugin(memnew(TileSourceInspectorPlugin)); |
| 588 | } |
| 589 | |
| 590 | TileSetScenesCollectionSourceEditor::~TileSetScenesCollectionSourceEditor() { |
| 591 | memdelete(scenes_collection_source_proxy_object); |
| 592 | memdelete(tile_proxy_object); |
| 593 | } |