From 6b5a4f2b1e57e7fd1604e6d075308a0b31cd5b55 Mon Sep 17 00:00:00 2001 From: StevenRafft Date: Sat, 9 Nov 2024 18:46:37 +0200 Subject: [PATCH] [TileMapLayer] Add set_cells to set multiple cells at once --- doc/classes/TileMapLayer.xml | 10 ++++++++++ scene/2d/tile_map_layer.cpp | 8 ++++++++ scene/2d/tile_map_layer.h | 1 + 3 files changed, 19 insertions(+) diff --git a/doc/classes/TileMapLayer.xml b/doc/classes/TileMapLayer.xml index 135f85de693c..ff14dd80c574 100644 --- a/doc/classes/TileMapLayer.xml +++ b/doc/classes/TileMapLayer.xml @@ -220,6 +220,16 @@ If [param source_id] is set to [code]-1[/code], [param atlas_coords] to [code]Vector2i(-1, -1)[/code], or [param alternative_tile] to [code]-1[/code], the cell will be erased. An erased cell gets [b]all[/b] its identifiers automatically set to their respective invalid values, namely [code]-1[/code], [code]Vector2i(-1, -1)[/code] and [code]-1[/code]. + + + + + + + + Sets the tile identifiers for all the cells inside the [param cells] array. + + diff --git a/scene/2d/tile_map_layer.cpp b/scene/2d/tile_map_layer.cpp index f737e2312613..b190f93b178c 100644 --- a/scene/2d/tile_map_layer.cpp +++ b/scene/2d/tile_map_layer.cpp @@ -1783,6 +1783,7 @@ void TileMapLayer::_bind_methods() { // --- Cells manipulation --- // Generic cells manipulations and access. ClassDB::bind_method(D_METHOD("set_cell", "coords", "source_id", "atlas_coords", "alternative_tile"), &TileMapLayer::set_cell, DEFVAL(TileSet::INVALID_SOURCE), DEFVAL(TileSetSource::INVALID_ATLAS_COORDS), DEFVAL(0)); + ClassDB::bind_method(D_METHOD("set_cells", "cells", "source_id", "atlas_coords", "alternative_tile"), &TileMapLayer::set_cells, DEFVAL(TileSet::INVALID_SOURCE), DEFVAL(TileSetSource::INVALID_ATLAS_COORDS), DEFVAL(0)); ClassDB::bind_method(D_METHOD("erase_cell", "coords"), &TileMapLayer::erase_cell); ClassDB::bind_method(D_METHOD("fix_invalid_tiles"), &TileMapLayer::fix_invalid_tiles); ClassDB::bind_method(D_METHOD("clear"), &TileMapLayer::clear); @@ -2382,6 +2383,13 @@ void TileMapLayer::set_cell(const Vector2i &p_coords, int p_source_id, const Vec used_rect_cache_dirty = true; } +void TileMapLayer::set_cells(TypedArray p_coords_array, int p_source_id, const Vector2i &p_atlas_coords, int p_alternative_tile) { + for (const Variant &variant : p_coords_array) { + const Vector2i &E = variant; + set_cell(E, p_source_id, p_atlas_coords, p_alternative_tile); + } +} + void TileMapLayer::erase_cell(const Vector2i &p_coords) { set_cell(p_coords, TileSet::INVALID_SOURCE, TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE); } diff --git a/scene/2d/tile_map_layer.h b/scene/2d/tile_map_layer.h index 6cb481849c54..2b440bd6813a 100644 --- a/scene/2d/tile_map_layer.h +++ b/scene/2d/tile_map_layer.h @@ -428,6 +428,7 @@ class TileMapLayer : public Node2D { // --- Cells manipulation --- // Generic cells manipulations and data access. void set_cell(const Vector2i &p_coords, int p_source_id = TileSet::INVALID_SOURCE, const Vector2i &p_atlas_coords = TileSetSource::INVALID_ATLAS_COORDS, int p_alternative_tile = 0); + void set_cells(TypedArray p_coords_array, int p_source_id = TileSet::INVALID_SOURCE, const Vector2i &p_atlas_coords = TileSetSource::INVALID_ATLAS_COORDS, int p_alternative_tile = 0); void erase_cell(const Vector2i &p_coords); void fix_invalid_tiles(); void clear();