Improved interoperation with System.Drawing with new addition, subtraction and implicit conversion operators, as well as additional method overloads.

Only expose fields through public properties to allow for parameter validation.
This commit is contained in:
the_fiddler 2009-11-02 07:20:59 +00:00
parent af2d77cdc5
commit 600f657b70
3 changed files with 292 additions and 86 deletions

View file

@ -1,4 +1,4 @@
#region License #region License
// //
// The Open Toolkit Library License // The Open Toolkit Library License
// //
@ -37,25 +37,7 @@ namespace OpenTK
{ {
#region Fields #region Fields
/// <summary> int x, y;
/// The X coordinate of this instance.
/// </summary>
public int X;
/// <summary>
/// The Y coordinate of this instance.
/// </summary>
public int Y;
/// <summary>
/// Returns the Point (0, 0).
/// </summary>
public static readonly Point Zero = new Point();
/// <summary>
/// Returns the Point (0, 0).
/// </summary>
public static readonly Point Empty = new Point();
#endregion #endregion
@ -76,6 +58,65 @@ namespace OpenTK
#region Public Members #region Public Members
/// <summary>
/// Gets a <see cref="System.Boolean"/> that indicates whether this instance is empty or zero.
/// </summary>
public bool IsEmpty { get { return X == 0 && Y == 0; } }
/// <summary>
/// Gets or sets the X coordinate of this instance.
/// </summary>
public int X { get { return x; } set { x = value; } }
/// <summary>
/// Gets or sets the Y coordinate of this instance.
/// </summary>
public int Y { get { return y; } set { y = value; } }
/// <summary>
/// Returns the Point (0, 0).
/// </summary>
public static readonly Point Zero = new Point();
/// <summary>
/// Returns the Point (0, 0).
/// </summary>
public static readonly Point Empty = new Point();
/// <summary>
/// Translates the specified Point by the specified Size.
/// </summary>
/// <param name="point">
/// The <see cref="Point"/> instance to translate.
/// </param>
/// <param name="size">
/// The <see cref="Size"/> instance to translate point with.
/// </param>
/// <returns>
/// A new <see cref="Point"/> instance translated by size.
/// </returns>
public static Point operator +(Point point, Size size)
{
return new Point(point.X + size.Width, point.Y + size.Height);
}
/// <summary>
/// Translates the specified Point by the negative of the specified Size.
/// </summary>
/// <param name="point">
/// The <see cref="Point"/> instance to translate.
/// </param>
/// <param name="size">
/// The <see cref="Size"/> instance to translate point with.
/// </param>
/// <returns>
/// A new <see cref="Point"/> instance translated by size.
/// </returns>
public static Point operator -(Point point, Size size)
{
return new Point(point.X - size.Width, point.Y - size.Height);
}
/// <summary> /// <summary>
/// Compares two instances for equality. /// Compares two instances for equality.
/// </summary> /// </summary>
@ -98,6 +139,48 @@ namespace OpenTK
return !left.Equals(right); return !left.Equals(right);
} }
/// <summary>
/// Converts an OpenTK.Point instance to a System.Drawing.Point.
/// </summary>
/// <param name="point">
/// The <see cref="Point"/> instance to convert.
/// </param>
/// <returns>
/// A <see cref="System.Drawing.Point"/> instance equivalent to point.
/// </returns>
public static implicit operator System.Drawing.Point(Point point)
{
return new System.Drawing.Point(point.X, point.Y);
}
/// <summary>
/// Converts a System.Drawing.Point instance to an OpenTK.Point.
/// </summary>
/// <param name="point">
/// The <see cref="System.Drawing.Point"/> instance to convert.
/// </param>
/// <returns>
/// A <see cref="Point"/> instance equivalent to point.
/// </returns>
public static implicit operator Point(System.Drawing.Point point)
{
return new Point(point.X, point.Y);
}
/// <summary>
/// Converts an OpenTK.Point instance to a System.Drawing.PointF.
/// </summary>
/// <param name="point">
/// The <see cref="Point"/> instance to convert.
/// </param>
/// <returns>
/// A <see cref="System.Drawing.PointF"/> instance equivalent to point.
/// </returns>
public static implicit operator System.Drawing.PointF(Point point)
{
return new System.Drawing.PointF(point.X, point.Y);
}
/// <summary> /// <summary>
/// Indicates whether this instance is equal to the specified object. /// Indicates whether this instance is equal to the specified object.
/// </summary> /// </summary>

View file

