using System;
namespace Otter {
///
/// Graphic that renders part of a sprite sheet, but does not automatically animate it at all.
///
public class ImageSet : Image {
#region Private Fields
int frame = 0;
#endregion
#region Public Properties
///
/// The number of rows on the image sheet.
///
public int Rows { get; private set; }
///
/// The number of columns on the image sheet.
///
public int Columns { get; private set; }
///
/// The number of frames on the image sheet.
///
public int Frames { get; private set; }
///
/// The frame to render from the image set.
///
public int Frame {
get { return frame; }
set {
frame = (int)Util.Clamp(value, 0, Frames - 1);
UpdateTextureRegion(frame);
}
}
#endregion
#region Constructors
///
/// Create a new ImageSet from a file path for a texture.
///
/// The file path to the texture to use for the image sheet.
/// The width of each cell on the image sheet.
/// The height of each cell on the image sheet.
public ImageSet(string source, int width, int height) : base(source) {
Initialize(width, height);
}
///
/// Create a new ImageSet from a Texture.
///
/// The Texture to use for the image sheet.
/// The width of each cell on the image sheet.
/// The height of each cell on the image sheet.
public ImageSet(Texture texture, int width, int height) : base(texture) {
Initialize(width, height);
}
///
/// Create a new ImageSet from an AtlasTexture.
///
/// The AtlasTexture to use for the image sheet.
/// The width of each cell on the image sheet.
/// The height of each cell on the image sheet.
public ImageSet(AtlasTexture texture, int width, int height) : base(texture) {
Initialize(width, height);
}
#endregion
#region Private Methods
void Initialize(int width, int height) {
Width = width;
Height = height;
ClippingRegion = new Rectangle(0, 0, Width, Height);
// Proper sprite batching coming soon.
//Batchable = true;
Columns = (int)Math.Ceiling((float)TextureRegion.Width / width);
Rows = (int)Math.Ceiling((float)TextureRegion.Height / height);
Frames = Columns * Rows;
UpdateTextureRegion(0);
}
///
/// Updates the internal source for the texture.
///
/// The frame in terms of the sprite sheet.
void UpdateTextureRegion(int frame) {
var top = (int)(Math.Floor((float)frame / Columns) * Height);
var left = (int)((frame % Columns) * Width);
if (TextureRegion != new Rectangle(left, top, Width, Height)) {
NeedsUpdate = true;
}
TextureRegion = new Rectangle(left, top, Width, Height);
}
#endregion
}
}