Skip to content

Commit

Permalink
feat(Data): set custom true and false values for boolean to float
Browse files Browse the repository at this point in the history
The BooleanToFloat component now has the ability to set the false and
true float value rather than it just being hard coded to 0f for false
and 1f for true.

The FloatToBoolean PositiveBounds property has also been made public
as it should be so it can be set via code if required.
  • Loading branch information
thestonefox committed May 9, 2022
1 parent 3d4c658 commit d05c220
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
38 changes: 37 additions & 1 deletion Runtime/Data/Type/Transformation/Conversion/BooleanToFloat.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Zinnia.Data.Type.Transformation.Conversion
{
using System;
using UnityEngine;
using UnityEngine.Events;

/// <summary>
Expand All @@ -18,14 +19,49 @@ public class BooleanToFloat : Transformer<bool, float, BooleanToFloat.UnityEvent
[Serializable]
public class UnityEvent : UnityEvent<float> { }

[Tooltip("The value to use if the boolean is false.")]
[SerializeField]
private float falseValue = 0f;
/// <summary>
/// The value to use if the boolean is false.
/// </summary>
public float FalseValue
{
get
{
return falseValue;
}
set
{
falseValue = value;
}
}
[Tooltip("The value to use if the boolean is true.")]
[SerializeField]
private float trueValue = 1f;
/// <summary>
/// The value to use if the boolean is true.
/// </summary>
public float TrueValue
{
get
{
return trueValue;
}
set
{
trueValue = value;
}
}

/// <summary>
/// Transforms the given input <see cref="bool"/> to the <see cref="float"/> equivalent value.
/// </summary>
/// <param name="input">The value to transform.</param>
/// <returns>The transformed value.</returns>
protected override float Process(bool input)
{
return input ? 1f : 0f;
return input ? TrueValue : FalseValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class UnityEvent : UnityEvent<bool> { }
/// <summary>
/// The bounds in which the <see cref="float"/> must be to be considered a positive boolean.
/// </summary>
protected FloatRange PositiveBounds
public FloatRange PositiveBounds
{
get
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,40 @@ public void TransformFalse()
Assert.IsTrue(transformedListenerMock.Received);
}

[Test]
public void TransformTrueCustomValue()
{
UnityEventListenerMock transformedListenerMock = new UnityEventListenerMock();
subject.Transformed.AddListener(transformedListenerMock.Listen);

Assert.AreEqual(0f, subject.Result);
Assert.IsFalse(transformedListenerMock.Received);

subject.TrueValue = 2f;
float result = subject.Transform(true);

Assert.AreEqual(2f, result);
Assert.AreEqual(2f, subject.Result);
Assert.IsTrue(transformedListenerMock.Received);
}

[Test]
public void TransformFalseCustomValue()
{
UnityEventListenerMock transformedListenerMock = new UnityEventListenerMock();
subject.Transformed.AddListener(transformedListenerMock.Listen);

Assert.AreEqual(0f, subject.Result);
Assert.IsFalse(transformedListenerMock.Received);

subject.FalseValue = -2f;
float result = subject.Transform(false);

Assert.AreEqual(-2f, result);
Assert.AreEqual(-2f, subject.Result);
Assert.IsTrue(transformedListenerMock.Received);
}

[Test]
public void TransformInactiveGameObject()
{
Expand Down

0 comments on commit d05c220

Please sign in to comment.