Browse Source

Add comparison and setting functionality for the Color variable

master
Jorge Ramirez 7 years ago
parent
commit
acfba940a9
  1. 14
      Assets/Fungus/Scripts/Commands/SetVariable.cs
  2. 14
      Assets/Fungus/Scripts/Commands/VariableCondition.cs
  3. 7
      Assets/Fungus/Scripts/Editor/SetVariableEditor.cs
  4. 3
      Assets/Fungus/Scripts/Editor/VariableConditionEditor.cs
  5. 42
      Assets/Fungus/Scripts/VariableTypes/ColorVariable.cs

14
Assets/Fungus/Scripts/Commands/SetVariable.cs

@ -22,6 +22,7 @@ namespace Fungus
typeof(StringVariable), typeof(StringVariable),
typeof(AnimatorVariable), typeof(AnimatorVariable),
typeof(AudioSourceVariable), typeof(AudioSourceVariable),
typeof(ColorVariable),
typeof(GameObjectVariable))] typeof(GameObjectVariable))]
[SerializeField] protected Variable variable; [SerializeField] protected Variable variable;
@ -46,6 +47,9 @@ namespace Fungus
[Tooltip("AudioSource value to set with")] [Tooltip("AudioSource value to set with")]
[SerializeField] protected AudioSourceData audioSourceData; [SerializeField] protected AudioSourceData audioSourceData;
[Tooltip("Color value to set with")]
[SerializeField] protected ColorData colorData;
[Tooltip("GameObject value to set with")] [Tooltip("GameObject value to set with")]
[SerializeField] protected GameObjectData gameObjectData; [SerializeField] protected GameObjectData gameObjectData;
@ -87,6 +91,11 @@ namespace Fungus
AudioSourceVariable audioSourceVariable = (variable as AudioSourceVariable); AudioSourceVariable audioSourceVariable = (variable as AudioSourceVariable);
audioSourceVariable.Apply(setOperator, audioSourceData.Value); audioSourceVariable.Apply(setOperator, audioSourceData.Value);
} }
else if (variable.GetType() == typeof(ColorVariable))
{
ColorVariable colorVariable = (variable as ColorVariable);
colorVariable.Apply(setOperator, colorData.Value);
}
else if (variable.GetType() == typeof(GameObjectVariable)) else if (variable.GetType() == typeof(GameObjectVariable))
{ {
GameObjectVariable gameObjectVariable = (variable as GameObjectVariable); GameObjectVariable gameObjectVariable = (variable as GameObjectVariable);
@ -103,6 +112,7 @@ namespace Fungus
{ typeof(StringVariable), StringVariable.setOperators }, { typeof(StringVariable), StringVariable.setOperators },
{ typeof(AnimatorVariable), AnimatorVariable.setOperators }, { typeof(AnimatorVariable), AnimatorVariable.setOperators },
{ typeof(AudioSourceVariable), AudioSourceVariable.setOperators }, { typeof(AudioSourceVariable), AudioSourceVariable.setOperators },
{ typeof(ColorVariable), ColorVariable.setOperators },
{ typeof(GameObjectVariable), GameObjectVariable.setOperators } { typeof(GameObjectVariable), GameObjectVariable.setOperators }
}; };
@ -174,6 +184,10 @@ namespace Fungus
{ {
description += audioSourceData.GetDescription(); description += audioSourceData.GetDescription();
} }
else if (variable.GetType() == typeof(ColorVariable))
{
description += colorData.GetDescription();
}
else if (variable.GetType() == typeof(GameObjectVariable)) else if (variable.GetType() == typeof(GameObjectVariable))
{ {
description += gameObjectData.GetDescription(); description += gameObjectData.GetDescription();

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

@ -18,6 +18,7 @@ namespace Fungus
typeof(StringVariable), typeof(StringVariable),
typeof(AnimatorVariable), typeof(AnimatorVariable),
typeof(AudioSourceVariable), typeof(AudioSourceVariable),
typeof(ColorVariable),
typeof(GameObjectVariable))] typeof(GameObjectVariable))]
[SerializeField] protected Variable variable; [SerializeField] protected Variable variable;
@ -39,6 +40,9 @@ namespace Fungus
[Tooltip("AudioSource value to compare against")] [Tooltip("AudioSource value to compare against")]
[SerializeField] protected AudioSourceData audioSourceData; [SerializeField] protected AudioSourceData audioSourceData;
[Tooltip("Color value to compare against")]
[SerializeField] protected ColorData colorData;
[Tooltip("GameObject value to compare against")] [Tooltip("GameObject value to compare against")]
[SerializeField] protected GameObjectData gameObjectData; [SerializeField] protected GameObjectData gameObjectData;
@ -81,6 +85,11 @@ namespace Fungus
AudioSourceVariable audioSourceVariable = (variable as AudioSourceVariable); AudioSourceVariable audioSourceVariable = (variable as AudioSourceVariable);
condition = audioSourceVariable.Evaluate(compareOperator, audioSourceData.Value); condition = audioSourceVariable.Evaluate(compareOperator, audioSourceData.Value);
} }
else if (variable.GetType() == typeof(ColorVariable))
{
ColorVariable colorVariable = (variable as ColorVariable);
condition = colorVariable.Evaluate(compareOperator, colorData.Value);
}
else if (variable.GetType() == typeof(GameObjectVariable)) else if (variable.GetType() == typeof(GameObjectVariable))
{ {
GameObjectVariable gameObjectVariable = (variable as GameObjectVariable); GameObjectVariable gameObjectVariable = (variable as GameObjectVariable);
@ -104,6 +113,7 @@ namespace Fungus
{ typeof(StringVariable), StringVariable.compareOperators }, { typeof(StringVariable), StringVariable.compareOperators },
{ typeof(AnimatorVariable), AnimatorVariable.compareOperators }, { typeof(AnimatorVariable), AnimatorVariable.compareOperators },
{ typeof(AudioSourceVariable), AudioSourceVariable.compareOperators }, { typeof(AudioSourceVariable), AudioSourceVariable.compareOperators },
{ typeof(ColorVariable), ColorVariable.compareOperators },
{ typeof(GameObjectVariable), GameObjectVariable.compareOperators } { typeof(GameObjectVariable), GameObjectVariable.compareOperators }
}; };
@ -146,6 +156,10 @@ namespace Fungus
{ {
summary += audioSourceData.GetDescription(); summary += audioSourceData.GetDescription();
} }
else if (variable.GetType() == typeof(ColorVariable))
{
summary += colorData.GetDescription();
}
else if (variable.GetType() == typeof(GameObjectVariable)) else if (variable.GetType() == typeof(GameObjectVariable))
{ {
summary += gameObjectData.GetDescription(); summary += gameObjectData.GetDescription();

7
Assets/Fungus/Scripts/Editor/SetVariableEditor.cs

@ -9,7 +9,7 @@ namespace Fungus.EditorUtils
{ {
[CustomEditor (typeof(SetVariable))] [CustomEditor (typeof(SetVariable))]
public class SetVariableEditor : CommandEditor public class SetVariableEditor : CommandEditor
{ {
protected struct VariablePropertyInfo protected struct VariablePropertyInfo
{ {
@ -46,6 +46,7 @@ namespace Fungus.EditorUtils
{ typeof(StringVariable), new VariablePropertyInfo("String", serializedObject.FindProperty("stringData")) }, { typeof(StringVariable), new VariablePropertyInfo("String", serializedObject.FindProperty("stringData")) },
{ typeof(AnimatorVariable), new VariablePropertyInfo("Animator", serializedObject.FindProperty("animatorData")) }, { typeof(AnimatorVariable), new VariablePropertyInfo("Animator", serializedObject.FindProperty("animatorData")) },
{ typeof(AudioSourceVariable), new VariablePropertyInfo("AudioSource", serializedObject.FindProperty("audioSourceData")) }, { 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")) }
}; };
} }
@ -107,7 +108,7 @@ namespace Fungus.EditorUtils
break; break;
} }
} }
// Get previously selected operator // Get previously selected operator
int selectedIndex = System.Array.IndexOf(setOperators, t._SetOperator); int selectedIndex = System.Array.IndexOf(setOperators, t._SetOperator);
if (selectedIndex < 0) if (selectedIndex < 0)
@ -119,7 +120,7 @@ namespace Fungus.EditorUtils
// Get next selected operator // Get next selected operator
selectedIndex = EditorGUILayout.Popup(new GUIContent("Operation", "Arithmetic operator to use"), selectedIndex, operatorsList.ToArray()); selectedIndex = EditorGUILayout.Popup(new GUIContent("Operation", "Arithmetic operator to use"), selectedIndex, operatorsList.ToArray());
setOperatorProp.enumValueIndex = (int)setOperators[selectedIndex]; setOperatorProp.enumValueIndex = (int)setOperators[selectedIndex];

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

@ -8,7 +8,7 @@ using System.Collections.Generic;
namespace Fungus.EditorUtils namespace Fungus.EditorUtils
{ {
[CustomEditor (typeof(VariableCondition), true)] [CustomEditor (typeof(VariableCondition), true)]
public class VariableConditionEditor : CommandEditor public class VariableConditionEditor : CommandEditor
{ {
protected SerializedProperty variableProp; protected SerializedProperty variableProp;
protected SerializedProperty compareOperatorProp; protected SerializedProperty compareOperatorProp;
@ -31,6 +31,7 @@ namespace Fungus.EditorUtils
{ typeof(StringVariable), serializedObject.FindProperty("stringData") }, { typeof(StringVariable), serializedObject.FindProperty("stringData") },
{ typeof(AnimatorVariable), serializedObject.FindProperty("animatorData") }, { typeof(AnimatorVariable), serializedObject.FindProperty("animatorData") },
{ typeof(AudioSourceVariable), serializedObject.FindProperty("audioSourceData") }, { typeof(AudioSourceVariable), serializedObject.FindProperty("audioSourceData") },
{ typeof(ColorVariable), serializedObject.FindProperty("colorData") },
{ typeof(GameObjectVariable), serializedObject.FindProperty("gameObjectData") } { typeof(GameObjectVariable), serializedObject.FindProperty("gameObjectData") }
}; };
} }

42
Assets/Fungus/Scripts/VariableTypes/ColorVariable.cs

@ -13,7 +13,47 @@ namespace Fungus
[AddComponentMenu("")] [AddComponentMenu("")]
[System.Serializable] [System.Serializable]
public class ColorVariable : VariableBase<Color> public class ColorVariable : VariableBase<Color>
{} {
public static readonly CompareOperator[] compareOperators = { CompareOperator.Equals, CompareOperator.NotEquals };
public static readonly SetOperator[] setOperators = { SetOperator.Assign };
protected static bool ColorsEqual(Color a, Color b) {
return ColorUtility.ToHtmlStringRGBA(a) == ColorUtility.ToHtmlStringRGBA(b);
}
public virtual bool Evaluate(CompareOperator compareOperator, Color value)
{
bool condition = false;
switch (compareOperator)
{
case CompareOperator.Equals:
condition = ColorsEqual(Value, value);
break;
case CompareOperator.NotEquals:
condition = !ColorsEqual(Value, value);
break;
default:
Debug.LogError("The " + compareOperator.ToString() + " comparison operator is not valid.");
break;
}
return condition;
}
public override void Apply(SetOperator setOperator, Color value)
{
switch (setOperator)
{
case SetOperator.Assign:
Value = value;
break;
default:
Debug.LogError("The " + setOperator.ToString() + " set operator is not valid.");
break;
}
}
}
/// <summary> /// <summary>
/// Container for a Color variable reference or constant value. /// Container for a Color variable reference or constant value.

Loading…
Cancel
Save