comparison Implab.Fx/AnimationHelpers.cs @ 5:f2559580b481

implemented overlay helpers and some animation heplers improvments
author cin
date Tue, 24 Sep 2013 03:31:24 +0400
parents 381095ad0a69
children 2fad2d1f4b03
comparison
equal deleted inserted replaced
4:381095ad0a69 5:f2559580b481
7 7
8 namespace Implab.Fx 8 namespace Implab.Fx
9 { 9 {
10 public static class AnimationHelpers 10 public static class AnimationHelpers
11 { 11 {
12 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 12 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
13 { 13 {
14 if (animation == null) 14 if (animation == null)
15 throw new ArgumentNullException("animation"); 15 throw new ArgumentNullException("animation");
16 16
17 TVal oldValue = getter(animation.Traget); 17 TVal oldValue = getter(animation.Traget);
23 }; 23 };
24 24
25 return animation; 25 return animation;
26 } 26 }
27 27
28 public static Animation<Form> AnimateTransparency(this Form ctl, float newValue) 28 public static Animation<T> AnimateTransparency<T>(this T ctl, float newValue) where T : Form
29 { 29 {
30 var anim = new Animation<Form>(ctl); 30 var anim = new Animation<T>(ctl);
31 31
32 anim.AnimateProperty( 32 anim.AnimateProperty(
33 (target, value) => target.Opacity = value, 33 (target, value) => target.Opacity = value,
34 target => target.Opacity, 34 target => target.Opacity,
35 newValue, 35 newValue,
36 (ov, nv, el, du) => ov + ((float)el / du) * (nv - ov) 36 (ov, nv, el, du) => ov + ((float)el / du) * (nv - ov)
37 ); 37 );
38 return anim; 38 return anim;
39 } 39 }
40
41 public static Promise<T> CloseFadeOut<T>(this T ctl) where T : Form
42 {
43 var anim = ctl.AnimateTransparency(0);
44
45 return anim.Play().DispatchToControl(ctl).Then(frm => frm.Close());
46 }
47
48 public static Promise<T> OverlayFadeIn<T>(this Form that, T overlay) where T : Form
49 {
50 if (that == null)
51 throw new ArgumentNullException("that");
52 if (overlay == null)
53 throw new ArgumentNullException("overlay");
54
55 // setup overlay
56 overlay.Opacity = 0;
57 overlay.FormBorderStyle = FormBorderStyle.None;
58 overlay.ShowInTaskbar = false;
59
60 that.AddOwnedForm(overlay);
61
62 EventHandler handler = (object sender, EventArgs args) =>
63 {
64 overlay.Bounds = that.RectangleToScreen(that.ClientRectangle);
65 };
66
67 // attach handlers
68 that.Move += handler;
69 that.Resize += handler;
70 that.Shown += handler;
71
72 // remove handlers to release overlay
73 overlay.FormClosed += (sender, args) =>
74 {
75 that.Move -= handler;
76 that.Resize -= handler;
77 that.Shown -= handler;
78 };
79
80 overlay.Show(that);
81 overlay.Bounds = that.RectangleToScreen(that.ClientRectangle);
82
83 return overlay
84 .AnimateTransparency(1)
85 .Play()
86 .DispatchToControl(overlay);
87 }
40 } 88 }
41 } 89 }