using UnityEngine;
using System;
namespace MoreMountains.Tools
{
///
/// String property setter
///
public class MMPropertyLinkString : MMPropertyLink
{
public Func GetStringDelegate;
public Action SetStringDelegate;
protected string _initialValue;
protected string _newValue;
///
/// On initialization we grab our initial value
///
///
public override void Initialization(MMProperty property)
{
base.Initialization(property);
_initialValue = (string)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)
{
GetStringDelegate = (Func)Delegate.CreateDelegate(typeof(Func),
firstArgument,
property.MemberPropertyInfo.GetGetMethod());
}
if (property.MemberPropertyInfo.GetSetMethod() != null)
{
SetStringDelegate = (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, (string)newValue);
}
///
/// Sets the level (above threshold : remap one, under threshold : remap zero)
///
///
///
///
public override void SetLevel(MMPropertyReceiver receiver, MMProperty property, float level)
{
base.SetLevel(receiver, property, level);
_newValue = (level > receiver.Threshold) ? receiver.StringRemapOne : receiver.StringRemapZero;
SetValueOptimized(property, _newValue);
}
///
/// Gets either the cached value or the raw value
///
///
///
protected virtual string GetValueOptimized(MMProperty property)
{
return _getterSetterInitialized ? GetStringDelegate() : (string)GetPropertyValue(property);
}
///
/// Sets either the cached value or the raw value
///
///
///
protected virtual void SetValueOptimized(MMProperty property, string newValue)
{
if (_getterSetterInitialized)
{
SetStringDelegate(_newValue);
}
else
{
SetPropertyValue(property, _newValue);
}
}
}
}