using System; using System.IO; using JetBrains.Annotations; using Robust.Shared.Graphics; using Robust.Shared.Graphics.RSI; using Robust.Shared.IoC; using Robust.Shared.Maths; using SixLabors.ImageSharp; using SixLabors.ImageSharp.PixelFormats; using Color = Robust.Shared.Maths.Color; namespace Robust.Client.Graphics; /// /// Contains a texture used for drawing things. /// [PublicAPI] public abstract class Texture : IRsiStateLike { /// /// The width of the texture, in pixels. /// public int Width => Size.X; /// /// The height of the texture, in pixels. /// public int Height => Size.Y; /// /// The size of the texture, in pixels. /// public Vector2i Size { get; /*protected set;*/ } public Color this[int x, int y] => this.GetPixel(x, y); protected Texture(Vector2i size) { Size = size; } Texture IDirectionalTextureProvider.Default => this; Texture IDirectionalTextureProvider.TextureFor(Direction dir) { return this; } RsiDirectionType IRsiStateLike.RsiDirections => RsiDirectionType.Dir1; bool IRsiStateLike.IsAnimated => false; int IRsiStateLike.AnimationFrameCount => 0; float IRsiStateLike.GetDelay(int frame) { if (frame != 0) throw new IndexOutOfRangeException(); return 0; } Texture IRsiStateLike.GetFrame(RsiDirection dir, int frame) { if (frame != 0) throw new IndexOutOfRangeException(); return this; } public abstract Color GetPixel(int x, int y); public static Texture Transparent => IoCManager.Resolve().GetStockTexture(ClydeStockTexture.Transparent); public static Texture White => IoCManager.Resolve().GetStockTexture(ClydeStockTexture.White); public static Texture Black => IoCManager.Resolve().GetStockTexture(ClydeStockTexture.Black); /// /// Loads a new texture an existing image. /// /// The image to load. /// The "name" of this texture. This can be referred to later to aid debugging. /// /// Parameters that influence the loading of textures. /// Defaults to if null. /// /// The type of pixels of the image. At the moment, images must be . public static Texture LoadFromImage(Image image, string? name = null, TextureLoadParameters? loadParameters = null) where T : unmanaged, IPixel { var manager = IoCManager.Resolve(); return manager.LoadTextureFromImage(image, name, loadParameters); } /// /// Loads an image from a stream containing PNG data. /// /// The stream to load the image from. /// The "name" of this texture. This can be referred to later to aid debugging. /// /// Parameters that influence the loading of textures. /// Defaults to if null. /// public static Texture LoadFromPNGStream(Stream stream, string? name = null, TextureLoadParameters? loadParameters = null) { var manager = IoCManager.Resolve(); return manager.LoadTextureFromPNGStream(stream, name, loadParameters); } }