@ -1,4 +1,4 @@
#region License #region License
// //
// The Open Toolkit Library License // The Open Toolkit Library License
// //
@ -37,15 +37,9 @@ namespace OpenTK
{ {
#region Fields #region Fields
public int X; Point location;
public int Y; Size size;
public int Width;
public int Height;
public static readonly Rectangle Empty = new Rectangle();
#endregion #endregion
@ -66,16 +60,40 @@ namespace OpenTK
#region Public Members #region Public Members
public int X
{
get { return Location.X; }
set { Location = new Point (value, Y); }
}
public int Y
{
get { return Location.Y; }
set { Location = new Point (X, value); }
}
public int Width
{
get { return Size.Width; }
set { Size = new Size (value, Height); }
}
public int Height
{
get { return Size.Height; }
set { Size = new Size(Width, value); }
}
public Point Location public Point Location
{ {
get { return new Point(X, Y); } get { return location; }
set { X = value.X; Y = value.Y; } set { location = value; }
} }
public Size Size public Size Size
{ {
get { return new Size(Width, Height); } get { return size; }
set { Width = value.Width; Height = value.Height; } set { size = value; }
} }
public int Top { get { return Y; } } public int Top { get { return Y; } }
@ -85,9 +103,12 @@ namespace OpenTK
public bool IsEmpty public bool IsEmpty
{ {
get { return X == 0 && Y == 0 && Width == 0 && Height == 0; } get { return Location.IsEmpty && Size.IsEmpty; }
} }
public static readonly Rectangle Zero = new Rectangle();
public static readonly Rectangle Empty = new Rectangle();
public static Rectangle FromLTRB(int left, int top, int right, int bottom) public static Rectangle FromLTRB(int left, int top, int right, int bottom)
{ {
return new Rectangle(new Point(left, top), new Size(right - left, bottom - top)); return new Rectangle(new Point(left, top), new Size(right - left, bottom - top));
@ -99,6 +120,11 @@ namespace OpenTK
point.Y >= Top && point.Y < Bottom; point.Y >= Top && point.Y < Bottom;
} }
public bool Contains(Rectangle rect)
{
return Contains(rect.Location) && Contains(rect.Location + rect.Size);
}
public static bool operator ==(Rectangle left, Rectangle right) public static bool operator ==(Rectangle left, Rectangle right)
{ {
return left.Equals(right); return left.Equals(right);
@ -109,6 +135,21 @@ namespace OpenTK
return !left.Equals(right); return !left.Equals(right);
} }
public static implicit operator System.Drawing.Rectangle(Rectangle rect)
{
return new System.Drawing.Rectangle(rect.Location, rect.Size);
}
public static implicit operator Rectangle(System.Drawing.Rectangle rect)
{
return new Rectangle(rect.Location, rect.Size);
}
public static implicit operator System.Drawing.RectangleF(Rectangle rect)
{
return new System.Drawing.RectangleF(rect.Location, rect.Size);
}
#endregion #endregion
#region IEquatable<Rectangle> Members #region IEquatable<Rectangle> Members

View file

@ -1,4 +1,4 @@
#region License #region License
// //
// The Open Toolkit Library License // The Open Toolkit Library License
// //
@ -37,15 +37,7 @@ namespace OpenTK
{ {
#region Fields #region Fields
/// <summary> int width, height;
/// The width of this instance.
/// </summary>
public int Width;
/// <summary>
/// The height of this instance.
/// </summary>
public int Height;
#endregion #endregion
@ -66,6 +58,52 @@ namespace OpenTK
#region Public Members #region Public Members
/// <summary>
/// Gets or sets the width of this instance.
/// </summary>
public int Width
{
get { return width; }
set
{
if (width < 0)
throw new ArgumentOutOfRangeException();
width = value;
}
}
/// <summary>
/// Gets or sets the height of this instance.
/// </summary>
public int Height
{
get { return height; }
set
{
if (height < 0)
throw new ArgumentOutOfRangeException();
height = value;
}
}
/// <summary>
/// Gets a <see cref="System.Boolean"/> that indicates whether this instance is empty or zero.
/// </summary>
public bool IsEmpty
{
get { return Width == 0 && Height == 0; }
}
/// <summary>
/// Returns a Size instance equal to (0, 0).
/// </summary>
public static readonly Size Empty = new Size();
/// <summary>
/// Returns a Size instance equal to (0, 0).
/// </summary>
public static readonly Size Zero = new Size();
/// <summary> /// <summary>
/// Compares two instances for equality. /// Compares two instances for equality.
/// </summary> /// </summary>
@ -88,6 +126,48 @@ namespace OpenTK
return !left.Equals(right); return !left.Equals(right);
} }
/// <summary>
/// Converts an OpenTK.Size instance to a System.Drawing.Size.
/// </summary>
/// <param name="size">
/// The <see cref="Size"/> instance to convert.
/// </param>
/// <returns>
/// A <see cref="System.Drawing.Size"/> instance equivalent to size.
/// </returns>
public static implicit operator System.Drawing.Size(Size size)
{
return new System.Drawing.Size(size.Width, size.Height);
}
/// <summary>
/// Converts a System.Drawing.Size instance to an OpenTK.Size.
/// </summary>
/// <param name="size">
/// The <see cref="System.Drawing.Size"/> instance to convert.
/// </param>
/// <returns>
/// A <see cref="Size"/> instance equivalent to size.
/// </returns>
public static implicit operator Size(System.Drawing.Size size)
{
return new Size(size.Width, size.Height);
}
/// <summary>
/// Converts an OpenTK.Point instance to a System.Drawing.SizeF.
/// </summary>
/// <param name="size">
/// The <see cref="Size"/> instance to convert.
/// </param>
/// <returns>
/// A <see cref="System.Drawing.SizeF"/> instance equivalent to size.
/// </returns>
public static implicit operator System.Drawing.SizeF(Size size)
{
return new System.Drawing.SizeF(size.Width, size.Height);
}
/// <summary> /// <summary>
/// Indicates whether this instance is equal to the specified object. /// Indicates whether this instance is equal to the specified object.
/// </summary> /// </summary>
@ -135,5 +215,7 @@ namespace OpenTK
#endregion #endregion
} }
#endif #endif
} }