Debian Codegraph @

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

raw symbols SCIP
1 /**************************************************************************/
2 /* tile_set_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_editor.h"
32
33 #include "tile_data_editors.h"
34 #include "tiles_editor_plugin.h"
35
36 #include "editor/editor_node.h"
37 #include "editor/editor_undo_redo_manager.h"
38 #include "editor/file_system/editor_file_system.h"
39 #include "editor/gui/editor_file_dialog.h"
40 #include "editor/inspector/editor_inspector.h"
41 #include "editor/settings/editor_command_palette.h"
42 #include "editor/settings/editor_settings.h"
43 #include "editor/themes/editor_scale.h"
44
45 #include "scene/gui/box_container.h"
46 #include "scene/gui/control.h"
47 #include "scene/gui/dialogs.h"
48
49 TileSetEditor *TileSetEditor::singleton = nullptr;
50
51 void TileSetEditor::_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
52 ERR_FAIL_COND(tile_set.is_null());
53
54 if (!_can_drop_data_fw(p_point, p_data, p_from)) {
55 return;
56 }
57
58 if (p_from == sources_list) {
59 // Handle dropping a texture in the list of atlas resources.
60 Dictionary d = p_data;
61 Vector<String> files = d["files"];
62 _load_texture_files(files);
63 }
64 }
65
66 bool TileSetEditor::_can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
67 ERR_FAIL_COND_V(tile_set.is_null(), false);
68
69 if (read_only) {
70 return false;
71 }
72
73 if (p_from == sources_list) {
74 Dictionary d = p_data;
75
76 if (!d.has("type")) {
77 return false;
78 }
79
80 // Check if we have a Texture2D.
81 if (String(d["type"]) == "files") {
82 Vector<String> files = d["files"];
83
84 if (files.is_empty()) {
85 return false;
86 }
87
88 for (int i = 0; i < files.size(); i++) {
89 String ftype = EditorFileSystem::get_singleton()->get_file_type(files[i]);
90
91 if (!ClassDB::is_parent_class(ftype, "Texture2D")) {
92 return false;
93 }
94 }
95
96 return true;
97 }
98 }
99 return false;
100 }
101
102 void TileSetEditor::_load_texture_files(const Vector<String> &p_paths) {
103 int source_id = TileSet::INVALID_SOURCE;
104 Vector<Ref<TileSetAtlasSource>> atlases;
105
106 for (const String &p_path : p_paths) {
107 Ref<Texture2D> texture = ResourceLoader::load(p_path);
108
109 if (texture.is_null()) {
110 EditorNode::get_singleton()->show_warning(TTR("Invalid texture selected."));
111 continue;
112 }
113
114 // Retrieve the id for the next created source.
115 source_id = tile_set->get_next_source_id();
116
117 // Actually create the new source.
118 Ref<TileSetAtlasSource> atlas_source = memnew(TileSetAtlasSource);
119 atlas_source->set_texture(texture);
120 atlas_source->set_texture_region_size(tile_set->get_tile_size());
121
122 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
123 undo_redo->create_action(TTR("Add a new atlas source"));
124 undo_redo->add_do_method(*tile_set, "add_source", atlas_source, source_id);
125 undo_redo->add_undo_method(*tile_set, "remove_source", source_id);
126 undo_redo->commit_action();
127
128 atlases.append(atlas_source);
129 }
130
131 if (!atlases.is_empty()) {
132 tile_set_atlas_source_editor->init_new_atlases(atlases);
133 }
134
135 // Update the selected source (thus triggering an update).
136 _update_sources_list(source_id);
137 }
138
139 void TileSetEditor::_update_sources_list(int force_selected_id) {
140 if (tile_set.is_null()) {
141 return;
142 }
143
144 // Get the previously selected id.
145 int old_selected = TileSet::INVALID_SOURCE;
146 if (sources_list->get_current() >= 0) {
147 int source_id = sources_list->get_item_metadata(sources_list->get_current());
148 if (tile_set->has_source(source_id)) {
149 old_selected = source_id;
150 }
151 }
152
153 int to_select = TileSet::INVALID_SOURCE;
154 if (force_selected_id >= 0) {
155 to_select = force_selected_id;
156 } else if (old_selected >= 0) {
157 to_select = old_selected;
158 }
159
160 // Clear the list.
161 sources_list->clear();
162
163 // Update the atlas sources.
164 List<int> source_ids = TilesEditorUtils::get_singleton()->get_sorted_sources(tile_set);
165 for (const int &source_id : source_ids) {
166 TileSetSource *source = *tile_set->get_source(source_id);
167
168 Ref<Texture2D> texture;
169 String item_text;
170
171 // Common to all type of sources.
172 if (!source->get_name().is_empty()) {
173 item_text = source->get_name();
174 }
175
176 // Atlas source.
177 TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
178 if (atlas_source) {
179 texture = atlas_source->get_texture();
180 if (item_text.is_empty()) {
181 if (texture.is_valid()) {
182 item_text = texture->get_path().get_file();
183 } else {
184 item_text = vformat(TTR("No Texture Atlas Source (ID: %d)"), source_id);
185 }
186 }
187 }
188
189 // Scene collection source.
190 TileSetScenesCollectionSource *scene_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source);
191 if (scene_collection_source) {
192 texture = get_editor_theme_icon(SNAME("PackedScene"));
193 if (item_text.is_empty()) {
194 if (scene_collection_source->get_scene_tiles_count() > 0) {
195 item_text = vformat(TTR("Scene Collection Source (ID: %d)"), source_id);
196 } else {
197 item_text = vformat(TTR("Empty Scene Collection Source (ID: %d)"), source_id);
198 }
199 }
200 }
201
202 // Use default if not valid.
203 if (item_text.is_empty()) {
204 item_text = vformat(TTR("Unknown Type Source (ID: %d)"), source_id);
205 }
206 if (texture.is_null()) {
207 texture = missing_texture_texture;
208 }
209
210 sources_list->add_item(item_text, texture);
211 sources_list->set_item_metadata(-1, source_id);
212 }
213
214 // Set again the current selected item if needed.
215 if (to_select >= 0) {
216 for (int i = 0; i < sources_list->get_item_count(); i++) {
217 if ((int)sources_list->get_item_metadata(i) == to_select) {
218 sources_list->set_current(i);
219 sources_list->ensure_current_is_visible();
220 if (old_selected != to_select) {
221 sources_list->emit_signal(SceneStringName(item_selected), sources_list->get_current());
222 }
223 break;
224 }
225 }
226 }
227
228 // If nothing is selected, select the first entry.
229 if (sources_list->get_current() < 0 && sources_list->get_item_count() > 0) {
230 sources_list->set_current(0);
231 if (old_selected != int(sources_list->get_item_metadata(0))) {
232 sources_list->emit_signal(SceneStringName(item_selected), sources_list->get_current());
233 }
234 }
235
236 // If there is no source left, hide all editors and show the label.
237 _source_selected(sources_list->get_current());
238
239 // Synchronize the lists.
240 TilesEditorUtils::get_singleton()->set_sources_lists_current(sources_list->get_current());
241 }
242
243 void TileSetEditor::_source_selected(int p_source_index) {
244 ERR_FAIL_COND(tile_set.is_null());
245
246 // Update the selected source.
247 sources_delete_button->set_disabled(p_source_index < 0 || read_only);
248
249 if (p_source_index >= 0) {
250 int source_id = sources_list->get_item_metadata(p_source_index);
251 TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(*tile_set->get_source(source_id));
252 TileSetScenesCollectionSource *scenes_collection_source = Object::cast_to<TileSetScenesCollectionSource>(*tile_set->get_source(source_id));
253 if (atlas_source) {
254 no_source_selected_label->hide();
255 tile_set_atlas_source_editor->edit(*tile_set, atlas_source, source_id);
256 tile_set_atlas_source_editor->show();
257 tile_set_scenes_collection_source_editor->hide();
258 } else if (scenes_collection_source) {
259 no_source_selected_label->hide();
260 tile_set_atlas_source_editor->hide();
261 tile_set_scenes_collection_source_editor->edit(*tile_set, scenes_collection_source, source_id);
262 tile_set_scenes_collection_source_editor->show();
263 } else {
264 no_source_selected_label->show();
265 tile_set_atlas_source_editor->hide();
266 tile_set_scenes_collection_source_editor->hide();
267 }
268 } else {
269 no_source_selected_label->show();
270 tile_set_atlas_source_editor->hide();
271 tile_set_scenes_collection_source_editor->hide();
272 }
273 }
274
275 void TileSetEditor::_source_delete_pressed() {
276 ERR_FAIL_COND(tile_set.is_null());
277
278 // Update the selected source.
279 int to_delete = sources_list->get_item_metadata(sources_list->get_current());
280
281 Ref<TileSetSource> source = tile_set->get_source(to_delete);
282
283 // Remove the source.
284 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
285 undo_redo->create_action(TTR("Remove source"));
286 undo_redo->add_do_method(*tile_set, "remove_source", to_delete);
287 undo_redo->add_undo_method(*tile_set, "add_source", source, to_delete);
288 undo_redo->commit_action();
289
290 _update_sources_list();
291 }
292
293 void TileSetEditor::_source_add_id_pressed(int p_id_pressed) {
294 ERR_FAIL_COND(tile_set.is_null());
295
296 switch (p_id_pressed) {
297 case 0: {
298 if (!texture_file_dialog) {
299 texture_file_dialog = memnew(EditorFileDialog);
300 add_child(texture_file_dialog);
301 texture_file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
302 texture_file_dialog->connect("files_selected", callable_mp(this, &TileSetEditor::_load_texture_files));
303
304 List<String> extensions;
305 ResourceLoader::get_recognized_extensions_for_type("Texture2D", &extensions);
306 for (const String &E : extensions) {
307 texture_file_dialog->add_filter("*." + E, E.to_upper());
308 }
309 }
310 texture_file_dialog->popup_file_dialog();
311 } break;
312 case 1: {
313 int source_id = tile_set->get_next_source_id();
314
315 Ref<TileSetScenesCollectionSource> scene_collection_source = memnew(TileSetScenesCollectionSource);
316
317 // Add a new source.
318 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
319 undo_redo->create_action(TTR("Add atlas source"));
320 undo_redo->add_do_method(*tile_set, "add_source", scene_collection_source, source_id);
321 undo_redo->add_undo_method(*tile_set, "remove_source", source_id);
322 undo_redo->commit_action();
323
324 _update_sources_list(source_id);
325 } break;
326 default:
327 ERR_FAIL();
328 }
329 }
330
331 void TileSetEditor::_sources_advanced_menu_id_pressed(int p_id_pressed) {
332 ERR_FAIL_COND(tile_set.is_null());
333
334 switch (p_id_pressed) {
335 case 0: {
336 atlas_merging_dialog->update_tile_set(tile_set);
337 atlas_merging_dialog->popup_centered_ratio(0.5);
338 } break;
339 case 1: {
340 tile_proxies_manager_dialog->update_tile_set(tile_set);
341 tile_proxies_manager_dialog->popup_centered_ratio(0.5);
342 } break;
343 }
344 }
345
346 void TileSetEditor::_set_source_sort(int p_sort) {
347 TilesEditorUtils::get_singleton()->set_sorting_option(p_sort);
348 for (int i = 0; i != TilesEditorUtils::SOURCE_SORT_MAX; i++) {
349 source_sort_button->get_popup()->set_item_checked(i, (i == (int)p_sort));
350 }
351
352 int old_selected = TileSet::INVALID_SOURCE;
353 if (sources_list->get_current() >= 0) {
354 int source_id = sources_list->get_item_metadata(sources_list->get_current());
355 if (tile_set->has_source(source_id)) {
356 old_selected = source_id;
357 }
358 }
359 _update_sources_list(old_selected);
360
361 if (!first_edit) {
362 EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "tile_source_sort", p_sort);
363 }
364 }
365
366 void TileSetEditor::_notification(int p_what) {
367 switch (p_what) {
368 case NOTIFICATION_THEME_CHANGED: {
369 sources_delete_button->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
370 sources_add_button->set_button_icon(get_editor_theme_icon(SNAME("Add")));
371 source_sort_button->set_button_icon(get_editor_theme_icon(SNAME("Sort")));
372 sources_advanced_menu_button->set_button_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl")));
373 missing_texture_texture = get_editor_theme_icon(SNAME("TileSet"));
374 expanded_area->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("expand_panel"), SNAME("TileSetEditor")));
375 _update_sources_list();
376 } break;
377
378 case NOTIFICATION_INTERNAL_PROCESS: {
379 if (tile_set_changed_needs_update) {
380 if (tile_set.is_valid()) {
381 tile_set->set_edited(true);
382 }
383
384 read_only = false;
385 if (tile_set.is_valid()) {
386 read_only = EditorNode::get_singleton()->is_resource_read_only(tile_set);
387 }
388
389 _update_sources_list();
390 _update_patterns_list();
391
392 sources_add_button->set_disabled(read_only);
393 sources_advanced_menu_button->set_disabled(read_only);
394 source_sort_button->set_disabled(read_only);
395
396 tile_set_changed_needs_update = false;
397 }
398 } break;
399
400 case NOTIFICATION_VISIBILITY_CHANGED: {
401 if (!is_visible_in_tree()) {
402 remove_expanded_editor();
403 }
404 } break;
405 }
406 }
407
408 void TileSetEditor::_patterns_item_list_gui_input(const Ref<InputEvent> &p_event) {
409 ERR_FAIL_COND(tile_set.is_null());
410
411 if (EditorNode::get_singleton()->is_resource_read_only(tile_set)) {
412 return;
413 }
414
415 if (ED_IS_SHORTCUT("tiles_editor/delete", p_event) && p_event->is_pressed() && !p_event->is_echo()) {
416 Vector<int> selected = patterns_item_list->get_selected_items();
417 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
418 undo_redo->create_action(TTR("Remove TileSet patterns"));
419 for (int i = 0; i < selected.size(); i++) {
420 int pattern_index = selected[i];
421 undo_redo->add_do_method(*tile_set, "remove_pattern", pattern_index);
422 undo_redo->add_undo_method(*tile_set, "add_pattern", tile_set->get_pattern(pattern_index), pattern_index);
423 }
424 undo_redo->commit_action();
425 patterns_item_list->accept_event();
426 }
427 }
428
429 void TileSetEditor::_pattern_preview_done(Ref<TileMapPattern> p_pattern, Ref<Texture2D> p_texture) {
430 // TODO optimize ?
431 for (int i = 0; i < patterns_item_list->get_item_count(); i++) {
432 if (patterns_item_list->get_item_metadata(i) == p_pattern) {
433 patterns_item_list->set_item_icon(i, p_texture);
434 break;
435 }
436 }
437 }
438
439 void TileSetEditor::_update_patterns_list() {
440 ERR_FAIL_COND(tile_set.is_null());
441
442 // Recreate the items.
443 patterns_item_list->clear();
444 for (int i = 0; i < tile_set->get_patterns_count(); i++) {
445 int id = patterns_item_list->add_item("");
446 patterns_item_list->set_item_metadata(id, tile_set->get_pattern(i));
447 patterns_item_list->set_item_tooltip(id, vformat(TTR("Index: %d"), i));
448 TilesEditorUtils::get_singleton()->queue_pattern_preview(tile_set, tile_set->get_pattern(i), callable_mp(this, &TileSetEditor::_pattern_preview_done));
449 }
450
451 // Update the label visibility.
452 patterns_help_label->set_visible(patterns_item_list->get_item_count() == 0);
453 }
454
455 void TileSetEditor::_tile_set_changed() {
456 tile_set_changed_needs_update = true;
457 }
458
459 void TileSetEditor::_tab_changed(int p_tab_changed) {
460 split_container->set_visible(p_tab_changed == 0);
461 patterns_mc->set_visible(p_tab_changed == 1);
462 }
463
464 void TileSetEditor::_move_tile_set_array_element(Object *p_undo_redo, Object *p_edited, const String &p_array_prefix, int p_from_index, int p_to_pos) {
465 EditorUndoRedoManager *undo_redo_man = Object::cast_to<EditorUndoRedoManager>(p_undo_redo);
466 ERR_FAIL_NULL(undo_redo_man);
467
468 TileSet *ed_tile_set = Object::cast_to<TileSet>(p_edited);
469 if (!ed_tile_set) {
470 return;
471 }
472
473 Vector<String> components = String(p_array_prefix).split("/", true, 2);
474
475 // Compute the array indices to save.
476 int begin = 0;
477 int end;
478 if (p_array_prefix == "occlusion_layer_") {
479 end = ed_tile_set->get_occlusion_layers_count();
480 } else if (p_array_prefix == "physics_layer_") {
481 end = ed_tile_set->get_physics_layers_count();
482 } else if (p_array_prefix == "terrain_set_") {
483 end = ed_tile_set->get_terrain_sets_count();
484 } else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int() && components[1] == "terrain_") {
485 int terrain_set = components[0].trim_prefix("terrain_set_").to_int();
486 end = ed_tile_set->get_terrains_count(terrain_set);
487 } else if (p_array_prefix == "navigation_layer_") {
488 end = ed_tile_set->get_navigation_layers_count();
489 } else if (p_array_prefix == "custom_data_layer_") {
490 end = ed_tile_set->get_custom_data_layers_count();
491 } else {
492 ERR_FAIL_MSG("Invalid array prefix for TileSet.");
493 }
494 if (p_from_index < 0) {
495 // Adding new.
496 if (p_to_pos >= 0) {
497 begin = p_to_pos;
498 } else {
499 end = 0; // Nothing to save when adding at the end.
500 }
501 } else if (p_to_pos < 0) {
502 // Removing.
503 begin = p_from_index;
504 } else {
505 // Moving.
506 begin = MIN(p_from_index, p_to_pos);
507 end = MIN(MAX(p_from_index, p_to_pos) + 1, end);
508 }
509
510 #define ADD_UNDO(obj, property) undo_redo_man->add_undo_property(obj, property, obj->get(property));
511
512 // Add undo method to adding array element.
513 if (p_array_prefix == "occlusion_layer_") {
514 if (p_from_index < 0) {
515 undo_redo_man->add_undo_method(ed_tile_set, "remove_occlusion_layer", p_to_pos < 0 ? ed_tile_set->get_occlusion_layers_count() : p_to_pos);
516 }
517 } else if (p_array_prefix == "physics_layer_") {
518 if (p_from_index < 0) {
519 undo_redo_man->add_undo_method(ed_tile_set, "remove_physics_layer", p_to_pos < 0 ? ed_tile_set->get_physics_layers_count() : p_to_pos);
520 }
521 } else if (p_array_prefix == "terrain_set_") {
522 if (p_from_index < 0) {
523 undo_redo_man->add_undo_method(ed_tile_set, "remove_terrain_set", p_to_pos < 0 ? ed_tile_set->get_terrain_sets_count() : p_to_pos);
524 }
525 } else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int() && components[1] == "terrain_") {
526 int terrain_set = components[0].trim_prefix("terrain_set_").to_int();
527 if (p_from_index < 0) {
528 undo_redo_man->add_undo_method(ed_tile_set, "remove_terrain", terrain_set, p_to_pos < 0 ? ed_tile_set->get_terrains_count(terrain_set) : p_to_pos);
529 }
530 } else if (p_array_prefix == "navigation_layer_") {
531 if (p_from_index < 0) {
532 undo_redo_man->add_undo_method(ed_tile_set, "remove_navigation_layer", p_to_pos < 0 ? ed_tile_set->get_navigation_layers_count() : p_to_pos);
533 }
534 } else if (p_array_prefix == "custom_data_layer_") {
535 if (p_from_index < 0) {
536 undo_redo_man->add_undo_method(ed_tile_set, "remove_custom_data_layer", p_to_pos < 0 ? ed_tile_set->get_custom_data_layers_count() : p_to_pos);
537 }
538 }
539
540 // Save layers' properties.
541 List<PropertyInfo> properties;
542 ed_tile_set->get_property_list(&properties);
543 for (PropertyInfo pi : properties) {
544 if (pi.name.begins_with(p_array_prefix)) {
545 String str = pi.name.trim_prefix(p_array_prefix);
546 int to_char_index = 0;
547 while (to_char_index < str.length()) {
548 if (!is_digit(str[to_char_index])) {
549 break;
550 }
551 to_char_index++;
552 }
553 if (to_char_index > 0) {
554 int array_index = str.left(to_char_index).to_int();
555 if (array_index >= begin && array_index < end) {
556 ADD_UNDO(ed_tile_set, pi.name);
557 }
558 }
559 }
560 }
561
562 // Save properties for TileSetAtlasSources tile data
563 for (int i = 0; i < ed_tile_set->get_source_count(); i++) {
564 int source_id = ed_tile_set->get_source_id(i);
565
566 Ref<TileSetAtlasSource> tas = ed_tile_set->get_source(source_id);
567 if (tas.is_valid()) {
568 for (int j = 0; j < tas->get_tiles_count(); j++) {
569 Vector2i tile_id = tas->get_tile_id(j);
570 for (int k = 0; k < tas->get_alternative_tiles_count(tile_id); k++) {
571 int alternative_id = tas->get_alternative_tile_id(tile_id, k);
572 TileData *tile_data = tas->get_tile_data(tile_id, alternative_id);
573 ERR_FAIL_NULL(tile_data);
574
575 // Actually saving stuff.
576 if (p_array_prefix == "occlusion_layer_") {
577 for (int layer_index = begin; layer_index < end; layer_index++) {
578 ADD_UNDO(tile_data, vformat("occlusion_layer_%d/polygon", layer_index));
579 }
580 } else if (p_array_prefix == "physics_layer_") {
581 for (int layer_index = begin; layer_index < end; layer_index++) {
582 ADD_UNDO(tile_data, vformat("physics_layer_%d/polygons_count", layer_index));
583 for (int polygon_index = 0; polygon_index < tile_data->get_collision_polygons_count(layer_index); polygon_index++) {
584 ADD_UNDO(tile_data, vformat("physics_layer_%d/polygon_%d/points", layer_index, polygon_index));
585 ADD_UNDO(tile_data, vformat("physics_layer_%d/polygon_%d/one_way", layer_index, polygon_index));
586 ADD_UNDO(tile_data, vformat("physics_layer_%d/polygon_%d/one_way_margin", layer_index, polygon_index));
587 }
588 }
589 } else if (p_array_prefix == "terrain_set_") {
590 ADD_UNDO(tile_data, "terrain_set");
591 for (int terrain_set_index = begin; terrain_set_index < end; terrain_set_index++) {
592 for (int l = 0; l < TileSet::CELL_NEIGHBOR_MAX; l++) {
593 TileSet::CellNeighbor bit = TileSet::CellNeighbor(l);
594 if (tile_data->is_valid_terrain_peering_bit(bit)) {
595 ADD_UNDO(tile_data, "terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[l]));
596 }
597 }
598 }
599 } else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int() && components[1] == "terrain_") {
600 for (int terrain_index = 0; terrain_index < TileSet::CELL_NEIGHBOR_MAX; terrain_index++) {
601 TileSet::CellNeighbor bit = TileSet::CellNeighbor(terrain_index);
602 if (tile_data->is_valid_terrain_peering_bit(bit)) {
603 ADD_UNDO(tile_data, "terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[terrain_index]));
604 }
605 }
606 } else if (p_array_prefix == "navigation_layer_") {
607 for (int layer_index = begin; layer_index < end; layer_index++) {
608 ADD_UNDO(tile_data, vformat("navigation_layer_%d/polygon", layer_index));
609 }
610 } else if (p_array_prefix == "custom_data_layer_") {
611 for (int layer_index = begin; layer_index < end; layer_index++) {
612 ADD_UNDO(tile_data, vformat("custom_data_%d", layer_index));
613 }
614 }
615 }
616 }
617 }
618 }
619 #undef ADD_UNDO
620
621 // Add do method to add/remove array element.
622 if (p_array_prefix == "occlusion_layer_") {
623 if (p_from_index < 0) {
624 undo_redo_man->add_do_method(ed_tile_set, "add_occlusion_layer", p_to_pos);
625 } else if (p_to_pos < 0) {
626 undo_redo_man->add_do_method(ed_tile_set, "remove_occlusion_layer", p_from_index);
627 } else {
628 undo_redo_man->add_do_method(ed_tile_set, "move_occlusion_layer", p_from_index, p_to_pos);
629 }
630 } else if (p_array_prefix == "physics_layer_") {
631 if (p_from_index < 0) {
632 undo_redo_man->add_do_method(ed_tile_set, "add_physics_layer", p_to_pos);
633 } else if (p_to_pos < 0) {
634 undo_redo_man->add_do_method(ed_tile_set, "remove_physics_layer", p_from_index);
635 } else {
636 undo_redo_man->add_do_method(ed_tile_set, "move_physics_layer", p_from_index, p_to_pos);
637 }
638 } else if (p_array_prefix == "terrain_set_") {
639 if (p_from_index < 0) {
640 undo_redo_man->add_do_method(ed_tile_set, "add_terrain_set", p_to_pos);
641 } else if (p_to_pos < 0) {
642 undo_redo_man->add_do_method(ed_tile_set, "remove_terrain_set", p_from_index);
643 } else {
644 undo_redo_man->add_do_method(ed_tile_set, "move_terrain_set", p_from_index, p_to_pos);
645 }
646 } else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int() && components[1] == "terrain_") {
647 int terrain_set = components[0].trim_prefix("terrain_set_").to_int();
648 if (p_from_index < 0) {
649 undo_redo_man->add_do_method(ed_tile_set, "add_terrain", terrain_set, p_to_pos);
650 } else if (p_to_pos < 0) {
651 undo_redo_man->add_do_method(ed_tile_set, "remove_terrain", terrain_set, p_from_index);
652 } else {
653 undo_redo_man->add_do_method(ed_tile_set, "move_terrain", terrain_set, p_from_index, p_to_pos);
654 }
655 } else if (p_array_prefix == "navigation_layer_") {
656 if (p_from_index < 0) {
657 undo_redo_man->add_do_method(ed_tile_set, "add_navigation_layer", p_to_pos);
658 } else if (p_to_pos < 0) {
659 undo_redo_man->add_do_method(ed_tile_set, "remove_navigation_layer", p_from_index);
660 } else {
661 undo_redo_man->add_do_method(ed_tile_set, "move_navigation_layer", p_from_index, p_to_pos);
662 }
663 } else if (p_array_prefix == "custom_data_layer_") {
664 if (p_from_index < 0) {
665 undo_redo_man->add_do_method(ed_tile_set, "add_custom_data_layer", p_to_pos);
666 } else if (p_to_pos < 0) {
667 undo_redo_man->add_do_method(ed_tile_set, "remove_custom_data_layer", p_from_index);
668 } else {
669 undo_redo_man->add_do_method(ed_tile_set, "move_custom_data_layer", p_from_index, p_to_pos);
670 }
671 }
672 }
673
674 void TileSetEditor::_undo_redo_inspector_callback(Object *p_undo_redo, Object *p_edited, const String &p_property, const Variant &p_new_value) {
675 EditorUndoRedoManager *undo_redo_man = Object::cast_to<EditorUndoRedoManager>(p_undo_redo);
676 ERR_FAIL_NULL(undo_redo_man);
677
678 #define ADD_UNDO(obj, property) undo_redo_man->add_undo_property(obj, property, obj->get(property));
679 TileSet *ed_tile_set = Object::cast_to<TileSet>(p_edited);
680 if (ed_tile_set) {
681 Vector<String> components = p_property.split("/", true, 3);
682 for (int i = 0; i < ed_tile_set->get_source_count(); i++) {
683 int source_id = ed_tile_set->get_source_id(i);
684
685 Ref<TileSetAtlasSource> tas = ed_tile_set->get_source(source_id);
686 if (tas.is_valid()) {
687 for (int j = 0; j < tas->get_tiles_count(); j++) {
688 Vector2i tile_id = tas->get_tile_id(j);
689 for (int k = 0; k < tas->get_alternative_tiles_count(tile_id); k++) {
690 int alternative_id = tas->get_alternative_tile_id(tile_id, k);
691 TileData *tile_data = tas->get_tile_data(tile_id, alternative_id);
692 ERR_FAIL_NULL(tile_data);
693
694 if (components.size() == 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int() && components[1] == "mode") {
695 ADD_UNDO(tile_data, "terrain_set");
696 ADD_UNDO(tile_data, "terrain");
697 for (int l = 0; l < TileSet::CELL_NEIGHBOR_MAX; l++) {
698 TileSet::CellNeighbor bit = TileSet::CellNeighbor(l);
699 if (tile_data->is_valid_terrain_peering_bit(bit)) {
700 ADD_UNDO(tile_data, "terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[l]));
701 }
702 }
703 } else if (components.size() == 2 && components[0].begins_with("custom_data_layer_") && components[0].trim_prefix("custom_data_layer_").is_valid_int() && components[1] == "type") {
704 int custom_data_layer = components[0].trim_prefix("custom_data_layer_").is_valid_int();
705 ADD_UNDO(tile_data, vformat("custom_data_%d", custom_data_layer));
706 }
707 }
708 }
709 }
710 }
711 }
712 #undef ADD_UNDO
713 }
714
715 void TileSetEditor::edit(Ref<TileSet> p_tile_set) {
716 bool new_read_only_state = false;
717 if (p_tile_set.is_valid()) {
718 new_read_only_state = EditorNode::get_singleton()->is_resource_read_only(p_tile_set);
719 }
720
721 if (p_tile_set == tile_set && new_read_only_state == read_only) {
722 return;
723 }
724
725 // Remove listener.
726 if (tile_set.is_valid()) {
727 tile_set->disconnect_changed(callable_mp(this, &TileSetEditor::_tile_set_changed));
728 }
729
730 // Change the edited object.
731 tile_set = p_tile_set;
732 sources_list->tile_set = p_tile_set;
733
734 // Read-only status is false by default
735 read_only = new_read_only_state;
736
737 // Add the listener again and check for read-only status.
738 if (tile_set.is_valid()) {
739 sources_add_button->set_disabled(read_only);
740 sources_advanced_menu_button->set_disabled(read_only);
741 source_sort_button->set_disabled(read_only);
742
743 tile_set->connect_changed(callable_mp(this, &TileSetEditor::_tile_set_changed));
744 if (first_edit) {
745 _set_source_sort(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "tile_source_sort", 0));
746 first_edit = false;
747 } else {
748 _update_sources_list();
749 }
750 _update_patterns_list();
751 }
752 }
753
754 void TileSetEditor::add_expanded_editor(Control *p_editor) {
755 expanded_editor = p_editor;
756 expanded_editor_parent = p_editor->get_parent()->get_instance_id();
757
758 // Find the scrollable control this node belongs to.
759 Node *check_parent = expanded_editor->get_parent();
760 Control *parent_container = nullptr;
761 while (check_parent) {
762 parent_container = Object::cast_to<EditorInspector>(check_parent);
763 if (parent_container) {
764 break;
765 }
766 parent_container = Object::cast_to<ScrollContainer>(check_parent);
767 if (parent_container) {
768 break;
769 }
770 check_parent = check_parent->get_parent();
771 }
772 ERR_FAIL_NULL(parent_container);
773
774 expanded_editor->set_meta("reparented", true);
775 expanded_editor->reparent(expanded_area);
776 expanded_area->show();
777 expanded_area->set_size(Vector2(parent_container->get_global_rect().get_end().x - expanded_area->get_global_position().x, expanded_area->get_size().y));
778
779 for (SplitContainer *split : disable_on_expand) {
780 split->set_dragger_visibility(SplitContainer::DRAGGER_HIDDEN);
781 }
782 }
783
784 void TileSetEditor::remove_expanded_editor() {
785 if (!expanded_editor) {
786 return;
787 }
788
789 Node *original_parent = ObjectDB::get_instance<Node>(expanded_editor_parent);
790 if (original_parent) {
791 expanded_editor->remove_meta("reparented");
792 expanded_editor->reparent(original_parent);
793 } else {
794 expanded_editor->queue_free();
795 }
796 expanded_editor = nullptr;
797 expanded_editor_parent = ObjectID();
798 expanded_area->hide();
799
800 for (SplitContainer *split : disable_on_expand) {
801 split->set_dragger_visibility(SplitContainer::DRAGGER_VISIBLE);
802 }
803 }
804
805 void TileSetEditor::register_split(SplitContainer *p_split) {
806 disable_on_expand.push_back(p_split);
807 }
808
809 TileSetEditor::TileSetEditor() {
810 singleton = this;
811 set_name(TTRC("TileSet"));
812 set_icon_name("TileSet");
813 set_dock_shortcut(ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_tile_set_bottom_panel", TTRC("Open TileSet Dock")));
814 set_default_slot(EditorDock::DOCK_SLOT_BOTTOM);
815 set_available_layouts(EditorDock::DOCK_LAYOUT_HORIZONTAL | EditorDock::DOCK_LAYOUT_FLOATING);
816 set_global(false);
817 set_transient(true);
818
819 set_process_internal(true);
820
821 VBoxContainer *main_vb = memnew(VBoxContainer);
822 add_child(main_vb);
823 main_vb->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
824
825 // TabBar.
826 tabs_bar = memnew(TabBar);
827 tabs_bar->set_theme_type_variation("TabBarInner");
828 tabs_bar->set_tab_alignment(TabBar::ALIGNMENT_CENTER);
829 tabs_bar->set_clip_tabs(false);
830 tabs_bar->add_tab(TTR("Tile Sources"));
831 tabs_bar->add_tab(TTR("Patterns"));
832 tabs_bar->connect("tab_changed", callable_mp(this, &TileSetEditor::_tab_changed));
833
834 tile_set_toolbar = memnew(HBoxContainer);
835 tile_set_toolbar->set_h_size_flags(SIZE_EXPAND_FILL);
836
837 PanelContainer *tabs_panel = memnew(PanelContainer);
838 tabs_panel->set_theme_type_variation("PanelContainerTabbarInner");
839 tabs_panel->add_child(tabs_bar);
840 tile_set_toolbar->add_child(tabs_panel);
841 main_vb->add_child(tile_set_toolbar);
842
843 //// Tiles ////
844 // Split container.
845 split_container = memnew(HSplitContainer);
846 split_container->set_name(TTR("Tiles"));
847 split_container->set_h_size_flags(SIZE_EXPAND_FILL);
848 split_container->set_v_size_flags(SIZE_EXPAND_FILL);
849 main_vb->add_child(split_container);
850
851 // Sources list.
852 VBoxContainer *split_container_left_side = memnew(VBoxContainer);
853 split_container_left_side->set_h_size_flags(SIZE_EXPAND_FILL);
854 split_container_left_side->set_v_size_flags(SIZE_EXPAND_FILL);
855 split_container_left_side->set_stretch_ratio(0.25);
856 split_container_left_side->set_custom_minimum_size(Size2(70, 0) * EDSCALE);
857 split_container->add_child(split_container_left_side);
858
859 source_sort_button = memnew(MenuButton);
860 source_sort_button->set_flat(false);
861 source_sort_button->set_theme_type_variation(SceneStringName(FlatButton));
862 source_sort_button->set_tooltip_text(TTR("Sort Sources"));
863
864 PopupMenu *p = source_sort_button->get_popup();
865 p->connect(SceneStringName(id_pressed), callable_mp(this, &TileSetEditor::_set_source_sort));
866 p->add_radio_check_item(TTR("Sort by ID (Ascending)"), TilesEditorUtils::SOURCE_SORT_ID);
867 p->add_radio_check_item(TTR("Sort by ID (Descending)"), TilesEditorUtils::SOURCE_SORT_ID_REVERSE);
868 p->add_radio_check_item(TTR("Sort by Name (Ascending)"), TilesEditorUtils::SOURCE_SORT_NAME);
869 p->add_radio_check_item(TTR("Sort by Name (Descending)"), TilesEditorUtils::SOURCE_SORT_NAME_REVERSE);
870 p->set_item_checked(TilesEditorUtils::SOURCE_SORT_ID, true);
871
872 sources_list = memnew(TileSetSourceItemList);
873 sources_list->connect(SceneStringName(item_selected), callable_mp(this, &TileSetEditor::_source_selected));
874 sources_list->connect(SceneStringName(item_selected), callable_mp(TilesEditorUtils::get_singleton(), &TilesEditorUtils::set_sources_lists_current));
875 sources_list->connect(SceneStringName(visibility_changed), callable_mp(TilesEditorUtils::get_singleton(), &TilesEditorUtils::synchronize_sources_list).bind(sources_list, source_sort_button));
876 sources_list->connect("sort_request", callable_mp(this, &TileSetEditor::_update_sources_list).bind(-1));
877 SET_DRAG_FORWARDING_CDU(sources_list, TileSetEditor);
878 split_container_left_side->add_child(sources_list);
879
880 HBoxContainer *sources_bottom_actions = memnew(HBoxContainer);
881 sources_bottom_actions->set_alignment(BoxContainer::ALIGNMENT_END);
882 split_container_left_side->add_child(sources_bottom_actions);
883
884 sources_delete_button = memnew(Button);
885 sources_delete_button->set_theme_type_variation(SceneStringName(FlatButton));
886 sources_delete_button->set_disabled(true);
887 sources_delete_button->connect(SceneStringName(pressed), callable_mp(this, &TileSetEditor::_source_delete_pressed));
888 sources_bottom_actions->add_child(sources_delete_button);
889
890 sources_add_button = memnew(MenuButton);
891 sources_add_button->set_flat(false);
892 sources_add_button->set_theme_type_variation(SceneStringName(FlatButton));
893 sources_add_button->get_popup()->add_item(TTR("Atlas"));
894 sources_add_button->get_popup()->set_item_tooltip(-1, TTR("A palette of tiles made from a texture."));
895 sources_add_button->get_popup()->add_item(TTR("Scenes Collection"));
896 sources_add_button->get_popup()->set_item_tooltip(-1, TTR("A collection of scenes that can be instantiated and placed as tiles."));
897 sources_add_button->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TileSetEditor::_source_add_id_pressed));
898 sources_bottom_actions->add_child(sources_add_button);
899
900 sources_advanced_menu_button = memnew(MenuButton);
901 sources_advanced_menu_button->set_flat(false);
902 sources_advanced_menu_button->set_theme_type_variation(SceneStringName(FlatButton));
903 sources_advanced_menu_button->get_popup()->add_item(TTR("Open Atlas Merging Tool"));
904 sources_advanced_menu_button->get_popup()->add_item(TTR("Manage Tile Proxies"));
905 sources_advanced_menu_button->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TileSetEditor::_sources_advanced_menu_id_pressed));
906 sources_advanced_menu_button->set_accessibility_name(TTRC("Advanced"));
907 sources_bottom_actions->add_child(sources_advanced_menu_button);
908 sources_bottom_actions->add_child(source_sort_button);
909
910 atlas_merging_dialog = memnew(AtlasMergingDialog);
911 add_child(atlas_merging_dialog);
912
913 tile_proxies_manager_dialog = memnew(TileProxiesManagerDialog);
914 add_child(tile_proxies_manager_dialog);
915
916 // Right side container.
917 VBoxContainer *split_container_right_side = memnew(VBoxContainer);
918 split_container_right_side->set_h_size_flags(SIZE_EXPAND_FILL);
919 split_container_right_side->set_v_size_flags(SIZE_EXPAND_FILL);
920 split_container->add_child(split_container_right_side);
921
922 // No source selected.
923 no_source_selected_label = memnew(Label);
924 no_source_selected_label->set_focus_mode(FOCUS_ACCESSIBILITY);
925 no_source_selected_label->set_text(TTR("No TileSet source selected. Select or create a TileSet source.\nYou can create a new source by using the Add button on the left or by dropping a tileset texture onto the source list."));
926 no_source_selected_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
927 no_source_selected_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
928 no_source_selected_label->set_h_size_flags(SIZE_EXPAND_FILL);
929 no_source_selected_label->set_v_size_flags(SIZE_EXPAND_FILL);
930 no_source_selected_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
931 no_source_selected_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
932 split_container_right_side->add_child(no_source_selected_label);
933
934 // Atlases editor.
935 tile_set_atlas_source_editor = memnew(TileSetAtlasSourceEditor);
936 tile_set_atlas_source_editor->set_h_size_flags(SIZE_EXPAND_FILL);
937 tile_set_atlas_source_editor->set_v_size_flags(SIZE_EXPAND_FILL);
938 tile_set_atlas_source_editor->connect("source_id_changed", callable_mp(this, &TileSetEditor::_update_sources_list));
939 split_container_right_side->add_child(tile_set_atlas_source_editor);
940 tile_set_atlas_source_editor->hide();
941
942 // Scenes collection editor.
943 tile_set_scenes_collection_source_editor = memnew(TileSetScenesCollectionSourceEditor);
944 tile_set_scenes_collection_source_editor->set_h_size_flags(SIZE_EXPAND_FILL);
945 tile_set_scenes_collection_source_editor->set_v_size_flags(SIZE_EXPAND_FILL);
946 tile_set_scenes_collection_source_editor->connect("source_id_changed", callable_mp(this, &TileSetEditor::_update_sources_list));
947 split_container_right_side->add_child(tile_set_scenes_collection_source_editor);
948 tile_set_scenes_collection_source_editor->hide();
949
950 patterns_mc = memnew(MarginContainer);
951 patterns_mc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
952 patterns_mc->set_theme_type_variation("NoBorderBottomPanel");
953 main_vb->add_child(patterns_mc);
954 patterns_mc->hide();
955
956 //// Patterns ////
957 int thumbnail_size = 64;
958 patterns_item_list = memnew(ItemList);
959 patterns_item_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
960 patterns_item_list->set_max_columns(0);
961 patterns_item_list->set_icon_mode(ItemList::ICON_MODE_TOP);
962 patterns_item_list->set_fixed_column_width(thumbnail_size * 3 / 2);
963 patterns_item_list->set_max_text_lines(2);
964 patterns_item_list->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
965 patterns_item_list->set_scroll_hint_mode(ItemList::SCROLL_HINT_MODE_BOTH);
966 patterns_item_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
967 patterns_item_list->connect(SceneStringName(gui_input), callable_mp(this, &TileSetEditor::_patterns_item_list_gui_input));
968 patterns_mc->add_child(patterns_item_list);
969
970 patterns_help_label = memnew(Label);
971 patterns_help_label->set_focus_mode(FOCUS_ACCESSIBILITY);
972 patterns_help_label->set_text(TTR("Add new patterns in the TileMap editing mode."));
973 patterns_help_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
974 patterns_help_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
975 patterns_help_label->set_anchors_and_offsets_preset(Control::PRESET_HCENTER_WIDE);
976 patterns_item_list->add_child(patterns_help_label);
977
978 // Expanded editor
979 expanded_area = memnew(PanelContainer);
980 add_child(expanded_area);
981 expanded_area->set_anchors_and_offsets_preset(PRESET_LEFT_WIDE);
982 expanded_area->hide();
983
984 // Registers UndoRedo inspector callback.
985 EditorNode::get_editor_data().add_move_array_element_function(SNAME("TileSet"), callable_mp(this, &TileSetEditor::_move_tile_set_array_element));
986 EditorNode::get_editor_data().add_undo_redo_inspector_hook_callback(callable_mp(this, &TileSetEditor::_undo_redo_inspector_callback));
987 }
988
989 void TileSourceInspectorPlugin::_show_id_edit_dialog(Object *p_for_source) {
990 if (!id_edit_dialog) {
991 id_edit_dialog = memnew(ConfirmationDialog);
992 TileSetEditor::get_singleton()->add_child(id_edit_dialog);
993
994 VBoxContainer *vbox = memnew(VBoxContainer);
995 id_edit_dialog->add_child(vbox);
996
997 Label *label = memnew(Label(TTR("Warning: Modifying a source ID will result in all TileMaps using that source to reference an invalid source instead. This may result in unexpected data loss. Change this ID carefully.")));
998 label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
999 // Workaround too tall popup window due to text autowrapping. See GH-83546.
1000 label->set_custom_minimum_size(Vector2i(400, 0));
1001 vbox->add_child(label);
1002
1003 id_input = memnew(SpinBox);
1004 vbox->add_child(id_input);
1005 id_input->set_max(INT_MAX);
1006
1007 id_edit_dialog->connect(SceneStringName(confirmed), callable_mp(this, &TileSourceInspectorPlugin::_confirm_change_id));
1008 }
1009 edited_source = p_for_source;
1010 id_input->set_value(p_for_source->get("id"));
1011 id_edit_dialog->popup_centered(Vector2i(400, 0) * EDSCALE);
1012 callable_mp((Control *)id_input->get_line_edit(), &Control::grab_focus).call_deferred(false);
1013 }
1014
1015 void TileSourceInspectorPlugin::_confirm_change_id() {
1016 edited_source->set("id", id_input->get_value());
1017 id_label->set_text(itos(edited_source->get("id"))); // Use get(), because the provided ID might've been invalid.
1018 }
1019
1020 bool TileSourceInspectorPlugin::can_handle(Object *p_object) {
1021 return p_object && (p_object->is_class("TileSetAtlasSourceProxyObject") || p_object->is_class("TileSetScenesCollectionProxyObject"));
1022 }
1023
1024 bool TileSourceInspectorPlugin::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide) {
1025 if (p_path == "id") {
1026 const Variant value = p_object->get("id");
1027 if (value.get_type() == Variant::NIL) { // May happen if the object is not yet initialized.
1028 return true;
1029 }
1030
1031 EditorProperty *ep = memnew(EditorProperty);
1032
1033 HBoxContainer *hbox = memnew(HBoxContainer);
1034 hbox->set_alignment(BoxContainer::ALIGNMENT_CENTER);
1035
1036 id_label = memnew(Label(itos(value)));
1037 id_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1038 hbox->add_child(id_label);
1039
1040 Button *button = memnew(Button(TTR("Edit")));
1041 button->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1042 hbox->add_child(button);
1043 button->connect(SceneStringName(pressed), callable_mp(this, &TileSourceInspectorPlugin::_show_id_edit_dialog).bind(p_object));
1044
1045 ep->add_child(hbox);
1046 add_property_editor(p_path, ep);
1047 return true;
1048 }
1049 return false;
1050 }