// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. using System; namespace FlaxEngine { partial struct SpriteHandle { /// /// Invalid sprite handle. /// public static SpriteHandle Invalid; /// /// Initializes a new instance of the struct. /// /// The atlas. /// The index. public SpriteHandle(SpriteAtlas atlas, int index) { Atlas = atlas; Index = index; } /// /// Returns true if sprite is valid. /// public bool IsValid => Atlas != null && Index != -1; /// /// Gets or sets the sprite name. /// [NoSerialize] public string Name { get { if (Atlas == null) throw new InvalidOperationException("Cannot use invalid sprite."); return Atlas.GetSprite(Index).Name; } set { if (Atlas == null) throw new InvalidOperationException("Cannot use invalid sprite."); var sprite = Atlas.GetSprite(Index); sprite.Name = value; Atlas.SetSprite(Index, ref sprite); } } /// /// Gets or sets the sprite location (in pixels). /// [NoSerialize] public Float2 Location { get => Area.Location * Atlas.Size; set { var area = Area; area.Location = value / Atlas.Size; Area = area; } } /// /// Gets or sets the sprite size (in pixels). /// [NoSerialize] public Float2 Size { get => Area.Size * Atlas.Size; set { var area = Area; area.Size = value / Atlas.Size; Area = area; } } /// /// Gets or sets the sprite area in atlas (in normalized atlas coordinates [0;1]). /// [NoSerialize] public Rectangle Area { get { if (Atlas == null) throw new InvalidOperationException("Cannot use invalid sprite."); return Atlas.GetSprite(Index).Area; } set { if (Atlas == null) throw new InvalidOperationException("Cannot use invalid sprite."); var sprite = Atlas.GetSprite(Index); sprite.Area = value; Atlas.SetSprite(Index, ref sprite); } } } }