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 8624ec0d1a
commit 2f522e790b
3 changed files with 292 additions and 86 deletions

View file

@ -1,4 +1,4 @@
#region License
#region License
//
// The Open Toolkit Library License
//
@ -25,7 +25,7 @@
//
#endregion
using System;
using System;
using System.Collections.Generic;
using System.Text;
@ -37,25 +37,7 @@ namespace OpenTK
{
#region Fields
/// <summary>
/// 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();
int x, y;
#endregion
@ -76,6 +58,65 @@ namespace OpenTK
#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>
/// Compares two instances for equality.
/// </summary>
@ -98,6 +139,48 @@ namespace OpenTK
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>
/// Indicates whether this instance is equal to the specified object.
/// </summary>

View file

@ -1,4 +1,4 @@
#region License
#region License
//
// The Open Toolkit Library License
//
@ -25,7 +25,7 @@
//
#endregion
using System;
using System;
using System.Collections.Generic;
using System.Text;
@ -37,15 +37,9 @@ namespace OpenTK
{
#region Fields
public int X;
Point location;
public int Y;
public int Width;
public int Height;
public static readonly Rectangle Empty = new Rectangle();
Size size;
#endregion
@ -66,16 +60,40 @@ namespace OpenTK
#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
{
get { return new Point(X, Y); }
set { X = value.X; Y = value.Y; }
get { return location; }
set { location = value; }
}
public Size Size
{
get { return new Size(Width, Height); }
set { Width = value.Width; Height = value.Height; }
get { return size; }
set { size = value; }
}
public int Top { get { return Y; } }
@ -83,20 +101,28 @@ namespace OpenTK
public int Bottom { get { return Y + Height; } }
public int Left { get { return X; } }
public bool IsEmpty
{
get { return X == 0 && Y == 0 && Width == 0 && Height == 0; }
public bool IsEmpty
{
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)
{
return new Rectangle(new Point(left, top), new Size(right - left, bottom - top));
}
public bool Contains(Point point)
{
return point.X >= Left && point.X < Right &&
point.Y >= Top && point.Y < Bottom;
public bool Contains(Point point)
{
return point.X >= Left && point.X < Right &&
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)
@ -109,6 +135,21 @@ namespace OpenTK
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
#region IEquatable<Rectangle> Members

View file

@ -1,51 +1,43 @@
#region License
//
// The Open Toolkit Library License
//
// Copyright (c) 2006 - 2009 the Open Toolkit library.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#endregion
using System;
#region License
//
// The Open Toolkit Library License
//
// Copyright (c) 2006 - 2009 the Open Toolkit library.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#endregion
using System;
using System.Collections.Generic;
using System.Text;
namespace OpenTK
{
#if EXPERIMENTAL
#if EXPERIMENTAL
public struct Size : IEquatable<Size>
{
#region Fields
/// <summary>
/// The width of this instance.
/// </summary>
public int Width;
/// <summary>
/// The height of this instance.
/// </summary>
public int Height;
int width, height;
#endregion
@ -66,6 +58,52 @@ namespace OpenTK
#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>
/// Compares two instances for equality.
/// </summary>
@ -83,11 +121,53 @@ namespace OpenTK
/// <param name="left">The first instance.</param>
/// <param name="right">The second instance.</param>
/// <returns>True, if left is not equal to right; false otherwise.</returns>
public static bool operator !=(Size left, Size right)
{
return !left.Equals(right);
public static bool operator !=(Size left, Size 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>
/// Indicates whether this instance is equal to the specified object.
/// </summary>
@ -97,7 +177,7 @@ namespace OpenTK
{
if (obj is Size)
return Equals((Size)obj);
return false;
}
@ -132,8 +212,10 @@ namespace OpenTK
{
return Width == other.Width && Height == other.Height;
}
#endregion
}
#endif
#endif
}