From e8a19dbcc56be1b1f7e58a1632a987104bfd1123 Mon Sep 17 00:00:00 2001 From: Jorge Ramirez Date: Sun, 8 Apr 2018 18:44:09 -0400 Subject: [PATCH] Add comparison and setting functionality for the Material variable --- Assets/Fungus/Scripts/Commands/SetVariable.cs | 28 ++++++++++---- .../Scripts/Commands/VariableCondition.cs | 24 +++++++++--- .../Scripts/Editor/SetVariableEditor.cs | 3 +- .../Scripts/Editor/VariableConditionEditor.cs | 3 +- .../Scripts/VariableTypes/MaterialVariable.cs | 38 ++++++++++++++++++- 5 files changed, 81 insertions(+), 15 deletions(-) diff --git a/Assets/Fungus/Scripts/Commands/SetVariable.cs b/Assets/Fungus/Scripts/Commands/SetVariable.cs index a328c71f..dc48f645 100644 --- a/Assets/Fungus/Scripts/Commands/SetVariable.cs +++ b/Assets/Fungus/Scripts/Commands/SetVariable.cs @@ -9,21 +9,22 @@ namespace Fungus /// /// Sets a Boolean, Integer, Float or String variable to a new value using a simple arithmetic operation. The value can be a constant or reference another variable of the same type. /// - [CommandInfo("Variable", - "Set Variable", + [CommandInfo("Variable", + "Set Variable", "Sets a Boolean, Integer, Float or String variable to a new value using a simple arithmetic operation. The value can be a constant or reference another variable of the same type.")] [AddComponentMenu("")] - public class SetVariable : Command + public class SetVariable : Command { [Tooltip("The variable whos value will be set")] [VariableProperty(typeof(BooleanVariable), - typeof(IntegerVariable), - typeof(FloatVariable), + typeof(IntegerVariable), + typeof(FloatVariable), typeof(StringVariable), typeof(AnimatorVariable), typeof(AudioSourceVariable), typeof(ColorVariable), - typeof(GameObjectVariable))] + typeof(GameObjectVariable), + typeof(MaterialVariable))] [SerializeField] protected Variable variable; [Tooltip("The type of math operation to be performed")] @@ -53,6 +54,9 @@ namespace Fungus [Tooltip("GameObject value to set with")] [SerializeField] protected GameObjectData gameObjectData; + [Tooltip("Material value to set with")] + [SerializeField] protected MaterialData materialData; + protected virtual void DoSetOperation() { if (variable == null) @@ -101,6 +105,11 @@ namespace Fungus GameObjectVariable gameObjectVariable = (variable as GameObjectVariable); gameObjectVariable.Apply(setOperator, gameObjectData.Value); } + else if (variable.GetType() == typeof(MaterialVariable)) + { + MaterialVariable materialVariable = (variable as MaterialVariable); + materialVariable.Apply(setOperator, materialData.Value); + } } #region Public members @@ -113,7 +122,8 @@ namespace Fungus { typeof(AnimatorVariable), AnimatorVariable.setOperators }, { typeof(AudioSourceVariable), AudioSourceVariable.setOperators }, { typeof(ColorVariable), ColorVariable.setOperators }, - { typeof(GameObjectVariable), GameObjectVariable.setOperators } + { typeof(GameObjectVariable), GameObjectVariable.setOperators }, + { typeof(MaterialVariable), MaterialVariable.setOperators } }; /// @@ -192,6 +202,10 @@ namespace Fungus { description += gameObjectData.GetDescription(); } + else if (variable.GetType() == typeof(MaterialVariable)) + { + description += materialData.GetDescription(); + } return description; } diff --git a/Assets/Fungus/Scripts/Commands/VariableCondition.cs b/Assets/Fungus/Scripts/Commands/VariableCondition.cs index 58a04fd4..45904051 100644 --- a/Assets/Fungus/Scripts/Commands/VariableCondition.cs +++ b/Assets/Fungus/Scripts/Commands/VariableCondition.cs @@ -13,13 +13,14 @@ namespace Fungus [Tooltip("Variable to use in expression")] [VariableProperty(typeof(BooleanVariable), - typeof(IntegerVariable), - typeof(FloatVariable), + typeof(IntegerVariable), + typeof(FloatVariable), typeof(StringVariable), typeof(AnimatorVariable), typeof(AudioSourceVariable), typeof(ColorVariable), - typeof(GameObjectVariable))] + typeof(GameObjectVariable), + typeof(MaterialVariable))] [SerializeField] protected Variable variable; [Tooltip("Boolean value to compare against")] @@ -46,6 +47,9 @@ namespace Fungus [Tooltip("GameObject value to compare against")] [SerializeField] protected GameObjectData gameObjectData; + [Tooltip("Material value to compare against")] + [SerializeField] protected MaterialData materialData; + protected override bool EvaluateCondition() { if (variable == null) @@ -95,6 +99,11 @@ namespace Fungus GameObjectVariable gameObjectVariable = (variable as GameObjectVariable); condition = gameObjectVariable.Evaluate(compareOperator, gameObjectData.Value); } + else if (variable.GetType() == typeof(MaterialVariable)) + { + MaterialVariable materialVariable = (variable as MaterialVariable); + condition = materialVariable.Evaluate(compareOperator, materialData.Value); + } return condition; } @@ -114,7 +123,8 @@ namespace Fungus { typeof(AnimatorVariable), AnimatorVariable.compareOperators }, { typeof(AudioSourceVariable), AudioSourceVariable.compareOperators }, { typeof(ColorVariable), ColorVariable.compareOperators }, - { typeof(GameObjectVariable), GameObjectVariable.compareOperators } + { typeof(GameObjectVariable), GameObjectVariable.compareOperators }, + { typeof(MaterialVariable), MaterialVariable.compareOperators } }; /// @@ -164,6 +174,10 @@ namespace Fungus { summary += gameObjectData.GetDescription(); } + else if (variable.GetType() == typeof(MaterialVariable)) + { + summary += materialData.GetDescription(); + } return summary; } @@ -180,4 +194,4 @@ namespace Fungus #endregion } -} \ No newline at end of file +} diff --git a/Assets/Fungus/Scripts/Editor/SetVariableEditor.cs b/Assets/Fungus/Scripts/Editor/SetVariableEditor.cs index df8d3c19..f7dc590a 100644 --- a/Assets/Fungus/Scripts/Editor/SetVariableEditor.cs +++ b/Assets/Fungus/Scripts/Editor/SetVariableEditor.cs @@ -47,7 +47,8 @@ namespace Fungus.EditorUtils { typeof(AnimatorVariable), new VariablePropertyInfo("Animator", serializedObject.FindProperty("animatorData")) }, { typeof(AudioSourceVariable), new VariablePropertyInfo("AudioSource", serializedObject.FindProperty("audioSourceData")) }, { typeof(ColorVariable), new VariablePropertyInfo("Color", serializedObject.FindProperty("colorData")) }, - { typeof(GameObjectVariable), new VariablePropertyInfo("GameObject", serializedObject.FindProperty("gameObjectData")) } + { typeof(GameObjectVariable), new VariablePropertyInfo("GameObject", serializedObject.FindProperty("gameObjectData")) }, + { typeof(MaterialVariable), new VariablePropertyInfo("Material", serializedObject.FindProperty("materialData")) } }; } diff --git a/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs b/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs index e472978d..80324288 100644 --- a/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs +++ b/Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs @@ -32,7 +32,8 @@ namespace Fungus.EditorUtils { typeof(AnimatorVariable), serializedObject.FindProperty("animatorData") }, { typeof(AudioSourceVariable), serializedObject.FindProperty("audioSourceData") }, { typeof(ColorVariable), serializedObject.FindProperty("colorData") }, - { typeof(GameObjectVariable), serializedObject.FindProperty("gameObjectData") } + { typeof(GameObjectVariable), serializedObject.FindProperty("gameObjectData") }, + { typeof(MaterialVariable), serializedObject.FindProperty("materialData") } }; } diff --git a/Assets/Fungus/Scripts/VariableTypes/MaterialVariable.cs b/Assets/Fungus/Scripts/VariableTypes/MaterialVariable.cs index 5f36ff87..5617a655 100644 --- a/Assets/Fungus/Scripts/VariableTypes/MaterialVariable.cs +++ b/Assets/Fungus/Scripts/VariableTypes/MaterialVariable.cs @@ -13,7 +13,43 @@ namespace Fungus [AddComponentMenu("")] [System.Serializable] public class MaterialVariable : VariableBase - {} + { + public static readonly CompareOperator[] compareOperators = { CompareOperator.Equals, CompareOperator.NotEquals }; + public static readonly SetOperator[] setOperators = { SetOperator.Assign }; + + public virtual bool Evaluate(CompareOperator compareOperator, Material 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, Material value) + { + switch (setOperator) + { + case SetOperator.Assign: + Value = value; + break; + default: + Debug.LogError("The " + setOperator.ToString() + " set operator is not valid."); + break; + } + } + } /// /// Container for a Material variable reference or constant value.