@ -3,11 +3,15 @@
using UnityEditor ;
using UnityEditor ;
using UnityEngine ;
using UnityEngine ;
using System.Collections.Generic ;
namespace Fungus.EditorUtils
namespace Fungus.EditorUtils
{
{
[CustomEditor (typeof(VariableCondition), true)]
/// <summary>
/// Handles custom drawing for ConditionExperssions within the VariableCondition and inherited commands.
///
/// TODO; refactor to allow a propertydrawer on ConditionExperssion and potentially list as reorderable
/// </summary>
[CustomEditor(typeof(VariableCondition), true)]
public class VariableConditionEditor : CommandEditor
public class VariableConditionEditor : CommandEditor
{
{
public static readonly GUIContent None = new GUIContent ( "<None>" ) ;
public static readonly GUIContent None = new GUIContent ( "<None>" ) ;
@ -17,7 +21,7 @@ namespace Fungus.EditorUtils
None ,
None ,
} ;
} ;
static readonly GUIContent [ ] compareListAll = new GUIContent [ ]
private static readonly GUIContent [ ] compareListAll = new GUIContent [ ]
{
{
new GUIContent ( VariableUtil . GetCompareOperatorDescription ( CompareOperator . Equals ) ) ,
new GUIContent ( VariableUtil . GetCompareOperatorDescription ( CompareOperator . Equals ) ) ,
new GUIContent ( VariableUtil . GetCompareOperatorDescription ( CompareOperator . NotEquals ) ) ,
new GUIContent ( VariableUtil . GetCompareOperatorDescription ( CompareOperator . NotEquals ) ) ,
@ -27,29 +31,19 @@ namespace Fungus.EditorUtils
new GUIContent ( VariableUtil . GetCompareOperatorDescription ( CompareOperator . GreaterThanOrEquals ) ) ,
new GUIContent ( VariableUtil . GetCompareOperatorDescription ( CompareOperator . GreaterThanOrEquals ) ) ,
} ;
} ;
static readonly GUIContent [ ] compareListEqualOnly = new GUIContent [ ]
private static readonly GUIContent [ ] compareListEqualOnly = new GUIContent [ ]
{
{
new GUIContent ( VariableUtil . GetCompareOperatorDescription ( CompareOperator . Equals ) ) ,
new GUIContent ( VariableUtil . GetCompareOperatorDescription ( CompareOperator . Equals ) ) ,
new GUIContent ( VariableUtil . GetCompareOperatorDescription ( CompareOperator . NotEquals ) ) ,
new GUIContent ( VariableUtil . GetCompareOperatorDescription ( CompareOperator . NotEquals ) ) ,
} ;
} ;
// protected SerializedProperty compareOperatorProp;
protected SerializedProperty conditions ;
// protected SerializedProperty anyVarProp;
protected SerializedProperty conditions ;
protected Dictionary < System . Type , SerializedProperty > propByVariableType ;
public override void OnEnable ( )
public override void OnEnable ( )
{
{
base . OnEnable ( ) ;
base . OnEnable ( ) ;
// compareOperatorProp = serializedObject.FindProperty("compareOperator");
// anyVarProp = serializedObject.FindProperty("anyVar");
conditions = serializedObject . FindProperty ( "conditions" ) ;
conditions = serializedObject . FindProperty ( "conditions" ) ;
}
}
public override void DrawCommandGUI ( )
public override void DrawCommandGUI ( )
@ -59,36 +53,36 @@ namespace Fungus.EditorUtils
EditorGUILayout . PropertyField ( serializedObject . FindProperty ( "anyOrAllConditions" ) ) ;
EditorGUILayout . PropertyField ( serializedObject . FindProperty ( "anyOrAllConditions" ) ) ;
conditions . arraySize = EditorGUILayout . IntField ( "Size" , conditions . arraySize ) ;
conditions . arraySize = EditorGUILayout . IntField ( "Size" , conditions . arraySize ) ;
GUILayout . Label ( "Conditions" , EditorStyles . boldLabel ) ;
GUILayout . Label ( "Conditions" , EditorStyles . boldLabel ) ;
for ( int i = 0 ; i < conditions . arraySize ; i + + )
VariableCondition t = target as VariableCondition ;
{
EditorGUI . indentLevel + + ;
VariableCondition t = target as VariableCondition ;
var flowchart = ( Flowchart ) t . GetFlowchart ( ) ;
var flowchart = ( Flowchart ) t . GetFlowchart ( ) ;
if ( flowchart = = null )
if ( flowchart = = null )
{
{
return ;
return ;
}
}
// EditorGUILayout.PropertyField(anyVarProp, true);
EditorGUI . indentLevel + + ;
for ( int i = 0 ; i < conditions . arraySize ; i + + )
{
var conditionAnyVar = conditions . GetArrayElementAtIndex ( i ) . FindPropertyRelative ( "anyVar" ) ;
var conditionAnyVar = conditions . GetArrayElementAtIndex ( i ) . FindPropertyRelative ( "anyVar" ) ;
var conditionCompare = conditions . GetArrayElementAtIndex ( i ) . FindPropertyRelative ( "compareOperator" ) ;
var conditionCompare = conditions . GetArrayElementAtIndex ( i ) . FindPropertyRelative ( "compareOperator" ) ;
EditorGUILayout . PropertyField ( conditionAnyVar , new GUIContent ( "Variable" ) , true ) ;
EditorGUILayout . PropertyField ( conditionAnyVar , new GUIContent ( "Variable" ) , true ) ;
// Get selected variable
// Get selected variable
Variable selectedVariable = conditionAnyVar . FindPropertyRelative ( "variable" ) . objectReferenceValue as Variable ;
Variable selectedVariable = conditionAnyVar . FindPropertyRelative ( "variable" ) . objectReferenceValue as Variable ;
if ( selectedVariable = = null )
continue ;
GUIContent [ ] operatorsList = emptyList ;
GUIContent [ ] operatorsList = emptyList ;
if ( selectedVariable ! = null )
operatorsList = selectedVariable . IsComparisonSupported ( ) ? compareListAll : compareListEqualOnly ;
{
operatorsList = selectedVariable . IsComparisonSupported ( ) ? compareListAll : compareListEqualOnly ;
}
// Get previously selected operator
// Get previously selected operator
int selectedIndex = ( int ) t . Conditions [ i ] . CompareOperator ;
int selectedIndex = conditionCompare . enumValueIndex ;
if ( selectedIndex < 0 )
if ( selectedIndex < 0 | | selectedIndex > = operatorsList . Length )
{
{
// Default to first index if the operator is not found in the available operators list
// Default to first index if the operator is not found in the available operators list
// This can occur when changing between variable types
// This can occur when changing between variable types
@ -100,14 +94,11 @@ namespace Fungus.EditorUtils
selectedIndex ,
selectedIndex ,
operatorsList ) ;
operatorsList ) ;
if ( selectedVariable ! = null )
conditionCompare . enumValueIndex = selectedIndex ;
{
conditionCompare . enumValueIndex = selectedIndex ;
}
EditorGUI . indentLevel - - ;
EditorGUILayout . Separator ( ) ;
GUILayout . Space ( 1 0f ) ;
}
}
EditorGUI . indentLevel - - ;
serializedObject . ApplyModifiedProperties ( ) ;
serializedObject . ApplyModifiedProperties ( ) ;
}
}
}
}