@ -11,12 +11,24 @@ namespace Fungus.EditorUtils
[CustomEditor (typeof(SetVariable))]
[CustomEditor (typeof(SetVariable))]
public class SetVariableEditor : CommandEditor
public class SetVariableEditor : CommandEditor
{
{
protected struct VariablePropertyInfo
{
public string name ;
public SerializedProperty dataProp ;
public VariablePropertyInfo ( string name , SerializedProperty dataProp ) {
this . name = name ;
this . dataProp = dataProp ;
}
}
protected SerializedProperty variableProp ;
protected SerializedProperty variableProp ;
protected SerializedProperty setOperatorProp ;
protected SerializedProperty setOperatorProp ;
protected SerializedProperty booleanDataProp ;
protected SerializedProperty integerDataProp ;
// Variable data props
protected SerializedProperty floatDataProp ;
protected Dictionary < System . Type , VariablePropertyInfo > propertyInfoByVariableType ;
protected SerializedProperty stringDataProp ;
protected List < SerializedProperty > variableDataProps ;
protected virtual void OnEnable ( )
protected virtual void OnEnable ( )
{
{
@ -25,10 +37,15 @@ namespace Fungus.EditorUtils
variableProp = serializedObject . FindProperty ( "variable" ) ;
variableProp = serializedObject . FindProperty ( "variable" ) ;
setOperatorProp = serializedObject . FindProperty ( "setOperator" ) ;
setOperatorProp = serializedObject . FindProperty ( "setOperator" ) ;
booleanDataProp = serializedObject . FindProperty ( "booleanData" ) ;
integerDataProp = serializedObject . FindProperty ( "integerData" ) ;
// Get variable data props by name
floatDataProp = serializedObject . FindProperty ( "floatData" ) ;
propertyInfoByVariableType = new Dictionary < System . Type , VariablePropertyInfo > ( ) {
stringDataProp = serializedObject . FindProperty ( "stringData" ) ;
{ typeof ( BooleanVariable ) , new VariablePropertyInfo ( "Boolean" , serializedObject . FindProperty ( "booleanData" ) ) } ,
{ typeof ( IntegerVariable ) , new VariablePropertyInfo ( "Integer" , serializedObject . FindProperty ( "integerData" ) ) } ,
{ typeof ( FloatVariable ) , new VariablePropertyInfo ( "Float" , serializedObject . FindProperty ( "floatData" ) ) } ,
{ typeof ( StringVariable ) , new VariablePropertyInfo ( "String" , serializedObject . FindProperty ( "stringData" ) ) } ,
{ typeof ( GameObjectVariable ) , new VariablePropertyInfo ( "GameObject" , serializedObject . FindProperty ( "gameObjectData" ) ) }
} ;
}
}
public override void DrawCommandGUI ( )
public override void DrawCommandGUI ( )
@ -43,6 +60,7 @@ namespace Fungus.EditorUtils
return ;
return ;
}
}
// Select Variable
EditorGUILayout . PropertyField ( variableProp ) ;
EditorGUILayout . PropertyField ( variableProp ) ;
if ( variableProp . objectReferenceValue = = null )
if ( variableProp . objectReferenceValue = = null )
@ -51,107 +69,62 @@ namespace Fungus.EditorUtils
return ;
return ;
}
}
// Get selected variable
Variable selectedVariable = variableProp . objectReferenceValue as Variable ;
Variable selectedVariable = variableProp . objectReferenceValue as Variable ;
System . Type variableType = selectedVariable . GetType ( ) ;
System . Type variableType = selectedVariable . GetType ( ) ;
// Get operators for the variable
SetOperator [ ] setOperators = SetVariable . operatorsByVariableType [ variableType ] ;
// Create operator list
List < GUIContent > operatorsList = new List < GUIContent > ( ) ;
List < GUIContent > operatorsList = new List < GUIContent > ( ) ;
operatorsList . Add ( new GUIContent ( "=" ) ) ;
foreach ( var setOperator in setOperators )
if ( variableType = = typeof ( BooleanVariable ) )
{
{
operatorsList . Add ( new GUIContent ( "=!" ) ) ;
switch ( setOperator )
}
else if ( variableType = = typeof ( IntegerVariable ) | |
variableType = = typeof ( FloatVariable ) )
{
{
operatorsList . Add ( new GUIContent ( "+=" ) ) ;
operatorsList . Add ( new GUIContent ( "-=" ) ) ;
operatorsList . Add ( new GUIContent ( "*=" ) ) ;
operatorsList . Add ( new GUIContent ( "\\=" ) ) ;
}
int selectedIndex = 0 ;
switch ( t . _ SetOperator )
{
default :
case SetOperator . Assign :
case SetOperator . Assign :
selectedIndex = 0 ;
operatorsList . Add ( new GUIContent ( "=" ) ) ;
break ;
break ;
case SetOperator . Negate :
case SetOperator . Negate :
selectedIndex = 1 ;
operatorsList . Add ( new GUIContent ( "=!" ) ) ;
break ;
break ;
case SetOperator . Add :
case SetOperator . Add :
selectedIndex = 1 ;
operatorsList . Add ( new GUIContent ( "+=" ) ) ;
break ;
break ;
case SetOperator . Subtract :
case SetOperator . Subtract :
selectedIndex = 2 ;
operatorsList . Add ( new GUIContent ( "-=" ) ) ;
break ;
break ;
case SetOperator . Multiply :
case SetOperator . Multiply :
selectedIndex = 3 ;
operatorsList . Add ( new GUIContent ( "*=" ) ) ;
break ;
break ;
case SetOperator . Divide :
case SetOperator . Divide :
selectedIndex = 4 ;
operatorsList . Add ( new GUIContent ( "\\=" ) ) ;
break ;
break ;
}
selectedIndex = EditorGUILayout . Popup ( new GUIContent ( "Operation" , "Arithmetic operator to use" ) , selectedIndex , operatorsList . ToArray ( ) ) ;
SetOperator setOperator = SetOperator . Assign ;
if ( variableType = = typeof ( BooleanVariable ) | |
variableType = = typeof ( StringVariable ) )
{
switch ( selectedIndex )
{
default :
default :
case 0 :
Debug . LogError ( "The " + setOperator . ToString ( ) + " operator has no matching GUIContent." ) ;
setOperator = SetOperator . Assign ;
break ;
case 1 :
setOperator = SetOperator . Negate ;
break ;
break ;
}
}
}
}
else if ( variableType = = typeof ( IntegerVariable ) | |
variableType = = typeof ( FloatVariable ) )
// Get previously selected operator
{
int selectedIndex = System . Array . IndexOf ( setOperators , t . _ SetOperator ) ;
switch ( selectedIndex )
if ( selectedIndex < 0 )
{
{
default :
// Default to first index if the operator is not found in the available operators list
case 0 :
// This can occur when changing between variable types
setOperator = SetOperator . Assign ;
selectedIndex = 0 ;
break ;
case 1 :
setOperator = SetOperator . Add ;
break ;
case 2 :
setOperator = SetOperator . Subtract ;
break ;
case 3 :
setOperator = SetOperator . Multiply ;
break ;
case 4 :
setOperator = SetOperator . Divide ;
break ;
}
}
}
setOperatorProp . enumValueIndex = ( int ) setOperator ;
// Get next selected operator
selectedIndex = EditorGUILayout . Popup ( new GUIContent ( "Operation" , "Arithmetic operator to use" ) , selectedIndex , operatorsList . ToArray ( ) ) ;
setOperatorProp . enumValueIndex = ( int ) setOperators [ selectedIndex ] ;
VariablePropertyInfo propertyInfo = propertyInfoByVariableType [ variableType ] ;
EditorGUILayout . PropertyField ( propertyInfo . dataProp , new GUIContent ( propertyInfo . name ) ) ;
if ( variableType = = typeof ( BooleanVariable ) )
{
EditorGUILayout . PropertyField ( booleanDataProp , new GUIContent ( "Boolean" ) ) ;
}
else if ( variableType = = typeof ( IntegerVariable ) )
{
EditorGUILayout . PropertyField ( integerDataProp , new GUIContent ( "Integer" ) ) ;
}
else if ( variableType = = typeof ( FloatVariable ) )
{
EditorGUILayout . PropertyField ( floatDataProp , new GUIContent ( "Float" ) ) ;
}
else if ( variableType = = typeof ( StringVariable ) )
{
EditorGUILayout . PropertyField ( stringDataProp , new GUIContent ( "String" ) ) ;
}
serializedObject . ApplyModifiedProperties ( ) ;
serializedObject . ApplyModifiedProperties ( ) ;
}
}