Chris Gregan 7 years ago
parent
commit
8254e45d1d
  1. 49
      Assets/Fungus/Scripts/Commands/VariableCondition.cs
  2. 93
      Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs
  3. 1
      Assets/Fungus/Scripts/VariableTypes/BooleanVariable.cs
  4. 16
      Assets/Fungus/Scripts/VariableTypes/FloatVariable.cs
  5. 22
      Assets/Fungus/Scripts/VariableTypes/GameObjectVariable.cs
  6. 16
      Assets/Fungus/Scripts/VariableTypes/IntegerVariable.cs
  7. 1
      Assets/Fungus/Scripts/VariableTypes/StringVariable.cs

49
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) // It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
using UnityEngine; using UnityEngine;
using System.Collections.Generic;
namespace Fungus namespace Fungus
{ {
@ -14,7 +15,8 @@ namespace Fungus
[VariableProperty(typeof(BooleanVariable), [VariableProperty(typeof(BooleanVariable),
typeof(IntegerVariable), typeof(IntegerVariable),
typeof(FloatVariable), typeof(FloatVariable),
typeof(StringVariable))] typeof(StringVariable),
typeof(GameObjectVariable))]
[SerializeField] protected Variable variable; [SerializeField] protected Variable variable;
[Tooltip("Boolean value to compare against")] [Tooltip("Boolean value to compare against")]
@ -29,31 +31,43 @@ namespace Fungus
[Tooltip("String value to compare against")] [Tooltip("String value to compare against")]
[SerializeField] protected StringDataMulti stringData; [SerializeField] protected StringDataMulti stringData;
[Tooltip("GameObject value to compare against")]
[SerializeField] protected GameObjectData gameObjectData;
protected override bool EvaluateCondition() protected override bool EvaluateCondition()
{ {
BooleanVariable booleanVariable = variable as BooleanVariable; if (variable == null)
IntegerVariable integerVariable = variable as IntegerVariable; {
FloatVariable floatVariable = variable as FloatVariable; return false;
StringVariable stringVariable = variable as StringVariable; }
bool condition = false; bool condition = false;
if (booleanVariable != null) if (variable.GetType() == typeof(BooleanVariable))
{ {
BooleanVariable booleanVariable = (variable as BooleanVariable);
condition = booleanVariable.Evaluate(compareOperator, booleanData.Value); 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); 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); 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); 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; return condition;
} }
@ -65,6 +79,19 @@ namespace Fungus
#region Public members #region Public members
public static readonly Dictionary<System.Type, CompareOperator[]> operatorsByVariableType = new Dictionary<System.Type, CompareOperator[]>() {
{ typeof(BooleanVariable), BooleanVariable.compareOperators },
{ typeof(IntegerVariable), IntegerVariable.compareOperators },
{ typeof(FloatVariable), FloatVariable.compareOperators },
{ typeof(StringVariable), StringVariable.compareOperators },
{ typeof(GameObjectVariable), GameObjectVariable.compareOperators }
};
/// <summary>
/// The type of comparison operation to be performed.
/// </summary>
public virtual CompareOperator _CompareOperator { get { return compareOperator; } }
public override string GetSummary() public override string GetSummary()
{ {
if (variable == null) if (variable == null)
@ -91,6 +118,10 @@ namespace Fungus
{ {
summary += stringData.GetDescription(); summary += stringData.GetDescription();
} }
else if (variable.GetType() == typeof(GameObjectVariable))
{
summary += gameObjectData.GetDescription();
}
return summary; return summary;
} }

93
Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs

@ -12,10 +12,8 @@ namespace Fungus.EditorUtils
{ {
protected SerializedProperty variableProp; protected SerializedProperty variableProp;
protected SerializedProperty compareOperatorProp; protected SerializedProperty compareOperatorProp;
protected SerializedProperty booleanDataProp;
protected SerializedProperty integerDataProp; protected Dictionary<System.Type, SerializedProperty> propByVariableType;
protected SerializedProperty floatDataProp;
protected SerializedProperty stringDataProp;
protected virtual void OnEnable() protected virtual void OnEnable()
{ {
@ -24,10 +22,15 @@ namespace Fungus.EditorUtils
variableProp = serializedObject.FindProperty("variable"); variableProp = serializedObject.FindProperty("variable");
compareOperatorProp = serializedObject.FindProperty("compareOperator"); compareOperatorProp = serializedObject.FindProperty("compareOperator");
booleanDataProp = serializedObject.FindProperty("booleanData");
integerDataProp = serializedObject.FindProperty("integerData"); // Get variable data props by name
floatDataProp = serializedObject.FindProperty("floatData"); propByVariableType = new Dictionary<System.Type, SerializedProperty>() {
stringDataProp = serializedObject.FindProperty("stringData"); { 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() public override void DrawCommandGUI()
@ -42,6 +45,7 @@ namespace Fungus.EditorUtils
return; return;
} }
// Select Variable
EditorGUILayout.PropertyField(variableProp); EditorGUILayout.PropertyField(variableProp);
if (variableProp.objectReferenceValue == null) if (variableProp.objectReferenceValue == null)
@ -50,42 +54,61 @@ 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();
List<GUIContent> operatorList = new List<GUIContent>(); // Get operators for the variable
operatorList.Add(new GUIContent("==")); CompareOperator[] compareOperators = VariableCondition.operatorsByVariableType[variableType];
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(">="));
}
compareOperatorProp.enumValueIndex = EditorGUILayout.Popup(new GUIContent("Compare", "The comparison operator to use when comparing values"), // Create operator list
compareOperatorProp.enumValueIndex, List<GUIContent> operatorsList = new List<GUIContent>();
operatorList.ToArray()); foreach (var compareOperator in compareOperators)
if (variableType == typeof(BooleanVariable))
{
EditorGUILayout.PropertyField(booleanDataProp);
}
else if (variableType == typeof(IntegerVariable))
{ {
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))
{ // Get previously selected operator
EditorGUILayout.PropertyField(floatDataProp); int selectedIndex = System.Array.IndexOf(compareOperators, t._CompareOperator);
} if (selectedIndex < 0)
else if (variableType == typeof(StringVariable))
{ {
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(); serializedObject.ApplyModifiedProperties();
} }
} }

1
Assets/Fungus/Scripts/VariableTypes/BooleanVariable.cs

@ -13,6 +13,7 @@ namespace Fungus
[System.Serializable] [System.Serializable]
public class BooleanVariable : VariableBase<bool> public class BooleanVariable : VariableBase<bool>
{ {
public static readonly CompareOperator[] compareOperators = { CompareOperator.Equals, CompareOperator.NotEquals };
public static readonly SetOperator[] setOperators = { SetOperator.Assign, SetOperator.Negate }; public static readonly SetOperator[] setOperators = { SetOperator.Assign, SetOperator.Negate };
public virtual bool Evaluate(CompareOperator compareOperator, bool booleanValue) public virtual bool Evaluate(CompareOperator compareOperator, bool booleanValue)

16
Assets/Fungus/Scripts/VariableTypes/FloatVariable.cs

@ -13,7 +13,21 @@ namespace Fungus
[System.Serializable] [System.Serializable]
public class FloatVariable : VariableBase<float> public class FloatVariable : VariableBase<float>
{ {
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) public virtual bool Evaluate(CompareOperator compareOperator, float floatValue)
{ {

22
Assets/Fungus/Scripts/VariableTypes/GameObjectVariable.cs

@ -14,8 +14,30 @@ namespace Fungus
[System.Serializable] [System.Serializable]
public class GameObjectVariable : VariableBase<GameObject> public class GameObjectVariable : VariableBase<GameObject>
{ {
public static readonly CompareOperator[] compareOperators = { CompareOperator.Equals, CompareOperator.NotEquals };
public static readonly SetOperator[] setOperators = { SetOperator.Assign }; 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) public override void Apply(SetOperator setOperator, GameObject value)
{ {
switch (setOperator) switch (setOperator)

16
Assets/Fungus/Scripts/VariableTypes/IntegerVariable.cs

@ -13,7 +13,21 @@ namespace Fungus
[System.Serializable] [System.Serializable]
public class IntegerVariable : VariableBase<int> public class IntegerVariable : VariableBase<int>
{ {
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) public virtual bool Evaluate(CompareOperator compareOperator, int integerValue)
{ {

1
Assets/Fungus/Scripts/VariableTypes/StringVariable.cs

@ -13,6 +13,7 @@ namespace Fungus
[System.Serializable] [System.Serializable]
public class StringVariable : VariableBase<string> public class StringVariable : VariableBase<string>
{ {
public static readonly CompareOperator[] compareOperators = { CompareOperator.Equals, CompareOperator.NotEquals };
public static readonly SetOperator[] setOperators = { SetOperator.Assign }; public static readonly SetOperator[] setOperators = { SetOperator.Assign };
public virtual bool Evaluate(CompareOperator compareOperator, string stringValue) public virtual bool Evaluate(CompareOperator compareOperator, string stringValue)

Loading…
Cancel
Save