skia2/experimental/skottie_ios/SkTimeKeeper.h
Hal Canary 2748306830 experimental/skottie_ios/SkottieUiView
skottie_ios builds either with or without Metal.  The SkAnimationDraw
and SkTimeKeeper classes help abstract out the common elements of the
UIView and the MTKView implementations.

Cq-Include-Trybots: skia/skia.primary:Build-Mac-Clang-arm-Debug-iOS
Cq-Include-Trybots: skia/skia.primary:Build-Mac-Clang-arm-Release-iOS
Cq-Include-Trybots: skia/skia.primary:Build-Mac-Clang-arm64-Debug-iOS
Cq-Include-Trybots: skia/skia.primary:Build-Mac-Clang-arm64-Debug-iOS_Metal
Cq-Include-Trybots: skia/skia.primary:Build-Mac-Clang-arm64-Release-iOS
Cq-Include-Trybots: skia/skia.primary:Build-Mac-Clang-arm64-Release-iOS_Metal
Cq-Include-Trybots: skia/skia.primary:Build-Mac-Clang-x64-Release-iOS
Change-Id: I2c5d217b6349188f32a81e013eb29c6254428831
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/258636
Commit-Queue: Hal Canary <halcanary@google.com>
Reviewed-by: Jim Van Verth <jvanverth@google.com>
2019-12-11 23:02:00 +00:00

67 lines
1.7 KiB
C++

// Copyright 2019 Google LLC.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
#ifndef SkTimeKeeper_DEFINED
#define SkTimeKeeper_DEFINED
#include "include/core/SkTime.h"
#include <cmath>
class SkTimeKeeper {
private:
double fStartTime = 0; // used when running
float fAnimationMoment = 0; // when paused.
float fDuration = 0;
bool fPaused = false;
bool fStopAtEnd = false;
public:
void setStopAtEnd(bool s) { fStopAtEnd = s; }
float currentTime() {
if (0 == fDuration) {
return 0;
}
if (fPaused) {
return fAnimationMoment;
}
double time = 1e-9 * (SkTime::GetNSecs() - fStartTime);
if (fStopAtEnd && time >= fDuration) {
fPaused = true;
fAnimationMoment = fDuration;
return fAnimationMoment;
}
return std::fmod(time, fDuration);
}
void setDuration(float d) {
fDuration = d;
fStartTime = SkTime::GetNSecs();
fAnimationMoment = 0;
}
bool paused() const { return fPaused; }
float duration() const { return fDuration; }
void seek(float seconds) {
if (fPaused) {
fAnimationMoment = std::fmod(seconds, fDuration);
} else {
fStartTime = SkTime::GetNSecs() - 1e9 * seconds;
}
}
void togglePaused() {
if (fPaused) {
double offset = (fAnimationMoment >= fDuration) ? 0 : -1e9 * fAnimationMoment;
fStartTime = SkTime::GetNSecs() + offset;
fPaused = false;
} else {
fAnimationMoment = this->currentTime();
fPaused = true;
}
}
};
#endif // SkTimeKeeper_DEFINED