Public Types | Public Member Functions | Static Public Member Functions | List of all members
ime::Timer Class Reference

Execute a callback after an interval/delay. More...

#include <Timer.h>

Public Types

enum class  Status { Running , Paused , Stopped }
 States the timer can be in. More...
 
template<typename... Args>
using Callback = std::function< void(Args...)>
 Event listener. More...
 
using Ptr = std::shared_ptr< Timer >
 

Public Member Functions

 Timer ()
 Constructor. More...
 
void setInterval (Time interval)
 Set the countdown starting point. More...
 
Time getInterval () const
 Get the countdown starting point. More...
 
Time getElapsedTime () const
 Get the time elapsed since the timer was started. More...
 
Time getRemainingDuration () const
 Get time remaining before the timer reaches zero. More...
 
void setRepeatCount (int repeatCount)
 Set whether or not the timer restarts after reaching zero. More...
 
int getRepeatCount () const
 Get the number of times the timer restarts before coming to a stop. More...
 
bool isRepeating () const
 Check whether or not the timer restarts after reaching zero. More...
 
void setLoop (bool loop)
 Set whether or not the timer repeats forever. More...
 
bool isLooped () const
 Check if the timer is looped or not. More...
 
void start ()
 Start the timer. More...
 
void stop ()
 Stop the timer. More...
 
void forceTimeout ()
 Stop the timer. More...
 
void pause ()
 Stop the timer without resetting the remaining duration. More...
 
void resume ()
 Resume a paused timer. More...
 
void restart ()
 Restart the countdown. More...
 
Status getStatus () const
 Get the current status of the timer (running, paused, stopped) More...
 
bool isRunning () const
 Check if the timer is running or not. More...
 
bool isPaused () const
 Check if the timer is paused or not. More...
 
bool isStopped () const
 Check if the timer is stopped or not. More...
 
int getDispatchCount () const
 Get the number of times the callback has been invoked. More...
 
bool isDispatched () const
 Check whether or not the callback is invoked. More...
 
void setTimescale (float timescale)
 Set the timescale factor. More...
 
float getTimescale () const
 Get the timescale factor. More...
 
void onTimeout (const Callback<> &callback)
 Add the function that is executed when countdown completes. More...
 
void onTimeout (const Callback< Timer & > &callback)
 Add the function that is executed when countdown completes. More...
 
void onStart (const Callback< Timer & > &callback)
 Add an event listener to a start event. More...
 
void onPause (const Callback< Timer & > &callback)
 Add an event listener to a pause event. More...
 
void onResume (const Callback< Timer & > &callback)
 Add an event listener to a resume event. More...
 
void onStop (const Callback< Timer & > &callback)
 Add an event listener to a stop event. More...
 
void onRestart (const Callback< Timer & > &callback)
 Add an event listener to a restart event. More...
 
void onUpdate (const Callback< Timer & > &callback)
 Add an event listener to an update event. More...
 
void update (Time deltaTime)
 

Static Public Member Functions

static Timer::Ptr create (Time interval, const Callback<> &callback, int repeatCounter=0)
 Create a timer. More...
 
static Timer::Ptr create (Time interval, const Callback< Timer & > &callback, int repeatCounter=0)
 Create a timer. More...
 

Detailed Description

Execute a callback after an interval/delay.

This class is not meant to be instantiated directly, use ime::Scene::getTimer or ime::Engine::getTimer

Definition at line 40 of file Timer.h.

Member Typedef Documentation

◆ Callback

template<typename... Args>
using ime::Timer::Callback = std::function<void(Args...)>

Event listener.

Definition at line 43 of file Timer.h.

◆ Ptr

using ime::Timer::Ptr = std::shared_ptr<Timer>

Definition at line 48 of file Timer.h.

Member Enumeration Documentation

◆ Status

enum class ime::Timer::Status
strong

States the timer can be in.

Enumerator
Running 

Timer is running.

Paused 

Timer is paused.

Stopped 

Timer is stopped.

Definition at line 54 of file Timer.h.

Constructor & Destructor Documentation

◆ Timer()

ime::Timer::Timer ( )

Constructor.

Member Function Documentation

◆ create() [1/2]

static Timer::Ptr ime::Timer::create ( Time  interval,
const Callback< Timer & > &  callback,
int  repeatCounter = 0 
)
static

Create a timer.

Parameters
intervalCountdown starting point
callbackFunction to execute when the timer reaches zero
repeatCounterThe number of timer the timer should repeat
Returns
The new timer
Exceptions
InvalidArgumentExceptionif the specified callback argument is a nullptr

The timer is not repeated be default (repeatCounter = 0), this means that the timer will stop after invoking the callback

Note
The timer is not started after creation, start function must be called on the returned timer when it is ready to be started
See also
start and setRepeatCount

◆ create() [2/2]

