2
0

Rect2I.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. namespace BansheeEngine
  7. {
  8. [StructLayout(LayoutKind.Sequential), SerializeObject]
  9. public struct Rect2I
  10. {
  11. public Rect2I(int x, int y, int width, int height)
  12. {
  13. this.x = x;
  14. this.y = y;
  15. this.width = width;
  16. this.height = height;
  17. }
  18. public static bool operator== (Rect2I lhs, Rect2I rhs)
  19. {
  20. return lhs.x == rhs.x && lhs.y == rhs.y && lhs.width == rhs.width && lhs.height == rhs.height;
  21. }
  22. public static bool operator!= (Rect2I lhs, Rect2I rhs)
  23. {
  24. return !(lhs == rhs);
  25. }
  26. public bool Contains(Vector2I point)
  27. {
  28. if(point.x >= x && point.x < (x + width))
  29. {
  30. if(point.y >= y && point.y < (y + height))
  31. return true;
  32. }
  33. return false;
  34. }
  35. public bool Overlaps(Rect2I other)
  36. {
  37. int otherRight = other.x + other.width;
  38. int myRight = x + width;
  39. int otherBottom = other.y + other.height;
  40. int myBottom = y + height;
  41. if(x < otherRight && myRight > other.x &&
  42. y < otherBottom && myBottom > other.y)
  43. return true;
  44. return false;
  45. }
  46. public void Clip(Rect2I clipRect)
  47. {
  48. int newLeft = Math.Max(x, clipRect.x);
  49. int newTop = Math.Max(y, clipRect.y);
  50. int newRight = Math.Min(x + width, clipRect.x + clipRect.width);
  51. int newBottom = Math.Min(y + height, clipRect.y + clipRect.height);
  52. x = Math.Min(newLeft, newRight);
  53. y = Math.Min(newTop, newBottom);
  54. width = Math.Max(0, newRight - newLeft);
  55. height = Math.Max(0, newBottom - newTop);
  56. }
  57. public override bool Equals(object other)
  58. {
  59. if (!(other is Rect2I))
  60. return false;
  61. Rect2I rect = (Rect2I)other;
  62. if (x.Equals(rect.x) && y.Equals(rect.y) && width.Equals(rect.width) && height.Equals(rect.height))
  63. return true;
  64. return false;
  65. }
  66. public override int GetHashCode()
  67. {
  68. return base.GetHashCode();
  69. }
  70. public override string ToString()
  71. {
  72. return String.Format("(x:{0} y:{1} width:{2} height:{3})", x, y, width, height);
  73. }
  74. public int x, y, width, height;
  75. }
  76. }