using UnityEngine;
using System;
namespace MoreMountains.Tools
{
///
/// Float property setter
///
public class MMPropertyLinkFloat : MMPropertyLink
{
public Func GetFloatDelegate;
public Action SetFloatDelegate;
protected float _initialValue;
protected float _newValue;
///
/// On init, grabs the initial float value
///
///
public override void Initialization(MMProperty property)
{
base.Initialization(property);
_initialValue = (float)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)
{
GetFloatDelegate = (Func)Delegate.CreateDelegate(typeof(Func),
firstArgument,
property.MemberPropertyInfo.GetGetMethod());
}
if (property.MemberPropertyInfo.GetSetMethod() != null)
{
SetFloatDelegate = (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, (float)newValue);
}
///
/// Returns this property link's level between 0 and 1
///
///
///
///
///
public override float GetLevel(MMPropertyEmitter emitter, MMProperty property)
{
float returnValue = GetValueOptimized(property);
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 = MMMaths.Remap(level, 0f, 1f, receiver.FloatRemapZero, receiver.FloatRemapOne);
if (receiver.RelativeValue)
{
_newValue = _initialValue + _newValue;
}
SetValueOptimized(property, _newValue);
}
///
/// Gets either the cached value or the raw value
///
///
///
protected virtual float GetValueOptimized(MMProperty property)
{
return _getterSetterInitialized ? GetFloatDelegate() : (float)GetPropertyValue(property);
}
///
/// Sets either the cached value or the raw value
///
///
///
protected virtual void SetValueOptimized(MMProperty property, float newValue)
{
if (_getterSetterInitialized)
{
SetFloatDelegate(_newValue);
}
else
{
SetPropertyValue(property, _newValue);
}
}
}
}