Code/Brush/TileBrushManual.cs

The TileBrushManual class implements a manual tile brush for tilemap creation. It defines how manual tile painting differs from automatic autotiling, preserving existing tile data during manual paint operations.

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;
	}
}