Merge pull request #21174 from KellyThomas/c-sharp-feature-parity-rect2

[Mono] Rect2 - add Abs(), rename private fields
This commit is contained in:
Ignacio Etcheverry 2018-08-22 22:34:45 +02:00 committed by GitHub
commit 980b81dd2a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -11,24 +11,24 @@ namespace Godot
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public struct Rect2 : IEquatable<Rect2> public struct Rect2 : IEquatable<Rect2>
{ {
private Vector2 position; private Vector2 _position;
private Vector2 size; private Vector2 _size;
public Vector2 Position public Vector2 Position
{ {
get { return position; } get { return _position; }
set { position = value; } set { _position = value; }
} }
public Vector2 Size public Vector2 Size
{ {
get { return size; } get { return _size; }
set { size = value; } set { _size = value; }
} }
public Vector2 End public Vector2 End
{ {
get { return position + size; } get { return _position + _size; }
} }
public real_t Area public real_t Area
@ -36,6 +36,13 @@ namespace Godot
get { return GetArea(); } get { return GetArea(); }
} }
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());
}
public Rect2 Clip(Rect2 b) public Rect2 Clip(Rect2 b)
{ {
var newRect = b; var newRect = b;
@ -43,31 +50,31 @@ namespace Godot
if (!Intersects(newRect)) if (!Intersects(newRect))
return new Rect2(); return new Rect2();
newRect.position.x = Mathf.Max(b.position.x, position.x); newRect._position.x = Mathf.Max(b._position.x, _position.x);
newRect.position.y = Mathf.Max(b.position.y, position.y); newRect._position.y = Mathf.Max(b._position.y, _position.y);
Vector2 bEnd = b.position + b.size; Vector2 bEnd = b._position + b._size;
Vector2 end = position + size; Vector2 end = _position + _size;
newRect.size.x = Mathf.Min(bEnd.x, end.x) - newRect.position.x; newRect._size.x = Mathf.Min(bEnd.x, end.x) - newRect._position.x;
newRect.size.y = Mathf.Min(bEnd.y, end.y) - newRect.position.y; newRect._size.y = Mathf.Min(bEnd.y, end.y) - newRect._position.y;
return newRect; return newRect;
} }
public bool Encloses(Rect2 b) public bool Encloses(Rect2 b)
{ {
return b.position.x >= position.x && b.position.y >= position.y && return b._position.x >= _position.x && b._position.y >= _position.y &&
b.position.x + b.size.x < position.x + size.x && b._position.x + b._size.x < _position.x + _size.x &&
b.position.y + b.size.y < position.y + size.y; b._position.y + b._size.y < _position.y + _size.y;
} }
public Rect2 Expand(Vector2 to) public Rect2 Expand(Vector2 to)
{ {
var expanded = this; var expanded = this;
Vector2 begin = expanded.position; Vector2 begin = expanded._position;
Vector2 end = expanded.position + expanded.size; Vector2 end = expanded._position + expanded._size;
if (to.x < begin.x) if (to.x < begin.x)
begin.x = to.x; begin.x = to.x;
@ -79,25 +86,25 @@ namespace Godot
if (to.y > end.y) if (to.y > end.y)
end.y = to.y; end.y = to.y;
expanded.position = begin; expanded._position = begin;
expanded.size = end - begin; expanded._size = end - begin;
return expanded; return expanded;
} }
public real_t GetArea() public real_t GetArea()
{ {
return size.x * size.y; return _size.x * _size.y;
} }
public Rect2 Grow(real_t by) public Rect2 Grow(real_t by)
{ {
var g = this; var g = this;
g.position.x -= by; g._position.x -= by;
g.position.y -= by; g._position.y -= by;
g.size.x += by * 2; g._size.x += by * 2;
g.size.y += by * 2; g._size.y += by * 2;
return g; return g;
} }
@ -106,10 +113,10 @@ namespace Godot
{ {
var g = this; var g = this;
g.position.x -= left; g._position.x -= left;
g.position.y -= top; g._position.y -= top;
g.size.x += left + right; g._size.x += left + right;
g.size.y += top + bottom; g._size.y += top + bottom;
return g; return g;
} }
@ -128,19 +135,19 @@ namespace Godot
public bool HasNoArea() public bool HasNoArea()
{ {
return size.x <= 0 || size.y <= 0; return _size.x <= 0 || _size.y <= 0;
} }
public bool HasPoint(Vector2 point) public bool HasPoint(Vector2 point)
{ {
if (point.x < position.x) if (point.x < _position.x)
return false; return false;
if (point.y < position.y) if (point.y < _position.y)
return false; return false;
if (point.x >= position.x + size.x) if (point.x >= _position.x + _size.x)
return false; return false;
if (point.y >= position.y + size.y) if (point.y >= _position.y + _size.y)
return false; return false;
return true; return true;
@ -148,13 +155,13 @@ namespace Godot
public bool Intersects(Rect2 b) public bool Intersects(Rect2 b)
{ {
if (position.x > b.position.x + b.size.x) if (_position.x > b._position.x + b._size.x)
return false; return false;
if (position.x + size.x < b.position.x) if (_position.x + _size.x < b._position.x)
return false; return false;
if (position.y > b.position.y + b.size.y) if (_position.y > b._position.y + b._size.y)
return false; return false;
if (position.y + size.y < b.position.y) if (_position.y + _size.y < b._position.y)
return false; return false;
return true; return true;
@ -164,13 +171,13 @@ namespace Godot
{ {
Rect2 newRect; Rect2 newRect;
newRect.position.x = Mathf.Min(b.position.x, position.x); newRect._position.x = Mathf.Min(b._position.x, _position.x);
newRect.position.y = Mathf.Min(b.position.y, position.y); newRect._position.y = Mathf.Min(b._position.y, _position.y);
newRect.size.x = Mathf.Max(b.position.x + b.size.x, position.x + size.x); 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); newRect._size.y = Mathf.Max(b._position.y + b._size.y, _position.y + _size.y);
newRect.size = newRect.size - newRect.position; // Make relative again newRect._size = newRect._size - newRect._position; // Make relative again
return newRect; return newRect;
} }
@ -178,23 +185,23 @@ namespace Godot
// Constructors // Constructors
public Rect2(Vector2 position, Vector2 size) public Rect2(Vector2 position, Vector2 size)
{ {
this.position = position; _position = position;
this.size = size; _size = size;
} }
public Rect2(Vector2 position, real_t width, real_t height) public Rect2(Vector2 position, real_t width, real_t height)
{ {
this.position = position; _position = position;
size = new Vector2(width, height); _size = new Vector2(width, height);
} }
public Rect2(real_t x, real_t y, Vector2 size) public Rect2(real_t x, real_t y, Vector2 size)
{ {
position = new Vector2(x, y); _position = new Vector2(x, y);
this.size = size; _size = size;
} }
public Rect2(real_t x, real_t y, real_t width, real_t height) public Rect2(real_t x, real_t y, real_t width, real_t height)
{ {
position = new Vector2(x, y); _position = new Vector2(x, y);
size = new Vector2(width, height); _size = new Vector2(width, height);
} }
public static bool operator ==(Rect2 left, Rect2 right) public static bool operator ==(Rect2 left, Rect2 right)
@ -219,20 +226,20 @@ namespace Godot
public bool Equals(Rect2 other) public bool Equals(Rect2 other)
{ {
return position.Equals(other.position) && size.Equals(other.size); return _position.Equals(other._position) && _size.Equals(other._size);
} }
public override int GetHashCode() public override int GetHashCode()
{ {
return position.GetHashCode() ^ size.GetHashCode(); return _position.GetHashCode() ^ _size.GetHashCode();
} }
public override string ToString() public override string ToString()
{ {
return String.Format("({0}, {1})", new object[] return String.Format("({0}, {1})", new object[]
{ {
position.ToString(), _position.ToString(),
size.ToString() _size.ToString()
}); });
} }
@ -240,8 +247,8 @@ namespace Godot
{ {
return String.Format("({0}, {1})", new object[] return String.Format("({0}, {1})", new object[]
{ {
position.ToString(format), _position.ToString(format),
size.ToString(format) _size.ToString(format)
}); });
} }
} }