forked from peters/winforms-modernui
/
AnimationBase.cs
189 lines (164 loc) · 6.46 KB
/
AnimationBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* MetroFramework - Modern UI for WinForms
*
* The MIT License (MIT)
* Copyright (c) 2011 Sven Walter, http://github.com/viperneo
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in the
* Software without restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Windows.Forms;
namespace MetroFramework.Animation
{
public delegate void AnimationAction();
public delegate bool AnimationFinishedEvaluator();
public abstract class AnimationBase
{
public event EventHandler AnimationCompleted;
private void OnAnimationCompleted()
{
if (AnimationCompleted != null)
AnimationCompleted(this, EventArgs.Empty);
}
private DelayedCall timer;
private Control targetControl;
private AnimationAction actionHandler;
private AnimationFinishedEvaluator evaluatorHandler;
protected TransitionType transitionType;
protected int counter;
protected int startTime;
protected int targetTime;
public bool IsCompleted
{
get
{
if (timer != null)
return !timer.IsWaiting;
return true;
}
}
public bool IsRunning
{
get
{
if (timer != null)
return timer.IsWaiting;
return false;
}
}
public void Cancel()
{
if (IsRunning)
timer.Cancel();
}
protected void Start(Control control, TransitionType transitionType, int duration, AnimationAction actionHandler)
{
Start(control, transitionType, duration, actionHandler, null);
}
protected void Start(Control control, TransitionType transitionType, int duration, AnimationAction actionHandler, AnimationFinishedEvaluator evaluatorHandler)
{
this.targetControl = control;
this.transitionType = transitionType;
this.actionHandler = actionHandler;
this.evaluatorHandler = evaluatorHandler;
this.counter = 0;
this.startTime = 0;
this.targetTime = duration;
timer = DelayedCall.Start(DoAnimation, duration);
}
private void DoAnimation()
{
if (evaluatorHandler == null || evaluatorHandler.Invoke())
{
OnAnimationCompleted();
}
else
{
actionHandler.Invoke();
counter++;
timer.Start();
}
}
protected int MakeTransition(float t, float b, float d, float c)
{
switch (transitionType)
{
case TransitionType.Linear:
// simple linear tweening - no easing
return (int)(c * t / d + b);
case TransitionType.EaseInQuad:
// quadratic (t^2) easing in - accelerating from zero velocity
return (int)(c * (t /= d) * t + b);
case TransitionType.EaseOutQuad:
// quadratic (t^2) easing out - decelerating to zero velocity
return (int)(-c * (t = t / d) * (t - 2) + b);
case TransitionType.EaseInOutQuad:
// quadratic easing in/out - acceleration until halfway, then deceleration
if ((t /= d / 2) < 1)
{
return (int)(c / 2 * t * t + b);
}
else
{
return (int)(-c / 2 * ((--t) * (t - 2) - 1) + b);
}
case TransitionType.EaseInCubic:
// cubic easing in - accelerating from zero velocity
return (int)(c * (t /= d) * t * t + b);
case TransitionType.EaseOutCubic:
// cubic easing in - accelerating from zero velocity
return (int)(c * ((t = t / d - 1) * t * t + 1) + b);
case TransitionType.EaseInOutCubic:
// cubic easing in - accelerating from zero velocity
if ((t /= d / 2) < 1)
{
return (int)(c / 2 * t * t * t + b);
}
else
{
return (int)(c / 2 * ((t -= 2) * t * t + 2) + b);
}
case TransitionType.EaseInQuart:
// quartic easing in - accelerating from zero velocity
return (int)(c * (t /= d) * t * t * t + b);
case TransitionType.EaseInExpo:
// exponential (2^t) easing in - accelerating from zero velocity
if (t == 0)
{
return (int)b;
}
else
{
return (int)(c * Math.Pow(2, (10 * (t / d - 1))) + b);
}
case TransitionType.EaseOutExpo:
// exponential (2^t) easing out - decelerating to zero velocity
if (t == d)
{
return (int)(b + c);
}
else
{
return (int)(c * (-Math.Pow(2, -10 * t / d) + 1) + b);
}
default:
return 0;
}
}
}
}