Browse Source

basic support for multiple conditions added - WIP

master
vjs22334 5 years ago
parent
commit
ca22eb792d
  1. 300
      Assets/Fungus/Scripts/Commands/VariableCondition.cs
  2. 84
      Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs

300
Assets/Fungus/Scripts/Commands/VariableCondition.cs

@ -6,29 +6,85 @@ using System.Collections.Generic;
namespace Fungus namespace Fungus
{ {
[System.Serializable]
public class conditionExpression
{
[SerializeField] protected CompareOperator compareOperator;
[SerializeField] protected AnyVariableAndDataPair anyVar;
public virtual AnyVariableAndDataPair AnyVar { get { return anyVar; } }
public virtual CompareOperator CompareOperator { get { return compareOperator; } }
public conditionExpression(){}
public conditionExpression(CompareOperator op, AnyVariableAndDataPair variablePair)
{
compareOperator = op;
anyVar = variablePair;
}
}
// anyone with a better name for this can update it
public enum AnyOrAllConditions
{
AnyOneTrue,
AllTrue
}
public abstract class VariableCondition : Condition, ISerializationCallbackReceiver public abstract class VariableCondition : Condition, ISerializationCallbackReceiver
{ {
[Tooltip("The type of comparison to be performed")] [Tooltip("The type of comparison to be performed")]
[SerializeField] protected CompareOperator compareOperator;
[SerializeField] protected AnyVariableAndDataPair anyVar = new AnyVariableAndDataPair(); [SerializeField] protected AnyOrAllConditions anyOrAllConditions;
[SerializeField] protected List<conditionExpression> conditions = new List<conditionExpression>();
protected override bool EvaluateCondition() protected override bool EvaluateCondition()
{ {
if (anyVar.variable == null) if (conditions == null || conditions.Count == 0)
{ {
return false; return false;
} }
bool condition = false; bool resultAny = false, resultAll = true;
anyVar.Compare(compareOperator, ref condition); foreach (conditionExpression condition in conditions)
{
bool curResult = false;
if (condition.AnyVar == null)
{
resultAll &= curResult;
resultAny |= curResult;
continue;
}
condition.AnyVar.Compare(condition.CompareOperator, ref curResult);
Debug.Log("res : " + curResult.ToString());
resultAll &= curResult;
resultAny |= curResult;
}
if (anyOrAllConditions == AnyOrAllConditions.AnyOneTrue) return resultAny;
return condition; return resultAll;
} }
protected override bool HasNeededProperties() protected override bool HasNeededProperties()
{ {
return (anyVar.variable != null); if( conditions == null || conditions.Count == 0){
return false;
}
foreach (conditionExpression condition in conditions)
{
if(condition.AnyVar == null || condition.AnyVar.variable == null){
return false;
}
}
return true;
} }
#region Public members #region Public members
@ -36,18 +92,40 @@ namespace Fungus
/// <summary> /// <summary>
/// The type of comparison operation to be performed. /// The type of comparison operation to be performed.
/// </summary> /// </summary>
public virtual CompareOperator CompareOperator { get { return compareOperator; } } public virtual CompareOperator CompareOperator { get { return conditions[0].CompareOperator; } }
public virtual List<conditionExpression> Conditions { get { return conditions; } }
public override string GetSummary() public override string GetSummary()
{ {
if (anyVar.variable == null) if (!this.HasNeededProperties())
{ {
return "Error: No variable selected"; return "Error: No variable selected";
} }
string summary = anyVar.variable.Key + " "; string summary = "";
summary += VariableUtil.GetCompareOperatorDescription(compareOperator) + " "; string connector = "";
summary += anyVar.GetDataDescription(); if(anyOrAllConditions == AnyOrAllConditions.AnyOneTrue){
connector = " Or ";
}
else{
connector = " And ";
}
for(int i = 0 ; i < conditions.Count; i++)
{
summary += conditions[i].AnyVar.variable.Key + " ";
summary += VariableUtil.GetCompareOperatorDescription(conditions[i].CompareOperator) + " ";
summary += conditions[i].AnyVar.GetDataDescription();
if(i<conditions.Count-1){
summary += connector;
}
}
return summary; return summary;
} }
@ -61,6 +139,10 @@ namespace Fungus
#region backwards compat #region backwards compat
[SerializeField] protected CompareOperator compareOperator;
[SerializeField] protected AnyVariableAndDataPair anyVar;
[Tooltip("Variable to use in expression")] [Tooltip("Variable to use in expression")]
[VariableProperty(AllVariableTypes.VariableAny.Any)] [VariableProperty(AllVariableTypes.VariableAny.Any)]
@ -120,100 +202,116 @@ namespace Fungus
void ISerializationCallbackReceiver.OnAfterDeserialize() void ISerializationCallbackReceiver.OnAfterDeserialize()
{ {
if (variable == null)
{
return;
}
else
{
anyVar.variable = variable;
}
if (variable.GetType() == typeof(BooleanVariable) && !booleanData.Equals(new BooleanData()))
{ if( variable != null)
anyVar.data.booleanData = booleanData;
booleanData = new BooleanData();
}
else if (variable.GetType() == typeof(IntegerVariable) && !integerData.Equals(new IntegerData()))
{
anyVar.data.integerData = integerData;
integerData = new IntegerData();
}
else if (variable.GetType() == typeof(FloatVariable) && !floatData.Equals(new FloatData()))
{
anyVar.data.floatData = floatData;
floatData = new FloatData();
}
else if (variable.GetType() == typeof(StringVariable) && !stringData.Equals(new StringDataMulti()))
{
anyVar.data.stringData.stringRef = stringData.stringRef;
anyVar.data.stringData.stringVal = stringData.stringVal;
stringData = new StringDataMulti();
}
else if (variable.GetType() == typeof(AnimatorVariable) && !animatorData.Equals(new AnimatorData()))
{
anyVar.data.animatorData = animatorData;
animatorData = new AnimatorData();
}
else if (variable.GetType() == typeof(AudioSourceVariable) && !audioSourceData.Equals(new AudioSourceData()))
{
anyVar.data.audioSourceData = audioSourceData;
audioSourceData = new AudioSourceData();
}
else if (variable.GetType() == typeof(ColorVariable) && !colorData.Equals(new ColorData()))
{
anyVar.data.colorData = colorData;
colorData = new ColorData();
}
else if (variable.GetType() == typeof(GameObjectVariable) && !gameObjectData.Equals(new GameObjectData()))
{
anyVar.data.gameObjectData = gameObjectData;
gameObjectData = new GameObjectData();
}
else if (variable.GetType() == typeof(MaterialVariable) && !materialData.Equals(new MaterialData()))
{
anyVar.data.materialData = materialData;
materialData = new MaterialData();
}
else if (variable.GetType() == typeof(ObjectVariable) && !objectData.Equals(new ObjectData()))
{
anyVar.data.objectData = objectData;
objectData = new ObjectData();
}
else if (variable.GetType() == typeof(Rigidbody2DVariable) && !rigidbody2DData.Equals(new Rigidbody2DData()))
{
anyVar.data.rigidbody2DData = rigidbody2DData;
rigidbody2DData = new Rigidbody2DData();
}
else if (variable.GetType() == typeof(SpriteVariable) && !spriteData.Equals(new SpriteData()))
{
anyVar.data.spriteData = spriteData;
spriteData = new SpriteData();
}
else if (variable.GetType() == typeof(TextureVariable) && !textureData.Equals(new TextureData()))
{
anyVar.data.textureData = textureData;
textureData = new TextureData();
}
else if (variable.GetType() == typeof(TransformVariable) && !transformData.Equals(new TransformData()))
{
anyVar.data.transformData = transformData;
transformData = new TransformData();
}
else if (variable.GetType() == typeof(Vector2Variable) && !vector2Data.Equals(new Vector2Data()))
{
anyVar.data.vector2Data = vector2Data;
vector2Data = new Vector2Data();
}
else if (variable.GetType() == typeof(Vector3Variable) && !vector3Data.Equals(new Vector3Data()))
{ {
anyVar.data.vector3Data = vector3Data; {
vector3Data = new Vector3Data(); anyVar.variable = variable;
}
if (variable.GetType() == typeof(BooleanVariable) && !booleanData.Equals(new BooleanData()))
{
anyVar.data.booleanData = booleanData;
booleanData = new BooleanData();
}
else if (variable.GetType() == typeof(IntegerVariable) && !integerData.Equals(new IntegerData()))
{
anyVar.data.integerData = integerData;
integerData = new IntegerData();
}
else if (variable.GetType() == typeof(FloatVariable) && !floatData.Equals(new FloatData()))
{
anyVar.data.floatData = floatData;
floatData = new FloatData();
}
else if (variable.GetType() == typeof(StringVariable) && !stringData.Equals(new StringDataMulti()))
{
anyVar.data.stringData.stringRef = stringData.stringRef;
anyVar.data.stringData.stringVal = stringData.stringVal;
stringData = new StringDataMulti();
}
else if (variable.GetType() == typeof(AnimatorVariable) && !animatorData.Equals(new AnimatorData()))
{
anyVar.data.animatorData = animatorData;
animatorData = new AnimatorData();
}
else if (variable.GetType() == typeof(AudioSourceVariable) && !audioSourceData.Equals(new AudioSourceData()))
{
anyVar.data.audioSourceData = audioSourceData;
audioSourceData = new AudioSourceData();
}
else if (variable.GetType() == typeof(ColorVariable) && !colorData.Equals(new ColorData()))
{
anyVar.data.colorData = colorData;
colorData = new ColorData();
}
else if (variable.GetType() == typeof(GameObjectVariable) && !gameObjectData.Equals(new GameObjectData()))
{
anyVar.data.gameObjectData = gameObjectData;
gameObjectData = new GameObjectData();
}
else if (variable.GetType() == typeof(MaterialVariable) && !materialData.Equals(new MaterialData()))
{
anyVar.data.materialData = materialData;
materialData = new MaterialData();
}
else if (variable.GetType() == typeof(ObjectVariable) && !objectData.Equals(new ObjectData()))
{
anyVar.data.objectData = objectData;
objectData = new ObjectData();
}
else if (variable.GetType() == typeof(Rigidbody2DVariable) && !rigidbody2DData.Equals(new Rigidbody2DData()))
{
anyVar.data.rigidbody2DData = rigidbody2DData;
rigidbody2DData = new Rigidbody2DData();
}
else if (variable.GetType() == typeof(SpriteVariable) && !spriteData.Equals(new SpriteData()))
{
anyVar.data.spriteData = spriteData;
spriteData = new SpriteData();
}
else if (variable.GetType() == typeof(TextureVariable) && !textureData.Equals(new TextureData()))
{
anyVar.data.textureData = textureData;
textureData = new TextureData();
}
else if (variable.GetType() == typeof(TransformVariable) && !transformData.Equals(new TransformData()))
{
anyVar.data.transformData = transformData;
transformData = new TransformData();
}
else if (variable.GetType() == typeof(Vector2Variable) && !vector2Data.Equals(new Vector2Data()))
{
anyVar.data.vector2Data = vector2Data;
vector2Data = new Vector2Data();
}
else if (variable.GetType() == typeof(Vector3Variable) && !vector3Data.Equals(new Vector3Data()))
{
anyVar.data.vector3Data = vector3Data;
vector3Data = new Vector3Data();
}
//moved to new anyvar storage, clear legacy.
variable = null;
} }
//moved to new anyvar storage, clear legacy. // just checking for anyVar != null fails here. is any var beig reintilaized somewhere?
variable = null;
if(anyVar != null && anyVar.variable != null){
conditionExpression c = new conditionExpression(compareOperator,anyVar);
if(!conditions.Contains(c)){
conditions.Add(c);
}
//added to list
anyVar = null;
//this is not nullabale?
//compareOperator = null;
}
} }
#endregion #endregion
} }
} }

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

