Skip to content

Commit

Permalink
- A few bugfixes
Browse files Browse the repository at this point in the history
- Added keyboard navigation to Paste Bin
  • Loading branch information
yasirkula committed Feb 21, 2020
1 parent e5af314 commit fbecd0a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 22 deletions.
14 changes: 9 additions & 5 deletions Plugins/InspectPlus/Editor/InspectPlusWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ private static void ContextMenuItemNewWindow( MenuCommand command )
[MenuItem( "Assets/Inspect+/Copy Value", priority = 1500 )]
[MenuItem( "CONTEXT/Component/" + CONTEXT_COPY_LABEL, priority = 1450 )]
[MenuItem( "CONTEXT/ScriptableObject/" + CONTEXT_COPY_LABEL, priority = 1450 )]
[MenuItem( "CONTEXT/Material/" + CONTEXT_COPY_LABEL, priority = 1450 )]
private static void ContextMenuItemCopyObject( MenuCommand command )
{
if( command.context )
Expand Down Expand Up @@ -431,7 +432,8 @@ private static void ContextMenuItemPasteObject( MenuCommand command )
{
do
{
sourceProperties[property.name] = property.Copy();
if( property.name != "m_Script" )
sourceProperties[property.name] = property.Copy();
} while( property.NextVisible( false ) );
}

Expand Down Expand Up @@ -1479,14 +1481,16 @@ private void ShowScrollableListContentsAsPopup( List<List<Object>> lists )
}
} );

Rect windowPosition = position;
float windowWidth = position.width;
Rect scrollableListIconRect = GUILayoutUtility.GetLastRect();
scrollableListIconRect.position = new Vector2( windowPosition.x, scrollableListIconRect.y + windowPosition.y );
scrollableListIconRect.x = 0f;
scrollableListIconRect.width = windowWidth;
scrollableListIconRect.position = GUIUtility.GUIToScreenPoint( scrollableListIconRect.position );

if( !InspectPlusSettings.Instance.CompactFavoritesAndHistoryLists )
scrollableListIconRect.height -= 2f;

window.ShowAsDropDown( scrollableListIconRect, new Vector2( windowPosition.width, Mathf.Min( Screen.currentResolution.height * 0.5f, allObjects.Count * ( EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing ) + EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing + 5f ) ) );
window.ShowAsDropDown( scrollableListIconRect, new Vector2( windowWidth, Mathf.Min( Screen.currentResolution.height * 0.5f, allObjects.Count * ( EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing ) + EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing + 5f ) ) );
objectBrowserWindowVisible = true;
}

Expand Down Expand Up @@ -1800,7 +1804,7 @@ private static void CopyValue( object obj )
SerializedProperty prop = (SerializedProperty) obj;
object clipboard = prop.CopyValue();
if( clipboard != null )
PasteBinWindow.AddToClipboard( clipboard, prop.serializedObject.targetObject.name + "." + prop.name );
PasteBinWindow.AddToClipboard( clipboard, string.Concat( prop.serializedObject.targetObject.name, ".", prop.serializedObject.targetObject.GetType().Name, ".", prop.name ) );
}

private static void PasteValue( object obj )
Expand Down
10 changes: 6 additions & 4 deletions Plugins/InspectPlus/Editor/ObjectBrowserWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ private void OnGUI()
Rect rect = new Rect( Vector2.zero, position.size );

// Draw borders around the window
GUI.Box( rect, GUIContent.none );
if( Event.current.type == EventType.Repaint )
EditorStyles.helpBox.Draw( rect, false, false, false, false );

rect.height -= EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
scrollPosition = GUI.BeginScrollView( rect, scrollPosition, new Rect( 0f, 0f, rect.width, objects.Count * ( EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing ) + 5f ), false, false, GUIStyle.none, GUI.skin.verticalScrollbar );
Expand Down Expand Up @@ -195,15 +196,16 @@ private void OnGUI()
rect.x += EditorGUIUtility.standardVerticalSpacing;
rect.width -= 2f * EditorGUIUtility.standardVerticalSpacing;

float labelWidth = EditorGUIUtility.labelWidth;
float originalLabelWidth = EditorGUIUtility.labelWidth;
EditorGUIUtility.labelWidth = 75f;

EditorGUI.BeginChangeCheck();
sortType = (SortType) EditorGUI.EnumPopup( rect, "Sort by:", sortType, EditorStyles.toolbarDropDown );

sortType = (SortType) EditorGUI.EnumPopup( rect, "Sort by:", sortType );
if( EditorGUI.EndChangeCheck() )
SortObjects();

EditorGUIUtility.labelWidth = labelWidth;
EditorGUIUtility.labelWidth = originalLabelWidth;
}

private void SortObjects()
Expand Down
85 changes: 72 additions & 13 deletions Plugins/InspectPlus/Editor/PasteBinWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public class PasteBinWindow : EditorWindow, IHasCustomMenu
private const int CLIPBOARD_CAPACITY = 30;

private static readonly Color activeClipboardColor = new Color32( 245, 170, 10, 255 );
private static readonly GUILayoutOption expandWidth = GUILayout.ExpandWidth( true );
private static GUIStyle activeClipboardBackgroundStyle;

private static readonly List<object> clipboard = new List<object>( 4 );
private static readonly List<string> clipboardLabels = new List<string>( 4 );
private static readonly List<GUIContent> clipboardLabels = new List<GUIContent>( 4 );

private static PasteBinWindow mainWindow;

