fixed memory leak
[platform/core/location/maps-plugin-mapzen.git] / src / mapzen / tangram / ease.h
1 #pragma once
2
3 #include "tangram.h"
4 #include <cmath>
5 #include <functional>
6
7 namespace Tangram {
8
9 using EaseCb = std::function<void (float)>;
10
11 template<typename T>
12 T ease(T _start, T _end, float _t, EaseType _e) {
13     float f = _t;
14     switch (_e) {
15         case EaseType::cubic: f = (-2 * f + 3) * f * f; break;
16         case EaseType::quint: f = (6 * f * f - 15 * f + 10) * f * f * f; break;
17         case EaseType::sine: f = 0.5 - 0.5 * cos(M_PI * f); break;
18         default: break;
19     }
20     return _start + (_end - _start) * f;
21 }
22
23 struct Ease {
24     float t;
25     float d;
26     EaseCb cb;
27
28     Ease() : t(0), d(0), cb([](float) {}) {}
29     Ease(float _duration, EaseCb _cb) : t(-1), d(_duration), cb(_cb) {}
30
31     bool finished() const { return t >= d; }
32
33     void update(float _dt) {
34         t = t < 0 ? 0 : std::fmin(t + _dt, d);
35         cb(std::fmin(1, t / d));
36     }
37 };
38
39 }