ObjectSelectionShape.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System;
  5. using System.Collections.Generic;
  6. namespace O3DWB
  7. {
  8. [Serializable]
  9. public class ObjectSelectionShape
  10. {
  11. #region Private Variables
  12. private bool _isVisibleForStandardMode = false;
  13. [SerializeField]
  14. private RectangleObjectInteractionShape _rectangleShape = new RectangleObjectInteractionShape();
  15. [SerializeField]
  16. private EllipseObjectInteractionShape _ellipseShape = new EllipseObjectInteractionShape();
  17. #endregion
  18. #region Public Properties
  19. public RectangleShapeRenderSettings RectangleShapeRenderSettings { get { return _rectangleShape.RenderSettings; } }
  20. public EllipseShapeRenderSettings EllipseShapeRenderSettings { get { return _ellipseShape.RenderSettings; } }
  21. #endregion
  22. #region Public Methods
  23. public void RenderGizmos()
  24. {
  25. if (IsVisible())
  26. {
  27. OnBeforeRender();
  28. GetShape().RenderGizmos();
  29. }
  30. }
  31. public List<GameObject> GetOverlappedGameObjects()
  32. {
  33. if(ObjectSelectionSettings.Get().SelectionMode == ObjectSelectionMode.Standard)
  34. {
  35. // Note: We will only select objects if at least one of the enclosing rectangle dimensions
  36. // is >= 'minRectSize'. This helps avoid situations in which the user wants to click on
  37. // a game object, but they accodentally drag the mouse a little bit which causes unwanted
  38. // objects to be selected.
  39. const int minRectSize = 15;
  40. ObjectInteraction2DShape shape = GetShape();
  41. Rect enclosingRectangle = shape.EnclosingRect;
  42. if (Mathf.Abs(enclosingRectangle.size.x) >= minRectSize ||
  43. Mathf.Abs(enclosingRectangle.size.y) >= minRectSize) return shape.GetOverlappedGameObjects(ObjectSelectionSettings.Get().AllowPartialOverlap);
  44. else return new List<GameObject>();
  45. }
  46. else return GetShape().GetOverlappedGameObjects(ObjectSelectionSettings.Get().AllowPartialOverlap);
  47. }
  48. public bool IsVisible()
  49. {
  50. if (!MouseCursor.Instance.IsInsideSceneView()) return false;
  51. if (ObjectSelectionSettings.Get().SelectionMode == ObjectSelectionMode.Standard) return _isVisibleForStandardMode;
  52. return true;
  53. }
  54. public void HandleMouseButtonDownEvent(Event e)
  55. {
  56. if(e.InvolvesLeftMouseButton())
  57. {
  58. e.DisableInSceneView();
  59. _isVisibleForStandardMode = true;
  60. if (ObjectSelectionSettings.Get().SelectionMode == ObjectSelectionMode.Standard) GetShape().SetEnclosingRectMinMaxPoints(e.InvMousePos(SceneViewCamera.Camera));
  61. }
  62. }
  63. public void HandleMouseButtonUpEvent(Event e)
  64. {
  65. if(e.InvolvesLeftMouseButton())
  66. {
  67. _isVisibleForStandardMode = false;
  68. if (ObjectSelectionSettings.Get().SelectionMode == ObjectSelectionMode.Standard) SceneView.RepaintAll();
  69. }
  70. }
  71. public void HandleMouseDragEvent(Event e)
  72. {
  73. if(e.InvolvesLeftMouseButton())
  74. {
  75. if (ObjectSelectionSettings.Get().SelectionMode == ObjectSelectionMode.Standard) AdjustStandardShapeSizeForMouseDragEvent(e);
  76. if (!MouseCursor.Instance.IsInsideSceneView()) _isVisibleForStandardMode = false;
  77. }
  78. }
  79. public void HandleMouseMoveEvent(Event e)
  80. {
  81. SceneView.RepaintAll();
  82. }
  83. #endregion
  84. #region Private Methods
  85. private ObjectInteraction2DShape GetShape()
  86. {
  87. ObjectSelectionSettings selectionSettings = ObjectSelectionSettings.Get();
  88. if (selectionSettings.SelectionShapeType == ObjectSelectionShapeType.Ellipse) return _ellipseShape;
  89. else if (selectionSettings.SelectionShapeType == ObjectSelectionShapeType.Rectangle) return _rectangleShape;
  90. return null;
  91. }
  92. private void OnBeforeRender()
  93. {
  94. ObjectSelectionSettings selectionSettings = ObjectSelectionSettings.Get();
  95. if(selectionSettings.SelectionMode == ObjectSelectionMode.Paint)
  96. {
  97. ObjectSelectionPaintModeSettings paintModeSettings = selectionSettings.PaintModeSettings;
  98. SetWidthHeight(paintModeSettings.SelectionShapeWidthInPixels, paintModeSettings.SelectionShapeHeightInPixels);
  99. GetShape().EnclosingRectCenter = Event.current.InvMousePos(SceneViewCamera.Camera);
  100. SceneView.RepaintAll();
  101. }
  102. }
  103. private void AdjustStandardShapeSizeForMouseDragEvent(Event e)
  104. {
  105. ObjectInteraction2DShape shape = GetShape();
  106. shape.SetEnclosingRectMaxPoint(e.InvMousePos(SceneViewCamera.Camera));
  107. SceneView.RepaintAll();
  108. }
  109. private void SetWidthHeight(float width, float height)
  110. {
  111. _rectangleShape.EnclosingRectWidth = width;
  112. _rectangleShape.EnclosingRectHeight = height;
  113. _ellipseShape.EnclosingRectWidth = width;
  114. _ellipseShape.EnclosingRectHeight = height;
  115. }
  116. #endregion
  117. }
  118. }
  119. #endif