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... | |
Execute a callback after an interval/delay.
This class is not meant to be instantiated directly, use ime::Scene::getTimer or ime::Engine::getTimer
using ime::Timer::Callback = std::function<void(Args...)> |
|
strong |
ime::Timer::Timer | ( | ) |
Constructor.
|
static |
Create a timer.
interval | Countdown starting point |
callback | Function to execute when the timer reaches zero |
repeatCounter | The number of timer the timer should repeat |
InvalidArgumentException | if 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
|
static |
Create a timer.
interval | Countdown starting point |
callback | Function to execute when the timer reaches zero |
repeatCounter | The number of timer the timer should repeat |
InvalidArgumentException | if 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
void ime::Timer::forceTimeout | ( | ) |
int ime::Timer::getDispatchCount | ( | ) | const |
Get the number of times the callback has been invoked.
Time ime::Timer::getElapsedTime | ( | ) | const |
Get 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
Time ime::Timer::getInterval | ( | ) | const |
Get the countdown starting point.
Time ime::Timer::getRemainingDuration | ( | ) | const |
Get time remaining before the timer reaches zero.
int ime::Timer::getRepeatCount | ( | ) | const |
Get the number of times the timer restarts before coming to a stop.
-1 = The timer repeats forever 0 = The timer does not repeat after invoking the callback (default) x = The timer repeats x times before stopping
Status ime::Timer::getStatus | ( | ) | const |
float ime::Timer::getTimescale | ( | ) | const |
bool ime::Timer::isDispatched | ( | ) | const |
Check whether or not the callback is invoked.
bool ime::Timer::isLooped | ( | ) | const |
Check if the timer is looped or not.
bool ime::Timer::isPaused | ( | ) | const |
bool ime::Timer::isRepeating | ( | ) | const |
Check whether or not the timer restarts after reaching zero.
bool ime::Timer::isRunning | ( | ) | const |
bool ime::Timer::isStopped | ( | ) | const |
Add an event listener to a pause event.
callback | Function 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
Add an event listener to a restart event.
callback | Function 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
Add an event listener to a resume event.
callback | Function 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
Add an event listener to a start event.
callback | The 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
Add an event listener to a stop event.
callback | Function 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
Add the function that is executed when countdown completes.
callback | Function to be executed when the countdown terminates |
InvalidArgumentException | if the specified callback argument is a nullptr |
The callback is passed the execution timer as an argument on invocation
void ime::Timer::onTimeout | ( | const Callback<> & | callback | ) |
Add the function that is executed when countdown completes.
callback | Function to be executed when the countdown terminates |
InvalidArgumentException | if the specified callback is argument is a nullptr |
Add an event listener to an update event.
callback | Function 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
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
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
void ime::Timer::setInterval | ( | Time | interval | ) |
Set the countdown starting point.
interval | Countdown 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
void ime::Timer::setLoop | ( | bool | loop | ) |
Set whether or not the timer repeats forever.
loop | True 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
void ime::Timer::setRepeatCount | ( | int | repeatCount | ) |
Set whether or not the timer restarts after reaching zero.
repeatCount | The 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
void ime::Timer::setTimescale | ( | float | timescale | ) |
Set the timescale factor.
timescale | The new timescale factor |
A timescale controls the countdown speed of the timer and can be set to the following values:
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)
void ime::Timer::start | ( | ) |
Start the timer.
AccessViolationException | if 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
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