Browse Source

TileMap: add fix_invalid_tiles

Poommetee Ketson 7 years ago
parent
commit
7effe46461

+ 7 - 0
doc/classes/TileMap.xml

@@ -19,6 +19,13 @@
 				Clear all cells.
 			</description>
 		</method>
+		<method name="fix_invalid_tiles">
+			<return type="void">
+			</return>
+			<description>
+				Clear cells that does not exist in the tileset.
+			</description>
+		</method>
 		<method name="get_cell" qualifiers="const">
 			<return type="int">
 			</return>

+ 11 - 0
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");
 

+ 1 - 0
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;

+ 11 - 0
scene/2d/tile_map.cpp

@@ -830,6 +830,16 @@ void TileMap::update_dirty_bitmask() {
 	}
 }
 
+void TileMap::fix_invalid_tiles() {
+
+	for (Map<PosKey, Cell>::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);

+ 1 - 0
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();