void CompleteRectSelection() { EditorApplication.modifierKeysChanged -= SendCommandsOnModifierKeys; m_RectSelecting = false; ActiveEditorTracker.delayFlushDirtyRebuild = false; ActiveEditorTracker.RebuildAllIfNecessary(); m_SelectionStart = new Object[0]; rectSelectionFinished(); }
public void OnGUI() { Event evt = Event.current; Handles.BeginGUI(); Vector2 mousePos = evt.mousePosition; int id = s_RectSelectionID; switch (evt.GetTypeForControl(id)) { case EventType.Layout: case EventType.MouseMove: if (!Tools.viewToolActive) { HandleUtility.AddDefaultControl(id); } break; case EventType.MouseDown: if (HandleUtility.nearestControl == id && evt.button == 0) { GUIUtility.hotControl = id; m_SelectStartPoint = mousePos; m_SelectionStart = Selection.objects; m_RectSelecting = false; } break; case EventType.MouseDrag: if (GUIUtility.hotControl == id) { if (!m_RectSelecting && (mousePos - m_SelectStartPoint).magnitude > 6f) { EditorApplication.modifierKeysChanged += SendCommandsOnModifierKeys; m_RectSelecting = true; ActiveEditorTracker.delayFlushDirtyRebuild = true; m_LastSelection = null; m_CurrentSelection = null; rectSelectionStarting(); } if (m_RectSelecting) { m_SelectMousePoint = new Vector2(Mathf.Max(mousePos.x, 0), Mathf.Max(mousePos.y, 0)); GameObject[] rectObjs = HandleUtility.PickRectObjects(EditorGUIExt.FromToRect(m_SelectStartPoint, m_SelectMousePoint)); m_CurrentSelection = rectObjs; bool setIt = false; if (m_LastSelection == null) { m_LastSelection = new Dictionary <GameObject, bool>(); setIt = true; } setIt |= m_LastSelection.Count != rectObjs.Length; if (!setIt) { Dictionary <GameObject, bool> set = new Dictionary <GameObject, bool>(rectObjs.Length); foreach (GameObject g in rectObjs) { set.Add(g, false); } foreach (GameObject g in m_LastSelection.Keys) { if (!set.ContainsKey(g)) { setIt = true; break; } } } if (setIt) { m_LastSelection = new Dictionary <GameObject, bool>(rectObjs.Length); foreach (GameObject g in rectObjs) { m_LastSelection.Add(g, false); } if (evt.shift) { UpdateSelection(m_SelectionStart, rectObjs, SelectionType.Additive, m_RectSelecting); } else if (EditorGUI.actionKey) { UpdateSelection(m_SelectionStart, rectObjs, SelectionType.Subtractive, m_RectSelecting); } else { UpdateSelection(m_SelectionStart, rectObjs, SelectionType.Normal, m_RectSelecting); } } } evt.Use(); } break; case EventType.Repaint: if (GUIUtility.hotControl == id && m_RectSelecting) { EditorStyles.selectionRect.Draw(EditorGUIExt.FromToRect(m_SelectStartPoint, m_SelectMousePoint), GUIContent.none, false, false, false, false); } break; case EventType.MouseUp: if (GUIUtility.hotControl == id && evt.button == 0) { GUIUtility.hotControl = 0; if (m_RectSelecting) { EditorApplication.modifierKeysChanged -= SendCommandsOnModifierKeys; m_RectSelecting = false; ActiveEditorTracker.delayFlushDirtyRebuild = false; ActiveEditorTracker.RebuildAllIfNecessary(); m_SelectionStart = new Object[0]; rectSelectionFinished(); evt.Use(); } else { if (evt.shift || EditorGUI.actionKey) { // For shift, we check if EXACTLY the active GO is hovered by mouse and then subtract. Otherwise additive. // For control/cmd, we check if ANY of the selected GO is hovered by mouse and then subtract. Otherwise additive. // Control/cmd takes priority over shift. GameObject hovered = HandleUtility.PickGameObject(evt.mousePosition, false); if (EditorGUI.actionKey ? Selection.gameObjects.Contains(hovered) : Selection.activeGameObject == hovered) { UpdateSelection(m_SelectionStart, hovered, SelectionType.Subtractive, m_RectSelecting); } else { UpdateSelection(m_SelectionStart, HandleUtility.PickGameObject(evt.mousePosition, true), SelectionType.Additive, m_RectSelecting); } } else // With no modifier keys, we do the "cycle through overlapped" picking logic in SceneViewPicking.cs { GameObject picked = SceneViewPicking.PickGameObject(evt.mousePosition); UpdateSelection(m_SelectionStart, picked, SelectionType.Normal, m_RectSelecting); } evt.Use(); } } break; case EventType.ExecuteCommand: if (id == GUIUtility.hotControl && evt.commandName == EventCommandNames.ModifierKeysChanged) { if (evt.shift) { UpdateSelection(m_SelectionStart, m_CurrentSelection, SelectionType.Additive, m_RectSelecting); } else if (EditorGUI.actionKey) { UpdateSelection(m_SelectionStart, m_CurrentSelection, SelectionType.Subtractive, m_RectSelecting); } else { UpdateSelection(m_SelectionStart, m_CurrentSelection, SelectionType.Normal, m_RectSelecting); } evt.Use(); } break; } Handles.EndGUI(); }