An editor TileBrush implementation for a manual brush in a tilemap editor. It returns a fixed manual tile index from the tileset, preserves manually painted tiles when refreshed, sets default tile flags to visible and collidable, and declares that it only affects the painted cell.
using Sandbox;
using System.Collections.Generic;
using Saandy.Tilemapper;
namespace Saandy.Editor.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;
}
}