| 1 | /**************************************************************************/ |
| 2 | /* tile_map_layer_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_map_layer_editor.h" |
| 32 | |
| 33 | #include "tiles_editor_plugin.h" |
| 34 | |
| 35 | #include "editor/editor_node.h" |
| 36 | #include "editor/editor_undo_redo_manager.h" |
| 37 | #include "editor/inspector/editor_resource_preview.h" |
| 38 | #include "editor/inspector/multi_node_edit.h" |
| 39 | #include "editor/scene/canvas_item_editor_plugin.h" |
| 40 | #include "editor/settings/editor_command_palette.h" |
| 41 | #include "editor/settings/editor_settings.h" |
| 42 | #include "editor/themes/editor_scale.h" |
| 43 | #include "scene/2d/tile_map.h" |
| 44 | #include "scene/2d/tile_map_layer.h" |
| 45 | #include "scene/gui/grid_container.h" |
| 46 | #include "scene/gui/split_container.h" |
| 47 | |
| 48 | #include "core/input/input.h" |
| 49 | #include "core/math/geometry_2d.h" |
| 50 | #include "core/math/random_pcg.h" |
| 51 | #include "core/os/keyboard.h" |
| 52 | |
| 53 | void SwitchSeparator::set_vertical(bool p_vertical) { |
| 54 | h_separator->set_visible(p_vertical); |
| 55 | v_separator->set_visible(!p_vertical); |
| 56 | } |
| 57 | |
| 58 | SwitchSeparator::SwitchSeparator() { |
| 59 | h_separator = memnew(HSeparator); |
| 60 | h_separator->hide(); |
| 61 | add_child(h_separator); |
| 62 | |
| 63 | v_separator = memnew(VSeparator); |
| 64 | add_child(v_separator); |
| 65 | } |
| 66 | |
| 67 | TileMapLayer *TileMapLayerSubEditorPlugin::_get_edited_layer() const { |
| 68 | return ObjectDB::get_instance<TileMapLayer>(edited_tile_map_layer_id); |
| 69 | } |
| 70 | |
| 71 | void TileMapLayerSubEditorPlugin::_add_to_output_if_tile_changed(HashMap<Vector2i, TileMapCell> &p_output, const TileMapLayer *p_layer, Vector2i p_coords, const TileMapCell &p_cell) { |
| 72 | if (p_cell != p_layer->get_cell(p_coords)) { |
| 73 | p_output[p_coords] = p_cell; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void TileMapLayerSubEditorPlugin::draw_tile_coords_over_viewport(Control *p_overlay, const TileMapLayer *p_edited_layer, Ref<TileSet> p_tile_set, bool p_show_rectangle_size, const Vector2i &p_rectangle_origin) { |
| 78 | Point2 msgpos = Point2(20 * EDSCALE, p_overlay->get_size().y - 20 * EDSCALE); |
| 79 | String text = String(p_tile_set->local_to_map(p_edited_layer->get_local_mouse_position())); |
| 80 | |
| 81 | if (p_show_rectangle_size) { |
| 82 | Vector2i rect_size = p_tile_set->local_to_map(p_edited_layer->get_local_mouse_position()) - p_tile_set->local_to_map(p_rectangle_origin); |
| 83 | text += vformat(" %s (%dx%d)", TTR("Drawing Rect:"), Math::abs(rect_size.x) + 1, Math::abs(rect_size.y) + 1); |
| 84 | } |
| 85 | |
| 86 | Ref<Font> font = p_overlay->get_theme_font(SceneStringName(font), SNAME("Label")); |
| 87 | int font_size = p_overlay->get_theme_font_size(SceneStringName(font_size), SNAME("Label")); |
| 88 | |
| 89 | p_overlay->draw_string(font, msgpos + Point2(1, 1), text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(0, 0, 0, 0.8)); |
| 90 | p_overlay->draw_string(font, msgpos + Point2(-1, -1), text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(0, 0, 0, 0.8)); |
| 91 | p_overlay->draw_string(font, msgpos, text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(1, 1, 1, 1)); |
| 92 | } |
| 93 | |
| 94 | void TileMapLayerEditorTilesPlugin::tile_set_changed() { |
| 95 | _update_fix_selected_and_hovered(); |
| 96 | _update_tile_set_sources_list(); |
| 97 | _update_source_display(); |
| 98 | _update_patterns_list(); |
| 99 | } |
| 100 | |
| 101 | void TileMapLayerEditorTilesPlugin::_on_random_tile_checkbox_toggled(bool p_pressed) { |
| 102 | scatter_controls_container->set_visible(p_pressed); |
| 103 | } |
| 104 | |
| 105 | void TileMapLayerEditorTilesPlugin::_on_scattering_spinbox_changed(double p_value) { |
| 106 | scattering = p_value; |
| 107 | } |
| 108 | |
| 109 | void TileMapLayerEditorTilesPlugin::_update_toolbar() { |
| 110 | // Stop dragging if needed. |
| 111 | _stop_dragging(); |
| 112 | |
| 113 | // Show only the correct settings. |
| 114 | bool using_select = (tool_buttons_group->get_pressed_button() == select_tool_button); |
| 115 | tools_settings_vsep->set_visible(!using_select); |
| 116 | picker_button->set_visible(!using_select); |
| 117 | erase_button->set_visible(!using_select); |
| 118 | random_tile_toggle->set_visible(!using_select); |
| 119 | bucket_contiguous_checkbox->set_visible(!using_select && tool_buttons_group->get_pressed_button() == bucket_tool_button); |
| 120 | scatter_controls_container->set_visible(!using_select && random_tile_toggle->is_pressed()); |
| 121 | CanvasItemEditor::get_singleton()->set_current_tool(CanvasItemEditor::TOOL_SELECT); |
| 122 | } |
| 123 | |
| 124 | void TileMapLayerEditorTilesPlugin::_update_transform_buttons() { |
| 125 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 126 | if (!edited_layer) { |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 131 | if (tile_set.is_null() || selection_pattern.is_null()) { |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | if (tile_set->get_tile_shape() != TileSet::TILE_SHAPE_SQUARE && selection_pattern->get_size() != Vector2i(1, 1)) { |
| 136 | _set_transform_buttons_state({ transform_button_flip_h, transform_button_flip_v }, { transform_button_rotate_left, transform_button_rotate_right }, |
| 137 | TTR("Can't rotate patterns when using non-square tile grid.")); |
| 138 | } else { |
| 139 | _set_transform_buttons_state({ transform_button_rotate_left, transform_button_rotate_right, transform_button_flip_h, transform_button_flip_v }, {}, ""); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void TileMapLayerEditorTilesPlugin::_set_transform_buttons_state(const Vector<Button *> &p_enabled_buttons, const Vector<Button *> &p_disabled_buttons, const String &p_why_disabled) { |
| 144 | for (Button *button : p_enabled_buttons) { |
| 145 | button->set_disabled(false); |
| 146 | button->set_tooltip_text(""); |
| 147 | } |
| 148 | for (Button *button : p_disabled_buttons) { |
| 149 | button->set_disabled(true); |
| 150 | button->set_tooltip_text(p_why_disabled); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | Vector<TileMapLayerSubEditorPlugin::TabData> TileMapLayerEditorTilesPlugin::get_tabs() const { |
| 155 | Vector<TileMapLayerSubEditorPlugin::TabData> tabs; |
| 156 | Vector<Control *> toolbar_controls; |
| 157 | toolbar_controls.push_back(tilemap_tiles_tools_buttons); |
| 158 | toolbar_controls.push_back(tools_settings); |
| 159 | toolbar_controls.push_back(tools_settings_vsep); |
| 160 | tabs.push_back({ toolbar_controls, wide_toolbar, tiles_bottom_panel }); |
| 161 | tabs.push_back({ toolbar_controls, wide_toolbar, patterns_mc }); |
| 162 | return tabs; |
| 163 | } |
| 164 | |
| 165 | void TileMapLayerEditorTilesPlugin::_tab_changed() { |
| 166 | if (tiles_bottom_panel->is_visible_in_tree()) { |
| 167 | _update_selection_pattern_from_tileset_tiles_selection(); |
| 168 | } else if (patterns_mc->is_visible_in_tree()) { |
| 169 | _update_selection_pattern_from_tileset_pattern_selection(); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void TileMapLayerEditorTilesPlugin::_update_tile_set_sources_list() { |
| 174 | // Update the sources. |
| 175 | int old_current = sources_list->get_current(); |
| 176 | int old_source = -1; |
| 177 | if (old_current > -1) { |
| 178 | old_source = sources_list->get_item_metadata(old_current); |
| 179 | } else { |
| 180 | old_source = sources_list->get_meta("old_source", -1); |
| 181 | } |
| 182 | sources_list->set_meta("old_source", old_source); |
| 183 | sources_list->clear(); |
| 184 | sources_list->tile_set = Ref<TileSet>(); |
| 185 | |
| 186 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 187 | if (!edited_layer) { |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 192 | if (tile_set.is_null()) { |
| 193 | return; |
| 194 | } |
| 195 | sources_list->tile_set = tile_set; |
| 196 | |
| 197 | if (!tile_set->has_source(old_source)) { |
| 198 | old_source = -1; |
| 199 | } |
| 200 | |
| 201 | List<int> source_ids = TilesEditorUtils::get_singleton()->get_sorted_sources(tile_set); |
| 202 | for (const int &source_id : source_ids) { |
| 203 | TileSetSource *source = *tile_set->get_source(source_id); |
| 204 | |
| 205 | Ref<Texture2D> texture; |
| 206 | String item_text; |
| 207 | |
| 208 | // Common to all type of sources. |
| 209 | if (!source->get_name().is_empty()) { |
| 210 | item_text = source->get_name(); |
| 211 | } |
| 212 | |
| 213 | // Atlas source. |
| 214 | TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source); |
| 215 | if (atlas_source) { |
| 216 | texture = atlas_source->get_texture(); |
| 217 | if (item_text.is_empty()) { |
| 218 | if (texture.is_valid()) { |
| 219 | item_text = texture->get_path().get_file(); |
| 220 | } else { |
| 221 | item_text = vformat(TTR("No Texture Atlas Source (ID: %d)"), source_id); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // Scene collection source. |
| 227 | TileSetScenesCollectionSource *scene_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source); |
| 228 | if (scene_collection_source) { |
| 229 | texture = tiles_bottom_panel->get_editor_theme_icon(SNAME("PackedScene")); |
| 230 | if (item_text.is_empty()) { |
| 231 | if (scene_collection_source->get_scene_tiles_count() > 0) { |
| 232 | item_text = vformat(TTR("Scene Collection Source (ID: %d)"), source_id); |
| 233 | } else { |
| 234 | item_text = vformat(TTR("Empty Scene Collection Source (ID: %d)"), source_id); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // Use default if not valid. |
| 240 | if (item_text.is_empty()) { |
| 241 | item_text = vformat(TTR("Unknown Type Source (ID: %d)"), source_id); |
| 242 | } |
| 243 | if (texture.is_null()) { |
| 244 | texture = missing_atlas_texture_icon; |
| 245 | } |
| 246 | |
| 247 | sources_list->add_item(item_text, texture); |
| 248 | sources_list->set_item_metadata(-1, source_id); |
| 249 | } |
| 250 | |
| 251 | if (sources_list->get_item_count() > 0) { |
| 252 | if (old_source >= 0) { |
| 253 | for (int i = 0; i < sources_list->get_item_count(); i++) { |
| 254 | if ((int)sources_list->get_item_metadata(i) == old_source) { |
| 255 | sources_list->set_current(i); |
| 256 | sources_list->ensure_current_is_visible(); |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | } else { |
| 261 | sources_list->set_current(0); |
| 262 | } |
| 263 | sources_list->emit_signal(SceneStringName(item_selected), sources_list->get_current()); |
| 264 | } |
| 265 | |
| 266 | // Synchronize the lists. |
| 267 | TilesEditorUtils::get_singleton()->set_sources_lists_current(sources_list->get_current()); |
| 268 | } |
| 269 | |
| 270 | void TileMapLayerEditorTilesPlugin::_update_source_display() { |
| 271 | // Update the atlas display. |
| 272 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 273 | if (!edited_layer) { |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 278 | if (tile_set.is_null()) { |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | int source_index = sources_list->get_current(); |
| 283 | if (source_index >= 0 && source_index < sources_list->get_item_count()) { |
| 284 | atlas_sources_split_container->show(); |
| 285 | missing_source_label->hide(); |
| 286 | |
| 287 | int source_id = sources_list->get_item_metadata(source_index); |
| 288 | TileSetSource *source = *tile_set->get_source(source_id); |
| 289 | TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source); |
| 290 | TileSetScenesCollectionSource *scenes_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source); |
| 291 | |
| 292 | if (atlas_source) { |
| 293 | scene_tiles_list->hide(); |
| 294 | invalid_source_label->hide(); |
| 295 | tile_atlas_view->show(); |
| 296 | _update_atlas_view(); |
| 297 | } else if (scenes_collection_source) { |
| 298 | tile_atlas_view->hide(); |
| 299 | invalid_source_label->hide(); |
| 300 | scene_tiles_list->show(); |
| 301 | _update_scenes_collection_view(); |
| 302 | } else { |
| 303 | tile_atlas_view->hide(); |
| 304 | scene_tiles_list->hide(); |
| 305 | invalid_source_label->show(); |
| 306 | } |
| 307 | } else { |
| 308 | atlas_sources_split_container->hide(); |
| 309 | missing_source_label->show(); |
| 310 | |
| 311 | tile_atlas_view->hide(); |
| 312 | scene_tiles_list->hide(); |
| 313 | invalid_source_label->hide(); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | void TileMapLayerEditorTilesPlugin::_patterns_item_list_gui_input(const Ref<InputEvent> &p_event) { |
| 318 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 319 | if (!edited_layer) { |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 324 | if (tile_set.is_null()) { |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | if (ED_IS_SHORTCUT("tiles_editor/paste", p_event) && p_event->is_pressed() && !p_event->is_echo()) { |
| 329 | select_last_pattern = true; |
| 330 | int new_pattern_index = tile_set->get_patterns_count(); |
| 331 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 332 | undo_redo->create_action(TTR("Add TileSet pattern")); |
| 333 | undo_redo->add_do_method(*tile_set, "add_pattern", tile_map_clipboard, new_pattern_index); |
| 334 | undo_redo->add_undo_method(*tile_set, "remove_pattern", new_pattern_index); |
| 335 | undo_redo->commit_action(); |
| 336 | patterns_item_list->accept_event(); |
| 337 | } |
| 338 | |
| 339 | if (ED_IS_SHORTCUT("tiles_editor/delete", p_event) && p_event->is_pressed() && !p_event->is_echo()) { |
| 340 | Vector<int> selected = patterns_item_list->get_selected_items(); |
| 341 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 342 | undo_redo->create_action(TTR("Remove TileSet patterns")); |
| 343 | for (int i = 0; i < selected.size(); i++) { |
| 344 | int pattern_index = selected[i]; |
| 345 | undo_redo->add_do_method(*tile_set, "remove_pattern", pattern_index); |
| 346 | undo_redo->add_undo_method(*tile_set, "add_pattern", tile_set->get_pattern(pattern_index), pattern_index); |
| 347 | } |
| 348 | undo_redo->commit_action(); |
| 349 | patterns_item_list->accept_event(); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | void TileMapLayerEditorTilesPlugin::_pattern_preview_done(Ref<TileMapPattern> p_pattern, Ref<Texture2D> p_texture) { |
| 354 | // TODO optimize ? |
| 355 | for (int i = 0; i < patterns_item_list->get_item_count(); i++) { |
| 356 | if (patterns_item_list->get_item_metadata(i) == p_pattern) { |
| 357 | patterns_item_list->set_item_icon(i, p_texture); |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | void TileMapLayerEditorTilesPlugin::_update_patterns_list() { |
| 364 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 365 | if (!edited_layer) { |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 370 | if (tile_set.is_null()) { |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | // Recreate the items. |
| 375 | patterns_item_list->clear(); |
| 376 | for (int i = 0; i < tile_set->get_patterns_count(); i++) { |
| 377 | int id = patterns_item_list->add_item(""); |
| 378 | patterns_item_list->set_item_metadata(id, tile_set->get_pattern(i)); |
| 379 | patterns_item_list->set_item_tooltip(id, vformat(TTR("Index: %d"), i)); |
| 380 | TilesEditorUtils::get_singleton()->queue_pattern_preview(tile_set, tile_set->get_pattern(i), callable_mp(this, &TileMapLayerEditorTilesPlugin::_pattern_preview_done)); |
| 381 | } |
| 382 | |
| 383 | // Update the label visibility. |
| 384 | patterns_help_label->set_visible(patterns_item_list->get_item_count() == 0); |
| 385 | |
| 386 | // Added a new pattern, thus select the last one. |
| 387 | if (select_last_pattern) { |
| 388 | patterns_item_list->select(tile_set->get_patterns_count() - 1); |
| 389 | patterns_item_list->grab_focus(true); |
| 390 | _update_selection_pattern_from_tileset_pattern_selection(); |
| 391 | } |
| 392 | select_last_pattern = false; |
| 393 | } |
| 394 | |
| 395 | void TileMapLayerEditorTilesPlugin::_update_atlas_view() { |
| 396 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 397 | if (!edited_layer) { |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 402 | if (tile_set.is_null()) { |
| 403 | return; |
| 404 | } |
| 405 | |
| 406 | int source_id = sources_list->get_item_metadata(sources_list->get_current()); |
| 407 | TileSetSource *source = *tile_set->get_source(source_id); |
| 408 | TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source); |
| 409 | ERR_FAIL_NULL(atlas_source); |
| 410 | |
| 411 | tile_atlas_view->set_atlas_source(*tile_set, atlas_source, source_id); |
| 412 | TilesEditorUtils::get_singleton()->synchronize_atlas_view(tile_atlas_view); |
| 413 | tile_atlas_control->queue_redraw(); |
| 414 | } |
| 415 | |
| 416 | void TileMapLayerEditorTilesPlugin::_update_scenes_collection_view() { |
| 417 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 418 | if (!edited_layer) { |
| 419 | return; |
| 420 | } |
| 421 | |
| 422 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 423 | if (tile_set.is_null()) { |
| 424 | return; |
| 425 | } |
| 426 | |
| 427 | int source_id = sources_list->get_item_metadata(sources_list->get_current()); |
| 428 | TileSetSource *source = *tile_set->get_source(source_id); |
| 429 | TileSetScenesCollectionSource *scenes_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source); |
| 430 | ERR_FAIL_NULL(scenes_collection_source); |
| 431 | |
| 432 | // Clear the list. |
| 433 | scene_tiles_list->clear(); |
| 434 | |
| 435 | // Rebuild the list. |
| 436 | for (int i = 0; i < scenes_collection_source->get_scene_tiles_count(); i++) { |
| 437 | int scene_id = scenes_collection_source->get_scene_tile_id(i); |
| 438 | |
| 439 | Ref<PackedScene> scene = scenes_collection_source->get_scene_tile_scene(scene_id); |
| 440 | |
| 441 | int item_index = 0; |
| 442 | if (scene.is_valid()) { |
| 443 | 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)); |
| 444 | EditorResourcePreview::get_singleton()->queue_edited_resource_preview(scene, callable_mp(this, &TileMapLayerEditorTilesPlugin::_scene_thumbnail_done).bind(i)); |
| 445 | } else { |
| 446 | item_index = scene_tiles_list->add_item(TTR("Tile with Invalid Scene"), tiles_bottom_panel->get_editor_theme_icon(SNAME("PackedScene"))); |
| 447 | } |
| 448 | scene_tiles_list->set_item_metadata(item_index, scene_id); |
| 449 | |
| 450 | // Check if in selection. |
| 451 | if (tile_set_selection.has(TileMapCell(source_id, Vector2i(), scene_id))) { |
| 452 | scene_tiles_list->select(item_index, false); |
| 453 | } |
| 454 | } |
| 455 | if (scene_tiles_list->get_item_count() == 0) { |
| 456 | scene_tiles_list->add_item(TTR("The selected scene collection source has no scenes. Add scenes in the TileSet bottom tab.")); |
| 457 | scene_tiles_list->set_item_disabled(-1, true); |
| 458 | } |
| 459 | |
| 460 | // Icon size update. |
| 461 | int int_size = int(EDITOR_GET("filesystem/file_dialog/thumbnail_size")) * EDSCALE; |
| 462 | scene_tiles_list->set_fixed_icon_size(Vector2(int_size, int_size)); |
| 463 | } |
| 464 | |
| 465 | void TileMapLayerEditorTilesPlugin::_scene_thumbnail_done(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, int p_index) { |
| 466 | if (p_index >= 0 && p_index < scene_tiles_list->get_item_count()) { |
| 467 | scene_tiles_list->set_item_icon(p_index, p_preview); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | void TileMapLayerEditorTilesPlugin::_scenes_list_multi_selected(int p_index, bool p_selected) { |
| 472 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 473 | if (!edited_layer) { |
| 474 | return; |
| 475 | } |
| 476 | |
| 477 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 478 | if (tile_set.is_null()) { |
| 479 | return; |
| 480 | } |
| 481 | |
| 482 | // Add or remove the Tile form the selection. |
| 483 | int scene_id = scene_tiles_list->get_item_metadata(p_index); |
| 484 | int source_id = sources_list->get_item_metadata(sources_list->get_current()); |
| 485 | TileSetSource *source = *tile_set->get_source(source_id); |
| 486 | TileSetScenesCollectionSource *scenes_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source); |
| 487 | ERR_FAIL_NULL(scenes_collection_source); |
| 488 | |
| 489 | TileMapCell selected = TileMapCell(source_id, Vector2i(), scene_id); |
| 490 | |
| 491 | // Clear the selection if shift is not pressed. |
| 492 | if (!Input::get_singleton()->is_key_pressed(Key::SHIFT)) { |
| 493 | tile_set_selection.clear(); |
| 494 | } |
| 495 | |
| 496 | if (p_selected) { |
| 497 | tile_set_selection.insert(selected); |
| 498 | } else { |
| 499 | if (tile_set_selection.has(selected)) { |
| 500 | tile_set_selection.erase(selected); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | _update_selection_pattern_from_tileset_tiles_selection(); |
| 505 | } |
| 506 | |
| 507 | void TileMapLayerEditorTilesPlugin::_scenes_list_lmb_empty_clicked(const Vector2 &p_pos, MouseButton p_mouse_button_index) { |
| 508 | if (p_mouse_button_index != MouseButton::LEFT) { |
| 509 | return; |
| 510 | } |
| 511 | |
| 512 | scene_tiles_list->deselect_all(); |
| 513 | tile_set_selection.clear(); |
| 514 | tile_map_selection.clear(); |
| 515 | selection_pattern.instantiate(); |
| 516 | _update_selection_pattern_from_tileset_tiles_selection(); |
| 517 | } |
| 518 | |
| 519 | void TileMapLayerEditorTilesPlugin::_update_theme() { |
| 520 | source_sort_button->set_button_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("Sort"))); |
| 521 | select_tool_button->set_button_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("ToolSelect"))); |
| 522 | paint_tool_button->set_button_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("Edit"))); |
| 523 | line_tool_button->set_button_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("Line"))); |
| 524 | rect_tool_button->set_button_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("Rectangle"))); |
| 525 | bucket_tool_button->set_button_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("Bucket"))); |
| 526 | |
| 527 | picker_button->set_button_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("ColorPick"))); |
| 528 | erase_button->set_button_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("Eraser"))); |
| 529 | random_tile_toggle->set_button_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("RandomNumberGenerator"))); |
| 530 | |
| 531 | transform_button_rotate_left->set_button_icon(tiles_bottom_panel->get_editor_theme_icon("RotateLeft")); |
| 532 | transform_button_rotate_right->set_button_icon(tiles_bottom_panel->get_editor_theme_icon("RotateRight")); |
| 533 | transform_button_flip_h->set_button_icon(tiles_bottom_panel->get_editor_theme_icon("MirrorX")); |
| 534 | transform_button_flip_v->set_button_icon(tiles_bottom_panel->get_editor_theme_icon("MirrorY")); |
| 535 | |
| 536 | missing_atlas_texture_icon = tiles_bottom_panel->get_editor_theme_icon(SNAME("TileSet")); |
| 537 | _update_tile_set_sources_list(); |
| 538 | } |
| 539 | |
| 540 | bool TileMapLayerEditorTilesPlugin::forward_canvas_gui_input(const Ref<InputEvent> &p_event) { |
| 541 | if (!(tiles_bottom_panel->is_visible_in_tree() || patterns_mc->is_visible_in_tree())) { |
| 542 | // If the bottom editor is not visible, we ignore inputs. |
| 543 | return false; |
| 544 | } |
| 545 | |
| 546 | if (CanvasItemEditor::get_singleton()->get_current_tool() != CanvasItemEditor::TOOL_SELECT) { |
| 547 | _stop_dragging(); |
| 548 | return false; |
| 549 | } |
| 550 | |
| 551 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 552 | if (!edited_layer) { |
| 553 | return false; |
| 554 | } |
| 555 | |
| 556 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 557 | if (tile_set.is_null()) { |
| 558 | return false; |
| 559 | } |
| 560 | |
| 561 | // Shortcuts |
| 562 | if (ED_IS_SHORTCUT("tiles_editor/cut", p_event) || ED_IS_SHORTCUT("tiles_editor/copy", p_event)) { |
| 563 | // Fill in the clipboard. |
| 564 | if (!tile_map_selection.is_empty()) { |
| 565 | tile_map_clipboard.instantiate(); |
| 566 | TypedArray<Vector2i> coords_array; |
| 567 | for (const Vector2i &E : tile_map_selection) { |
| 568 | coords_array.push_back(E); |
| 569 | } |
| 570 | tile_map_clipboard = edited_layer->get_pattern(coords_array); |
| 571 | } |
| 572 | |
| 573 | if (ED_IS_SHORTCUT("tiles_editor/cut", p_event)) { |
| 574 | // Delete selected tiles. |
| 575 | if (!tile_map_selection.is_empty()) { |
| 576 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 577 | undo_redo->create_action(TTR("Delete tiles")); |
| 578 | for (const Vector2i &coords : tile_map_selection) { |
| 579 | undo_redo->add_do_method(edited_layer, "set_cell", coords, TileSet::INVALID_SOURCE, TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE); |
| 580 | undo_redo->add_undo_method(edited_layer, "set_cell", coords, edited_layer->get_cell_source_id(coords), edited_layer->get_cell_atlas_coords(coords), edited_layer->get_cell_alternative_tile(coords)); |
| 581 | } |
| 582 | undo_redo->add_undo_method(this, "_set_tile_map_selection", _get_tile_map_selection()); |
| 583 | tile_map_selection.clear(); |
| 584 | undo_redo->add_do_method(this, "_set_tile_map_selection", _get_tile_map_selection()); |
| 585 | undo_redo->commit_action(); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | return true; |
| 590 | } |
| 591 | if (ED_IS_SHORTCUT("tiles_editor/paste", p_event)) { |
| 592 | if (drag_type == DRAG_TYPE_NONE) { |
| 593 | drag_type = DRAG_TYPE_CLIPBOARD_PASTE; |
| 594 | } |
| 595 | CanvasItemEditor::get_singleton()->update_viewport(); |
| 596 | return true; |
| 597 | } |
| 598 | if (ED_IS_SHORTCUT("tiles_editor/cancel", p_event)) { |
| 599 | if (drag_type == DRAG_TYPE_CLIPBOARD_PASTE) { |
| 600 | drag_type = DRAG_TYPE_NONE; |
| 601 | CanvasItemEditor::get_singleton()->update_viewport(); |
| 602 | return true; |
| 603 | } |
| 604 | } |
| 605 | if (ED_IS_SHORTCUT("tiles_editor/delete", p_event)) { |
| 606 | // Delete selected tiles. |
| 607 | if (!tile_map_selection.is_empty()) { |
| 608 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 609 | undo_redo->create_action(TTR("Delete tiles")); |
| 610 | for (const Vector2i &coords : tile_map_selection) { |
| 611 | undo_redo->add_do_method(edited_layer, "set_cell", coords, TileSet::INVALID_SOURCE, TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE); |
| 612 | undo_redo->add_undo_method(edited_layer, "set_cell", coords, edited_layer->get_cell_source_id(coords), edited_layer->get_cell_atlas_coords(coords), edited_layer->get_cell_alternative_tile(coords)); |
| 613 | } |
| 614 | undo_redo->add_undo_method(this, "_set_tile_map_selection", _get_tile_map_selection()); |
| 615 | tile_map_selection.clear(); |
| 616 | undo_redo->add_do_method(this, "_set_tile_map_selection", _get_tile_map_selection()); |
| 617 | undo_redo->commit_action(); |
| 618 | } |
| 619 | return true; |
| 620 | } |
| 621 | |
| 622 | Ref<InputEventKey> k = p_event; |
| 623 | if (k.is_valid() && k->is_pressed() && !k->is_echo()) { |
| 624 | for (BaseButton *b : viewport_shortcut_buttons) { |
| 625 | if (b->is_disabled()) { |
| 626 | continue; |
| 627 | } |
| 628 | |
| 629 | if (b->get_shortcut().is_valid() && b->get_shortcut()->matches_event(p_event)) { |
| 630 | if (b->is_toggle_mode()) { |
| 631 | b->set_pressed(b->get_button_group().is_valid() || !b->is_pressed()); |
| 632 | } else { |
| 633 | // Can't press a button without toggle mode, so just emit the signal directly. |
| 634 | b->emit_signal(SceneStringName(pressed)); |
| 635 | } |
| 636 | return true; |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | Ref<InputEventMouseMotion> mm = p_event; |
| 642 | if (mm.is_valid()) { |
| 643 | has_mouse = true; |
| 644 | Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas(); |
| 645 | Vector2 mpos = xform.affine_inverse().xform(mm->get_position()); |
| 646 | |
| 647 | switch (drag_type) { |
| 648 | case DRAG_TYPE_PAINT: { |
| 649 | HashMap<Vector2i, TileMapCell> to_draw = _draw_line(drag_start_mouse_pos, drag_last_mouse_pos, mpos, drag_erasing); |
| 650 | for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) { |
| 651 | Vector2i coords = E.key; |
| 652 | if (!drag_modified.has(coords)) { |
| 653 | drag_modified.insert(coords, edited_layer->get_cell(coords)); |
| 654 | if (!drag_erasing && E.value.source_id == TileSet::INVALID_SOURCE) { |
| 655 | continue; |
| 656 | } |
| 657 | edited_layer->set_cell(coords, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile); |
| 658 | } |
| 659 | } |
| 660 | _fix_invalid_tiles_in_tile_map_selection(); |
| 661 | } break; |
| 662 | case DRAG_TYPE_BUCKET: { |
| 663 | Vector<Vector2i> line = TileMapLayerEditor::get_line(edited_layer, tile_set->local_to_map(drag_last_mouse_pos), tile_set->local_to_map(mpos)); |
| 664 | for (int i = 0; i < line.size(); i++) { |
| 665 | if (!drag_modified.has(line[i])) { |
| 666 | HashMap<Vector2i, TileMapCell> to_draw = _draw_bucket_fill(line[i], bucket_contiguous_checkbox->is_pressed(), drag_erasing); |
| 667 | for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) { |
| 668 | Vector2i coords = E.key; |
| 669 | if (!drag_modified.has(coords)) { |
| 670 | drag_modified.insert(coords, edited_layer->get_cell(coords)); |
| 671 | if (!drag_erasing && E.value.source_id == TileSet::INVALID_SOURCE) { |
| 672 | continue; |
| 673 | } |
| 674 | edited_layer->set_cell(coords, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile); |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | _fix_invalid_tiles_in_tile_map_selection(); |
| 680 | } break; |
| 681 | default: |
| 682 | break; |
| 683 | } |
| 684 | drag_last_mouse_pos = mpos; |
| 685 | CanvasItemEditor::get_singleton()->update_viewport(); |
| 686 | |
| 687 | return true; |
| 688 | } |
| 689 | |
| 690 | Ref<InputEventMouseButton> mb = p_event; |
| 691 | if (mb.is_valid()) { |
| 692 | has_mouse = true; |
| 693 | Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas(); |
| 694 | Vector2 mpos = xform.affine_inverse().xform(mb->get_position()); |
| 695 | |
| 696 | if (mb->get_button_index() == MouseButton::LEFT || mb->get_button_index() == MouseButton::RIGHT) { |
| 697 | if (mb->is_pressed()) { |
| 698 | // Pressed |
| 699 | if (erase_button->is_pressed() || mb->get_button_index() == MouseButton::RIGHT) { |
| 700 | drag_erasing = true; |
| 701 | } |
| 702 | |
| 703 | if (drag_type == DRAG_TYPE_CLIPBOARD_PASTE) { |
| 704 | // Cancel tile pasting on right-click |
| 705 | if (mb->get_button_index() == MouseButton::RIGHT) { |
| 706 | drag_type = DRAG_TYPE_NONE; |
| 707 | } |
| 708 | } else if (tool_buttons_group->get_pressed_button() == select_tool_button) { |
| 709 | drag_start_mouse_pos = mpos; |
| 710 | if (tile_map_selection.has(tile_set->local_to_map(drag_start_mouse_pos)) && !mb->is_shift_pressed() && !mb->is_command_or_control_pressed()) { |
| 711 | // Move the selection |
| 712 | _update_selection_pattern_from_tilemap_selection(); // Make sure the pattern is up to date before moving. |
| 713 | drag_type = DRAG_TYPE_MOVE; |
| 714 | drag_modified.clear(); |
| 715 | for (const Vector2i &E : tile_map_selection) { |
| 716 | Vector2i coords = E; |
| 717 | drag_modified.insert(coords, edited_layer->get_cell(coords)); |
| 718 | edited_layer->set_cell(coords, TileSet::INVALID_SOURCE, TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE); |
| 719 | } |
| 720 | } else { |
| 721 | // Select tiles |
| 722 | drag_type = DRAG_TYPE_SELECT; |
| 723 | } |
| 724 | } else { |
| 725 | // Check if we are picking a tile. |
| 726 | if (picker_button->is_pressed() || (Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL) && !Input::get_singleton()->is_key_pressed(Key::SHIFT))) { |
| 727 | drag_type = DRAG_TYPE_PICK; |
| 728 | drag_start_mouse_pos = mpos; |
| 729 | } else { |
| 730 | // Paint otherwise. |
| 731 | if (tool_buttons_group->get_pressed_button() == paint_tool_button && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL) && !Input::get_singleton()->is_key_pressed(Key::SHIFT)) { |
| 732 | drag_type = DRAG_TYPE_PAINT; |
| 733 | drag_start_mouse_pos = mpos; |
| 734 | drag_modified.clear(); |
| 735 | HashMap<Vector2i, TileMapCell> to_draw = _draw_line(drag_start_mouse_pos, mpos, mpos, drag_erasing); |
| 736 | for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) { |
| 737 | if (!drag_erasing && E.value.source_id == TileSet::INVALID_SOURCE) { |
| 738 | continue; |
| 739 | } |
| 740 | Vector2i coords = E.key; |
| 741 | if (!drag_modified.has(coords)) { |
| 742 | drag_modified.insert(coords, edited_layer->get_cell(coords)); |
| 743 | } |
| 744 | edited_layer->set_cell(coords, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile); |
| 745 | } |
| 746 | _fix_invalid_tiles_in_tile_map_selection(); |
| 747 | } else if (tool_buttons_group->get_pressed_button() == line_tool_button || (tool_buttons_group->get_pressed_button() == paint_tool_button && Input::get_singleton()->is_key_pressed(Key::SHIFT) && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL))) { |
| 748 | drag_type = DRAG_TYPE_LINE; |
| 749 | drag_start_mouse_pos = mpos; |
| 750 | drag_modified.clear(); |
| 751 | } else if (tool_buttons_group->get_pressed_button() == rect_tool_button || (tool_buttons_group->get_pressed_button() == paint_tool_button && Input::get_singleton()->is_key_pressed(Key::SHIFT) && Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL))) { |
| 752 | drag_type = DRAG_TYPE_RECT; |
| 753 | drag_start_mouse_pos = mpos; |
| 754 | drag_modified.clear(); |
| 755 | } else if (tool_buttons_group->get_pressed_button() == bucket_tool_button) { |
| 756 | drag_type = DRAG_TYPE_BUCKET; |
| 757 | drag_start_mouse_pos = mpos; |
| 758 | drag_modified.clear(); |
| 759 | Vector<Vector2i> line = TileMapLayerEditor::get_line(edited_layer, tile_set->local_to_map(drag_last_mouse_pos), tile_set->local_to_map(mpos)); |
| 760 | for (int i = 0; i < line.size(); i++) { |
| 761 | if (!drag_modified.has(line[i])) { |
| 762 | HashMap<Vector2i, TileMapCell> to_draw = _draw_bucket_fill(line[i], bucket_contiguous_checkbox->is_pressed(), drag_erasing); |
| 763 | for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) { |
| 764 | Vector2i coords = E.key; |
| 765 | if (!drag_modified.has(coords)) { |
| 766 | drag_modified.insert(coords, edited_layer->get_cell(coords)); |
| 767 | if (!drag_erasing && E.value.source_id == TileSet::INVALID_SOURCE) { |
| 768 | continue; |
| 769 | } |
| 770 | edited_layer->set_cell(coords, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile); |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | _fix_invalid_tiles_in_tile_map_selection(); |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | } else { |
| 781 | // Released. |
| 782 | if (drag_type == DRAG_TYPE_NONE) { |
| 783 | drag_erasing = false; |
| 784 | return false; |
| 785 | } else { |
| 786 | _stop_dragging(); |
| 787 | } |
| 788 | drag_erasing = false; |
| 789 | } |
| 790 | |
| 791 | CanvasItemEditor::get_singleton()->update_viewport(); |
| 792 | |
| 793 | return true; |
| 794 | } |
| 795 | drag_last_mouse_pos = mpos; |
| 796 | } |
| 797 | |
| 798 | return false; |
| 799 | } |
| 800 | |
| 801 | void TileMapLayerEditorTilesPlugin::forward_canvas_draw_over_viewport(Control *p_overlay) { |
| 802 | const TileMapLayer *edited_layer = _get_edited_layer(); |
| 803 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 804 | |
| 805 | Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas(); |
| 806 | Vector2 mpos = edited_layer->get_local_mouse_position(); |
| 807 | Vector2i tile_shape_size = tile_set->get_tile_size(); |
| 808 | bool drawing_rect = false; |
| 809 | |
| 810 | // Draw the selection. |
| 811 | if ((tiles_bottom_panel->is_visible_in_tree() || patterns_mc->is_visible_in_tree()) && tool_buttons_group->get_pressed_button() == select_tool_button) { |
| 812 | // In select mode, we only draw the current selection if we are modifying it (pressing control or shift). |
| 813 | if (drag_type == DRAG_TYPE_MOVE || (drag_type == DRAG_TYPE_SELECT && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL) && !Input::get_singleton()->is_key_pressed(Key::SHIFT))) { |
| 814 | // Do nothing. |
| 815 | } else { |
| 816 | Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color"); |
| 817 | Color selection_color = Color::from_hsv(Math::fposmod(grid_color.get_h() + 0.5, 1.0), grid_color.get_s(), grid_color.get_v(), 1.0); |
| 818 | tile_set->draw_cells_outline(p_overlay, tile_map_selection, selection_color, xform); |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | // Handle the preview of the tiles to be placed. |
| 823 | if ((tiles_bottom_panel->is_visible_in_tree() || patterns_mc->is_visible_in_tree()) && CanvasItemEditor::get_singleton()->get_current_tool() == CanvasItemEditor::TOOL_SELECT && has_mouse) { // Only if the tilemap editor is opened and the viewport is hovered. |
| 824 | HashMap<Vector2i, TileMapCell> preview; |
| 825 | Rect2i drawn_grid_rect; |
| 826 | |
| 827 | if (drag_type == DRAG_TYPE_PICK) { |
| 828 | // Draw the area being picked. |
| 829 | Rect2i rect = Rect2i(tile_set->local_to_map(drag_start_mouse_pos), tile_set->local_to_map(mpos) - tile_set->local_to_map(drag_start_mouse_pos)).abs(); |
| 830 | rect.size += Vector2i(1, 1); |
| 831 | for (int x = rect.position.x; x < rect.get_end().x; x++) { |
| 832 | for (int y = rect.position.y; y < rect.get_end().y; y++) { |
| 833 | Vector2i coords = Vector2i(x, y); |
| 834 | if (edited_layer->get_cell_source_id(coords) != TileSet::INVALID_SOURCE) { |
| 835 | Transform2D tile_xform(0, tile_shape_size, 0, tile_set->map_to_local(coords)); |
| 836 | tile_set->draw_tile_shape(p_overlay, xform * tile_xform, Color(1.0, 1.0, 1.0), false); |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | } else if (drag_type == DRAG_TYPE_SELECT) { |
| 841 | // Draw the area being selected. |
| 842 | Rect2i rect = Rect2i(tile_set->local_to_map(drag_start_mouse_pos), tile_set->local_to_map(mpos) - tile_set->local_to_map(drag_start_mouse_pos)).abs(); |
| 843 | rect.size += Vector2i(1, 1); |
| 844 | RBSet<Vector2i> to_draw; |
| 845 | for (int x = rect.position.x; x < rect.get_end().x; x++) { |
| 846 | for (int y = rect.position.y; y < rect.get_end().y; y++) { |
| 847 | Vector2i coords = Vector2i(x, y); |
| 848 | if (edited_layer->get_cell_source_id(coords) != TileSet::INVALID_SOURCE) { |
| 849 | to_draw.insert(coords); |
| 850 | } |
| 851 | Transform2D tile_xform(0, tile_shape_size, 0, tile_set->map_to_local(coords)); |
| 852 | tile_set->draw_tile_shape(p_overlay, xform * tile_xform, Color(1.0, 1.0, 1.0, 0.2), true); |
| 853 | } |
| 854 | } |
| 855 | tile_set->draw_cells_outline(p_overlay, to_draw, Color(1.0, 1.0, 1.0), xform); |
| 856 | } else if (drag_type == DRAG_TYPE_MOVE) { |
| 857 | if (!(patterns_item_list->is_visible_in_tree() && patterns_item_list->has_point(patterns_item_list->get_local_mouse_position()))) { |
| 858 | // Preview when moving. |
| 859 | Vector2i top_left; |
| 860 | if (!tile_map_selection.is_empty()) { |
| 861 | top_left = tile_map_selection.front()->get(); |
| 862 | } |
| 863 | for (const Vector2i &E : tile_map_selection) { |
| 864 | top_left = top_left.min(E); |
| 865 | } |
| 866 | Vector2i offset = drag_start_mouse_pos - tile_set->map_to_local(top_left); |
| 867 | offset = tile_set->local_to_map(mpos - offset) - tile_set->local_to_map(drag_start_mouse_pos - offset); |
| 868 | |
| 869 | TypedArray<Vector2i> selection_used_cells = selection_pattern->get_used_cells(); |
| 870 | for (int i = 0; i < selection_used_cells.size(); i++) { |
| 871 | Vector2i coords = tile_set->map_pattern(offset + top_left, selection_used_cells[i], selection_pattern); |
| 872 | preview[coords] = TileMapCell(selection_pattern->get_cell_source_id(selection_used_cells[i]), selection_pattern->get_cell_atlas_coords(selection_used_cells[i]), selection_pattern->get_cell_alternative_tile(selection_used_cells[i])); |
| 873 | } |
| 874 | } |
| 875 | } else if (drag_type == DRAG_TYPE_CLIPBOARD_PASTE) { |
| 876 | // Preview when pasting. |
| 877 | Vector2 mouse_offset = (Vector2(tile_map_clipboard->get_size()) / 2.0 - Vector2(0.5, 0.5)) * tile_set->get_tile_size(); |
| 878 | TypedArray<Vector2i> clipboard_used_cells = tile_map_clipboard->get_used_cells(); |
| 879 | for (int i = 0; i < clipboard_used_cells.size(); i++) { |
| 880 | Vector2i coords = tile_set->map_pattern(tile_set->local_to_map(mpos - mouse_offset), clipboard_used_cells[i], tile_map_clipboard); |
| 881 | preview[coords] = TileMapCell(tile_map_clipboard->get_cell_source_id(clipboard_used_cells[i]), tile_map_clipboard->get_cell_atlas_coords(clipboard_used_cells[i]), tile_map_clipboard->get_cell_alternative_tile(clipboard_used_cells[i])); |
| 882 | } |
| 883 | } else if (!picker_button->is_pressed() && !(drag_type == DRAG_TYPE_NONE && Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL) && !Input::get_singleton()->is_key_pressed(Key::SHIFT))) { |
| 884 | bool expand_grid = false; |
| 885 | if (tool_buttons_group->get_pressed_button() == paint_tool_button && drag_type == DRAG_TYPE_NONE) { |
| 886 | // Preview for a single pattern. |
| 887 | preview = _draw_line(mpos, mpos, mpos, erase_button->is_pressed()); |
| 888 | expand_grid = true; |
| 889 | } else if (tool_buttons_group->get_pressed_button() == line_tool_button || drag_type == DRAG_TYPE_LINE) { |
| 890 | if (drag_type == DRAG_TYPE_NONE) { |
| 891 | // Preview for a single pattern. |
| 892 | preview = _draw_line(mpos, mpos, mpos, erase_button->is_pressed()); |
| 893 | expand_grid = true; |
| 894 | } else if (drag_type == DRAG_TYPE_LINE) { |
| 895 | // Preview for a line pattern. |
| 896 | preview = _draw_line(drag_start_mouse_pos, drag_start_mouse_pos, mpos, drag_erasing); |
| 897 | expand_grid = true; |
| 898 | } |
| 899 | } else if (drag_type == DRAG_TYPE_RECT) { |
| 900 | // Preview for a rect pattern. |
| 901 | preview = _draw_rect(tile_set->local_to_map(drag_start_mouse_pos), tile_set->local_to_map(mpos), drag_erasing); |
| 902 | drawing_rect = !preview.is_empty(); |
| 903 | expand_grid = true; |
| 904 | } else if (tool_buttons_group->get_pressed_button() == bucket_tool_button && drag_type == DRAG_TYPE_NONE) { |
| 905 | // Preview for a fill pattern. |
| 906 | preview = _draw_bucket_fill(tile_set->local_to_map(mpos), bucket_contiguous_checkbox->is_pressed(), erase_button->is_pressed()); |
| 907 | } |
| 908 | |
| 909 | // Expand the grid if needed |
| 910 | if (expand_grid && !preview.is_empty()) { |
| 911 | drawn_grid_rect = Rect2i(preview.begin()->key, Vector2i(0, 0)); |
| 912 | for (const KeyValue<Vector2i, TileMapCell> &E : preview) { |
| 913 | drawn_grid_rect.expand_to(E.key); |
| 914 | } |
| 915 | drawn_grid_rect.size += Vector2i(1, 1); |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | if (!preview.is_empty()) { |
| 920 | const int fading = 5; |
| 921 | |
| 922 | // Draw the lines of the grid behind the preview. |
| 923 | bool display_grid = EDITOR_GET("editors/tiles_editor/display_grid"); |
| 924 | if (display_grid) { |
| 925 | Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color"); |
| 926 | if (drawn_grid_rect.size.x > 0 && drawn_grid_rect.size.y > 0) { |
| 927 | drawn_grid_rect = drawn_grid_rect.grow(fading); |
| 928 | for (int x = drawn_grid_rect.position.x; x < (drawn_grid_rect.position.x + drawn_grid_rect.size.x); x++) { |
| 929 | for (int y = drawn_grid_rect.position.y; y < (drawn_grid_rect.position.y + drawn_grid_rect.size.y); y++) { |
| 930 | Vector2i pos_in_rect = Vector2i(x, y) - drawn_grid_rect.position; |
| 931 | |
| 932 | // Fade out the border of the grid. |
| 933 | float left_opacity = CLAMP(Math::inverse_lerp(0.0f, (float)fading, (float)pos_in_rect.x), 0.0f, 1.0f); |
| 934 | float right_opacity = CLAMP(Math::inverse_lerp((float)drawn_grid_rect.size.x, (float)(drawn_grid_rect.size.x - fading), (float)(pos_in_rect.x + 1)), 0.0f, 1.0f); |
| 935 | float top_opacity = CLAMP(Math::inverse_lerp(0.0f, (float)fading, (float)pos_in_rect.y), 0.0f, 1.0f); |
| 936 | float bottom_opacity = CLAMP(Math::inverse_lerp((float)drawn_grid_rect.size.y, (float)(drawn_grid_rect.size.y - fading), (float)(pos_in_rect.y + 1)), 0.0f, 1.0f); |
| 937 | float opacity = CLAMP(MIN(left_opacity, MIN(right_opacity, MIN(top_opacity, bottom_opacity))) + 0.1, 0.0f, 1.0f); |
| 938 | |
| 939 | Transform2D tile_xform; |
| 940 | tile_xform.set_origin(tile_set->map_to_local(Vector2(x, y))); |
| 941 | tile_xform.set_scale(tile_shape_size); |
| 942 | Color color = grid_color; |
| 943 | color.a = color.a * opacity; |
| 944 | tile_set->draw_tile_shape(p_overlay, xform * tile_xform, color, false); |
| 945 | } |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | // Draw the preview. |
| 951 | for (const KeyValue<Vector2i, TileMapCell> &E : preview) { |
| 952 | Transform2D tile_xform; |
| 953 | tile_xform.set_origin(tile_set->map_to_local(E.key)); |
| 954 | tile_xform.set_scale(tile_set->get_tile_size()); |
| 955 | if (!(drag_erasing || erase_button->is_pressed()) && random_tile_toggle->is_pressed()) { |
| 956 | tile_set->draw_tile_shape(p_overlay, xform * tile_xform, Color(1.0, 1.0, 1.0, 0.5), true); |
| 957 | } else { |
| 958 | if (tile_set->has_source(E.value.source_id)) { |
| 959 | TileSetSource *source = *tile_set->get_source(E.value.source_id); |
| 960 | TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source); |
| 961 | if (atlas_source) { |
| 962 | // Get tile data. |
| 963 | TileData *tile_data = atlas_source->get_tile_data(E.value.get_atlas_coords(), E.value.alternative_tile); |
| 964 | if (!tile_data) { |
| 965 | continue; |
| 966 | } |
| 967 | |
| 968 | Rect2i source_rect = atlas_source->get_tile_texture_region(E.value.get_atlas_coords()); |
| 969 | |
| 970 | // Compute the destination rectangle in the CanvasItem. |
| 971 | Rect2 dest_rect; |
| 972 | bool transpose; |
| 973 | TileMapLayer::compute_transformed_tile_dest_rect(dest_rect, transpose, tile_set->map_to_local(E.key), source_rect.size, tile_data, E.value.alternative_tile); |
| 974 | |
| 975 | // Get the tile modulation. |
| 976 | Color modulate = tile_data->get_modulate() * edited_layer->get_modulate_in_tree() * edited_layer->get_self_modulate(); |
| 977 | |
| 978 | // Draw the tile. |
| 979 | p_overlay->draw_set_transform_matrix(xform); |
| 980 | p_overlay->draw_texture_rect_region(atlas_source->get_texture(), dest_rect, source_rect, modulate * Color(1.0, 1.0, 1.0, 0.5), transpose, tile_set->is_uv_clipping()); |
| 981 | p_overlay->draw_set_transform_matrix(Transform2D()); |
| 982 | } else { |
| 983 | tile_set->draw_tile_shape(p_overlay, xform * tile_xform, Color(1.0, 1.0, 1.0, 0.5), true); |
| 984 | } |
| 985 | } else { |
| 986 | tile_set->draw_tile_shape(p_overlay, xform * tile_xform, Color(0.0, 0.0, 0.0, 0.5), true); |
| 987 | } |
| 988 | } |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | draw_tile_coords_over_viewport(p_overlay, edited_layer, tile_set, drawing_rect, drag_start_mouse_pos); |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | void TileMapLayerEditorTilesPlugin::_mouse_exited_viewport() { |
| 997 | has_mouse = false; |
| 998 | CanvasItemEditor::get_singleton()->update_viewport(); |
| 999 | } |
| 1000 | |
| 1001 | TileMapCell TileMapLayerEditorTilesPlugin::_pick_random_tile(Ref<TileMapPattern> p_pattern) { |
| 1002 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 1003 | if (!edited_layer) { |
| 1004 | return TileMapCell(); |
| 1005 | } |
| 1006 | |
| 1007 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 1008 | if (tile_set.is_null()) { |
| 1009 | return TileMapCell(); |
| 1010 | } |
| 1011 | |
| 1012 | TypedArray<Vector2i> used_cells = p_pattern->get_used_cells(); |
| 1013 | double sum = 0.0; |
| 1014 | for (int i = 0; i < used_cells.size(); i++) { |
| 1015 | int source_id = p_pattern->get_cell_source_id(used_cells[i]); |
| 1016 | Vector2i atlas_coords = p_pattern->get_cell_atlas_coords(used_cells[i]); |
| 1017 | int alternative_tile = p_pattern->get_cell_alternative_tile(used_cells[i]); |
| 1018 | |
| 1019 | TileSetSource *source = *tile_set->get_source(source_id); |
| 1020 | TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source); |
| 1021 | if (atlas_source) { |
| 1022 | TileData *tile_data = atlas_source->get_tile_data(atlas_coords, alternative_tile); |
| 1023 | ERR_FAIL_NULL_V(tile_data, TileMapCell()); |
| 1024 | sum += tile_data->get_probability(); |
| 1025 | } else { |
| 1026 | sum += 1.0; |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | double empty_probability = sum * scattering; |
| 1031 | double current = 0.0; |
| 1032 | double rand = Math::random(0.0, sum + empty_probability); |
| 1033 | for (int i = 0; i < used_cells.size(); i++) { |
| 1034 | int source_id = p_pattern->get_cell_source_id(used_cells[i]); |
| 1035 | Vector2i atlas_coords = p_pattern->get_cell_atlas_coords(used_cells[i]); |
| 1036 | int alternative_tile = p_pattern->get_cell_alternative_tile(used_cells[i]); |
| 1037 | |
| 1038 | TileSetSource *source = *tile_set->get_source(source_id); |
| 1039 | TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source); |
| 1040 | if (atlas_source) { |
| 1041 | current += atlas_source->get_tile_data(atlas_coords, alternative_tile)->get_probability(); |
| 1042 | } else { |
| 1043 | current += 1.0; |
| 1044 | } |
| 1045 | |
| 1046 | if (current >= rand) { |
| 1047 | return TileMapCell(source_id, atlas_coords, alternative_tile); |
| 1048 | } |
| 1049 | } |
| 1050 | return TileMapCell(); |
| 1051 | } |
| 1052 | |
| 1053 | HashMap<Vector2i, TileMapCell> TileMapLayerEditorTilesPlugin::_draw_line(Vector2 p_start_drag_mouse_pos, Vector2 p_from_mouse_pos, Vector2 p_to_mouse_pos, bool p_erase) { |
| 1054 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 1055 | if (!edited_layer) { |
| 1056 | return HashMap<Vector2i, TileMapCell>(); |
| 1057 | } |
| 1058 | |
| 1059 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 1060 | if (tile_set.is_null()) { |
| 1061 | return HashMap<Vector2i, TileMapCell>(); |
| 1062 | } |
| 1063 | |
| 1064 | // Get or create the pattern. |
| 1065 | Ref<TileMapPattern> pattern = p_erase ? erase_pattern : selection_pattern; |
| 1066 | |
| 1067 | HashMap<Vector2i, TileMapCell> output; |
| 1068 | if (!pattern->is_empty()) { |
| 1069 | // Paint the tiles on the tile map. |
| 1070 | if (!p_erase && random_tile_toggle->is_pressed()) { |
| 1071 | // Paint a random tile. |
| 1072 | Vector<Vector2i> line = TileMapLayerEditor::get_line(edited_layer, tile_set->local_to_map(p_from_mouse_pos), tile_set->local_to_map(p_to_mouse_pos)); |
| 1073 | for (int i = 0; i < line.size(); i++) { |
| 1074 | _add_to_output_if_tile_changed(output, edited_layer, line[i], _pick_random_tile(pattern)); |
| 1075 | } |
| 1076 | } else { |
| 1077 | // Paint the pattern. |
| 1078 | // If we paint several tiles, we virtually move the mouse as if it was in the center of the "brush" |
| 1079 | Vector2 mouse_offset = (Vector2(pattern->get_size()) / 2.0 - Vector2(0.5, 0.5)) * tile_set->get_tile_size(); |
| 1080 | Vector2i last_hovered_cell = tile_set->local_to_map(p_from_mouse_pos - mouse_offset); |
| 1081 | Vector2i new_hovered_cell = tile_set->local_to_map(p_to_mouse_pos - mouse_offset); |
| 1082 | Vector2i drag_start_cell = tile_set->local_to_map(p_start_drag_mouse_pos - mouse_offset); |
| 1083 | |
| 1084 | TypedArray<Vector2i> used_cells = pattern->get_used_cells(); |
| 1085 | Vector2i offset = Vector2i(Math::posmod(drag_start_cell.x, pattern->get_size().x), Math::posmod(drag_start_cell.y, pattern->get_size().y)); // Note: no posmodv for Vector2i for now. Meh.s |
| 1086 | Vector<Vector2i> line = TileMapLayerEditor::get_line(edited_layer, (last_hovered_cell - offset) / pattern->get_size(), (new_hovered_cell - offset) / pattern->get_size()); |
| 1087 | for (int i = 0; i < line.size(); i++) { |
| 1088 | Vector2i top_left = line[i] * pattern->get_size() + offset; |
| 1089 | for (int j = 0; j < used_cells.size(); j++) { |
| 1090 | Vector2i coords = tile_set->map_pattern(top_left, used_cells[j], pattern); |
| 1091 | _add_to_output_if_tile_changed(output, edited_layer, coords, TileMapCell(pattern->get_cell_source_id(used_cells[j]), pattern->get_cell_atlas_coords(used_cells[j]), pattern->get_cell_alternative_tile(used_cells[j]))); |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | return output; |
| 1097 | } |
| 1098 | |
| 1099 | HashMap<Vector2i, TileMapCell> TileMapLayerEditorTilesPlugin::_draw_rect(Vector2i p_start_cell, Vector2i p_end_cell, bool p_erase) { |
| 1100 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 1101 | if (!edited_layer) { |
| 1102 | return HashMap<Vector2i, TileMapCell>(); |
| 1103 | } |
| 1104 | |
| 1105 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 1106 | if (tile_set.is_null()) { |
| 1107 | return HashMap<Vector2i, TileMapCell>(); |
| 1108 | } |
| 1109 | |
| 1110 | // Create the rect to draw. |
| 1111 | Rect2i rect = Rect2i(p_start_cell, p_end_cell - p_start_cell).abs(); |
| 1112 | rect.size += Vector2i(1, 1); |
| 1113 | |
| 1114 | // Get or create the pattern. |
| 1115 | Ref<TileMapPattern> pattern = p_erase ? erase_pattern : selection_pattern; |
| 1116 | |
| 1117 | ERR_FAIL_COND_V(pattern->is_empty(), (HashMap<Vector2i, TileMapCell>())); |
| 1118 | |
| 1119 | // Compute the offset to align things to the bottom or right. |
| 1120 | bool aligned_right = p_end_cell.x < p_start_cell.x; |
| 1121 | bool valigned_bottom = p_end_cell.y < p_start_cell.y; |
| 1122 | Vector2i offset = Vector2i(aligned_right ? -(pattern->get_size().x - (rect.get_size().x % pattern->get_size().x)) : 0, valigned_bottom ? -(pattern->get_size().y - (rect.get_size().y % pattern->get_size().y)) : 0); |
| 1123 | |
| 1124 | HashMap<Vector2i, TileMapCell> output; |
| 1125 | if (!pattern->is_empty()) { |
| 1126 | if (!p_erase && random_tile_toggle->is_pressed()) { |
| 1127 | // Paint a random tile. |
| 1128 | for (int x = 0; x < rect.size.x; x++) { |
| 1129 | for (int y = 0; y < rect.size.y; y++) { |
| 1130 | Vector2i coords = rect.position + Vector2i(x, y); |
| 1131 | _add_to_output_if_tile_changed(output, edited_layer, coords, _pick_random_tile(pattern)); |
| 1132 | } |
| 1133 | } |
| 1134 | } else { |
| 1135 | // Paint the pattern. |
| 1136 | TypedArray<Vector2i> used_cells = pattern->get_used_cells(); |
| 1137 | for (int x = 0; x <= rect.size.x / pattern->get_size().x; x++) { |
| 1138 | for (int y = 0; y <= rect.size.y / pattern->get_size().y; y++) { |
| 1139 | Vector2i pattern_coords = rect.position + Vector2i(x, y) * pattern->get_size() + offset; |
| 1140 | for (int j = 0; j < used_cells.size(); j++) { |
| 1141 | Vector2i coords = pattern_coords + used_cells[j]; |
| 1142 | if (rect.has_point(coords)) { |
| 1143 | _add_to_output_if_tile_changed(output, edited_layer, coords, TileMapCell(pattern->get_cell_source_id(used_cells[j]), pattern->get_cell_atlas_coords(used_cells[j]), pattern->get_cell_alternative_tile(used_cells[j]))); |
| 1144 | } |
| 1145 | } |
| 1146 | } |
| 1147 | } |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | return output; |
| 1152 | } |
| 1153 | |
| 1154 | HashMap<Vector2i, TileMapCell> TileMapLayerEditorTilesPlugin::_draw_bucket_fill(Vector2i p_coords, bool p_contiguous, bool p_erase) { |
| 1155 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 1156 | if (!edited_layer) { |
| 1157 | return HashMap<Vector2i, TileMapCell>(); |
| 1158 | } |
| 1159 | |
| 1160 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 1161 | if (tile_set.is_null()) { |
| 1162 | return HashMap<Vector2i, TileMapCell>(); |
| 1163 | } |
| 1164 | |
| 1165 | HashMap<Vector2i, TileMapCell> output; |
| 1166 | |
| 1167 | // Get or create the pattern. |
| 1168 | Ref<TileMapPattern> pattern = p_erase ? erase_pattern : selection_pattern; |
| 1169 | |
| 1170 | if (!pattern->is_empty()) { |
| 1171 | TileMapCell source_cell = edited_layer->get_cell(p_coords); |
| 1172 | |
| 1173 | // If we are filling empty tiles, compute the tilemap boundaries. |
| 1174 | Rect2i boundaries; |
| 1175 | if (source_cell.source_id == TileSet::INVALID_SOURCE) { |
| 1176 | boundaries = edited_layer->get_used_rect(); |
| 1177 | } |
| 1178 | |
| 1179 | if (p_contiguous) { |
| 1180 | // Replace continuous tiles like the source. |
| 1181 | RBSet<Vector2i> already_checked; |
| 1182 | List<Vector2i> to_check; |
| 1183 | to_check.push_back(p_coords); |
| 1184 | while (!to_check.is_empty()) { |
| 1185 | Vector2i coords = to_check.back()->get(); |
| 1186 | to_check.pop_back(); |
| 1187 | if (!already_checked.has(coords)) { |
| 1188 | if (source_cell.source_id == edited_layer->get_cell_source_id(coords) && |
| 1189 | source_cell.get_atlas_coords() == edited_layer->get_cell_atlas_coords(coords) && |
| 1190 | source_cell.alternative_tile == edited_layer->get_cell_alternative_tile(coords) && |
| 1191 | (source_cell.source_id != TileSet::INVALID_SOURCE || boundaries.has_point(coords))) { |
| 1192 | if (!p_erase && random_tile_toggle->is_pressed()) { |
| 1193 | // Paint a random tile. |
| 1194 | _add_to_output_if_tile_changed(output, edited_layer, coords, _pick_random_tile(pattern)); |
| 1195 | } else { |
| 1196 | // Paint the pattern. |
| 1197 | Vector2i pattern_coords = (coords - p_coords) % pattern->get_size(); // Note: it would be good to have posmodv for Vector2i. |
| 1198 | pattern_coords.x = pattern_coords.x < 0 ? pattern_coords.x + pattern->get_size().x : pattern_coords.x; |
| 1199 | pattern_coords.y = pattern_coords.y < 0 ? pattern_coords.y + pattern->get_size().y : pattern_coords.y; |
| 1200 | if (pattern->has_cell(pattern_coords)) { |
| 1201 | _add_to_output_if_tile_changed(output, edited_layer, coords, TileMapCell(pattern->get_cell_source_id(pattern_coords), pattern->get_cell_atlas_coords(pattern_coords), pattern->get_cell_alternative_tile(pattern_coords))); |
| 1202 | } else { |
| 1203 | _add_to_output_if_tile_changed(output, edited_layer, coords, TileMapCell()); |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | // Get surrounding tiles (handles different tile shapes). |
| 1208 | TypedArray<Vector2i> around = tile_set->get_surrounding_cells(coords); |
| 1209 | for (int i = 0; i < around.size(); i++) { |
| 1210 | to_check.push_back(around[i]); |
| 1211 | } |
| 1212 | } |
| 1213 | already_checked.insert(coords); |
| 1214 | } |
| 1215 | } |
| 1216 | } else { |
| 1217 | // Replace all tiles like the source. |
| 1218 | TypedArray<Vector2i> to_check; |
| 1219 | if (source_cell.source_id == TileSet::INVALID_SOURCE) { |
| 1220 | Rect2i rect = edited_layer->get_used_rect(); |
| 1221 | if (!rect.has_area()) { |
| 1222 | rect = Rect2i(p_coords, Vector2i(1, 1)); |
| 1223 | } |
| 1224 | for (int x = boundaries.position.x; x < boundaries.get_end().x; x++) { |
| 1225 | for (int y = boundaries.position.y; y < boundaries.get_end().y; y++) { |
| 1226 | to_check.append(Vector2i(x, y)); |
| 1227 | } |
| 1228 | } |
| 1229 | } else { |
| 1230 | to_check = edited_layer->get_used_cells(); |
| 1231 | } |
| 1232 | for (int i = 0; i < to_check.size(); i++) { |
| 1233 | Vector2i coords = to_check[i]; |
| 1234 | if (source_cell.source_id == edited_layer->get_cell_source_id(coords) && |
| 1235 | source_cell.get_atlas_coords() == edited_layer->get_cell_atlas_coords(coords) && |
| 1236 | source_cell.alternative_tile == edited_layer->get_cell_alternative_tile(coords) && |
| 1237 | (source_cell.source_id != TileSet::INVALID_SOURCE || boundaries.has_point(coords))) { |
| 1238 | if (!p_erase && random_tile_toggle->is_pressed()) { |
| 1239 | // Paint a random tile. |
| 1240 | _add_to_output_if_tile_changed(output, edited_layer, coords, _pick_random_tile(pattern)); |
| 1241 | } else { |
| 1242 | // Paint the pattern. |
| 1243 | Vector2i pattern_coords = (coords - p_coords) % pattern->get_size(); // Note: it would be good to have posmodv for Vector2i. |
| 1244 | pattern_coords.x = pattern_coords.x < 0 ? pattern_coords.x + pattern->get_size().x : pattern_coords.x; |
| 1245 | pattern_coords.y = pattern_coords.y < 0 ? pattern_coords.y + pattern->get_size().y : pattern_coords.y; |
| 1246 | if (pattern->has_cell(pattern_coords)) { |
| 1247 | _add_to_output_if_tile_changed(output, edited_layer, coords, TileMapCell(pattern->get_cell_source_id(pattern_coords), pattern->get_cell_atlas_coords(pattern_coords), pattern->get_cell_alternative_tile(pattern_coords))); |
| 1248 | } else { |
| 1249 | _add_to_output_if_tile_changed(output, edited_layer, coords, TileMapCell()); |
| 1250 | } |
| 1251 | } |
| 1252 | } |
| 1253 | } |
| 1254 | } |
| 1255 | } |
| 1256 | return output; |
| 1257 | } |
| 1258 | |
| 1259 | void TileMapLayerEditorTilesPlugin::_stop_dragging() { |
| 1260 | if (drag_type == DRAG_TYPE_NONE) { |
| 1261 | return; |
| 1262 | } |
| 1263 | |
| 1264 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 1265 | if (!edited_layer) { |
| 1266 | return; |
| 1267 | } |
| 1268 | |
| 1269 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 1270 | if (tile_set.is_null()) { |
| 1271 | return; |
| 1272 | } |
| 1273 | |
| 1274 | Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas(); |
| 1275 | Vector2 mpos = xform.affine_inverse().xform(CanvasItemEditor::get_singleton()->get_viewport_control()->get_local_mouse_position()); |
| 1276 | |
| 1277 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 1278 | switch (drag_type) { |
| 1279 | case DRAG_TYPE_SELECT: { |
| 1280 | undo_redo->create_action_for_history(TTR("Change selection"), EditorNode::get_editor_data().get_current_edited_scene_history_id(), UndoRedo::MERGE_DISABLE, false, false); |
| 1281 | undo_redo->add_undo_method(this, "_set_tile_map_selection", _get_tile_map_selection()); |
| 1282 | |
| 1283 | if (!Input::get_singleton()->is_key_pressed(Key::SHIFT) && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) { |
| 1284 | tile_map_selection.clear(); |
| 1285 | } |
| 1286 | Rect2i rect = Rect2i(tile_set->local_to_map(drag_start_mouse_pos), tile_set->local_to_map(mpos) - tile_set->local_to_map(drag_start_mouse_pos)).abs(); |
| 1287 | for (int x = rect.position.x; x <= rect.get_end().x; x++) { |
| 1288 | for (int y = rect.position.y; y <= rect.get_end().y; y++) { |
| 1289 | Vector2i coords = Vector2i(x, y); |
| 1290 | if (Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) { |
| 1291 | if (tile_map_selection.has(coords)) { |
| 1292 | tile_map_selection.erase(coords); |
| 1293 | } |
| 1294 | } else { |
| 1295 | if (edited_layer->get_cell_source_id(coords) != TileSet::INVALID_SOURCE) { |
| 1296 | tile_map_selection.insert(coords); |
| 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | } |
| 1301 | undo_redo->add_do_method(this, "_set_tile_map_selection", _get_tile_map_selection()); |
| 1302 | undo_redo->commit_action(false); |
| 1303 | |
| 1304 | _update_selection_pattern_from_tilemap_selection(); |
| 1305 | _update_tileset_selection_from_selection_pattern(); |
| 1306 | } break; |
| 1307 | case DRAG_TYPE_MOVE: { |
| 1308 | if (patterns_item_list->is_visible_in_tree() && patterns_item_list->has_point(patterns_item_list->get_local_mouse_position())) { |
| 1309 | // Restore the cells. |
| 1310 | for (KeyValue<Vector2i, TileMapCell> kv : drag_modified) { |
| 1311 | edited_layer->set_cell(kv.key, kv.value.source_id, kv.value.get_atlas_coords(), kv.value.alternative_tile); |
| 1312 | } |
| 1313 | |
| 1314 | if (!EditorNode::get_singleton()->is_resource_read_only(tile_set)) { |
| 1315 | // Creating a pattern in the pattern list. |
| 1316 | select_last_pattern = true; |
| 1317 | int new_pattern_index = tile_set->get_patterns_count(); |
| 1318 | undo_redo->create_action(TTR("Add TileSet pattern")); |
| 1319 | undo_redo->add_do_method(*tile_set, "add_pattern", selection_pattern, new_pattern_index); |
| 1320 | undo_redo->add_undo_method(*tile_set, "remove_pattern", new_pattern_index); |
| 1321 | undo_redo->commit_action(); |
| 1322 | } |
| 1323 | } else { |
| 1324 | // Get the top-left cell. |
| 1325 | Vector2i top_left; |
| 1326 | if (!tile_map_selection.is_empty()) { |
| 1327 | top_left = tile_map_selection.front()->get(); |
| 1328 | } |
| 1329 | for (const Vector2i &E : tile_map_selection) { |
| 1330 | top_left = top_left.min(E); |
| 1331 | } |
| 1332 | |
| 1333 | // Get the offset from the mouse. |
| 1334 | Vector2i offset = drag_start_mouse_pos - tile_set->map_to_local(top_left); |
| 1335 | offset = tile_set->local_to_map(mpos - offset) - tile_set->local_to_map(drag_start_mouse_pos - offset); |
| 1336 | |
| 1337 | TypedArray<Vector2i> selection_used_cells = selection_pattern->get_used_cells(); |
| 1338 | |
| 1339 | // Build the list of cells to undo. |
| 1340 | Vector2i coords; |
| 1341 | HashMap<Vector2i, TileMapCell> cells_undo; |
| 1342 | for (int i = 0; i < selection_used_cells.size(); i++) { |
| 1343 | coords = tile_set->map_pattern(top_left + offset, selection_used_cells[i], selection_pattern); |
| 1344 | cells_undo[coords] = TileMapCell(edited_layer->get_cell_source_id(coords), edited_layer->get_cell_atlas_coords(coords), edited_layer->get_cell_alternative_tile(coords)); |
| 1345 | } |
| 1346 | for (int i = 0; i < selection_used_cells.size(); i++) { |
| 1347 | coords = tile_set->map_pattern(top_left, selection_used_cells[i], selection_pattern); |
| 1348 | cells_undo[coords] = TileMapCell(drag_modified[coords].source_id, drag_modified[coords].get_atlas_coords(), drag_modified[coords].alternative_tile); |
| 1349 | } |
| 1350 | |
| 1351 | // Build the list of cells to do. |
| 1352 | HashMap<Vector2i, TileMapCell> cells_do; |
| 1353 | for (int i = 0; i < selection_used_cells.size(); i++) { |
| 1354 | coords = tile_set->map_pattern(top_left, selection_used_cells[i], selection_pattern); |
| 1355 | cells_do[coords] = TileMapCell(); |
| 1356 | } |
| 1357 | for (int i = 0; i < selection_used_cells.size(); i++) { |
| 1358 | coords = tile_set->map_pattern(top_left + offset, selection_used_cells[i], selection_pattern); |
| 1359 | cells_do[coords] = TileMapCell(selection_pattern->get_cell_source_id(selection_used_cells[i]), selection_pattern->get_cell_atlas_coords(selection_used_cells[i]), selection_pattern->get_cell_alternative_tile(selection_used_cells[i])); |
| 1360 | } |
| 1361 | |
| 1362 | // Move the tiles. |
| 1363 | undo_redo->create_action(TTR("Move tiles")); |
| 1364 | for (const KeyValue<Vector2i, TileMapCell> &E : cells_do) { |
| 1365 | undo_redo->add_do_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile); |
| 1366 | } |
| 1367 | for (const KeyValue<Vector2i, TileMapCell> &E : cells_undo) { |
| 1368 | undo_redo->add_undo_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile); |
| 1369 | } |
| 1370 | |
| 1371 | // Update the selection. |
| 1372 | undo_redo->add_undo_method(this, "_set_tile_map_selection", _get_tile_map_selection()); |
| 1373 | tile_map_selection.clear(); |
| 1374 | for (int i = 0; i < selection_used_cells.size(); i++) { |
| 1375 | coords = tile_set->map_pattern(top_left + offset, selection_used_cells[i], selection_pattern); |
| 1376 | tile_map_selection.insert(coords); |
| 1377 | } |
| 1378 | undo_redo->add_do_method(this, "_set_tile_map_selection", _get_tile_map_selection()); |
| 1379 | undo_redo->commit_action(); |
| 1380 | } |
| 1381 | } break; |
| 1382 | case DRAG_TYPE_PICK: { |
| 1383 | Rect2i rect = Rect2i(tile_set->local_to_map(drag_start_mouse_pos), tile_set->local_to_map(mpos) - tile_set->local_to_map(drag_start_mouse_pos)).abs(); |
| 1384 | rect.size += Vector2i(1, 1); |
| 1385 | |
| 1386 | int picked_source = -1; |
| 1387 | TypedArray<Vector2i> coords_array; |
| 1388 | for (int x = rect.position.x; x < rect.get_end().x; x++) { |
| 1389 | for (int y = rect.position.y; y < rect.get_end().y; y++) { |
| 1390 | Vector2i coords = Vector2i(x, y); |
| 1391 | |
| 1392 | int source = edited_layer->get_cell_source_id(coords); |
| 1393 | if (source != TileSet::INVALID_SOURCE) { |
| 1394 | coords_array.push_back(coords); |
| 1395 | if (picked_source == -1) { |
| 1396 | picked_source = source; |
| 1397 | } else if (picked_source != source) { |
| 1398 | picked_source = -2; |
| 1399 | } |
| 1400 | } |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | if (picked_source >= 0) { |
| 1405 | for (int i = 0; i < sources_list->get_item_count(); i++) { |
| 1406 | if (int(sources_list->get_item_metadata(i)) == picked_source) { |
| 1407 | sources_list->set_current(i); |
| 1408 | TilesEditorUtils::get_singleton()->set_sources_lists_current(i); |
| 1409 | break; |
| 1410 | } |
| 1411 | } |
| 1412 | sources_list->ensure_current_is_visible(); |
| 1413 | } |
| 1414 | |
| 1415 | Ref<TileMapPattern> new_selection_pattern = edited_layer->get_pattern(coords_array); |
| 1416 | if (!new_selection_pattern->is_empty()) { |
| 1417 | selection_pattern = new_selection_pattern; |
| 1418 | _update_tileset_selection_from_selection_pattern(); |
| 1419 | } |
| 1420 | picker_button->set_pressed(false); |
| 1421 | } break; |
| 1422 | case DRAG_TYPE_PAINT: { |
| 1423 | undo_redo->create_action(TTR("Paint tiles")); |
| 1424 | for (const KeyValue<Vector2i, TileMapCell> &E : drag_modified) { |
| 1425 | undo_redo->add_do_method(edited_layer, "set_cell", E.key, edited_layer->get_cell_source_id(E.key), edited_layer->get_cell_atlas_coords(E.key), edited_layer->get_cell_alternative_tile(E.key)); |
| 1426 | undo_redo->add_undo_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile); |
| 1427 | } |
| 1428 | undo_redo->commit_action(false); |
| 1429 | } break; |
| 1430 | case DRAG_TYPE_LINE: { |
| 1431 | HashMap<Vector2i, TileMapCell> to_draw = _draw_line(drag_start_mouse_pos, drag_start_mouse_pos, mpos, drag_erasing); |
| 1432 | undo_redo->create_action(TTR("Paint tiles")); |
| 1433 | for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) { |
| 1434 | if (!drag_erasing && E.value.source_id == TileSet::INVALID_SOURCE) { |
| 1435 | continue; |
| 1436 | } |
| 1437 | undo_redo->add_do_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile); |
| 1438 | undo_redo->add_undo_method(edited_layer, "set_cell", E.key, edited_layer->get_cell_source_id(E.key), edited_layer->get_cell_atlas_coords(E.key), edited_layer->get_cell_alternative_tile(E.key)); |
| 1439 | } |
| 1440 | undo_redo->commit_action(); |
| 1441 | } break; |
| 1442 | case DRAG_TYPE_RECT: { |
| 1443 | HashMap<Vector2i, TileMapCell> to_draw = _draw_rect(tile_set->local_to_map(drag_start_mouse_pos), tile_set->local_to_map(mpos), drag_erasing); |
| 1444 | undo_redo->create_action(TTR("Paint tiles")); |
| 1445 | for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) { |
| 1446 | if (!drag_erasing && E.value.source_id == TileSet::INVALID_SOURCE) { |
| 1447 | continue; |
| 1448 | } |
| 1449 | undo_redo->add_do_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile); |
| 1450 | undo_redo->add_undo_method(edited_layer, "set_cell", E.key, edited_layer->get_cell_source_id(E.key), edited_layer->get_cell_atlas_coords(E.key), edited_layer->get_cell_alternative_tile(E.key)); |
| 1451 | } |
| 1452 | undo_redo->commit_action(); |
| 1453 | } break; |
| 1454 | case DRAG_TYPE_BUCKET: { |
| 1455 | undo_redo->create_action(TTR("Paint tiles")); |
| 1456 | for (const KeyValue<Vector2i, TileMapCell> &E : drag_modified) { |
| 1457 | undo_redo->add_do_method(edited_layer, "set_cell", E.key, edited_layer->get_cell_source_id(E.key), edited_layer->get_cell_atlas_coords(E.key), edited_layer->get_cell_alternative_tile(E.key)); |
| 1458 | undo_redo->add_undo_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile); |
| 1459 | } |
| 1460 | undo_redo->commit_action(false); |
| 1461 | } break; |
| 1462 | case DRAG_TYPE_CLIPBOARD_PASTE: { |
| 1463 | Vector2 mouse_offset = (Vector2(tile_map_clipboard->get_size()) / 2.0 - Vector2(0.5, 0.5)) * tile_set->get_tile_size(); |
| 1464 | undo_redo->create_action(TTR("Paste tiles")); |
| 1465 | TypedArray<Vector2i> used_cells = tile_map_clipboard->get_used_cells(); |
| 1466 | for (int i = 0; i < used_cells.size(); i++) { |
| 1467 | Vector2i coords = tile_set->map_pattern(tile_set->local_to_map(mpos - mouse_offset), used_cells[i], tile_map_clipboard); |
| 1468 | undo_redo->add_do_method(edited_layer, "set_cell", coords, tile_map_clipboard->get_cell_source_id(used_cells[i]), tile_map_clipboard->get_cell_atlas_coords(used_cells[i]), tile_map_clipboard->get_cell_alternative_tile(used_cells[i])); |
| 1469 | undo_redo->add_undo_method(edited_layer, "set_cell", coords, edited_layer->get_cell_source_id(coords), edited_layer->get_cell_atlas_coords(coords), edited_layer->get_cell_alternative_tile(coords)); |
| 1470 | } |
| 1471 | undo_redo->commit_action(); |
| 1472 | } break; |
| 1473 | default: |
| 1474 | break; |
| 1475 | } |
| 1476 | drag_type = DRAG_TYPE_NONE; |
| 1477 | } |
| 1478 | |
| 1479 | void TileMapLayerEditorTilesPlugin::_apply_transform(TileTransformType p_type) { |
| 1480 | if (selection_pattern.is_null() || selection_pattern->is_empty()) { |
| 1481 | return; |
| 1482 | } |
| 1483 | |
| 1484 | Ref<TileMapPattern> transformed_pattern; |
| 1485 | transformed_pattern.instantiate(); |
| 1486 | |
| 1487 | Vector2i size = selection_pattern->get_size(); |
| 1488 | for (int y = 0; y < size.y; y++) { |
| 1489 | for (int x = 0; x < size.x; x++) { |
| 1490 | Vector2i src_coords = Vector2i(x, y); |
| 1491 | if (!selection_pattern->has_cell(src_coords)) { |
| 1492 | continue; |
| 1493 | } |
| 1494 | |
| 1495 | Vector2i dst_coords; |
| 1496 | |
| 1497 | if (p_type == TRANSFORM_ROTATE_LEFT) { |
| 1498 | dst_coords = Vector2i(y, size.x - x - 1); |
| 1499 | } else if (p_type == TRANSFORM_ROTATE_RIGHT) { |
| 1500 | dst_coords = Vector2i(size.y - y - 1, x); |
| 1501 | } else if (p_type == TRANSFORM_FLIP_H) { |
| 1502 | dst_coords = Vector2i(size.x - x - 1, y); |
| 1503 | } else if (p_type == TRANSFORM_FLIP_V) { |
| 1504 | dst_coords = Vector2i(x, size.y - y - 1); |
| 1505 | } |
| 1506 | |
| 1507 | transformed_pattern->set_cell(dst_coords, |
| 1508 | selection_pattern->get_cell_source_id(src_coords), selection_pattern->get_cell_atlas_coords(src_coords), |
| 1509 | _get_transformed_alternative(selection_pattern->get_cell_alternative_tile(src_coords), p_type)); |
| 1510 | } |
| 1511 | } |
| 1512 | selection_pattern = transformed_pattern; |
| 1513 | CanvasItemEditor::get_singleton()->update_viewport(); |
| 1514 | } |
| 1515 | |
| 1516 | int TileMapLayerEditorTilesPlugin::_get_transformed_alternative(int p_alternative_id, TileTransformType p_transform) { |
| 1517 | bool transform_flip_h = p_alternative_id & TileSetAtlasSource::TRANSFORM_FLIP_H; |
| 1518 | bool transform_flip_v = p_alternative_id & TileSetAtlasSource::TRANSFORM_FLIP_V; |
| 1519 | bool transform_transpose = p_alternative_id & TileSetAtlasSource::TRANSFORM_TRANSPOSE; |
| 1520 | |
| 1521 | switch (p_transform) { |
| 1522 | case TRANSFORM_ROTATE_LEFT: { // (h, v, t) -> (v, !h, !t) |
| 1523 | bool negated_flip_h = !transform_flip_h; |
| 1524 | transform_flip_h = transform_flip_v; |
| 1525 | transform_flip_v = negated_flip_h; |
| 1526 | transform_transpose = !transform_transpose; |
| 1527 | } break; |
| 1528 | case TRANSFORM_ROTATE_RIGHT: { // (h, v, t) -> (!v, h, !t) |
| 1529 | bool negated_flip_v = !transform_flip_v; |
| 1530 | transform_flip_v = transform_flip_h; |
| 1531 | transform_flip_h = negated_flip_v; |
| 1532 | transform_transpose = !transform_transpose; |
| 1533 | } break; |
| 1534 | case TRANSFORM_FLIP_H: { // (h, v, t) -> (!h, v, t) |
| 1535 | transform_flip_h = !transform_flip_h; |
| 1536 | } break; |
| 1537 | case TRANSFORM_FLIP_V: { // (h, v, t) -> (h, !v, t) |
| 1538 | transform_flip_v = !transform_flip_v; |
| 1539 | } break; |
| 1540 | } |
| 1541 | |
| 1542 | return TileSetAtlasSource::alternative_no_transform(p_alternative_id) | |
| 1543 | int(transform_flip_h) * TileSetAtlasSource::TRANSFORM_FLIP_H | |
| 1544 | int(transform_flip_v) * TileSetAtlasSource::TRANSFORM_FLIP_V | |
| 1545 | int(transform_transpose) * TileSetAtlasSource::TRANSFORM_TRANSPOSE; |
| 1546 | } |
| 1547 | |
| 1548 | void TileMapLayerEditorTilesPlugin::_update_fix_selected_and_hovered() { |
| 1549 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 1550 | if (!edited_layer) { |
| 1551 | hovered_tile.source_id = TileSet::INVALID_SOURCE; |
| 1552 | hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS); |
| 1553 | hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE; |
| 1554 | tile_set_selection.clear(); |
| 1555 | patterns_item_list->deselect_all(); |
| 1556 | tile_map_selection.clear(); |
| 1557 | selection_pattern.instantiate(); |
| 1558 | return; |
| 1559 | } |
| 1560 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 1561 | if (tile_set.is_null()) { |
| 1562 | hovered_tile.source_id = TileSet::INVALID_SOURCE; |
| 1563 | hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS); |
| 1564 | hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE; |
| 1565 | tile_set_selection.clear(); |
| 1566 | patterns_item_list->deselect_all(); |
| 1567 | tile_map_selection.clear(); |
| 1568 | selection_pattern.instantiate(); |
| 1569 | return; |
| 1570 | } |
| 1571 | |
| 1572 | int source_index = sources_list->get_current(); |
| 1573 | if (source_index < 0 || source_index >= sources_list->get_item_count()) { |
| 1574 | hovered_tile.source_id = TileSet::INVALID_SOURCE; |
| 1575 | hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS); |
| 1576 | hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE; |
| 1577 | tile_set_selection.clear(); |
| 1578 | patterns_item_list->deselect_all(); |
| 1579 | tile_map_selection.clear(); |
| 1580 | selection_pattern.instantiate(); |
| 1581 | return; |
| 1582 | } |
| 1583 | |
| 1584 | int source_id = sources_list->get_item_metadata(source_index); |
| 1585 | |
| 1586 | // Clear hovered if needed. |
| 1587 | if (source_id != hovered_tile.source_id || |
| 1588 | !tile_set->has_source(hovered_tile.source_id) || |
| 1589 | !tile_set->get_source(hovered_tile.source_id)->has_tile(hovered_tile.get_atlas_coords()) || |
| 1590 | !tile_set->get_source(hovered_tile.source_id)->has_alternative_tile(hovered_tile.get_atlas_coords(), hovered_tile.alternative_tile)) { |
| 1591 | hovered_tile.source_id = TileSet::INVALID_SOURCE; |
| 1592 | hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS); |
| 1593 | hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE; |
| 1594 | } |
| 1595 | |
| 1596 | // Cleanup tile set selection. |
| 1597 | for (RBSet<TileMapCell>::Element *E = tile_set_selection.front(); E;) { |
| 1598 | RBSet<TileMapCell>::Element *N = E->next(); |
| 1599 | const TileMapCell *selected = &(E->get()); |
| 1600 | if (!tile_set->has_source(selected->source_id) || |
| 1601 | !tile_set->get_source(selected->source_id)->has_tile(selected->get_atlas_coords()) || |
| 1602 | !tile_set->get_source(selected->source_id)->has_alternative_tile(selected->get_atlas_coords(), selected->alternative_tile)) { |
| 1603 | tile_set_selection.erase(E); |
| 1604 | } |
| 1605 | E = N; |
| 1606 | } |
| 1607 | |
| 1608 | // Cleanup selection. |
| 1609 | for (const KeyValue<Vector2i, TileMapCell> &E : selection_pattern->get_pattern()) { |
| 1610 | const Vector2i key = E.key; |
| 1611 | const TileMapCell &selected = E.value; |
| 1612 | if (!tile_set->has_source(selected.source_id) || |
| 1613 | !tile_set->get_source(selected.source_id)->has_tile(selected.get_atlas_coords()) || |
| 1614 | !tile_set->get_source(selected.source_id)->has_alternative_tile(selected.get_atlas_coords(), selected.alternative_tile)) { |
| 1615 | selection_pattern->remove_cell(key); |
| 1616 | } |
| 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | void TileMapLayerEditorTilesPlugin::_fix_invalid_tiles_in_tile_map_selection() { |
| 1621 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 1622 | if (!edited_layer) { |
| 1623 | return; |
| 1624 | } |
| 1625 | |
| 1626 | RBSet<Vector2i> to_remove; |
| 1627 | for (Vector2i selected : tile_map_selection) { |
| 1628 | TileMapCell cell = edited_layer->get_cell(selected); |
| 1629 | if (cell.source_id == TileSet::INVALID_SOURCE && cell.get_atlas_coords() == TileSetSource::INVALID_ATLAS_COORDS && cell.alternative_tile == TileSetAtlasSource::INVALID_TILE_ALTERNATIVE) { |
| 1630 | to_remove.insert(selected); |
| 1631 | } |
| 1632 | } |
| 1633 | |
| 1634 | for (Vector2i cell : to_remove) { |
| 1635 | tile_map_selection.erase(cell); |
| 1636 | } |
| 1637 | } |
| 1638 | void TileMapLayerEditorTilesPlugin::patterns_item_list_empty_clicked(const Vector2 &p_pos, MouseButton p_mouse_button_index) { |
| 1639 | if (p_mouse_button_index == MouseButton::LEFT) { |
| 1640 | _update_selection_pattern_from_tileset_pattern_selection(); |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | void TileMapLayerEditorTilesPlugin::_update_selection_pattern_from_tilemap_selection() { |
| 1645 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 1646 | if (!edited_layer) { |
| 1647 | return; |
| 1648 | } |
| 1649 | |
| 1650 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 1651 | if (tile_set.is_null()) { |
| 1652 | return; |
| 1653 | } |
| 1654 | |
| 1655 | selection_pattern.instantiate(); |
| 1656 | |
| 1657 | TypedArray<Vector2i> coords_array; |
| 1658 | for (const Vector2i &E : tile_map_selection) { |
| 1659 | coords_array.push_back(E); |
| 1660 | } |
| 1661 | selection_pattern = edited_layer->get_pattern(coords_array); |
| 1662 | _update_transform_buttons(); |
| 1663 | } |
| 1664 | |
| 1665 | void TileMapLayerEditorTilesPlugin::_update_selection_pattern_from_tileset_tiles_selection() { |
| 1666 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 1667 | if (!edited_layer) { |
| 1668 | return; |
| 1669 | } |
| 1670 | |
| 1671 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 1672 | if (tile_set.is_null()) { |
| 1673 | return; |
| 1674 | } |
| 1675 | |
| 1676 | // Clear the tilemap selection. |
| 1677 | tile_map_selection.clear(); |
| 1678 | |
| 1679 | // Clear the selected pattern. |
| 1680 | selection_pattern.instantiate(); |
| 1681 | |
| 1682 | // Group per source. |
| 1683 | HashMap<int, List<const TileMapCell *>> per_source; |
| 1684 | for (const TileMapCell &E : tile_set_selection) { |
| 1685 | per_source[E.source_id].push_back(&(E)); |
| 1686 | } |
| 1687 | |
| 1688 | int vertical_offset = 0; |
| 1689 | for (const KeyValue<int, List<const TileMapCell *>> &E_source : per_source) { |
| 1690 | // Per source. |
| 1691 | List<const TileMapCell *> unorganized; |
| 1692 | Rect2i encompassing_rect_coords; |
| 1693 | HashMap<Vector2i, const TileMapCell *> organized_pattern; |
| 1694 | |
| 1695 | TileSetSource *source = *tile_set->get_source(E_source.key); |
| 1696 | TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source); |
| 1697 | if (atlas_source) { |
| 1698 | // Organize using coordinates. |
| 1699 | for (const TileMapCell *current : E_source.value) { |
| 1700 | if (current->alternative_tile == 0) { |
| 1701 | organized_pattern[current->get_atlas_coords()] = current; |
| 1702 | } else { |
| 1703 | unorganized.push_back(current); |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | // Compute the encompassing rect for the organized pattern. |
| 1708 | HashMap<Vector2i, const TileMapCell *>::Iterator E_cell = organized_pattern.begin(); |
| 1709 | if (E_cell) { |
| 1710 | encompassing_rect_coords = Rect2i(E_cell->key, Vector2i(1, 1)); |
| 1711 | for (; E_cell; ++E_cell) { |
| 1712 | encompassing_rect_coords.expand_to(E_cell->key + Vector2i(1, 1)); |
| 1713 | encompassing_rect_coords.expand_to(E_cell->key); |
| 1714 | } |
| 1715 | } |
| 1716 | } else { |
| 1717 | // Add everything unorganized. |
| 1718 | for (const TileMapCell *cell : E_source.value) { |
| 1719 | unorganized.push_back(cell); |
| 1720 | } |
| 1721 | } |
| 1722 | |
| 1723 | // Now add everything to the output pattern. |
| 1724 | for (const KeyValue<Vector2i, const TileMapCell *> &E_cell : organized_pattern) { |
| 1725 | selection_pattern->set_cell(E_cell.key - encompassing_rect_coords.position + Vector2i(0, vertical_offset), E_cell.value->source_id, E_cell.value->get_atlas_coords(), E_cell.value->alternative_tile); |
| 1726 | } |
| 1727 | Vector2i organized_size = selection_pattern->get_size(); |
| 1728 | int unorganized_index = 0; |
| 1729 | for (const TileMapCell *cell : unorganized) { |
| 1730 | selection_pattern->set_cell(Vector2(organized_size.x + unorganized_index, vertical_offset), cell->source_id, cell->get_atlas_coords(), cell->alternative_tile); |
| 1731 | unorganized_index++; |
| 1732 | } |
| 1733 | vertical_offset += MAX(organized_size.y, 1); |
| 1734 | } |
| 1735 | CanvasItemEditor::get_singleton()->update_viewport(); |
| 1736 | _update_transform_buttons(); |
| 1737 | } |
| 1738 | |
| 1739 | void TileMapLayerEditorTilesPlugin::_update_selection_pattern_from_tileset_pattern_selection() { |
| 1740 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 1741 | if (!edited_layer) { |
| 1742 | return; |
| 1743 | } |
| 1744 | |
| 1745 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 1746 | if (tile_set.is_null()) { |
| 1747 | return; |
| 1748 | } |
| 1749 | |
| 1750 | // Clear the tilemap selection. |
| 1751 | tile_map_selection.clear(); |
| 1752 | |
| 1753 | // Clear the selected pattern. |
| 1754 | selection_pattern.instantiate(); |
| 1755 | |
| 1756 | if (patterns_item_list->get_selected_items().size() >= 1) { |
| 1757 | selection_pattern = patterns_item_list->get_item_metadata(patterns_item_list->get_selected_items()[0]); |
| 1758 | } |
| 1759 | |
| 1760 | CanvasItemEditor::get_singleton()->update_viewport(); |
| 1761 | } |
| 1762 | |
| 1763 | void TileMapLayerEditorTilesPlugin::_update_tileset_selection_from_selection_pattern() { |
| 1764 | tile_set_selection.clear(); |
| 1765 | TypedArray<Vector2i> used_cells = selection_pattern->get_used_cells(); |
| 1766 | for (int i = 0; i < used_cells.size(); i++) { |
| 1767 | Vector2i coords = used_cells[i]; |
| 1768 | if (selection_pattern->get_cell_source_id(coords) != TileSet::INVALID_SOURCE) { |
| 1769 | tile_set_selection.insert(TileMapCell(selection_pattern->get_cell_source_id(coords), selection_pattern->get_cell_atlas_coords(coords), selection_pattern->get_cell_alternative_tile(coords))); |
| 1770 | } |
| 1771 | } |
| 1772 | _update_source_display(); |
| 1773 | tile_atlas_control->queue_redraw(); |
| 1774 | alternative_tiles_control->queue_redraw(); |
| 1775 | _update_transform_buttons(); |
| 1776 | } |
| 1777 | |
| 1778 | void TileMapLayerEditorTilesPlugin::_tile_atlas_control_draw() { |
| 1779 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 1780 | if (!edited_layer) { |
| 1781 | return; |
| 1782 | } |
| 1783 | |
| 1784 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 1785 | if (tile_set.is_null()) { |
| 1786 | return; |
| 1787 | } |
| 1788 | |
| 1789 | int source_index = sources_list->get_current(); |
| 1790 | if (source_index < 0 || source_index >= sources_list->get_item_count()) { |
| 1791 | return; |
| 1792 | } |
| 1793 | |
| 1794 | int source_id = sources_list->get_item_metadata(source_index); |
| 1795 | if (!tile_set->has_source(source_id)) { |
| 1796 | return; |
| 1797 | } |
| 1798 | |
| 1799 | TileSetAtlasSource *atlas = Object::cast_to<TileSetAtlasSource>(*tile_set->get_source(source_id)); |
| 1800 | if (!atlas) { |
| 1801 | return; |
| 1802 | } |
| 1803 | |
| 1804 | // Draw the selection. |
| 1805 | Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color"); |
| 1806 | Color selection_color = Color::from_hsv(Math::fposmod(grid_color.get_h() + 0.5, 1.0), grid_color.get_s(), grid_color.get_v(), 1.0); |
| 1807 | for (const TileMapCell &E : tile_set_selection) { |
| 1808 | int16_t untransformed_alternative_id = E.alternative_tile & TileSetAtlasSource::UNTRANSFORM_MASK; |
| 1809 | if (E.source_id == source_id && untransformed_alternative_id == 0) { |
| 1810 | for (int frame = 0; frame < atlas->get_tile_animation_frames_count(E.get_atlas_coords()); frame++) { |
| 1811 | Color color = selection_color; |
| 1812 | if (frame > 0) { |
| 1813 | color.a *= 0.3; |
| 1814 | } |
| 1815 | TilesEditorUtils::draw_selection_rect(tile_atlas_control, atlas->get_tile_texture_region(E.get_atlas_coords(), frame), color); |
| 1816 | } |
| 1817 | } |
| 1818 | } |
| 1819 | |
| 1820 | // Draw the hovered tile. |
| 1821 | if (hovered_tile.get_atlas_coords() != TileSetSource::INVALID_ATLAS_COORDS && hovered_tile.alternative_tile == 0 && !tile_set_dragging_selection) { |
| 1822 | for (int frame = 0; frame < atlas->get_tile_animation_frames_count(hovered_tile.get_atlas_coords()); frame++) { |
| 1823 | Color color = Color(1.0, 0.8, 0.0, frame == 0 ? 0.6 : 0.3); |
| 1824 | TilesEditorUtils::draw_selection_rect(tile_atlas_control, atlas->get_tile_texture_region(hovered_tile.get_atlas_coords(), frame), color); |
| 1825 | } |
| 1826 | } |
| 1827 | |
| 1828 | // Draw the selection rect. |
| 1829 | if (tile_set_dragging_selection) { |
| 1830 | Vector2i start_tile = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_set_drag_start_mouse_pos, true); |
| 1831 | Vector2i end_tile = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position(), true); |
| 1832 | |
| 1833 | Rect2i region = Rect2i(start_tile, end_tile - start_tile).abs(); |
| 1834 | region.size += Vector2i(1, 1); |
| 1835 | |
| 1836 | RBSet<Vector2i> to_draw; |
| 1837 | for (int x = region.position.x; x < region.get_end().x; x++) { |
| 1838 | for (int y = region.position.y; y < region.get_end().y; y++) { |
| 1839 | Vector2i tile = atlas->get_tile_at_coords(Vector2i(x, y)); |
| 1840 | if (tile != TileSetSource::INVALID_ATLAS_COORDS) { |
| 1841 | to_draw.insert(tile); |
| 1842 | } |
| 1843 | } |
| 1844 | } |
| 1845 | for (const Vector2i &E : to_draw) { |
| 1846 | TilesEditorUtils::draw_selection_rect(tile_atlas_control, atlas->get_tile_texture_region(E)); |
| 1847 | } |
| 1848 | } |
| 1849 | } |
| 1850 | |
| 1851 | void TileMapLayerEditorTilesPlugin::_tile_atlas_control_mouse_exited() { |
| 1852 | hovered_tile.source_id = TileSet::INVALID_SOURCE; |
| 1853 | hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS); |
| 1854 | hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE; |
| 1855 | tile_atlas_control->queue_redraw(); |
| 1856 | } |
| 1857 | |
| 1858 | void TileMapLayerEditorTilesPlugin::_tile_atlas_control_gui_input(const Ref<InputEvent> &p_event) { |
| 1859 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 1860 | if (!edited_layer) { |
| 1861 | return; |
| 1862 | } |
| 1863 | |
| 1864 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 1865 | if (tile_set.is_null()) { |
| 1866 | return; |
| 1867 | } |
| 1868 | |
| 1869 | int source_index = sources_list->get_current(); |
| 1870 | if (source_index < 0 || source_index >= sources_list->get_item_count()) { |
| 1871 | return; |
| 1872 | } |
| 1873 | |
| 1874 | int source_id = sources_list->get_item_metadata(source_index); |
| 1875 | if (!tile_set->has_source(source_id)) { |
| 1876 | return; |
| 1877 | } |
| 1878 | |
| 1879 | TileSetAtlasSource *atlas = Object::cast_to<TileSetAtlasSource>(*tile_set->get_source(source_id)); |
| 1880 | if (!atlas) { |
| 1881 | return; |
| 1882 | } |
| 1883 | |
| 1884 | // Update the hovered tile |
| 1885 | hovered_tile.source_id = source_id; |
| 1886 | hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS); |
| 1887 | hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE; |
| 1888 | Vector2i coords = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position()); |
| 1889 | if (coords != TileSetSource::INVALID_ATLAS_COORDS) { |
| 1890 | coords = atlas->get_tile_at_coords(coords); |
| 1891 | if (coords != TileSetSource::INVALID_ATLAS_COORDS) { |
| 1892 | hovered_tile.set_atlas_coords(coords); |
| 1893 | hovered_tile.alternative_tile = 0; |
| 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | Ref<InputEventMouseMotion> mm = p_event; |
| 1898 | if (mm.is_valid()) { |
| 1899 | tile_atlas_control->queue_redraw(); |
| 1900 | alternative_tiles_control->queue_redraw(); |
| 1901 | } |
| 1902 | |
| 1903 | Ref<InputEventMouseButton> mb = p_event; |
| 1904 | if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) { |
| 1905 | if (mb->is_pressed()) { // Pressed |
| 1906 | tile_set_dragging_selection = true; |
| 1907 | tile_set_drag_start_mouse_pos = tile_atlas_control->get_local_mouse_position(); |
| 1908 | if (!mb->is_shift_pressed()) { |
| 1909 | tile_set_selection.clear(); |
| 1910 | } |
| 1911 | |
| 1912 | if (hovered_tile.get_atlas_coords() != TileSetSource::INVALID_ATLAS_COORDS && hovered_tile.alternative_tile == 0) { |
| 1913 | if (mb->is_shift_pressed() && tile_set_selection.has(TileMapCell(source_id, hovered_tile.get_atlas_coords(), 0))) { |
| 1914 | tile_set_selection.erase(TileMapCell(source_id, hovered_tile.get_atlas_coords(), 0)); |
| 1915 | } else { |
| 1916 | tile_set_selection.insert(TileMapCell(source_id, hovered_tile.get_atlas_coords(), 0)); |
| 1917 | } |
| 1918 | } |
| 1919 | _update_selection_pattern_from_tileset_tiles_selection(); |
| 1920 | } else { // Released |
| 1921 | if (tile_set_dragging_selection) { |
| 1922 | if (!mb->is_shift_pressed()) { |
| 1923 | tile_set_selection.clear(); |
| 1924 | } |
| 1925 | // Compute the covered area. |
| 1926 | Vector2i start_tile = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_set_drag_start_mouse_pos, true); |
| 1927 | Vector2i end_tile = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position(), true); |
| 1928 | if (start_tile != TileSetSource::INVALID_ATLAS_COORDS && end_tile != TileSetSource::INVALID_ATLAS_COORDS) { |
| 1929 | Rect2i region = Rect2i(start_tile, end_tile - start_tile).abs(); |
| 1930 | region.size += Vector2i(1, 1); |
| 1931 | |
| 1932 | // To update the selection, we copy the selected/not selected status of the tiles we drag from. |
| 1933 | Vector2i start_coords = atlas->get_tile_at_coords(start_tile); |
| 1934 | if (mb->is_shift_pressed() && start_coords != TileSetSource::INVALID_ATLAS_COORDS && !tile_set_selection.has(TileMapCell(source_id, start_coords, 0))) { |
| 1935 | // Remove from the selection. |
| 1936 | for (int x = region.position.x; x < region.get_end().x; x++) { |
| 1937 | for (int y = region.position.y; y < region.get_end().y; y++) { |
| 1938 | Vector2i tile_coords = atlas->get_tile_at_coords(Vector2i(x, y)); |
| 1939 | if (tile_coords != TileSetSource::INVALID_ATLAS_COORDS && tile_set_selection.has(TileMapCell(source_id, tile_coords, 0))) { |
| 1940 | tile_set_selection.erase(TileMapCell(source_id, tile_coords, 0)); |
| 1941 | } |
| 1942 | } |
| 1943 | } |
| 1944 | } else { |
| 1945 | // Insert in the selection. |
| 1946 | for (int x = region.position.x; x < region.get_end().x; x++) { |
| 1947 | for (int y = region.position.y; y < region.get_end().y; y++) { |
| 1948 | Vector2i tile_coords = atlas->get_tile_at_coords(Vector2i(x, y)); |
| 1949 | if (tile_coords != TileSetSource::INVALID_ATLAS_COORDS) { |
| 1950 | tile_set_selection.insert(TileMapCell(source_id, tile_coords, 0)); |
| 1951 | } |
| 1952 | } |
| 1953 | } |
| 1954 | } |
| 1955 | } |
| 1956 | _update_selection_pattern_from_tileset_tiles_selection(); |
| 1957 | } |
| 1958 | tile_set_dragging_selection = false; |
| 1959 | } |
| 1960 | tile_atlas_control->queue_redraw(); |
| 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | void TileMapLayerEditorTilesPlugin::_tile_alternatives_control_draw() { |
| 1965 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 1966 | if (!edited_layer) { |
| 1967 | return; |
| 1968 | } |
| 1969 | |
| 1970 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 1971 | if (tile_set.is_null()) { |
| 1972 | return; |
| 1973 | } |
| 1974 | |
| 1975 | int source_index = sources_list->get_current(); |
| 1976 | if (source_index < 0 || source_index >= sources_list->get_item_count()) { |
| 1977 | return; |
| 1978 | } |
| 1979 | |
| 1980 | int source_id = sources_list->get_item_metadata(source_index); |
| 1981 | if (!tile_set->has_source(source_id)) { |
| 1982 | return; |
| 1983 | } |
| 1984 | |
| 1985 | TileSetAtlasSource *atlas = Object::cast_to<TileSetAtlasSource>(*tile_set->get_source(source_id)); |
| 1986 | if (!atlas) { |
| 1987 | return; |
| 1988 | } |
| 1989 | |
| 1990 | // Draw the selection. |
| 1991 | for (const TileMapCell &E : tile_set_selection) { |
| 1992 | int16_t untransformed_alternative_id = E.alternative_tile & TileSetAtlasSource::UNTRANSFORM_MASK; |
| 1993 | if (E.source_id == source_id && E.get_atlas_coords() != TileSetSource::INVALID_ATLAS_COORDS && untransformed_alternative_id > 0) { |
| 1994 | Rect2i rect = tile_atlas_view->get_alternative_tile_rect(E.get_atlas_coords(), untransformed_alternative_id); |
| 1995 | if (rect != Rect2i()) { |
| 1996 | TilesEditorUtils::draw_selection_rect(alternative_tiles_control, rect); |
| 1997 | } |
| 1998 | } |
| 1999 | } |
| 2000 | |
| 2001 | // Draw hovered tile. |
| 2002 | if (hovered_tile.get_atlas_coords() != TileSetSource::INVALID_ATLAS_COORDS && hovered_tile.alternative_tile > 0) { |
| 2003 | Rect2i rect = tile_atlas_view->get_alternative_tile_rect(hovered_tile.get_atlas_coords(), hovered_tile.alternative_tile); |
| 2004 | if (rect != Rect2i()) { |
| 2005 | TilesEditorUtils::draw_selection_rect(alternative_tiles_control, rect, Color(1.0, 0.8, 0.0, 0.5)); |
| 2006 | } |
| 2007 | } |
| 2008 | } |
| 2009 | |
| 2010 | void TileMapLayerEditorTilesPlugin::_tile_alternatives_control_mouse_exited() { |
| 2011 | hovered_tile.source_id = TileSet::INVALID_SOURCE; |
| 2012 | hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS); |
| 2013 | hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE; |
| 2014 | alternative_tiles_control->queue_redraw(); |
| 2015 | } |
| 2016 | |
| 2017 | void TileMapLayerEditorTilesPlugin::_tile_alternatives_control_gui_input(const Ref<InputEvent> &p_event) { |
| 2018 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 2019 | if (!edited_layer) { |
| 2020 | return; |
| 2021 | } |
| 2022 | |
| 2023 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 2024 | if (tile_set.is_null()) { |
| 2025 | return; |
| 2026 | } |
| 2027 | |
| 2028 | int source_index = sources_list->get_current(); |
| 2029 | if (source_index < 0 || source_index >= sources_list->get_item_count()) { |
| 2030 | return; |
| 2031 | } |
| 2032 | |
| 2033 | int source_id = sources_list->get_item_metadata(source_index); |
| 2034 | if (!tile_set->has_source(source_id)) { |
| 2035 | return; |
| 2036 | } |
| 2037 | |
| 2038 | TileSetAtlasSource *atlas = Object::cast_to<TileSetAtlasSource>(*tile_set->get_source(source_id)); |
| 2039 | if (!atlas) { |
| 2040 | return; |
| 2041 | } |
| 2042 | |
| 2043 | // Update the hovered tile |
| 2044 | hovered_tile.source_id = source_id; |
| 2045 | hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS); |
| 2046 | hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE; |
| 2047 | Vector3i alternative_coords = tile_atlas_view->get_alternative_tile_at_pos(alternative_tiles_control->get_local_mouse_position()); |
| 2048 | Vector2i coords = Vector2i(alternative_coords.x, alternative_coords.y); |
| 2049 | int alternative = alternative_coords.z; |
| 2050 | if (coords != TileSetSource::INVALID_ATLAS_COORDS && alternative != TileSetSource::INVALID_TILE_ALTERNATIVE) { |
| 2051 | hovered_tile.set_atlas_coords(coords); |
| 2052 | hovered_tile.alternative_tile = alternative; |
| 2053 | } |
| 2054 | |
| 2055 | Ref<InputEventMouseMotion> mm = p_event; |
| 2056 | if (mm.is_valid()) { |
| 2057 | tile_atlas_control->queue_redraw(); |
| 2058 | alternative_tiles_control->queue_redraw(); |
| 2059 | } |
| 2060 | |
| 2061 | Ref<InputEventMouseButton> mb = p_event; |
| 2062 | if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) { |
| 2063 | if (mb->is_pressed()) { // Pressed |
| 2064 | // Left click pressed. |
| 2065 | if (!mb->is_shift_pressed()) { |
| 2066 | tile_set_selection.clear(); |
| 2067 | } |
| 2068 | |
| 2069 | if (coords != TileSetSource::INVALID_ATLAS_COORDS && alternative != TileSetAtlasSource::INVALID_TILE_ALTERNATIVE) { |
| 2070 | if (mb->is_shift_pressed() && tile_set_selection.has(TileMapCell(source_id, hovered_tile.get_atlas_coords(), hovered_tile.alternative_tile))) { |
| 2071 | tile_set_selection.erase(TileMapCell(source_id, hovered_tile.get_atlas_coords(), hovered_tile.alternative_tile)); |
| 2072 | } else { |
| 2073 | tile_set_selection.insert(TileMapCell(source_id, hovered_tile.get_atlas_coords(), hovered_tile.alternative_tile)); |
| 2074 | } |
| 2075 | } |
| 2076 | _update_selection_pattern_from_tileset_tiles_selection(); |
| 2077 | } |
| 2078 | tile_atlas_control->queue_redraw(); |
| 2079 | alternative_tiles_control->queue_redraw(); |
| 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | void TileMapLayerEditorTilesPlugin::_set_tile_map_selection(const TypedArray<Vector2i> &p_selection) { |
| 2084 | tile_map_selection.clear(); |
| 2085 | for (int i = 0; i < p_selection.size(); i++) { |
| 2086 | tile_map_selection.insert(p_selection[i]); |
| 2087 | } |
| 2088 | _update_selection_pattern_from_tilemap_selection(); |
| 2089 | _update_tileset_selection_from_selection_pattern(); |
| 2090 | CanvasItemEditor::get_singleton()->update_viewport(); |
| 2091 | } |
| 2092 | |
| 2093 | TypedArray<Vector2i> TileMapLayerEditorTilesPlugin::_get_tile_map_selection() const { |
| 2094 | TypedArray<Vector2i> output; |
| 2095 | for (const Vector2i &E : tile_map_selection) { |
| 2096 | output.push_back(E); |
| 2097 | } |
| 2098 | return output; |
| 2099 | } |
| 2100 | |
| 2101 | void TileMapLayerEditorTilesPlugin::_set_source_sort(int p_sort) { |
| 2102 | for (int i = 0; i != TilesEditorUtils::SOURCE_SORT_MAX; i++) { |
| 2103 | source_sort_button->get_popup()->set_item_checked(i, (i == (int)p_sort)); |
| 2104 | } |
| 2105 | TilesEditorUtils::get_singleton()->set_sorting_option(p_sort); |
| 2106 | _update_tile_set_sources_list(); |
| 2107 | EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "tile_source_sort", p_sort); |
| 2108 | } |
| 2109 | |
| 2110 | void TileMapLayerEditorTilesPlugin::_bind_methods() { |
| 2111 | ClassDB::bind_method(D_METHOD("_set_tile_map_selection", "selection"), &TileMapLayerEditorTilesPlugin::_set_tile_map_selection); |
| 2112 | ClassDB::bind_method(D_METHOD("_get_tile_map_selection"), &TileMapLayerEditorTilesPlugin::_get_tile_map_selection); |
| 2113 | } |
| 2114 | |
| 2115 | void TileMapLayerEditorTilesPlugin::edit(ObjectID p_tile_map_layer_id) { |
| 2116 | _stop_dragging(); // Avoids staying in a wrong drag state. |
| 2117 | |
| 2118 | // Disable sort button if the tileset is read-only |
| 2119 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 2120 | Ref<TileSet> tile_set; |
| 2121 | if (edited_layer) { |
| 2122 | tile_set = edited_layer->get_tile_set(); |
| 2123 | if (tile_set.is_valid()) { |
| 2124 | source_sort_button->set_disabled(EditorNode::get_singleton()->is_resource_read_only(tile_set)); |
| 2125 | } |
| 2126 | } |
| 2127 | |
| 2128 | TileMapLayer *new_tile_map_layer = ObjectDB::get_instance<TileMapLayer>(p_tile_map_layer_id); |
| 2129 | Ref<TileSet> new_tile_set; |
| 2130 | if (new_tile_map_layer) { |
| 2131 | new_tile_set = new_tile_map_layer->get_tile_set(); |
| 2132 | } |
| 2133 | |
| 2134 | if (tile_set.is_valid() && tile_set != new_tile_set) { |
| 2135 | // Clear the selection. |
| 2136 | tile_set_selection.clear(); |
| 2137 | patterns_item_list->deselect_all(); |
| 2138 | tile_map_selection.clear(); |
| 2139 | selection_pattern.instantiate(); |
| 2140 | } |
| 2141 | |
| 2142 | edited_tile_map_layer_id = p_tile_map_layer_id; |
| 2143 | } |
| 2144 | |
| 2145 | void TileMapLayerEditorTilesPlugin::update_layout(EditorDock::DockLayout p_layout) { |
| 2146 | bool is_vertical = (p_layout == EditorDock::DockLayout::DOCK_LAYOUT_VERTICAL); |
| 2147 | atlas_sources_split_container->set_vertical(is_vertical); |
| 2148 | atlas_sources_split_container->move_child(split_container_left_side, is_vertical ? -1 : 0); |
| 2149 | split_container_left_side->set_vertical(!is_vertical); |
| 2150 | |
| 2151 | tilemap_tiles_tools_buttons->set_vertical(is_vertical); |
| 2152 | transform_toolbar->set_vertical(is_vertical); |
| 2153 | tools_settings->set_vertical(is_vertical); |
| 2154 | tools_settings_vsep->set_vertical(is_vertical); |
| 2155 | transform_separator->set_vertical(is_vertical); |
| 2156 | |
| 2157 | wide_toolbar->set_visible(is_vertical); |
| 2158 | bucket_contiguous_checkbox->reparent(is_vertical ? wide_toolbar : tools_settings); |
| 2159 | scatter_controls_container->reparent(is_vertical ? wide_toolbar : tools_settings); |
| 2160 | |
| 2161 | if (p_layout == EditorDock::DockLayout::DOCK_LAYOUT_FLOATING) { |
| 2162 | patterns_mc->set_theme_type_variation("NoBorderHorizontalBottom"); |
| 2163 | patterns_item_list->set_scroll_hint_mode(ItemList::SCROLL_HINT_MODE_TOP); |
| 2164 | } else { |
| 2165 | patterns_mc->set_theme_type_variation(is_vertical ? "" : "NoBorderBottomPanel"); |
| 2166 | patterns_item_list->set_scroll_hint_mode(is_vertical ? ItemList::SCROLL_HINT_MODE_DISABLED : ItemList::SCROLL_HINT_MODE_BOTH); |
| 2167 | } |
| 2168 | patterns_item_list->set_theme_type_variation(is_vertical ? "ItemListSecondary" : ""); |
| 2169 | } |
| 2170 | |
| 2171 | TileMapLayerEditorTilesPlugin::TileMapLayerEditorTilesPlugin() { |
| 2172 | CanvasItemEditor::get_singleton() |
| 2173 | ->get_viewport_control() |
| 2174 | ->connect(SceneStringName(mouse_exited), callable_mp(this, &TileMapLayerEditorTilesPlugin::_mouse_exited_viewport)); |
| 2175 | |
| 2176 | // --- Initialize references --- |
| 2177 | tile_map_clipboard.instantiate(); |
| 2178 | selection_pattern.instantiate(); |
| 2179 | |
| 2180 | erase_pattern.instantiate(); |
| 2181 | erase_pattern->set_cell(Vector2i(0, 0), TileSet::INVALID_SOURCE, TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE); |
| 2182 | |
| 2183 | // --- Toolbar --- |
| 2184 | wide_toolbar = memnew(HBoxContainer); |
| 2185 | |
| 2186 | tilemap_tiles_tools_buttons = memnew(BoxContainer); |
| 2187 | |
| 2188 | tool_buttons_group.instantiate(); |
| 2189 | |
| 2190 | select_tool_button = memnew(Button); |
| 2191 | select_tool_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2192 | select_tool_button->set_toggle_mode(true); |
| 2193 | select_tool_button->set_button_group(tool_buttons_group); |
| 2194 | select_tool_button->set_shortcut(ED_SHORTCUT("tiles_editor/selection_tool", TTRC("Selection Tool"), Key::S)); |
| 2195 | select_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_toolbar)); |
| 2196 | select_tool_button->set_accessibility_name(TTRC("Selection Tool")); |
| 2197 | tilemap_tiles_tools_buttons->add_child(select_tool_button); |
| 2198 | viewport_shortcut_buttons.push_back(select_tool_button); |
| 2199 | |
| 2200 | paint_tool_button = memnew(Button); |
| 2201 | paint_tool_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2202 | paint_tool_button->set_toggle_mode(true); |
| 2203 | paint_tool_button->set_button_group(tool_buttons_group); |
| 2204 | paint_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/paint_tool")); |
| 2205 | paint_tool_button->set_tooltip_text(TTR("Shift: Draw line.") + "\n" + vformat(TTR("%s+Shift: Draw rectangle."), keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL))); |
| 2206 | paint_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_toolbar)); |
| 2207 | paint_tool_button->set_accessibility_name(TTRC("Paint Tool")); |
| 2208 | tilemap_tiles_tools_buttons->add_child(paint_tool_button); |
| 2209 | viewport_shortcut_buttons.push_back(paint_tool_button); |
| 2210 | |
| 2211 | line_tool_button = memnew(Button); |
| 2212 | line_tool_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2213 | line_tool_button->set_toggle_mode(true); |
| 2214 | line_tool_button->set_button_group(tool_buttons_group); |
| 2215 | // TRANSLATORS: This refers to the line tool in the tilemap editor. |
| 2216 | line_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/line_tool")); |
| 2217 | line_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_toolbar)); |
| 2218 | line_tool_button->set_accessibility_name(TTRC("Line Tool")); |
| 2219 | tilemap_tiles_tools_buttons->add_child(line_tool_button); |
| 2220 | viewport_shortcut_buttons.push_back(line_tool_button); |
| 2221 | |
| 2222 | rect_tool_button = memnew(Button); |
| 2223 | rect_tool_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2224 | rect_tool_button->set_toggle_mode(true); |
| 2225 | rect_tool_button->set_button_group(tool_buttons_group); |
| 2226 | rect_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/rect_tool")); |
| 2227 | rect_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_toolbar)); |
| 2228 | rect_tool_button->set_accessibility_name(TTRC("Rect Tool")); |
| 2229 | tilemap_tiles_tools_buttons->add_child(rect_tool_button); |
| 2230 | viewport_shortcut_buttons.push_back(rect_tool_button); |
| 2231 | |
| 2232 | bucket_tool_button = memnew(Button); |
| 2233 | bucket_tool_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2234 | bucket_tool_button->set_toggle_mode(true); |
| 2235 | bucket_tool_button->set_button_group(tool_buttons_group); |
| 2236 | bucket_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/bucket_tool")); |
| 2237 | bucket_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_toolbar)); |
| 2238 | bucket_tool_button->set_accessibility_name(TTRC("Bucket Tool")); |
| 2239 | tilemap_tiles_tools_buttons->add_child(bucket_tool_button); |
| 2240 | viewport_shortcut_buttons.push_back(bucket_tool_button); |
| 2241 | |
| 2242 | // -- TileMap tool settings -- |
| 2243 | tools_settings = memnew(BoxContainer); |
| 2244 | tools_settings_vsep = memnew(SwitchSeparator); |
| 2245 | tools_settings_vsep->set_vertical(false); |
| 2246 | |
| 2247 | // Picker |
| 2248 | picker_button = memnew(Button); |
| 2249 | picker_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2250 | picker_button->set_toggle_mode(true); |
| 2251 | picker_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/picker")); |
| 2252 | Key key = OS::prefer_meta_over_ctrl() ? Key::META : Key::CTRL; |
| 2253 | picker_button->set_tooltip_text(vformat(TTR("Alternatively hold %s with other tools to pick tile."), find_keycode_name(key))); |
| 2254 | picker_button->connect(SceneStringName(pressed), callable_mp(CanvasItemEditor::get_singleton(), &CanvasItemEditor::update_viewport)); |
| 2255 | picker_button->set_accessibility_name(TTRC("Pick")); |
| 2256 | tools_settings->add_child(picker_button); |
| 2257 | viewport_shortcut_buttons.push_back(picker_button); |
| 2258 | |
| 2259 | // Erase button. |
| 2260 | erase_button = memnew(Button); |
| 2261 | erase_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2262 | erase_button->set_toggle_mode(true); |
| 2263 | erase_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/eraser")); |
| 2264 | erase_button->set_tooltip_text(TTRC("Alternatively use RMB to erase tiles.")); |
| 2265 | erase_button->connect(SceneStringName(pressed), callable_mp(CanvasItemEditor::get_singleton(), &CanvasItemEditor::update_viewport)); |
| 2266 | erase_button->set_accessibility_name(TTRC("Erase")); |
| 2267 | tools_settings->add_child(erase_button); |
| 2268 | viewport_shortcut_buttons.push_back(erase_button); |
| 2269 | |
| 2270 | // Transform toolbar. |
| 2271 | transform_toolbar = memnew(BoxContainer); |
| 2272 | tools_settings->add_child(transform_toolbar); |
| 2273 | transform_separator = memnew(SwitchSeparator); |
| 2274 | transform_separator->set_vertical(false); |
| 2275 | transform_toolbar->add_child(transform_separator); |
| 2276 | |
| 2277 | transform_button_rotate_left = memnew(Button); |
| 2278 | transform_button_rotate_left->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2279 | transform_button_rotate_left->set_shortcut(ED_SHORTCUT("tiles_editor/rotate_tile_left", TTRC("Rotate Tile Left"), Key::Z)); |
| 2280 | transform_toolbar->add_child(transform_button_rotate_left); |
| 2281 | transform_button_rotate_left->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_apply_transform).bind(TRANSFORM_ROTATE_LEFT)); |
| 2282 | transform_button_rotate_left->set_accessibility_name(TTRC("Rotate Tile Left")); |
| 2283 | viewport_shortcut_buttons.push_back(transform_button_rotate_left); |
| 2284 | |
| 2285 | transform_button_rotate_right = memnew(Button); |
| 2286 | transform_button_rotate_right->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2287 | transform_button_rotate_right->set_shortcut(ED_SHORTCUT("tiles_editor/rotate_tile_right", TTRC("Rotate Tile Right"), Key::X)); |
| 2288 | transform_toolbar->add_child(transform_button_rotate_right); |
| 2289 | transform_button_rotate_right->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_apply_transform).bind(TRANSFORM_ROTATE_RIGHT)); |
| 2290 | transform_button_rotate_right->set_accessibility_name(TTRC("Rotate Tile Right")); |
| 2291 | viewport_shortcut_buttons.push_back(transform_button_rotate_right); |
| 2292 | |
| 2293 | transform_button_flip_h = memnew(Button); |
| 2294 | transform_button_flip_h->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2295 | transform_button_flip_h->set_shortcut(ED_SHORTCUT("tiles_editor/flip_tile_horizontal", TTRC("Flip Tile Horizontally"), Key::C)); |
| 2296 | transform_toolbar->add_child(transform_button_flip_h); |
| 2297 | transform_button_flip_h->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_apply_transform).bind(TRANSFORM_FLIP_H)); |
| 2298 | transform_button_flip_h->set_accessibility_name(TTRC("Flip Tile Horizontally")); |
| 2299 | viewport_shortcut_buttons.push_back(transform_button_flip_h); |
| 2300 | |
| 2301 | transform_button_flip_v = memnew(Button); |
| 2302 | transform_button_flip_v->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2303 | transform_button_flip_v->set_shortcut(ED_SHORTCUT("tiles_editor/flip_tile_vertical", TTRC("Flip Tile Vertically"), Key::V)); |
| 2304 | transform_toolbar->add_child(transform_button_flip_v); |
| 2305 | transform_button_flip_v->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_apply_transform).bind(TRANSFORM_FLIP_V)); |
| 2306 | transform_button_flip_v->set_accessibility_name(TTRC("Flip Tile Vertically")); |
| 2307 | viewport_shortcut_buttons.push_back(transform_button_flip_v); |
| 2308 | |
| 2309 | // Continuous checkbox. |
| 2310 | bucket_contiguous_checkbox = memnew(CheckBox); |
| 2311 | bucket_contiguous_checkbox->set_flat(true); |
| 2312 | bucket_contiguous_checkbox->set_text(TTR("Contiguous")); |
| 2313 | bucket_contiguous_checkbox->set_pressed(true); |
| 2314 | bucket_contiguous_checkbox->hide(); |
| 2315 | tools_settings->add_child(bucket_contiguous_checkbox); |
| 2316 | |
| 2317 | // Random tile checkbox. |
| 2318 | random_tile_toggle = memnew(Button); |
| 2319 | random_tile_toggle->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2320 | random_tile_toggle->set_toggle_mode(true); |
| 2321 | random_tile_toggle->set_tooltip_text(TTR("Place Random Tile")); |
| 2322 | random_tile_toggle->connect(SceneStringName(toggled), callable_mp(this, &TileMapLayerEditorTilesPlugin::_on_random_tile_checkbox_toggled)); |
| 2323 | tools_settings->add_child(random_tile_toggle); |
| 2324 | |
| 2325 | // Random tile scattering. |
| 2326 | scatter_controls_container = memnew(BoxContainer); |
| 2327 | scatter_controls_container->set_vertical(false); |
| 2328 | |
| 2329 | scatter_label = memnew(Label); |
| 2330 | scatter_label->set_tooltip_text(TTR("Modifies the chance of painting nothing instead of a randomly selected tile.")); |
| 2331 | scatter_label->set_text(TTR("Scattering:")); |
| 2332 | scatter_controls_container->add_child(scatter_label); |
| 2333 | |
| 2334 | scatter_spinbox = memnew(SpinBox); |
| 2335 | scatter_spinbox->set_min(0.0); |
| 2336 | scatter_spinbox->set_max(1000); |
| 2337 | scatter_spinbox->set_step(0.001); |
| 2338 | scatter_spinbox->set_tooltip_text(TTR("Modifies the chance of painting nothing instead of a randomly selected tile.")); |
| 2339 | scatter_spinbox->get_line_edit()->add_theme_constant_override("minimum_character_width", 4); |
| 2340 | scatter_spinbox->connect(SceneStringName(value_changed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_on_scattering_spinbox_changed)); |
| 2341 | scatter_spinbox->set_accessibility_name(TTRC("Scattering:")); |
| 2342 | scatter_controls_container->add_child(scatter_spinbox); |
| 2343 | tools_settings->add_child(scatter_controls_container); |
| 2344 | |
| 2345 | _on_random_tile_checkbox_toggled(false); |
| 2346 | |
| 2347 | // Default tool. |
| 2348 | paint_tool_button->set_pressed(true); |
| 2349 | _update_toolbar(); |
| 2350 | |
| 2351 | // --- Bottom panel tiles --- |
| 2352 | tiles_bottom_panel = memnew(VBoxContainer); |
| 2353 | // FIXME: This can trigger theme updates when the nodes that we want to update are not yet available. |
| 2354 | // The toolbar should be extracted to a dedicated control and theme updates should be handled through |
| 2355 | // the notification. |
| 2356 | tiles_bottom_panel->connect(SceneStringName(theme_changed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_theme)); |
| 2357 | tiles_bottom_panel->connect(SceneStringName(visibility_changed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_stop_dragging)); |
| 2358 | tiles_bottom_panel->connect(SceneStringName(visibility_changed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tab_changed)); |
| 2359 | tiles_bottom_panel->set_name(TTR("Tiles")); |
| 2360 | |
| 2361 | missing_source_label = memnew(Label); |
| 2362 | missing_source_label->set_focus_mode(Control::FOCUS_ACCESSIBILITY); |
| 2363 | missing_source_label->set_text(TTR("This TileMap's TileSet has no Tile Source configured. Go to the TileSet bottom panel to add one.")); |
| 2364 | missing_source_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART); |
| 2365 | missing_source_label->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 2366 | missing_source_label->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 2367 | missing_source_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
| 2368 | missing_source_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER); |
| 2369 | missing_source_label->hide(); |
| 2370 | tiles_bottom_panel->add_child(missing_source_label); |
| 2371 | |
| 2372 | atlas_sources_split_container = memnew(SplitContainer); |
| 2373 | atlas_sources_split_container->set_vertical(false); |
| 2374 | atlas_sources_split_container->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 2375 | atlas_sources_split_container->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 2376 | tiles_bottom_panel->add_child(atlas_sources_split_container); |
| 2377 | |
| 2378 | split_container_left_side = memnew(BoxContainer); |
| 2379 | split_container_left_side->set_vertical(true); |
| 2380 | split_container_left_side->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 2381 | split_container_left_side->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 2382 | split_container_left_side->set_stretch_ratio(0.25); |
| 2383 | split_container_left_side->set_custom_minimum_size(Size2(70, 0) * EDSCALE); |
| 2384 | atlas_sources_split_container->add_child(split_container_left_side); |
| 2385 | |
| 2386 | source_sort_button = memnew(MenuButton); |
| 2387 | source_sort_button->set_flat(false); |
| 2388 | source_sort_button->set_theme_type_variation("FlatMenuButton"); |
| 2389 | source_sort_button->set_tooltip_text(TTR("Sort sources")); |
| 2390 | source_sort_button->set_h_size_flags(Control::SIZE_SHRINK_END); |
| 2391 | source_sort_button->set_v_size_flags(Control::SIZE_SHRINK_END); |
| 2392 | |
| 2393 | PopupMenu *p = source_sort_button->get_popup(); |
| 2394 | p->connect(SceneStringName(id_pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_set_source_sort)); |
| 2395 | p->add_radio_check_item(TTR("Sort by ID (Ascending)"), TilesEditorUtils::SOURCE_SORT_ID); |
| 2396 | p->add_radio_check_item(TTR("Sort by ID (Descending)"), TilesEditorUtils::SOURCE_SORT_ID_REVERSE); |
| 2397 | p->add_radio_check_item(TTR("Sort by Name (Ascending)"), TilesEditorUtils::SOURCE_SORT_NAME); |
| 2398 | p->add_radio_check_item(TTR("Sort by Name (Descending)"), TilesEditorUtils::SOURCE_SORT_NAME_REVERSE); |
| 2399 | p->set_item_checked(TilesEditorUtils::SOURCE_SORT_ID, true); |
| 2400 | |
| 2401 | sources_list = memnew(TileSetSourceItemList); |
| 2402 | sources_list->connect(SceneStringName(item_selected), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_source_display).unbind(1)); |
| 2403 | sources_list->connect(SceneStringName(item_selected), callable_mp(TilesEditorUtils::get_singleton(), &TilesEditorUtils::set_sources_lists_current)); |
| 2404 | sources_list->connect("item_activated", callable_mp(TilesEditorUtils::get_singleton(), &TilesEditorUtils::display_tile_set_editor_panel).unbind(1)); |
| 2405 | sources_list->connect(SceneStringName(visibility_changed), callable_mp(TilesEditorUtils::get_singleton(), &TilesEditorUtils::synchronize_sources_list).bind(sources_list, source_sort_button)); |
| 2406 | sources_list->connect("sort_request", callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_tile_set_sources_list)); |
| 2407 | split_container_left_side->add_child(sources_list); |
| 2408 | split_container_left_side->add_child(source_sort_button); |
| 2409 | |
| 2410 | // Tile atlas source. |
| 2411 | tile_atlas_view = memnew(TileAtlasView); |
| 2412 | tile_atlas_view->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 2413 | tile_atlas_view->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 2414 | tile_atlas_view->set_texture_grid_visible(false); |
| 2415 | tile_atlas_view->set_tile_shape_grid_visible(false); |
| 2416 | tile_atlas_view->connect("transform_changed", callable_mp(TilesEditorUtils::get_singleton(), &TilesEditorUtils::set_atlas_view_transform)); |
| 2417 | atlas_sources_split_container->add_child(tile_atlas_view); |
| 2418 | |
| 2419 | tile_atlas_control = memnew(Control); |
| 2420 | tile_atlas_control->connect(SceneStringName(draw), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tile_atlas_control_draw)); |
| 2421 | tile_atlas_control->connect(SceneStringName(mouse_exited), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tile_atlas_control_mouse_exited)); |
| 2422 | tile_atlas_control->connect(SceneStringName(gui_input), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tile_atlas_control_gui_input)); |
| 2423 | tile_atlas_view->add_control_over_atlas_tiles(tile_atlas_control); |
| 2424 | |
| 2425 | alternative_tiles_control = memnew(Control); |
| 2426 | alternative_tiles_control->connect(SceneStringName(draw), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tile_alternatives_control_draw)); |
| 2427 | alternative_tiles_control->connect(SceneStringName(mouse_exited), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tile_alternatives_control_mouse_exited)); |
| 2428 | alternative_tiles_control->connect(SceneStringName(gui_input), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tile_alternatives_control_gui_input)); |
| 2429 | tile_atlas_view->add_control_over_alternative_tiles(alternative_tiles_control); |
| 2430 | |
| 2431 | // Scenes collection source. |
| 2432 | scene_tiles_list = memnew(ItemList); |
| 2433 | scene_tiles_list->set_auto_translate_mode(Node::AUTO_TRANSLATE_MODE_DISABLED); |
| 2434 | scene_tiles_list->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 2435 | scene_tiles_list->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 2436 | scene_tiles_list->set_select_mode(ItemList::SELECT_MULTI); |
| 2437 | scene_tiles_list->connect("multi_selected", callable_mp(this, &TileMapLayerEditorTilesPlugin::_scenes_list_multi_selected)); |
| 2438 | scene_tiles_list->connect("empty_clicked", callable_mp(this, &TileMapLayerEditorTilesPlugin::_scenes_list_lmb_empty_clicked)); |
| 2439 | scene_tiles_list->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST); |
| 2440 | atlas_sources_split_container->add_child(scene_tiles_list); |
| 2441 | |
| 2442 | // Invalid source label. |
| 2443 | invalid_source_label = memnew(Label); |
| 2444 | invalid_source_label->set_focus_mode(Control::FOCUS_ACCESSIBILITY); |
| 2445 | invalid_source_label->set_text(TTR("Invalid source selected.")); |
| 2446 | invalid_source_label->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 2447 | invalid_source_label->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 2448 | invalid_source_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
| 2449 | invalid_source_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER); |
| 2450 | invalid_source_label->hide(); |
| 2451 | atlas_sources_split_container->add_child(invalid_source_label); |
| 2452 | |
| 2453 | // --- Bottom panel patterns --- |
| 2454 | patterns_mc = memnew(MarginContainer); |
| 2455 | patterns_mc->set_name(TTR("Patterns")); |
| 2456 | patterns_mc->connect(SceneStringName(visibility_changed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tab_changed)); |
| 2457 | |
| 2458 | int thumbnail_size = 64; |
| 2459 | patterns_item_list = memnew(ItemList); |
| 2460 | patterns_item_list->set_auto_translate_mode(Node::AUTO_TRANSLATE_MODE_DISABLED); |
| 2461 | patterns_item_list->set_max_columns(0); |
| 2462 | patterns_item_list->set_icon_mode(ItemList::ICON_MODE_TOP); |
| 2463 | patterns_item_list->set_fixed_column_width(thumbnail_size * 3 / 2); |
| 2464 | patterns_item_list->set_max_text_lines(2); |
| 2465 | patterns_item_list->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size)); |
| 2466 | patterns_item_list->set_scroll_hint_mode(ItemList::SCROLL_HINT_MODE_BOTH); |
| 2467 | patterns_item_list->connect(SceneStringName(gui_input), callable_mp(this, &TileMapLayerEditorTilesPlugin::_patterns_item_list_gui_input)); |
| 2468 | patterns_item_list->connect(SceneStringName(item_selected), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_selection_pattern_from_tileset_pattern_selection).unbind(1)); |
| 2469 | patterns_item_list->connect("item_activated", callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_selection_pattern_from_tileset_pattern_selection).unbind(1)); |
| 2470 | patterns_item_list->connect("empty_clicked", callable_mp(this, &TileMapLayerEditorTilesPlugin::patterns_item_list_empty_clicked)); |
| 2471 | patterns_mc->add_child(patterns_item_list); |
| 2472 | |
| 2473 | patterns_help_label = memnew(Label); |
| 2474 | patterns_help_label->set_focus_mode(Control::FOCUS_ACCESSIBILITY); |
| 2475 | patterns_help_label->set_text(TTR("Drag and drop or paste a TileMap selection here to store a pattern.")); |
| 2476 | patterns_help_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART); |
| 2477 | patterns_help_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
| 2478 | patterns_help_label->set_anchors_and_offsets_preset(Control::PRESET_HCENTER_WIDE); |
| 2479 | patterns_item_list->add_child(patterns_help_label); |
| 2480 | |
| 2481 | // Update. |
| 2482 | _update_source_display(); |
| 2483 | } |
| 2484 | |
| 2485 | void TileMapLayerEditorTerrainsPlugin::tile_set_changed() { |
| 2486 | _update_terrains_cache(); |
| 2487 | _update_terrains_tree(); |
| 2488 | _update_tiles_list(); |
| 2489 | } |
| 2490 | |
| 2491 | void TileMapLayerEditorTerrainsPlugin::_update_toolbar() { |
| 2492 | bucket_contiguous_checkbox->set_visible(tool_buttons_group->get_pressed_button() == bucket_tool_button); |
| 2493 | } |
| 2494 | |
| 2495 | Vector<TileMapLayerSubEditorPlugin::TabData> TileMapLayerEditorTerrainsPlugin::get_tabs() const { |
| 2496 | Vector<TileMapLayerSubEditorPlugin::TabData> tabs; |
| 2497 | Vector<Control *> toolbar_controls; |
| 2498 | toolbar_controls.push_back(tilemap_tiles_tools_buttons); |
| 2499 | toolbar_controls.push_back(tools_settings); |
| 2500 | tabs.push_back({ toolbar_controls, wide_toolbar, main_box_container }); |
| 2501 | return tabs; |
| 2502 | } |
| 2503 | |
| 2504 | HashMap<Vector2i, TileMapCell> TileMapLayerEditorTerrainsPlugin::_draw_terrain_path_or_connect(const Vector<Vector2i> &p_to_paint, int p_terrain_set, int p_terrain, bool p_connect) const { |
| 2505 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 2506 | if (!edited_layer) { |
| 2507 | return HashMap<Vector2i, TileMapCell>(); |
| 2508 | } |
| 2509 | |
| 2510 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 2511 | if (tile_set.is_null()) { |
| 2512 | return HashMap<Vector2i, TileMapCell>(); |
| 2513 | } |
| 2514 | |
| 2515 | HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_output; |
| 2516 | if (p_connect) { |
| 2517 | terrain_fill_output = edited_layer->terrain_fill_connect(p_to_paint, p_terrain_set, p_terrain, false); |
| 2518 | } else { |
| 2519 | terrain_fill_output = edited_layer->terrain_fill_path(p_to_paint, p_terrain_set, p_terrain, false); |
| 2520 | } |
| 2521 | |
| 2522 | // Make the painted path a set for faster lookups |
| 2523 | HashSet<Vector2i> painted_set; |
| 2524 | for (Vector2i coords : p_to_paint) { |
| 2525 | painted_set.insert(coords); |
| 2526 | } |
| 2527 | |
| 2528 | HashMap<Vector2i, TileMapCell> output; |
| 2529 | for (const KeyValue<Vector2i, TileSet::TerrainsPattern> &kv : terrain_fill_output) { |
| 2530 | if (painted_set.has(kv.key)) { |
| 2531 | // Paint a random tile with the correct terrain for the painted path. |
| 2532 | _add_to_output_if_tile_changed(output, edited_layer, kv.key, tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value)); |
| 2533 | } else { |
| 2534 | // Avoids updating the painted path from the output if the new pattern is the same as before. |
| 2535 | TileSet::TerrainsPattern in_map_terrain_pattern = TileSet::TerrainsPattern(*tile_set, p_terrain_set); |
| 2536 | TileMapCell cell = edited_layer->get_cell(kv.key); |
| 2537 | if (cell.source_id != TileSet::INVALID_SOURCE) { |
| 2538 | TileSetSource *source = *tile_set->get_source(cell.source_id); |
| 2539 | TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source); |
| 2540 | if (atlas_source) { |
| 2541 | // Get tile data. |
| 2542 | TileData *tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile); |
| 2543 | if (tile_data && tile_data->get_terrain_set() == p_terrain_set) { |
| 2544 | in_map_terrain_pattern = tile_data->get_terrains_pattern(); |
| 2545 | } |
| 2546 | } |
| 2547 | } |
| 2548 | if (in_map_terrain_pattern != kv.value) { |
| 2549 | _add_to_output_if_tile_changed(output, edited_layer, kv.key, tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value)); |
| 2550 | } |
| 2551 | } |
| 2552 | } |
| 2553 | return output; |
| 2554 | } |
| 2555 | |
| 2556 | HashMap<Vector2i, TileMapCell> TileMapLayerEditorTerrainsPlugin::_draw_terrain_pattern(const Vector<Vector2i> &p_to_paint, int p_terrain_set, TileSet::TerrainsPattern p_terrains_pattern) const { |
| 2557 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 2558 | if (!edited_layer) { |
| 2559 | return HashMap<Vector2i, TileMapCell>(); |
| 2560 | } |
| 2561 | |
| 2562 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 2563 | if (tile_set.is_null()) { |
| 2564 | return HashMap<Vector2i, TileMapCell>(); |
| 2565 | } |
| 2566 | |
| 2567 | HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_output = edited_layer->terrain_fill_pattern(p_to_paint, p_terrain_set, p_terrains_pattern, false); |
| 2568 | |
| 2569 | // Make the painted path a set for faster lookups |
| 2570 | HashSet<Vector2i> painted_set; |
| 2571 | for (Vector2i coords : p_to_paint) { |
| 2572 | painted_set.insert(coords); |
| 2573 | } |
| 2574 | |
| 2575 | HashMap<Vector2i, TileMapCell> output; |
| 2576 | for (const KeyValue<Vector2i, TileSet::TerrainsPattern> &kv : terrain_fill_output) { |
| 2577 | if (painted_set.has(kv.key)) { |
| 2578 | // Paint a random tile with the correct terrain for the painted path. |
| 2579 | _add_to_output_if_tile_changed(output, edited_layer, kv.key, tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value)); |
| 2580 | } else { |
| 2581 | // Avoids updating the painted path from the output if the new pattern is the same as before. |
| 2582 | TileSet::TerrainsPattern in_map_terrain_pattern = TileSet::TerrainsPattern(*tile_set, p_terrain_set); |
| 2583 | TileMapCell cell = edited_layer->get_cell(kv.key); |
| 2584 | if (cell.source_id != TileSet::INVALID_SOURCE) { |
| 2585 | TileSetSource *source = *tile_set->get_source(cell.source_id); |
| 2586 | TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source); |
| 2587 | if (atlas_source) { |
| 2588 | // Get tile data. |
| 2589 | TileData *tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile); |
| 2590 | if (tile_data && tile_data->get_terrain_set() == p_terrain_set) { |
| 2591 | in_map_terrain_pattern = tile_data->get_terrains_pattern(); |
| 2592 | } |
| 2593 | } |
| 2594 | } |
| 2595 | if (in_map_terrain_pattern != kv.value) { |
| 2596 | _add_to_output_if_tile_changed(output, edited_layer, kv.key, tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value)); |
| 2597 | } |
| 2598 | } |
| 2599 | } |
| 2600 | return output; |
| 2601 | } |
| 2602 | |
| 2603 | HashMap<Vector2i, TileMapCell> TileMapLayerEditorTerrainsPlugin::_draw_line(Vector2i p_start_cell, Vector2i p_end_cell, bool p_erase) { |
| 2604 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 2605 | if (!edited_layer) { |
| 2606 | return HashMap<Vector2i, TileMapCell>(); |
| 2607 | } |
| 2608 | |
| 2609 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 2610 | if (tile_set.is_null()) { |
| 2611 | return HashMap<Vector2i, TileMapCell>(); |
| 2612 | } |
| 2613 | |
| 2614 | if (p_erase) { |
| 2615 | return _draw_terrain_pattern(TileMapLayerEditor::get_line(edited_layer, p_start_cell, p_end_cell), selected_terrain_set, TileSet::TerrainsPattern(*tile_set, selected_terrain_set)); |
| 2616 | } else { |
| 2617 | if (selected_type == SELECTED_TYPE_CONNECT) { |
| 2618 | return _draw_terrain_path_or_connect(TileMapLayerEditor::get_line(edited_layer, p_start_cell, p_end_cell), selected_terrain_set, selected_terrain, true); |
| 2619 | } else if (selected_type == SELECTED_TYPE_PATH) { |
| 2620 | return _draw_terrain_path_or_connect(TileMapLayerEditor::get_line(edited_layer, p_start_cell, p_end_cell), selected_terrain_set, selected_terrain, false); |
| 2621 | } else { // SELECTED_TYPE_PATTERN |
| 2622 | return _draw_terrain_pattern(TileMapLayerEditor::get_line(edited_layer, p_start_cell, p_end_cell), selected_terrain_set, selected_terrains_pattern); |
| 2623 | } |
| 2624 | } |
| 2625 | } |
| 2626 | |
| 2627 | HashMap<Vector2i, TileMapCell> TileMapLayerEditorTerrainsPlugin::_draw_rect(Vector2i p_start_cell, Vector2i p_end_cell, bool p_erase) { |
| 2628 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 2629 | if (!edited_layer) { |
| 2630 | return HashMap<Vector2i, TileMapCell>(); |
| 2631 | } |
| 2632 | |
| 2633 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 2634 | if (tile_set.is_null()) { |
| 2635 | return HashMap<Vector2i, TileMapCell>(); |
| 2636 | } |
| 2637 | |
| 2638 | Rect2i rect; |
| 2639 | rect.set_position(p_start_cell); |
| 2640 | rect.set_end(p_end_cell); |
| 2641 | rect = rect.abs(); |
| 2642 | |
| 2643 | Vector<Vector2i> to_draw; |
| 2644 | for (int x = rect.position.x; x <= rect.get_end().x; x++) { |
| 2645 | for (int y = rect.position.y; y <= rect.get_end().y; y++) { |
| 2646 | to_draw.append(Vector2i(x, y)); |
| 2647 | } |
| 2648 | } |
| 2649 | |
| 2650 | if (p_erase) { |
| 2651 | return _draw_terrain_pattern(to_draw, selected_terrain_set, TileSet::TerrainsPattern(*tile_set, selected_terrain_set)); |
| 2652 | } else { |
| 2653 | if (selected_type == SELECTED_TYPE_CONNECT || selected_type == SELECTED_TYPE_PATH) { |
| 2654 | return _draw_terrain_path_or_connect(to_draw, selected_terrain_set, selected_terrain, true); |
| 2655 | } else { // SELECTED_TYPE_PATTERN |
| 2656 | return _draw_terrain_pattern(to_draw, selected_terrain_set, selected_terrains_pattern); |
| 2657 | } |
| 2658 | } |
| 2659 | } |
| 2660 | |
| 2661 | RBSet<Vector2i> TileMapLayerEditorTerrainsPlugin::_get_cells_for_bucket_fill(Vector2i p_coords, bool p_contiguous) { |
| 2662 | const TileMapLayer *edited_layer = _get_edited_layer(); |
| 2663 | if (!edited_layer) { |
| 2664 | return RBSet<Vector2i>(); |
| 2665 | } |
| 2666 | |
| 2667 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 2668 | if (tile_set.is_null()) { |
| 2669 | return RBSet<Vector2i>(); |
| 2670 | } |
| 2671 | |
| 2672 | TileMapCell source_cell = edited_layer->get_cell(p_coords); |
| 2673 | |
| 2674 | TileSet::TerrainsPattern source_pattern(*tile_set, selected_terrain_set); |
| 2675 | if (source_cell.source_id != TileSet::INVALID_SOURCE) { |
| 2676 | TileData *tile_data = nullptr; |
| 2677 | Ref<TileSetSource> source = tile_set->get_source(source_cell.source_id); |
| 2678 | Ref<TileSetAtlasSource> atlas_source = source; |
| 2679 | if (atlas_source.is_valid()) { |
| 2680 | tile_data = atlas_source->get_tile_data(source_cell.get_atlas_coords(), source_cell.alternative_tile); |
| 2681 | } |
| 2682 | if (!tile_data) { |
| 2683 | return RBSet<Vector2i>(); |
| 2684 | } |
| 2685 | source_pattern = tile_data->get_terrains_pattern(); |
| 2686 | } |
| 2687 | |
| 2688 | // If we are filling empty tiles, compute the tilemap boundaries. |
| 2689 | Rect2i boundaries; |
| 2690 | if (source_cell.source_id == TileSet::INVALID_SOURCE) { |
| 2691 | boundaries = edited_layer->get_used_rect(); |
| 2692 | } |
| 2693 | |
| 2694 | RBSet<Vector2i> output; |
| 2695 | if (p_contiguous) { |
| 2696 | // Replace continuous tiles like the source. |
| 2697 | RBSet<Vector2i> already_checked; |
| 2698 | List<Vector2i> to_check; |
| 2699 | to_check.push_back(p_coords); |
| 2700 | while (!to_check.is_empty()) { |
| 2701 | Vector2i coords = to_check.back()->get(); |
| 2702 | to_check.pop_back(); |
| 2703 | if (!already_checked.has(coords)) { |
| 2704 | // Get the candidate cell pattern. |
| 2705 | TileSet::TerrainsPattern candidate_pattern(*tile_set, selected_terrain_set); |
| 2706 | if (edited_layer->get_cell_source_id(coords) != TileSet::INVALID_SOURCE) { |
| 2707 | TileData *tile_data = nullptr; |
| 2708 | Ref<TileSetSource> source = tile_set->get_source(edited_layer->get_cell_source_id(coords)); |
| 2709 | Ref<TileSetAtlasSource> atlas_source = source; |
| 2710 | if (atlas_source.is_valid()) { |
| 2711 | tile_data = atlas_source->get_tile_data(edited_layer->get_cell_atlas_coords(coords), edited_layer->get_cell_alternative_tile(coords)); |
| 2712 | } |
| 2713 | if (tile_data) { |
| 2714 | candidate_pattern = tile_data->get_terrains_pattern(); |
| 2715 | } |
| 2716 | } |
| 2717 | |
| 2718 | // Draw. |
| 2719 | if (candidate_pattern == source_pattern && (!source_pattern.is_erase_pattern() || boundaries.has_point(coords))) { |
| 2720 | output.insert(coords); |
| 2721 | |
| 2722 | // Get surrounding tiles (handles different tile shapes). |
| 2723 | TypedArray<Vector2i> around = tile_set->get_surrounding_cells(coords); |
| 2724 | for (int i = 0; i < around.size(); i++) { |
| 2725 | to_check.push_back(around[i]); |
| 2726 | } |
| 2727 | } |
| 2728 | already_checked.insert(coords); |
| 2729 | } |
| 2730 | } |
| 2731 | } else { |
| 2732 | // Replace all tiles like the source. |
| 2733 | TypedArray<Vector2i> to_check; |
| 2734 | if (source_cell.source_id == TileSet::INVALID_SOURCE) { |
| 2735 | Rect2i rect = edited_layer->get_used_rect(); |
| 2736 | if (!rect.has_area()) { |
| 2737 | rect = Rect2i(p_coords, Vector2i(1, 1)); |
| 2738 | } |
| 2739 | for (int x = boundaries.position.x; x < boundaries.get_end().x; x++) { |
| 2740 | for (int y = boundaries.position.y; y < boundaries.get_end().y; y++) { |
| 2741 | to_check.append(Vector2i(x, y)); |
| 2742 | } |
| 2743 | } |
| 2744 | } else { |
| 2745 | to_check = edited_layer->get_used_cells(); |
| 2746 | } |
| 2747 | for (int i = 0; i < to_check.size(); i++) { |
| 2748 | Vector2i coords = to_check[i]; |
| 2749 | // Get the candidate cell pattern. |
| 2750 | TileSet::TerrainsPattern candidate_pattern; |
| 2751 | if (edited_layer->get_cell_source_id(coords) != TileSet::INVALID_SOURCE) { |
| 2752 | TileData *tile_data = nullptr; |
| 2753 | Ref<TileSetSource> source = tile_set->get_source(edited_layer->get_cell_source_id(coords)); |
| 2754 | Ref<TileSetAtlasSource> atlas_source = source; |
| 2755 | if (atlas_source.is_valid()) { |
| 2756 | tile_data = atlas_source->get_tile_data(edited_layer->get_cell_atlas_coords(coords), edited_layer->get_cell_alternative_tile(coords)); |
| 2757 | } |
| 2758 | if (tile_data) { |
| 2759 | candidate_pattern = tile_data->get_terrains_pattern(); |
| 2760 | } |
| 2761 | } |
| 2762 | |
| 2763 | // Draw. |
| 2764 | if (candidate_pattern == source_pattern && (!source_pattern.is_erase_pattern() || boundaries.has_point(coords))) { |
| 2765 | output.insert(coords); |
| 2766 | } |
| 2767 | } |
| 2768 | } |
| 2769 | return output; |
| 2770 | } |
| 2771 | |
| 2772 | HashMap<Vector2i, TileMapCell> TileMapLayerEditorTerrainsPlugin::_draw_bucket_fill(Vector2i p_coords, bool p_contiguous, bool p_erase) { |
| 2773 | const TileMapLayer *edited_layer = _get_edited_layer(); |
| 2774 | if (!edited_layer) { |
| 2775 | return HashMap<Vector2i, TileMapCell>(); |
| 2776 | } |
| 2777 | |
| 2778 | const Ref<TileSet> &tile_set = edited_layer->get_tile_set(); |
| 2779 | if (tile_set.is_null()) { |
| 2780 | return HashMap<Vector2i, TileMapCell>(); |
| 2781 | } |
| 2782 | |
| 2783 | RBSet<Vector2i> cells_to_draw = _get_cells_for_bucket_fill(p_coords, p_contiguous); |
| 2784 | Vector<Vector2i> cells_to_draw_as_vector; |
| 2785 | for (Vector2i cell : cells_to_draw) { |
| 2786 | cells_to_draw_as_vector.append(cell); |
| 2787 | } |
| 2788 | |
| 2789 | if (p_erase) { |
| 2790 | return _draw_terrain_pattern(cells_to_draw_as_vector, selected_terrain_set, TileSet::TerrainsPattern(*tile_set, selected_terrain_set)); |
| 2791 | } else { |
| 2792 | if (selected_type == SELECTED_TYPE_CONNECT || selected_type == SELECTED_TYPE_PATH) { |
| 2793 | return _draw_terrain_path_or_connect(cells_to_draw_as_vector, selected_terrain_set, selected_terrain, true); |
| 2794 | } else { // SELECTED_TYPE_PATTERN |
| 2795 | return _draw_terrain_pattern(cells_to_draw_as_vector, selected_terrain_set, selected_terrains_pattern); |
| 2796 | } |
| 2797 | } |
| 2798 | } |
| 2799 | |
| 2800 | void TileMapLayerEditorTerrainsPlugin::_stop_dragging() { |
| 2801 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 2802 | if (!edited_layer) { |
| 2803 | return; |
| 2804 | } |
| 2805 | |
| 2806 | const Ref<TileSet> &tile_set = edited_layer->get_tile_set(); |
| 2807 | if (tile_set.is_null()) { |
| 2808 | return; |
| 2809 | } |
| 2810 | |
| 2811 | Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas(); |
| 2812 | Vector2 mpos = xform.affine_inverse().xform(CanvasItemEditor::get_singleton()->get_viewport_control()->get_local_mouse_position()); |
| 2813 | |
| 2814 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 2815 | switch (drag_type) { |
| 2816 | case DRAG_TYPE_PICK: { |
| 2817 | Vector2i coords = tile_set->local_to_map(mpos); |
| 2818 | TileMapCell cell = edited_layer->get_cell(coords); |
| 2819 | TileData *tile_data = nullptr; |
| 2820 | |
| 2821 | Ref<TileSetSource> source = tile_set->get_source(cell.source_id); |
| 2822 | Ref<TileSetAtlasSource> atlas_source = source; |
| 2823 | if (atlas_source.is_valid()) { |
| 2824 | tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile); |
| 2825 | } |
| 2826 | |
| 2827 | if (tile_data) { |
| 2828 | TileSet::TerrainsPattern terrains_pattern = tile_data->get_terrains_pattern(); |
| 2829 | |
| 2830 | // Find the tree item for the right terrain set. |
| 2831 | bool need_tree_item_switch = true; |
| 2832 | TreeItem *tree_item = terrains_tree->get_selected(); |
| 2833 | int new_terrain_set = -1; |
| 2834 | if (tree_item) { |
| 2835 | Dictionary metadata_dict = tree_item->get_metadata(0); |
| 2836 | if (metadata_dict.has("terrain_set") && metadata_dict.has("terrain_id")) { |
| 2837 | int terrain_set = metadata_dict["terrain_set"]; |
| 2838 | int terrain_id = metadata_dict["terrain_id"]; |
| 2839 | if (per_terrain_terrains_patterns[terrain_set][terrain_id].has(terrains_pattern)) { |
| 2840 | new_terrain_set = terrain_set; |
| 2841 | need_tree_item_switch = false; |
| 2842 | } |
| 2843 | } |
| 2844 | } |
| 2845 | |
| 2846 | if (need_tree_item_switch) { |
| 2847 | for (tree_item = terrains_tree->get_root()->get_first_child(); tree_item; tree_item = tree_item->get_next_visible()) { |
| 2848 | Dictionary metadata_dict = tree_item->get_metadata(0); |
| 2849 | if (metadata_dict.has("terrain_set") && metadata_dict.has("terrain_id")) { |
| 2850 | int terrain_set = metadata_dict["terrain_set"]; |
| 2851 | int terrain_id = metadata_dict["terrain_id"]; |
| 2852 | if (per_terrain_terrains_patterns[terrain_set][terrain_id].has(terrains_pattern)) { |
| 2853 | // Found |
| 2854 | new_terrain_set = terrain_set; |
| 2855 | tree_item->select(0); |
| 2856 | _update_tiles_list(); |
| 2857 | break; |
| 2858 | } |
| 2859 | } |
| 2860 | } |
| 2861 | } |
| 2862 | |
| 2863 | // Find the list item for the given tile. |
| 2864 | if (tree_item) { |
| 2865 | for (int i = 0; i < terrains_tile_list->get_item_count(); i++) { |
| 2866 | Dictionary metadata_dict = terrains_tile_list->get_item_metadata(i); |
| 2867 | if (int(metadata_dict["type"]) == SELECTED_TYPE_PATTERN) { |
| 2868 | TileSet::TerrainsPattern in_meta_terrains_pattern(*tile_set, new_terrain_set); |
| 2869 | in_meta_terrains_pattern.from_array(metadata_dict["terrains_pattern"]); |
| 2870 | if (in_meta_terrains_pattern == terrains_pattern) { |
| 2871 | terrains_tile_list->select(i); |
| 2872 | break; |
| 2873 | } |
| 2874 | } |
| 2875 | } |
| 2876 | } else { |
| 2877 | ERR_PRINT("Terrain tile not found."); |
| 2878 | } |
| 2879 | } |
| 2880 | picker_button->set_pressed(false); |
| 2881 | } break; |
| 2882 | case DRAG_TYPE_PAINT: { |
| 2883 | undo_redo->create_action(TTR("Paint terrain")); |
| 2884 | for (const KeyValue<Vector2i, TileMapCell> &E : drag_modified) { |
| 2885 | undo_redo->add_do_method(edited_layer, "set_cell", E.key, edited_layer->get_cell_source_id(E.key), edited_layer->get_cell_atlas_coords(E.key), edited_layer->get_cell_alternative_tile(E.key)); |
| 2886 | undo_redo->add_undo_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile); |
| 2887 | } |
| 2888 | undo_redo->commit_action(false); |
| 2889 | } break; |
| 2890 | case DRAG_TYPE_LINE: { |
| 2891 | HashMap<Vector2i, TileMapCell> to_draw = _draw_line(tile_set->local_to_map(drag_start_mouse_pos), tile_set->local_to_map(mpos), drag_erasing); |
| 2892 | undo_redo->create_action(TTR("Paint terrain")); |
| 2893 | for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) { |
| 2894 | if (!drag_erasing && E.value.source_id == TileSet::INVALID_SOURCE) { |
| 2895 | continue; |
| 2896 | } |
| 2897 | undo_redo->add_do_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile); |
| 2898 | undo_redo->add_undo_method(edited_layer, "set_cell", E.key, edited_layer->get_cell_source_id(E.key), edited_layer->get_cell_atlas_coords(E.key), edited_layer->get_cell_alternative_tile(E.key)); |
| 2899 | } |
| 2900 | undo_redo->commit_action(); |
| 2901 | } break; |
| 2902 | case DRAG_TYPE_RECT: { |
| 2903 | HashMap<Vector2i, TileMapCell> to_draw = _draw_rect(tile_set->local_to_map(drag_start_mouse_pos), tile_set->local_to_map(mpos), drag_erasing); |
| 2904 | undo_redo->create_action(TTR("Paint terrain")); |
| 2905 | for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) { |
| 2906 | if (!drag_erasing && E.value.source_id == TileSet::INVALID_SOURCE) { |
| 2907 | continue; |
| 2908 | } |
| 2909 | undo_redo->add_do_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile); |
| 2910 | undo_redo->add_undo_method(edited_layer, "set_cell", E.key, edited_layer->get_cell_source_id(E.key), edited_layer->get_cell_atlas_coords(E.key), edited_layer->get_cell_alternative_tile(E.key)); |
| 2911 | } |
| 2912 | undo_redo->commit_action(); |
| 2913 | } break; |
| 2914 | case DRAG_TYPE_BUCKET: { |
| 2915 | undo_redo->create_action(TTR("Paint terrain")); |
| 2916 | for (const KeyValue<Vector2i, TileMapCell> &E : drag_modified) { |
| 2917 | undo_redo->add_do_method(edited_layer, "set_cell", E.key, edited_layer->get_cell_source_id(E.key), edited_layer->get_cell_atlas_coords(E.key), edited_layer->get_cell_alternative_tile(E.key)); |
| 2918 | undo_redo->add_undo_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile); |
| 2919 | } |
| 2920 | undo_redo->commit_action(false); |
| 2921 | } break; |
| 2922 | |
| 2923 | default: |
| 2924 | break; |
| 2925 | } |
| 2926 | drag_type = DRAG_TYPE_NONE; |
| 2927 | } |
| 2928 | |
| 2929 | void TileMapLayerEditorTerrainsPlugin::_mouse_exited_viewport() { |
| 2930 | has_mouse = false; |
| 2931 | CanvasItemEditor::get_singleton()->update_viewport(); |
| 2932 | } |
| 2933 | |
| 2934 | void TileMapLayerEditorTerrainsPlugin::_update_selection() { |
| 2935 | const TileMapLayer *edited_layer = _get_edited_layer(); |
| 2936 | if (!edited_layer) { |
| 2937 | return; |
| 2938 | } |
| 2939 | |
| 2940 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 2941 | if (tile_set.is_null()) { |
| 2942 | return; |
| 2943 | } |
| 2944 | |
| 2945 | // Get the selected terrain. |
| 2946 | selected_terrain_set = -1; |
| 2947 | selected_terrains_pattern = TileSet::TerrainsPattern(); |
| 2948 | |
| 2949 | TreeItem *selected_tree_item = terrains_tree->get_selected(); |
| 2950 | if (selected_tree_item && selected_tree_item->get_metadata(0)) { |
| 2951 | Dictionary metadata_dict = selected_tree_item->get_metadata(0); |
| 2952 | // Selected terrain |
| 2953 | selected_terrain_set = metadata_dict["terrain_set"]; |
| 2954 | selected_terrain = metadata_dict["terrain_id"]; |
| 2955 | |
| 2956 | // Selected mode/terrain pattern |
| 2957 | if (erase_button->is_pressed()) { |
| 2958 | selected_type = SELECTED_TYPE_PATTERN; |
| 2959 | selected_terrains_pattern = TileSet::TerrainsPattern(*tile_set, selected_terrain_set); |
| 2960 | } else if (terrains_tile_list->is_anything_selected()) { |
| 2961 | metadata_dict = terrains_tile_list->get_item_metadata(terrains_tile_list->get_selected_items()[0]); |
| 2962 | if (int(metadata_dict["type"]) == SELECTED_TYPE_CONNECT) { |
| 2963 | selected_type = SELECTED_TYPE_CONNECT; |
| 2964 | } else if (int(metadata_dict["type"]) == SELECTED_TYPE_PATH) { |
| 2965 | selected_type = SELECTED_TYPE_PATH; |
| 2966 | } else if (int(metadata_dict["type"]) == SELECTED_TYPE_PATTERN) { |
| 2967 | selected_type = SELECTED_TYPE_PATTERN; |
| 2968 | selected_terrains_pattern = TileSet::TerrainsPattern(*tile_set, selected_terrain_set); |
| 2969 | selected_terrains_pattern.from_array(metadata_dict["terrains_pattern"]); |
| 2970 | } else { |
| 2971 | ERR_FAIL(); |
| 2972 | } |
| 2973 | } |
| 2974 | } |
| 2975 | } |
| 2976 | |
| 2977 | bool TileMapLayerEditorTerrainsPlugin::forward_canvas_gui_input(const Ref<InputEvent> &p_event) { |
| 2978 | if (!main_box_container->is_visible_in_tree()) { |
| 2979 | // If the bottom editor is not visible, we ignore inputs. |
| 2980 | return false; |
| 2981 | } |
| 2982 | |
| 2983 | if (CanvasItemEditor::get_singleton()->get_current_tool() != CanvasItemEditor::TOOL_SELECT) { |
| 2984 | return false; |
| 2985 | } |
| 2986 | |
| 2987 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 2988 | if (!edited_layer) { |
| 2989 | return false; |
| 2990 | } |
| 2991 | |
| 2992 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 2993 | if (tile_set.is_null()) { |
| 2994 | return false; |
| 2995 | } |
| 2996 | |
| 2997 | _update_selection(); |
| 2998 | |
| 2999 | Ref<InputEventKey> k = p_event; |
| 3000 | if (k.is_valid() && k->is_pressed() && !k->is_echo()) { |
| 3001 | for (BaseButton *b : viewport_shortcut_buttons) { |
| 3002 | if (b->get_shortcut().is_valid() && b->get_shortcut()->matches_event(p_event)) { |
| 3003 | b->set_pressed(b->get_button_group().is_valid() || !b->is_pressed()); |
| 3004 | return true; |
| 3005 | } |
| 3006 | } |
| 3007 | } |
| 3008 | |
| 3009 | Ref<InputEventMouseMotion> mm = p_event; |
| 3010 | if (mm.is_valid()) { |
| 3011 | has_mouse = true; |
| 3012 | Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas(); |
| 3013 | Vector2 mpos = xform.affine_inverse().xform(mm->get_position()); |
| 3014 | |
| 3015 | switch (drag_type) { |
| 3016 | case DRAG_TYPE_PAINT: { |
| 3017 | if (selected_terrain_set >= 0) { |
| 3018 | HashMap<Vector2i, TileMapCell> to_draw = _draw_line(tile_set->local_to_map(drag_last_mouse_pos), tile_set->local_to_map(mpos), drag_erasing); |
| 3019 | for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) { |
| 3020 | if (!drag_modified.has(E.key)) { |
| 3021 | drag_modified[E.key] = edited_layer->get_cell(E.key); |
| 3022 | } |
| 3023 | edited_layer->set_cell(E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile); |
| 3024 | } |
| 3025 | } |
| 3026 | } break; |
| 3027 | default: |
| 3028 | break; |
| 3029 | } |
| 3030 | drag_last_mouse_pos = mpos; |
| 3031 | CanvasItemEditor::get_singleton()->update_viewport(); |
| 3032 | |
| 3033 | return true; |
| 3034 | } |
| 3035 | |
| 3036 | Ref<InputEventMouseButton> mb = p_event; |
| 3037 | if (mb.is_valid()) { |
| 3038 | has_mouse = true; |
| 3039 | Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas(); |
| 3040 | Vector2 mpos = xform.affine_inverse().xform(mb->get_position()); |
| 3041 | |
| 3042 | if (mb->get_button_index() == MouseButton::LEFT || mb->get_button_index() == MouseButton::RIGHT) { |
| 3043 | if (mb->is_pressed()) { |
| 3044 | // Pressed |
| 3045 | if (erase_button->is_pressed() || mb->get_button_index() == MouseButton::RIGHT) { |
| 3046 | drag_erasing = true; |
| 3047 | } |
| 3048 | |
| 3049 | if (picker_button->is_pressed()) { |
| 3050 | drag_type = DRAG_TYPE_PICK; |
| 3051 | } else { |
| 3052 | // Paint otherwise. |
| 3053 | if (tool_buttons_group->get_pressed_button() == paint_tool_button && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL) && !Input::get_singleton()->is_key_pressed(Key::SHIFT)) { |
| 3054 | if (selected_terrain_set < 0 || selected_terrain < 0 || (selected_type == SELECTED_TYPE_PATTERN && !selected_terrains_pattern.is_valid())) { |
| 3055 | return true; |
| 3056 | } |
| 3057 | |
| 3058 | drag_type = DRAG_TYPE_PAINT; |
| 3059 | drag_start_mouse_pos = mpos; |
| 3060 | |
| 3061 | drag_modified.clear(); |
| 3062 | Vector2i cell = tile_set->local_to_map(mpos); |
| 3063 | HashMap<Vector2i, TileMapCell> to_draw = _draw_line(cell, cell, drag_erasing); |
| 3064 | for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) { |
| 3065 | drag_modified[E.key] = edited_layer->get_cell(E.key); |
| 3066 | edited_layer->set_cell(E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile); |
| 3067 | } |
| 3068 | } else if (tool_buttons_group->get_pressed_button() == line_tool_button || (tool_buttons_group->get_pressed_button() == paint_tool_button && Input::get_singleton()->is_key_pressed(Key::SHIFT) && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL))) { |
| 3069 | if (selected_terrain_set < 0 || selected_terrain < 0 || (selected_type == SELECTED_TYPE_PATTERN && !selected_terrains_pattern.is_valid())) { |
| 3070 | return true; |
| 3071 | } |
| 3072 | drag_type = DRAG_TYPE_LINE; |
| 3073 | drag_start_mouse_pos = mpos; |
| 3074 | drag_modified.clear(); |
| 3075 | } else if (tool_buttons_group->get_pressed_button() == rect_tool_button || (tool_buttons_group->get_pressed_button() == paint_tool_button && Input::get_singleton()->is_key_pressed(Key::SHIFT) && Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL))) { |
| 3076 | if (selected_terrain_set < 0 || selected_terrain < 0 || (selected_type == SELECTED_TYPE_PATTERN && !selected_terrains_pattern.is_valid())) { |
| 3077 | return true; |
| 3078 | } |
| 3079 | drag_type = DRAG_TYPE_RECT; |
| 3080 | drag_start_mouse_pos = mpos; |
| 3081 | drag_modified.clear(); |
| 3082 | } else if (tool_buttons_group->get_pressed_button() == bucket_tool_button) { |
| 3083 | if (selected_terrain_set < 0 || selected_terrain < 0 || (selected_type == SELECTED_TYPE_PATTERN && !selected_terrains_pattern.is_valid())) { |
| 3084 | return true; |
| 3085 | } |
| 3086 | drag_type = DRAG_TYPE_BUCKET; |
| 3087 | drag_start_mouse_pos = mpos; |
| 3088 | drag_modified.clear(); |
| 3089 | Vector<Vector2i> line = TileMapLayerEditor::get_line(edited_layer, tile_set->local_to_map(drag_last_mouse_pos), tile_set->local_to_map(mpos)); |
| 3090 | for (int i = 0; i < line.size(); i++) { |
| 3091 | if (!drag_modified.has(line[i])) { |
| 3092 | HashMap<Vector2i, TileMapCell> to_draw = _draw_bucket_fill(line[i], bucket_contiguous_checkbox->is_pressed(), drag_erasing); |
| 3093 | for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) { |
| 3094 | if (!drag_erasing && E.value.source_id == TileSet::INVALID_SOURCE) { |
| 3095 | continue; |
| 3096 | } |
| 3097 | Vector2i coords = E.key; |
| 3098 | if (!drag_modified.has(coords)) { |
| 3099 | drag_modified.insert(coords, edited_layer->get_cell(coords)); |
| 3100 | } |
| 3101 | edited_layer->set_cell(coords, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile); |
| 3102 | } |
| 3103 | } |
| 3104 | } |
| 3105 | } |
| 3106 | } |
| 3107 | } else { |
| 3108 | // Released |
| 3109 | _stop_dragging(); |
| 3110 | drag_erasing = false; |
| 3111 | } |
| 3112 | |
| 3113 | CanvasItemEditor::get_singleton()->update_viewport(); |
| 3114 | |
| 3115 | return true; |
| 3116 | } |
| 3117 | drag_last_mouse_pos = mpos; |
| 3118 | } |
| 3119 | |
| 3120 | return false; |
| 3121 | } |
| 3122 | |
| 3123 | void TileMapLayerEditorTerrainsPlugin::forward_canvas_draw_over_viewport(Control *p_overlay) { |
| 3124 | const TileMapLayer *edited_layer = _get_edited_layer(); |
| 3125 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 3126 | |
| 3127 | Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas(); |
| 3128 | Vector2 mpos = edited_layer->get_local_mouse_position(); |
| 3129 | Vector2i tile_shape_size = tile_set->get_tile_size(); |
| 3130 | bool drawing_rect = false; |
| 3131 | |
| 3132 | // Handle the preview of the tiles to be placed. |
| 3133 | if (main_box_container->is_visible_in_tree() && has_mouse) { // Only if the tilemap editor is opened and the viewport is hovered. |
| 3134 | RBSet<Vector2i> preview; |
| 3135 | Rect2i drawn_grid_rect; |
| 3136 | |
| 3137 | if (drag_type == DRAG_TYPE_PICK) { |
| 3138 | // Draw the area being picked. |
| 3139 | Vector2i coords = tile_set->local_to_map(mpos); |
| 3140 | if (edited_layer->get_cell_source_id(coords) != TileSet::INVALID_SOURCE) { |
| 3141 | Transform2D tile_xform; |
| 3142 | tile_xform.set_origin(tile_set->map_to_local(coords)); |
| 3143 | tile_xform.set_scale(tile_shape_size); |
| 3144 | tile_set->draw_tile_shape(p_overlay, xform * tile_xform, Color(1.0, 1.0, 1.0), false); |
| 3145 | } |
| 3146 | } else if (!picker_button->is_pressed() && !(drag_type == DRAG_TYPE_NONE && Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL) && !Input::get_singleton()->is_key_pressed(Key::SHIFT))) { |
| 3147 | bool expand_grid = false; |
| 3148 | if (tool_buttons_group->get_pressed_button() == paint_tool_button && drag_type == DRAG_TYPE_NONE) { |
| 3149 | // Preview for a single tile. |
| 3150 | preview.insert(tile_set->local_to_map(mpos)); |
| 3151 | expand_grid = true; |
| 3152 | } else if (tool_buttons_group->get_pressed_button() == line_tool_button || drag_type == DRAG_TYPE_LINE) { |
| 3153 | if (drag_type == DRAG_TYPE_NONE) { |
| 3154 | // Preview for a single tile. |
| 3155 | preview.insert(tile_set->local_to_map(mpos)); |
| 3156 | } else if (drag_type == DRAG_TYPE_LINE) { |
| 3157 | // Preview for a line. |
| 3158 | Vector<Vector2i> line = TileMapLayerEditor::get_line(edited_layer, tile_set->local_to_map(drag_start_mouse_pos), tile_set->local_to_map(mpos)); |
| 3159 | for (int i = 0; i < line.size(); i++) { |
| 3160 | preview.insert(line[i]); |
| 3161 | } |
| 3162 | expand_grid = true; |
| 3163 | } |
| 3164 | } else if (drag_type == DRAG_TYPE_RECT) { |
| 3165 | // Preview for a rect. |
| 3166 | Rect2i rect; |
| 3167 | rect.set_position(tile_set->local_to_map(drag_start_mouse_pos)); |
| 3168 | rect.set_end(tile_set->local_to_map(mpos)); |
| 3169 | rect = rect.abs(); |
| 3170 | |
| 3171 | HashMap<Vector2i, TileSet::TerrainsPattern> to_draw; |
| 3172 | for (int x = rect.position.x; x <= rect.get_end().x; x++) { |
| 3173 | for (int y = rect.position.y; y <= rect.get_end().y; y++) { |
| 3174 | preview.insert(Vector2i(x, y)); |
| 3175 | } |
| 3176 | } |
| 3177 | |
| 3178 | drawing_rect = !preview.is_empty(); |
| 3179 | expand_grid = true; |
| 3180 | } else if (tool_buttons_group->get_pressed_button() == bucket_tool_button && drag_type == DRAG_TYPE_NONE) { |
| 3181 | // Preview for a fill. |
| 3182 | preview = _get_cells_for_bucket_fill(tile_set->local_to_map(mpos), bucket_contiguous_checkbox->is_pressed()); |
| 3183 | } |
| 3184 | |
| 3185 | // Expand the grid if needed |
| 3186 | if (expand_grid && !preview.is_empty()) { |
| 3187 | drawn_grid_rect = Rect2i(preview.front()->get(), Vector2i(1, 1)); |
| 3188 | for (const Vector2i &E : preview) { |
| 3189 | drawn_grid_rect.expand_to(E); |
| 3190 | } |
| 3191 | } |
| 3192 | } |
| 3193 | |
| 3194 | if (!preview.is_empty()) { |
| 3195 | const int fading = 5; |
| 3196 | |
| 3197 | // Draw the lines of the grid behind the preview. |
| 3198 | bool display_grid = EDITOR_GET("editors/tiles_editor/display_grid"); |
| 3199 | if (display_grid) { |
| 3200 | Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color"); |
| 3201 | if (drawn_grid_rect.size.x > 0 && drawn_grid_rect.size.y > 0) { |
| 3202 | drawn_grid_rect = drawn_grid_rect.grow(fading); |
| 3203 | for (int x = drawn_grid_rect.position.x; x < (drawn_grid_rect.position.x + drawn_grid_rect.size.x); x++) { |
| 3204 | for (int y = drawn_grid_rect.position.y; y < (drawn_grid_rect.position.y + drawn_grid_rect.size.y); y++) { |
| 3205 | Vector2i pos_in_rect = Vector2i(x, y) - drawn_grid_rect.position; |
| 3206 | |
| 3207 | // Fade out the border of the grid. |
| 3208 | float left_opacity = CLAMP(Math::inverse_lerp(0.0f, (float)fading, (float)pos_in_rect.x), 0.0f, 1.0f); |
| 3209 | float right_opacity = CLAMP(Math::inverse_lerp((float)drawn_grid_rect.size.x, (float)(drawn_grid_rect.size.x - fading), (float)(pos_in_rect.x + 1)), 0.0f, 1.0f); |
| 3210 | float top_opacity = CLAMP(Math::inverse_lerp(0.0f, (float)fading, (float)pos_in_rect.y), 0.0f, 1.0f); |
| 3211 | float bottom_opacity = CLAMP(Math::inverse_lerp((float)drawn_grid_rect.size.y, (float)(drawn_grid_rect.size.y - fading), (float)(pos_in_rect.y + 1)), 0.0f, 1.0f); |
| 3212 | float opacity = CLAMP(MIN(left_opacity, MIN(right_opacity, MIN(top_opacity, bottom_opacity))) + 0.1, 0.0f, 1.0f); |
| 3213 | |
| 3214 | Transform2D tile_xform; |
| 3215 | tile_xform.set_origin(tile_set->map_to_local(Vector2(x, y))); |
| 3216 | tile_xform.set_scale(tile_shape_size); |
| 3217 | Color color = grid_color; |
| 3218 | color.a = color.a * opacity; |
| 3219 | tile_set->draw_tile_shape(p_overlay, xform * tile_xform, color, false); |
| 3220 | } |
| 3221 | } |
| 3222 | } |
| 3223 | } |
| 3224 | |
| 3225 | // Draw the preview. |
| 3226 | for (const Vector2i &E : preview) { |
| 3227 | Transform2D tile_xform; |
| 3228 | tile_xform.set_origin(tile_set->map_to_local(E)); |
| 3229 | tile_xform.set_scale(tile_set->get_tile_size()); |
| 3230 | if (drag_erasing || erase_button->is_pressed()) { |
| 3231 | tile_set->draw_tile_shape(p_overlay, xform * tile_xform, Color(0.0, 0.0, 0.0, 0.5), true); |
| 3232 | } else { |
| 3233 | tile_set->draw_tile_shape(p_overlay, xform * tile_xform, Color(1.0, 1.0, 1.0, 0.5), true); |
| 3234 | } |
| 3235 | } |
| 3236 | } |
| 3237 | |
| 3238 | draw_tile_coords_over_viewport(p_overlay, edited_layer, tile_set, drawing_rect, drag_start_mouse_pos); |
| 3239 | } |
| 3240 | } |
| 3241 | |
| 3242 | void TileMapLayerEditorTerrainsPlugin::_update_terrains_cache() { |
| 3243 | const TileMapLayer *edited_layer = _get_edited_layer(); |
| 3244 | if (!edited_layer) { |
| 3245 | return; |
| 3246 | } |
| 3247 | |
| 3248 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 3249 | if (tile_set.is_null()) { |
| 3250 | return; |
| 3251 | } |
| 3252 | |
| 3253 | // Organizes tiles into structures. |
| 3254 | per_terrain_terrains_patterns.resize(tile_set->get_terrain_sets_count()); |
| 3255 | for (int i = 0; i < tile_set->get_terrain_sets_count(); i++) { |
| 3256 | per_terrain_terrains_patterns[i].resize(tile_set->get_terrains_count(i)); |
| 3257 | for (RBSet<TileSet::TerrainsPattern> &pattern : per_terrain_terrains_patterns[i]) { |
| 3258 | pattern.clear(); |
| 3259 | } |
| 3260 | } |
| 3261 | |
| 3262 | for (int source_index = 0; source_index < tile_set->get_source_count(); source_index++) { |
| 3263 | int source_id = tile_set->get_source_id(source_index); |
| 3264 | Ref<TileSetSource> source = tile_set->get_source(source_id); |
| 3265 | |
| 3266 | Ref<TileSetAtlasSource> atlas_source = source; |
| 3267 | if (atlas_source.is_valid()) { |
| 3268 | for (int tile_index = 0; tile_index < source->get_tiles_count(); tile_index++) { |
| 3269 | Vector2i tile_id = source->get_tile_id(tile_index); |
| 3270 | for (int alternative_index = 0; alternative_index < source->get_alternative_tiles_count(tile_id); alternative_index++) { |
| 3271 | int alternative_id = source->get_alternative_tile_id(tile_id, alternative_index); |
| 3272 | |
| 3273 | TileData *tile_data = atlas_source->get_tile_data(tile_id, alternative_id); |
| 3274 | int terrain_set = tile_data->get_terrain_set(); |
| 3275 | if (terrain_set >= 0) { |
| 3276 | ERR_FAIL_INDEX(terrain_set, (int)per_terrain_terrains_patterns.size()); |
| 3277 | |
| 3278 | TileMapCell cell; |
| 3279 | cell.source_id = source_id; |
| 3280 | cell.set_atlas_coords(tile_id); |
| 3281 | cell.alternative_tile = alternative_id; |
| 3282 | |
| 3283 | TileSet::TerrainsPattern terrains_pattern = tile_data->get_terrains_pattern(); |
| 3284 | |
| 3285 | // Terrain center bit |
| 3286 | int terrain = terrains_pattern.get_terrain(); |
| 3287 | if (terrain >= 0 && terrain < (int)per_terrain_terrains_patterns[terrain_set].size()) { |
| 3288 | per_terrain_terrains_patterns[terrain_set][terrain].insert(terrains_pattern); |
| 3289 | } |
| 3290 | |
| 3291 | // Terrain bits. |
| 3292 | for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) { |
| 3293 | TileSet::CellNeighbor bit = TileSet::CellNeighbor(i); |
| 3294 | if (tile_set->is_valid_terrain_peering_bit(terrain_set, bit)) { |
| 3295 | terrain = terrains_pattern.get_terrain_peering_bit(bit); |
| 3296 | if (terrain >= 0 && terrain < (int)per_terrain_terrains_patterns[terrain_set].size()) { |
| 3297 | per_terrain_terrains_patterns[terrain_set][terrain].insert(terrains_pattern); |
| 3298 | } |
| 3299 | } |
| 3300 | } |
| 3301 | } |
| 3302 | } |
| 3303 | } |
| 3304 | } |
| 3305 | } |
| 3306 | } |
| 3307 | |
| 3308 | void TileMapLayerEditorTerrainsPlugin::_update_terrains_tree() { |
| 3309 | terrains_tree->clear(); |
| 3310 | terrains_tree->create_item(); |
| 3311 | |
| 3312 | const TileMapLayer *edited_layer = _get_edited_layer(); |
| 3313 | if (!edited_layer) { |
| 3314 | return; |
| 3315 | } |
| 3316 | |
| 3317 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 3318 | if (tile_set.is_null()) { |
| 3319 | return; |
| 3320 | } |
| 3321 | |
| 3322 | // Fill in the terrain list. |
| 3323 | Vector<Vector<Ref<Texture2D>>> icons = tile_set->generate_terrains_icons(Size2(16, 16) * EDSCALE); |
| 3324 | for (int terrain_set_index = 0; terrain_set_index < tile_set->get_terrain_sets_count(); terrain_set_index++) { |
| 3325 | // Add an item for the terrain set. |
| 3326 | TreeItem *terrain_set_tree_item = terrains_tree->create_item(); |
| 3327 | String matches; |
| 3328 | if (tile_set->get_terrain_set_mode(terrain_set_index) == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES) { |
| 3329 | terrain_set_tree_item->set_icon(0, main_box_container->get_editor_theme_icon(SNAME("TerrainMatchCornersAndSides"))); |
| 3330 | matches = String(TTR("Matches Corners and Sides")); |
| 3331 | } else if (tile_set->get_terrain_set_mode(terrain_set_index) == TileSet::TERRAIN_MODE_MATCH_CORNERS) { |
| 3332 | terrain_set_tree_item->set_icon(0, main_box_container->get_editor_theme_icon(SNAME("TerrainMatchCorners"))); |
| 3333 | matches = String(TTR("Matches Corners Only")); |
| 3334 | } else { |
| 3335 | terrain_set_tree_item->set_icon(0, main_box_container->get_editor_theme_icon(SNAME("TerrainMatchSides"))); |
| 3336 | matches = String(TTR("Matches Sides Only")); |
| 3337 | } |
| 3338 | terrain_set_tree_item->set_text(0, vformat(TTR("Terrain Set %d (%s)"), terrain_set_index, matches)); |
| 3339 | terrain_set_tree_item->set_selectable(0, false); |
| 3340 | |
| 3341 | for (int terrain_index = 0; terrain_index < tile_set->get_terrains_count(terrain_set_index); terrain_index++) { |
| 3342 | // Add the item to the terrain list. |
| 3343 | TreeItem *terrain_tree_item = terrains_tree->create_item(terrain_set_tree_item); |
| 3344 | terrain_tree_item->set_text(0, tile_set->get_terrain_name(terrain_set_index, terrain_index)); |
| 3345 | terrain_tree_item->set_icon_max_width(0, 32 * EDSCALE); |
| 3346 | terrain_tree_item->set_icon(0, icons[terrain_set_index][terrain_index]); |
| 3347 | |
| 3348 | Dictionary metadata_dict; |
| 3349 | metadata_dict["terrain_set"] = terrain_set_index; |
| 3350 | metadata_dict["terrain_id"] = terrain_index; |
| 3351 | terrain_tree_item->set_metadata(0, metadata_dict); |
| 3352 | } |
| 3353 | } |
| 3354 | } |
| 3355 | |
| 3356 | void TileMapLayerEditorTerrainsPlugin::_update_tiles_list() { |
| 3357 | terrains_tile_list->clear(); |
| 3358 | |
| 3359 | const TileMapLayer *edited_layer = _get_edited_layer(); |
| 3360 | if (!edited_layer) { |
| 3361 | return; |
| 3362 | } |
| 3363 | |
| 3364 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 3365 | if (tile_set.is_null()) { |
| 3366 | return; |
| 3367 | } |
| 3368 | |
| 3369 | TreeItem *selected_tree_item = terrains_tree->get_selected(); |
| 3370 | if (selected_tree_item && selected_tree_item->get_metadata(0)) { |
| 3371 | Dictionary metadata_dict = selected_tree_item->get_metadata(0); |
| 3372 | int sel_terrain_set = metadata_dict["terrain_set"]; |
| 3373 | int sel_terrain_id = metadata_dict["terrain_id"]; |
| 3374 | ERR_FAIL_INDEX(sel_terrain_set, tile_set->get_terrain_sets_count()); |
| 3375 | ERR_FAIL_INDEX(sel_terrain_id, tile_set->get_terrains_count(sel_terrain_set)); |
| 3376 | |
| 3377 | // Add the two first generic modes |
| 3378 | int item_index = terrains_tile_list->add_icon_item(main_box_container->get_editor_theme_icon(SNAME("TerrainConnect"))); |
| 3379 | terrains_tile_list->set_item_tooltip(item_index, TTR("Connect mode: paints a terrain, then connects it with the surrounding tiles with the same terrain.")); |
| 3380 | Dictionary list_metadata_dict; |
| 3381 | list_metadata_dict["type"] = SELECTED_TYPE_CONNECT; |
| 3382 | terrains_tile_list->set_item_metadata(item_index, list_metadata_dict); |
| 3383 | |
| 3384 | item_index = terrains_tile_list->add_icon_item(main_box_container->get_editor_theme_icon(SNAME("TerrainPath"))); |
| 3385 | terrains_tile_list->set_item_tooltip(item_index, TTR("Path mode: paints a terrain, then connects it to the previous tile painted within the same stroke.")); |
| 3386 | list_metadata_dict = Dictionary(); |
| 3387 | list_metadata_dict["type"] = SELECTED_TYPE_PATH; |
| 3388 | terrains_tile_list->set_item_metadata(item_index, list_metadata_dict); |
| 3389 | |
| 3390 | // Sort the items in a map by the number of corresponding terrains. |
| 3391 | RBMap<int, RBSet<TileSet::TerrainsPattern>> sorted; |
| 3392 | |
| 3393 | for (const TileSet::TerrainsPattern &E : per_terrain_terrains_patterns[sel_terrain_set][sel_terrain_id]) { |
| 3394 | // Count the number of matching sides/terrains. |
| 3395 | int count = 0; |
| 3396 | |
| 3397 | for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) { |
| 3398 | TileSet::CellNeighbor bit = TileSet::CellNeighbor(i); |
| 3399 | if (tile_set->is_valid_terrain_peering_bit(sel_terrain_set, bit) && E.get_terrain_peering_bit(bit) == sel_terrain_id) { |
| 3400 | count++; |
| 3401 | } |
| 3402 | } |
| 3403 | sorted[count].insert(E); |
| 3404 | } |
| 3405 | |
| 3406 | for (RBMap<int, RBSet<TileSet::TerrainsPattern>>::Element *E_set = sorted.back(); E_set; E_set = E_set->prev()) { |
| 3407 | for (const TileSet::TerrainsPattern &E : E_set->get()) { |
| 3408 | TileSet::TerrainsPattern terrains_pattern = E; |
| 3409 | |
| 3410 | // Get the icon. |
| 3411 | Ref<Texture2D> icon; |
| 3412 | Rect2 region; |
| 3413 | bool transpose = false; |
| 3414 | |
| 3415 | double max_probability = -1.0; |
| 3416 | for (const TileMapCell &cell : tile_set->get_tiles_for_terrains_pattern(sel_terrain_set, terrains_pattern)) { |
| 3417 | Ref<TileSetSource> source = tile_set->get_source(cell.source_id); |
| 3418 | |
| 3419 | Ref<TileSetAtlasSource> atlas_source = source; |
| 3420 | if (atlas_source.is_valid()) { |
| 3421 | TileData *tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile); |
| 3422 | if (tile_data->get_probability() > max_probability) { |
| 3423 | icon = atlas_source->get_texture(); |
| 3424 | region = atlas_source->get_tile_texture_region(cell.get_atlas_coords()); |
| 3425 | if (tile_data->get_flip_h()) { |
| 3426 | region.size.x = -region.size.x; |
| 3427 | } |
| 3428 | if (tile_data->get_flip_v()) { |
| 3429 | region.size.y = -region.size.y; |
| 3430 | } |
| 3431 | transpose = tile_data->get_transpose(); |
| 3432 | max_probability = tile_data->get_probability(); |
| 3433 | } |
| 3434 | } |
| 3435 | } |
| 3436 | |
| 3437 | // Create the ItemList's item. |
| 3438 | item_index = terrains_tile_list->add_item(""); |
| 3439 | terrains_tile_list->set_item_icon(item_index, icon); |
| 3440 | terrains_tile_list->set_item_icon_region(item_index, region); |
| 3441 | terrains_tile_list->set_item_icon_transposed(item_index, transpose); |
| 3442 | list_metadata_dict = Dictionary(); |
| 3443 | list_metadata_dict["type"] = SELECTED_TYPE_PATTERN; |
| 3444 | list_metadata_dict["terrains_pattern"] = terrains_pattern.as_array(); |
| 3445 | terrains_tile_list->set_item_metadata(item_index, list_metadata_dict); |
| 3446 | } |
| 3447 | } |
| 3448 | if (terrains_tile_list->get_item_count() > 0) { |
| 3449 | terrains_tile_list->select(0); |
| 3450 | } |
| 3451 | } |
| 3452 | } |
| 3453 | |
| 3454 | void TileMapLayerEditorTerrainsPlugin::_update_theme() { |
| 3455 | paint_tool_button->set_button_icon(main_box_container->get_editor_theme_icon(SNAME("Edit"))); |
| 3456 | line_tool_button->set_button_icon(main_box_container->get_editor_theme_icon(SNAME("Line"))); |
| 3457 | rect_tool_button->set_button_icon(main_box_container->get_editor_theme_icon(SNAME("Rectangle"))); |
| 3458 | bucket_tool_button->set_button_icon(main_box_container->get_editor_theme_icon(SNAME("Bucket"))); |
| 3459 | |
| 3460 | picker_button->set_button_icon(main_box_container->get_editor_theme_icon(SNAME("ColorPick"))); |
| 3461 | erase_button->set_button_icon(main_box_container->get_editor_theme_icon(SNAME("Eraser"))); |
| 3462 | |
| 3463 | _update_tiles_list(); |
| 3464 | } |
| 3465 | |
| 3466 | void TileMapLayerEditorTerrainsPlugin::edit(ObjectID p_edited_tile_map_layer_id) { |
| 3467 | _stop_dragging(); // Avoids staying in a wrong drag state. |
| 3468 | |
| 3469 | if (edited_tile_map_layer_id != p_edited_tile_map_layer_id) { |
| 3470 | edited_tile_map_layer_id = p_edited_tile_map_layer_id; |
| 3471 | |
| 3472 | // Clear the selection. |
| 3473 | _update_terrains_cache(); |
| 3474 | _update_terrains_tree(); |
| 3475 | _update_tiles_list(); |
| 3476 | } |
| 3477 | } |
| 3478 | |
| 3479 | void TileMapLayerEditorTerrainsPlugin::update_layout(EditorDock::DockLayout p_layout) { |
| 3480 | bool is_vertical = (p_layout == EditorDock::DockLayout::DOCK_LAYOUT_VERTICAL); |
| 3481 | // Main Panel. |
| 3482 | main_box_container->set_vertical(is_vertical); |
| 3483 | tilemap_tab_terrains->move_child(terrains_tree, is_vertical ? 1 : 0); |
| 3484 | tilemap_tab_terrains->set_vertical(is_vertical); |
| 3485 | |
| 3486 | // Toolbar. |
| 3487 | tilemap_tiles_tools_buttons->set_vertical(is_vertical); |
| 3488 | tools_settings->set_vertical(is_vertical); |
| 3489 | tools_settings_vsep->set_vertical(is_vertical); |
| 3490 | |
| 3491 | wide_toolbar->set_visible(is_vertical); |
| 3492 | bucket_contiguous_checkbox->reparent(is_vertical ? wide_toolbar : tools_settings); |
| 3493 | } |
| 3494 | |
| 3495 | TileMapLayerEditorTerrainsPlugin::TileMapLayerEditorTerrainsPlugin() { |
| 3496 | wide_toolbar = memnew(HBoxContainer); |
| 3497 | main_box_container = memnew(BoxContainer); |
| 3498 | main_box_container->set_vertical(true); |
| 3499 | // FIXME: This can trigger theme updates when the nodes that we want to update are not yet available. |
| 3500 | // The toolbar should be extracted to a dedicated control and theme updates should be handled through |
| 3501 | // the notification. |
| 3502 | main_box_container->connect(SceneStringName(theme_changed), callable_mp(this, &TileMapLayerEditorTerrainsPlugin::_update_theme)); |
| 3503 | main_box_container->set_name(TTRC("Terrains")); |
| 3504 | |
| 3505 | tilemap_tab_terrains = memnew(SplitContainer); |
| 3506 | tilemap_tab_terrains->set_vertical(false); |
| 3507 | tilemap_tab_terrains->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 3508 | tilemap_tab_terrains->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 3509 | main_box_container->add_child(tilemap_tab_terrains); |
| 3510 | |
| 3511 | terrains_tree = memnew(Tree); |
| 3512 | terrains_tree->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 3513 | terrains_tree->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 3514 | terrains_tree->set_stretch_ratio(0.25); |
| 3515 | terrains_tree->set_custom_minimum_size(Size2(70, 0) * EDSCALE); |
| 3516 | terrains_tree->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST); |
| 3517 | terrains_tree->set_hide_root(true); |
| 3518 | terrains_tree->set_theme_type_variation("TreeSecondary"); |
| 3519 | terrains_tree->connect(SceneStringName(item_selected), callable_mp(this, &TileMapLayerEditorTerrainsPlugin::_update_tiles_list)); |
| 3520 | tilemap_tab_terrains->add_child(terrains_tree); |
| 3521 | |
| 3522 | terrains_tile_list = memnew(ItemList); |
| 3523 | terrains_tile_list->set_auto_translate_mode(Node::AUTO_TRANSLATE_MODE_DISABLED); |
| 3524 | terrains_tile_list->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 3525 | terrains_tile_list->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 3526 | terrains_tile_list->set_max_columns(0); |
| 3527 | terrains_tile_list->set_same_column_width(true); |
| 3528 | terrains_tile_list->set_fixed_icon_size(Size2(32, 32) * EDSCALE); |
| 3529 | terrains_tile_list->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST); |
| 3530 | terrains_tile_list->set_theme_type_variation("ItemListSecondary"); |
| 3531 | tilemap_tab_terrains->add_child(terrains_tile_list); |
| 3532 | |
| 3533 | // --- Toolbar --- |
| 3534 | tilemap_tiles_tools_buttons = memnew(BoxContainer); |
| 3535 | tool_buttons_group.instantiate(); |
| 3536 | |
| 3537 | paint_tool_button = memnew(Button); |
| 3538 | paint_tool_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 3539 | paint_tool_button->set_toggle_mode(true); |
| 3540 | paint_tool_button->set_button_group(tool_buttons_group); |
| 3541 | paint_tool_button->set_pressed(true); |
| 3542 | paint_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/paint_tool")); |
| 3543 | paint_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTerrainsPlugin::_update_toolbar)); |
| 3544 | paint_tool_button->set_accessibility_name(TTRC("Paint Tool")); |
| 3545 | tilemap_tiles_tools_buttons->add_child(paint_tool_button); |
| 3546 | viewport_shortcut_buttons.push_back(paint_tool_button); |
| 3547 | |
| 3548 | line_tool_button = memnew(Button); |
| 3549 | line_tool_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 3550 | line_tool_button->set_toggle_mode(true); |
| 3551 | line_tool_button->set_button_group(tool_buttons_group); |
| 3552 | line_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/line_tool")); |
| 3553 | line_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTerrainsPlugin::_update_toolbar)); |
| 3554 | line_tool_button->set_accessibility_name(TTRC("Line Tool")); |
| 3555 | tilemap_tiles_tools_buttons->add_child(line_tool_button); |
| 3556 | viewport_shortcut_buttons.push_back(line_tool_button); |
| 3557 | |
| 3558 | rect_tool_button = memnew(Button); |
| 3559 | rect_tool_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 3560 | rect_tool_button->set_toggle_mode(true); |
| 3561 | rect_tool_button->set_button_group(tool_buttons_group); |
| 3562 | rect_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/rect_tool")); |
| 3563 | rect_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTerrainsPlugin::_update_toolbar)); |
| 3564 | rect_tool_button->set_accessibility_name(TTRC("Rect Tool")); |
| 3565 | tilemap_tiles_tools_buttons->add_child(rect_tool_button); |
| 3566 | viewport_shortcut_buttons.push_back(rect_tool_button); |
| 3567 | |
| 3568 | bucket_tool_button = memnew(Button); |
| 3569 | bucket_tool_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 3570 | bucket_tool_button->set_toggle_mode(true); |
| 3571 | bucket_tool_button->set_button_group(tool_buttons_group); |
| 3572 | bucket_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/bucket_tool")); |
| 3573 | bucket_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTerrainsPlugin::_update_toolbar)); |
| 3574 | bucket_tool_button->set_accessibility_name(TTRC("Bucket Tool")); |
| 3575 | tilemap_tiles_tools_buttons->add_child(bucket_tool_button); |
| 3576 | viewport_shortcut_buttons.push_back(bucket_tool_button); |
| 3577 | |
| 3578 | // -- TileMap tool settings -- |
| 3579 | tools_settings = memnew(BoxContainer); |
| 3580 | |
| 3581 | tools_settings_vsep = memnew(SwitchSeparator); |
| 3582 | tools_settings_vsep->set_vertical(false); |
| 3583 | tools_settings->add_child(tools_settings_vsep); |
| 3584 | |
| 3585 | // Picker |
| 3586 | picker_button = memnew(Button); |
| 3587 | picker_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 3588 | picker_button->set_toggle_mode(true); |
| 3589 | picker_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/picker")); |
| 3590 | picker_button->connect(SceneStringName(pressed), callable_mp(CanvasItemEditor::get_singleton(), &CanvasItemEditor::update_viewport)); |
| 3591 | picker_button->set_accessibility_name(TTRC("Pick")); |
| 3592 | tools_settings->add_child(picker_button); |
| 3593 | viewport_shortcut_buttons.push_back(picker_button); |
| 3594 | |
| 3595 | // Erase button. |
| 3596 | erase_button = memnew(Button); |
| 3597 | erase_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 3598 | erase_button->set_toggle_mode(true); |
| 3599 | erase_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/eraser")); |
| 3600 | erase_button->connect(SceneStringName(pressed), callable_mp(CanvasItemEditor::get_singleton(), &CanvasItemEditor::update_viewport)); |
| 3601 | erase_button->set_accessibility_name(TTRC("Erase")); |
| 3602 | tools_settings->add_child(erase_button); |
| 3603 | viewport_shortcut_buttons.push_back(erase_button); |
| 3604 | |
| 3605 | // Continuous checkbox. |
| 3606 | bucket_contiguous_checkbox = memnew(CheckBox); |
| 3607 | bucket_contiguous_checkbox->set_flat(true); |
| 3608 | bucket_contiguous_checkbox->set_text(TTR("Contiguous")); |
| 3609 | bucket_contiguous_checkbox->set_pressed(true); |
| 3610 | bucket_contiguous_checkbox->hide(); |
| 3611 | tools_settings->add_child(bucket_contiguous_checkbox); |
| 3612 | } |
| 3613 | |
| 3614 | TileMapLayer *TileMapLayerEditor::_get_edited_layer() const { |
| 3615 | return ObjectDB::get_instance<TileMapLayer>(edited_tile_map_layer_id); |
| 3616 | } |
| 3617 | |
| 3618 | void TileMapLayerEditor::_find_tile_map_layers_in_scene(Node *p_current, const Node *p_owner, Vector<TileMapLayer *> &r_list) const { |
| 3619 | ERR_FAIL_COND(!p_current || !p_owner); |
| 3620 | |
| 3621 | if (p_current != p_owner) { |
| 3622 | if (!p_current->get_owner()) { |
| 3623 | return; |
| 3624 | } |
| 3625 | if (p_current->get_owner() != p_owner && !p_owner->is_editable_instance(p_current->get_owner())) { |
| 3626 | return; |
| 3627 | } |
| 3628 | } |
| 3629 | |
| 3630 | TileMapLayer *layer = Object::cast_to<TileMapLayer>(p_current); |
| 3631 | if (layer) { |
| 3632 | r_list.append(layer); |
| 3633 | } |
| 3634 | for (int i = 0; i < p_current->get_child_count(); i++) { |
| 3635 | Node *child = p_current->get_child(i); |
| 3636 | _find_tile_map_layers_in_scene(child, p_owner, r_list); |
| 3637 | } |
| 3638 | } |
| 3639 | |
| 3640 | void TileMapLayerEditor::_update_tile_map_layers_in_scene_list_cache() { |
| 3641 | if (!layers_in_scene_list_cache_needs_update) { |
| 3642 | return; |
| 3643 | } |
| 3644 | EditorNode *en = EditorNode::get_singleton(); |
| 3645 | Node *edited_scene_root = en->get_edited_scene(); |
| 3646 | if (!edited_scene_root) { |
| 3647 | return; |
| 3648 | } |
| 3649 | |
| 3650 | tile_map_layers_in_scene_cache.clear(); |
| 3651 | _find_tile_map_layers_in_scene(edited_scene_root, edited_scene_root, tile_map_layers_in_scene_cache); |
| 3652 | layers_in_scene_list_cache_needs_update = false; |
| 3653 | } |
| 3654 | |
| 3655 | void TileMapLayerEditor::_node_change(Node *p_node) { |
| 3656 | if (!layers_in_scene_list_cache_needs_update && p_node->is_part_of_edited_scene() && Object::cast_to<TileMapLayer>(p_node)) { |
| 3657 | layers_in_scene_list_cache_needs_update = true; |
| 3658 | } |
| 3659 | } |
| 3660 | |
| 3661 | void TileMapLayerEditor::_notification(int p_what) { |
| 3662 | switch (p_what) { |
| 3663 | case NOTIFICATION_READY: { |
| 3664 | toggle_grid_button->set_pressed_no_signal(EDITOR_GET("editors/tiles_editor/display_grid")); |
| 3665 | toggle_highlight_selected_layer_button->set_pressed_no_signal(EDITOR_GET("editors/tiles_editor/highlight_selected_layer")); |
| 3666 | } break; |
| 3667 | |
| 3668 | case NOTIFICATION_ENTER_TREE: { |
| 3669 | get_tree()->connect("node_added", callable_mp(this, &TileMapLayerEditor::_node_change)); |
| 3670 | get_tree()->connect("node_removed", callable_mp(this, &TileMapLayerEditor::_node_change)); |
| 3671 | } break; |
| 3672 | |
| 3673 | case NOTIFICATION_EXIT_TREE: { |
| 3674 | get_tree()->disconnect("node_added", callable_mp(this, &TileMapLayerEditor::_node_change)); |
| 3675 | get_tree()->disconnect("node_removed", callable_mp(this, &TileMapLayerEditor::_node_change)); |
| 3676 | } break; |
| 3677 | |
| 3678 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 3679 | if (custom_overlay) { |
| 3680 | custom_overlay->set_visible(is_visible_in_tree()); |
| 3681 | } |
| 3682 | if (is_visible()) { |
| 3683 | CanvasItemEditor::get_singleton()->set_current_tool(CanvasItemEditor::TOOL_SELECT); |
| 3684 | } |
| 3685 | } break; |
| 3686 | |
| 3687 | case NOTIFICATION_THEME_CHANGED: { |
| 3688 | missing_tile_texture = get_editor_theme_icon(SNAME("StatusWarning")); |
| 3689 | warning_pattern_texture = get_editor_theme_icon(SNAME("WarningPattern")); |
| 3690 | advanced_menu_button->set_button_icon(get_editor_theme_icon(SNAME("Tools"))); |
| 3691 | select_previous_layer->set_button_icon(get_editor_theme_icon(SNAME("MoveUp"))); |
| 3692 | select_next_layer->set_button_icon(get_editor_theme_icon(SNAME("MoveDown"))); |
| 3693 | select_all_layers->set_button_icon(get_editor_theme_icon(SNAME("FileList"))); |
| 3694 | toggle_grid_button->set_button_icon(get_editor_theme_icon(SNAME("Grid"))); |
| 3695 | toggle_highlight_selected_layer_button->set_button_icon(get_editor_theme_icon(SNAME("TileMapHighlightSelected"))); |
| 3696 | } break; |
| 3697 | |
| 3698 | case NOTIFICATION_INTERNAL_PROCESS: { |
| 3699 | if (is_visible_in_tree() && tile_map_layer_changed_needs_update) { |
| 3700 | _update_bottom_panel(); |
| 3701 | _update_layers_selector(); |
| 3702 | tabs_plugins[tabs_bar->get_current_tab()]->tile_set_changed(); |
| 3703 | |
| 3704 | const TileMapLayer *edited_layer = _get_edited_layer(); |
| 3705 | if (edited_layer && custom_overlay) { |
| 3706 | custom_overlay->set_texture_filter(edited_layer->get_texture_filter_in_tree()); |
| 3707 | } |
| 3708 | |
| 3709 | CanvasItemEditor::get_singleton()->update_viewport(); |
| 3710 | tile_map_layer_changed_needs_update = false; |
| 3711 | } |
| 3712 | } break; |
| 3713 | |
| 3714 | case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { |
| 3715 | if (EditorSettings::get_singleton()->check_changed_settings_in_group("editors/tiles_editor")) { |
| 3716 | toggle_grid_button->set_pressed_no_signal(EDITOR_GET("editors/tiles_editor/display_grid")); |
| 3717 | toggle_highlight_selected_layer_button->set_pressed_no_signal(EDITOR_GET("editors/tiles_editor/highlight_selected_layer")); |
| 3718 | } |
| 3719 | } break; |
| 3720 | |
| 3721 | case NOTIFICATION_APPLICATION_FOCUS_OUT: { |
| 3722 | // Simulate mouse released event to stop drawing when editor focus exits. |
| 3723 | Ref<InputEventMouseButton> release; |
| 3724 | release.instantiate(); |
| 3725 | release->set_button_index(MouseButton::LEFT); |
| 3726 | forward_canvas_gui_input(release); |
| 3727 | } break; |
| 3728 | } |
| 3729 | } |
| 3730 | |
| 3731 | void TileMapLayerEditor::_on_grid_toggled(bool p_pressed) { |
| 3732 | EditorSettings::get_singleton()->set("editors/tiles_editor/display_grid", p_pressed); |
| 3733 | CanvasItemEditor::get_singleton()->update_viewport(); |
| 3734 | } |
| 3735 | |
| 3736 | void TileMapLayerEditor::_select_previous_layer_pressed() { |
| 3737 | _layers_select_next_or_previous(false); |
| 3738 | } |
| 3739 | |
| 3740 | void TileMapLayerEditor::_select_next_layer_pressed() { |
| 3741 | _layers_select_next_or_previous(true); |
| 3742 | } |
| 3743 | |
| 3744 | void TileMapLayerEditor::_select_all_layers_pressed() { |
| 3745 | EditorNode *en = EditorNode::get_singleton(); |
| 3746 | Node *edited_scene_root = en->get_edited_scene(); |
| 3747 | ERR_FAIL_NULL(edited_scene_root); |
| 3748 | |
| 3749 | en->get_editor_selection()->clear(); |
| 3750 | if (tile_map_layers_in_scene_cache.size() == 1) { |
| 3751 | en->edit_node(tile_map_layers_in_scene_cache[0]); |
| 3752 | en->get_editor_selection()->add_node(tile_map_layers_in_scene_cache[0]); |
| 3753 | } else { |
| 3754 | _update_tile_map_layers_in_scene_list_cache(); |
| 3755 | Ref<MultiNodeEdit> multi_node_edit = memnew(MultiNodeEdit); |
| 3756 | for (TileMapLayer *layer : tile_map_layers_in_scene_cache) { |
| 3757 | multi_node_edit->add_node(edited_scene_root->get_path_to(layer)); |
| 3758 | en->get_editor_selection()->add_node(layer); |
| 3759 | } |
| 3760 | en->push_item(multi_node_edit.ptr()); |
| 3761 | } |
| 3762 | } |
| 3763 | |
| 3764 | void TileMapLayerEditor::_layers_selection_item_selected(int p_index) { |
| 3765 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 3766 | ERR_FAIL_NULL(edited_layer); |
| 3767 | |
| 3768 | TileMap *tile_map = Object::cast_to<TileMap>(edited_layer->get_parent()); |
| 3769 | ERR_FAIL_NULL(tile_map); |
| 3770 | |
| 3771 | TileMapLayer *new_edited = Object::cast_to<TileMapLayer>(tile_map->get_child(p_index)); |
| 3772 | edit(new_edited); |
| 3773 | } |
| 3774 | |
| 3775 | void TileMapLayerEditor::_update_layers_selector() { |
| 3776 | const TileMapLayer *edited_layer = _get_edited_layer(); |
| 3777 | |
| 3778 | // Update the selector. |
| 3779 | layers_selection_button->clear(); |
| 3780 | layers_selection_button->hide(); |
| 3781 | select_all_layers->show(); |
| 3782 | select_next_layer->set_disabled(false); |
| 3783 | select_previous_layer->set_disabled(false); |
| 3784 | advanced_menu_button->get_popup()->set_item_disabled(ADVANCED_MENU_EXTRACT_TILE_MAP_LAYERS, true); |
| 3785 | if (edited_layer) { |
| 3786 | TileMap *tile_map = Object::cast_to<TileMap>(edited_layer->get_parent()); |
| 3787 | if (tile_map && edited_layer->get_index_in_tile_map() >= 0) { |
| 3788 | // Build the list of layers. |
| 3789 | for (int i = 0; i < tile_map->get_layers_count(); i++) { |
| 3790 | const TileMapLayer *layer = Object::cast_to<TileMapLayer>(tile_map->get_child(i)); |
| 3791 | if (layer) { |
| 3792 | int index = layers_selection_button->get_item_count(); |
| 3793 | layers_selection_button->add_item(layer->get_name()); |
| 3794 | layers_selection_button->set_item_metadata(index, layer->get_name()); |
| 3795 | if (edited_layer == layer) { |
| 3796 | layers_selection_button->select(index); |
| 3797 | } |
| 3798 | } |
| 3799 | } |
| 3800 | |
| 3801 | // Disable selector if there's no layer to select. |
| 3802 | layers_selection_button->set_disabled(false); |
| 3803 | if (layers_selection_button->get_item_count() == 0) { |
| 3804 | layers_selection_button->set_disabled(true); |
| 3805 | layers_selection_button->set_text(TTR("No Layers")); |
| 3806 | } |
| 3807 | |
| 3808 | // Disable next/previous if there's one or less layers. |
| 3809 | if (layers_selection_button->get_item_count() <= 1) { |
| 3810 | select_next_layer->set_disabled(true); |
| 3811 | select_previous_layer->set_disabled(true); |
| 3812 | } |
| 3813 | layers_selection_button->show(); |
| 3814 | select_all_layers->hide(); |
| 3815 | |
| 3816 | // Enable the "extract as TileMapLayer" option only if we are editing a TleMap. |
| 3817 | advanced_menu_button->get_popup()->set_item_disabled(ADVANCED_MENU_EXTRACT_TILE_MAP_LAYERS, false); |
| 3818 | } |
| 3819 | } else { |
| 3820 | select_all_layers->hide(); |
| 3821 | select_next_layer->set_disabled(true); |
| 3822 | select_previous_layer->set_disabled(true); |
| 3823 | } |
| 3824 | |
| 3825 | _update_layer_selector_layout(get_current_layout() == EditorDock::DockLayout::DOCK_LAYOUT_VERTICAL); |
| 3826 | } |
| 3827 | |
| 3828 | void TileMapLayerEditor::_clear_all_layers_highlighting() { |
| 3829 | // Note: This function might be removed if we remove the TileMap node at some point. |
| 3830 | // All processing could be done in _update_all_layers_highlighting otherwise. |
| 3831 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 3832 | |
| 3833 | // Use default mode. |
| 3834 | if (edited_layer && edited_layer->get_index_in_tile_map() >= 0) { |
| 3835 | // For the TileMap node. |
| 3836 | TileMap *tile_map = Object::cast_to<TileMap>(edited_layer->get_parent()); |
| 3837 | if (tile_map) { |
| 3838 | for (int i = 0; i < tile_map->get_layers_count(); i++) { |
| 3839 | TileMapLayer *layer = Object::cast_to<TileMapLayer>(tile_map->get_child(i)); |
| 3840 | layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_DEFAULT); |
| 3841 | } |
| 3842 | } |
| 3843 | } else { |
| 3844 | // For other TileMapLayer nodes. |
| 3845 | _update_tile_map_layers_in_scene_list_cache(); |
| 3846 | for (TileMapLayer *layer : tile_map_layers_in_scene_cache) { |
| 3847 | layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_DEFAULT); |
| 3848 | } |
| 3849 | } |
| 3850 | } |
| 3851 | |
| 3852 | void TileMapLayerEditor::_update_all_layers_highlighting() { |
| 3853 | EditorNode *en = EditorNode::get_singleton(); |
| 3854 | Node *edited_scene_root = en->get_edited_scene(); |
| 3855 | if (!edited_scene_root) { |
| 3856 | return; |
| 3857 | } |
| 3858 | |
| 3859 | // Get selected layer. |
| 3860 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 3861 | |
| 3862 | bool highlight_selected_layer = EDITOR_GET("editors/tiles_editor/highlight_selected_layer"); |
| 3863 | if (edited_layer && highlight_selected_layer) { |
| 3864 | int edited_z_index = edited_layer->get_z_index(); |
| 3865 | |
| 3866 | if (edited_layer->get_index_in_tile_map() >= 0) { |
| 3867 | // For the TileMap node. |
| 3868 | TileMap *tile_map = Object::cast_to<TileMap>(edited_layer->get_parent()); |
| 3869 | ERR_FAIL_NULL(tile_map); |
| 3870 | |
| 3871 | bool passed = false; |
| 3872 | for (int i = 0; i < tile_map->get_layers_count(); i++) { |
| 3873 | TileMapLayer *layer = Object::cast_to<TileMapLayer>(tile_map->get_child(i)); |
| 3874 | if (layer == edited_layer) { |
| 3875 | passed = true; |
| 3876 | layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_DEFAULT); |
| 3877 | } else { |
| 3878 | if (passed || layer->get_z_index() > edited_z_index) { |
| 3879 | layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_ABOVE); |
| 3880 | } else { |
| 3881 | layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_BELOW); |
| 3882 | } |
| 3883 | } |
| 3884 | } |
| 3885 | } else { |
| 3886 | // Update highlight mode for independent layers. |
| 3887 | _update_tile_map_layers_in_scene_list_cache(); |
| 3888 | bool passed = false; |
| 3889 | for (TileMapLayer *layer : tile_map_layers_in_scene_cache) { |
| 3890 | if (layer == edited_layer) { |
| 3891 | passed = true; |
| 3892 | layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_DEFAULT); |
| 3893 | } else { |
| 3894 | if (passed || layer->get_z_index() > edited_z_index) { |
| 3895 | layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_ABOVE); |
| 3896 | } else { |
| 3897 | layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_BELOW); |
| 3898 | } |
| 3899 | } |
| 3900 | } |
| 3901 | } |
| 3902 | } |
| 3903 | } |
| 3904 | |
| 3905 | void TileMapLayerEditor::_highlight_selected_layer_button_toggled(bool p_pressed) { |
| 3906 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 3907 | if (!edited_layer) { |
| 3908 | return; |
| 3909 | } |
| 3910 | |
| 3911 | EditorSettings::get_singleton()->set("editors/tiles_editor/highlight_selected_layer", p_pressed); |
| 3912 | if (p_pressed) { |
| 3913 | _update_all_layers_highlighting(); |
| 3914 | } else { |
| 3915 | _clear_all_layers_highlighting(); |
| 3916 | } |
| 3917 | } |
| 3918 | |
| 3919 | void TileMapLayerEditor::_advanced_menu_button_id_pressed(int p_id) { |
| 3920 | TileMapLayer *edited_layer = _get_edited_layer(); |
| 3921 | if (!edited_layer) { |
| 3922 | return; |
| 3923 | } |
| 3924 | |
| 3925 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 3926 | if (tile_set.is_null()) { |
| 3927 | return; |
| 3928 | } |
| 3929 | |
| 3930 | if (p_id == ADVANCED_MENU_REPLACE_WITH_PROXIES) { // Replace Tile Proxies |
| 3931 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 3932 | undo_redo->create_action(TTR("Replace Tiles with Proxies")); |
| 3933 | TypedArray<Vector2i> used_cells = edited_layer->get_used_cells(); |
| 3934 | for (int i = 0; i < used_cells.size(); i++) { |
| 3935 | Vector2i cell_coords = used_cells[i]; |
| 3936 | TileMapCell from = edited_layer->get_cell(cell_coords); |
| 3937 | Array to_array = tile_set->map_tile_proxy(from.source_id, from.get_atlas_coords(), from.alternative_tile); |
| 3938 | TileMapCell to; |
| 3939 | to.source_id = to_array[0]; |
| 3940 | to.set_atlas_coords(to_array[1]); |
| 3941 | to.alternative_tile = to_array[2]; |
| 3942 | if (from != to) { |
| 3943 | undo_redo->add_do_method(edited_layer, "set_cell", cell_coords, to.source_id, to.get_atlas_coords(), to.alternative_tile); |
| 3944 | undo_redo->add_undo_method(edited_layer, "set_cell", cell_coords, from.source_id, from.get_atlas_coords(), from.alternative_tile); |
| 3945 | } |
| 3946 | } |
| 3947 | |
| 3948 | undo_redo->commit_action(); |
| 3949 | } else if (p_id == ADVANCED_MENU_EXTRACT_TILE_MAP_LAYERS) { // Transform internal TileMap layers into TileMapLayers. |
| 3950 | ERR_FAIL_COND(edited_layer->get_index_in_tile_map() < 0); |
| 3951 | |
| 3952 | EditorNode *en = EditorNode::get_singleton(); |
| 3953 | Node *edited_scene_root = en->get_edited_scene(); |
| 3954 | ERR_FAIL_NULL(edited_scene_root); |
| 3955 | |
| 3956 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 3957 | undo_redo->create_action(TTR("Extract TileMap layers as individual TileMapLayer nodes")); |
| 3958 | |
| 3959 | TileMap *tile_map = Object::cast_to<TileMap>(edited_layer->get_parent()); |
| 3960 | for (int i = 0; i < tile_map->get_layers_count(); i++) { |
| 3961 | undo_redo->add_do_method(tile_map, "remove_layer", 0); |
| 3962 | } |
| 3963 | |
| 3964 | for (int i = 0; i < tile_map->get_layers_count(); i++) { |
| 3965 | TileMapLayer *new_layer = tile_map->duplicate_layer_from_internal(i); |
| 3966 | undo_redo->add_do_method(tile_map, "add_child", new_layer); |
| 3967 | undo_redo->add_do_method(new_layer, "set_owner", edited_scene_root); |
| 3968 | undo_redo->add_do_property(new_layer, "tile_set", tile_map->get_tileset()); // Workaround for a bug: #89947. |
| 3969 | undo_redo->add_undo_method(tile_map, "remove_child", new_layer); |
| 3970 | undo_redo->add_do_reference(new_layer); |
| 3971 | } |
| 3972 | |
| 3973 | List<PropertyInfo> prop_list; |
| 3974 | tile_map->get_property_list(&prop_list); |
| 3975 | for (PropertyInfo &prop : prop_list) { |
| 3976 | undo_redo->add_undo_property(tile_map, prop.name, tile_map->get(prop.name)); |
| 3977 | } |
| 3978 | undo_redo->commit_action(); |
| 3979 | } |
| 3980 | } |
| 3981 | |
| 3982 | void TileMapLayerEditor::_update_bottom_panel() { |
| 3983 | const TileMapLayer *edited_layer = _get_edited_layer(); |
| 3984 | Ref<TileSet> tile_set; |
| 3985 | if (edited_layer) { |
| 3986 | tile_set = edited_layer->get_tile_set(); |
| 3987 | } |
| 3988 | |
| 3989 | // Update state labels. |
| 3990 | if (is_multi_node_edit) { |
| 3991 | cant_edit_label->set_text(TTR("Can't edit multiple layers at once.")); |
| 3992 | cant_edit_label->show(); |
| 3993 | } else if (!edited_layer) { |
| 3994 | cant_edit_label->set_text(TTR("The selected TileMap has no layer to edit.")); |
| 3995 | cant_edit_label->show(); |
| 3996 | } else if (!edited_layer->is_enabled() || !edited_layer->is_visible_in_tree()) { |
| 3997 | cant_edit_label->set_text(TTR("The edited layer is disabled or invisible")); |
| 3998 | cant_edit_label->show(); |
| 3999 | } else if (tile_set.is_null()) { |
| 4000 | cant_edit_label->set_text(TTR("The edited TileMap or TileMapLayer node has no TileSet resource.\nCreate or load a TileSet resource in the Tile Set property in the inspector.")); |
| 4001 | cant_edit_label->show(); |
| 4002 | } else { |
| 4003 | cant_edit_label->hide(); |
| 4004 | } |
| 4005 | |
| 4006 | // Update tabs visibility. |
| 4007 | for (int i = 0; i < int(tabs_data.size()); i++) { |
| 4008 | TileMapLayerSubEditorPlugin::TabData &tab_data = tabs_data[i]; |
| 4009 | if (i == tabs_bar->get_current_tab()) { |
| 4010 | tab_data.panel->set_visible(!cant_edit_label->is_visible()); |
| 4011 | } else { |
| 4012 | tab_data.panel->hide(); |
| 4013 | } |
| 4014 | } |
| 4015 | } |
| 4016 | |
| 4017 | Vector<Vector2i> TileMapLayerEditor::get_line(const TileMapLayer *p_tile_map_layer, Vector2i p_from_cell, Vector2i p_to_cell) { |
| 4018 | ERR_FAIL_NULL_V(p_tile_map_layer, Vector<Vector2i>()); |
| 4019 | |
| 4020 | Ref<TileSet> tile_set = p_tile_map_layer->get_tile_set(); |
| 4021 | ERR_FAIL_COND_V(tile_set.is_null(), Vector<Vector2i>()); |
| 4022 | |
| 4023 | if (tile_set->get_tile_shape() == TileSet::TILE_SHAPE_SQUARE) { |
| 4024 | return Geometry2D::bresenham_line(p_from_cell, p_to_cell); |
| 4025 | } else { |
| 4026 | // Adapt the bresenham line algorithm to half-offset shapes. |
| 4027 | // See this blog post: http://zvold.blogspot.com/2010/01/bresenhams-line-drawing-algorithm-on_26.html |
| 4028 | Vector<Point2i> points; |
| 4029 | |
| 4030 | bool transposed = tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL; |
| 4031 | p_from_cell = TileSet::transform_coords_layout(p_from_cell, tile_set->get_tile_offset_axis(), tile_set->get_tile_layout(), TileSet::TILE_LAYOUT_STACKED); |
| 4032 | p_to_cell = TileSet::transform_coords_layout(p_to_cell, tile_set->get_tile_offset_axis(), tile_set->get_tile_layout(), TileSet::TILE_LAYOUT_STACKED); |
| 4033 | if (transposed) { |
| 4034 | SWAP(p_from_cell.x, p_from_cell.y); |
| 4035 | SWAP(p_to_cell.x, p_to_cell.y); |
| 4036 | } |
| 4037 | |
| 4038 | Vector2i delta = p_to_cell - p_from_cell; |
| 4039 | delta = Vector2i(2 * delta.x + Math::abs(p_to_cell.y % 2) - Math::abs(p_from_cell.y % 2), delta.y); |
| 4040 | Vector2i sign = delta.sign(); |
| 4041 | |
| 4042 | Vector2i current = p_from_cell; |
| 4043 | points.push_back(TileSet::transform_coords_layout(transposed ? Vector2i(current.y, current.x) : current, tile_set->get_tile_offset_axis(), TileSet::TILE_LAYOUT_STACKED, tile_set->get_tile_layout())); |
| 4044 | |
| 4045 | int err = 0; |
| 4046 | if (Math::abs(delta.y) < Math::abs(delta.x)) { |
| 4047 | Vector2i err_step = 3 * delta.abs(); |
| 4048 | while (current != p_to_cell) { |
| 4049 | err += err_step.y; |
| 4050 | if (err > Math::abs(delta.x)) { |
| 4051 | if (sign.x == 0) { |
| 4052 | current += Vector2(sign.y, 0); |
| 4053 | } else { |
| 4054 | current += Vector2(bool(current.y % 2) != (sign.x < 0) ? sign.x : 0, sign.y); |
| 4055 | } |
| 4056 | err -= err_step.x; |
| 4057 | } else { |
| 4058 | current += Vector2i(sign.x, 0); |
| 4059 | err += err_step.y; |
| 4060 | } |
| 4061 | points.push_back(TileSet::transform_coords_layout(transposed ? Vector2i(current.y, current.x) : current, tile_set->get_tile_offset_axis(), TileSet::TILE_LAYOUT_STACKED, tile_set->get_tile_layout())); |
| 4062 | } |
| 4063 | } else { |
| 4064 | Vector2i err_step = delta.abs(); |
| 4065 | while (current != p_to_cell) { |
| 4066 | err += err_step.x; |
| 4067 | if (err > 0) { |
| 4068 | if (sign.x == 0) { |
| 4069 | current += Vector2(0, sign.y); |
| 4070 | } else { |
| 4071 | current += Vector2(bool(current.y % 2) != (sign.x < 0) ? sign.x : 0, sign.y); |
| 4072 | } |
| 4073 | err -= err_step.y; |
| 4074 | } else { |
| 4075 | if (sign.x == 0) { |
| 4076 | current += Vector2(0, sign.y); |
| 4077 | } else { |
| 4078 | current += Vector2(bool(current.y % 2) ^ (sign.x > 0) ? -sign.x : 0, sign.y); |
| 4079 | } |
| 4080 | err += err_step.y; |
| 4081 | } |
| 4082 | points.push_back(TileSet::transform_coords_layout(transposed ? Vector2i(current.y, current.x) : current, tile_set->get_tile_offset_axis(), TileSet::TILE_LAYOUT_STACKED, tile_set->get_tile_layout())); |
| 4083 | } |
| 4084 | } |
| 4085 | |
| 4086 | return points; |
| 4087 | } |
| 4088 | } |
| 4089 | |
| 4090 | void TileMapLayerEditor::_tile_map_layer_changed() { |
| 4091 | tile_map_layer_changed_needs_update = true; |
| 4092 | } |
| 4093 | |
| 4094 | void TileMapLayerEditor::_tab_changed(int p_tab_id) { |
| 4095 | // Make the plugin edit the correct tilemap. |
| 4096 | tabs_plugins[tabs_bar->get_current_tab()]->edit(edited_tile_map_layer_id); |
| 4097 | |
| 4098 | // Update toolbar. |
| 4099 | for (TileMapLayerSubEditorPlugin::TabData &tab_data : tabs_data) { |
| 4100 | for (Control *toolbar_control : tab_data.toolbar) { |
| 4101 | toolbar_control->hide(); |
| 4102 | } |
| 4103 | tab_data.wide_toolbar->hide(); |
| 4104 | } |
| 4105 | |
| 4106 | for (Control *toolbar_control : tabs_data[p_tab_id].toolbar) { |
| 4107 | toolbar_control->show(); |
| 4108 | } |
| 4109 | tabs_data[p_tab_id].wide_toolbar->show(); |
| 4110 | |
| 4111 | // Update visible panel. |
| 4112 | for (TileMapLayerSubEditorPlugin::TabData &tab_data : tabs_data) { |
| 4113 | tab_data.panel->hide(); |
| 4114 | } |
| 4115 | |
| 4116 | TileMapLayer *tile_map_layer = _get_edited_layer(); |
| 4117 | if (tile_map_layer) { |
| 4118 | if (tile_map_layer->get_tile_set().is_valid()) { |
| 4119 | tabs_data[tabs_bar->get_current_tab()].panel->show(); |
| 4120 | } |
| 4121 | } |
| 4122 | |
| 4123 | // Graphical update. |
| 4124 | tabs_data[tabs_bar->get_current_tab()].panel->queue_redraw(); |
| 4125 | CanvasItemEditor::get_singleton()->update_viewport(); |
| 4126 | _update_bottom_panel(); |
| 4127 | } |
| 4128 | |
| 4129 | void TileMapLayerEditor::_layers_select_next_or_previous(bool p_next) { |
| 4130 | const TileMapLayer *edited_layer = _get_edited_layer(); |
| 4131 | if (!edited_layer) { |
| 4132 | return; |
| 4133 | } |
| 4134 | |
| 4135 | EditorNode *en = EditorNode::get_singleton(); |
| 4136 | Node *edited_scene_root = en->get_edited_scene(); |
| 4137 | ERR_FAIL_NULL(edited_scene_root); |
| 4138 | |
| 4139 | TileMapLayer *new_selected_layer = nullptr; |
| 4140 | int inc = p_next ? 1 : -1; |
| 4141 | if (edited_layer->get_index_in_tile_map() >= 0) { |
| 4142 | // Part of a TileMap. |
| 4143 | TileMap *tile_map = Object::cast_to<TileMap>(edited_layer->get_parent()); |
| 4144 | new_selected_layer = Object::cast_to<TileMapLayer>(tile_map->get_child(Math::posmod(edited_layer->get_index_in_tile_map() + inc, tile_map->get_layers_count()))); |
| 4145 | } else { |
| 4146 | // Individual layer. |
| 4147 | _update_tile_map_layers_in_scene_list_cache(); |
| 4148 | int edited_index = -1; |
| 4149 | for (int i = 0; i < tile_map_layers_in_scene_cache.size(); i++) { |
| 4150 | if (tile_map_layers_in_scene_cache[i] == edited_layer) { |
| 4151 | edited_index = i; |
| 4152 | break; |
| 4153 | } |
| 4154 | } |
| 4155 | new_selected_layer = tile_map_layers_in_scene_cache[Math::posmod(edited_index + inc, tile_map_layers_in_scene_cache.size())]; |
| 4156 | } |
| 4157 | |
| 4158 | ERR_FAIL_NULL(new_selected_layer); |
| 4159 | |
| 4160 | if (edited_layer->get_index_in_tile_map() < 0) { |
| 4161 | // Only if not part of a TileMap. |
| 4162 | en->edit_node(new_selected_layer); |
| 4163 | en->get_editor_selection()->clear(); |
| 4164 | en->get_editor_selection()->add_node(new_selected_layer); |
| 4165 | } else { |
| 4166 | edit(new_selected_layer); |
| 4167 | } |
| 4168 | } |
| 4169 | |
| 4170 | void TileMapLayerEditor::_move_tile_map_array_element(Object *p_undo_redo, Object *p_edited, const String &p_array_prefix, int p_from_index, int p_to_pos) { |
| 4171 | EditorUndoRedoManager *undo_redo_man = Object::cast_to<EditorUndoRedoManager>(p_undo_redo); |
| 4172 | ERR_FAIL_NULL(undo_redo_man); |
| 4173 | |
| 4174 | TileMap *tile_map = Object::cast_to<TileMap>(p_edited); |
| 4175 | if (!tile_map) { |
| 4176 | return; |
| 4177 | } |
| 4178 | |
| 4179 | // Compute the array indices to save. |
| 4180 | int begin = 0; |
| 4181 | int end; |
| 4182 | if (p_array_prefix == "layer_") { |
| 4183 | end = tile_map->get_layers_count(); |
| 4184 | } else { |
| 4185 | ERR_FAIL_MSG("Invalid array prefix for TileSet."); |
| 4186 | } |
| 4187 | if (p_from_index < 0) { |
| 4188 | // Adding new. |
| 4189 | if (p_to_pos >= 0) { |
| 4190 | begin = p_to_pos; |
| 4191 | } else { |
| 4192 | end = 0; // Nothing to save when adding at the end. |
| 4193 | } |
| 4194 | } else if (p_to_pos < 0) { |
| 4195 | // Removing. |
| 4196 | begin = p_from_index; |
| 4197 | } else { |
| 4198 | // Moving. |
| 4199 | begin = MIN(p_from_index, p_to_pos); |
| 4200 | end = MIN(MAX(p_from_index, p_to_pos) + 1, end); |
| 4201 | } |
| 4202 | |
| 4203 | #define ADD_UNDO(obj, property) undo_redo_man->add_undo_property(obj, property, obj->get(property)); |
| 4204 | // Save layers' properties. |
| 4205 | if (p_from_index < 0) { |
| 4206 | undo_redo_man->add_undo_method(tile_map, "remove_layer", p_to_pos < 0 ? tile_map->get_layers_count() : p_to_pos); |
| 4207 | } else if (p_to_pos < 0) { |
| 4208 | undo_redo_man->add_undo_method(tile_map, "add_layer", p_from_index); |
| 4209 | } |
| 4210 | |
| 4211 | List<PropertyInfo> properties; |
| 4212 | tile_map->get_property_list(&properties); |
| 4213 | for (PropertyInfo pi : properties) { |
| 4214 | if (pi.name.begins_with(p_array_prefix)) { |
| 4215 | String str = pi.name.trim_prefix(p_array_prefix); |
| 4216 | int to_char_index = 0; |
| 4217 | while (to_char_index < str.length()) { |
| 4218 | if (!is_digit(str[to_char_index])) { |
| 4219 | break; |
| 4220 | } |
| 4221 | to_char_index++; |
| 4222 | } |
| 4223 | if (to_char_index > 0) { |
| 4224 | int array_index = str.left(to_char_index).to_int(); |
| 4225 | if (array_index >= begin && array_index < end) { |
| 4226 | ADD_UNDO(tile_map, pi.name); |
| 4227 | } |
| 4228 | } |
| 4229 | } |
| 4230 | } |
| 4231 | #undef ADD_UNDO |
| 4232 | |
| 4233 | if (p_from_index < 0) { |
| 4234 | undo_redo_man->add_do_method(tile_map, "add_layer", p_to_pos); |
| 4235 | } else if (p_to_pos < 0) { |
| 4236 | undo_redo_man->add_do_method(tile_map, "remove_layer", p_from_index); |
| 4237 | } else { |
| 4238 | undo_redo_man->add_do_method(tile_map, "move_layer", p_from_index, p_to_pos); |
| 4239 | } |
| 4240 | } |
| 4241 | |
| 4242 | bool TileMapLayerEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) { |
| 4243 | if (ED_IS_SHORTCUT("tiles_editor/select_next_layer", p_event) && p_event->is_pressed()) { |
| 4244 | _layers_select_next_or_previous(true); |
| 4245 | return true; |
| 4246 | } |
| 4247 | |
| 4248 | if (ED_IS_SHORTCUT("tiles_editor/select_previous_layer", p_event) && p_event->is_pressed()) { |
| 4249 | _layers_select_next_or_previous(false); |
| 4250 | return true; |
| 4251 | } |
| 4252 | |
| 4253 | return tabs_plugins[tabs_bar->get_current_tab()]->forward_canvas_gui_input(p_event); |
| 4254 | } |
| 4255 | |
| 4256 | void TileMapLayerEditor::forward_canvas_draw_over_viewport(Control *p_overlay) { |
| 4257 | if (!custom_overlay) { |
| 4258 | custom_overlay = memnew(Control); |
| 4259 | custom_overlay->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
| 4260 | custom_overlay->set_mouse_filter(Control::MOUSE_FILTER_IGNORE); |
| 4261 | custom_overlay->set_clip_contents(true); |
| 4262 | custom_overlay->set_draw_behind_parent(true); |
| 4263 | p_overlay->add_child(custom_overlay); |
| 4264 | custom_overlay->connect(SceneStringName(draw), callable_mp(this, &TileMapLayerEditor::_draw_overlay)); |
| 4265 | } |
| 4266 | custom_overlay->queue_redraw(); |
| 4267 | } |
| 4268 | |
| 4269 | void TileMapLayerEditor::_draw_overlay() { |
| 4270 | const TileMapLayer *edited_layer = _get_edited_layer(); |
| 4271 | if (!edited_layer || !edited_layer->is_visible_in_tree()) { |
| 4272 | return; |
| 4273 | } |
| 4274 | |
| 4275 | Ref<TileSet> tile_set = edited_layer->get_tile_set(); |
| 4276 | if (tile_set.is_null()) { |
| 4277 | return; |
| 4278 | } |
| 4279 | |
| 4280 | Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas(); |
| 4281 | Transform2D xform_inv = xform.affine_inverse(); |
| 4282 | Vector2i tile_shape_size = tile_set->get_tile_size(); |
| 4283 | |
| 4284 | // Fade the overlay out when size too small. |
| 4285 | Vector2 hint_distance = xform.get_scale() * tile_shape_size; |
| 4286 | float scale_fading = MIN(1, (MIN(hint_distance.x, hint_distance.y) - 5) / 5); |
| 4287 | if (scale_fading > 0) { |
| 4288 | // Draw tiles with invalid IDs in the grid. |
| 4289 | TypedArray<Vector2i> used_cells = edited_layer->get_used_cells(); |
| 4290 | for (int i = 0; i < used_cells.size(); i++) { |
| 4291 | Vector2i coords = used_cells[i]; |
| 4292 | int tile_source_id = edited_layer->get_cell_source_id(coords); |
| 4293 | if (tile_source_id >= 0) { |
| 4294 | Vector2i tile_atlas_coords = edited_layer->get_cell_atlas_coords(coords); |
| 4295 | int tile_alternative_tile = edited_layer->get_cell_alternative_tile(coords); |
| 4296 | |
| 4297 | TileSetSource *source = nullptr; |
| 4298 | if (tile_set->has_source(tile_source_id)) { |
| 4299 | source = *tile_set->get_source(tile_source_id); |
| 4300 | } |
| 4301 | |
| 4302 | if (!source || !source->has_tile(tile_atlas_coords) || !source->has_alternative_tile(tile_atlas_coords, tile_alternative_tile)) { |
| 4303 | // Generate a random color from the hashed identifier of the tiles. |
| 4304 | Array to_hash = { tile_source_id, tile_atlas_coords, tile_alternative_tile }; |
| 4305 | uint32_t hash = RandomPCG(to_hash.hash()).rand(); |
| 4306 | |
| 4307 | Color color; |
| 4308 | color = color.from_hsv( |
| 4309 | (float)((hash >> 24) & 0xFF) / 256.0, |
| 4310 | Math::lerp(0.5, 1.0, (float)((hash >> 16) & 0xFF) / 256.0), |
| 4311 | Math::lerp(0.5, 1.0, (float)((hash >> 8) & 0xFF) / 256.0), |
| 4312 | 0.8 * scale_fading); |
| 4313 | |
| 4314 | // Display the warning pattern. |
| 4315 | Transform2D tile_xform; |
| 4316 | tile_xform.set_origin(tile_set->map_to_local(coords)); |
| 4317 | tile_xform.set_scale(tile_shape_size); |
| 4318 | tile_set->draw_tile_shape(custom_overlay, xform * tile_xform, color, true, warning_pattern_texture); |
| 4319 | |
| 4320 | // Draw the warning icon. |
| 4321 | Vector2::Axis min_axis = missing_tile_texture->get_size().min_axis_index(); |
| 4322 | Vector2 icon_size; |
| 4323 | icon_size[min_axis] = tile_set->get_tile_size()[min_axis] / 3; |
| 4324 | icon_size[(min_axis + 1) % 2] = (icon_size[min_axis] * missing_tile_texture->get_size()[(min_axis + 1) % 2] / missing_tile_texture->get_size()[min_axis]); |
| 4325 | Rect2 rect = Rect2(xform.xform(tile_set->map_to_local(coords)) - (icon_size * xform.get_scale() / 2), icon_size * xform.get_scale()); |
| 4326 | custom_overlay->draw_texture_rect(missing_tile_texture, rect, false, Color(1, 1, 1, scale_fading)); |
| 4327 | } |
| 4328 | } |
| 4329 | } |
| 4330 | |
| 4331 | // Fading on the border. |
| 4332 | const int fading = 5; |
| 4333 | |
| 4334 | // Determine the drawn area. |
| 4335 | Size2 screen_size = custom_overlay->get_size(); |
| 4336 | Rect2i screen_rect; |
| 4337 | screen_rect.position = tile_set->local_to_map(xform_inv.xform(Vector2())); |
| 4338 | screen_rect.expand_to(tile_set->local_to_map(xform_inv.xform(Vector2(0, screen_size.height)))); |
| 4339 | screen_rect.expand_to(tile_set->local_to_map(xform_inv.xform(Vector2(screen_size.width, 0)))); |
| 4340 | screen_rect.expand_to(tile_set->local_to_map(xform_inv.xform(screen_size))); |
| 4341 | screen_rect = screen_rect.grow(1); |
| 4342 | |
| 4343 | Rect2i tilemap_used_rect = edited_layer->get_used_rect(); |
| 4344 | |
| 4345 | Rect2i displayed_rect = tilemap_used_rect.intersection(screen_rect); |
| 4346 | displayed_rect = displayed_rect.grow(fading); |
| 4347 | |
| 4348 | // Reduce the drawn area to avoid crashes if needed. |
| 4349 | int max_size = 100; |
| 4350 | if (displayed_rect.size.x > max_size) { |
| 4351 | displayed_rect = displayed_rect.grow_individual(-(displayed_rect.size.x - max_size) / 2, 0, -(displayed_rect.size.x - max_size) / 2, 0); |
| 4352 | } |
| 4353 | if (displayed_rect.size.y > max_size) { |
| 4354 | displayed_rect = displayed_rect.grow_individual(0, -(displayed_rect.size.y - max_size) / 2, 0, -(displayed_rect.size.y - max_size) / 2); |
| 4355 | } |
| 4356 | |
| 4357 | // Draw the grid. |
| 4358 | bool display_grid = EDITOR_GET("editors/tiles_editor/display_grid"); |
| 4359 | if (display_grid) { |
| 4360 | Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color"); |
| 4361 | for (int x = displayed_rect.position.x; x < (displayed_rect.position.x + displayed_rect.size.x); x++) { |
| 4362 | for (int y = displayed_rect.position.y; y < (displayed_rect.position.y + displayed_rect.size.y); y++) { |
| 4363 | Vector2i pos_in_rect = Vector2i(x, y) - displayed_rect.position; |
| 4364 | |
| 4365 | // Fade out the border of the grid. |
| 4366 | float left_opacity = CLAMP(Math::inverse_lerp(0.0f, (float)fading, (float)pos_in_rect.x), 0.0f, 1.0f); |
| 4367 | float right_opacity = CLAMP(Math::inverse_lerp((float)displayed_rect.size.x, (float)(displayed_rect.size.x - fading), (float)(pos_in_rect.x + 1)), 0.0f, 1.0f); |
| 4368 | float top_opacity = CLAMP(Math::inverse_lerp(0.0f, (float)fading, (float)pos_in_rect.y), 0.0f, 1.0f); |
| 4369 | float bottom_opacity = CLAMP(Math::inverse_lerp((float)displayed_rect.size.y, (float)(displayed_rect.size.y - fading), (float)(pos_in_rect.y + 1)), 0.0f, 1.0f); |
| 4370 | float opacity = CLAMP(MIN(left_opacity, MIN(right_opacity, MIN(top_opacity, bottom_opacity))) + 0.1, 0.0f, 1.0f); |
| 4371 | |
| 4372 | Transform2D tile_xform; |
| 4373 | tile_xform.set_origin(tile_set->map_to_local(Vector2(x, y))); |
| 4374 | tile_xform.set_scale(tile_shape_size); |
| 4375 | Color color = grid_color; |
| 4376 | color.a = color.a * opacity * scale_fading; |
| 4377 | tile_set->draw_tile_shape(custom_overlay, xform * tile_xform, color, false); |
| 4378 | } |
| 4379 | } |
| 4380 | } |
| 4381 | |
| 4382 | // Draw the IDs for debug. |
| 4383 | /*Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label")); |
| 4384 | for (int x = displayed_rect.position.x; x < (displayed_rect.position.x + displayed_rect.size.x); x++) { |
| 4385 | for (int y = displayed_rect.position.y; y < (displayed_rect.position.y + displayed_rect.size.y); y++) { |
| 4386 | custom_overlay->draw_string(font, xform.xform(tile_set->map_to_local(Vector2(x, y))) + Vector2i(-tile_shape_size.x / 2, 0), vformat("%s", Vector2(x, y))); |
| 4387 | } |
| 4388 | }*/ |
| 4389 | } |
| 4390 | |
| 4391 | // Draw the plugins. |
| 4392 | tabs_plugins[tabs_bar->get_current_tab()]->forward_canvas_draw_over_viewport(custom_overlay); |
| 4393 | } |
| 4394 | |
| 4395 | void TileMapLayerEditor::edit(Object *p_edited) { |
| 4396 | if (p_edited && p_edited->get_instance_id() == edited_tile_map_layer_id) { |
| 4397 | return; |
| 4398 | } |
| 4399 | |
| 4400 | _clear_all_layers_highlighting(); |
| 4401 | |
| 4402 | // Disconnect to changes. |
| 4403 | TileMapLayer *tile_map_layer = _get_edited_layer(); |
| 4404 | if (tile_map_layer) { |
| 4405 | tile_map_layer->disconnect(CoreStringName(changed), callable_mp(this, &TileMapLayerEditor::_tile_map_layer_changed)); |
| 4406 | tile_map_layer->disconnect(SceneStringName(visibility_changed), callable_mp(this, &TileMapLayerEditor::_tile_map_layer_changed)); |
| 4407 | } |
| 4408 | |
| 4409 | // Update the edited layer. |
| 4410 | TileMapLayer *new_layer = Object::cast_to<TileMapLayer>(p_edited); |
| 4411 | if (new_layer) { |
| 4412 | // Change the edited object. |
| 4413 | edited_tile_map_layer_id = new_layer->get_instance_id(); |
| 4414 | |
| 4415 | tile_map_layer = _get_edited_layer(); |
| 4416 | // Connect to changes. |
| 4417 | if (!tile_map_layer->is_connected(CoreStringName(changed), callable_mp(this, &TileMapLayerEditor::_tile_map_layer_changed))) { |
| 4418 | tile_map_layer->connect(CoreStringName(changed), callable_mp(this, &TileMapLayerEditor::_tile_map_layer_changed)); |
| 4419 | tile_map_layer->connect(SceneStringName(visibility_changed), callable_mp(this, &TileMapLayerEditor::_tile_map_layer_changed)); |
| 4420 | } |
| 4421 | } else { |
| 4422 | edited_tile_map_layer_id = ObjectID(); |
| 4423 | } |
| 4424 | |
| 4425 | // Check if we are trying to use a MultiNodeEdit. |
| 4426 | is_multi_node_edit = Object::cast_to<MultiNodeEdit>(p_edited); |
| 4427 | |
| 4428 | // Call the plugins and update everything. |
| 4429 | tabs_plugins[tabs_bar->get_current_tab()]->edit(edited_tile_map_layer_id); |
| 4430 | _update_layers_selector(); |
| 4431 | _update_all_layers_highlighting(); |
| 4432 | |
| 4433 | _tile_map_layer_changed(); |
| 4434 | } |
| 4435 | |
| 4436 | void TileMapLayerEditor::set_show_layer_selector(bool p_show_layer_selector) { |
| 4437 | show_layers_selector = p_show_layer_selector; |
| 4438 | _update_layers_selector(); |
| 4439 | } |
| 4440 | |
| 4441 | void TileMapLayerEditor::_update_layer_selector_layout(bool p_is_vertical) { |
| 4442 | if (p_is_vertical && show_layers_selector) { |
| 4443 | layer_selection_hbox->reparent(tile_map_wide_toolbar); |
| 4444 | tile_map_wide_toolbar->move_child(layer_selection_hbox, 1); |
| 4445 | layer_selection_hbox->set_vertical(false); |
| 4446 | } else { |
| 4447 | layer_selection_hbox->reparent(tile_map_toolbar); |
| 4448 | tile_map_toolbar->move_child(layer_selection_hbox, -5); |
| 4449 | layer_selection_hbox->set_vertical(p_is_vertical); |
| 4450 | } |
| 4451 | } |
| 4452 | |
| 4453 | void TileMapLayerEditor::update_layout(DockLayout p_layout) { |
| 4454 | bool is_vertical = (p_layout == EditorDock::DockLayout::DOCK_LAYOUT_VERTICAL); |
| 4455 | tile_map_toolbar->set_vertical(is_vertical); |
| 4456 | layer_selector_separator->set_vertical(is_vertical); |
| 4457 | tile_map_toolbar->set_h_size_flags(is_vertical ? SIZE_SHRINK_BEGIN : SIZE_EXPAND_FILL); |
| 4458 | tile_map_toolbar->set_v_size_flags(is_vertical ? SIZE_EXPAND_FILL : SIZE_SHRINK_BEGIN); |
| 4459 | |
| 4460 | main_box_container->move_child(padding_control, is_vertical ? 0 : 2); |
| 4461 | if (is_vertical) { |
| 4462 | main_box_container->remove_theme_constant_override(SNAME("h_separation")); |
| 4463 | } else { |
| 4464 | main_box_container->add_theme_constant_override(SNAME("h_separation"), 0); |
| 4465 | } |
| 4466 | |
| 4467 | if (is_vertical) { |
| 4468 | tabs_panel->reparent(tile_map_wide_toolbar); |
| 4469 | tile_map_wide_toolbar->move_child(tabs_panel, 0); |
| 4470 | } else { |
| 4471 | tabs_panel->reparent(tile_map_toolbar); |
| 4472 | tile_map_toolbar->move_child(tabs_panel, 0); |
| 4473 | } |
| 4474 | |
| 4475 | _update_layer_selector_layout(is_vertical); |
| 4476 | |
| 4477 | // Propagate layout change to sub plugins |
| 4478 | for (TileMapLayerSubEditorPlugin *tab_plugin : tabs_plugins) { |
| 4479 | tab_plugin->update_layout(p_layout); |
| 4480 | } |
| 4481 | } |
| 4482 | |
| 4483 | TileMapLayerEditor::TileMapLayerEditor() { |
| 4484 | set_process_internal(true); |
| 4485 | set_name(TTRC("TileMap")); |
| 4486 | set_icon_name("TileMapDock"); |
| 4487 | set_dock_shortcut(ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_tile_map_bottom_panel", TTRC("Open TileMap Dock"))); |
| 4488 | set_default_slot(EditorDock::DOCK_SLOT_BOTTOM); |
| 4489 | set_available_layouts(EditorDock::DOCK_LAYOUT_ALL); |
| 4490 | set_global(false); |
| 4491 | set_transient(true); |
| 4492 | |
| 4493 | main_box_container = memnew(GridContainer); |
| 4494 | main_box_container->set_columns(2); |
| 4495 | add_child(main_box_container); |
| 4496 | |
| 4497 | // Shortcuts. |
| 4498 | ED_SHORTCUT("tiles_editor/select_next_layer", TTRC("Select Next Tile Map Layer"), Key::PAGEDOWN); |
| 4499 | ED_SHORTCUT("tiles_editor/select_previous_layer", TTRC("Select Previous Tile Map Layer"), Key::PAGEUP); |
| 4500 | |
| 4501 | // TileMap editor plugins |
| 4502 | tile_map_editor_plugins.push_back(memnew(TileMapLayerEditorTilesPlugin)); |
| 4503 | tile_map_editor_plugins.push_back(memnew(TileMapLayerEditorTerrainsPlugin)); |
| 4504 | |
| 4505 | // TabBar. |
| 4506 | tabs_bar = memnew(TabBar); |
| 4507 | tabs_bar->set_theme_type_variation("TabBarInner"); |
| 4508 | tabs_bar->set_clip_tabs(false); |
| 4509 | for (int plugin_index = 0; plugin_index < tile_map_editor_plugins.size(); plugin_index++) { |
| 4510 | Vector<TileMapLayerSubEditorPlugin::TabData> tabs_vector = tile_map_editor_plugins[plugin_index]->get_tabs(); |
| 4511 | for (int tab_index = 0; tab_index < tabs_vector.size(); tab_index++) { |
| 4512 | tabs_bar->add_tab(tabs_vector[tab_index].panel->get_name()); |
| 4513 | tabs_data.push_back(tabs_vector[tab_index]); |
| 4514 | tabs_plugins.push_back(tile_map_editor_plugins[plugin_index]); |
| 4515 | } |
| 4516 | } |
| 4517 | tabs_bar->connect("tab_changed", callable_mp(this, &TileMapLayerEditor::_tab_changed)); |
| 4518 | |
| 4519 | // --- TileMap toolbar --- |
| 4520 | tile_map_wide_toolbar = memnew(VBoxContainer); |
| 4521 | main_box_container->add_child(tile_map_wide_toolbar); |
| 4522 | |
| 4523 | tile_map_toolbar = memnew(FlowContainer); |
| 4524 | tile_map_toolbar->set_h_size_flags(SIZE_EXPAND_FILL); |
| 4525 | main_box_container->add_child(tile_map_toolbar); |
| 4526 | |
| 4527 | padding_control = memnew(Control); |
| 4528 | main_box_container->add_child(padding_control); |
| 4529 | |
| 4530 | // Tabs. |
| 4531 | tabs_panel = memnew(PanelContainer); |
| 4532 | tabs_panel->set_theme_type_variation("PanelContainerTabbarInner"); |
| 4533 | tabs_panel->add_child(tabs_bar); |
| 4534 | tile_map_toolbar->add_child(tabs_panel); |
| 4535 | |
| 4536 | // Tabs toolbars. |
| 4537 | for (TileMapLayerSubEditorPlugin::TabData &tab_data : tabs_data) { |
| 4538 | for (Control *toolbar_control : tab_data.toolbar) { |
| 4539 | toolbar_control->hide(); |
| 4540 | if (!toolbar_control->get_parent()) { |
| 4541 | tile_map_toolbar->add_child(toolbar_control); |
| 4542 | } |
| 4543 | } |
| 4544 | |
| 4545 | tab_data.wide_toolbar->hide(); |
| 4546 | if (!tab_data.wide_toolbar->get_parent()) { |
| 4547 | tile_map_wide_toolbar->add_child(tab_data.wide_toolbar); |
| 4548 | } |
| 4549 | } |
| 4550 | |
| 4551 | // Wide empty separation control. (like BoxContainer::add_spacer()) |
| 4552 | Control *c = memnew(Control); |
| 4553 | c->set_mouse_filter(MOUSE_FILTER_PASS); |
| 4554 | c->set_h_size_flags(SIZE_EXPAND_FILL); |
| 4555 | tile_map_toolbar->add_child(c); |
| 4556 | |
| 4557 | // Layer selector. |
| 4558 | layer_selection_hbox = memnew(BoxContainer); |
| 4559 | tile_map_toolbar->add_child(layer_selection_hbox); |
| 4560 | |
| 4561 | layers_selection_button = memnew(OptionButton); |
| 4562 | layers_selection_button->set_custom_minimum_size(Size2(200, 0)); |
| 4563 | layers_selection_button->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS); |
| 4564 | layers_selection_button->set_tooltip_text(TTR("TileMap Layers")); |
| 4565 | layers_selection_button->connect(SceneStringName(item_selected), callable_mp(this, &TileMapLayerEditor::_layers_selection_item_selected)); |
| 4566 | layer_selection_hbox->add_child(layers_selection_button); |
| 4567 | |
| 4568 | select_previous_layer = memnew(Button); |
| 4569 | select_previous_layer->set_theme_type_variation(SceneStringName(FlatButton)); |
| 4570 | select_previous_layer->set_tooltip_text(TTR("Select previous layer")); |
| 4571 | select_previous_layer->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditor::_select_previous_layer_pressed)); |
| 4572 | layer_selection_hbox->add_child(select_previous_layer); |
| 4573 | |
| 4574 | select_next_layer = memnew(Button); |
| 4575 | select_next_layer->set_theme_type_variation(SceneStringName(FlatButton)); |
| 4576 | select_next_layer->set_tooltip_text(TTR("Select next layer")); |
| 4577 | select_next_layer->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditor::_select_next_layer_pressed)); |
| 4578 | layer_selection_hbox->add_child(select_next_layer); |
| 4579 | |
| 4580 | select_all_layers = memnew(Button); |
| 4581 | select_all_layers->set_theme_type_variation(SceneStringName(FlatButton)); |
| 4582 | select_all_layers->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditor::_select_all_layers_pressed)); |
| 4583 | select_all_layers->set_tooltip_text(TTR("Select all TileMapLayers in scene")); |
| 4584 | layer_selection_hbox->add_child(select_all_layers); |
| 4585 | |
| 4586 | // Highlighting selected layer. |
| 4587 | toggle_highlight_selected_layer_button = memnew(Button); |
| 4588 | toggle_highlight_selected_layer_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 4589 | toggle_highlight_selected_layer_button->set_toggle_mode(true); |
| 4590 | toggle_highlight_selected_layer_button->connect(SceneStringName(toggled), callable_mp(this, &TileMapLayerEditor::_highlight_selected_layer_button_toggled)); |
| 4591 | toggle_highlight_selected_layer_button->set_tooltip_text(TTR("Highlight Selected TileMap Layer")); |
| 4592 | tile_map_toolbar->add_child(toggle_highlight_selected_layer_button); |
| 4593 | |
| 4594 | layer_selector_separator = memnew(SwitchSeparator); |
| 4595 | layer_selector_separator->set_vertical(false); |
| 4596 | tile_map_toolbar->add_child(layer_selector_separator); |
| 4597 | |
| 4598 | // Grid toggle. |
| 4599 | toggle_grid_button = memnew(Button); |
| 4600 | toggle_grid_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 4601 | toggle_grid_button->set_toggle_mode(true); |
| 4602 | toggle_grid_button->set_tooltip_text(TTR("Toggle grid visibility.")); |
| 4603 | toggle_grid_button->connect(SceneStringName(toggled), callable_mp(this, &TileMapLayerEditor::_on_grid_toggled)); |
| 4604 | tile_map_toolbar->add_child(toggle_grid_button); |
| 4605 | |
| 4606 | // Advanced settings menu button. |
| 4607 | advanced_menu_button = memnew(MenuButton); |
| 4608 | advanced_menu_button->set_flat(false); |
| 4609 | advanced_menu_button->set_tooltip_text(TTRC("Advanced settings.")); |
| 4610 | advanced_menu_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 4611 | advanced_menu_button->get_popup()->add_item(TTR("Automatically Replace Tiles with Proxies"), ADVANCED_MENU_REPLACE_WITH_PROXIES); |
| 4612 | advanced_menu_button->get_popup()->add_item(TTR("Extract TileMap layers as individual TileMapLayer nodes"), ADVANCED_MENU_EXTRACT_TILE_MAP_LAYERS); |
| 4613 | advanced_menu_button->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TileMapLayerEditor::_advanced_menu_button_id_pressed)); |
| 4614 | tile_map_toolbar->add_child(advanced_menu_button); |
| 4615 | |
| 4616 | // A label for editing errors. |
| 4617 | cant_edit_label = memnew(Label); |
| 4618 | cant_edit_label->set_focus_mode(Control::FOCUS_ACCESSIBILITY); |
| 4619 | cant_edit_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART); |
| 4620 | cant_edit_label->set_anchors_and_offsets_preset(Control::PRESET_HCENTER_WIDE); |
| 4621 | cant_edit_label->set_h_size_flags(SIZE_EXPAND_FILL); |
| 4622 | cant_edit_label->set_v_size_flags(SIZE_EXPAND_FILL); |
| 4623 | cant_edit_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
| 4624 | cant_edit_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER); |
| 4625 | cant_edit_label->hide(); |
| 4626 | main_box_container->add_child(cant_edit_label); |
| 4627 | |
| 4628 | for (unsigned int tab_index = 0; tab_index < tabs_data.size(); tab_index++) { |
| 4629 | main_box_container->add_child(tabs_data[tab_index].panel); |
| 4630 | tabs_data[tab_index].panel->set_v_size_flags(SIZE_EXPAND_FILL); |
| 4631 | tabs_data[tab_index].panel->set_visible(tab_index == 0); |
| 4632 | tabs_data[tab_index].panel->set_h_size_flags(SIZE_EXPAND_FILL); |
| 4633 | } |
| 4634 | |
| 4635 | _tab_changed(0); |
| 4636 | |
| 4637 | // Registers UndoRedo inspector callback. |
| 4638 | EditorNode::get_editor_data().add_move_array_element_function(SNAME("TileMap"), callable_mp(this, &TileMapLayerEditor::_move_tile_map_array_element)); |
| 4639 | } |
| 4640 | |
| 4641 | TileMapLayerEditor::~TileMapLayerEditor() { |
| 4642 | for (int i = 0; i < tile_map_editor_plugins.size(); i++) { |
| 4643 | memdelete(tile_map_editor_plugins[i]); |
| 4644 | } |
| 4645 | } |