Browse Source

Refactor compare variable system and add comparison for GameObjects

The main point of this refactor is to make it easier to add variable comparison options
for new variables and to better match the set variable system.
master
Jorge Ramirez 7 years ago
parent
commit
c93f81a007
  1. 49
      Assets/Fungus/Scripts/Commands/VariableCondition.cs
  2. 89
      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)
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<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()
{
if (variable == null)
@ -91,6 +118,10 @@ namespace Fungus
{
summary += stringData.GetDescription();
}
else if (variable.GetType() == typeof(GameObjectVariable))
{
summary += gameObjectData.GetDescription();
}
return summary;
}

89
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<System.Type, SerializedProperty> 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<System.Type, SerializedProperty>() {
{ 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<GUIContent> operatorList = new List<GUIContent>();
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(">="));
}
compareOperatorProp.enumValueIndex = EditorGUILayout.Popup(new GUIContent("Compare", "The comparison operator to use when comparing values"),
compareOperatorProp.enumValueIndex,
operatorList.ToArray());
// Get operators for the variable
CompareOperator[] compareOperators = VariableCondition.operatorsByVariableType[variableType];
if (variableType == typeof(BooleanVariable))
// Create operator list
List<GUIContent> operatorsList = new List<GUIContent>();
foreach (var compareOperator in compareOperators)
{
EditorGUILayout.PropertyField(booleanDataProp);
}
else if (variableType == typeof(IntegerVariable))
switch (compareOperator)
{
EditorGUILayout.PropertyField(integerDataProp);
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();
}
}

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

@ -13,6 +13,7 @@ namespace Fungus
[System.Serializable]
public class BooleanVariable : VariableBase<bool>
{
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)

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

@ -13,7 +13,21 @@ namespace Fungus
[System.Serializable]
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)
{

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

@ -14,8 +14,30 @@ namespace Fungus
[System.Serializable]
public class GameObjectVariable : VariableBase<GameObject>
{
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)

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

@ -13,7 +13,21 @@ namespace Fungus
[System.Serializable]
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)
{

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

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

Loading…
Cancel
Save