@ -1,12 +1,12 @@
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
using UnityEngine ;
using UnityEngine ;
using UnityEditor ;
using UnityEditor ;
using System ;
using System ;
using UnityEditorInternal ;
using UnityEditorInternal ;
using System.Collections.Generic ;
using System.Collections.Generic ;
using System.Linq ;
namespace Fungus.EditorUtils
namespace Fungus.EditorUtils
{
{
@ -47,6 +47,18 @@ namespace Fungus.EditorUtils
return this [ index ] . objectReferenceValue as Variable ;
return this [ index ] . objectReferenceValue as Variable ;
}
}
public void SetVarAt ( int index , Variable v )
{
if ( list . list ! = null )
{
list . list [ index ] = v ;
}
else
{
this [ index ] . objectReferenceValue = v ;
}
}
public VariableListAdaptor ( SerializedProperty arrayProperty , Flowchart _ targetFlowchart )
public VariableListAdaptor ( SerializedProperty arrayProperty , Flowchart _ targetFlowchart )
{
{
if ( arrayProperty = = null )
if ( arrayProperty = = null )
@ -152,6 +164,11 @@ namespace Fungus.EditorUtils
return ;
return ;
}
}
if ( Event . current . type = = EventType . ContextClick & & position . Contains ( Event . current . mousePosition ) )
{
DoRightClickMenu ( index ) ;
}
for ( int i = 0 ; i < 4 ; + + i )
for ( int i = 0 ; i < 4 ; + + i )
{
{
itemRects [ i ] = position ;
itemRects [ i ] = position ;
@ -262,6 +279,55 @@ namespace Fungus.EditorUtils
GUI . backgroundColor = Color . white ;
GUI . backgroundColor = Color . white ;
}
}
private void DoRightClickMenu ( int index )
{
var v = GetVarAt ( index ) ;
GenericMenu commandMenu = new GenericMenu ( ) ;
commandMenu . AddItem ( new GUIContent ( "Remove" ) , false , ( ) = > { list . index = index ; RemoveItem ( list ) ; } ) ;
commandMenu . AddItem ( new GUIContent ( "Duplicate" ) , false , ( ) = > VariableSelectPopupWindowContent . AddVariable ( v . GetType ( ) , v . Key ) ) ;
commandMenu . AddItem ( new GUIContent ( "Find References" ) , false , ( ) = > FindUsage ( GetVarAt ( index ) ) ) ;
commandMenu . AddSeparator ( "" ) ;
commandMenu . AddItem ( new GUIContent ( "Sort by Name" ) , false , ( ) = > SortBy ( x = > x . Key ) ) ;
commandMenu . AddItem ( new GUIContent ( "Sort by Type" ) , false , ( ) = > SortBy ( x = > x . GetType ( ) . Name ) ) ;
commandMenu . AddItem ( new GUIContent ( "Sort by Value" ) , false , ( ) = > SortBy ( x = > x . GetValue ( ) ) ) ;
commandMenu . ShowAsContext ( ) ;
}
private void SortBy < TKey > ( Func < Variable , TKey > orderFunc )
{
List < Variable > vars = new List < Variable > ( ) ;
for ( int i = 0 ; i < list . count ; i + + )
{
vars . Add ( GetVarAt ( i ) ) ;
}
vars = vars . OrderBy ( orderFunc ) . ToList ( ) ;
for ( int i = 0 ; i < list . count ; i + + )
{
SetVarAt ( i , vars [ i ] ) ;
}
_ arrayProperty . serializedObject . ApplyModifiedProperties ( ) ;
}
private void FindUsage ( Variable variable )
{
var varRefs = EditorExtensions . FindObjectsOfInterface < IVariableReference > ( )
. Where ( x = > x . HasReference ( variable ) )
. Select ( x = > x . GetLocationIdentifier ( ) ) . ToList ( ) ; ;
string varRefString = variable . Key + " referenced in;\n" ;
if ( varRefs . Count > 0 )
varRefString + = string . Join ( "\n" , varRefs ) ;
else
varRefString + = "None" ;
Debug . Log ( varRefString ) ;
}
}
}
}
}