Browse Source

validation for empty conditions and improved UI

master
vjs22334 5 years ago
parent
commit
1736ff1393
  1. 72
      Assets/Fungus/Scripts/Commands/VariableCondition.cs
  2. 13
      Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs
  3. 8
      Assets/FungusExamples/Conditions.meta
  4. 1436
      Assets/FungusExamples/Conditions/MultipleConditionsExample.unity
  5. 7
      Assets/FungusExamples/Conditions/MultipleConditionsExample.unity.meta

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

@ -27,6 +27,7 @@ namespace Fungus
} }
// anyone with a better name for this can update it // anyone with a better name for this can update it
public enum AnyOrAllConditions public enum AnyOrAllConditions
{ {
@ -43,6 +44,26 @@ namespace Fungus
[SerializeField] protected List<conditionExpression> conditions = new List<conditionExpression>(); [SerializeField] protected List<conditionExpression> conditions = new List<conditionExpression>();
/// <summary>
/// Called when the script is loaded or a value is changed in the
/// inspector (Called in the editor only).
/// </summary>
public override void OnValidate()
{
base.OnValidate();
if (conditions == null)
{
conditions = new List<conditionExpression>();
}
if (conditions.Count == 0)
{
conditions.Add(new conditionExpression());
}
}
protected override bool EvaluateCondition() protected override bool EvaluateCondition()
{ {
if (conditions == null || conditions.Count == 0) if (conditions == null || conditions.Count == 0)
@ -61,7 +82,6 @@ namespace Fungus
continue; continue;
} }
condition.AnyVar.Compare(condition.CompareOperator, ref curResult); condition.AnyVar.Compare(condition.CompareOperator, ref curResult);
Debug.Log("res : " + curResult.ToString());
resultAll &= curResult; resultAll &= curResult;
resultAny |= curResult; resultAny |= curResult;
} }
@ -73,13 +93,15 @@ namespace Fungus
protected override bool HasNeededProperties() protected override bool HasNeededProperties()
{ {
if( conditions == null || conditions.Count == 0){ if (conditions == null || conditions.Count == 0)
{
return false; return false;
} }
foreach (conditionExpression condition in conditions) foreach (conditionExpression condition in conditions)
{ {
if(condition.AnyVar == null || condition.AnyVar.variable == null){ if (condition.AnyVar == null || condition.AnyVar.variable == null)
{
return false; return false;
} }
@ -105,28 +127,26 @@ namespace Fungus
string summary = ""; string summary = "";
string connector = ""; string connector = "";
if(anyOrAllConditions == AnyOrAllConditions.AnyOneTrue){ if (anyOrAllConditions == AnyOrAllConditions.AnyOneTrue)
{
connector = " Or "; connector = " Or ";
} }
else{ else
{
connector = " And "; connector = " And ";
} }
for(int i = 0 ; i < conditions.Count; i++) for (int i = 0 ; i < conditions.Count; i++)
{ {
summary += conditions[i].AnyVar.variable.Key + " "; summary += conditions[i].AnyVar.variable.Key + " ";
summary += VariableUtil.GetCompareOperatorDescription(conditions[i].CompareOperator) + " "; summary += VariableUtil.GetCompareOperatorDescription(conditions[i].CompareOperator) + " ";
summary += conditions[i].AnyVar.GetDataDescription(); summary += conditions[i].AnyVar.GetDataDescription();
if(i<conditions.Count-1){ if (i < conditions.Count - 1)
{
summary += connector; summary += connector;
} }
} }
return summary; return summary;
} }
@ -139,8 +159,10 @@ namespace Fungus
#region backwards compat #region backwards compat
[HideInInspector]
[SerializeField] protected CompareOperator compareOperator; [SerializeField] protected CompareOperator compareOperator;
[HideInInspector]
[SerializeField] protected AnyVariableAndDataPair anyVar; [SerializeField] protected AnyVariableAndDataPair anyVar;
@ -202,13 +224,9 @@ namespace Fungus
void ISerializationCallbackReceiver.OnAfterDeserialize() void ISerializationCallbackReceiver.OnAfterDeserialize()
{ {
if (variable != null)
if( variable != null)
{ {
{ anyVar.variable = variable;
anyVar.variable = variable;
}
if (variable.GetType() == typeof(BooleanVariable) && !booleanData.Equals(new BooleanData())) if (variable.GetType() == typeof(BooleanVariable) && !booleanData.Equals(new BooleanData()))
{ {
@ -296,22 +314,22 @@ namespace Fungus
variable = null; variable = null;
} }
// just checking for anyVar != null fails here. is any var beig reintilaized somewhere? // just checking for anyVar != null fails here. is any var being reintilaized somewhere?
if(anyVar != null && anyVar.variable != null){ if (anyVar != null && anyVar.variable != null)
{
conditionExpression c = new conditionExpression(compareOperator,anyVar); conditionExpression c = new conditionExpression(compareOperator,anyVar);
if(!conditions.Contains(c)){ if (!conditions.Contains(c))
{
conditions.Add(c); conditions.Add(c);
} }
//added to list }
anyVar = null;
//this is not nullabale? if (anyVar != null && anyVar.variable == null)
//compareOperator = null; {
anyVar = null;
} }
} }
#endregion #endregion
} }
} }

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

@ -49,6 +49,7 @@ namespace Fungus.EditorUtils
conditions = serializedObject.FindProperty("conditions"); conditions = serializedObject.FindProperty("conditions");
} }
public override void DrawCommandGUI() public override void DrawCommandGUI()
@ -62,6 +63,12 @@ namespace Fungus.EditorUtils
for (int i = 0; i < conditions.arraySize; i++) for (int i = 0; i < conditions.arraySize; i++)
{ {
GUILayout.Label("Condition "+(i+1).ToString(),EditorStyles.boldLabel);
//uncomment this for indentation
// EditorGUI.indentLevel++;
VariableCondition t = target as VariableCondition; VariableCondition t = target as VariableCondition;
var flowchart = (Flowchart)t.GetFlowchart(); var flowchart = (Flowchart)t.GetFlowchart();
@ -103,11 +110,13 @@ namespace Fungus.EditorUtils
conditionCompare.enumValueIndex = selectedIndex; conditionCompare.enumValueIndex = selectedIndex;
} }
//uncomment this for indentation
// EditorGUI.indentLevel--;
GUILayout.Space(10f);
}
}
serializedObject.ApplyModifiedProperties(); serializedObject.ApplyModifiedProperties();
} }
} }

8
Assets/FungusExamples/Conditions.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ab073c9a6f825fe4b82cbe37c37addb0
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

1436
Assets/FungusExamples/Conditions/MultipleConditionsExample.unity

File diff suppressed because it is too large Load Diff

7
Assets/FungusExamples/Conditions/MultipleConditionsExample.unity.meta

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 0d9b345eec71ebc46bfb14d1a3b8af20
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
Loading…
Cancel
Save