Object2DMassEraseShape.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 Object2DMassEraseShape
  10. {
  11. #region Private Variables
  12. [SerializeField]
  13. private EllipseObjectInteractionShape _circleShape = new EllipseObjectInteractionShape();
  14. #endregion
  15. #region Public Properties
  16. public EllipseShapeRenderSettings CircleShapeRenderSettings { get { return _circleShape.RenderSettings; } }
  17. #endregion
  18. #region Public Methods
  19. public void RenderGizmos()
  20. {
  21. if (IsVisible())
  22. {
  23. EnsureWidthAndHeightAreUpToDate();
  24. GetShape().RenderGizmos();
  25. }
  26. }
  27. public List<GameObject> GetOverlappedGameObjectsForEraseOperation()
  28. {
  29. return GetShape().GetOverlappedGameObjects(ObjectEraserSettings.Get().Mass2DEraseSettings.AllowPartialOverlap);
  30. }
  31. public bool IsVisible()
  32. {
  33. if (ObjectEraserSettings.Get().EraseMode != ObjectEraseMode.ObjectMass2D) return false;
  34. if (!SceneViewCamera.Camera.pixelRect.Contains(Event.current.mousePosition)) return false;
  35. return true;
  36. }
  37. public void HandleMouseMoveEvent(Event e)
  38. {
  39. if (IsVisible())
  40. {
  41. EnsureWidthAndHeightAreUpToDate();
  42. EnsureCenterIsUpToDate();
  43. }
  44. }
  45. public void HandleMouseDragEvent(Event e)
  46. {
  47. if(IsVisible())
  48. {
  49. EnsureWidthAndHeightAreUpToDate();
  50. EnsureCenterIsUpToDate();
  51. }
  52. }
  53. #endregion
  54. #region Private Methods
  55. private ObjectInteraction2DShape GetShape()
  56. {
  57. return _circleShape;
  58. }
  59. private void EnsureWidthAndHeightAreUpToDate()
  60. {
  61. Object2DMassEraseSettings objectMassEraseSettings = ObjectEraserSettings.Get().Mass2DEraseSettings;
  62. SetWidthHeight(objectMassEraseSettings.CircleShapeRadius, objectMassEraseSettings.CircleShapeRadius);
  63. }
  64. private void EnsureCenterIsUpToDate()
  65. {
  66. GetShape().EnclosingRectCenter = Event.current.InvMousePos(SceneViewCamera.Camera);
  67. }
  68. private void SetWidthHeight(float width, float height)
  69. {
  70. Vector2 center = _circleShape.EnclosingRectCenter;
  71. _circleShape.EnclosingRectWidth = width;
  72. _circleShape.EnclosingRectHeight = height;
  73. _circleShape.EnclosingRectCenter = center;
  74. }
  75. #endregion
  76. }
  77. }
  78. #endif