diff --git a/Assets/Fungus/Scripts/Commands/VariableCondition.cs b/Assets/Fungus/Scripts/Commands/VariableCondition.cs index c06edbb7..e5a953b4 100644 --- a/Assets/Fungus/Scripts/Commands/VariableCondition.cs +++ b/Assets/Fungus/Scripts/Commands/VariableCondition.cs @@ -2,6 +2,7 @@ // It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE) using UnityEngine; +using System.Collections.Generic; namespace Fungus { @@ -14,7 +15,8 @@ namespace Fungus [VariableProperty(typeof(BooleanVariable), typeof(IntegerVariable), typeof(FloatVariable), - typeof(StringVariable))] + typeof(StringVariable), + typeof(GameObjectVariable))] [SerializeField] protected Variable variable; [Tooltip("Boolean value to compare against")] @@ -29,31 +31,43 @@ namespace Fungus [Tooltip("String value to compare against")] [SerializeField] protected StringDataMulti stringData; + [Tooltip("GameObject value to compare against")] + [SerializeField] protected GameObjectData gameObjectData; + protected override bool EvaluateCondition() { - BooleanVariable booleanVariable = variable as BooleanVariable; - IntegerVariable integerVariable = variable as IntegerVariable; - FloatVariable floatVariable = variable as FloatVariable; - StringVariable stringVariable = variable as StringVariable; - + if (variable == null) + { + return false; + } + bool condition = false; - - if (booleanVariable != null) + + if (variable.GetType() == typeof(BooleanVariable)) { + BooleanVariable booleanVariable = (variable as BooleanVariable); condition = booleanVariable.Evaluate(compareOperator, booleanData.Value); } - else if (integerVariable != null) + else if (variable.GetType() == typeof(IntegerVariable)) { + IntegerVariable integerVariable = (variable as IntegerVariable); condition = integerVariable.Evaluate(compareOperator, integerData.Value); } - else if (floatVariable != null) + else if (variable.GetType() == typeof(FloatVariable)) { + FloatVariable floatVariable = (variable as FloatVariable); condition = floatVariable.Evaluate(compareOperator, floatData.Value); } - else if (stringVariable != null) + else if (variable.GetType() == typeof(StringVariable)) { + StringVariable stringVariable = (variable as StringVariable); condition = stringVariable.Evaluate(compareOperator, stringData.Value); } + else if (variable.GetType() == typeof(GameObjectVariable)) + { + GameObjectVariable gameObjectVariable = (variable as GameObjectVariable); + condition = gameObjectVariable.Evaluate(compareOperator, gameObjectData.Value); + } return condition; } @@ -65,6 +79,19 @@ namespace Fungus #region Public members + public static readonly Dictionary operatorsByVariableType = new Dictionary() { + { typeof(BooleanVariable), BooleanVariable.compareOperators }, + { typeof(IntegerVariable), IntegerVariable.compareOperators }, + { typeof(FloatVariable), FloatVariable.compareOperators }, + { typeof(StringVariable), StringVariable.compareOperators }, + { typeof(GameObjectVariable), GameObjectVariable.compareOperators } + }; + + /// + /// The type of comparison operation to be performed. + /// + public virtual CompareOperator _CompareOperator { get { return compareOperator; } } + public override string GetSummary() { if (variable == null) @@ -91,6 +118,10 @@ namespace Fungus { summary += stringData.GetDescription(); } + else if (variable.GetType() == typeof(GameObjectVariable)) + { + summary += gameObjectData.GetDescription(); + } return summary; } diff --git a/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs b/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs index 8fd4ef5f..3fd3f2fd 100644 --- a/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs +++ b/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs @@ -12,10 +12,8 @@ namespace Fungus.EditorUtils { protected SerializedProperty variableProp; protected SerializedProperty compareOperatorProp; - protected SerializedProperty booleanDataProp; - protected SerializedProperty integerDataProp; - protected SerializedProperty floatDataProp; - protected SerializedProperty stringDataProp; + + protected Dictionary propByVariableType; protected virtual void OnEnable() { @@ -24,10 +22,15 @@ namespace Fungus.EditorUtils variableProp = serializedObject.FindProperty("variable"); compareOperatorProp = serializedObject.FindProperty("compareOperator"); - booleanDataProp = serializedObject.FindProperty("booleanData"); - integerDataProp = serializedObject.FindProperty("integerData"); - floatDataProp = serializedObject.FindProperty("floatData"); - stringDataProp = serializedObject.FindProperty("stringData"); + + // Get variable data props by name + propByVariableType = new Dictionary() { + { typeof(BooleanVariable), serializedObject.FindProperty("booleanData") }, + { typeof(IntegerVariable), serializedObject.FindProperty("integerData") }, + { typeof(FloatVariable), serializedObject.FindProperty("floatData") }, + { typeof(StringVariable), serializedObject.FindProperty("stringData") }, + { typeof(GameObjectVariable), serializedObject.FindProperty("gameObjectData") } + }; } public override void DrawCommandGUI() @@ -42,6 +45,7 @@ namespace Fungus.EditorUtils return; } + // Select Variable EditorGUILayout.PropertyField(variableProp); if (variableProp.objectReferenceValue == null) @@ -50,42 +54,61 @@ namespace Fungus.EditorUtils return; } + // Get selected variable Variable selectedVariable = variableProp.objectReferenceValue as Variable; System.Type variableType = selectedVariable.GetType(); - List operatorList = new List(); - operatorList.Add(new GUIContent("==")); - operatorList.Add(new GUIContent("!=")); - if (variableType == typeof(IntegerVariable) || - variableType == typeof(FloatVariable)) - { - operatorList.Add(new GUIContent("<")); - operatorList.Add(new GUIContent(">")); - operatorList.Add(new GUIContent("<=")); - operatorList.Add(new GUIContent(">=")); - } + // Get operators for the variable + CompareOperator[] compareOperators = VariableCondition.operatorsByVariableType[variableType]; - compareOperatorProp.enumValueIndex = EditorGUILayout.Popup(new GUIContent("Compare", "The comparison operator to use when comparing values"), - compareOperatorProp.enumValueIndex, - operatorList.ToArray()); - - if (variableType == typeof(BooleanVariable)) - { - EditorGUILayout.PropertyField(booleanDataProp); - } - else if (variableType == typeof(IntegerVariable)) + // Create operator list + List operatorsList = new List(); + foreach (var compareOperator in compareOperators) { - EditorGUILayout.PropertyField(integerDataProp); + switch (compareOperator) + { + case CompareOperator.Equals: + operatorsList.Add(new GUIContent("==")); + break; + case CompareOperator.NotEquals: + operatorsList.Add(new GUIContent("!=")); + break; + case CompareOperator.LessThan: + operatorsList.Add(new GUIContent("<")); + break; + case CompareOperator.GreaterThan: + operatorsList.Add(new GUIContent(">")); + break; + case CompareOperator.LessThanOrEquals: + operatorsList.Add(new GUIContent("<=")); + break; + case CompareOperator.GreaterThanOrEquals: + operatorsList.Add(new GUIContent(">=")); + break; + default: + Debug.LogError("The " + compareOperator.ToString() + " operator has no matching GUIContent."); + break; + } } - else if (variableType == typeof(FloatVariable)) - { - EditorGUILayout.PropertyField(floatDataProp); - } - else if (variableType == typeof(StringVariable)) + + // Get previously selected operator + int selectedIndex = System.Array.IndexOf(compareOperators, t._CompareOperator); + if (selectedIndex < 0) { - EditorGUILayout.PropertyField(stringDataProp); + // Default to first index if the operator is not found in the available operators list + // This can occur when changing between variable types + selectedIndex = 0; } + selectedIndex = EditorGUILayout.Popup( + new GUIContent("Compare", "The comparison operator to use when comparing values"), + selectedIndex, + operatorsList.ToArray()); + + compareOperatorProp.enumValueIndex = (int)compareOperators[selectedIndex]; + + EditorGUILayout.PropertyField(propByVariableType[variableType]); + serializedObject.ApplyModifiedProperties(); } } diff --git a/Assets/Fungus/Scripts/VariableTypes/BooleanVariable.cs b/Assets/Fungus/Scripts/VariableTypes/BooleanVariable.cs index 1037241a..30cb3120 100644 --- a/Assets/Fungus/Scripts/VariableTypes/BooleanVariable.cs +++ b/Assets/Fungus/Scripts/VariableTypes/BooleanVariable.cs @@ -13,6 +13,7 @@ namespace Fungus [System.Serializable] public class BooleanVariable : VariableBase { + public static readonly CompareOperator[] compareOperators = { CompareOperator.Equals, CompareOperator.NotEquals }; public static readonly SetOperator[] setOperators = { SetOperator.Assign, SetOperator.Negate }; public virtual bool Evaluate(CompareOperator compareOperator, bool booleanValue) diff --git a/Assets/Fungus/Scripts/VariableTypes/FloatVariable.cs b/Assets/Fungus/Scripts/VariableTypes/FloatVariable.cs index d3e9614f..e64464f0 100644 --- a/Assets/Fungus/Scripts/VariableTypes/FloatVariable.cs +++ b/Assets/Fungus/Scripts/VariableTypes/FloatVariable.cs @@ -13,7 +13,21 @@ namespace Fungus [System.Serializable] public class FloatVariable : VariableBase { - public static readonly SetOperator[] setOperators = { SetOperator.Assign, SetOperator.Add, SetOperator.Subtract, SetOperator.Multiply, SetOperator.Divide }; + public static readonly CompareOperator[] compareOperators = { + CompareOperator.Equals, + CompareOperator.NotEquals, + CompareOperator.LessThan, + CompareOperator.GreaterThan, + CompareOperator.LessThanOrEquals, + CompareOperator.GreaterThanOrEquals + }; + public static readonly SetOperator[] setOperators = { + SetOperator.Assign, + SetOperator.Add, + SetOperator.Subtract, + SetOperator.Multiply, + SetOperator.Divide + }; public virtual bool Evaluate(CompareOperator compareOperator, float floatValue) { diff --git a/Assets/Fungus/Scripts/VariableTypes/GameObjectVariable.cs b/Assets/Fungus/Scripts/VariableTypes/GameObjectVariable.cs index 4cf7b1ba..6b627563 100644 --- a/Assets/Fungus/Scripts/VariableTypes/GameObjectVariable.cs +++ b/Assets/Fungus/Scripts/VariableTypes/GameObjectVariable.cs @@ -14,8 +14,30 @@ namespace Fungus [System.Serializable] public class GameObjectVariable : VariableBase { + public static readonly CompareOperator[] compareOperators = { CompareOperator.Equals, CompareOperator.NotEquals }; public static readonly SetOperator[] setOperators = { SetOperator.Assign }; + public virtual bool Evaluate(CompareOperator compareOperator, GameObject gameObjectValue) + { + GameObject lhs = Value; + GameObject rhs = gameObjectValue; + + bool condition = false; + + switch (compareOperator) + { + case CompareOperator.Equals: + condition = lhs == rhs; + break; + case CompareOperator.NotEquals: + default: + condition = lhs != rhs; + break; + } + + return condition; + } + public override void Apply(SetOperator setOperator, GameObject value) { switch (setOperator) diff --git a/Assets/Fungus/Scripts/VariableTypes/IntegerVariable.cs b/Assets/Fungus/Scripts/VariableTypes/IntegerVariable.cs index a3a78b78..52617eff 100644 --- a/Assets/Fungus/Scripts/VariableTypes/IntegerVariable.cs +++ b/Assets/Fungus/Scripts/VariableTypes/IntegerVariable.cs @@ -13,7 +13,21 @@ namespace Fungus [System.Serializable] public class IntegerVariable : VariableBase { - public static readonly SetOperator[] setOperators = { SetOperator.Assign, SetOperator.Add, SetOperator.Subtract, SetOperator.Multiply, SetOperator.Divide }; + public static readonly CompareOperator[] compareOperators = { + CompareOperator.Equals, + CompareOperator.NotEquals, + CompareOperator.LessThan, + CompareOperator.GreaterThan, + CompareOperator.LessThanOrEquals, + CompareOperator.GreaterThanOrEquals + }; + public static readonly SetOperator[] setOperators = { + SetOperator.Assign, + SetOperator.Add, + SetOperator.Subtract, + SetOperator.Multiply, + SetOperator.Divide + }; public virtual bool Evaluate(CompareOperator compareOperator, int integerValue) { diff --git a/Assets/Fungus/Scripts/VariableTypes/StringVariable.cs b/Assets/Fungus/Scripts/VariableTypes/StringVariable.cs index c9772b3b..75921ef0 100644 --- a/Assets/Fungus/Scripts/VariableTypes/StringVariable.cs +++ b/Assets/Fungus/Scripts/VariableTypes/StringVariable.cs @@ -13,6 +13,7 @@ namespace Fungus [System.Serializable] public class StringVariable : VariableBase { + public static readonly CompareOperator[] compareOperators = { CompareOperator.Equals, CompareOperator.NotEquals }; public static readonly SetOperator[] setOperators = { SetOperator.Assign }; public virtual bool Evaluate(CompareOperator compareOperator, string stringValue)