AnimationHelpers.cs
89 lines
| 2.9 KiB
| text/x-csharp
|
CSharpLexer
/ Implab.Fx / AnimationHelpers.cs
|
|
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 | ||||
| { | ||||
|
|
r5 | 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 | ||
|
|
r4 | { | ||
| 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; | ||||
| } | ||||
|
|
r5 | public static Animation<T> AnimateTransparency<T>(this T ctl, float newValue) where T : Form | ||
|
|
r4 | { | ||
|
|
r5 | var anim = new Animation<T>(ctl); | ||
|
|
r4 | |||
| anim.AnimateProperty( | ||||
| (target, value) => target.Opacity = value, | ||||
| target => target.Opacity, | ||||
| newValue, | ||||
| (ov, nv, el, du) => ov + ((float)el / du) * (nv - ov) | ||||
| ); | ||||
| return anim; | ||||
| } | ||||
|
|
r5 | |||
|
|
r30 | public static IPromise<T> CloseFadeOut<T>(this T ctl) where T : Form | ||
|
|
r5 | { | ||
| var anim = ctl.AnimateTransparency(0); | ||||
| return anim.Play().DispatchToControl(ctl).Then(frm => frm.Close()); | ||||
| } | ||||
|
|
r30 | public static IPromise<T> OverlayFadeIn<T>(this Form that, T overlay) where T : Form | ||
|
|
r5 | { | ||
| if (that == null) | ||||
| throw new ArgumentNullException("that"); | ||||
| if (overlay == null) | ||||
| throw new ArgumentNullException("overlay"); | ||||
| // setup overlay | ||||
| overlay.Opacity = 0; | ||||
| overlay.FormBorderStyle = FormBorderStyle.None; | ||||
| overlay.ShowInTaskbar = false; | ||||
| that.AddOwnedForm(overlay); | ||||
| EventHandler handler = (object sender, EventArgs args) => | ||||
| { | ||||
| overlay.Bounds = that.RectangleToScreen(that.ClientRectangle); | ||||
| }; | ||||
| // attach handlers | ||||
| that.Move += handler; | ||||
| that.Resize += handler; | ||||
| that.Shown += handler; | ||||
| // remove handlers to release overlay | ||||
| overlay.FormClosed += (sender, args) => | ||||
| { | ||||
| that.Move -= handler; | ||||
| that.Resize -= handler; | ||||
| that.Shown -= handler; | ||||
| }; | ||||
| overlay.Show(that); | ||||
| overlay.Bounds = that.RectangleToScreen(that.ClientRectangle); | ||||
| return overlay | ||||
| .AnimateTransparency(1) | ||||
| .Play() | ||||
| .DispatchToControl(overlay); | ||||
| } | ||||
|
|
r4 | } | ||
| } | ||||
