Advanced timer with the ability to trigger function calls. More...
Public Member Functions | |
CallbackTimer () | |
Default constructor: Creates a callback timer that is initially expired. More... | |
virtual void | reset (sf::Time timeLimit) |
Resets the timer's remaining time to the given limit and stops it. More... | |
virtual void | restart (sf::Time timeLimit) |
Resets the timer's remaining time to the given limit and starts it again. More... | |
void | update () |
Trigger listeners, if expired. More... | |
Connection | connect (std::function< void(CallbackTimer &)> unaryListener) |
Registers a unary function which will be called when the time reaches zero. More... | |
Connection | connect0 (std::function< void()> nullaryListener) |
Registers a nullary function which will be called when the time reaches zero. More... | |
void | clearConnections () |
Removes all currently associated timer listeners. More... | |
sf::Time | getRemainingTime () const |
Returns the remaining time. More... | |
bool | isRunning () const |
Checks if the timer is currently running. More... | |
bool | isExpired () const |
Checks if the timer has expired yet. More... | |
void | start () |
Starts or continues the timer. More... | |
void | stop () |
Pauses the timer. More... | |
Advanced timer with the ability to trigger function calls.
Clock class that counts time down. As an extension of Timer, this class is able to register functions that are called at expiration time. When you use this class, make sure you invoke the update() method every frame. Possible usage:
thor::CallbackTimer::CallbackTimer | ( | ) |
void thor::CallbackTimer::clearConnections | ( | ) |
Removes all currently associated timer listeners.
This also invalidates all connections to those listeners.
Connection thor::CallbackTimer::connect | ( | std::function< void(CallbackTimer &)> | unaryListener | ) |
Registers a unary function which will be called when the time reaches zero.
Make sure to call update() each frame to invoke potential listeners. Use this function if your callback should get the timer as a parameter.
unaryListener | The function you want to associate with the timer expiration. The first parameter is a reference to the CallbackTimer instance that just expired. |
Connection thor::CallbackTimer::connect0 | ( | std::function< void()> | nullaryListener | ) |
Registers a nullary function which will be called when the time reaches zero.
Make sure to call update() each frame to invoke potential listeners. Use this function if you don't care about the timer that fired the callback, and thus don't need a parameter for it in your callback.
nullaryListener | The function you want to associate with the timer expiration. |
|
inherited |
Returns the remaining time.
If the timer has expired, sf::Time::Zero is returned.
|
inherited |
Checks if the timer has expired yet.
Timers expire when their remaining time reaches zero.
|
inherited |
Checks if the timer is currently running.
As soon as the timer expires, it stops running.
|
virtual |
Resets the timer's remaining time to the given limit and stops it.
timeLimit | The new time limit (must be greater than zero). |
In contrast to restart(), the timer is not running after the call.
Reimplemented from thor::Timer.
|
virtual |
Resets the timer's remaining time to the given limit and starts it again.
timeLimit | The new time limit (must be greater than zero). |
The behavior is equivalent to reset(timeLimit) followed by start().
Reimplemented from thor::Timer.
|
inherited |
Starts or continues the timer.
If it is already running, nothing happens.
|
inherited |
Pauses the timer.
If it is already paused, nothing happens.
void thor::CallbackTimer::update | ( | ) |
Trigger listeners, if expired.
This is the most important function of this class. You should call update() every frame (or, even better, let this an automated routine do) to assure that all associated listeners are invoked when the timer expires. If you restart the timer, call update() before restarting; otherwise the listeners won't fire.