private void AddLerp(Lerper lerper, GlideInfo info, object from, object to) { varHash.Add(info.PropertyName, vars.Count); vars.Add(info); start.Add(from); end.Add(to); lerpers.Add(lerper); }
/// <summary> /// <para>Tweens a set of properties on an object.</para> /// <para>To tween instance properties/fields, pass the object.</para> /// <para>To tween static properties/fields, pass the type of the object, using typeof(ObjectType) or object.GetType().</para> /// </summary> /// <param name="target">The object or type to tween.</param> /// <param name="values">The values to tween to, in an anonymous type ( new { prop1 = 100, prop2 = 0} ).</param> /// <param name="duration">Duration of the tween in seconds.</param> /// <param name="delay">Delay before the tween starts, in seconds.</param> /// <param name="overwrite">Whether pre-existing tweens should be overwritten if this tween involves the same properties.</param> /// <returns>The tween created, for setting properties on.</returns> public Tween Tween <T>(T target, object values, float duration, float delay = 0, bool overwrite = true) where T : class { if (target == null) { throw new ArgumentNullException("target"); } // Prevent tweening on structs if you cheat by casting target as Object var targetType = target.GetType(); if (targetType.IsValueType) { throw new Exception("Target of tween cannot be a struct!"); } var tween = new Tween(target, duration, delay, this); AddAndRemove(); toAdd.Add(tween); if (values == null) // valid in case of manual timer { return(tween); } var props = values.GetType().GetProperties(); for (int i = 0; i < props.Length; ++i) { List <Tween> library = null; if (overwrite && tweens.TryGetValue(target, out library)) { for (int j = 0; j < library.Count; j++) { library[j].Cancel(props[i].Name); } } var property = props[i]; var info = new GlideInfo(target, property.Name); var to = new GlideInfo(values, property.Name, false); var lerper = CreateLerper(info.PropertyType); tween.AddLerp(lerper, info, info.Value, to.Value); } return(tween); }
/// <summary> /// Apply target values to a starting point before tweening. /// </summary> /// <param name="values">The values to apply, in an anonymous type ( new { prop1 = 100, prop2 = 0} ).</param> /// <returns>A reference to this.</returns> public Tween From(object values) { var props = values.GetType().GetProperties(); for (int i = 0; i < props.Length; ++i) { var property = props[i]; var propValue = property.GetValue(values, null); int index = -1; if (varHash.TryGetValue(property.Name, out index)) { // if we're already tweening this value, adjust the range start[index] = propValue; } // if we aren't tweening this value, just set it var info = new GlideInfo(Target, property.Name, true); info.Value = propValue; } return(this); }