public override void OnGUI() { var id = GetControlID( FocusType.Passive ); if ( GUIUtility.hotControl != 0 && GUIUtility.hotControl != id ) return; if ( Input.KeyDown( KeyCode.LeftAlt ) || Input.KeyDown( KeyCode.RightAlt ) ) return; if ( ( Input.KeyDown( KeyCode.LeftShift ) || Input.KeyDown( KeyCode.RightShift ) ) && Input.ButtonReleased( EMouseButton.Left ) ) { var controls = Window.GetControlsSlow<SelectableControl>(); foreach ( var item in controls ) { if ( item.Contains( Input.MousePosition ) ) { item.OnSelect(); SelectedControls.Add( item ); return; } } } if ( Input.ButtonDown( EMouseButton.Left ) ) { if ( SelectedControls.Count < 2 ) { var controls = Window.GetControlsSlow<SelectableControl>(); var newControls = new List<SelectableControl>(); for ( int i = 0; i < controls.Count; i++ ) { if ( controls[i].Contains( Input.MousePosition ) ) { newControls.Add( controls[i] ); } } if ( newControls.Count == 1 ) { for ( int i = 0; i < SelectedControls.Count; i++ ) { SelectedControls[i].OnDeselect(); } SelectedControls.Clear(); SelectedControls.Add( newControls[0] ); newControls[0].OnSelect(); } } } if ( Input.Type == EventType.MouseDrag && Input.ButtonDown( EMouseButton.Left ) ) { var controls = Window.GetControlsSlow<SelectableControl>(); var delta = Input.MouseDelta; if ( Window.Settings.UseCamera ) { delta = Window.ScaleMatrix.inverse.MultiplyVector( delta ); } if ( !startedDrag && SelectedControls.Count > 0 ) { for ( int i = 0; i < SelectedControls.Count; i++ ) { if ( !dragControls ) { if ( SelectedControls[i].Contains( Input.MousePosition ) ) { GUIUtility.hotControl = id; GUIUtility.keyboardControl = 0; dragControls = true; i = -1; } } else { SelectedControls[i].Move( delta ); } } } if ( dragControls ) return; if ( !startedDrag ) { GUIUtility.hotControl = id; GUIUtility.keyboardControl = 0; start = Input.MousePosition; startedDrag = true; } end = Input.MousePosition; var minx = Mathf.Min( start.x, end.x ); var maxx = Mathf.Max( start.x, end.x ); var miny = Mathf.Min( start.y, end.y ); var maxy = Mathf.Max( start.y, end.y ); Position.Set( minx, miny ); Size.Set( maxx - minx, maxy - miny ); foreach ( var item in controls ) { if ( Rectangle.Contains( item.Center() ) ) { if ( !item.IsSelected ) { item.OnSelect(); SelectedControls.Add( item ); } } else { if ( item.IsSelected ) { item.OnDeselect(); SelectedControls.Remove( item ); } } } } else if ( Input.IsDoubleClick && Input.Button == EMouseButton.Left ) { foreach ( var item in SelectedControls ) { item.OnDeselect(); } SelectedControls.Clear(); } else if ( Input.ButtonReleased( EMouseButton.Left ) ) { startedDrag = false; dragControls = false; if ( GUIUtility.hotControl == id ) { GUIUtility.hotControl = 0; } } if ( startedDrag ) { GUI.Box( Rectangle, "" ); } }
public override void OnGUI() { if ( items.Length == 0 ) return; base.OnGUI(); var itemsToProcess = new List<string>( items ); var listRect = Rectangle; var viewRect = Rectangle; var boxRect = new Rect( Rectangle.x - 1, Rectangle.y - 1, Rectangle.width + 2, Rectangle.height + 2 ); var lineHeight = GUI.skin.label.CalcSize( new GUIContent( items[0] ) ).y; var mouseDown = Input.ButtonPressed( EMouseButton.Left ); var mousePos = Input.MousePosition; var controlID = GetControlID( FocusType.Passive ); GUI.Box( boxRect, "", EditorStyles.helpBox ); if ( searchable ) { searchText = ExtendedGUI.ToolbarSearchFieldWithBackground( listRect, searchText ).ToLower(); for ( int i = itemsToProcess.Count - 1; i >= 0; i-- ) { if ( !itemsToProcess[i].ToLower().Contains( searchText ) ) { itemsToProcess.RemoveAt( i ); } } listRect.y += 17.5f; listRect.height -= 17.5f; } if ( scrollable ) { var h = lineHeight * itemsToProcess.Count; if ( h > viewRect.height ) { viewRect.height = h; viewRect.width -= 15f; listRect.height -= 17.5f; } if ( searchable ) { viewRect.y += 17.5f; viewRect.height -= 17.5f; } } if ( mouseDown ) { index = -1; if ( new Rect( listRect.x, listRect.y, viewRect.width, listRect.height ).Contains( mousePos ) ) { GUIUtility.hotControl = controlID; GUIUtility.keyboardControl = 0; } } scrollPosition = GUI.BeginScrollView( listRect, scrollPosition, viewRect, false, false ); for ( int i = 0; i < itemsToProcess.Count; i++ ) { var r = new Rect( viewRect.x, viewRect.y + ( i * lineHeight ), viewRect.width, lineHeight ); if ( i % 2 == 0 ) { EditorGUI.DrawRect( r, alternateColor ); } if ( mouseDown ) { if ( listRect.Contains( mousePos ) ) { if ( r.Contains( mousePos + scrollPosition ) ) { index = i; if ( OnSelectedItemChanged != null ) { OnSelectedItemChanged.Invoke( this, new ListEventArgs( i, itemsToProcess[i] ) ); } GUIUtility.keyboardControl = 0; } } } else { GUI.Label( r, itemsToProcess[i] ); } if ( index == i ) { EditorGUI.DrawRect( r, highlightColor ); GUI.Label( r, itemsToProcess[i] ); } else { GUI.Label( r, itemsToProcess[i] ); } if ( Input.IsDoubleClick && Input.Button == EMouseButton.Left ) { if ( listRect.Contains( mousePos ) ) { if ( r.Contains( mousePos + scrollPosition ) ) { if ( OnItemDoubleClick != null ) { OnItemDoubleClick.Invoke( this, new ListEventArgs( i, itemsToProcess[i] ) ); } } } } } GUI.EndScrollView(); if ( Input.ButtonUp( EMouseButton.Left ) && GUIUtility.hotControl == controlID ) { GUIUtility.hotControl = 0; } }