From 27b97990d653431c8a30652c120141a29ccf33b1 Mon Sep 17 00:00:00 2001 From: Jorge Ramirez Date: Sun, 8 Apr 2018 19:50:54 -0400 Subject: [PATCH] Add comparison and setting functionality for the Sprite variable --- Assets/Fungus/Scripts/Commands/SetVariable.cs | 18 ++++++++- .../Scripts/Commands/VariableCondition.cs | 18 ++++++++- .../Scripts/Editor/SetVariableEditor.cs | 3 +- .../Scripts/Editor/VariableConditionEditor.cs | 3 +- .../Scripts/VariableTypes/SpriteVariable.cs | 38 ++++++++++++++++++- 5 files changed, 73 insertions(+), 7 deletions(-) diff --git a/Assets/Fungus/Scripts/Commands/SetVariable.cs b/Assets/Fungus/Scripts/Commands/SetVariable.cs index 87e7615a..dc89bee9 100644 --- a/Assets/Fungus/Scripts/Commands/SetVariable.cs +++ b/Assets/Fungus/Scripts/Commands/SetVariable.cs @@ -26,7 +26,8 @@ namespace Fungus typeof(GameObjectVariable), typeof(MaterialVariable), typeof(ObjectVariable), - typeof(Rigidbody2DVariable))] + typeof(Rigidbody2DVariable), + typeof(SpriteVariable))] [SerializeField] protected Variable variable; [Tooltip("The type of math operation to be performed")] @@ -65,6 +66,9 @@ namespace Fungus [Tooltip("Rigidbody2D value to set with")] [SerializeField] protected Rigidbody2DData rigidbody2DData; + [Tooltip("Sprite value to set with")] + [SerializeField] protected SpriteData spriteData; + protected virtual void DoSetOperation() { if (variable == null) @@ -128,6 +132,11 @@ namespace Fungus Rigidbody2DVariable rigidbody2DVariable = (variable as Rigidbody2DVariable); rigidbody2DVariable.Apply(setOperator, rigidbody2DData.Value); } + else if (variable.GetType() == typeof(SpriteVariable)) + { + SpriteVariable spriteVariable = (variable as SpriteVariable); + spriteVariable.Apply(setOperator, spriteData.Value); + } } #region Public members @@ -143,7 +152,8 @@ namespace Fungus { typeof(GameObjectVariable), GameObjectVariable.setOperators }, { typeof(MaterialVariable), MaterialVariable.setOperators }, { typeof(ObjectVariable), ObjectVariable.setOperators }, - { typeof(Rigidbody2DVariable), Rigidbody2DVariable.setOperators } + { typeof(Rigidbody2DVariable), Rigidbody2DVariable.setOperators }, + { typeof(SpriteVariable), SpriteVariable.setOperators } }; /// @@ -234,6 +244,10 @@ namespace Fungus { description += rigidbody2DData.GetDescription(); } + else if (variable.GetType() == typeof(SpriteVariable)) + { + description += spriteData.GetDescription(); + } return description; } diff --git a/Assets/Fungus/Scripts/Commands/VariableCondition.cs b/Assets/Fungus/Scripts/Commands/VariableCondition.cs index a229d7c7..64acafba 100644 --- a/Assets/Fungus/Scripts/Commands/VariableCondition.cs +++ b/Assets/Fungus/Scripts/Commands/VariableCondition.cs @@ -22,7 +22,8 @@ namespace Fungus typeof(GameObjectVariable), typeof(MaterialVariable), typeof(ObjectVariable), - typeof(Rigidbody2DVariable))] + typeof(Rigidbody2DVariable), + typeof(SpriteVariable))] [SerializeField] protected Variable variable; [Tooltip("Boolean value to compare against")] @@ -58,6 +59,9 @@ namespace Fungus [Tooltip("Rigidbody2D value to compare against")] [SerializeField] protected Rigidbody2DData rigidbody2DData; + [Tooltip("Sprite value to compare against")] + [SerializeField] protected SpriteData spriteData; + protected override bool EvaluateCondition() { if (variable == null) @@ -122,6 +126,11 @@ namespace Fungus Rigidbody2DVariable rigidbody2DVariable = (variable as Rigidbody2DVariable); condition = rigidbody2DVariable.Evaluate(compareOperator, rigidbody2DData.Value); } + else if (variable.GetType() == typeof(SpriteVariable)) + { + SpriteVariable spriteVariable = (variable as SpriteVariable); + condition = spriteVariable.Evaluate(compareOperator, spriteData.Value); + } return condition; } @@ -144,7 +153,8 @@ namespace Fungus { typeof(GameObjectVariable), GameObjectVariable.compareOperators }, { typeof(MaterialVariable), MaterialVariable.compareOperators }, { typeof(ObjectVariable), ObjectVariable.compareOperators }, - { typeof(Rigidbody2DVariable), Rigidbody2DVariable.compareOperators } + { typeof(Rigidbody2DVariable), Rigidbody2DVariable.compareOperators }, + { typeof(SpriteVariable), SpriteVariable.compareOperators } }; /// @@ -206,6 +216,10 @@ namespace Fungus { summary += rigidbody2DData.GetDescription(); } + else if (variable.GetType() == typeof(SpriteVariable)) + { + summary += spriteData.GetDescription(); + } return summary; } diff --git a/Assets/Fungus/Scripts/Editor/SetVariableEditor.cs b/Assets/Fungus/Scripts/Editor/SetVariableEditor.cs index c90d9596..ae81d38f 100644 --- a/Assets/Fungus/Scripts/Editor/SetVariableEditor.cs +++ b/Assets/Fungus/Scripts/Editor/SetVariableEditor.cs @@ -50,7 +50,8 @@ namespace Fungus.EditorUtils { typeof(GameObjectVariable), new VariablePropertyInfo("GameObject", serializedObject.FindProperty("gameObjectData")) }, { typeof(MaterialVariable), new VariablePropertyInfo("Material", serializedObject.FindProperty("materialData")) }, { typeof(ObjectVariable), new VariablePropertyInfo("Object", serializedObject.FindProperty("objectData")) }, - { typeof(Rigidbody2DVariable), new VariablePropertyInfo("Rigidbody2D", serializedObject.FindProperty("rigidbody2DData")) } + { typeof(Rigidbody2DVariable), new VariablePropertyInfo("Rigidbody2D", serializedObject.FindProperty("rigidbody2DData")) }, + { typeof(SpriteVariable), new VariablePropertyInfo("Sprite", serializedObject.FindProperty("spriteData")) } }; } diff --git a/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs b/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs index 56558ff6..ed588974 100644 --- a/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs +++ b/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs @@ -35,7 +35,8 @@ namespace Fungus.EditorUtils { typeof(GameObjectVariable), serializedObject.FindProperty("gameObjectData") }, { typeof(MaterialVariable), serializedObject.FindProperty("materialData") }, { typeof(ObjectVariable), serializedObject.FindProperty("objectData") }, - { typeof(Rigidbody2DVariable), serializedObject.FindProperty("rigidbody2DData") } + { typeof(Rigidbody2DVariable), serializedObject.FindProperty("rigidbody2DData") }, + { typeof(SpriteVariable), serializedObject.FindProperty("spriteData") } }; } diff --git a/Assets/Fungus/Scripts/VariableTypes/SpriteVariable.cs b/Assets/Fungus/Scripts/VariableTypes/SpriteVariable.cs index ca7a0d63..fb59cc47 100644 --- a/Assets/Fungus/Scripts/VariableTypes/SpriteVariable.cs +++ b/Assets/Fungus/Scripts/VariableTypes/SpriteVariable.cs @@ -13,7 +13,43 @@ namespace Fungus [AddComponentMenu("")] [System.Serializable] public class SpriteVariable : VariableBase - {} + { + public static readonly CompareOperator[] compareOperators = { CompareOperator.Equals, CompareOperator.NotEquals }; + public static readonly SetOperator[] setOperators = { SetOperator.Assign }; + + public virtual bool Evaluate(CompareOperator compareOperator, Sprite value) + { + bool condition = false; + + switch (compareOperator) + { + case CompareOperator.Equals: + condition = Value == value; + break; + case CompareOperator.NotEquals: + condition = Value != value; + break; + default: + Debug.LogError("The " + compareOperator.ToString() + " comparison operator is not valid."); + break; + } + + return condition; + } + + public override void Apply(SetOperator setOperator, Sprite value) + { + switch (setOperator) + { + case SetOperator.Assign: + Value = value; + break; + default: + Debug.LogError("The " + setOperator.ToString() + " set operator is not valid."); + break; + } + } + } /// /// Container for a Sprite variable reference or constant value.