static Timer::Ptr ime::Timer::create ( Time  interval,
const Callback<> &  callback,
int  repeatCounter = 0 
)
static

Create a timer.

Parameters
intervalCountdown starting point
callbackFunction to execute when the timer reaches zero
repeatCounterThe number of timer the timer should repeat
Returns
The new timer
Exceptions
InvalidArgumentExceptionif the specified callback argument is a nullptr

The timer is not repeated be default (repeatCounter = 0), this means that the timer will stop after invoking the callback

Note
The timer is not started after creation, start function must be called on the returned timer when it is ready to be started
See also
start and setRepeatCount

◆ forceTimeout()

void ime::Timer::forceTimeout ( )

Stop the timer.

This function resets the remaining duration to the value of the interval and stops the timer. However unlike stop(), the timeout callback is executed

See also
stop

◆ getDispatchCount()

int ime::Timer::getDispatchCount ( ) const

Get the number of times the callback has been invoked.

Returns
The number of times the callback has been invoked

◆ getElapsedTime()

Time ime::Timer::getElapsedTime ( ) const

Get the time elapsed since the timer was started.

Returns
The time elapsed since the timer was started

Note that the elapsed time is reset to ime::Time::Zero after each invocation of the callback or when the timer is stopped before its expiry time

See also
start, getInterval and getRemainingDuration

◆ getInterval()

Time ime::Timer::getInterval ( ) const

Get the countdown starting point.

Returns
The countdown starting point
See also
setInterval, getElapsedTime

◆ getRemainingDuration()

Time ime::Timer::getRemainingDuration ( ) const

Get time remaining before the timer reaches zero.

Returns
The time remaining before the timer reaches zero
See also
getElapsedTime and getInterval

◆ getRepeatCount()

int ime::Timer::getRepeatCount ( ) const

Get the number of times the timer restarts before coming to a stop.

Returns
The number of times the timer is restarts after counting down

-1 = The timer repeats forever 0 = The timer does not repeat after invoking the callback (default) x = The timer repeats x times before stopping

◆ getStatus()

Status ime::Timer::getStatus ( ) const

Get the current status of the timer (running, paused, stopped)

Returns
The current status of the timer

By default, the timer is stopped

See also
isRunning, isPaused, isStopped

◆ getTimescale()

float ime::Timer::getTimescale ( ) const

Get the timescale factor.

Returns
The timescale factor
See also
setTimescale

◆ isDispatched()

bool ime::Timer::isDispatched ( ) const

Check whether or not the callback is invoked.

Returns
True if the callback was called, otherwise false

◆ isLooped()

bool ime::Timer::isLooped ( ) const

Check if the timer is looped or not.

Returns
True if the timer is looped, otherwise false
See also
setLoop

◆ isPaused()

bool ime::Timer::isPaused ( ) const

Check if the timer is paused or not.

Returns
True if the timer is paused, otherwise false
See also
isRunning, isStopped, getStatus

◆ isRepeating()

bool ime::Timer::isRepeating ( ) const

Check whether or not the timer restarts after reaching zero.

Returns
True if the timer restarts, otherwise false
See also
setRepeatCount

◆ isRunning()

bool ime::Timer::isRunning ( ) const

Check if the timer is running or not.

Returns
True if the timer is running, otherwise false
See also
isPaused, isStopped, getStatus

◆ isStopped()

bool ime::Timer::isStopped ( ) const

Check if the timer is stopped or not.

Returns
True if the timer is stopped, otherwise false
See also
isRunning, isPaused, getStatus

◆ onPause()

void ime::Timer::onPause ( const Callback< Timer & > &  callback)

Add an event listener to a pause event.

Parameters
callbackFunction to be executed when the timer is paused

By default, there is no callback registered to this event. Pass nullptr to remove any registered event listener. In addition, note that adding a new event listener removes the previous event listener

See also
pause, onStart, onStop and onRestart

◆ onRestart()

void ime::Timer::onRestart ( const Callback< Timer & > &  callback)

Add an event listener to a restart event.

Parameters
callbackFunction to be executed when the timer is restarted

By default, there is no callback registered to this event. Pass nullptr to remove any registered event listener. In addition, note that adding a new event listener removes the previous event listener

See also
restart, onStart, onPause and onStop

◆ onResume()

void ime::Timer::onResume ( const Callback< Timer & > &  callback)

Add an event listener to a resume event.

Parameters
callbackFunction to be executed when the timer is resumed

By default, there is no callback registered to this event. Pass nullptr to remove any registered event listener. In addition, note that adding a new event listener removes the previous event listener

See also
pause, onStart, onStop and onRestart

◆ onStart()

void ime::Timer::onStart ( const Callback< Timer & > &  callback)

Add an event listener to a start event.

Parameters
callbackThe function to be executed when the timer is started

This event is triggered when the timer is started from a stopped state

