Browse Source

Correct VariableCondition formatting

Also VariableConditionEditor
master
Steve Halliwell 5 years ago
parent
commit
9d77cd93ea
  1. 3
      Assets/Fungus/Scripts/Commands/Condition.cs
  2. 89
      Assets/Fungus/Scripts/Commands/VariableCondition.cs
  3. 79
      Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs

3
Assets/Fungus/Scripts/Commands/Condition.cs

@ -13,8 +13,6 @@ namespace Fungus
{ {
protected End endCommand; protected End endCommand;
#region Public members
public override void OnEnter() public override void OnEnter()
{ {
if (ParentBlock == null) if (ParentBlock == null)
@ -88,7 +86,6 @@ namespace Fungus
} }
} }
#endregion
protected End FindOurEndCommand() protected End FindOurEndCommand()
{ {

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

@ -1,13 +1,12 @@
// This code is part of the Fungus library (https://github.com/snozbot/fungus) // This code is part of the Fungus library (https://github.com/snozbot/fungus)
// 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 System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using UnityEngine;
namespace Fungus namespace Fungus
{ {
/// <summary> /// <summary>
/// class for a single condition. A list of this is used for multiple conditions. /// class for a single condition. A list of this is used for multiple conditions.
/// </summary> /// </summary>
@ -20,35 +19,30 @@ namespace Fungus
public virtual AnyVariableAndDataPair AnyVar { get { return anyVar; } } public virtual AnyVariableAndDataPair AnyVar { get { return anyVar; } }
public virtual CompareOperator CompareOperator { get { return compareOperator; } } public virtual CompareOperator CompareOperator { get { return compareOperator; } }
public ConditionExpression(){} public ConditionExpression()
public ConditionExpression(CompareOperator op, AnyVariableAndDataPair variablePair)
{ {
}
public ConditionExpression(CompareOperator op, AnyVariableAndDataPair variablePair)
{
compareOperator = op; compareOperator = op;
anyVar = variablePair; 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
{ {
public enum AnyOrAll
[Tooltip("Selecting \"Any One True\" will result in true if atleast one of the conditions is true. Selecting \"All True\" will result in true only when all the conditions are true.")] {
AnyOf_OR,//Use as a chain of ORs
AllOf_AND,//Use as a chain of ANDs
}
[SerializeField] protected AnyOrAllConditions anyOrAllConditions; [Tooltip("Selecting AnyOf will result in true if at least one of the conditions is true. Selecting AllOF will result in true only when all the conditions are true.")]
[SerializeField] protected AnyOrAll anyOrAllConditions;
[SerializeField] protected List<ConditionExpression> conditions = new List<ConditionExpression>(); [SerializeField] protected List<ConditionExpression> conditions = new List<ConditionExpression>();
/// <summary> /// <summary>
/// Called when the script is loaded or a value is changed in the /// Called when the script is loaded or a value is changed in the
/// inspector (Called in the editor only). /// inspector (Called in the editor only).
@ -66,7 +60,7 @@ namespace Fungus
{ {
conditions.Add(new ConditionExpression()); conditions.Add(new ConditionExpression());
} }
} }
protected override bool EvaluateCondition() protected override bool EvaluateCondition()
{ {
@ -76,10 +70,10 @@ namespace Fungus
} }
bool resultAny = false, resultAll = true; bool resultAny = false, resultAll = true;
foreach (ConditionExpression condition in conditions) foreach (ConditionExpression condition in conditions)
{ {
bool curResult = false; bool curResult = false;
if (condition.AnyVar == null) if (condition.AnyVar == null)
{ {
resultAll &= curResult; resultAll &= curResult;
resultAny |= curResult; resultAny |= curResult;
@ -90,8 +84,8 @@ namespace Fungus
resultAny |= curResult; resultAny |= curResult;
} }
if (anyOrAllConditions == AnyOrAllConditions.AnyOneTrue) return resultAny; if (anyOrAllConditions == AnyOrAll.AnyOf_OR) return resultAny;
return resultAll; return resultAll;
} }
@ -102,26 +96,16 @@ namespace Fungus
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;
} }
} }
return true; return true;
} }
#region Public members
/// <summary>
/// The type of comparison operation to be performed.
/// </summary>
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 (!this.HasNeededProperties()) if (!this.HasNeededProperties())
@ -130,7 +114,7 @@ namespace Fungus
} }
string connector = ""; string connector = "";
if (anyOrAllConditions == AnyOrAllConditions.AnyOneTrue) if (anyOrAllConditions == AnyOrAll.AnyOf_OR)
{ {
connector = " <b>OR</b> "; connector = " <b>OR</b> ";
} }
@ -139,11 +123,11 @@ namespace Fungus
connector = " <b>AND</b> "; connector = " <b>AND</b> ";
} }
StringBuilder summary = new StringBuilder(""); StringBuilder summary = new StringBuilder("");
for (int i = 0 ; i < conditions.Count; i++) for (int i = 0; i < conditions.Count; i++)
{ {
summary.Append(conditions[i].AnyVar.variable.Key + " " + summary.Append(conditions[i].AnyVar.variable.Key + " " +
VariableUtil.GetCompareOperatorDescription(conditions[i].CompareOperator) + " " + VariableUtil.GetCompareOperatorDescription(conditions[i].CompareOperator) + " " +
conditions[i].AnyVar.GetDataDescription()); conditions[i].AnyVar.GetDataDescription());
if (i < conditions.Count - 1) if (i < conditions.Count - 1)
@ -159,17 +143,15 @@ namespace Fungus
return anyVar.HasReference(variable); return anyVar.HasReference(variable);
} }
#endregion
#region backwards compat #region backwards compat
[HideInInspector] [HideInInspector]
[SerializeField] protected CompareOperator compareOperator; [SerializeField] protected CompareOperator compareOperator;
[HideInInspector] [HideInInspector]
[SerializeField] protected AnyVariableAndDataPair anyVar; [SerializeField] protected AnyVariableAndDataPair anyVar;
[Tooltip("Variable to use in expression")] [Tooltip("Variable to use in expression")]
[VariableProperty(AllVariableTypes.VariableAny.Any)] [VariableProperty(AllVariableTypes.VariableAny.Any)]
[SerializeField] protected Variable variable; [SerializeField] protected Variable variable;
@ -221,8 +203,8 @@ namespace Fungus
[Tooltip("Vector3 value to compare against")] [Tooltip("Vector3 value to compare against")]
[SerializeField] protected Vector3Data vector3Data; [SerializeField] protected Vector3Data vector3Data;
void ISerializationCallbackReceiver.OnBeforeSerialize() void ISerializationCallbackReceiver.OnBeforeSerialize()
{ {
} }
@ -322,18 +304,17 @@ namespace Fungus
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);
} }
}
if (anyVar != null && anyVar.variable == null)
{
anyVar = null; anyVar = null;
variable = null;
} }
} }
#endregion
#endregion backwards compat
} }
} }

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

