Skip to content

Commit

Permalink
Merge pull request #503 from ExtendRealityLtd/feat/adding-stuff
Browse files Browse the repository at this point in the history
Feat/adding stuff
  • Loading branch information
thestonefox authored Jun 7, 2020
2 parents a7585df + 6f62f49 commit e4d783c
Show file tree
Hide file tree
Showing 15 changed files with 197 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class RigidbodyPropertyMutator : MonoBehaviour
{
/// <summary>
/// The rigidbody to mutate.
/// The <see cref="Rigidbody"/> to mutate.
/// </summary>
[Serialized, Cleared]
[field: DocumentedByXml]
Expand Down
38 changes: 35 additions & 3 deletions Runtime/Data/Operation/Mutation/TransformEulerRotationMutator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,28 @@
using Malimbe.XmlDocumentationAttribute;
using System;
using UnityEngine;
using Zinnia.Data.Type;
using Zinnia.Extension;

/// <summary>
/// Mutates the euler rotation of a transform with an optional custom rotation origin.
/// </summary>
public class TransformEulerRotationMutator : TransformPropertyMutator
{
#region Rotation Settings
/// <summary>
/// An optional rotation origin to perform the rotation around. The origin must be a child of the <see cref="TransformPropertyMutator.Target"/>.
/// </summary>
[Serialized, Cleared]
[field: DocumentedByXml]
[field: Header("Rotation Settings"), DocumentedByXml]
public GameObject Origin { get; set; }
/// <summary>
/// Determines which axes to consider from the <see cref="Origin"/>.
/// </summary>
[Serialized]
[field: DocumentedByXml]
public Vector3State ApplyOriginOnAxis { get; set; } = Vector3State.True;
#endregion

/// <inheritdoc/>
protected override float GetGlobalAxisValue(int axis)
Expand Down Expand Up @@ -82,7 +92,29 @@ protected virtual void OnEnable()
/// <returns>The origin position.</returns>
protected virtual Vector3 GetOriginPosition()
{
return Origin != null ? Origin.transform.position : Vector3.zero;
if (Origin == null)
{
return Vector3.zero;
}

Vector3 originAxesToApply = ApplyOriginOnAxis.ToVector3();
Vector3? cachedOriginLocalPosition = null;

if (!originAxesToApply.ApproxEquals(Vector3.one))
{
cachedOriginLocalPosition = Origin.transform.localPosition;
originAxesToApply.Scale(Origin.transform.localPosition);
Origin.transform.localPosition = originAxesToApply;
}

Vector3 returnValue = Origin.transform.position;

if (cachedOriginLocalPosition != null)
{
Origin.transform.localPosition = cachedOriginLocalPosition.GetValueOrDefault();
}

return returnValue;
}

/// <summary>
Expand All @@ -96,7 +128,7 @@ protected virtual void ApplyRotationOriginPosition(Vector3 originPosition)
return;
}

originPosition -= Origin.transform.position;
originPosition -= GetOriginPosition();
Target.transform.position += originPosition;
}

Expand Down
28 changes: 26 additions & 2 deletions Runtime/Data/Operation/Mutation/TransformPositionMutator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,28 @@
using Malimbe.PropertySerializationAttribute;
using Malimbe.XmlDocumentationAttribute;
using UnityEngine;
using Zinnia.Data.Type;
using Zinnia.Extension;

