@ -8,32 +8,31 @@
using UnityEngine ;
using UnityEditor ;
using System ;
using Rotorz.ReorderableList ;
using UnityEditorInternal ;
using System.Collections.Generic ;
namespace Fungus.EditorUtils
{
public class VariableListAdaptor : IReorderableListAdaptor
public class VariableListAdaptor
{
protected class AddVariableInfo
{
public Flowchart flowchart ;
public System . Type variableType ;
}
public static readonly int DefaultWidth = 8 0 + 1 0 0 + 1 4 0 + 6 0 ;
public static readonly int ScrollSpacer = 8 ;
public static readonly int ReorderListSkirts = 7 0 ;
public static readonly int ScrollSpacer = 0 ;
public static readonly int ReorderListSkirts = 5 0 ;
protected SerializedProperty _ arrayProperty ;
public float fixedItemHeight ;
public int widthOfList ;
static public void DrawVarList ( int w , SerializedProperty variablesProp )
{
ReorderableListGUI . Title ( "Variables" ) ;
VariableListAdaptor adaptor = new VariableListAdaptor ( variablesProp , 0 , w = = 0 ? VariableListAdaptor . DefaultWidth : w ) ;
ReorderableListFlags flags = ReorderableListFlags . DisableContextMenu | ReorderableListFlags . HideAddButton ;
ReorderableListControl . DrawControlFromState ( adaptor , null , flags ) ;
}
private ReorderableList list ;
private Flowchart targetFlowchart ;
public SerializedProperty this [ int index ]
{
@ -45,88 +44,120 @@ namespace Fungus.EditorUtils
get { return _ arrayProperty ; }
}
public VariableListAdaptor ( SerializedProperty arrayProperty , float fixedItemHeight , int widthOfList )
public VariableListAdaptor ( SerializedProperty arrayProperty , float fixedItemHeight , int widthOfList , Flowchart _ targetFlowchart )
{
if ( arrayProperty = = null )
throw new ArgumentNullException ( "Array property was null." ) ;
if ( ! arrayProperty . isArray )
throw new InvalidOperationException ( "Specified serialized propery is not an array." ) ;
this . targetFlowchart = _ targetFlowchart ;
this . _ arrayProperty = arrayProperty ;
this . fixedItemHeight = fixedItemHeight ;
this . widthOfList = widthOfList - ScrollSpacer ;
list = new ReorderableList ( arrayProperty . serializedObject , arrayProperty , true , false , true , true ) ;
list . drawElementCallback = DrawItem ;
list . onRemoveCallback = RemoveItem ;
//list.drawHeaderCallback = DrawHeader;
list . onAddCallback = AddButton ;
list . onRemoveCallback = RemoveItem ;
}
public VariableListAdaptor ( SerializedProperty arrayProperty ) : this ( arrayProperty , 0f , DefaultWidth )
private void RemoveItem ( ReorderableList list )
{
int index = list . index ;
// Remove the Fungus Variable component
Variable variable = _ arrayProperty . GetArrayElementAtIndex ( index ) . objectReferenceValue as Variable ;
Undo . DestroyObjectImmediate ( variable ) ;
}
public int Count
private void AddButton ( ReorderableList list )
{
get { return _ arrayProperty . arraySize ; }
}
GenericMenu menu = new GenericMenu ( ) ;
List < System . Type > types = FlowchartEditor . FindAllDerivedTypes < Variable > ( ) ;
public virtual bool CanDrag ( int index )
{
return true ;
}
// Add variable types without a category
foreach ( var type in types )
{
VariableInfoAttribute variableInfo = VariableEditor . GetVariableInfo ( type ) ;
if ( variableInfo = = null | |
variableInfo . Category ! = "" )
{
continue ;
}
public virtual bool CanRemove ( int index )
{
return true ;
}
AddVariableInfo addVariableInfo = new AddVariableInfo ( ) ;
addVariableInfo . flowchart = targetFlowchart ;
addVariableInfo . variableType = type ;
public void Add ( )
{
int newIndex = _ arrayProperty . arraySize ;
+ + _ arrayProperty . arraySize ;
_ arrayProperty . GetArrayElementAtIndex ( newIndex ) . ResetValue ( ) ;
}
GUIContent typeName = new GUIContent ( variableInfo . VariableType ) ;
public void Insert ( int index )
{
_ arrayProperty . InsertArrayElementAtIndex ( index ) ;
_ arrayProperty . GetArrayElementAtIndex ( index ) . ResetValue ( ) ;
}
menu . AddItem ( typeName , false , AddVariable , addVariableInfo ) ;
}
public void Duplicate ( int index )
{
_ arrayProperty . InsertArrayElementAtIndex ( index ) ;
}
// Add types with a category
foreach ( var type in types )
{
VariableInfoAttribute variableInfo = VariableEditor . GetVariableInfo ( type ) ;
if ( variableInfo = = null | |
variableInfo . Category = = "" )
{
continue ;
}
public void Remove ( int index )
{
// Remove the Fungus Variable component
Variable variable = _ arrayProperty . GetArrayElementAtIndex ( index ) . objectReferenceValue as Variable ;
Undo . DestroyObjectImmediate ( variable ) ;
AddVariableInfo info = new AddVariableInfo ( ) ;
info . flowchart = targetFlowchart ;
info . variableType = type ;
_ arrayProperty . GetArrayElementAtIndex ( index ) . objectReferenceValue = null ;
_ arrayProperty . DeleteArrayElementAtIndex ( index ) ;
GUIContent typeName = new GUIContent ( variableInfo . Category + "/" + variableInfo . VariableType ) ;
menu . AddItem ( typeName , false , AddVariable , info ) ;
}
menu . ShowAsContext ( ) ;
}
public void Move ( int sourceIndex , int destIndex )
protected virtual void AddVariable ( object obj )
{
if ( destIndex > sourceIndex )
- - destIndex ;
_ arrayProperty . MoveArrayElement ( sourceIndex , destIndex ) ;
AddVariableInfo addVariableInfo = obj as AddVariableInfo ;
if ( addVariableInfo = = null )
{
return ;
}
var flowchart = addVariableInfo . flowchart ;
System . Type variableType = addVariableInfo . variableType ;
Undo . RecordObject ( flowchart , "Add Variable" ) ;
Variable newVariable = flowchart . gameObject . AddComponent ( variableType ) as Variable ;
newVariable . Key = flowchart . GetUniqueVariableKey ( "" ) ;
flowchart . Variables . Add ( newVariable ) ;
// Because this is an async call, we need to force prefab instances to record changes
PrefabUtility . RecordPrefabInstancePropertyModifications ( flowchart ) ;
}
public void Clear ( )
private void DrawHeader ( Rect rect )
{
_ arrayProperty . ClearArray ( ) ;
EditorGUI . PrefixLabel ( rect , new GUIContent ( "Variables" ) ) ;
}
public void BeginGUI ( )
{ }
public void DrawVarList ( int w )
{
this . widthOfList = ( w = = 0 ? VariableListAdaptor . DefaultWidth : w ) - ScrollSpacer ;
public void EndGUI ( )
{ }
if ( GUILayout . Button ( "Variables" ) )
{
arrayProperty . isExpanded = ! arrayProperty . isExpanded ;
}
public virtual void DrawItemBackground ( Rect position , int index )
{
if ( arrayProperty . isExpanded )
{
list . DoLayoutList ( ) ;
}
}
public void DrawItem ( Rect position , int index )
public void DrawItem ( Rect position , int index , bool selected , bool focused )
{
Variable variable = this [ index ] . objectReferenceValue as Variable ;
@ -162,7 +193,7 @@ namespace Fungus.EditorUtils
return ;
}
var flowchart = FlowchartWindow . GetFlowchart ( ) ;
var flowchart = targetFlowchart ;
if ( flowchart = = null )
{
return ;
@ -250,14 +281,6 @@ namespace Fungus.EditorUtils
GUI . backgroundColor = Color . white ;
}
public virtual float GetItemHeight ( int index )
{
return fixedItemHeight ! = 0f
? fixedItemHeight
: EditorGUI . GetPropertyHeight ( this [ index ] , GUIContent . none , false )
;
}
}
}