blob: 060de00c2e997ec702738de1325dc31194bceabb [file] [log] [blame]
reed76113a92015-02-02 12:55:02 -08001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkTime.h"
9
10#ifndef SkAnimTimer_DEFINED
11#define SkAnimTimer_DEFINED
12
13/**
14 * Class to track a "timer". It supports 3 states: stopped, paused, running.
15 *
16 * The caller must call updateTime() to resync with the clock (typically just before
17 * using the timer). Forcing the caller to do this ensures that the timer's return values
18 * are consistent if called repeatedly, as they only reflect the time since the last
19 * calle to updateTimer().
20 */
21class SkAnimTimer {
22public:
23 enum State {
24 kStopped_State,
25 kPaused_State,
26 kRunning_State
27 };
28
29 /**
30 * Class begins in the "stopped" state.
31 */
benjaminwagnerec4d4d72016-03-25 12:59:53 -070032 SkAnimTimer() : fBaseTimeNanos(0), fCurrTimeNanos(0), fState(kStopped_State) {}
reed76113a92015-02-02 12:55:02 -080033
34 bool isStopped() const { return kStopped_State == fState; }
35 bool isRunning() const { return kRunning_State == fState; }
36 bool isPaused() const { return kPaused_State == fState; }
37
38 /**
39 * Stops the timer, and resets it, such that the next call to run or togglePauseResume
40 * will begin at time 0.
41 */
42 void stop() {
43 this->setState(kStopped_State);
44 }
45
46 /**
47 * If the timer is paused or stopped, it will resume (or start if it was stopped).
48 */
49 void run() {
50 this->setState(kRunning_State);
51 }
52
53 /**
54 * If the timer is stopped, this has no effect, else it toggles between paused and running.
55 */
56 void togglePauseResume() {
57 if (kRunning_State == fState) {
58 this->setState(kPaused_State);
59 } else {
60 this->setState(kRunning_State);
61 }
62 }
63
64 /**
65 * Call this each time you want to sample the clock for the timer. This is NOT done
66 * automatically, so that repeated calls to msec() or secs() will always return the
67 * same value.
68 *
69 * This may safely be called with the timer in any state.
70 */
71 void updateTime() {
72 if (kRunning_State == fState) {
benjaminwagnerec4d4d72016-03-25 12:59:53 -070073 fCurrTimeNanos = SkTime::GetNSecs();
reed76113a92015-02-02 12:55:02 -080074 }
75 }
76
77 /**
78 * Return the time in milliseconds the timer has been in the running state.
benjaminwagnerec4d4d72016-03-25 12:59:53 -070079 * Returns 0 if the timer is stopped. Behavior is undefined if the timer
80 * has been running longer than SK_MSecMax.
reed76113a92015-02-02 12:55:02 -080081 */
benjaminwagnerec4d4d72016-03-25 12:59:53 -070082 SkMSec msec() const {
83 const double msec = (fCurrTimeNanos - fBaseTimeNanos) * 1e-6;
84 SkASSERT(SK_MSecMax >= msec);
85 return static_cast<SkMSec>(msec);
86 }
reed76113a92015-02-02 12:55:02 -080087
88 /**
89 * Return the time in seconds the timer has been in the running state.
90 * Returns 0 if the timer is stopped.
91 */
benjaminwagnerec4d4d72016-03-25 12:59:53 -070092 double secs() const { return (fCurrTimeNanos - fBaseTimeNanos) * 1e-9; }
reed76113a92015-02-02 12:55:02 -080093
94 /**
95 * Return the time in seconds the timer has been in the running state,
96 * scaled by "speed" and (if not zero) mod by period.
97 * Returns 0 if the timer is stopped.
98 */
99 SkScalar scaled(SkScalar speed, SkScalar period = 0) const {
100 double value = this->secs() * speed;
101 if (period) {
102 value = ::fmod(value, SkScalarToDouble(period));
103 }
104 return SkDoubleToScalar(value);
105 }
106
bsalomonc67bb572016-05-13 13:48:48 -0700107 /**
108 * Transitions from ends->mid->ends linearly over period seconds. The phase specifies a phase
109 * shift in seconds.
110 */
111 SkScalar pingPong(SkScalar period, SkScalar phase, SkScalar ends, SkScalar mid) const {
112 return PingPong(this->secs(), period, phase, ends, mid);
113 }
114
115 /** Helper for computing a ping-pong value without a SkAnimTimer object. */
116 static SkScalar PingPong(double t, SkScalar period, SkScalar phase, SkScalar ends,
117 SkScalar mid) {
118 double value = ::fmod(t + phase, period);
119 double half = period / 2.0;
120 double diff = ::fabs(value - half);
121 return SkDoubleToScalar(ends + (1.0 - diff / half) * (mid - ends));
122 }
123
reed76113a92015-02-02 12:55:02 -0800124private:
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700125 double fBaseTimeNanos;
126 double fCurrTimeNanos;
reed76113a92015-02-02 12:55:02 -0800127 State fState;
128
129 void setState(State newState) {
130 switch (newState) {
131 case kStopped_State:
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700132 fBaseTimeNanos = fCurrTimeNanos = 0;
reed76113a92015-02-02 12:55:02 -0800133 fState = kStopped_State;
134 break;
135 case kPaused_State:
136 if (kRunning_State == fState) {
137 fState = kPaused_State;
138 } // else stay stopped or paused
139 break;
140 case kRunning_State:
141 switch (fState) {
142 case kStopped_State:
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700143 fBaseTimeNanos = fCurrTimeNanos = SkTime::GetNSecs();
reed76113a92015-02-02 12:55:02 -0800144 break;
145 case kPaused_State: {// they want "resume"
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700146 double now = SkTime::GetNSecs();
147 fBaseTimeNanos += now - fCurrTimeNanos;
148 fCurrTimeNanos = now;
reed76113a92015-02-02 12:55:02 -0800149 } break;
150 case kRunning_State:
151 break;
152 }
153 fState = kRunning_State;
154 break;
155 }
156 }
157};
158
159#endif