2011-10-31 05:36:24 +00:00
|
|
|
using System;
|
2012-08-09 09:45:04 +00:00
|
|
|
using System.Globalization;
|
2012-10-13 19:43:12 +00:00
|
|
|
using ANX.Framework.NonXNA.Development;
|
2015-04-08 14:50:03 +02:00
|
|
|
using System.ComponentModel;
|
|
|
|
using ANX.Framework.Design;
|
2011-10-31 05:36:24 +00:00
|
|
|
|
2012-08-09 09:45:04 +00:00
|
|
|
// This file is part of the ANX.Framework created by the
|
|
|
|
// "ANX.Framework developer group" and released under the Ms-PL license.
|
|
|
|
// For details see: http://anxframework.codeplex.com/license
|
2011-10-31 05:36:24 +00:00
|
|
|
|
|
|
|
namespace ANX.Framework
|
|
|
|
{
|
2012-10-13 19:43:12 +00:00
|
|
|
[PercentageComplete(100)]
|
2012-10-13 21:36:46 +00:00
|
|
|
[Developer("Glatzemann, GinieDp")]
|
2012-10-14 09:02:26 +00:00
|
|
|
[TestState(TestStateAttribute.TestState.Tested)]
|
2015-04-08 14:50:03 +02:00
|
|
|
#if !WINDOWSMETRO
|
|
|
|
[Serializable]
|
|
|
|
[TypeConverter(typeof(RectangleConverter))]
|
|
|
|
#endif
|
2011-10-31 05:36:24 +00:00
|
|
|
public struct Rectangle : IEquatable<Rectangle>
|
|
|
|
{
|
|
|
|
#region fields
|
|
|
|
public int X;
|
2012-08-21 18:13:30 +00:00
|
|
|
public int Y;
|
|
|
|
public int Width;
|
|
|
|
public int Height;
|
2011-10-31 05:36:24 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region properties
|
|
|
|
public Point Center
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2012-08-09 09:45:04 +00:00
|
|
|
return new Point((int)(X + Width * 0.5f), (int)(Y + Height * 0.5f));
|
2011-10-31 05:36:24 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-09 09:45:04 +00:00
|
|
|
|
2011-10-31 05:36:24 +00:00
|
|
|
public static Rectangle Empty
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return new Rectangle();
|
|
|
|
}
|
|
|
|
}
|
2012-08-09 09:45:04 +00:00
|
|
|
|
2011-10-31 05:36:24 +00:00
|
|
|
public bool IsEmpty
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2012-08-09 09:45:04 +00:00
|
|
|
return (this.X == 0) &&
|
2015-04-08 14:50:03 +02:00
|
|
|
(this.Y == 0) &&
|
|
|
|
(this.Width == 0) &&
|
|
|
|
(this.Height == 0);
|
2011-10-31 05:36:24 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-09 09:45:04 +00:00
|
|
|
|
2011-10-31 05:36:24 +00:00
|
|
|
public Point Location
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2012-08-09 09:45:04 +00:00
|
|
|
return new Point(this.X, this.Y);
|
2011-10-31 05:36:24 +00:00
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
this.X = value.X;
|
|
|
|
this.Y = value.Y;
|
|
|
|
}
|
|
|
|
}
|
2012-08-09 09:45:04 +00:00
|
|
|
|
2012-08-21 18:13:30 +00:00
|
|
|
public int Left
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return this.X;
|
|
|
|
}
|
|
|
|
}
|
2012-08-09 09:45:04 +00:00
|
|
|
|
2011-10-31 05:36:24 +00:00
|
|
|
public int Right
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return this.X + Width;
|
|
|
|
}
|
|
|
|
}
|
2012-08-09 09:45:04 +00:00
|
|
|
|
2011-10-31 05:36:24 +00:00
|
|
|
public int Top
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return this.Y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-21 18:13:30 +00:00
|
|
|
public int Bottom
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return this.Y + Height;
|
|
|
|
}
|
|
|
|
}
|
2012-08-09 09:45:04 +00:00
|
|
|
#endregion
|
2011-10-31 05:36:24 +00:00
|
|
|
|
|
|
|
#region constructors
|
|
|
|
public Rectangle(int x, int y, int width, int height)
|
|
|
|
{
|
|
|
|
this.Height = height;
|
|
|
|
this.Width = width;
|
|
|
|
this.X = x;
|
|
|
|
this.Y = y;
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region public methods
|
|
|
|
public bool Contains(int x, int y)
|
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
this.Contains(ref x, ref y, out result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Contains(Point value)
|
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
this.Contains(ref value.X, ref value.Y, out result);
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
public void Contains(ref Point value, out bool result)
|
|
|
|
{
|
|
|
|
this.Contains(ref value.X, ref value.Y, out result);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Contains(ref int x, ref int y, out bool result)
|
|
|
|
{
|
|
|
|
result = x > this.X &&
|
2012-08-21 18:13:30 +00:00
|
|
|
x < this.Right &&
|
|
|
|
y > this.Y &&
|
|
|
|
y < this.Bottom;
|
2011-10-31 05:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool Contains(Rectangle value)
|
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
this.Contains(ref value, out result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Contains(ref Rectangle value, out bool result)
|
|
|
|
{
|
|
|
|
result = value.X >= this.X &&
|
2012-10-14 09:02:26 +00:00
|
|
|
value.X + value.Width <= this.Right &&
|
|
|
|
value.Y >= this.Y &&
|
|
|
|
value.Y + this.Height <= this.Bottom;
|
2011-10-31 05:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
{
|
|
|
|
return this.X + this.Y + this.Width + this.Height;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Inflate(int horizontalAmount, int verticalAmount)
|
|
|
|
{
|
|
|
|
this.X -= horizontalAmount;
|
|
|
|
this.Y -= verticalAmount;
|
|
|
|
this.Width += horizontalAmount * 2;
|
|
|
|
this.Height += verticalAmount * 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Intersects(Rectangle value)
|
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
this.Intersects(ref value, out result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Intersects(ref Rectangle value, out bool result)
|
|
|
|
{
|
|
|
|
//intersects if it dont contains it and is not outer
|
|
|
|
|
|
|
|
//outer
|
2012-08-09 09:45:04 +00:00
|
|
|
if (value.X > this.Right ||
|
2012-08-21 18:13:30 +00:00
|
|
|
value.Y > this.Bottom ||
|
|
|
|
value.X + value.Width < this.X ||
|
|
|
|
value.Y + value.Height < this.Y)
|
2011-10-31 05:36:24 +00:00
|
|
|
{
|
|
|
|
result = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//contains
|
|
|
|
if (this.Contains(value))
|
|
|
|
{
|
|
|
|
result = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Offset(int offsetX, int offsetY)
|
|
|
|
{
|
|
|
|
this.X += offsetX;
|
|
|
|
this.Y += offsetY;
|
|
|
|
}
|
2012-10-14 09:02:26 +00:00
|
|
|
|
2011-10-31 05:36:24 +00:00
|
|
|
public void Offset(Point amount)
|
|
|
|
{
|
|
|
|
this.X += amount.X;
|
|
|
|
this.Y += amount.Y;
|
|
|
|
}
|
2011-11-16 22:35:53 +00:00
|
|
|
|
2011-10-31 05:36:24 +00:00
|
|
|
public override string ToString()
|
2012-08-21 18:13:30 +00:00
|
|
|
{
|
|
|
|
var culture = CultureInfo.CurrentCulture;
|
2012-10-14 09:02:26 +00:00
|
|
|
// This may look a bit more ugly, but String.Format should be avoided cause of it's bad performance!
|
2012-08-21 18:13:30 +00:00
|
|
|
return "{X:" + X.ToString(culture) +
|
|
|
|
" Y:" + Y.ToString(culture) +
|
|
|
|
" Width:" + Width.ToString(culture) +
|
|
|
|
" Height:" + Height.ToString(culture) + "}";
|
2011-10-31 05:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region static methods
|
|
|
|
public static Rectangle Intersect(Rectangle value1, Rectangle value2)
|
|
|
|
{
|
|
|
|
Rectangle result;
|
|
|
|
Rectangle.Intersect(ref value1, ref value2, out result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Intersect(ref Rectangle value1, ref Rectangle value2, out Rectangle result)
|
|
|
|
{
|
|
|
|
result = new Rectangle();
|
|
|
|
int x, y, w, h;
|
|
|
|
if (value1.X > value2.X)
|
|
|
|
{
|
|
|
|
if (value1.X < value2.X + value2.Width)
|
|
|
|
{
|
|
|
|
x = value1.X;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (value2.X < value1.X + value1.Width)
|
|
|
|
{
|
|
|
|
x = value2.X;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value1.Y > value2.Y)
|
|
|
|
{
|
|
|
|
if (value1.Y < value2.Y + value2.Height)
|
|
|
|
{
|
|
|
|
y = value1.Y;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (value2.Y < value1.Y + value1.Height)
|
|
|
|
{
|
|
|
|
y = value2.Y;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value1.X + value1.Width < value2.X + value2.Width)
|
|
|
|
{
|
|
|
|
if (value1.X + value1.Width > value2.X)
|
|
|
|
{
|
|
|
|
w = value1.Width;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (value2.X + value2.Width > value1.X)
|
|
|
|
{
|
|
|
|
w = value2.Width;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value1.Y + value1.Height < value2.Y + value2.Height)
|
|
|
|
{
|
|
|
|
if (value1.Y + value1.Height > value2.Y)
|
|
|
|
{
|
|
|
|
h = value1.Height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (value2.Y + value2.Height > value1.Y)
|
|
|
|
{
|
|
|
|
h = value2.Height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-14 09:02:26 +00:00
|
|
|
result = new Rectangle(x, y, w - x, h - y);
|
2011-10-31 05:36:24 +00:00
|
|
|
}
|
2012-10-14 09:02:26 +00:00
|
|
|
|
2011-10-31 05:36:24 +00:00
|
|
|
public static Rectangle Union(Rectangle value1, Rectangle value2)
|
|
|
|
{
|
|
|
|
Rectangle result;
|
|
|
|
Rectangle.Union(ref value1, ref value2, out result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Union(ref Rectangle value1, ref Rectangle value2, out Rectangle result)
|
|
|
|
{
|
|
|
|
//Pick smallest x and y
|
|
|
|
int x = value1.X < value2.X ? value1.X : value2.X;
|
|
|
|
int y = value1.Y < value2.Y ? value1.Y : value2.Y;
|
|
|
|
|
|
|
|
//pick greatest height and width
|
|
|
|
int w = value1.X + value1.Width > value2.X + value2.Width ? value1.X + value1.Width : value2.X + value2.Width;
|
|
|
|
int h = value1.Y + value1.Height > value2.Y + value2.Height ? value1.Y + value1.Height : value2.Y + value2.Height;
|
|
|
|
|
|
|
|
result = new Rectangle(x, y, w-x, h-y);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region IEquatable implementation
|
|
|
|
public override bool Equals(Object obj)
|
|
|
|
{
|
2012-10-14 09:02:26 +00:00
|
|
|
return obj is Rectangle && Equals((Rectangle)obj);
|
2011-10-31 05:36:24 +00:00
|
|
|
}
|
2012-10-14 09:02:26 +00:00
|
|
|
|
2011-10-31 05:36:24 +00:00
|
|
|
public bool Equals(Rectangle other)
|
|
|
|
{
|
2012-08-09 09:45:04 +00:00
|
|
|
return this.Height == other.Height &&
|
2012-10-14 09:02:26 +00:00
|
|
|
this.Width == other.Width &&
|
|
|
|
this.X == other.X &&
|
|
|
|
this.Y == other.Y;
|
2011-10-31 05:36:24 +00:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region operator overloading
|
|
|
|
public static bool operator ==(Rectangle a, Rectangle b)
|
2012-08-21 18:13:30 +00:00
|
|
|
{
|
|
|
|
// NOTE: Duplicated code is better than copying 4 floats around!
|
|
|
|
return a.Height == b.Height &&
|
|
|
|
a.Width == b.Width &&
|
|
|
|
a.X == b.X &&
|
|
|
|
a.Y == b.Y;
|
2011-10-31 05:36:24 +00:00
|
|
|
}
|
2012-10-14 09:02:26 +00:00
|
|
|
|
2011-10-31 05:36:24 +00:00
|
|
|
public static bool operator !=(Rectangle a, Rectangle b)
|
2012-08-21 18:13:30 +00:00
|
|
|
{
|
|
|
|
// NOTE: Duplicated code is better than copying 4 floats around!
|
|
|
|
return a.Height != b.Height ||
|
|
|
|
a.Width != b.Width ||
|
|
|
|
a.X != b.X ||
|
|
|
|
a.Y != b.Y;
|
2011-10-31 05:36:24 +00:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|