1 module easings;
2 
3 /*******************************************************************************************
4 *
5 *   raylib easings (header only file)
6 *
7 *   Useful easing functions for values animation
8 *
9 *   This header uses:
10 *       #define EASINGS_STATIC_INLINE       // Inlines all functions code, so it runs faster.
11 *                                           // This requires lots of memory on system.
12 *   How to use:
13 *   The four inputs t,b,c,d are defined as follows:
14 *   t = current time (in any unit measure, but same unit as duration)
15 *   b = starting value to interpolate
16 *   c = the total change in value of b that needs to occur
17 *   d = total time it should take to complete (duration)
18 *
19 *   Example:
20 *
21 *   int currentTime = 0;
22 *   int duration = 100;
23 *   float startPositionX = 0.0f;
24 *   float finalPositionX = 30.0f;
25 *   float currentPositionX = startPositionX;
26 *
27 *   while (currentPositionX < finalPositionX)
28 *   {
29 *       currentPositionX = EaseSineIn(currentTime, startPositionX, finalPositionX - startPositionX, duration);
30 *       currentTime++;
31 *   }
32 *
33 *   A port of Robert Penner's easing equations to C (http://robertpenner.com/easing/)
34 *
35 *   Robert Penner License
36 *   ---------------------------------------------------------------------------------
37 *   Open source under the BSD License.
38 *
39 *   Copyright (c) 2001 Robert Penner. All rights reserved.
40 *
41 *   Redistribution and use in source and binary forms, with or without modification,
42 *   are permitted provided that the following conditions are met:
43 *
44 *       - Redistributions of source code must retain the above copyright notice,
45 *         this list of conditions and the following disclaimer.
46 *       - Redistributions in binary form must reproduce the above copyright notice,
47 *         this list of conditions and the following disclaimer in the documentation
48 *         and/or other materials provided with the distribution.
49 *       - Neither the name of the author nor the names of contributors may be used
50 *         to endorse or promote products derived from this software without specific
51 *         prior written permission.
52 *
53 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
54 *   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
55 *   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
56 *   IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
57 *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
58 *   BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
60 *   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
61 *   OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
62 *   OF THE POSSIBILITY OF SUCH DAMAGE.
63 *   ---------------------------------------------------------------------------------
64 *
65 *   Copyright (c) 2015 Ramon Santamaria
66 *
67 *   This software is provided "as-is", without any express or implied warranty. In no event
68 *   will the authors be held liable for any damages arising from the use of this software.
69 *
70 *   Permission is granted to anyone to use this software for any purpose, including commercial
71 *   applications, and to alter it and redistribute it freely, subject to the following restrictions:
72 *
73 *     1. The origin of this software must not be misrepresented; you must not claim that you
74 *     wrote the original software. If you use this software in a product, an acknowledgment
75 *     in the product documentation would be appreciated but is not required.
76 *
77 *     2. Altered source versions must be plainly marked as such, and must not be misrepresented
78 *     as being the original software.
79 *
80 *     3. This notice may not be removed or altered from any source distribution.
81 *
82 **********************************************************************************************/
83 
84 extern (C) @nogc nothrow:
85 pragma(inline): // NOTE: By default, compile functions as static inline
86 
87 import core.stdc.math; // Required for: sinf(), cosf(), sqrt(), pow()
88 
89 enum PI = 3.14159265358979323846f; //Required as PI is not always defined in math.h
90 
91 // Linear Easing functions
92 static float EaseLinearNone(float t, float b, float c, float d) { return (c*t/d + b); }
93 static float EaseLinearIn(float t, float b, float c, float d) { return (c*t/d + b); }
94 static float EaseLinearOut(float t, float b, float c, float d) { return (c*t/d + b); }
95 static float EaseLinearInOut(float t,float b, float c, float d) { return (c*t/d + b); }
96 
97 // Sine Easing functions
98 static float EaseSineIn(float t, float b, float c, float d) { return (-c*cosf(t/d*(PI/2.0f)) + c + b); }
99 static float EaseSineOut(float t, float b, float c, float d) { return (c*sinf(t/d*(PI/2.0f)) + b); }
100 static float EaseSineInOut(float t, float b, float c, float d) { return (-c/2.0f*(cosf(PI*t/d) - 1.0f) + b); }
101 
102 // Circular Easing functions
103 static float EaseCircIn(float t, float b, float c, float d) { t /= d; return (-c*(sqrtf(1.0f - t*t) - 1.0f) + b); }
104 static float EaseCircOut(float t, float b, float c, float d) { t = t/d - 1.0f; return (c*sqrtf(1.0f - t*t) + b); }
105 static float EaseCircInOut(float t, float b, float c, float d)
106 {
107     if ((t/=d/2.0f) < 1.0f) return (-c/2.0f*(sqrtf(1.0f - t*t) - 1.0f) + b);
108     t -= 2.0f; return (c/2.0f*(sqrtf(1.0f - t*t) + 1.0f) + b);
109 }
110 
111 // Cubic Easing functions
112 static float EaseCubicIn(float t, float b, float c, float d) { t /= d; return (c*t*t*t + b); }
113 static float EaseCubicOut(float t, float b, float c, float d) { t = t/d - 1.0f; return (c*(t*t*t + 1.0f) + b); }
114 static float EaseCubicInOut(float t, float b, float c, float d)
115 {
116     if ((t/=d/2.0f) < 1.0f) return (c/2.0f*t*t*t + b);
117     t -= 2.0f; return (c/2.0f*(t*t*t + 2.0f) + b);
118 }
119 
120 // Quadratic Easing functions
121 static float EaseQuadIn(float t, float b, float c, float d) { t /= d; return (c*t*t + b); }
122 static float EaseQuadOut(float t, float b, float c, float d) { t /= d; return (-c*t*(t - 2.0f) + b); }
123 static float EaseQuadInOut(float t, float b, float c, float d)
124 {
125     if ((t/=d/2) < 1) return (((c/2)*(t*t)) + b);
126     return (-c/2.0f*(((t - 1.0f)*(t - 3.0f)) - 1.0f) + b);
127 }
128 
129 // Exponential Easing functions
130 static float EaseExpoIn(float t, float b, float c, float d) { return (t == 0.0f) ? b : (c*powf(2.0f, 10.0f*(t/d - 1.0f)) + b); }
131 static float EaseExpoOut(float t, float b, float c, float d) { return (t == d) ? (b + c) : (c*(-powf(2.0f, -10.0f*t/d) + 1.0f) + b);    }
132 static float EaseExpoInOut(float t, float b, float c, float d)
133 {
134     if (t == 0.0f) return b;
135     if (t == d) return (b + c);
136     if ((t/=d/2.0f) < 1.0f) return (c/2.0f*powf(2.0f, 10.0f*(t - 1.0f)) + b);
137 
138     return (c/2.0f*(-powf(2.0f, -10.0f*(t - 1.0f)) + 2.0f) + b);
139 }
140 
141 // Back Easing functions
142 static float EaseBackIn(float t, float b, float c, float d)
143 {
144     float s = 1.70158f;
145     float postFix = t/=d;
146     return (c*(postFix)*t*((s + 1.0f)*t - s) + b);
147 }
148 
149 static float EaseBackOut(float t, float b, float c, float d)
150 {
151     float s = 1.70158f;
152     t = t/d - 1.0f;
153     return (c*(t*t*((s + 1.0f)*t + s) + 1.0f) + b);
154 }
155 
156 static float EaseBackInOut(float t, float b, float c, float d)
157 {
158     float s = 1.70158f;
159     if ((t/=d/2.0f) < 1.0f)
160     {
161         s *= 1.525f;
162         return (c/2.0f*(t*t*((s + 1.0f)*t - s)) + b);
163     }
164 
165     float postFix = t-=2.0f;
166     s *= 1.525f;
167     return (c/2.0f*((postFix)*t*((s + 1.0f)*t + s) + 2.0f) + b);
168 }
169 
170 // Bounce Easing functions
171 static float EaseBounceOut(float t, float b, float c, float d)
172 {
173     if ((t/=d) < (1.0f/2.75f))
174     {
175         return (c*(7.5625f*t*t) + b);
176     }
177     else if (t < (2.0f/2.75f))
178     {
179         float postFix = t-=(1.5f/2.75f);
180         return (c*(7.5625f*(postFix)*t + 0.75f) + b);
181     }
182     else if (t < (2.5/2.75))
183     {
184         float postFix = t-=(2.25f/2.75f);
185         return (c*(7.5625f*(postFix)*t + 0.9375f) + b);
186     }
187     else
188     {
189         float postFix = t-=(2.625f/2.75f);
190         return (c*(7.5625f*(postFix)*t + 0.984375f) + b);
191     }
192 }
193 
194 static float EaseBounceIn(float t, float b, float c, float d) { return (c - EaseBounceOut(d - t, 0.0f, c, d) + b); }
195 static float EaseBounceInOut(float t, float b, float c, float d)
196 {
197     if (t < d/2.0f) return (EaseBounceIn(t*2.0f, 0.0f, c, d)*0.5f + b);
198     else return (EaseBounceOut(t*2.0f - d, 0.0f, c, d)*0.5f + c*0.5f + b);
199 }
200 
201 // Elastic Easing functions
202 static float EaseElasticIn(float t, float b, float c, float d)
203 {
204     if (t == 0.0f) return b;
205     if ((t/=d) == 1.0f) return (b + c);
206 
207     float p = d*0.3f;
208     float a = c;
209     float s = p/4.0f;
210     float postFix = a*powf(2.0f, 10.0f*(t-=1.0f));
211 
212     return (-(postFix*sinf((t*d-s)*(2.0f*PI)/p )) + b);
213 }
214 
215 static float EaseElasticOut(float t, float b, float c, float d)
216 {
217     if (t == 0.0f) return b;
218     if ((t/=d) == 1.0f) return (b + c);
219 
220     float p = d*0.3f;
221     float a = c;
222     float s = p/4.0f;
223 
224     return (a*powf(2.0f,-10.0f*t)*sinf((t*d-s)*(2.0f*PI)/p) + c + b);
225 }
226 
227 static float EaseElasticInOut(float t, float b, float c, float d)
228 {
229     if (t == 0.0f) return b;
230     if ((t/=d/2.0f) == 2.0f) return (b + c);
231 
232     float p = d*(0.3f*1.5f);
233     float a = c;
234     float s = p/4.0f;
235 
236     if (t < 1.0f)
237     {
238         float postFix = a*powf(2.0f, 10.0f*(t-=1.0f));
239         return -0.5f*(postFix*sinf((t*d-s)*(2.0f*PI)/p)) + b;
240     }
241 
242     float postFix = a*powf(2.0f, -10.0f*(t-=1.0f));
243 
244     return (postFix*sinf((t*d-s)*(2.0f*PI)/p)*0.5f + c + b);
245 }