From 600f657b706697cd4a3858de04df3eae7084e4fd Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Mon, 2 Nov 2009 07:20:59 +0000 Subject: [PATCH] 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. --- Source/OpenTK/Math/Point.cs | 125 +++++++++++++++++++---- Source/OpenTK/Math/Rectangle.cs | 83 ++++++++++++---- Source/OpenTK/Math/Size.cs | 170 +++++++++++++++++++++++--------- 3 files changed, 292 insertions(+), 86 deletions(-) diff --git a/Source/OpenTK/Math/Point.cs b/Source/OpenTK/Math/Point.cs index 58d7ffe4..205f3758 100644 --- a/Source/OpenTK/Math/Point.cs +++ b/Source/OpenTK/Math/Point.cs @@ -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 - /// - /// The X coordinate of this instance. - /// - public int X; - - /// - /// The Y coordinate of this instance. - /// - public int Y; - - /// - /// Returns the Point (0, 0). - /// - public static readonly Point Zero = new Point(); - - /// - /// Returns the Point (0, 0). - /// - public static readonly Point Empty = new Point(); + int x, y; #endregion @@ -76,6 +58,65 @@ namespace OpenTK #region Public Members + /// + /// Gets a that indicates whether this instance is empty or zero. + /// + public bool IsEmpty { get { return X == 0 && Y == 0; } } + + /// + /// Gets or sets the X coordinate of this instance. + /// + public int X { get { return x; } set { x = value; } } + + /// + /// Gets or sets the Y coordinate of this instance. + /// + public int Y { get { return y; } set { y = value; } } + + /// + /// Returns the Point (0, 0). + /// + public static readonly Point Zero = new Point(); + + /// + /// Returns the Point (0, 0). + /// + public static readonly Point Empty = new Point(); + + /// + /// Translates the specified Point by the specified Size. + /// + /// + /// The instance to translate. + /// + /// + /// The instance to translate point with. + /// + /// + /// A new instance translated by size. + /// + public static Point operator +(Point point, Size size) + { + return new Point(point.X + size.Width, point.Y + size.Height); + } + + /// + /// Translates the specified Point by the negative of the specified Size. + /// + /// + /// The instance to translate. + /// + /// + /// The instance to translate point with. + /// + /// + /// A new instance translated by size. + /// + public static Point operator -(Point point, Size size) + { + return new Point(point.X - size.Width, point.Y - size.Height); + } + /// /// Compares two instances for equality. /// @@ -98,6 +139,48 @@ namespace OpenTK return !left.Equals(right); } + /// + /// Converts an OpenTK.Point instance to a System.Drawing.Point. + /// + /// + /// The instance to convert. + /// + /// + /// A instance equivalent to point. + /// + public static implicit operator System.Drawing.Point(Point point) + { + return new System.Drawing.Point(point.X, point.Y); + } + + /// + /// Converts a System.Drawing.Point instance to an OpenTK.Point. + /// + /// + /// The instance to convert. + /// + /// + /// A instance equivalent to point. + /// + public static implicit operator Point(System.Drawing.Point point) + { + return new Point(point.X, point.Y); + } + + /// + /// Converts an OpenTK.Point instance to a System.Drawing.PointF. + /// + /// + /// The instance to convert. + /// + /// + /// A instance equivalent to point. + /// + public static implicit operator System.Drawing.PointF(Point point) + { + return new System.Drawing.PointF(point.X, point.Y); + } + /// /// Indicates whether this instance is equal to the specified object. /// diff --git a/Source/OpenTK/Math/Rectangle.cs b/Source/OpenTK/Math/Rectangle.cs index 45277a2f..0182867a 100644 --- a/Source/OpenTK/Math/Rectangle.cs +++ b/Source/OpenTK/Math/Rectangle.cs @@ -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 Members diff --git a/Source/OpenTK/Math/Size.cs b/Source/OpenTK/Math/Size.cs index 1a0acf91..a37c502e 100644 --- a/Source/OpenTK/Math/Size.cs +++ b/Source/OpenTK/Math/Size.cs @@ -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 { #region Fields - /// - /// The width of this instance. - /// - public int Width; - - /// - /// The height of this instance. - /// - public int Height; + int width, height; #endregion @@ -66,6 +58,52 @@ namespace OpenTK #region Public Members + /// + /// Gets or sets the width of this instance. + /// + public int Width + { + get { return width; } + set + { + if (width < 0) + throw new ArgumentOutOfRangeException(); + width = value; + } + } + + /// + /// Gets or sets the height of this instance. + /// + public int Height + { + get { return height; } + set + { + if (height < 0) + throw new ArgumentOutOfRangeException(); + height = value; + } + } + + /// + /// Gets a that indicates whether this instance is empty or zero. + /// + public bool IsEmpty + { + get { return Width == 0 && Height == 0; } + } + + /// + /// Returns a Size instance equal to (0, 0). + /// + public static readonly Size Empty = new Size(); + + /// + /// Returns a Size instance equal to (0, 0). + /// + public static readonly Size Zero = new Size(); + /// /// Compares two instances for equality. /// @@ -83,11 +121,53 @@ namespace OpenTK /// The first instance. /// The second instance. /// True, if left is not equal to right; false otherwise. - public static bool operator !=(Size left, Size right) - { - return !left.Equals(right); + public static bool operator !=(Size left, Size right) + { + return !left.Equals(right); } + /// + /// Converts an OpenTK.Size instance to a System.Drawing.Size. + /// + /// + /// The instance to convert. + /// + /// + /// A instance equivalent to size. + /// + public static implicit operator System.Drawing.Size(Size size) +{ + return new System.Drawing.Size(size.Width, size.Height); + } + + /// + /// Converts a System.Drawing.Size instance to an OpenTK.Size. + /// + /// + /// The instance to convert. + /// + /// + /// A instance equivalent to size. + /// + public static implicit operator Size(System.Drawing.Size size) + { + return new Size(size.Width, size.Height); + } + + /// + /// Converts an OpenTK.Point instance to a System.Drawing.SizeF. + /// + /// + /// The instance to convert. + /// + /// + /// A instance equivalent to size. + /// + public static implicit operator System.Drawing.SizeF(Size size) + { + return new System.Drawing.SizeF(size.Width, size.Height); + } + /// /// Indicates whether this instance is equal to the specified object. /// @@ -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 + }