By default, there is no callback registered to this event. Pass nullptr to remove any registered event listener. In addition, note that adding a new event listener removes the previous event listener

See also
start, onStop, onPause and onRestart

◆ onStop()

void ime::Timer::onStop ( const Callback< Timer & > &  callback)

Add an event listener to a stop event.

Parameters
callbackFunction to b execute when the timer is stopped

By default, there is no callback registered to this event. Pass nullptr to remove any registered event listener. In addition, note that adding a new event listener removes the previous event listener

See also
stop, onStart, onPause and onRestart

◆ onTimeout() [1/2]

void ime::Timer::onTimeout ( const Callback< Timer & > &  callback)

Add the function that is executed when countdown completes.

Parameters
callbackFunction to be executed when the countdown terminates
Exceptions
InvalidArgumentExceptionif the specified callback argument is a nullptr

The callback is passed the execution timer as an argument on invocation

See also
start

◆ onTimeout() [2/2]

void ime::Timer::onTimeout ( const Callback<> &  callback)

Add the function that is executed when countdown completes.

Parameters
callbackFunction to be executed when the countdown terminates
Exceptions
InvalidArgumentExceptionif the specified callback is argument is a nullptr
See also
start

◆ onUpdate()

void ime::Timer::onUpdate ( const Callback< Timer & > &  callback)

Add an event listener to an update event.

Parameters
callbackFunction to be executed when the timer ticks

This event is triggered every time the timer advances/updates. Only one event listener may be registered at a time. Pass nullptr to remove the current event listener

See also
onStart, onPause and onStop, onRestart

◆ pause()

void ime::Timer::pause ( )

Stop the timer without resetting the remaining duration.

When the timer is paused it can be resumed by calling the start() or resume() functions. The timer will begin the countdown from the remaining duration instead of restarting from the interval

See also
onPause, start and setInterval

◆ restart()

void ime::Timer::restart ( )

Restart the countdown.

Unlike stop, this function stops the timer and immediately starts it. However, the start and stop events will not be triggered, only the restart event. The countdown will start from the interval and the remaining duration will be reset to the value of the interval

See also
onRestart, setInterval

◆ resume()

void ime::Timer::resume ( )

Resume a paused timer.

See also
onResume, pause and start

◆ setInterval()

void ime::Timer::setInterval ( Time  interval)

Set the countdown starting point.

Parameters
intervalCountdown starting point

if the interval is set while the timer is running and the interval is not zero, then the timer will restart. If the interval is less than zero, it will be set to zero and the timer will stop immediately without executing the callback if it was running

See also
restart

◆ setLoop()

void ime::Timer::setLoop ( bool  loop)

Set whether or not the timer repeats forever.

Parameters
loopTrue to loop timer, otherwise false

This function is a simplification of setRepeatCount(). When the loop argument is true, the repeat count will be set to -1 and when it is false, the repeat count will be set to zero

See also
setRepeatCount, isLooped

◆ setRepeatCount()

void ime::Timer::setRepeatCount ( int  repeatCount)

Set whether or not the timer restarts after reaching zero.

Parameters
repeatCountThe number of times the timer repeats

Pass -1 to repeat the timer indefinitely or 0 to stop the repetition if the timer is currently repeating

By default, the repeat counter is 0, this means that the timer stops after invoking the callback for the first time

Note
If the repetition is cancelled while the timer is running, the timer will continue execution and stop immediately after executing the callback
See also
stop and setInterval

◆ setTimescale()

void ime::Timer::setTimescale ( float  timescale)

Set the timescale factor.

Parameters
timescaleThe new timescale factor

A timescale controls the countdown speed of the timer and can be set to the following values:

  • 1 = Normal/Real-time countdown
  • < 1 = Slower countdown
  • > 1 = Faster countdown

For example, a timescale of 2.0f will make the timer countdown twice as fast whilst a timescale of 0.5f will make the timer countdown at half its normal countdown speed.

Note that zero or negative timescale values will be ignored

By default the timescale is 1.0f (real-time)

See also
getTimescale

◆ start()

void ime::Timer::start ( )

Start the timer.

Exceptions
AccessViolationExceptionif this function is called before the timeout callback is set

This function will start the timer if it was not started or resume it if it was paused. If start() is called while the timer is running then, the timer will restart

Note
A Timer needs a timeout callback before it can be started. Attempting to start the timer without registering a timeout callback first will trigger a ime::AccessViolationException exception, see onTimeout()
See also
onStart, setInterval, onTimeout, restart and pause

◆ stop()

void ime::Timer::stop ( )

Stop the timer.

This function resets the remaining duration to the value of the interval and stops the timer without executing the callback. This behavior is similar to the restart function except the timer is not immediately started after it has been stooped. The function start must be called to restart the timer after it has been stopped

See also
onStop, forceTimeout, start and getRemainingDuration

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