annotate Implab.Fx/Animation.cs @ 212:a01d9df88d74 v2

Added class Trace<T> to manage channels for individual classes, if SomeClass uses Trace<SomeClass> it sould be marked with TraceSourceAttribute
author cin
date Tue, 04 Apr 2017 12:04:05 +0300
parents dfa21d507bc5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
1e9583086e99 Added Impl.Fx
cin
parents:
diff changeset
1 using System;
1e9583086e99 Added Impl.Fx
cin
parents:
diff changeset
2 using System.Collections.Generic;
1e9583086e99 Added Impl.Fx
cin
parents:
diff changeset
3 using System.Linq;
1e9583086e99 Added Impl.Fx
cin
parents:
diff changeset
4 using System.Text;
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
5 using System.Timers;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
6 using System.ComponentModel;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
7 using System.Diagnostics;
3
1e9583086e99 Added Impl.Fx
cin
parents:
diff changeset
8
1e9583086e99 Added Impl.Fx
cin
parents:
diff changeset
9 namespace Implab.Fx
1e9583086e99 Added Impl.Fx
cin
parents:
diff changeset
10 {
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
11 public delegate void AnimationStep<T>(T target, int elapsed, int duration);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
12
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
13 public class Animation<TArg> where TArg: class
3
1e9583086e99 Added Impl.Fx
cin
parents:
diff changeset
14 {
1e9583086e99 Added Impl.Fx
cin
parents:
diff changeset
15 int m_duration;
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
16 int m_delay;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
17 int m_elapsed;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
18 int m_prevTicks;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
19 TArg m_arg;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
20 ISynchronizeInvoke m_syncronizationObject;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
21
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
22 public event AnimationStep<TArg> Step;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
23
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
24 Promise<TArg> m_promise;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
25
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
26 public Animation(TArg target, int duration, int delay)
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
27 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
28 if (duration <= 0)
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
29 throw new ArgumentOutOfRangeException("duration");
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
30 if (delay <= 0)
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
31 throw new ArgumentOutOfRangeException("delay");
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
32
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
33 m_arg = target;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
34 m_syncronizationObject = target as ISynchronizeInvoke;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
35 m_duration = duration;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
36 m_delay = delay;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
37 m_promise = new Promise<TArg>();
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
38 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
39
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
40 public Animation(TArg target)
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
41 : this(target, 500, 30)
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
42 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
43 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
44
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
45 public TArg Traget
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
46 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
47 get { return m_arg; }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
48 }
3
1e9583086e99 Added Impl.Fx
cin
parents:
diff changeset
49
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
50 public Promise<TArg> Play()
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
51 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
52 var timer = new Timer(m_delay);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
53
6
dfa21d507bc5 *refactoring: Promise.Then now returns a new chained promise
cin
parents: 4
diff changeset
54 timer.AutoReset = false;
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
55 timer.SynchronizingObject = m_syncronizationObject;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
56 timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
3
1e9583086e99 Added Impl.Fx
cin
parents:
diff changeset
57
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
58 m_prevTicks = Environment.TickCount;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
59
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
60 timer.Start();
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
61
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
62 return m_promise;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
63 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
64
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
65 void timer_Elapsed(object sender, ElapsedEventArgs args)
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
66 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
67 var timer = sender as Timer;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
68
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
69 var dt = Environment.TickCount - m_prevTicks;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
70 m_prevTicks = Environment.TickCount;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
71
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
72 m_elapsed += dt;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
73
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
74 if (m_elapsed > m_duration)
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
75 m_elapsed = m_duration;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
76
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
77 try
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
78 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
79 var handler = Step;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
80 if (handler != null)
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
81 handler(m_arg, m_elapsed, m_duration);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
82 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
83 catch (Exception e)
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
84 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
85 Trace.TraceError(e.ToString());
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
86 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
87
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
88 if (m_elapsed < m_duration)
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
89 timer.Start();
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
90 else
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
91 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
92 timer.Dispose();
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
93 m_promise.Resolve(m_arg);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
94 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 3
diff changeset
95 }
3
1e9583086e99 Added Impl.Fx
cin
parents:
diff changeset
96 }
1e9583086e99 Added Impl.Fx
cin
parents:
diff changeset
97 }