From ca22eb792d39381a1e499ea33e5b77b8fc729f9e Mon Sep 17 00:00:00 2001 From: vjs22334 Date: Thu, 20 Feb 2020 18:21:04 +0530 Subject: [PATCH] basic support for multiple conditions added - WIP --- .../Scripts/Commands/VariableCondition.cs | 302 ++++++++++++------ .../Scripts/Editor/VariableConditionEditor.cs | 92 +++--- 2 files changed, 256 insertions(+), 138 deletions(-) diff --git a/Assets/Fungus/Scripts/Commands/VariableCondition.cs b/Assets/Fungus/Scripts/Commands/VariableCondition.cs index f99d784a..f3645602 100644 --- a/Assets/Fungus/Scripts/Commands/VariableCondition.cs +++ b/Assets/Fungus/Scripts/Commands/VariableCondition.cs @@ -6,29 +6,85 @@ using System.Collections.Generic; namespace Fungus { - public abstract class VariableCondition : Condition, ISerializationCallbackReceiver + + [System.Serializable] + public class conditionExpression { - [Tooltip("The type of comparison to be performed")] [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 + { - [SerializeField] protected AnyVariableAndDataPair anyVar = new AnyVariableAndDataPair(); + [Tooltip("The type of comparison to be performed")] + + [SerializeField] protected AnyOrAllConditions anyOrAllConditions; + + + [SerializeField] protected List conditions = new List(); protected override bool EvaluateCondition() { - if (anyVar.variable == null) + if (conditions == null || conditions.Count == 0) { return false; } - bool condition = false; - anyVar.Compare(compareOperator, ref condition); + bool resultAny = false, resultAll = true; + 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() { - 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 @@ -36,18 +92,40 @@ namespace Fungus /// /// The type of comparison operation to be performed. /// - public virtual CompareOperator CompareOperator { get { return compareOperator; } } + public virtual CompareOperator CompareOperator { get { return conditions[0].CompareOperator; } } + + public virtual List Conditions { get { return conditions; } } public override string GetSummary() { - if (anyVar.variable == null) + if (!this.HasNeededProperties()) { return "Error: No variable selected"; } - string summary = anyVar.variable.Key + " "; - summary += VariableUtil.GetCompareOperatorDescription(compareOperator) + " "; - summary += anyVar.GetDataDescription(); + string summary = ""; + string connector = ""; + 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 propByVariableType; @@ -42,52 +44,70 @@ namespace Fungus.EditorUtils { base.OnEnable(); - compareOperatorProp = serializedObject.FindProperty("compareOperator"); - anyVarProp = serializedObject.FindProperty("anyVar"); + // compareOperatorProp = serializedObject.FindProperty("compareOperator"); + // anyVarProp = serializedObject.FindProperty("anyVar"); + + conditions = serializedObject.FindProperty("conditions"); + } public override void DrawCommandGUI() { serializedObject.Update(); - VariableCondition t = target as VariableCondition; - - var flowchart = (Flowchart)t.GetFlowchart(); - if (flowchart == null) - { - return; - } + EditorGUILayout.PropertyField(serializedObject.FindProperty("anyOrAllConditions")); - EditorGUILayout.PropertyField(anyVarProp, true); + conditions.arraySize = EditorGUILayout.IntField("Size", conditions.arraySize); - // Get selected variable - Variable selectedVariable = anyVarProp.FindPropertyRelative("variable").objectReferenceValue as Variable; - GUIContent[] operatorsList = emptyList; - if (selectedVariable != null) + for (int i = 0; i < conditions.arraySize; i++) { - operatorsList = selectedVariable.IsComparisonSupported() ? compareListAll : compareListEqualOnly; - } - - // 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; + VariableCondition t = target as VariableCondition; + + var flowchart = (Flowchart)t.GetFlowchart(); + if (flowchart == null) + { + return; + } + + // 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; + } + + + } - + serializedObject.ApplyModifiedProperties(); } }