| 1 | /**************************************************************************/ |
| 2 | /* tiles_editor_plugin.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 "tiles_editor_plugin.h" |
| 32 | |
| 33 | #include "tile_set_editor.h" |
| 34 | |
| 35 | #include "core/os/mutex.h" |
| 36 | |
| 37 | #include "editor/docks/editor_dock_manager.h" |
| 38 | #include "editor/editor_interface.h" |
| 39 | #include "editor/editor_node.h" |
| 40 | #include "editor/editor_string_names.h" |
| 41 | #include "editor/inspector/multi_node_edit.h" |
| 42 | #include "editor/scene/canvas_item_editor_plugin.h" |
| 43 | #include "editor/settings/editor_command_palette.h" |
| 44 | #include "editor/settings/editor_settings.h" |
| 45 | #include "editor/themes/editor_scale.h" |
| 46 | #include "scene/2d/tile_map.h" |
| 47 | #include "scene/2d/tile_map_layer.h" |
| 48 | #include "scene/gui/button.h" |
| 49 | #include "scene/gui/control.h" |
| 50 | #include "scene/resources/2d/tile_set.h" |
| 51 | #include "scene/resources/image_texture.h" |
| 52 | |
| 53 | TilesEditorUtils *TilesEditorUtils::singleton = nullptr; |
| 54 | TileMapEditorPlugin *tile_map_plugin_singleton = nullptr; |
| 55 | TileSetEditorPlugin *tile_set_plugin_singleton = nullptr; |
| 56 | |
| 57 | void TilesEditorUtils::_preview_frame_started() { |
| 58 | RS::get_singleton()->request_frame_drawn_callback(callable_mp(this, &TilesEditorUtils::_pattern_preview_done)); |
| 59 | } |
| 60 | |
| 61 | void TilesEditorUtils::_pattern_preview_done() { |
| 62 | pattern_preview_done.post(); |
| 63 | } |
| 64 | |
| 65 | void TilesEditorUtils::_thread_func(void *ud) { |
| 66 | TilesEditorUtils *te = static_cast<TilesEditorUtils *>(ud); |
| 67 | set_current_thread_safe_for_nodes(true); |
| 68 | te->_thread(); |
| 69 | } |
| 70 | |
| 71 | void TilesEditorUtils::_thread() { |
| 72 | pattern_thread_exited.clear(); |
| 73 | while (!pattern_thread_exit.is_set()) { |
| 74 | pattern_preview_sem.wait(); |
| 75 | |
| 76 | pattern_preview_mutex.lock(); |
| 77 | if (pattern_preview_queue.is_empty()) { |
| 78 | pattern_preview_mutex.unlock(); |
| 79 | } else { |
| 80 | QueueItem item = pattern_preview_queue.front()->get(); |
| 81 | pattern_preview_queue.pop_front(); |
| 82 | pattern_preview_mutex.unlock(); |
| 83 | |
| 84 | int thumbnail_size = EDITOR_GET("filesystem/file_dialog/thumbnail_size"); |
| 85 | thumbnail_size *= EDSCALE; |
| 86 | Vector2 thumbnail_size2 = Vector2(thumbnail_size, thumbnail_size); |
| 87 | |
| 88 | if (item.pattern.is_valid() && !item.pattern->is_empty()) { |
| 89 | // Generate the pattern preview |
| 90 | SubViewport *viewport = memnew(SubViewport); |
| 91 | viewport->set_size(thumbnail_size2); |
| 92 | viewport->set_disable_input(true); |
| 93 | viewport->set_transparent_background(true); |
| 94 | viewport->set_update_mode(SubViewport::UPDATE_ONCE); |
| 95 | |
| 96 | TileMapLayer *tile_map_layer = memnew(TileMapLayer); |
| 97 | tile_map_layer->set_tile_set(item.tile_set); |
| 98 | tile_map_layer->set_pattern(Vector2(), item.pattern); |
| 99 | viewport->add_child(tile_map_layer); |
| 100 | |
| 101 | Rect2 encompassing_rect; |
| 102 | encompassing_rect.set_position(tile_map_layer->map_to_local(tile_map_layer->get_tile_map_layer_data().begin()->key)); |
| 103 | for (KeyValue<Vector2i, CellData> kv : tile_map_layer->get_tile_map_layer_data()) { |
| 104 | Vector2i cell = kv.key; |
| 105 | Vector2 world_pos = tile_map_layer->map_to_local(cell); |
| 106 | encompassing_rect.expand_to(world_pos); |
| 107 | |
| 108 | // Texture. |
| 109 | Ref<TileSetAtlasSource> atlas_source = item.tile_set->get_source(tile_map_layer->get_cell_source_id(cell)); |
| 110 | if (atlas_source.is_valid()) { |
| 111 | Vector2i coords = tile_map_layer->get_cell_atlas_coords(cell); |
| 112 | int alternative = tile_map_layer->get_cell_alternative_tile(cell); |
| 113 | |
| 114 | if (atlas_source->has_tile(coords) && atlas_source->has_alternative_tile(coords, alternative)) { |
| 115 | Vector2 center = world_pos - atlas_source->get_tile_data(coords, alternative)->get_texture_origin(); |
| 116 | encompassing_rect.expand_to(center - atlas_source->get_tile_texture_region(coords).size / 2); |
| 117 | encompassing_rect.expand_to(center + atlas_source->get_tile_texture_region(coords).size / 2); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | Vector2 scale = thumbnail_size2 / MAX(encompassing_rect.size.x, encompassing_rect.size.y); |
| 123 | tile_map_layer->set_scale(scale); |
| 124 | tile_map_layer->set_position(-(scale * encompassing_rect.get_center()) + thumbnail_size2 / 2); |
| 125 | |
| 126 | // Add the viewport at the last moment to avoid rendering too early. |
| 127 | callable_mp((Node *)EditorNode::get_singleton(), &Node::add_child).call_deferred(viewport, false, Node::INTERNAL_MODE_DISABLED); |
| 128 | |
| 129 | RS::get_singleton()->connect(SNAME("frame_pre_draw"), callable_mp(this, &TilesEditorUtils::_preview_frame_started), Object::CONNECT_ONE_SHOT); |
| 130 | |
| 131 | pattern_preview_done.wait(); |
| 132 | |
| 133 | Ref<Image> image = viewport->get_texture()->get_image(); |
| 134 | |
| 135 | // Find the index for the given pattern. TODO: optimize. |
| 136 | item.callback.call(item.pattern, ImageTexture::create_from_image(image)); |
| 137 | |
| 138 | viewport->queue_free(); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | pattern_thread_exited.set(); |
| 143 | } |
| 144 | |
| 145 | void TilesEditorUtils::queue_pattern_preview(Ref<TileSet> p_tile_set, Ref<TileMapPattern> p_pattern, Callable p_callback) { |
| 146 | ERR_FAIL_COND(p_tile_set.is_null()); |
| 147 | ERR_FAIL_COND(p_pattern.is_null()); |
| 148 | { |
| 149 | MutexLock lock(pattern_preview_mutex); |
| 150 | pattern_preview_queue.push_back({ p_tile_set, p_pattern, p_callback }); |
| 151 | } |
| 152 | pattern_preview_sem.post(); |
| 153 | } |
| 154 | |
| 155 | void TilesEditorUtils::set_sources_lists_current(int p_current) { |
| 156 | atlas_sources_lists_current = p_current; |
| 157 | } |
| 158 | |
| 159 | void TilesEditorUtils::synchronize_sources_list(Object *p_current_list, Object *p_current_sort_button) { |
| 160 | ItemList *item_list = Object::cast_to<ItemList>(p_current_list); |
| 161 | MenuButton *sorting_button = Object::cast_to<MenuButton>(p_current_sort_button); |
| 162 | ERR_FAIL_NULL(item_list); |
| 163 | ERR_FAIL_NULL(sorting_button); |
| 164 | |
| 165 | if (sorting_button->is_visible_in_tree()) { |
| 166 | for (int i = 0; i != SOURCE_SORT_MAX; i++) { |
| 167 | sorting_button->get_popup()->set_item_checked(i, (i == (int)source_sort)); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if (item_list->is_visible_in_tree()) { |
| 172 | // Make sure the selection is not overwritten after sorting. |
| 173 | int atlas_sources_lists_current_mem = atlas_sources_lists_current; |
| 174 | item_list->emit_signal(SNAME("sort_request")); |
| 175 | atlas_sources_lists_current = atlas_sources_lists_current_mem; |
| 176 | |
| 177 | if (atlas_sources_lists_current < 0 || atlas_sources_lists_current >= item_list->get_item_count()) { |
| 178 | item_list->deselect_all(); |
| 179 | } else { |
| 180 | item_list->set_current(atlas_sources_lists_current); |
| 181 | item_list->ensure_current_is_visible(); |
| 182 | item_list->emit_signal(SceneStringName(item_selected), atlas_sources_lists_current); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void TilesEditorUtils::set_atlas_view_transform(float p_zoom, Vector2 p_scroll) { |
| 188 | atlas_view_zoom = p_zoom; |
| 189 | atlas_view_scroll = p_scroll; |
| 190 | } |
| 191 | |
| 192 | void TilesEditorUtils::synchronize_atlas_view(Object *p_current) { |
| 193 | TileAtlasView *tile_atlas_view = Object::cast_to<TileAtlasView>(p_current); |
| 194 | ERR_FAIL_NULL(tile_atlas_view); |
| 195 | |
| 196 | if (tile_atlas_view->is_visible_in_tree()) { |
| 197 | tile_atlas_view->set_transform(atlas_view_zoom, atlas_view_scroll); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | void TilesEditorUtils::set_sorting_option(int p_option) { |
| 202 | source_sort = p_option; |
| 203 | } |
| 204 | |
| 205 | List<int> TilesEditorUtils::get_sorted_sources(const Ref<TileSet> p_tile_set) const { |
| 206 | SourceNameComparator::tile_set = p_tile_set; |
| 207 | List<int> source_ids; |
| 208 | |
| 209 | for (int i = 0; i < p_tile_set->get_source_count(); i++) { |
| 210 | source_ids.push_back(p_tile_set->get_source_id(i)); |
| 211 | } |
| 212 | |
| 213 | switch (source_sort) { |
| 214 | case SOURCE_SORT_ID_REVERSE: |
| 215 | // Already sorted. |
| 216 | source_ids.reverse(); |
| 217 | break; |
| 218 | case SOURCE_SORT_NAME: |
| 219 | source_ids.sort_custom<SourceNameComparator>(); |
| 220 | break; |
| 221 | case SOURCE_SORT_NAME_REVERSE: |
| 222 | source_ids.sort_custom<SourceNameComparator>(); |
| 223 | source_ids.reverse(); |
| 224 | break; |
| 225 | default: // SOURCE_SORT_ID |
| 226 | break; |
| 227 | } |
| 228 | |
| 229 | SourceNameComparator::tile_set.unref(); |
| 230 | return source_ids; |
| 231 | } |
| 232 | |
| 233 | Ref<TileSet> TilesEditorUtils::SourceNameComparator::tile_set; |
| 234 | |
| 235 | bool TilesEditorUtils::SourceNameComparator::operator()(const int &p_a, const int &p_b) const { |
| 236 | String name_a; |
| 237 | String name_b; |
| 238 | |
| 239 | { |
| 240 | TileSetSource *source = *tile_set->get_source(p_a); |
| 241 | |
| 242 | if (!source->get_name().is_empty()) { |
| 243 | name_a = source->get_name(); |
| 244 | } |
| 245 | |
| 246 | TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source); |
| 247 | if (atlas_source) { |
| 248 | Ref<Texture2D> texture = atlas_source->get_texture(); |
| 249 | if (name_a.is_empty() && texture.is_valid()) { |
| 250 | name_a = texture->get_path().get_file(); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | if (name_a.is_empty()) { |
| 255 | name_a = itos(p_a); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | { |
| 260 | TileSetSource *source = *tile_set->get_source(p_b); |
| 261 | |
| 262 | if (!source->get_name().is_empty()) { |
| 263 | name_b = source->get_name(); |
| 264 | } |
| 265 | |
| 266 | TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source); |
| 267 | if (atlas_source) { |
| 268 | Ref<Texture2D> texture = atlas_source->get_texture(); |
| 269 | if (name_b.is_empty() && texture.is_valid()) { |
| 270 | name_b = texture->get_path().get_file(); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | if (name_b.is_empty()) { |
| 275 | name_b = itos(p_b); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | return NaturalNoCaseComparator()(name_a, name_b); |
| 280 | } |
| 281 | |
| 282 | void TilesEditorUtils::display_tile_set_editor_panel() { |
| 283 | tile_set_plugin_singleton->make_visible(true); |
| 284 | } |
| 285 | |
| 286 | void TilesEditorUtils::draw_selection_rect(CanvasItem *p_ci, const Rect2 &p_rect, const Color &p_color) { |
| 287 | Ref<Texture2D> selection_texture = EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("TileSelection"), EditorStringName(EditorIcons)); |
| 288 | |
| 289 | real_t scale = p_ci->get_global_transform().get_scale().x * 0.5; |
| 290 | p_ci->draw_set_transform(p_rect.position, 0, Vector2(1, 1) / scale); |
| 291 | RS::get_singleton()->canvas_item_add_nine_patch( |
| 292 | p_ci->get_canvas_item(), Rect2(Vector2(), p_rect.size * scale), Rect2(), selection_texture->get_rid(), |
| 293 | Vector2(2, 2), Vector2(2, 2), RS::NINE_PATCH_STRETCH, RS::NINE_PATCH_STRETCH, false, p_color); |
| 294 | p_ci->draw_set_transform_matrix(Transform2D()); |
| 295 | } |
| 296 | |
| 297 | TilesEditorUtils::TilesEditorUtils() { |
| 298 | singleton = this; |
| 299 | // Pattern preview generation thread. |
| 300 | pattern_preview_thread.start(_thread_func, this); |
| 301 | |
| 302 | ED_SHORTCUT("tiles_editor/cut", TTRC("Cut"), KeyModifierMask::CMD_OR_CTRL | Key::X); |
| 303 | ED_SHORTCUT("tiles_editor/copy", TTRC("Copy"), KeyModifierMask::CMD_OR_CTRL | Key::C); |
| 304 | ED_SHORTCUT("tiles_editor/paste", TTRC("Paste"), KeyModifierMask::CMD_OR_CTRL | Key::V); |
| 305 | ED_SHORTCUT("tiles_editor/cancel", TTRC("Cancel"), Key::ESCAPE); |
| 306 | ED_SHORTCUT("tiles_editor/delete", TTRC("Delete"), Key::KEY_DELETE); |
| 307 | |
| 308 | ED_SHORTCUT("tiles_editor/paint_tool", TTRC("Paint Tool"), Key::D); |
| 309 | ED_SHORTCUT("tiles_editor/line_tool", TTRC("Line Tool"), Key::L); |
| 310 | ED_SHORTCUT("tiles_editor/rect_tool", TTRC("Rect Tool"), Key::R); |
| 311 | ED_SHORTCUT("tiles_editor/bucket_tool", TTRC("Bucket Tool"), Key::B); |
| 312 | ED_SHORTCUT("tiles_editor/eraser", TTRC("Eraser Tool"), Key::E); |
| 313 | ED_SHORTCUT("tiles_editor/picker", TTRC("Picker Tool"), Key::P); |
| 314 | } |
| 315 | |
| 316 | TilesEditorUtils::~TilesEditorUtils() { |
| 317 | if (pattern_preview_thread.is_started()) { |
| 318 | pattern_thread_exit.set(); |
| 319 | pattern_preview_sem.post(); |
| 320 | while (!pattern_thread_exited.is_set()) { |
| 321 | OS::get_singleton()->delay_usec(10000); |
| 322 | RenderingServer::get_singleton()->sync(); //sync pending stuff, as thread may be blocked on visual server |
| 323 | } |
| 324 | pattern_preview_thread.wait_to_finish(); |
| 325 | } |
| 326 | singleton = nullptr; |
| 327 | } |
| 328 | |
| 329 | String TileSetSourceItemList::get_tooltip(const Point2 &p_pos) const { |
| 330 | int idx = get_item_at_position(p_pos); |
| 331 | if (tile_set.is_null() || idx == -1) { |
| 332 | return ItemList::get_tooltip(p_pos); |
| 333 | } |
| 334 | idx = get_item_metadata(idx); |
| 335 | |
| 336 | Ref<TileSetAtlasSource> atlas = tile_set->get_source(idx); |
| 337 | if (atlas.is_valid() && atlas->get_texture().is_valid()) { |
| 338 | return vformat(TTR("Source ID: %d\nTexture path: %s"), idx, atlas->get_texture()->get_path()); |
| 339 | } |
| 340 | return vformat(TTR("Source ID: %d"), idx); |
| 341 | } |
| 342 | |
| 343 | TileSetSourceItemList::TileSetSourceItemList() { |
| 344 | set_fixed_icon_size(Size2(60, 60) * EDSCALE); |
| 345 | set_h_size_flags(SIZE_EXPAND_FILL); |
| 346 | set_v_size_flags(SIZE_EXPAND_FILL); |
| 347 | set_stretch_ratio(0.25); |
| 348 | set_custom_minimum_size(Size2(70, 0) * EDSCALE); |
| 349 | set_theme_type_variation("ItemListSecondary"); |
| 350 | set_texture_filter(TEXTURE_FILTER_NEAREST); |
| 351 | set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 352 | add_user_signal(MethodInfo("sort_request")); |
| 353 | } |
| 354 | |
| 355 | void TileMapEditorPlugin::_tile_map_layer_changed() { |
| 356 | if (tile_map_changed_needs_update) { |
| 357 | return; |
| 358 | } |
| 359 | tile_map_changed_needs_update = true; |
| 360 | callable_mp(this, &TileMapEditorPlugin::_update_tile_map).call_deferred(); |
| 361 | } |
| 362 | |
| 363 | void TileMapEditorPlugin::_tile_map_layer_removed() { |
| 364 | // Workaround for TileMap, making sure the editor stays open when you delete the currently edited layer. |
| 365 | TileMap *tile_map = ObjectDB::get_instance<TileMap>(tile_map_group_id); |
| 366 | if (tile_map) { |
| 367 | edit(tile_map); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | void TileMapEditorPlugin::_update_tile_map() { |
| 372 | TileMapLayer *edited_layer = ObjectDB::get_instance<TileMapLayer>(tile_map_layer_id); |
| 373 | if (edited_layer) { |
| 374 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 375 | if (tile_set.is_valid() && tile_set_id != tile_set->get_instance_id()) { |
| 376 | tile_set_plugin_singleton->edit(tile_set.ptr()); |
| 377 | tile_set_plugin_singleton->make_visible(true); |
| 378 | tile_set_id = tile_set->get_instance_id(); |
| 379 | } else if (tile_set.is_null()) { |
| 380 | tile_set_plugin_singleton->edit(nullptr); |
| 381 | tile_set_plugin_singleton->make_visible(false); |
| 382 | tile_set_id = ObjectID(); |
| 383 | } |
| 384 | } |
| 385 | tile_map_changed_needs_update = false; |
| 386 | } |
| 387 | |
| 388 | void TileMapEditorPlugin::_select_layer(const StringName &p_name) { |
| 389 | TileMapLayer *edited_layer = ObjectDB::get_instance<TileMapLayer>(tile_map_layer_id); |
| 390 | ERR_FAIL_NULL(edited_layer); |
| 391 | |
| 392 | Node *parent = edited_layer->get_parent(); |
| 393 | if (parent) { |
| 394 | TileMapLayer *new_layer = Object::cast_to<TileMapLayer>(parent->get_node_or_null(String(p_name))); |
| 395 | edit(new_layer); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | void TileMapEditorPlugin::_edit_tile_map_layer(TileMapLayer *p_tile_map_layer, bool p_show_layer_selector) { |
| 400 | ERR_FAIL_NULL(p_tile_map_layer); |
| 401 | |
| 402 | editor->edit(p_tile_map_layer); |
| 403 | editor->set_show_layer_selector(p_show_layer_selector); |
| 404 | |
| 405 | // Update the object IDs. |
| 406 | tile_map_layer_id = p_tile_map_layer->get_instance_id(); |
| 407 | p_tile_map_layer->connect(CoreStringName(changed), callable_mp(this, &TileMapEditorPlugin::_tile_map_layer_changed)); |
| 408 | p_tile_map_layer->connect(SceneStringName(tree_exited), callable_mp(this, &TileMapEditorPlugin::_tile_map_layer_removed)); |
| 409 | |
| 410 | // Update the edited tileset. |
| 411 | Ref<TileSet> tile_set = p_tile_map_layer->get_tile_set(); |
| 412 | if (tile_set.is_valid()) { |
| 413 | tile_set_plugin_singleton->edit(tile_set.ptr()); |
| 414 | tile_set_plugin_singleton->open_editor(); |
| 415 | tile_set_id = tile_set->get_instance_id(); |
| 416 | } else { |
| 417 | tile_set_plugin_singleton->edit(nullptr); |
| 418 | tile_set_plugin_singleton->make_visible(false); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | void TileMapEditorPlugin::_edit_tile_map(TileMap *p_tile_map) { |
| 423 | ERR_FAIL_NULL(p_tile_map); |
| 424 | |
| 425 | tile_map_group_id = p_tile_map->get_instance_id(); |
| 426 | if (!p_tile_map->is_connected(CoreStringName(changed), callable_mp(editor, &TileMapLayerEditor::set_show_layer_selector))) { |
| 427 | p_tile_map->connect(CoreStringName(changed), callable_mp(editor, &TileMapLayerEditor::set_show_layer_selector).bind(p_tile_map->get_layers_count())); |
| 428 | } |
| 429 | |
| 430 | if (p_tile_map->get_layers_count() > 0) { |
| 431 | TileMapLayer *selected_layer = Object::cast_to<TileMapLayer>(p_tile_map->get_child(0)); |
| 432 | _edit_tile_map_layer(selected_layer, true); |
| 433 | } else { |
| 434 | editor->edit(nullptr); |
| 435 | editor->set_show_layer_selector(false); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | void TileMapEditorPlugin::_notification(int p_notification) { |
| 440 | if (p_notification == NOTIFICATION_EXIT_TREE) { |
| 441 | get_tree()->queue_delete(TilesEditorUtils::get_singleton()); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | void TileMapEditorPlugin::edit(Object *p_object) { |
| 446 | TileMapLayer *edited_layer = ObjectDB::get_instance<TileMapLayer>(tile_map_layer_id); |
| 447 | if (edited_layer) { |
| 448 | edited_layer->disconnect(CoreStringName(changed), callable_mp(this, &TileMapEditorPlugin::_tile_map_layer_changed)); |
| 449 | edited_layer->disconnect(SceneStringName(tree_exited), callable_mp(this, &TileMapEditorPlugin::_tile_map_layer_removed)); |
| 450 | } |
| 451 | |
| 452 | TileMap *edited_map = ObjectDB::get_instance<TileMap>(tile_map_group_id); |
| 453 | if (edited_map) { |
| 454 | edited_map->disconnect(CoreStringName(changed), callable_mp(editor, &TileMapLayerEditor::set_show_layer_selector)); |
| 455 | } |
| 456 | |
| 457 | tile_map_group_id = ObjectID(); |
| 458 | tile_map_layer_id = ObjectID(); |
| 459 | tile_set_id = ObjectID(); |
| 460 | |
| 461 | TileMap *tile_map = Object::cast_to<TileMap>(p_object); |
| 462 | TileMapLayer *tile_map_layer = Object::cast_to<TileMapLayer>(p_object); |
| 463 | MultiNodeEdit *multi_node_edit = Object::cast_to<MultiNodeEdit>(p_object); |
| 464 | if (tile_map) { |
| 465 | _edit_tile_map(tile_map); |
| 466 | } else if (tile_map_layer) { |
| 467 | _edit_tile_map_layer(tile_map_layer, false); |
| 468 | } else if (multi_node_edit) { |
| 469 | editor->edit(multi_node_edit); |
| 470 | } else { |
| 471 | editor->edit(nullptr); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | bool TileMapEditorPlugin::handles(Object *p_object) const { |
| 476 | MultiNodeEdit *multi_node_edit = Object::cast_to<MultiNodeEdit>(p_object); |
| 477 | Node *edited_scene = EditorNode::get_singleton()->get_edited_scene(); |
| 478 | if (multi_node_edit && edited_scene) { |
| 479 | bool only_tile_map_layers = true; |
| 480 | for (int i = 0; i < multi_node_edit->get_node_count(); i++) { |
| 481 | if (!Object::cast_to<TileMapLayer>(edited_scene->get_node(multi_node_edit->get_node(i)))) { |
| 482 | only_tile_map_layers = false; |
| 483 | break; |
| 484 | } |
| 485 | } |
| 486 | return only_tile_map_layers; |
| 487 | } |
| 488 | return Object::cast_to<TileMapLayer>(p_object) != nullptr || Object::cast_to<TileMap>(p_object) != nullptr; |
| 489 | } |
| 490 | |
| 491 | void TileMapEditorPlugin::make_visible(bool p_visible) { |
| 492 | if (p_visible) { |
| 493 | editor->make_visible(); |
| 494 | } else { |
| 495 | editor->close(); |
| 496 | TileSetEditor::get_singleton()->close(); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | bool TileMapEditorPlugin::forward_canvas_gui_input(const Ref<InputEvent> &p_event) { |
| 501 | return editor->forward_canvas_gui_input(p_event); |
| 502 | } |
| 503 | |
| 504 | void TileMapEditorPlugin::forward_canvas_draw_over_viewport(Control *p_overlay) { |
| 505 | editor->forward_canvas_draw_over_viewport(p_overlay); |
| 506 | } |
| 507 | |
| 508 | bool TileMapEditorPlugin::is_editor_visible() const { |
| 509 | return editor->is_visible_in_tree(); |
| 510 | } |
| 511 | |
| 512 | TileMapEditorPlugin::TileMapEditorPlugin() { |
| 513 | if (!TilesEditorUtils::get_singleton()) { |
| 514 | memnew(TilesEditorUtils); |
| 515 | } |
| 516 | tile_map_plugin_singleton = this; |
| 517 | |
| 518 | editor = memnew(TileMapLayerEditor); |
| 519 | editor->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 520 | editor->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 521 | editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE); |
| 522 | editor->hide(); |
| 523 | |
| 524 | EditorDockManager::get_singleton()->add_dock(editor); |
| 525 | editor->close(); |
| 526 | } |
| 527 | |
| 528 | TileMapEditorPlugin::~TileMapEditorPlugin() { |
| 529 | tile_map_plugin_singleton = nullptr; |
| 530 | } |
| 531 | |
| 532 | void TileSetEditorPlugin::edit(Object *p_object) { |
| 533 | editor->edit(Ref<TileSet>(p_object)); |
| 534 | if (p_object) { |
| 535 | edited_tileset = p_object->get_instance_id(); |
| 536 | } else { |
| 537 | edited_tileset = ObjectID(); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | bool TileSetEditorPlugin::handles(Object *p_object) const { |
| 542 | return Object::cast_to<TileSet>(p_object) != nullptr; |
| 543 | } |
| 544 | |
| 545 | void TileSetEditorPlugin::make_visible(bool p_visible) { |
| 546 | if (p_visible) { |
| 547 | editor->make_visible(); |
| 548 | } else { |
| 549 | editor->close(); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | void TileSetEditorPlugin::open_editor() { |
| 554 | editor->open(); |
| 555 | } |
| 556 | |
| 557 | ObjectID TileSetEditorPlugin::get_edited_tileset() const { |
| 558 | return edited_tileset; |
| 559 | } |
| 560 | |
| 561 | TileSetEditorPlugin::TileSetEditorPlugin() { |
| 562 | if (!TilesEditorUtils::get_singleton()) { |
| 563 | memnew(TilesEditorUtils); |
| 564 | } |
| 565 | tile_set_plugin_singleton = this; |
| 566 | |
| 567 | editor = memnew(TileSetEditor); |
| 568 | editor->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 569 | editor->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 570 | editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE); |
| 571 | editor->hide(); |
| 572 | |
| 573 | EditorDockManager::get_singleton()->add_dock(editor); |
| 574 | editor->close(); |
| 575 | } |
| 576 | |
| 577 | TileSetEditorPlugin::~TileSetEditorPlugin() { |
| 578 | tile_set_plugin_singleton = nullptr; |
| 579 | } |