AnimationHelpers.cs
41 lines
| 1.3 KiB
| text/x-csharp
|
CSharpLexer
/ Implab.Fx / AnimationHelpers.cs
cin
|
r4 | using System; | ||
using System.Collections.Generic; | ||||
using System.Linq; | ||||
using System.Text; | ||||
using System.Windows.Forms; | ||||
using System.Diagnostics; | ||||
namespace Implab.Fx | ||||
{ | ||||
public static class AnimationHelpers | ||||
{ | ||||
public static Animation<TTarget> AnimateProperty<TTarget,TVal>(this Animation<TTarget> animation, Action<TTarget,TVal> setter, Func<TTarget,TVal> getter, TVal newValue, Func<TVal,TVal,int,int,TVal> fx) where TTarget: class | ||||
{ | ||||
if (animation == null) | ||||
throw new ArgumentNullException("animation"); | ||||
TVal oldValue = getter(animation.Traget); | ||||
animation.Step += (target, elaped, duration) => | ||||
{ | ||||
var value = fx(oldValue, newValue, elaped, duration); | ||||
setter(target, value); | ||||
}; | ||||
return animation; | ||||
} | ||||
public static Animation<Form> AnimateTransparency(this Form ctl, float newValue) | ||||
{ | ||||
var anim = new Animation<Form>(ctl); | ||||
anim.AnimateProperty( | ||||
(target, value) => target.Opacity = value, | ||||
target => target.Opacity, | ||||
newValue, | ||||
(ov, nv, el, du) => ov + ((float)el / du) * (nv - ov) | ||||
); | ||||
return anim; | ||||
} | ||||
} | ||||
} | ||||