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. 53
      Assets/Fungus/Scripts/Commands/VariableCondition.cs
  3. 45
      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()
{

53
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 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
{
public enum AnyOrAll
{
AnyOf_OR,//Use as a chain of ORs
AllOf_AND,//Use as a chain of ANDs
}
[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.")]
[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).
@ -90,7 +84,7 @@ namespace Fungus
resultAny |= curResult;
}
if (anyOrAllConditions == AnyOrAllConditions.AnyOneTrue) return resultAny;
if (anyOrAllConditions == AnyOrAll.AnyOf_OR) return resultAny;
return resultAll;
}
@ -108,20 +102,10 @@ namespace Fungus
{
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> ";
}
@ -159,7 +143,6 @@ namespace Fungus
return anyVar.HasReference(variable);
}
#endregion
#region backwards compat
@ -169,7 +152,6 @@ namespace Fungus
[HideInInspector]
[SerializeField] protected AnyVariableAndDataPair anyVar;
[Tooltip("Variable to use in expression")]
[VariableProperty(AllVariableTypes.VariableAny.Any)]
[SerializeField] protected Variable variable;
@ -327,13 +309,12 @@ namespace Fungus
{
conditions.Add(c);
}
}
if (anyVar != null && anyVar.variable == null)
{
anyVar = null;
variable = null;
}
}
#endregion
#endregion backwards compat
}
}

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

@ -3,10 +3,14 @@
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
namespace Fungus.EditorUtils
{
/// <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
{
@ -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,29 +31,19 @@ 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;
public override void OnEnable()
{
base.OnEnable();
// compareOperatorProp = serializedObject.FindProperty("compareOperator");
// anyVarProp = serializedObject.FindProperty("anyVar");
conditions = serializedObject.FindProperty("conditions");
}
public override void DrawCommandGUI()
@ -61,9 +55,6 @@ namespace Fungus.EditorUtils
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;
var flowchart = (Flowchart)t.GetFlowchart();
@ -72,7 +63,9 @@ namespace Fungus.EditorUtils
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");
@ -80,15 +73,16 @@ namespace Fungus.EditorUtils
// 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;
}
// 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,14 +94,11 @@ namespace Fungus.EditorUtils
selectedIndex,
operatorsList);
if (selectedVariable != null)
{
conditionCompare.enumValueIndex = selectedIndex;
}
EditorGUI.indentLevel--;
GUILayout.Space(10f);
EditorGUILayout.Separator();
}
EditorGUI.indentLevel--;
serializedObject.ApplyModifiedProperties();
}
}

Loading…
Cancel
Save