@ -17,6 +17,21 @@ namespace Fungus
t . hideFlags = HideFlags . HideInInspector ;
t . hideFlags = HideFlags . HideInInspector ;
}
}
public static VariableInfoAttribute GetVariableInfo ( System . Type variableType )
{
object [ ] attributes = variableType . GetCustomAttributes ( typeof ( VariableInfoAttribute ) , false ) ;
foreach ( object obj in attributes )
{
VariableInfoAttribute variableInfoAttr = obj as VariableInfoAttribute ;
if ( variableInfoAttr ! = null )
{
return variableInfoAttr ;
}
}
return null ;
}
static public void VariableField ( SerializedProperty property , GUIContent label , FungusScript fungusScript , Func < Variable , bool > filter = null )
static public void VariableField ( SerializedProperty property , GUIContent label , FungusScript fungusScript , Func < Variable , bool > filter = null )
{
{
List < string > variableKeys = new List < string > ( ) ;
List < string > variableKeys = new List < string > ( ) ;
@ -81,93 +96,31 @@ namespace Fungus
}
}
}
}
[CustomPropertyDrawer (typeof(BooleanData))]
public class VariableDataDrawer < T > : PropertyDrawer where T : Variable
public class BooleanDataDrawer : PropertyDrawer
{
{
protected enum BooleanState
{
True ,
False
}
public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
{
{
EditorGUI . BeginProperty ( position , label , property ) ;
EditorGUI . BeginProperty ( position , label , property ) ;
SerializedProperty referenceProp = property . FindPropertyRelative ( "booleanRef" ) ;
// The variable reference and data properties must follow the naming convention 'typeRef', 'typeVal'
SerializedProperty valueProp = property . FindPropertyRelative ( "booleanVal" ) ;
const int popupWidth = 6 5 ;
Rect controlRect = EditorGUI . PrefixLabel ( position , label ) ;
Rect valueRect = controlRect ;
valueRect . width = controlRect . width - popupWidth - 5 ;
Rect popupRect = controlRect ;
if ( referenceProp . objectReferenceValue = = null )
{
valueProp . boolValue = ( ( BooleanState ) EditorGUI . EnumPopup ( valueRect , valueProp . boolValue ? BooleanState . True : BooleanState . False ) = = BooleanState . True ) ;
popupRect . x + = valueRect . width + 5 ;
popupRect . width = popupWidth ;
}
FungusScript fungusScript = property . serializedObject . targetObject as FungusScript ;
if ( fungusScript = = null )
{
Command command = property . serializedObject . targetObject as Command ;
if ( command ! = null )
{
fungusScript = command . GetFungusScript ( ) ;
}
}
if ( fungusScript ! = null )
{
BooleanVariable selectedBooleanVariable = referenceProp . objectReferenceValue as BooleanVariable ;
List < string > variableKeys = new List < string > ( ) ;
List < Variable > variableObjects = new List < Variable > ( ) ;
variableKeys . Add ( "<Value>" ) ;
variableObjects . Add ( null ) ;
int index = 0 ;
VariableInfoAttribute typeInfo = VariableEditor . GetVariableInfo ( typeof ( T ) ) ;
int selectedIndex = 0 ;
if ( typeInfo = = null )
foreach ( Variable v in fungusScript . variables )
{
if ( v . GetType ( ) ! = typeof ( BooleanVariable ) )
{
{
continue ;
return ;
}
}
variableKeys . Add ( v . key ) ;
string propNameBase = typeInfo . VariableType ;
variableObjects . Add ( v ) ;
propNameBase = Char . ToLowerInvariant ( propNameBase [ 0 ] ) + propNameBase . Substring ( 1 ) ;
index + + ;
SerializedProperty referenceProp = property . FindPropertyRelative ( propNameBase + "Ref" ) ;
SerializedProperty valueProp = property . FindPropertyRelative ( propNameBase + "Val" ) ;
if ( v = = selectedBooleanVariable )
if ( referenceProp = = null | | valueProp = = null )
{
{
selectedIndex = index ;
return ;
}
}
selectedIndex = EditorGUI . Popup ( popupRect , selectedIndex , variableKeys . ToArray ( ) ) ;
referenceProp . objectReferenceValue = variableObjects [ selectedIndex ] ;
}
EditorGUI . EndProperty ( ) ;
}
}
}
[CustomPropertyDrawer (typeof(IntegerData))]
public class IntegerDataDrawer : PropertyDrawer
{
public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
{
EditorGUI . BeginProperty ( position , label , property ) ;
SerializedProperty referenceProp = property . FindPropertyRelative ( "integerRef" ) ;
SerializedProperty valueProp = property . FindPropertyRelative ( "integerVal" ) ;
const int popupWidth = 6 5 ;
const int popupWidth = 6 5 ;
@ -178,7 +131,7 @@ namespace Fungus
if ( referenceProp . objectReferenceValue = = null )
if ( referenceProp . objectReferenceValue = = null )
{
{
valueProp . intValue = EditorGUI . IntField ( valueRect , valueProp . intValue ) ;
EditorGUI . PropertyField ( valueRect , valueProp , new GUIContent ( "" ) ) ;
popupRect . x + = valueRect . width + 5 ;
popupRect . x + = valueRect . width + 5 ;
popupRect . width = popupWidth ;
popupRect . width = popupWidth ;
}
}
@ -195,7 +148,7 @@ namespace Fungus
if ( fungusScript ! = null )
if ( fungusScript ! = null )
{
{
IntegerVariable selectedVariable = referenceProp . objectReferenceValue as IntegerVariable ;
T selectedVariable = referenceProp . objectReferenceValue as T ;
List < string > variableKeys = new List < string > ( ) ;
List < string > variableKeys = new List < string > ( ) ;
List < Variable > variableObjects = new List < Variable > ( ) ;
List < Variable > variableObjects = new List < Variable > ( ) ;
@ -207,7 +160,7 @@ namespace Fungus
int selectedIndex = 0 ;
int selectedIndex = 0 ;
foreach ( Variable v in fungusScript . variables )
foreach ( Variable v in fungusScript . variables )
{
{
if ( v . GetType ( ) ! = typeof ( IntegerVariable ) )
if ( v . GetType ( ) ! = typeof ( T ) )
{
{
continue ;
continue ;
}
}
@ -231,149 +184,51 @@ namespace Fungus
}
}
}
}
[CustomPropertyDrawer (typeof(FloatData))]
[CustomPropertyDrawer (typeof(BooleanData))]
public class FloatDataDrawer : PropertyDrawer
public class BooleanDataDrawer : VariableDataDrawer < BooleanVariable >
{
{ }
public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
{
EditorGUI . BeginProperty ( position , label , property ) ;
SerializedProperty referenceProp = property . FindPropertyRelative ( "floatRef" ) ;
SerializedProperty valueProp = property . FindPropertyRelative ( "floatVal" ) ;
const int popupWidth = 6 5 ;
Rect controlRect = EditorGUI . PrefixLabel ( position , label ) ;
Rect valueRect = controlRect ;
valueRect . width = controlRect . width - popupWidth - 5 ;
Rect popupRect = controlRect ;
if ( referenceProp . objectReferenceValue = = null )
{
valueProp . floatValue = EditorGUI . FloatField ( valueRect , valueProp . floatValue ) ;
popupRect . x + = valueRect . width + 5 ;
popupRect . width = popupWidth ;
}
FungusScript fungusScript = property . serializedObject . targetObject as FungusScript ;
if ( fungusScript = = null )
{
Command command = property . serializedObject . targetObject as Command ;
if ( command ! = null )
{
fungusScript = command . GetFungusScript ( ) ;
}
}
if ( fungusScript ! = null )
{
FloatVariable selectedVariable = referenceProp . objectReferenceValue as FloatVariable ;
List < string > variableKeys = new List < string > ( ) ;
List < Variable > variableObjects = new List < Variable > ( ) ;
variableKeys . Add ( "<Value>" ) ;
variableObjects . Add ( null ) ;
int index = 0 ;
int selectedIndex = 0 ;
foreach ( Variable v in fungusScript . variables )
{
if ( v . GetType ( ) ! = typeof ( FloatVariable ) )
{
continue ;
}
variableKeys . Add ( v . key ) ;
variableObjects . Add ( v ) ;
index + + ;
if ( v = = selectedVariable )
{
selectedIndex = index ;
}
}
selectedIndex = EditorGUI . Popup ( popupRect , selectedIndex , variableKeys . ToArray ( ) ) ;
[CustomPropertyDrawer (typeof(IntegerData))]
referenceProp . objectReferenceValue = variableObjects [ selectedIndex ] ;
public class IntegerDataDrawer : VariableDataDrawer < IntegerVariable >
}
{ }
EditorGUI . EndProperty ( ) ;
[CustomPropertyDrawer (typeof(FloatData))]
}
public class FloatDataDrawer : VariableDataDrawer < FloatVariable >
}
{ }
[CustomPropertyDrawer (typeof(StringData))]
[CustomPropertyDrawer (typeof(StringData))]
public class StringDataDrawer : PropertyDrawer
public class StringDataDrawer : VariableDataDrawer < StringVariable >
{
{ }
public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
{
EditorGUI . BeginProperty ( position , label , property ) ;
SerializedProperty referenceProp = property . FindPropertyRelative ( "stringRef" ) ;
[CustomPropertyDrawer (typeof(ColorData))]
SerializedProperty valueProp = property . FindPropertyRelative ( "stringVal" ) ;
public class ColorDataDrawer : VariableDataDrawer < ColorVariable >
{ }
const int popupWidth = 6 5 ;
[CustomPropertyDrawer (typeof(Vector2Data))]
public class Vector2DataDrawer : VariableDataDrawer < Vector2Variable >
{ }
Rect controlRect = EditorGUI . PrefixLabel ( position , label ) ;
[CustomPropertyDrawer (typeof(Vector3Data))]
Rect valueRect = controlRect ;
public class Vector3DataDrawer : VariableDataDrawer < Vector3Variable >
valueRect . width = controlRect . width - popupWidth - 5 ;
{ }
Rect popupRect = controlRect ;
if ( referenceProp . objectReferenceValue = = null )
[CustomPropertyDrawer (typeof(MaterialData))]
{
public class MaterialDataDrawer : VariableDataDrawer < MaterialVariable >
// StringData stringData = valueProp.serializedObject
{ }
valueProp . stringValue = EditorGUI . TextField ( valueRect , valueProp . stringValue ) ;
popupRect . x + = valueRect . width + 5 ;
popupRect . width = popupWidth ;
}
FungusScript fungusScript = property . serializedObject . targetObject as FungusScript ;
if ( fungusScript = = null )
{
Command command = property . serializedObject . targetObject as Command ;
if ( command ! = null )
{
fungusScript = command . GetFungusScript ( ) ;
}
}
if ( fungusScript ! = null )
{
StringVariable selectedVariable = referenceProp . objectReferenceValue as StringVariable ;
List < string > variableKeys = new List < string > ( ) ;
[CustomPropertyDrawer (typeof(TextureData))]
List < Variable > variableObjects = new List < Variable > ( ) ;
public class TextureDataDrawer : VariableDataDrawer < TextureVariable >
{ }
variableKeys . Add ( "<Value>" ) ;
[CustomPropertyDrawer (typeof(SpriteData))]
variableObjects . Add ( null ) ;
public class SpriteDataDrawer : VariableDataDrawer < SpriteVariable >
{ }
int index = 0 ;
[CustomPropertyDrawer (typeof(GameObjectData))]
int selectedIndex = 0 ;
public class GameObjectDataDrawer : VariableDataDrawer < GameObjectVariable >
foreach ( Variable v in fungusScript . variables )
{ }
{
if ( v . GetType ( ) ! = typeof ( StringVariable ) )
{
continue ;
}
variableKeys . Add ( v . key ) ;
variableObjects . Add ( v ) ;
index + + ;
if ( v = = selectedVariable )
{
selectedIndex = index ;
}
}
selectedIndex = EditorGUI . Popup ( popupRect , selectedIndex , variableKeys . ToArray ( ) ) ;
referenceProp . objectReferenceValue = variableObjects [ selectedIndex ] ;
}
EditorGUI . EndProperty ( ) ;
}
}
[CustomPropertyDrawer (typeof(ObjectData))]
public class ObjectDataDrawer : VariableDataDrawer < ObjectVariable >
{ }
}
}