using UnityEngine;
using System;
namespace MoreMountains.Tools
{
///
/// Vector3 property setter
///
public class MMPropertyLinkVector3 : MMPropertyLink
{
public Func GetVector3Delegate;
public Action SetVector3Delegate;
protected Vector3 _initialValue;
protected Vector3 _newValue;
protected Vector3 _vector3;
///
/// On init we grab our initial value
///
///
public override void Initialization(MMProperty property)
{
base.Initialization(property);
_initialValue = (Vector3)GetPropertyValue(property);
}
///
/// Creates cached getter and setters for properties
///
///
public override void CreateGettersAndSetters(MMProperty property)
{
base.CreateGettersAndSetters(property);
if (property.MemberType == MMProperty.MemberTypes.Property)
{
object firstArgument = (property.TargetScriptableObject == null) ? (object)property.TargetComponent : (object)property.TargetScriptableObject;
if (property.MemberPropertyInfo.GetGetMethod() != null)
{
GetVector3Delegate = (Func)Delegate.CreateDelegate(typeof(Func),
firstArgument,
property.MemberPropertyInfo.GetGetMethod());
}
if (property.MemberPropertyInfo.GetSetMethod() != null)
{
SetVector3Delegate = (Action)Delegate.CreateDelegate(typeof(Action),
firstArgument,
property.MemberPropertyInfo.GetSetMethod());
}
_getterSetterInitialized = true;
}
}
///
/// Gets the raw value of the property, a normalized float value, caching the operation if possible
///
///
///
///
public override object GetValue(MMPropertyEmitter emitter, MMProperty property)
{
return GetValueOptimized(property);
}
///
/// Sets the raw property value, float normalized, caching the operation if possible
///
///
///
///
public override void SetValue(MMPropertyReceiver receiver, MMProperty property, object newValue)
{
SetValueOptimized(property, (Vector3)newValue);
}
///
/// Returns this property link's level between 0 and 1
///
///
///
///
///
public override float GetLevel(MMPropertyEmitter emitter, MMProperty property)
{
_vector3 = GetValueOptimized(property);
float newValue = 0f;
switch (emitter.Vector3Option)
{
case MMPropertyEmitter.Vector3Options.X:
newValue = _vector3.x;
break;
case MMPropertyEmitter.Vector3Options.Y:
newValue = _vector3.y;
break;
case MMPropertyEmitter.Vector3Options.Z:
newValue = _vector3.z;
break;
}
float returnValue = newValue;
returnValue = MMMaths.Clamp(returnValue, emitter.FloatRemapMinToZero, emitter.FloatRemapMaxToOne, emitter.ClampMin, emitter.ClampMax);
returnValue = MMMaths.Remap(returnValue, emitter.FloatRemapMinToZero, emitter.FloatRemapMaxToOne, 0f, 1f);
emitter.Level = returnValue;
return returnValue;
}
///
/// Sets the level
///
///
///
///
public override void SetLevel(MMPropertyReceiver receiver, MMProperty property, float level)
{
base.SetLevel(receiver, property, level);
_newValue.x = receiver.ModifyX ? MMMaths.Remap(level, 0f, 1f, receiver.Vector3RemapZero.x, receiver.Vector3RemapOne.x) : 0f;
_newValue.y = receiver.ModifyY ? MMMaths.Remap(level, 0f, 1f, receiver.Vector3RemapZero.y, receiver.Vector3RemapOne.y) : 0f;
_newValue.z = receiver.ModifyZ ? MMMaths.Remap(level, 0f, 1f, receiver.Vector3RemapZero.z, receiver.Vector3RemapOne.z) : 0f;
if (receiver.RelativeValue)
{
_newValue = _initialValue + _newValue;
}
SetValueOptimized(property, _newValue);
}
///
/// Gets either the cached value or the raw value
///
///
///
protected virtual Vector3 GetValueOptimized(MMProperty property)
{
return _getterSetterInitialized ? GetVector3Delegate() : (Vector3)GetPropertyValue(property);
}
///
/// Sets either the cached value or the raw value
///
///
///
protected virtual void SetValueOptimized(MMProperty property, Vector3 newValue)
{
if (_getterSetterInitialized)
{
SetVector3Delegate(_newValue);
}
else
{
SetPropertyValue(property, _newValue);
}
}
}
}