Expand All @@ -37,7 +37,7 @@ public class PasteBinWindow : EditorWindow, IHasCustomMenu
private void OnEnable()
{
mainWindow = this;
gradientField = typeof( EditorGUILayout ).GetMethod( "GradientField", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static, null, new System.Type[] { typeof( string ), typeof( Gradient ), typeof( GUILayoutOption[] ) }, null );
gradientField = typeof( EditorGUILayout ).GetMethod( "GradientField", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static, null, new System.Type[] { typeof( GUIContent ), typeof( Gradient ), typeof( GUILayoutOption[] ) }, null );

Repaint();
}
Expand All @@ -52,7 +52,7 @@ void IHasCustomMenu.AddItemsToMenu( GenericMenu menu )
menu.AddItem( new GUIContent( "Clear" ), false, ClearClipboard );
}

public static void AddToClipboard( object obj, string name )
public static void AddToClipboard( object obj, string label )
{
if( obj == null || obj.Equals( null ) )
return;
Expand All @@ -64,7 +64,7 @@ public static void AddToClipboard( object obj, string name )
}

clipboard.Add( obj );
clipboardLabels.Add( name );
clipboardLabels.Add( new GUIContent( label, label ) );

activeClipboardIndex = clipboard.Count - 1;

Expand All @@ -74,6 +74,19 @@ public static void AddToClipboard( object obj, string name )

private void OnGUI()
{
if( activeClipboardBackgroundStyle == null )
{
Texture2D background = new Texture2D( 1, 1 );
background.SetPixel( 0, 0, activeClipboardColor );
background.Apply( false, true );

activeClipboardBackgroundStyle = new GUIStyle();
activeClipboardBackgroundStyle.normal.background = background;
activeClipboardBackgroundStyle.onNormal.background = background;
}

Event ev = Event.current;

bool originalWideMode = EditorGUIUtility.wideMode;
float originalLabelWidth = EditorGUIUtility.labelWidth;

Expand All @@ -87,15 +100,15 @@ private void OnGUI()

for( int i = 0; i < clipboard.Count; i++ )
{
if( activeClipboardIndex == i )
if( clipboard[i] == null || clipboard[i].Equals( null ) )
{
Rect backgroundRect = EditorGUILayout.GetControlRect( false, 0f, expandWidth );
backgroundRect.y += EditorGUIUtility.standardVerticalSpacing;
backgroundRect.height = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;

EditorGUI.DrawRect( backgroundRect, activeClipboardColor );
RemoveClipboard( i-- );
continue;
}

if( activeClipboardIndex == i )
GUILayout.BeginHorizontal( activeClipboardBackgroundStyle );

if( clipboard[i] is Object )
clipboard[i] = EditorGUILayout.ObjectField( clipboardLabels[i], clipboard[i] as Object, typeof( Object ), true );
else if( clipboard[i] is long )
Expand Down Expand Up @@ -128,11 +141,14 @@ private void OnGUI()
GUI.enabled = true;
}

Event ev = Event.current;
if( ev.type == EventType.MouseDown && ev.button == 0 && ev.mousePosition.x <= EditorGUIUtility.labelWidth && GUILayoutUtility.GetLastRect().Contains( ev.mousePosition ) )
if( activeClipboardIndex == i )
GUILayout.EndHorizontal();

if( ev.type == EventType.MouseDown && ev.button == 0 && GUILayoutUtility.GetLastRect().Contains( ev.mousePosition ) )
{
activeClipboardIndex = i;
Repaint();
ev.Use();
}
else if( ev.type == EventType.ContextClick && GUILayoutUtility.GetLastRect().Contains( ev.mousePosition ) )
{
Expand All @@ -142,11 +158,54 @@ private void OnGUI()
menu.AddItem( new GUIContent( "Copy" ), false, SetActiveClipboard, j );
menu.AddItem( new GUIContent( "Remove" ), false, RemoveClipboard, j );
menu.ShowAsContext();

GUIUtility.keyboardControl = 0;
}
}

EditorGUILayout.EndScrollView();

if( ( ev.type == EventType.DragPerform || ev.type == EventType.DragUpdated ) && GUILayoutUtility.GetLastRect().Contains( ev.mousePosition ) )
{
// Accept drag&drop
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
if( ev.type == EventType.DragPerform )
{
DragAndDrop.AcceptDrag();

Object[] draggedObjects = DragAndDrop.objectReferences;
for( int i = 0; i < draggedObjects.Length; i++ )
AddToClipboard( draggedObjects[i], "DRAG&DROP" );
}

ev.Use();
}
else if( ev.type == EventType.KeyDown )
{
// KeyCode.Delete won't be captured by PasteBinWindow if an ObjectField has keyboard focus, therefore
// there are some "GUIUtility.keyboardControl = 0;" calls here and there to remove keyboard focus
if( ev.keyCode == KeyCode.Delete )
{
RemoveClipboard( activeClipboardIndex );
Repaint();
ev.Use();
}
else if( ev.keyCode == KeyCode.UpArrow )
{
activeClipboardIndex = Mathf.Max( 0, activeClipboardIndex - 1 );
GUIUtility.keyboardControl = 0;
Repaint();
ev.Use();
}
else if( ev.keyCode == KeyCode.DownArrow )
{
activeClipboardIndex = Mathf.Min( clipboard.Count - 1, activeClipboardIndex + 1 );
GUIUtility.keyboardControl = 0;
Repaint();
ev.Use();
}
}

EditorGUIUtility.wideMode = originalWideMode;
EditorGUIUtility.labelWidth = originalLabelWidth;
}
Expand Down

0 comments on commit fbecd0a

Please sign in to comment.