@ -3,11 +3,15 @@
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
using System.Collections.Generic;
namespace Fungus.EditorUtils namespace Fungus.EditorUtils
{ {
[CustomEditor (typeof(VariableCondition), true)] /// <summary>
/// Handles custom drawing for ConditionExperssions within the VariableCondition and inherited commands.
///
/// TODO; refactor to allow a propertydrawer on ConditionExperssion and potentially list as reorderable
/// </summary>
[CustomEditor(typeof(VariableCondition), true)]
public class VariableConditionEditor : CommandEditor public class VariableConditionEditor : CommandEditor
{ {
public static readonly GUIContent None = new GUIContent("<None>"); public static readonly GUIContent None = new GUIContent("<None>");
@ -17,7 +21,7 @@ namespace Fungus.EditorUtils
None, None,
}; };
static readonly GUIContent[] compareListAll = new GUIContent[] private static readonly GUIContent[] compareListAll = new GUIContent[]
{ {
new GUIContent(VariableUtil.GetCompareOperatorDescription(CompareOperator.Equals)), new GUIContent(VariableUtil.GetCompareOperatorDescription(CompareOperator.Equals)),
new GUIContent(VariableUtil.GetCompareOperatorDescription(CompareOperator.NotEquals)), new GUIContent(VariableUtil.GetCompareOperatorDescription(CompareOperator.NotEquals)),
@ -27,68 +31,58 @@ namespace Fungus.EditorUtils
new GUIContent(VariableUtil.GetCompareOperatorDescription(CompareOperator.GreaterThanOrEquals)), new GUIContent(VariableUtil.GetCompareOperatorDescription(CompareOperator.GreaterThanOrEquals)),
}; };
static readonly GUIContent[] compareListEqualOnly = new GUIContent[] private static readonly GUIContent[] compareListEqualOnly = new GUIContent[]
{ {
new GUIContent(VariableUtil.GetCompareOperatorDescription(CompareOperator.Equals)), new GUIContent(VariableUtil.GetCompareOperatorDescription(CompareOperator.Equals)),
new GUIContent(VariableUtil.GetCompareOperatorDescription(CompareOperator.NotEquals)), new GUIContent(VariableUtil.GetCompareOperatorDescription(CompareOperator.NotEquals)),
}; };
// protected SerializedProperty compareOperatorProp; protected SerializedProperty conditions;
// protected SerializedProperty anyVarProp;
protected SerializedProperty conditions;
protected Dictionary<System.Type, SerializedProperty> propByVariableType;
public override void OnEnable() public override void OnEnable()
{ {
base.OnEnable(); base.OnEnable();
// compareOperatorProp = serializedObject.FindProperty("compareOperator");
// anyVarProp = serializedObject.FindProperty("anyVar");
conditions = serializedObject.FindProperty("conditions"); conditions = serializedObject.FindProperty("conditions");
} }
public override void DrawCommandGUI() public override void DrawCommandGUI()
{ {
serializedObject.Update(); serializedObject.Update();
EditorGUILayout.PropertyField(serializedObject.FindProperty("anyOrAllConditions")); EditorGUILayout.PropertyField(serializedObject.FindProperty("anyOrAllConditions"));
conditions.arraySize = EditorGUILayout.IntField("Size", conditions.arraySize); conditions.arraySize = EditorGUILayout.IntField("Size", conditions.arraySize);
GUILayout.Label("Conditions",EditorStyles.boldLabel); GUILayout.Label("Conditions", EditorStyles.boldLabel);
for (int i = 0; i < conditions.arraySize; i++)
{
EditorGUI.indentLevel++;
VariableCondition t = target as VariableCondition;
var flowchart = (Flowchart)t.GetFlowchart(); VariableCondition t = target as VariableCondition;
if (flowchart == null)
{ var flowchart = (Flowchart)t.GetFlowchart();
return; if (flowchart == null)
} {
return;
}
// EditorGUILayout.PropertyField(anyVarProp, true); EditorGUI.indentLevel++;
for (int i = 0; i < conditions.arraySize; i++)
{
var conditionAnyVar = conditions.GetArrayElementAtIndex(i).FindPropertyRelative("anyVar"); var conditionAnyVar = conditions.GetArrayElementAtIndex(i).FindPropertyRelative("anyVar");
var conditionCompare = conditions.GetArrayElementAtIndex(i).FindPropertyRelative("compareOperator"); var conditionCompare = conditions.GetArrayElementAtIndex(i).FindPropertyRelative("compareOperator");
EditorGUILayout.PropertyField(conditionAnyVar,new GUIContent("Variable"),true); EditorGUILayout.PropertyField(conditionAnyVar, new GUIContent("Variable"), true);
// Get selected variable // Get selected variable
Variable selectedVariable = conditionAnyVar.FindPropertyRelative("variable").objectReferenceValue as Variable; Variable selectedVariable = conditionAnyVar.FindPropertyRelative("variable").objectReferenceValue as Variable;
if (selectedVariable == null)
continue;
GUIContent[] operatorsList = emptyList; GUIContent[] operatorsList = emptyList;
if (selectedVariable != null) operatorsList = selectedVariable.IsComparisonSupported() ? compareListAll : compareListEqualOnly;
{
operatorsList = selectedVariable.IsComparisonSupported() ? compareListAll : compareListEqualOnly;
}
// Get previously selected operator // Get previously selected operator
int selectedIndex = (int)t.Conditions[i].CompareOperator; int selectedIndex = conditionCompare.enumValueIndex;
if (selectedIndex < 0) if (selectedIndex < 0 || selectedIndex >= operatorsList.Length)
{ {
// Default to first index if the operator is not found in the available operators list // Default to first index if the operator is not found in the available operators list
// This can occur when changing between variable types // This can occur when changing between variable types
@ -100,15 +94,12 @@ namespace Fungus.EditorUtils
selectedIndex, selectedIndex,
operatorsList); operatorsList);
if (selectedVariable != null) conditionCompare.enumValueIndex = selectedIndex;
{
conditionCompare.enumValueIndex = selectedIndex;
}
EditorGUI.indentLevel--; EditorGUILayout.Separator();
GUILayout.Space(10f);
} }
EditorGUI.indentLevel--;
serializedObject.ApplyModifiedProperties(); serializedObject.ApplyModifiedProperties();
} }
} }
} }
Loading…
Cancel
Save