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;
#region Public members
public override void OnEnter()
{
if (ParentBlock == null)
@ -88,7 +86,6 @@ namespace Fungus
}
}
#endregion
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)
// 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.Text;
using System.Text;
using UnityEngine;
namespace Fungus
{
/// <summary>
/// class for a single condition. A list of this is used for multiple conditions.
/// </summary>
@ -20,35 +19,30 @@ namespace Fungus
public virtual AnyVariableAndDataPair AnyVar { get { return anyVar; } }
public virtual CompareOperator CompareOperator { get { return compareOperator; } }
public ConditionExpression(){}
public ConditionExpression(CompareOperator op, AnyVariableAndDataPair variablePair)
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
{
[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.")]
{
public enum AnyOrAll
{
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>();
/// <summary>
/// Called when the script is loaded or a value is changed in the
/// inspector (Called in the editor only).
@ -66,7 +60,7 @@ namespace Fungus
{
conditions.Add(new ConditionExpression());
}
}
}
protected override bool EvaluateCondition()
{
@ -76,10 +70,10 @@ namespace Fungus
}
bool resultAny = false, resultAll = true;
foreach (ConditionExpression condition in conditions)
foreach (ConditionExpression condition in conditions)
{
bool curResult = false;
if (condition.AnyVar == null)
if (condition.AnyVar == null)
{
resultAll &= curResult;
resultAny |= curResult;
@ -90,8 +84,8 @@ namespace Fungus
resultAny |= curResult;
}
if (anyOrAllConditions == AnyOrAllConditions.AnyOneTrue) return resultAny;
if (anyOrAllConditions == AnyOrAll.AnyOf_OR) return resultAny;
return resultAll;
}
@ -102,26 +96,16 @@ namespace Fungus
return false;
}
foreach (ConditionExpression condition in conditions)
foreach (ConditionExpression condition in conditions)
{
if (condition.AnyVar == null || condition.AnyVar.variable == null)
{
return false;
}
}
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()
{
if (!this.HasNeededProperties())
@ -130,7 +114,7 @@ namespace Fungus
}
string connector = "";
if (anyOrAllConditions == AnyOrAllConditions.AnyOneTrue)
if (anyOrAllConditions == AnyOrAll.AnyOf_OR)
{
connector = " <b>OR</b> ";
}
@ -139,11 +123,11 @@ namespace Fungus
connector = " <b>AND</b> ";
}
StringBuilder summary = new StringBuilder("");
for (int i = 0 ; i < conditions.Count; i++)
StringBuilder summary = new StringBuilder("");
for (int i = 0; i < conditions.Count; i++)
{
summary.Append(conditions[i].AnyVar.variable.Key + " " +
VariableUtil.GetCompareOperatorDescription(conditions[i].CompareOperator) + " " +
summary.Append(conditions[i].AnyVar.variable.Key + " " +
VariableUtil.GetCompareOperatorDescription(conditions[i].CompareOperator) + " " +
conditions[i].AnyVar.GetDataDescription());
if (i < conditions.Count - 1)
@ -159,17 +143,15 @@ namespace Fungus
return anyVar.HasReference(variable);
}
#endregion
#region backwards compat
[HideInInspector]
[SerializeField] protected CompareOperator compareOperator;
[HideInInspector]
[HideInInspector]
[SerializeField] protected AnyVariableAndDataPair anyVar;
[Tooltip("Variable to use in expression")]
[VariableProperty(AllVariableTypes.VariableAny.Any)]
[SerializeField] protected Variable variable;
@ -221,8 +203,8 @@ namespace Fungus
[Tooltip("Vector3 value to compare against")]
[SerializeField] protected Vector3Data vector3Data;
void ISerializationCallbackReceiver.OnBeforeSerialize()
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
}
@ -322,18 +304,17 @@ namespace Fungus
if (anyVar != null && anyVar.variable != null)
{
ConditionExpression c = new ConditionExpression(compareOperator,anyVar);
ConditionExpression c = new ConditionExpression(compareOperator, anyVar);
if (!conditions.Contains(c))
{
conditions.Add(c);
}
}
if (anyVar != null && anyVar.variable == null)
{
anyVar = null;
variable = null;
}
}
#endregion
#endregion backwards compat
}
}
}

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

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