Flutter Engine
The Flutter Engine
third_party
skia
tools
viewer
AnimTimer.h
Go to the documentation of this file.
1
/*
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 "
include/core/SkScalar.h
"
9
#include "
src/base/SkTime.h
"
10
11
#ifndef AnimTimer_DEFINED
12
#define AnimTimer_DEFINED
13
14
/**
15
* Class to track a "timer". It supports 3 states: stopped, paused, and running.
16
* Playback speed is variable.
17
*
18
* The caller must call updateTime() to resync with the clock (typically just before
19
* using the timer). Forcing the caller to do this ensures that the timer's return values
20
* are consistent if called repeatedly, as they only reflect the time since the last
21
* calle to updateTimer().
22
*/
23
class
AnimTimer
{
24
public
:
25
/**
26
* Class begins in the "stopped" state.
27
*/
28
AnimTimer
() {}
29
30
enum
State
{
kStopped_State
,
kPaused_State
,
kRunning_State
};
31
32
State
state
()
const
{
return
fState; }
33
34
double
nanos
()
const
{
return
fElapsedNanos; }
35
36
/**
37
* Control the rate at which time advances.
38
*/
39
float
getSpeed
()
const
{
return
fSpeed; }
40
void
setSpeed
(
float
speed) { fSpeed = speed; }
41
42
/**
43
* If the timer is paused or stopped, it will resume (or start if it was stopped).
44
*/
45
void
run
() {
46
switch
(this->
state
()) {
47
case
kStopped_State
:
48
fPreviousNanos =
SkTime::GetNSecs
();
49
fElapsedNanos = 0;
50
break
;
51
case
kPaused_State
:
// they want "resume"
52
fPreviousNanos =
SkTime::GetNSecs
();
53
break
;
54
case
kRunning_State
:
break
;
55
}
56
fState =
kRunning_State
;
57
}
58
59
void
pause
() {
60
if
(
kRunning_State
== this->
state
()) {
61
fState =
kPaused_State
;
62
}
// else stay stopped or paused
63
}
64
65
/**
66
* If the timer is stopped, start running, else it toggles between paused and running.
67
*/
68
void
togglePauseResume
() {
69
if
(
kRunning_State
== this->
state
()) {
70
this->
pause
();
71
}
else
{
72
this->
run
();
73
}
74
}
75
76
/**
77
* Call this each time you want to sample the clock for the timer. This is NOT done
78
* automatically, so that repeated calls to msec() or secs() will always return the
79
* same value.
80
*
81
* This may safely be called with the timer in any state.
82
*/
83
void
updateTime
() {
84
if
(
kRunning_State
== this->
state
()) {
85
double
now =
SkTime::GetNSecs
();
86
fElapsedNanos += (now - fPreviousNanos) * fSpeed;
87
fPreviousNanos = now;
88
}
89
}
90
91
private
:
92
double
fPreviousNanos = 0;
93
double
fElapsedNanos = 0;
94
float
fSpeed = 1;
95
State
fState =
kStopped_State
;
96
};
97
98
#endif
SkScalar.h
SkTime.h
AnimTimer
Definition:
AnimTimer.h:23
AnimTimer::togglePauseResume
void togglePauseResume()
Definition:
AnimTimer.h:68
AnimTimer::state
State state() const
Definition:
AnimTimer.h:32
AnimTimer::pause
void pause()
Definition:
AnimTimer.h:59
AnimTimer::nanos
double nanos() const
Definition:
AnimTimer.h:34
AnimTimer::getSpeed
float getSpeed() const
Definition:
AnimTimer.h:39
AnimTimer::kPaused_State
@ kPaused_State
Definition:
AnimTimer.h:30
AnimTimer::kStopped_State
@ kStopped_State
Definition:
AnimTimer.h:30
AnimTimer::kRunning_State
@ kRunning_State
Definition:
AnimTimer.h:30
AnimTimer::AnimTimer
AnimTimer()
Definition:
AnimTimer.h:28
AnimTimer::updateTime
void updateTime()
Definition:
AnimTimer.h:83
AnimTimer::setSpeed
void setSpeed(float speed)
Definition:
AnimTimer.h:40
AnimTimer::run
void run()
Definition:
AnimTimer.h:45
SkTime::GetNSecs
double GetNSecs()
Definition:
SkTime.cpp:17
State
Definition:
SerialProcsTest.cpp:46
Generated on Sun Jun 23 2024 21:56:51 for Flutter Engine by
1.9.4