Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
AnimTimer Class Reference

#include <AnimTimer.h>

Public Types

enum  State { kStopped_State , kPaused_State , kRunning_State }
 

Public Member Functions

 AnimTimer ()
 
State state () const
 
double nanos () const
 
float getSpeed () const
 
void setSpeed (float speed)
 
void run ()
 
void pause ()
 
void togglePauseResume ()
 
void updateTime ()
 

Detailed Description

Class to track a "timer". It supports 3 states: stopped, paused, and running. Playback speed is variable.

The caller must call updateTime() to resync with the clock (typically just before using the timer). Forcing the caller to do this ensures that the timer's return values are consistent if called repeatedly, as they only reflect the time since the last calle to updateTimer().

Definition at line 23 of file AnimTimer.h.

Member Enumeration Documentation

◆ State

Enumerator
kStopped_State 
kPaused_State 
kRunning_State 

Definition at line 30 of file AnimTimer.h.

Constructor & Destructor Documentation

◆ AnimTimer()

AnimTimer::AnimTimer ( )
inline

Class begins in the "stopped" state.

Definition at line 28 of file AnimTimer.h.

28{}

Member Function Documentation

◆ getSpeed()

float AnimTimer::getSpeed ( ) const
inline

Control the rate at which time advances.

Definition at line 39 of file AnimTimer.h.

39{ return fSpeed; }

◆ nanos()

double AnimTimer::nanos ( ) const
inline

Definition at line 34 of file AnimTimer.h.

34{ return fElapsedNanos; }

◆ pause()

void AnimTimer::pause ( )
inline

Definition at line 59 of file AnimTimer.h.

59 {
60 if (kRunning_State == this->state()) {
61 fState = kPaused_State;
62 } // else stay stopped or paused
63 }
State state() const
Definition AnimTimer.h:32

◆ run()

void AnimTimer::run ( )
inline

If the timer is paused or stopped, it will resume (or start if it was stopped).

Definition at line 45 of file AnimTimer.h.

45 {
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 }
double GetNSecs()
Definition SkTime.cpp:17

◆ setSpeed()

void AnimTimer::setSpeed ( float  speed)
inline

Definition at line 40 of file AnimTimer.h.

40{ fSpeed = speed; }

◆ state()

State AnimTimer::state ( ) const
inline

Definition at line 32 of file AnimTimer.h.

32{ return fState; }

◆ togglePauseResume()

void AnimTimer::togglePauseResume ( )
inline

If the timer is stopped, start running, else it toggles between paused and running.

Definition at line 68 of file AnimTimer.h.

68 {
69 if (kRunning_State == this->state()) {
70 this->pause();
71 } else {
72 this->run();
73 }
74 }
void pause()
Definition AnimTimer.h:59
void run()
Definition AnimTimer.h:45

◆ updateTime()

void AnimTimer::updateTime ( )
inline

Call this each time you want to sample the clock for the timer. This is NOT done automatically, so that repeated calls to msec() or secs() will always return the same value.

This may safely be called with the timer in any state.

Definition at line 83 of file AnimTimer.h.

83 {
84 if (kRunning_State == this->state()) {
85 double now = SkTime::GetNSecs();
86 fElapsedNanos += (now - fPreviousNanos) * fSpeed;
87 fPreviousNanos = now;
88 }
89 }

The documentation for this class was generated from the following file: