#region CPL License /* Nuclex Framework Copyright (C) 2002-2009 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #endregion using System; using System.Collections.Generic; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace Nuclex.Graphics { /// Represents a limited region of a texture /// /// This is similar to .NET's ArraySegment class, only that it applies to /// a two-dimensional texture instead of a one-dimensional array. /// public struct TextureRegion2D { /// Initializes a new texture region using a full texture /// Texture that will be used in full public TextureRegion2D(Texture2D texture) : this(texture, Vector2.Zero, Vector2.One) { } /// Initializes a new texture region using part of a texture /// Texture of which a region will be used /// Minimum X texture coordinate (normalized) of the region /// Minimum Y texture coordinate (normalized) of the region /// Maximum X texture coordinate (normalized) of the region /// Maximum Y texture coordinate (normalized) of the region public TextureRegion2D( Texture2D texture, float minX, float minY, float maxX, float maxY ) : this(texture, new Vector2(minX, minY), new Vector2(maxX, maxY)) { } /// Initializes a new texture region using part of a texture /// Texture of which a region will be used /// Minimum texture coordinates (normalized) of the region /// Maximum texture coordinates (normalized) of the region public TextureRegion2D(Texture2D texture, Vector2 min, Vector2 max) { this.Texture = texture; this.Min = min; this.Max = max; } /// Initializes a new texture region using part of a texture /// Texture of which a region will be used /// Minimum texture coordinates of the region /// Maximum texture coordinates of the region public static TextureRegion2D FromTexels(Texture2D texture, Point min, Point max) { float width = (float)texture.Width; float height = (float)texture.Height; return new TextureRegion2D( texture, min.X / width, min.Y / height, max.X / width, max.Y / height ); } /// Initializes a new texture region using part of a texture /// Texture of which a region will be used /// Minimum X texture coordinate of the region /// Minimum Y texture coordinate of the region /// Maximum X texture coordinate of the region /// Maximum Y texture coordinate of the region public static TextureRegion2D FromTexels( Texture2D texture, int minX, int minY, int maxX, int maxY ) { float width = (float)texture.Width; float height = (float)texture.Height; return new TextureRegion2D( texture, minX / width, minY / height, maxX / width, maxY / height ); } /// Returns the hash code for the current instance /// A 32-bit signed integer hash code public override int GetHashCode() { return this.Texture.GetHashCode() ^ this.Min.GetHashCode() ^ this.Max.GetHashCode(); } /// /// Determines whether the specified object is equal to the current instance /// /// /// True if the specified object is a structure and is /// equal to the current instance; otherwise, false /// /// The object to be compared with the current instance public override bool Equals(object other) { return (other is TextureRegion2D) && this.Equals((TextureRegion2D)other); } /// /// Determines whether the specified structure is equal /// to the current instance /// /// /// True if the specified structure is equal to the /// current instance; otherwise, false /// /// /// The structure to be compared with /// the current instance /// public bool Equals(TextureRegion2D other) { return (other.Texture == this.Texture) && (other.Min == this.Min) && (other.Max == this.Max); } /// /// Indicates whether two structures are equal /// /// True if a is equal to b; otherwise, false /// /// The structure on the left side of the /// equality operator /// /// /// The structure on the right side of the /// equality operator /// public static bool operator ==(TextureRegion2D left, TextureRegion2D right) { return (left.Texture == right.Texture) && (left.Min == right.Min) && (left.Max == right.Max); } /// /// Indicates whether two structures are unequal /// /// True if a is not equal to b; otherwise, false /// /// The structure on the left side of the /// inequality operator /// /// /// The structure on the right side of the /// inequality operator /// public static bool operator !=(TextureRegion2D left, TextureRegion2D right) { return (left.Texture != right.Texture) || (left.Min != right.Min) || (left.Max != right.Max); } /// Returns a string representation of the string segment /// The string representation of the string segment public override string ToString() { string textureString = this.Texture.ToString(); string minString = this.Min.ToString(); string maxString = this.Max.ToString(); StringBuilder stringBuilder = new StringBuilder( textureString.Length + 2 + minString.Length + 3 + maxString.Length + 1 ); stringBuilder.Append(textureString); stringBuilder.Append(" {"); stringBuilder.Append(minString); stringBuilder.Append(" - "); stringBuilder.Append(maxString); stringBuilder.Append('}'); return stringBuilder.ToString(); } /// Texture from which a region is used public Texture2D Texture; /// Minimum coordinates of the region on the texture /// /// These coordinates are normalized: 0,0 is the lower left corner and /// 1,1 is the upper right corner. /// public Vector2 Min; /// Maximum coordinates of the region on the texture /// /// These coordinates are normalized: 0,0 is the lower left corner and /// 1,1 is the upper right corner. /// public Vector2 Max; } } // namespace Nuclex.Graphics