@ -33,8 +33,10 @@ namespace Fungus.EditorUtils
new GUIContent(VariableUtil.GetCompareOperatorDescription(CompareOperator.NotEquals)), new GUIContent(VariableUtil.GetCompareOperatorDescription(CompareOperator.NotEquals)),
}; };
protected SerializedProperty compareOperatorProp; // protected SerializedProperty compareOperatorProp;
protected SerializedProperty anyVarProp; // protected SerializedProperty anyVarProp;
protected SerializedProperty conditions;
protected Dictionary<System.Type, SerializedProperty> propByVariableType; protected Dictionary<System.Type, SerializedProperty> propByVariableType;
@ -42,49 +44,67 @@ namespace Fungus.EditorUtils
{ {
base.OnEnable(); base.OnEnable();
compareOperatorProp = serializedObject.FindProperty("compareOperator"); // compareOperatorProp = serializedObject.FindProperty("compareOperator");
anyVarProp = serializedObject.FindProperty("anyVar"); // anyVarProp = serializedObject.FindProperty("anyVar");
conditions = serializedObject.FindProperty("conditions");
} }
public override void DrawCommandGUI() public override void DrawCommandGUI()
{ {
serializedObject.Update(); serializedObject.Update();
VariableCondition t = target as VariableCondition; EditorGUILayout.PropertyField(serializedObject.FindProperty("anyOrAllConditions"));
conditions.arraySize = EditorGUILayout.IntField("Size", conditions.arraySize);
var flowchart = (Flowchart)t.GetFlowchart(); for (int i = 0; i < conditions.arraySize; i++)
if (flowchart == null)
{ {
return;
}
EditorGUILayout.PropertyField(anyVarProp, true); VariableCondition t = target as VariableCondition;
// Get selected variable var flowchart = (Flowchart)t.GetFlowchart();
Variable selectedVariable = anyVarProp.FindPropertyRelative("variable").objectReferenceValue as Variable; if (flowchart == null)
GUIContent[] operatorsList = emptyList; {
if (selectedVariable != null) return;
{ }
operatorsList = selectedVariable.IsComparisonSupported() ? compareListAll : compareListEqualOnly;
} // EditorGUILayout.PropertyField(anyVarProp, true);
var conditionAnyVar = conditions.GetArrayElementAtIndex(i).FindPropertyRelative("anyVar");
var conditionCompare = conditions.GetArrayElementAtIndex(i).FindPropertyRelative("compareOperator");
EditorGUILayout.PropertyField(conditionAnyVar,new GUIContent("Variable"),true);
// Get selected variable
Variable selectedVariable = conditionAnyVar.FindPropertyRelative("variable").objectReferenceValue as Variable;
GUIContent[] operatorsList = emptyList;
if (selectedVariable != null)
{
operatorsList = selectedVariable.IsComparisonSupported() ? compareListAll : compareListEqualOnly;
}
// Get previously selected operator
int selectedIndex = (int)t.Conditions[i].CompareOperator;
if (selectedIndex < 0)
{
// 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);
if (selectedVariable != null)
{
conditionCompare.enumValueIndex = selectedIndex;
}
// Get previously selected operator
int selectedIndex = (int)t.CompareOperator;
if (selectedIndex < 0)
{
// 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);
if (selectedVariable != null)
{
compareOperatorProp.enumValueIndex = selectedIndex;
} }

Loading…
Cancel
Save