diff --git a/editor/plugins/tiles/tile_map_layer_editor.cpp b/editor/plugins/tiles/tile_map_layer_editor.cpp index 5d42115c3cc4..338ba79b4442 100644 --- a/editor/plugins/tiles/tile_map_layer_editor.cpp +++ b/editor/plugins/tiles/tile_map_layer_editor.cpp @@ -52,6 +52,12 @@ TileMapLayer *TileMapLayerSubEditorPlugin::_get_edited_layer() const { return ObjectDB::get_instance(edited_tile_map_layer_id); } +void TileMapLayerSubEditorPlugin::_add_to_output_if_tile_changed(HashMap &p_output, const TileMapLayer *p_layer, Vector2i p_coords, TileMapCell p_cell) { + if (p_cell != p_layer->get_cell(p_coords)) { + p_output[p_coords] = p_cell; + } +} + void TileMapLayerSubEditorPlugin::draw_tile_coords_over_viewport(Control *p_overlay, const TileMapLayer *p_edited_layer, Ref p_tile_set, bool p_show_rectangle_size, const Vector2i &p_rectangle_origin) { Point2 msgpos = Point2(20 * EDSCALE, p_overlay->get_size().y - 20 * EDSCALE); String text = String(p_tile_set->local_to_map(p_edited_layer->get_local_mouse_position())); @@ -1074,7 +1080,7 @@ HashMap TileMapLayerEditorTilesPlugin::_draw_line(Vector2 // Paint a random tile. Vector line = TileMapLayerEditor::get_line(edited_layer, tile_set->local_to_map(p_from_mouse_pos), tile_set->local_to_map(p_to_mouse_pos)); for (int i = 0; i < line.size(); i++) { - output.insert(line[i], _pick_random_tile(pattern)); + _add_to_output_if_tile_changed(output, edited_layer, line[i], _pick_random_tile(pattern)); } } else { // Paint the pattern. @@ -1091,7 +1097,7 @@ HashMap TileMapLayerEditorTilesPlugin::_draw_line(Vector2 Vector2i top_left = line[i] * pattern->get_size() + offset; for (int j = 0; j < used_cells.size(); j++) { Vector2i coords = tile_set->map_pattern(top_left, used_cells[j], pattern); - output.insert(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]))); + _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]))); } } } @@ -1132,7 +1138,7 @@ HashMap TileMapLayerEditorTilesPlugin::_draw_rect(Vector2 for (int x = 0; x < rect.size.x; x++) { for (int y = 0; y < rect.size.y; y++) { Vector2i coords = rect.position + Vector2i(x, y); - output.insert(coords, _pick_random_tile(pattern)); + _add_to_output_if_tile_changed(output, edited_layer, coords, _pick_random_tile(pattern)); } } } else { @@ -1144,7 +1150,7 @@ HashMap TileMapLayerEditorTilesPlugin::_draw_rect(Vector2 for (int j = 0; j < used_cells.size(); j++) { Vector2i coords = pattern_coords + used_cells[j]; if (rect.has_point(coords)) { - output.insert(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]))); + _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]))); } } } @@ -1195,16 +1201,16 @@ HashMap TileMapLayerEditorTilesPlugin::_draw_bucket_fill( (source_cell.source_id != TileSet::INVALID_SOURCE || boundaries.has_point(coords))) { if (!p_erase && random_tile_toggle->is_pressed()) { // Paint a random tile. - output.insert(coords, _pick_random_tile(pattern)); + _add_to_output_if_tile_changed(output, edited_layer, coords, _pick_random_tile(pattern)); } else { // Paint the pattern. Vector2i pattern_coords = (coords - p_coords) % pattern->get_size(); // Note: it would be good to have posmodv for Vector2i. pattern_coords.x = pattern_coords.x < 0 ? pattern_coords.x + pattern->get_size().x : pattern_coords.x; pattern_coords.y = pattern_coords.y < 0 ? pattern_coords.y + pattern->get_size().y : pattern_coords.y; if (pattern->has_cell(pattern_coords)) { - output.insert(coords, TileMapCell(pattern->get_cell_source_id(pattern_coords), pattern->get_cell_atlas_coords(pattern_coords), pattern->get_cell_alternative_tile(pattern_coords))); + _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))); } else { - output.insert(coords, TileMapCell()); + _add_to_output_if_tile_changed(output, edited_layer, coords, TileMapCell()); } } @@ -1241,16 +1247,16 @@ HashMap TileMapLayerEditorTilesPlugin::_draw_bucket_fill( (source_cell.source_id != TileSet::INVALID_SOURCE || boundaries.has_point(coords))) { if (!p_erase && random_tile_toggle->is_pressed()) { // Paint a random tile. - output.insert(coords, _pick_random_tile(pattern)); + _add_to_output_if_tile_changed(output, edited_layer, coords, _pick_random_tile(pattern)); } else { // Paint the pattern. Vector2i pattern_coords = (coords - p_coords) % pattern->get_size(); // Note: it would be good to have posmodv for Vector2i. pattern_coords.x = pattern_coords.x < 0 ? pattern_coords.x + pattern->get_size().x : pattern_coords.x; pattern_coords.y = pattern_coords.y < 0 ? pattern_coords.y + pattern->get_size().y : pattern_coords.y; if (pattern->has_cell(pattern_coords)) { - output.insert(coords, TileMapCell(pattern->get_cell_source_id(pattern_coords), pattern->get_cell_atlas_coords(pattern_coords), pattern->get_cell_alternative_tile(pattern_coords))); + _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))); } else { - output.insert(coords, TileMapCell()); + _add_to_output_if_tile_changed(output, edited_layer, coords, TileMapCell()); } } } @@ -2533,7 +2539,7 @@ HashMap TileMapLayerEditorTerrainsPlugin::_draw_terrain_p for (const KeyValue &kv : terrain_fill_output) { if (painted_set.has(kv.key)) { // Paint a random tile with the correct terrain for the painted path. - output[kv.key] = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value); + _add_to_output_if_tile_changed(output, edited_layer, kv.key, tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value)); } else { // Avoids updating the painted path from the output if the new pattern is the same as before. TileSet::TerrainsPattern in_map_terrain_pattern = TileSet::TerrainsPattern(*tile_set, p_terrain_set); @@ -2550,7 +2556,7 @@ HashMap TileMapLayerEditorTerrainsPlugin::_draw_terrain_p } } if (in_map_terrain_pattern != kv.value) { - output[kv.key] = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value); + _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,7 +2586,7 @@ HashMap TileMapLayerEditorTerrainsPlugin::_draw_terrain_p for (const KeyValue &kv : terrain_fill_output) { if (painted_set.has(kv.key)) { // Paint a random tile with the correct terrain for the painted path. - output[kv.key] = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value); + _add_to_output_if_tile_changed(output, edited_layer, kv.key, tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value)); } else { // Avoids updating the painted path from the output if the new pattern is the same as before. TileSet::TerrainsPattern in_map_terrain_pattern = TileSet::TerrainsPattern(*tile_set, p_terrain_set); @@ -2597,7 +2603,7 @@ HashMap TileMapLayerEditorTerrainsPlugin::_draw_terrain_p } } if (in_map_terrain_pattern != kv.value) { - output[kv.key] = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value); + _add_to_output_if_tile_changed(output, edited_layer, kv.key, tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value)); } } } diff --git a/editor/plugins/tiles/tile_map_layer_editor.h b/editor/plugins/tiles/tile_map_layer_editor.h index af78f4577ed9..59a22e1a50ef 100644 --- a/editor/plugins/tiles/tile_map_layer_editor.h +++ b/editor/plugins/tiles/tile_map_layer_editor.h @@ -52,6 +52,7 @@ class TileMapLayerSubEditorPlugin : public Object { protected: ObjectID edited_tile_map_layer_id; TileMapLayer *_get_edited_layer() const; + static void _add_to_output_if_tile_changed(HashMap &p_output, const TileMapLayer *p_layer, Vector2i p_coords, TileMapCell p_cell); public: struct TabData {