From 7effe46461090861cae417f30db29e4c14a83db2 Mon Sep 17 00:00:00 2001 From: Poommetee Ketson Date: Sat, 24 Feb 2018 12:56:48 +0700 Subject: [PATCH] TileMap: add fix_invalid_tiles --- doc/classes/TileMap.xml | 7 +++++++ editor/plugins/tile_map_editor_plugin.cpp | 11 +++++++++++ editor/plugins/tile_map_editor_plugin.h | 1 + scene/2d/tile_map.cpp | 11 +++++++++++ scene/2d/tile_map.h | 1 + 5 files changed, 31 insertions(+) diff --git a/doc/classes/TileMap.xml b/doc/classes/TileMap.xml index 0a5fc1951b9..d7bc3a7ac27 100644 --- a/doc/classes/TileMap.xml +++ b/doc/classes/TileMap.xml @@ -19,6 +19,13 @@ Clear all cells. + + + + + Clear cells that does not exist in the tileset. + + diff --git a/editor/plugins/tile_map_editor_plugin.cpp b/editor/plugins/tile_map_editor_plugin.cpp index c97e949403a..6e26a7f4ca7 100644 --- a/editor/plugins/tile_map_editor_plugin.cpp +++ b/editor/plugins/tile_map_editor_plugin.cpp @@ -139,6 +139,15 @@ void TileMapEditor::_menu_option(int p_option) { canvas_item_editor->update(); } break; + case OPTION_FIX_INVALID: { + + undo_redo->create_action(TTR("Fix Invalid Tiles")); + undo_redo->add_undo_method(node, "set", "tile_data", node->get("tile_data")); + node->fix_invalid_tiles(); + undo_redo->add_do_method(node, "set", "tile_data", node->get("tile_data")); + undo_redo->commit_action(); + + } break; } } @@ -1575,6 +1584,8 @@ TileMapEditor::TileMapEditor(EditorNode *p_editor) { p->add_shortcut(ED_SHORTCUT("tile_map_editor/select", TTR("Select"), KEY_MASK_CMD + KEY_B), OPTION_SELECT); p->add_shortcut(ED_SHORTCUT("tile_map_editor/duplicate_selection", TTR("Duplicate Selection"), KEY_MASK_CMD + KEY_D), OPTION_DUPLICATE); p->add_shortcut(ED_GET_SHORTCUT("tile_map_editor/erase_selection"), OPTION_ERASE_SELECTION); + p->add_separator(); + p->add_item(TTR("Fix Invalid Tiles"), OPTION_FIX_INVALID); p->connect("id_pressed", this, "_menu_option"); diff --git a/editor/plugins/tile_map_editor_plugin.h b/editor/plugins/tile_map_editor_plugin.h index 0a937e200ed..2d582d030b3 100644 --- a/editor/plugins/tile_map_editor_plugin.h +++ b/editor/plugins/tile_map_editor_plugin.h @@ -71,6 +71,7 @@ class TileMapEditor : public VBoxContainer { OPTION_DUPLICATE, OPTION_ERASE_SELECTION, OPTION_PAINTING, + OPTION_FIX_INVALID, }; TileMap *node; diff --git a/scene/2d/tile_map.cpp b/scene/2d/tile_map.cpp index 2aa55e28257..5f11804dd7f 100644 --- a/scene/2d/tile_map.cpp +++ b/scene/2d/tile_map.cpp @@ -830,6 +830,16 @@ void TileMap::update_dirty_bitmask() { } } +void TileMap::fix_invalid_tiles() { + + for (Map::Element *E = tile_map.front(); E; E = E->next()) { + + if (!tile_set->has_tile(get_cell(E->key().x, E->key().y))) { + set_cell(E->key().x, E->key().y, INVALID_CELL); + } + } +} + int TileMap::get_cell(int p_x, int p_y) const { PosKey pk(p_x, p_y); @@ -1515,6 +1525,7 @@ void TileMap::_bind_methods() { ClassDB::bind_method(D_METHOD("is_cell_y_flipped", "x", "y"), &TileMap::is_cell_y_flipped); ClassDB::bind_method(D_METHOD("is_cell_transposed", "x", "y"), &TileMap::is_cell_transposed); + ClassDB::bind_method(D_METHOD("fix_invalid_tiles"), &TileMap::fix_invalid_tiles); ClassDB::bind_method(D_METHOD("clear"), &TileMap::clear); ClassDB::bind_method(D_METHOD("get_used_cells"), &TileMap::get_used_cells); diff --git a/scene/2d/tile_map.h b/scene/2d/tile_map.h index 973e527b420..6ee3e5dd58d 100644 --- a/scene/2d/tile_map.h +++ b/scene/2d/tile_map.h @@ -308,6 +308,7 @@ public: void set_clip_uv(bool p_enable); bool get_clip_uv() const; + void fix_invalid_tiles(); void clear(); TileMap();