summaryrefslogtreecommitdiff
path: root/src/renderer/frame_history.cpp
blob: 8b69162a23100e25a620a92363249e2bcf06dba1 (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
#include <mbgl/renderer/frame_history.hpp>

using namespace mbgl;

// Record frame history that will be used to calculate fading params
void FrameHistory::record(timestamp now, float zoom) {
    // first frame ever
    if (!history.size()) {
        history.emplace_back(FrameSnapshot{0, zoom});
        history.emplace_back(FrameSnapshot{0, zoom});
    }

    if (history.size() > 0 || history.back().z != zoom) {
        history.emplace_back(FrameSnapshot{now, zoom});
    }
}

bool FrameHistory::needsAnimation(const timestamp duration) const {
    if (!history.size()) {
        return false;
    }

    // If we have a value that is older than duration and whose z value is the
    // same as the most current z value, and if all values inbetween have the
    // same z value, we don't need animation, otherwise we probably do.
    const FrameSnapshot &pivot = history.back();

    int i = -1;
    while ((int)history.size() > i + 1 && history[i + 1].t + duration < pivot.t) {
        i++;
    }

    if (i < 0) {
        // There is no frame that is older than the duration time, so we need to
        // check all frames.
        i = 0;
    }

    // Make sure that all subsequent snapshots have the same zoom as the last
    // pivot element.
    for (; (int)history.size() > i; i++) {
        if (history[i].z != pivot.z) {
            return true;
        }
    }

    return false;
}

FadeProperties FrameHistory::getFadeProperties(timestamp duration)
{
    const timestamp currentTime = util::now();

    // Remove frames until only one is outside the duration, or until there are only three
    while (history.size() > 3 && history[1].t + duration < currentTime) {
        history.pop_front();
    }

    if (history[1].t + duration < currentTime) {
        history[0].z = history[1].z;
    }

    // Find the range of zoom levels we want to fade between
    float startingZ = history.front().z;
    const FrameSnapshot lastFrame = history.back();
    float endingZ = lastFrame.z;
    float lowZ = std::fmin(startingZ, endingZ);
    float highZ = std::fmax(startingZ, endingZ);

    // Calculate the speed of zooming, and how far it would zoom in terms of zoom levels in one
    // duration
    float zoomDiff = endingZ - history[1].z, timeDiff = lastFrame.t - history[1].t;
    float fadedist = zoomDiff / (timeDiff / duration);

    // At end of a zoom when the zoom stops changing continue pretending to zoom at that speed
    // bump is how much farther it would have been if it had continued zooming at the same rate
    float bump = (currentTime - lastFrame.t) / duration * fadedist;

    return FadeProperties {
        fadedist,
        lowZ,
        highZ,
        bump
    };
}