annotate Implab.Fx/AnimationHelpers.cs @ 30:2fad2d1f4b03

small refactoring, cleanup.
author cin
date Mon, 07 Apr 2014 03:25:57 +0400
parents f2559580b481
children d4e38929ce36
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
1 using System;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
2 using System.Collections.Generic;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
3 using System.Linq;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
4 using System.Text;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
5 using System.Windows.Forms;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
6 using System.Diagnostics;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
7
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
8 namespace Implab.Fx
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
9 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
10 public static class AnimationHelpers
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
11 {
5
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
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
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
13 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
14 if (animation == null)
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
15 throw new ArgumentNullException("animation");
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
16
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
17 TVal oldValue = getter(animation.Traget);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
18
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
19 animation.Step += (target, elaped, duration) =>
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
20 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
21 var value = fx(oldValue, newValue, elaped, duration);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
22 setter(target, value);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
23 };
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
24
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
25 return animation;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
26 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
27
5
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
28 public static Animation<T> AnimateTransparency<T>(this T ctl, float newValue) where T : Form
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
29 {
5
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
30 var anim = new Animation<T>(ctl);
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
31
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
32 anim.AnimateProperty(
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
33 (target, value) => target.Opacity = value,
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
34 target => target.Opacity,
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
35 newValue,
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
36 (ov, nv, el, du) => ov + ((float)el / du) * (nv - ov)
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
37 );
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
38 return anim;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
39 }
5
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
40
30
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 5
diff changeset
41 public static IPromise<T> CloseFadeOut<T>(this T ctl) where T : Form
5
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
42 {
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
43 var anim = ctl.AnimateTransparency(0);
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
44
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
45 return anim.Play().DispatchToControl(ctl).Then(frm => frm.Close());
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
46 }
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
47
30
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 5
diff changeset
48 public static IPromise<T> OverlayFadeIn<T>(this Form that, T overlay) where T : Form
5
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
49 {
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
50 if (that == null)
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
51 throw new ArgumentNullException("that");
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
52 if (overlay == null)
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
53 throw new ArgumentNullException("overlay");
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
54
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
55 // setup overlay
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
56 overlay.Opacity = 0;
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
57 overlay.FormBorderStyle = FormBorderStyle.None;
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
58 overlay.ShowInTaskbar = false;
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
59
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
60 that.AddOwnedForm(overlay);
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
61
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
62 EventHandler handler = (object sender, EventArgs args) =>
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
63 {
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
64 overlay.Bounds = that.RectangleToScreen(that.ClientRectangle);
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
65 };
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
66
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
67 // attach handlers
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
68 that.Move += handler;
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
69 that.Resize += handler;
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
70 that.Shown += handler;
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
71
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
72 // remove handlers to release overlay
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
73 overlay.FormClosed += (sender, args) =>
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
74 {
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
75 that.Move -= handler;
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
76 that.Resize -= handler;
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
77 that.Shown -= handler;
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
78 };
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
79
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
80 overlay.Show(that);
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
81 overlay.Bounds = that.RectangleToScreen(that.ClientRectangle);
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
82
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
83 return overlay
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
84 .AnimateTransparency(1)
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
85 .Play()
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
86 .DispatchToControl(overlay);
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
87 }
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
88 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
89 }