using System; using UnityEngine; namespace MoreMountains.Tools { /// /// A class, meant to be extended, used to control a MMProperty and get/set its value /// public abstract class MMPropertyLink { protected bool _getterSetterInitialized = false; /// /// Initialization method /// /// public virtual void Initialization(MMProperty property) { CreateGettersAndSetters(property); } /// /// A method used to cache getter and setter for properties, not fields (sadly) /// /// public virtual void CreateGettersAndSetters(MMProperty property) { } /// /// Gets the "level" of the property, a normalized float value, caching the operation if possible /// /// /// /// public virtual float GetLevel(MMPropertyEmitter emitter, MMProperty property) { return 0f; } /// /// Sets the property's level, float normalized, caching the operation if possible /// /// /// /// public virtual void SetLevel(MMPropertyReceiver receiver, MMProperty property, float level) { receiver.Level = level; } /// /// Gets the raw value of the property, a normalized float value, caching the operation if possible /// /// /// /// public virtual object GetValue(MMPropertyEmitter emitter, MMProperty property) { return 0f; } /// /// Sets the raw property value, float normalized, caching the operation if possible /// /// /// /// public virtual void SetValue(MMPropertyReceiver receiver, MMProperty property, object newValue) { } /// /// Returns the value of the selected property /// /// protected virtual object GetPropertyValue(MMProperty property) { object target = (property.TargetScriptableObject == null) ? (object)property.TargetComponent : (object)property.TargetScriptableObject; if (property.MemberType == MMProperty.MemberTypes.Property) { return property.MemberPropertyInfo.GetValue(target); } else if (property.MemberType == MMProperty.MemberTypes.Field) { return property.MemberFieldInfo.GetValue(target); } return 0f; } /// /// Sets the value of the selected property /// /// protected virtual void SetPropertyValue(MMProperty property, object newValue) { object target = (property.TargetScriptableObject == null) ? (object)property.TargetComponent : (object)property.TargetScriptableObject; if (property.MemberType == MMProperty.MemberTypes.Property) { property.MemberPropertyInfo.SetValue(target, newValue); } else if (property.MemberType == MMProperty.MemberTypes.Field) { property.MemberFieldInfo.SetValue(target, newValue); } } } }