Represents a single tile definition in a tileset. Stores an Id, name, source rectangle, tags, and a back-reference to the TilesetResource, and computes UV coordinates for the tile within the tileset texture.
using Sandbox;
using System;
using System.Text.Json.Serialization;
namespace Saandy.Tilemapper;
public sealed class TileDefinition
{
[Property, Hide]
public Guid Id { get; set; }
[Property]
public string Name { get; set; }
[Property]
public Rect SourceRect { get; set; }
[Property, Group( "Tags" ), Title( "Tile Tags" )]
public TagSet Tags { get; set; } = new();
[JsonIgnore, Hide]
public TilesetResource Tileset { get; set; }
public Vector4 GetUvRect()
{
if ( Tileset == null )
return new Vector4( 0, 0, 1, 1 );
Vector2Int textureSize = Tileset.GetTextureSize();
float textureWidth = Math.Max( 1.0f, textureSize.x );
float textureHeight = Math.Max( 1.0f, textureSize.y );
return new Vector4(
SourceRect.Left / textureWidth,
SourceRect.Top / textureHeight,
SourceRect.Width / textureWidth,
SourceRect.Height / textureHeight
);
}
}