Welcome to mirror list, hosted at ThFree Co, Russian Federation.

im_tween.h « IMUI « GUI « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ccbf050ce384d33d3f29c8e8c08ec4c07a3a162b (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//-----------------------------------------------------------------------------
//           Name: im_tween.h
//      Developer: Wolfire Games LLC
//    Description: A collection of tweening functions
//        License: Read below
//-----------------------------------------------------------------------------
/*******
 *
 * Based on tween.lua by Enrique García Cota (https://github.com/kikito/tween.lua)
 *
 * Original license (for this file):
 *
 *  MIT LICENSE
 *
 *  Copyright (c) 2014 Enrique García Cota, Yuichi Tateno, Emmanuel Oga
 *
 *  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.
 *
 */
#pragma once

#include <Math/enginemath.h>
#include <cmath>

using std::pow;

// For all functions:
// time     t     running time. How much time has passed *right now*
// begin    b     starting property value
// change   c     ending - beginning
// duration d     how much time has to pass for the tweening to complete

/*******************************************************************************************/
/**
 * @brief  Base struct for all tweens
 *
 */

struct IMTween {
    int refCount;

    virtual float compute(float t, float b, float c, float d) = 0;

    // dummy functions as we're not ref counting these
    void AddRef() {
    }
    void Release() {
    }
};

/**
 * Linear
 **/

struct LinearTween : public IMTween {
    float compute(float t, float b, float c, float d) override {
        return c * t / d + b;
    }
};

/**
 * Quad
 **/

struct InQuadTween : public IMTween {
    float compute(float t, float b, float c, float d) override {
        return c * pow(t / d, 2) + b;
    }
};

struct OutQuadTween : public IMTween {
    float compute(float t, float b, float c, float d) override {
        t = t / d;
        return -c * t * (t - 2) + b;
    }
};

struct InOutQuadTween : public IMTween {
    float compute(float t, float b, float c, float d) override {
        t = t / d * 2;
        if (t < 1) {
            return c / 2 * pow(t, 2) + b;
        }
        return -c / 2 * ((t - 1) * (t - 3) - 1) + b;
    }
};

struct OutInQuadTween : public IMTween {
    InQuadTween inQuad;
    OutQuadTween outQuad;

    float compute(float t, float b, float c, float d) override {
        if (t < d / 2) {
            return outQuad.compute(t * 2, b, c / 2, d);
        }
        return inQuad.compute((t * 2) - d, b + c / 2, c / 2, d);
    }
};

/**
 * Cubic
 **/

struct InCubicTween : public IMTween {
    float compute(float t, float b, float c, float d) override {
        return c * pow(t / d, 3) + b;
    }
};

struct OutCubicTween : public IMTween {
    float compute(float t, float b, float c, float d) override {
        return c * (pow(t / d - 1, 3) + 1) + b;
    }
};

struct InOutCubicTween : public IMTween {
    float compute(float t, float b, float c, float d) override {
        t = t / d * 2;
        if (t < 1) {
            return c / 2 * t * t * t + b;
        }
        t = t - 2;
        return c / 2 * (t * t * t + 2) + b;
    }
};

struct OutInCubicTween : public IMTween {
    OutCubicTween outCubic;
    InCubicTween inCubic;

    float compute(float t, float b, float c, float d) override {
        if (t < d / 2) {
            return outCubic.compute(t * 2, b, c / 2, d);
        }
        return inCubic.compute((t * 2) - d, b + c / 2, c / 2, d);
    }
};

/**
 * Quart
 **/

struct InQuartTween : public IMTween {
    float compute(float t, float b, float c, float d) override {
        return c * pow(t / d, 4) + b;
    }
};

struct OutQuartTween : public IMTween {
    float compute(float t, float b, float c, float d) override {
        return -c * (pow(t / d - 1, 4) - 1) + b;
    }
};

struct InOutQuartTween : public IMTween {
    float compute(float t, float b, float c, float d) override {
        t = t / d * 2;
        if (t < 1) {
            return c / 2 * pow(t, 4) + b;
        }
        return -c / 2 * (pow(t - 2, 4) - 2) + b;
    }
};

struct OutInQuartTween : public IMTween {
    OutQuartTween outQuart;
    InQuartTween inQuart;

    float compute(float t, float b, float c, float d) override {
        if (t < d / 2) {
            return outQuart.compute(t * 2, b, c / 2, d);
        }
        return inQuart.compute((t * 2) - d, b + c / 2, c / 2, d);
    }
};

/**
 * Quint
 **/

struct InQuintTween : public IMTween {
    float compute(float t, float b, float c, float d) override {
        return c * pow(t / d, 5) + b;
    }
};

struct OutQuintTween : public IMTween {
    float compute(float t, float b, float c, float d) override {
        return c * (pow(t / d - 1, 5) + 1) + b;
    }
};

struct InOutQuintTween : public IMTween {
    float compute(float t, float b, float c, float d) override {
        t = t / d * 2;

        if (t < 1) {
            return c / 2 * pow(t, 5) + b;
        }

        return c / 2 * (pow(t - 2, 5) + 2) + b;
    }
};

struct OutInQuintTween : public IMTween {
    InQuintTween inQuint;
    OutQuintTween outQuint;

    float compute(float t, float b, float c, float d) override {
        if (t < d / 2) {
            return outQuint.compute(t * 2, b, c / 2, d);
        }
        return inQuint.compute((t * 2) - d, b + c / 2, c / 2, d);
    }
};

/**
 * Sine
 **/

struct InSineTween : public IMTween {
    float compute(float t, float b, float c, float d) override {
        return -c * cos(t / d * (PI_f / 2)) + c + b;
    }
};

struct OutSineTween : public IMTween {
    float compute(float t, float b, float c, float d) override {
        return c * sin(t / d * (PI_f / 2)) + b;
    }
};

struct InOutSineTween : public IMTween {
    float compute(float t, float b, float c, float d) override {
        return -c / 2 * (cos(PI_f * t / d) - 1) + b;
    }
};

struct OutInSineTween : public IMTween {
    InSineTween inSine;
    OutSineTween outSine;

    float compute(float t, float b, float c, float d) override {
        if (t < d / 2) {
            return outSine.compute(t * 2, b, c / 2, d);
        }
        return inSine.compute((t * 2) - d, b + c / 2, c / 2, d);
    }
};

/**
 * Expo
 **/

struct InExpoTween : public IMTween {
    float compute(float t, float b, float c, float d) override {
        if (t == 0) {
            return b;
        }
        return (float)(c * pow(2, 10 * (t / d - 1)) + b - c * 0.001);
    }
};

struct OutExpoTween : public IMTween {
    float compute(float t, float b, float c, float d) override {
        if (t == d) {
            return b + c;
        }

        return (float)(c * 1.001 * (-pow(2, -10 * t / d) + 1) + b);
    }
};

struct InOutExpoTween : public IMTween {
    float compute(float t, float b, float c, float d) override {
        if (t == 0) {
            return b;
        }

        if (t == d) {
            return b + c;
        }

        t = t / d * 2;

        if (t < 1) {
            return (float)(c / 2 * pow(2, 10 * (t - 1)) + b - c * 0.0005);
        }

        return (float)(c / 2 * 1.0005 * (-pow(2, -10 * (t - 1)) + 2) + b);
    }
};

struct OutInExpoTween : public IMTween {
    InExpoTween inExpo;
    OutExpoTween outExpo;

    float compute(float t, float b, float c, float d) override {
        if (t < d / 2) {
            return outExpo.compute(t * 2, b, c / 2, d);
        }

        return inExpo.compute((t * 2) - d, b + c / 2, c / 2, d);
    }
};

/**
 * Circ
 **/

struct InCircTween : public IMTween {
    float compute(float t, float b, float c, float d) override {
        return (-c * (sqrt(1 - pow(t / d, 2)) - 1) + b);
    }
};

struct OutCircTween : public IMTween {
    float compute(float t, float b, float c, float d) override {
        return (c * sqrt(1 - pow(t / d - 1, 2)) + b);
    }
};

struct InOutCircTween : public IMTween {
    float compute(float t, float b, float c, float d) override {
        t = t / d * 2;
        if (t < 1) {
            return -c / 2 * (sqrt(1 - t * t) - 1) + b;
        }
        t = t - 2;
        return c / 2 * (sqrt(1 - t * t) + 1) + b;
    }
};

struct OutInCircTween : public IMTween {
    InCircTween inCirc;
    OutCircTween outCirc;

    float compute(float t, float b, float c, float d) override {
        if (t < d / 2) {
            return outCirc.compute(t * 2, b, c / 2, d);
        }
        return inCirc.compute((t * 2) - d, b + c / 2, c / 2, d);
    }
};

/**
 * Bounce
 **/

struct OutBounceTween : public IMTween {
    float compute(float t, float b, float c, float d) override {
        t = t / d;
        if (t < 1 / 2.75f) {
            return c * (7.5625f * t * t) + b;
        }

        if (t < 2 / 2.75f) {
            t = t - (1.5f / 2.75f);
            return c * (7.5625f * t * t + 0.75f) + b;
        } else if (t < 2.5f / 2.75f) {
            t = t - (2.25f / 2.75f);
            return c * (7.5625f * t * t + 0.9375f) + b;
        }
        t = t - (2.625f / 2.75f);
        return c * (7.5625f * t * t + 0.984375f) + b;
    }
};

struct InBounceTween : public IMTween {
    OutBounceTween outBounce;

    float compute(float t, float b, float c, float d) override {
        return c - outBounce.compute(d - t, 0, c, d) + b;
    }
};

struct InOutBounceTween : public IMTween {
    OutBounceTween outBounce;
    InBounceTween inBounce;

    float compute(float t, float b, float c, float d) override {
        if (t < d / 2) {
            return inBounce.compute(t * 2, 0, c, d) * 0.5f + b;
        }
        return outBounce.compute(t * 2 - d, 0, c, d) * 0.5f + c * 0.5f + b;
    }
};

struct OutInBounceTween : public IMTween {
    InBounceTween inBounce;
    OutBounceTween outBounce;

    float compute(float t, float b, float c, float d) override {
        if (t < d / 2) {
            return outBounce.compute(t * 2, b, c / 2, d);
        }
        return inBounce.compute((t * 2) - d, b + c / 2, c / 2, d);
    }
};

/*******
 *
 * Enum type for enumerating the tweens
 *
 */
enum IMTweenType {
    linearTween,
    inQuadTween,
    outQuadTween,
    inOutQuadTween,
    outInQuadTween,
    inCubicTween,
    outCubicTween,
    inOutCubicTween,
    outInCubicTween,
    inQuartTween,
    outQuartTween,
    inOutQuartTween,
    outInQuartTween,
    inQuintTween,
    outQuintTween,
    inOutQuintTween,
    outInQuintTween,
    inSineTween,
    outSineTween,
    inOutSineTween,
    outInSineTween,
    inExpoTween,
    outExpoTween,
    inOutExpoTween,
    outInExpoTween,
    inCircTween,
    outCircTween,
    inOutCircTween,
    outInCircTween,
    outBounceTween,
    inBounceTween,
    inOutBounceTween,
    outInBounceTween
};

// Turn an instance into a pointer to a tween object
IMTween* getTweenInstance(IMTweenType tweenType);