Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions include/mbgl/map/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ struct AnimationOptions {
/** The easing timing curve of the transition. */
optional<mbgl::util::UnitBezier> easing;

/** A function that is called on each frame of the transition, just before a
screen update, except on the last frame. The first parameter indicates
the elapsed time as a percentage of the duration. */
/** A function that is called from the Map thread on each frame of the
transition, just before a screen update, except on the last frame. The
first parameter indicates the elapsed time as a percentage of the
duration. */
std::function<void(double)> transitionFrameFn;

/** A function that is called once on the last frame of the transition, just
before the corresponding screen update. */
/** A function that is called once from the Map thread on the last frame of
the transition, just before the corresponding screen update. */
std::function<void()> transitionFinishFn;

/** Creates an animation with no options specified. */
Expand Down
36 changes: 22 additions & 14 deletions src/mbgl/map/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,26 +553,20 @@ void Transform::startTransition(const CameraOptions& camera,
const AnimationOptions& animation,
std::function<Update(double)> frame,
const Duration& duration) {
if (transitionFinishFn) {
bool isAnimated = duration != Duration::zero();
if (isAnimated && transitionFinishFn) {
transitionFinishFn();
}

bool isAnimated = duration != Duration::zero();
view.notifyMapChange(isAnimated ? MapChangeRegionWillChangeAnimated : MapChangeRegionWillChange);

// Associate the anchor, if given, with a coordinate.
PrecisionPoint anchor = camera.anchor ? *camera.anchor : PrecisionPoint(NAN, NAN);
LatLng anchorLatLng;
if (_validPoint(anchor)) {
anchor.y = state.getHeight() - anchor.y;
anchorLatLng = state.pointToLatLng(anchor);
}

transitionStart = Clock::now();
transitionDuration = duration;

transitionFrameFn = [isAnimated, animation, frame, anchor, anchorLatLng, this](const TimePoint now) {
float t = isAnimated ? (std::chrono::duration<float>(now - transitionStart) / transitionDuration) : 1.0;

auto frameFn = [animation, frame, anchor, anchorLatLng, this](double t) {
Update result;
if (t >= 1.0) {
result = frame(1.0);
Expand All @@ -584,6 +578,24 @@ void Transform::startTransition(const CameraOptions& camera,
if (_validPoint(anchor)) {
state.moveLatLng(anchorLatLng, anchor);
}
return result;
};

if (!isAnimated) {
view.notifyMapChange(MapChangeRegionWillChange);
frameFn(1.0);
view.notifyMapChange(MapChangeRegionDidChange);
return;
}

view.notifyMapChange(MapChangeRegionWillChangeAnimated);

transitionStart = Clock::now();
transitionDuration = duration;

transitionFrameFn = [isAnimated, animation, frame, anchor, anchorLatLng, frameFn, this](const TimePoint now) {
float t = isAnimated ? (std::chrono::duration<float>(now - transitionStart) / transitionDuration) : 1.0;
Update result = frameFn(t);

// At t = 1.0, a DidChangeAnimated notification should be sent from finish().
if (t < 1.0) {
Expand Down Expand Up @@ -611,10 +623,6 @@ void Transform::startTransition(const CameraOptions& camera,
}
view.notifyMapChange(isAnimated ? MapChangeRegionDidChangeAnimated : MapChangeRegionDidChange);
};

if (!isAnimated) {
transitionFrameFn(Clock::now());
}
}

bool Transform::inTransition() const {
Expand Down