/// <summary>
/// Mutates the position of a transform with an optional facing direction.
/// </summary>
public class TransformPositionMutator : TransformPropertyMutator
{
#region Position Settings
/// <summary>
/// Determines the facing direction when mutating the position.
/// </summary>
[Serialized, Cleared]
[field: DocumentedByXml]
[field: Header("Position Settings"), DocumentedByXml]
public GameObject FacingDirection { get; set; }
/// <summary>
/// Determines which axes to take from the <see cref="FacingDirection"/>.
/// </summary>
[Serialized]
[field: DocumentedByXml]
public Vector3State ApplyFacingDirectionOnAxis { get; set; } = Vector3State.True;
#endregion

/// <inheritdoc/>
protected override float GetGlobalAxisValue(int axis)
Expand Down Expand Up @@ -59,7 +69,21 @@ protected override Vector3 SetLocal(Vector3 input)
/// <returns>The facing direction.</returns>
protected virtual Quaternion GetFacingDirection()
{
return FacingDirection == null ? Quaternion.identity : (UseLocalValues ? FacingDirection.transform.localRotation : FacingDirection.transform.rotation);
if (FacingDirection == null)
{
return Quaternion.identity;
}

Quaternion returnValue = UseLocalValues ? FacingDirection.transform.localRotation : FacingDirection.transform.rotation;
Vector3 facingAxesToApply = ApplyFacingDirectionOnAxis.ToVector3();

if (facingAxesToApply.ApproxEquals(Vector3.one))
{
return returnValue;
}

facingAxesToApply.Scale(returnValue.eulerAngles);
return Quaternion.Euler(facingAxesToApply);
}
}
}
4 changes: 3 additions & 1 deletion Runtime/Data/Operation/Mutation/TransformPropertyMutator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
/// </summary>
public abstract class TransformPropertyMutator : MonoBehaviour
{
#region Target Settings
/// <summary>
/// The target to mutate.
/// </summary>
[Serialized, Cleared]
[field: DocumentedByXml]
[field: Header("Target Settings"), DocumentedByXml]
public GameObject Target { get; set; }
/// <summary>
/// Determines whether to mutate the local or global values.
Expand All @@ -30,6 +31,7 @@ public abstract class TransformPropertyMutator : MonoBehaviour
[Serialized]
[field: DocumentedByXml]
public Vector3State MutateOnAxis { get; set; } = Vector3State.True;
#endregion

/// <summary>
/// Sets the property to the new value.
Expand Down
10 changes: 9 additions & 1 deletion Runtime/Process/Component/GameObjectSourceTargetProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@
using Malimbe.MemberClearanceMethod;
using Malimbe.PropertySerializationAttribute;
using Malimbe.XmlDocumentationAttribute;
using System;
using UnityEngine;
using UnityEngine.Events;
using Zinnia.Data.Collection.List;
using Zinnia.Extension;
using Zinnia.Rule;

/// <summary>
/// A <see cref="SourceTargetProcessor{TSource, TTarget}"/> that specifically processes a <see cref="GameObject"/>.
/// </summary>
public abstract class GameObjectSourceTargetProcessor : SourceTargetProcessor<GameObject, GameObject>
public abstract class GameObjectSourceTargetProcessor : SourceTargetProcessor<GameObject, GameObject, GameObjectSourceTargetProcessor.GameObjectUnityEvent>
{
/// <summary>
/// Defines the event with the <see cref="GameObject"/>.
/// </summary>
[Serializable]
public class GameObjectUnityEvent : UnityEvent<GameObject> { }

#region Processor Settings
/// <summary>
/// A <see cref="GameObject"/> collection of sources to apply data from.
Expand Down
22 changes: 21 additions & 1 deletion Runtime/Process/Component/SourceTargetProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
using Malimbe.XmlDocumentationAttribute;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using Zinnia.Data.Type;

/// <summary>
/// An <see cref="IProcessable"/> that runs a set method on each (or the first active) source collection against a collection of targets.
/// </summary>
public abstract class SourceTargetProcessor<TSource, TTarget> : MonoBehaviour, IProcessable
public abstract class SourceTargetProcessor<TSource, TTarget, TEvent> : MonoBehaviour, IProcessable where TEvent : UnityEvent<TSource>, new()
{
/// <summary>
/// Emitted if the <see cref="ActiveSource"/> value is going to change with the new value as the payload.
/// </summary>
[DocumentedByXml]
public TEvent ActiveSourceChanging = new TEvent();

/// <summary>
/// Whether to cease the processing of the source collection after the first valid source is processed.
/// </summary>
Expand Down Expand Up @@ -73,6 +80,7 @@ protected virtual bool IsTargetValid(TTarget target)
/// <param name="targets">The targets to apply the data to.</param>
protected virtual void ApplySourcesToTargets(HeapAllocationFreeReadOnlyList<TSource> sources, HeapAllocationFreeReadOnlyList<TTarget> targets)
{
bool foundValidSource = false;
for (int sourceIndex = 0; sourceIndex < sources.Count; sourceIndex++)
{
TSource currentSource = sources[sourceIndex];
Expand All @@ -93,12 +101,24 @@ protected virtual void ApplySourcesToTargets(HeapAllocationFreeReadOnlyList<TSou
ApplySourceToTarget(currentSource, currentTarget);
}

if (!EqualityComparer<TSource>.Default.Equals(ActiveSource, currentSource))
{
ActiveSourceChanging?.Invoke(currentSource);
}
ActiveSource = currentSource;
foundValidSource = true;

if (CeaseAfterFirstSourceProcessed)
{
break;
}
}

if (!foundValidSource && !EqualityComparer<TSource>.Default.Equals(ActiveSource, default))
{
ActiveSource = default;
ActiveSourceChanging?.Invoke(default);
}
}
}
}
4 changes: 4 additions & 0 deletions Tests/Editor/Data/Operation/Cache/FloatCacheTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ namespace Test.Zinnia.Data.Operation.Cache
public class FloatCacheTest
{
private GameObject containingObject;
#pragma warning disable 0618
private FloatCache subject;
#pragma warning restore 0618

[SetUp]
public void SetUp()
{
containingObject = new GameObject();
#pragma warning disable 0618
subject = containingObject.AddComponent<FloatCache>();
#pragma warning restore 0618
}

[TearDown]
Expand Down
4 changes: 4 additions & 0 deletions Tests/Editor/Data/Operation/Cache/GameObjectCacheTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ namespace Test.Zinnia.Data.Operation.Cache
public class GameObjectCacheTest
{
private GameObject containingObject;
#pragma warning disable 0618
private GameObjectCache subject;
#pragma warning restore 0618

[SetUp]
public void SetUp()
{
containingObject = new GameObject();
#pragma warning disable 0618
subject = containingObject.AddComponent<GameObjectCache>();
#pragma warning restore 0618
}

[TearDown]
Expand Down
4 changes: 4 additions & 0 deletions Tests/Editor/Data/Operation/Cache/IntCacheTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ namespace Test.Zinnia.Data.Operation.Cache
public class IntCacheTest
{
private GameObject containingObject;
#pragma warning disable 0618
private IntCache subject;
#pragma warning restore 0618

[SetUp]
public void SetUp()
{
containingObject = new GameObject();
#pragma warning disable 0618
subject = containingObject.AddComponent<IntCache>();
#pragma warning restore 0618
}

[TearDown]
Expand Down
4 changes: 4 additions & 0 deletions Tests/Editor/Data/Operation/Cache/StringCacheTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ namespace Test.Zinnia.Data.Operation.Cache
public class StringCacheTest
{
private GameObject containingObject;
#pragma warning disable 0618
private StringCache subject;
#pragma warning restore 0618

[SetUp]
public void SetUp()
{
containingObject = new GameObject();
#pragma warning disable 0618
subject = containingObject.AddComponent<StringCache>();
#pragma warning restore 0618
}

[TearDown]
Expand Down
4 changes: 4 additions & 0 deletions Tests/Editor/Data/Operation/Cache/Vector2CacheTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ namespace Test.Zinnia.Data.Operation.Cache
public class Vector2CacheTest
{
private GameObject containingObject;
#pragma warning disable 0618
private Vector2Cache subject;
#pragma warning restore 0618

[SetUp]
public void SetUp()
{
containingObject = new GameObject();
#pragma warning disable 0618
subject = containingObject.AddComponent<Vector2Cache>();
#pragma warning restore 0618
}

[TearDown]
Expand Down
4 changes: 4 additions & 0 deletions Tests/Editor/Data/Operation/Cache/Vector3CacheTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ namespace Test.Zinnia.Data.Operation.Cache
public class Vector3CacheTest
{
private GameObject containingObject;
#pragma warning disable 0618
private Vector3Cache subject;
#pragma warning restore 0618

[SetUp]
public void SetUp()
{
containingObject = new GameObject();
#pragma warning disable 0618
subject = containingObject.AddComponent<Vector3Cache>();
#pragma warning restore 0618
}

[TearDown]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Test.Zinnia.Data.Operation.Mutation
{
using NUnit.Framework;
using NUnit.Framework.Constraints;
using UnityEngine;
using Assert = UnityEngine.Assertions.Assert;

Expand Down Expand Up @@ -133,6 +134,34 @@ public void SetPropertyWithOrigin()
Object.DestroyImmediate(target);
}

[Test]
public void SetPropertyWithOriginIgnoreZ()
{
GameObject target = new GameObject();
GameObject origin = new GameObject();
origin.transform.SetParent(target.transform);

subject.Target = target;
subject.Origin = origin;
subject.MutateOnAxis = new Vector3State(false, true, false);
subject.ApplyOriginOnAxis = new Vector3State(true, true, false);

origin.transform.position = new Vector3(1f, 0f, 1f);

Assert.AreEqual(Vector3.zero, target.transform.eulerAngles);

Vector3 inputRotation = new Vector3(10f, 20f, 30f);
Vector3 expectedRotation = new Vector3(0f, 20f, 0f);
Vector3 expectedPosition = new Vector3(0.1f, 0f, 0.3f);

subject.SetProperty(inputRotation);

Assert.AreEqual(expectedRotation.ToString(), target.transform.eulerAngles.ToString());
Assert.AreEqual(expectedPosition.ToString(), target.transform.position.ToString());

Object.DestroyImmediate(target);
}

[Test]
public void IncrementPropertyLocal()
{
Expand Down
Loading

0 comments on commit e4d783c

Please sign in to comment.