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