The TileBrushManual class implements a manual tile brush within the tilemapper system. It enforces the use of a single specified tile index from the tileset and prevents surrounding autotile refresh logic from modifying manually placed tiles.
using Sandbox;
using System.Collections.Generic;
using Saandy.Tilemapper;
namespace Saandy.Tilemapper;
public class TileBrushManual : TileBrush
{
public override string Name { get; protected set; } = "Manual Brush";
public override ushort GetSpriteIndexToSet( TileMap tilemap, TilesetResource tileset, int x, int y )
{
if ( tileset == null )
return 0;
if ( tileset.Tiles == null || tileset.Tiles.Count == 0 )
return 0;
return (ushort)System.Math.Clamp( tileset.ManualTileIndex, 0, tileset.Tiles.Count - 1 );
}
public override (bool IsVisible, bool UseCollider) GetDefaultTileFlags( ushort spriteIndex )
{
return (true, true);
}
public override (TilesetResource Tileset, ushort TileId, bool IsVisible, bool UseCollider) GetTileToSet(
TileMap tilemap,
TilesetResource tileset,
TileMap.Tile existingTile,
int x,
int y,
bool isRefresh )
{
// If another autotile brush refreshes this manual tile as an affected neighbour,
// preserve the manually painted tile id and flags. Manual tiles do not derive
// their sprite from adjacency.
if ( isRefresh && !existingTile.IsEmpty )
return (tileset, existingTile.TileId, existingTile.IsVisible, existingTile.UseCollider);
return base.GetTileToSet( tilemap, tileset, existingTile, x, y, isRefresh );
}
protected override IEnumerable<Vector2Int> GetAffectedTiles( Vector2Int cell )
{
// Manual painting only changes the cells the user actually painted.
// No surrounding autotile refresh is needed.
yield break;
}
}