ObjectInteraction2DShape.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace O3DWB
  6. {
  7. [Serializable]
  8. public abstract class ObjectInteraction2DShape
  9. {
  10. #region Private Variables
  11. private Rect _enclosingRect;
  12. #endregion
  13. #region Public Properties
  14. public Rect EnclosingRect { get { return _enclosingRect; } set { _enclosingRect = value; } }
  15. public Vector2 EnclosingRectCenter { get { return _enclosingRect.center; } set { _enclosingRect.center = value; } }
  16. public float EnclosingRectWidth { get { return _enclosingRect.width; } set { _enclosingRect.width = value; } }
  17. public float EnclosingRectHeight { get { return _enclosingRect.height; } set { _enclosingRect.height = value; } }
  18. #endregion
  19. #region Public Methods
  20. public void SetEnclosingRectMinMaxPoints(Vector2 minMaxPoint)
  21. {
  22. SetEnclosingRectMinPoint(minMaxPoint);
  23. SetEnclosingRectMaxPoint(minMaxPoint);
  24. }
  25. public void SetEnclosingRectMinMaxPoints(Vector2 minPoint, Vector2 maxPoint)
  26. {
  27. SetEnclosingRectMinPoint(minPoint);
  28. SetEnclosingRectMaxPoint(maxPoint);
  29. }
  30. public void SetEnclosingRectMinPoint(Vector2 minPoint)
  31. {
  32. _enclosingRect.xMin = minPoint.x;
  33. _enclosingRect.yMin = minPoint.y;
  34. }
  35. public void SetEnclosingRectMaxPoint(Vector2 maxPoint)
  36. {
  37. _enclosingRect.xMax = maxPoint.x;
  38. _enclosingRect.yMax = maxPoint.y;
  39. }
  40. public void AdjustEnclosingRectSizeUsing1To1Ratio(Vector2 mousePosition, Vector2 pivotPoint)
  41. {
  42. List<Vector2> rectangleCornerPoints = _enclosingRect.GetCornerPoints();
  43. Vector2 fromPivotPointToMousePos = mousePosition - pivotPoint;
  44. float deltaX = fromPivotPointToMousePos.x;
  45. float deltaY = fromPivotPointToMousePos.y;
  46. float absDeltaX = Mathf.Abs(deltaX);
  47. float absDeltaY = Mathf.Abs(deltaY);
  48. Vector2 absRectSize = _enclosingRect.size.GetVectorWithAbsoluteValueComponents();
  49. if (absDeltaY > absRectSize.y && absDeltaX <= absRectSize.x)
  50. {
  51. Vector2 bottomLeftRectPoint = rectangleCornerPoints[3];
  52. float distanceFromSegment = mousePosition.GetPointDistanceFromSegment(pivotPoint, bottomLeftRectPoint, true);
  53. _enclosingRect.max = _enclosingRect.min + new Vector2(distanceFromSegment * Mathf.Sign(deltaX), distanceFromSegment * Mathf.Sign(deltaY));
  54. }
  55. else
  56. if(absDeltaX > absRectSize.x && absDeltaY <= absRectSize.y)
  57. {
  58. Vector2 topRightRectPoint = rectangleCornerPoints[1];
  59. float distanceFromSegment = mousePosition.GetPointDistanceFromSegment(pivotPoint, topRightRectPoint, true);
  60. _enclosingRect.max = _enclosingRect.min + new Vector2(distanceFromSegment * Mathf.Sign(deltaX), distanceFromSegment * Mathf.Sign(deltaY));
  61. }
  62. else
  63. if ((absDeltaX > absRectSize.x && absDeltaY > absRectSize.y) || _enclosingRect.Contains(mousePosition, true))
  64. {
  65. float minAbsValue = absDeltaX;
  66. if (minAbsValue > absDeltaY) minAbsValue = absDeltaY;
  67. _enclosingRect.max = _enclosingRect.min + new Vector2(minAbsValue * Mathf.Sign(deltaX), minAbsValue * Mathf.Sign(deltaY));
  68. }
  69. }
  70. #endregion
  71. #region Public Abstract Methods
  72. public abstract void RenderGizmos();
  73. public abstract List<GameObject> GetOverlappedGameObjects(bool allowPartialOverlap);
  74. #endregion
  75. #region Protected Methods
  76. protected List<GameObject> GetGameObjectsOverlappedByEnclosingRect(bool allowPartialOverlap)
  77. {
  78. Camera camera = SceneViewCamera.Camera;
  79. List<GameObject> visibleGameObjects = SceneViewCamera.Instance.GetVisibleGameObjects();
  80. if (visibleGameObjects.Count == 0) return new List<GameObject>();
  81. if (allowPartialOverlap) return GetGameObjectsOverlappedByEnclosingRectForPartialOverlap(visibleGameObjects, camera);
  82. else return GetGameObjectsOverlappedByEnclosingRectForFullOverlap(visibleGameObjects, camera);
  83. }
  84. #endregion
  85. #region Private Methods
  86. private List<GameObject> GetGameObjectsOverlappedByEnclosingRectForPartialOverlap(List<GameObject> visibleGameObjects, Camera camera)
  87. {
  88. var overlappedGameObjects = new List<GameObject>(visibleGameObjects.Count);
  89. foreach (GameObject visibleGameObject in visibleGameObjects)
  90. {
  91. if (visibleGameObject.GetScreenRectangle(camera).Overlaps(_enclosingRect, true)) overlappedGameObjects.Add(visibleGameObject);
  92. }
  93. return overlappedGameObjects;
  94. }
  95. private List<GameObject> GetGameObjectsOverlappedByEnclosingRectForFullOverlap(List<GameObject> visibleGameObjects, Camera camera)
  96. {
  97. var overlappedGameObjects = new List<GameObject>(visibleGameObjects.Count);
  98. foreach (GameObject visibleGameObject in visibleGameObjects)
  99. {
  100. OrientedBox worldOrientedBox = visibleGameObject.GetWorldOrientedBox();
  101. if (worldOrientedBox.IsValid() && _enclosingRect.FullyContainsOrientedBoxCenterAndCornerPointsInScreenSpace(worldOrientedBox, camera)) overlappedGameObjects.Add(visibleGameObject);
  102. }
  103. return overlappedGameObjects;
  104. }
  105. #endregion
  106. }
  107. }
  108. #endif