using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MonoScene.Graphics.Content { /// /// Defines an animatable property with a default value and a collection of animation curve tracks. /// /// A type that can be interpolated with [System.Diagnostics.DebuggerDisplay("{Value} with {CurveCount} curves.")] public sealed class AnimatableProperty where T : struct { #region lifecycle public AnimatableProperty(T defaultValue) { Value = defaultValue; } #endregion #region data private List> _Curves; /// /// Gets the default value of this instance. /// When animations are disabled, or there's no animation track available, this will be the returned value. /// public T Value { get; set; } #endregion #region properties public bool IsAnimated => _Curves == null ? false : _Curves.Count > 0; public int CurveCount => _Curves.Count; #endregion #region API /// /// Evaluates the value of this at a given for a given . /// /// The index of the animation track /// The time offset within the curve /// The evaluated value taken from the animation , or if a track was not found. public T GetValueAt(int curveIndex, float offset) { if (_Curves == null) return this.Value; if (curveIndex < 0 || curveIndex >= _Curves.Count) return this.Value; return _Curves[curveIndex]?.Evaluate(offset) ?? this.Value; } public void SetCurve(int curveIndex, Microsoft.Xna.Framework.ICurveEvaluator sampler) { if (curveIndex < 0) throw new ArgumentOutOfRangeException(nameof(curveIndex)); if (_Curves == null) _Curves = new List>(); while (_Curves.Count <= curveIndex) _Curves.Add(null); _Curves[curveIndex] = sampler ?? throw new ArgumentNullException(nameof(sampler)); } #endregion } }