-
-
Save SuperWangKai/d480c48f6056d0ba69a019ae37b665d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "CallbackTimer.h" | |
#include "Urho3D/Core/Context.h" | |
#include "Urho3D/DebugNew.h" | |
CallbackTimer::CallbackTimer(Context* context) | |
: LogicComponent(context) | |
{ | |
// Only the scene update event is needed: unsubscribe from the rest for optimization | |
SetUpdateEventMask(USE_UPDATE); | |
} | |
void CallbackTimer::SetCallback(const std::function<void()>& callback, float time, bool repeat) | |
{ | |
callback_ = callback; | |
timeToRemove_ = time; | |
repeat_ = repeat; | |
currentTime_ = 0.0f; | |
} | |
void CallbackTimer::Update(float timeStep) | |
{ | |
currentTime_ += timeStep; | |
if (currentTime_ >= timeToRemove_) | |
{ | |
// Keep this object from being removed in the callback function. | |
SharedPtr<CallbackTimer> self(this); | |
// Do callback | |
callback_(); | |
if (repeat_) | |
{ | |
currentTime_ = 0.0f; | |
} | |
else | |
{ | |
Remove(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <Urho3D/Scene/LogicComponent.h> | |
using namespace Urho3D; | |
/// Callback timer. | |
class CallbackTimer : public LogicComponent | |
{ | |
URHO3D_OBJECT(CallbackTimer, LogicComponent); | |
public: | |
/// Construct. | |
explicit CallbackTimer(Context* context); | |
/// Set time before remover. | |
void SetCallback(const std::function<void()>& callback, float time, bool repeat=false); | |
/// Handle update. Called by LogicComponent base class. | |
void Update(float timeStep) override; | |
private: | |
/// Time seconds before removed. | |
float timeToRemove_ = 0.0f; | |
/// Current time seconds; | |
float currentTime_ = 0.0f; | |
/// If timer should be repeated. | |
bool repeat_ = false; | |
/// Callback function. | |
std::function<void()> callback_; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment