-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathDefaultEasingFunction.cs
187 lines (135 loc) · 7.55 KB
/
DefaultEasingFunction.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
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
namespace osu.Framework.Graphics.Transforms
{
/// <summary>
/// An easing function corresponding to one of the <see cref="Easing"/> types.
/// </summary>
public readonly struct DefaultEasingFunction : IEasingFunction
{
private const double elastic_const = 2 * Math.PI / .3;
private const double elastic_const2 = .3 / 4;
private const double back_const = 1.70158;
private const double back_const2 = back_const * 1.525;
private const double bounce_const = 1 / 2.75;
// constants used to fix expo and elastic curves to start/end at 0/1
private static readonly double expo_offset = Math.Pow(2, -10);
private static readonly double elastic_offset_full = Math.Pow(2, -11);
private static readonly double elastic_offset_half = Math.Pow(2, -10) * Math.Sin((.5 - elastic_const2) * elastic_const);
private static readonly double elastic_offset_quarter = Math.Pow(2, -10) * Math.Sin((.25 - elastic_const2) * elastic_const);
private static readonly double in_out_elastic_offset = Math.Pow(2, -10) * Math.Sin((1 - elastic_const2 * 1.5) * elastic_const / 1.5);
private readonly Easing easing;
/// <summary>
/// Creates a new <see cref="DefaultEasingFunction"/> for an <see cref="Easing"/>.
/// </summary>
/// <param name="easing">The <see cref="Easing"/> type.</param>
public DefaultEasingFunction(Easing easing)
{
this.easing = easing;
}
public double ApplyEasing(double time)
{
switch (easing)
{
default:
return time;
case Easing.In:
case Easing.InQuad:
return time * time;
case Easing.Out:
case Easing.OutQuad:
return time * (2 - time);
case Easing.InOutQuad:
if (time < .5) return time * time * 2;
return --time * time * -2 + 1;
case Easing.InCubic:
return time * time * time;
case Easing.OutCubic:
return --time * time * time + 1;
case Easing.InOutCubic:
if (time < .5) return time * time * time * 4;
return --time * time * time * 4 + 1;
case Easing.InQuart:
return time * time * time * time;
case Easing.OutQuart:
return 1 - --time * time * time * time;
case Easing.InOutQuart:
if (time < .5) return time * time * time * time * 8;
return --time * time * time * time * -8 + 1;
case Easing.InQuint:
return time * time * time * time * time;
case Easing.OutQuint:
return --time * time * time * time * time + 1;
case Easing.InOutQuint:
if (time < .5) return time * time * time * time * time * 16;
return --time * time * time * time * time * 16 + 1;
case Easing.InSine:
return 1 - Math.Cos(time * Math.PI * .5);
case Easing.OutSine:
return Math.Sin(time * Math.PI * .5);
case Easing.InOutSine:
return .5 - .5 * Math.Cos(Math.PI * time);
case Easing.InExpo:
return Math.Pow(2, 10 * (time - 1)) + expo_offset * (time - 1);
case Easing.OutExpo:
return -Math.Pow(2, -10 * time) + 1 + expo_offset * time;
case Easing.InOutExpo:
if (time < .5) return .5 * (Math.Pow(2, 20 * time - 10) + expo_offset * (2 * time - 1));
return 1 - .5 * (Math.Pow(2, -20 * time + 10) + expo_offset * (-2 * time + 1));
case Easing.InCirc:
return 1 - Math.Sqrt(1 - time * time);
case Easing.OutCirc:
return Math.Sqrt(1 - --time * time);
case Easing.InOutCirc:
if ((time *= 2) < 1) return .5 - .5 * Math.Sqrt(1 - time * time);
return .5 * Math.Sqrt(1 - (time -= 2) * time) + .5;
case Easing.InElastic:
return -Math.Pow(2, -10 + 10 * time) * Math.Sin((1 - elastic_const2 - time) * elastic_const) + elastic_offset_full * (1 - time);
case Easing.OutElastic:
return Math.Pow(2, -10 * time) * Math.Sin((time - elastic_const2) * elastic_const) + 1 - elastic_offset_full * time;
case Easing.OutElasticHalf:
return Math.Pow(2, -10 * time) * Math.Sin((.5 * time - elastic_const2) * elastic_const) + 1 - elastic_offset_half * time;
case Easing.OutElasticQuarter:
return Math.Pow(2, -10 * time) * Math.Sin((.25 * time - elastic_const2) * elastic_const) + 1 - elastic_offset_quarter * time;
case Easing.InOutElastic:
if ((time *= 2) < 1)
{
return -.5 * (Math.Pow(2, -10 + 10 * time) * Math.Sin((1 - elastic_const2 * 1.5 - time) * elastic_const / 1.5)
- in_out_elastic_offset * (1 - time));
}
return .5 * (Math.Pow(2, -10 * --time) * Math.Sin((time - elastic_const2 * 1.5) * elastic_const / 1.5)
- in_out_elastic_offset * time) + 1;
case Easing.InBack:
return time * time * ((back_const + 1) * time - back_const);
case Easing.OutBack:
return --time * time * ((back_const + 1) * time + back_const) + 1;
case Easing.InOutBack:
if ((time *= 2) < 1) return .5 * time * time * ((back_const2 + 1) * time - back_const2);
return .5 * ((time -= 2) * time * ((back_const2 + 1) * time + back_const2) + 2);
case Easing.InBounce:
time = 1 - time;
if (time < bounce_const)
return 1 - 7.5625 * time * time;
if (time < 2 * bounce_const)
return 1 - (7.5625 * (time -= 1.5 * bounce_const) * time + .75);
if (time < 2.5 * bounce_const)
return 1 - (7.5625 * (time -= 2.25 * bounce_const) * time + .9375);
return 1 - (7.5625 * (time -= 2.625 * bounce_const) * time + .984375);
case Easing.OutBounce:
if (time < bounce_const)
return 7.5625 * time * time;
if (time < 2 * bounce_const)
return 7.5625 * (time -= 1.5 * bounce_const) * time + .75;
if (time < 2.5 * bounce_const)
return 7.5625 * (time -= 2.25 * bounce_const) * time + .9375;
return 7.5625 * (time -= 2.625 * bounce_const) * time + .984375;
case Easing.InOutBounce:
if (time < .5) return .5 - .5 * new DefaultEasingFunction(Easing.OutBounce).ApplyEasing(1 - time * 2);
return new DefaultEasingFunction(Easing.OutBounce).ApplyEasing((time - .5) * 2) * .5 + .5;
case Easing.OutPow10:
return --time * Math.Pow(time, 10) + 1;
}
}
}
}