2017-10-02 21:24:00 +00:00
|
|
|
using System;
|
|
|
|
using System.Runtime.InteropServices;
|
2018-03-01 07:51:35 +00:00
|
|
|
#if REAL_T_IS_DOUBLE
|
|
|
|
using real_t = System.Double;
|
|
|
|
#else
|
|
|
|
using real_t = System.Single;
|
|
|
|
#endif
|
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
namespace Godot
|
|
|
|
{
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
|
|
public struct Rect2 : IEquatable<Rect2>
|
|
|
|
{
|
|
|
|
private Vector2 position;
|
|
|
|
private Vector2 size;
|
|
|
|
|
|
|
|
public Vector2 Position
|
|
|
|
{
|
|
|
|
get { return position; }
|
|
|
|
set { position = value; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2 Size
|
|
|
|
{
|
|
|
|
get { return size; }
|
|
|
|
set { size = value; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2 End
|
|
|
|
{
|
|
|
|
get { return position + size; }
|
|
|
|
}
|
|
|
|
|
2018-03-01 07:51:35 +00:00
|
|
|
public real_t Area
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2017-11-21 22:32:19 +00:00
|
|
|
get { return GetArea(); }
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public Rect2 Clip(Rect2 b)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2018-04-08 03:30:43 +00:00
|
|
|
var newRect = b;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
if (!Intersects(newRect))
|
2017-10-02 21:24:00 +00:00
|
|
|
return new Rect2();
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
newRect.position.x = Mathf.Max(b.position.x, position.x);
|
|
|
|
newRect.position.y = Mathf.Max(b.position.y, position.y);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
Vector2 bEnd = b.position + b.size;
|
|
|
|
Vector2 end = position + size;
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
newRect.size.x = Mathf.Min(bEnd.x, end.x) - newRect.position.x;
|
|
|
|
newRect.size.y = Mathf.Min(bEnd.y, end.y) - newRect.position.y;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
return newRect;
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public bool Encloses(Rect2 b)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
|
|
|
return (b.position.x >= position.x) && (b.position.y >= position.y) &&
|
|
|
|
((b.position.x + b.size.x) < (position.x + size.x)) &&
|
|
|
|
((b.position.y + b.size.y) < (position.y + size.y));
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public Rect2 Expand(Vector2 to)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2018-04-08 03:30:43 +00:00
|
|
|
var expanded = this;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
Vector2 begin = expanded.position;
|
|
|
|
Vector2 end = expanded.position + expanded.size;
|
|
|
|
|
|
|
|
if (to.x < begin.x)
|
|
|
|
begin.x = to.x;
|
|
|
|
if (to.y < begin.y)
|
|
|
|
begin.y = to.y;
|
|
|
|
|
|
|
|
if (to.x > end.x)
|
|
|
|
end.x = to.x;
|
|
|
|
if (to.y > end.y)
|
|
|
|
end.y = to.y;
|
|
|
|
|
|
|
|
expanded.position = begin;
|
|
|
|
expanded.size = end - begin;
|
|
|
|
|
|
|
|
return expanded;
|
|
|
|
}
|
|
|
|
|
2018-03-01 07:51:35 +00:00
|
|
|
public real_t GetArea()
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
|
|
|
return size.x * size.y;
|
|
|
|
}
|
|
|
|
|
2018-03-01 07:51:35 +00:00
|
|
|
public Rect2 Grow(real_t by)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2018-04-08 03:30:43 +00:00
|
|
|
var g = this;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
g.position.x -= by;
|
|
|
|
g.position.y -= by;
|
|
|
|
g.size.x += by * 2;
|
|
|
|
g.size.y += by * 2;
|
|
|
|
|
|
|
|
return g;
|
|
|
|
}
|
|
|
|
|
2018-03-01 07:51:35 +00:00
|
|
|
public Rect2 GrowIndividual(real_t left, real_t top, real_t right, real_t bottom)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2018-04-08 03:30:43 +00:00
|
|
|
var g = this;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
g.position.x -= left;
|
|
|
|
g.position.y -= top;
|
|
|
|
g.size.x += left + right;
|
|
|
|
g.size.y += top + bottom;
|
|
|
|
|
|
|
|
return g;
|
|
|
|
}
|
|
|
|
|
2018-03-01 07:51:35 +00:00
|
|
|
public Rect2 GrowMargin(Margin margin, real_t by)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2018-04-08 03:30:43 +00:00
|
|
|
var g = this;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2017-12-24 02:17:48 +00:00
|
|
|
g.GrowIndividual((Margin.Left == margin) ? by : 0,
|
|
|
|
(Margin.Top == margin) ? by : 0,
|
|
|
|
(Margin.Right == margin) ? by : 0,
|
|
|
|
(Margin.Bottom == margin) ? by : 0);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
return g;
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public bool HasNoArea()
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
|
|
|
return size.x <= 0 || size.y <= 0;
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public bool HasPoint(Vector2 point)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
|
|
|
if (point.x < position.x)
|
|
|
|
return false;
|
|
|
|
if (point.y < position.y)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (point.x >= (position.x + size.x))
|
|
|
|
return false;
|
|
|
|
if (point.y >= (position.y + size.y))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public bool Intersects(Rect2 b)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
|
|
|
if (position.x > (b.position.x + b.size.x))
|
|
|
|
return false;
|
|
|
|
if ((position.x + size.x) < b.position.x)
|
|
|
|
return false;
|
|
|
|
if (position.y > (b.position.y + b.size.y))
|
|
|
|
return false;
|
|
|
|
if ((position.y + size.y) < b.position.y)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public Rect2 Merge(Rect2 b)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
|
|
|
Rect2 newRect;
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
newRect.position.x = Mathf.Min(b.position.x, position.x);
|
|
|
|
newRect.position.y = Mathf.Min(b.position.y, position.y);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
newRect.size.x = Mathf.Max(b.position.x + b.size.x, position.x + size.x);
|
|
|
|
newRect.size.y = Mathf.Max(b.position.y + b.size.y, position.y + size.y);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
newRect.size = newRect.size - newRect.position; // Make relative again
|
|
|
|
|
|
|
|
return newRect;
|
|
|
|
}
|
2018-03-01 07:51:35 +00:00
|
|
|
|
|
|
|
// Constructors
|
2017-10-02 21:24:00 +00:00
|
|
|
public Rect2(Vector2 position, Vector2 size)
|
|
|
|
{
|
|
|
|
this.position = position;
|
|
|
|
this.size = size;
|
|
|
|
}
|
2018-03-01 07:51:35 +00:00
|
|
|
public Rect2(Vector2 position, real_t width, real_t height)
|
|
|
|
{
|
|
|
|
this.position = position;
|
2018-04-08 03:28:24 +00:00
|
|
|
size = new Vector2(width, height);
|
2018-03-01 07:51:35 +00:00
|
|
|
}
|
|
|
|
public Rect2(real_t x, real_t y, Vector2 size)
|
|
|
|
{
|
2018-04-08 03:28:24 +00:00
|
|
|
position = new Vector2(x, y);
|
2018-03-01 07:51:35 +00:00
|
|
|
this.size = size;
|
|
|
|
}
|
|
|
|
public Rect2(real_t x, real_t y, real_t width, real_t height)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2018-04-08 03:28:24 +00:00
|
|
|
position = new Vector2(x, y);
|
|
|
|
size = new Vector2(width, height);
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator ==(Rect2 left, Rect2 right)
|
|
|
|
{
|
|
|
|
return left.Equals(right);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator !=(Rect2 left, Rect2 right)
|
|
|
|
{
|
|
|
|
return !left.Equals(right);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
{
|
|
|
|
if (obj is Rect2)
|
|
|
|
{
|
|
|
|
return Equals((Rect2)obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Equals(Rect2 other)
|
|
|
|
{
|
|
|
|
return position.Equals(other.position) && size.Equals(other.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
{
|
|
|
|
return position.GetHashCode() ^ size.GetHashCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
return String.Format("({0}, {1})", new object[]
|
|
|
|
{
|
2018-04-08 03:28:24 +00:00
|
|
|
position.ToString(),
|
|
|
|
size.ToString()
|
2017-10-02 21:24:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public string ToString(string format)
|
|
|
|
{
|
|
|
|
return String.Format("({0}, {1})", new object[]
|
|
|
|
{
|
2018-04-08 03:28:24 +00:00
|
|
|
position.ToString(format),
|
|
|
|
size.ToString(format)
|
2017-10-02 21:24:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-12-24 02:17:48 +00:00
|
|
|
}
|