comparison Implab.Fx/Animation.cs @ 4:381095ad0a69

Implab.Fx: implemented animation object Implab.Fx: implemented transparency animation helper
author cin
date Tue, 17 Sep 2013 04:27:30 +0400
parents 1e9583086e99
children dfa21d507bc5
comparison
equal deleted inserted replaced
3:1e9583086e99 4:381095ad0a69
1 using System; 1 using System;
2 using System.Collections.Generic; 2 using System.Collections.Generic;
3 using System.Linq; 3 using System.Linq;
4 using System.Text; 4 using System.Text;
5 using System.Timers;
6 using System.ComponentModel;
7 using System.Diagnostics;
5 8
6 namespace Implab.Fx 9 namespace Implab.Fx
7 { 10 {
8 public class Animation 11 public delegate void AnimationStep<T>(T target, int elapsed, int duration);
12
13 public class Animation<TArg> where TArg: class
9 { 14 {
10 int m_duration; 15 int m_duration;
11 int m_fps; 16 int m_delay;
17 int m_elapsed;
18 int m_prevTicks;
19 TArg m_arg;
20 ISynchronizeInvoke m_syncronizationObject;
12 21
22 public event AnimationStep<TArg> Step;
13 23
24 Promise<TArg> m_promise;
25
26 public Animation(TArg target, int duration, int delay)
27 {
28 if (duration <= 0)
29 throw new ArgumentOutOfRangeException("duration");
30 if (delay <= 0)
31 throw new ArgumentOutOfRangeException("delay");
32
33 m_arg = target;
34 m_syncronizationObject = target as ISynchronizeInvoke;
35 m_duration = duration;
36 m_delay = delay;
37 m_promise = new Promise<TArg>();
38 }
39
40 public Animation(TArg target)
41 : this(target, 500, 30)
42 {
43 }
44
45 public TArg Traget
46 {
47 get { return m_arg; }
48 }
49
50 public Promise<TArg> Play()
51 {
52 var timer = new Timer(m_delay);
53
54 timer.AutoReset = true;
55 timer.SynchronizingObject = m_syncronizationObject;
56 timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
57
58 m_prevTicks = Environment.TickCount;
59
60 timer.Start();
61
62 return m_promise;
63 }
64
65 void timer_Elapsed(object sender, ElapsedEventArgs args)
66 {
67 var timer = sender as Timer;
68
69 var dt = Environment.TickCount - m_prevTicks;
70 m_prevTicks = Environment.TickCount;
71
72 m_elapsed += dt;
73
74 if (m_elapsed > m_duration)
75 m_elapsed = m_duration;
76
77 try
78 {
79 var handler = Step;
80 if (handler != null)
81 handler(m_arg, m_elapsed, m_duration);
82 }
83 catch (Exception e)
84 {
85 Trace.TraceError(e.ToString());
86 }
87
88 if (m_elapsed < m_duration)
89 timer.Start();
90 else
91 {
92 timer.Dispose();
93 m_promise.Resolve(m_arg);
94 }
95 }
14 } 96 }
15 } 97 }