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

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

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

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

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

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

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

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

@ -13,7 +13,47 @@ namespace Fungus
[AddComponentMenu("")]
[System.Serializable]
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>
/// Container for a Color variable reference or constant value.

Loading…
Cancel
Save