MouseDragObjectSelectionUpdateOperation.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. namespace O3DWB
  5. {
  6. public class MouseDragObjectSelectionUpdateOperation : ObjectSelectionUpdateOperation
  7. {
  8. #region Private Variables
  9. private bool _mustAppendObjectsToSelection;
  10. private bool _mustDeselectObjects;
  11. #endregion
  12. #region Public Methods
  13. public override void Perform()
  14. {
  15. if (Event.current.InvolvesLeftMouseButton())
  16. {
  17. Event.current.DisableInSceneView();
  18. AcquireAppendAndDeselectStates();
  19. UpdateSelection();
  20. }
  21. }
  22. #endregion
  23. #region Private Methods
  24. private void UpdateSelection()
  25. {
  26. List<GameObject> gameObjectsOverlappedBySelectionShape = ObjectSelection.Get().GetAllGameObjectsOverlappedBySelectionShape();
  27. if (!_mustAppendObjectsToSelection && !_mustDeselectObjects) UpdateSelectionForNoAppendAndNoDeselect(gameObjectsOverlappedBySelectionShape);
  28. else
  29. if (_mustAppendObjectsToSelection) AppendObjectsToSelection(gameObjectsOverlappedBySelectionShape);
  30. else
  31. if (_mustDeselectObjects) DeselectObjectsWithSelectionShape(gameObjectsOverlappedBySelectionShape);
  32. }
  33. private void AcquireAppendAndDeselectStates()
  34. {
  35. _mustDeselectObjects = AllShortcutCombos.Instance.EnableDeselectObjectsWithSelectionShape.IsActive();
  36. // Note: We append only if:
  37. // -the append shortcut is active;
  38. // -the selection mode is set to 'Paint' and the user is not intending on deselecting objects with the object selection shape.
  39. _mustAppendObjectsToSelection = AllShortcutCombos.Instance.EnableAppendObjectsToSelection.IsActive() ||
  40. (ObjectSelectionSettings.Get().SelectionMode == ObjectSelectionMode.Paint && !_mustDeselectObjects);
  41. }
  42. private void UpdateSelectionForNoAppendAndNoDeselect(List<GameObject> gameObjectsOverlappedBySelectionShape)
  43. {
  44. ObjectSelection objectSelection = ObjectSelection.Get();
  45. ObjectSelectionUpdateMode _selectionUpdateMode = ObjectSelectionSettings.Get().SelectionUpdateMode;
  46. // If no object was overlapped by the selection shape, we can clear the selection.
  47. // Note: We only clear the selection if the current number of selected objects is not 0. This
  48. // allows us to avoid registering an Undo for nothing.
  49. if (gameObjectsOverlappedBySelectionShape.Count == 0 && objectSelection.NumberOfSelectedObjects != 0)
  50. {
  51. UndoEx.RecordForToolAction(objectSelection);
  52. objectSelection.Clear();
  53. }
  54. else
  55. // When the selection shape has overlapped objects, we must reset the selection by clearing it and selecting only
  56. // the objects which were overlapped.
  57. if (gameObjectsOverlappedBySelectionShape.Count != 0)
  58. {
  59. UndoEx.RecordForToolAction(objectSelection);
  60. objectSelection.Clear();
  61. if(_selectionUpdateMode == ObjectSelectionUpdateMode.EntireHierarchy) objectSelection.AddEntireGameObjectHierarchyToSelection(gameObjectsOverlappedBySelectionShape);
  62. else objectSelection.AddGameObjectCollectionToSelection(gameObjectsOverlappedBySelectionShape);
  63. objectSelection.ObjectSelectionGizmos.OnObjectSelectionUpdatedUsingSelectionShape();
  64. }
  65. }
  66. private void AppendObjectsToSelection(List<GameObject> gameObjectsOverlappedBySelectionShape)
  67. {
  68. if (gameObjectsOverlappedBySelectionShape.Count != 0)
  69. {
  70. ObjectSelection objectSelection = ObjectSelection.Get();
  71. ObjectSelectionUpdateMode _selectionUpdateMode = ObjectSelectionSettings.Get().SelectionUpdateMode;
  72. // Note: We only continue if the current object selection is not the same as the
  73. // collection of objects we are appending. If it is, we would be registering
  74. // an unnecessary Undo operation.
  75. if (!objectSelection.IsSameAs(gameObjectsOverlappedBySelectionShape))
  76. {
  77. if (_selectionUpdateMode == ObjectSelectionUpdateMode.EntireHierarchy)
  78. {
  79. UndoEx.RecordForToolAction(objectSelection);
  80. objectSelection.AddEntireGameObjectHierarchyToSelection(gameObjectsOverlappedBySelectionShape);
  81. }
  82. else
  83. {
  84. UndoEx.RecordForToolAction(objectSelection);
  85. objectSelection.AddGameObjectCollectionToSelection(gameObjectsOverlappedBySelectionShape);
  86. }
  87. objectSelection.ObjectSelectionGizmos.OnObjectSelectionUpdatedUsingSelectionShape();
  88. }
  89. }
  90. }
  91. private void DeselectObjectsWithSelectionShape(List<GameObject> gameObjectsOverlappedBySelectionShape)
  92. {
  93. if (gameObjectsOverlappedBySelectionShape.Count != 0)
  94. {
  95. ObjectSelection objectSelection = ObjectSelection.Get();
  96. ObjectSelectionUpdateMode _selectionUpdateMode = ObjectSelectionSettings.Get().SelectionUpdateMode;
  97. if(_selectionUpdateMode == ObjectSelectionUpdateMode.EntireHierarchy)
  98. {
  99. UndoEx.RecordForToolAction(objectSelection);
  100. objectSelection.RemoveEntireGameObjectHierarchyFromSelection(gameObjectsOverlappedBySelectionShape);
  101. }
  102. else
  103. {
  104. UndoEx.RecordForToolAction(objectSelection);
  105. objectSelection.RemoveGameObjectCollectionFromSelection(gameObjectsOverlappedBySelectionShape);
  106. }
  107. objectSelection.ObjectSelectionGizmos.OnObjectSelectionUpdatedUsingSelectionShape();
  108. }
  109. }
  110. #endregion
  111. }
  112. }
  113. #endif