summaryrefslogtreecommitdiff
path: root/include/mbgl/util/transition.hpp
blob: 8a6836c885764c878cb0a123e9f606af679facf9 (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
#ifndef MBGL_UTIL_TRANSITION
#define MBGL_UTIL_TRANSITION

#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/time.hpp>
#include <mbgl/style/types.hpp>

namespace mbgl {
namespace util {

class transition : private noncopyable {
public:
    enum state {
        running,
        complete
    };

    inline transition(timestamp start, timestamp duration)
        : start(start),
          duration(duration) {}

    inline float progress(timestamp now) const {
        if (duration == 0) return 1;
        if (start > now) return 0;

        return (float)(now - start) / duration;
    }

    virtual state update(timestamp now) const = 0;
    virtual ~transition();

protected:
    const timestamp start, duration;
};

template <typename T>
class ease_transition : public transition {
public:
    ease_transition(T from, T to, T& value, timestamp start, timestamp duration)
        : transition(start, duration),
          from(from),
          to(to),
          value(value) {}

    state update(timestamp now) const;

private:
    const T from, to;
    T& value;

};

template <typename T>
class timeout : public transition {
public:
    timeout(T final_value, T& value, timestamp start, timestamp duration)
        : transition(start, duration),
          final_value(final_value),
          value(value) {}

    state update(timestamp now) const {
        if (progress(now) >= 1) {
            value = final_value;
            return complete;
        } else {
            return running;
        }
    }

private:
    const T final_value;
    T& value;
};

}
}

#endif