2018-09-07 18:20:07 +08:00
|
|
|
#include "..\e2dobject.h"
|
2018-07-05 22:05:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
e2d::Task::Task(const Function & func, const String & name)
|
2018-09-04 22:42:34 +08:00
|
|
|
: running_(true)
|
|
|
|
|
, stopped_(false)
|
|
|
|
|
, run_times_(0)
|
|
|
|
|
, total_times_(-1)
|
|
|
|
|
, delay_()
|
|
|
|
|
, callback_(func)
|
|
|
|
|
, name_(name)
|
2018-07-05 22:05:23 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-28 20:06:27 +08:00
|
|
|
e2d::Task::Task(const Function & func, float delay, int times, const String & name)
|
2018-09-04 22:42:34 +08:00
|
|
|
: running_(true)
|
|
|
|
|
, stopped_(false)
|
|
|
|
|
, run_times_(0)
|
|
|
|
|
, delay_(std::max(delay, 0.f))
|
|
|
|
|
, total_times_(times)
|
|
|
|
|
, callback_(func)
|
|
|
|
|
, name_(name)
|
2018-07-05 22:05:23 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
void e2d::Task::Start()
|
2018-07-05 22:05:23 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
running_ = true;
|
|
|
|
|
last_time_ = Time::Now();
|
2018-07-05 22:05:23 +08:00
|
|
|
}
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
void e2d::Task::Stop()
|
2018-07-05 22:05:23 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
running_ = false;
|
2018-07-05 22:05:23 +08:00
|
|
|
}
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
void e2d::Task::Update()
|
2018-07-05 22:05:23 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
if (total_times_ == 0)
|
2018-07-05 22:05:23 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
stopped_ = true;
|
2018-08-02 00:27:45 +08:00
|
|
|
return;
|
2018-07-05 22:05:23 +08:00
|
|
|
}
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
++run_times_;
|
|
|
|
|
last_time_ += delay_;
|
2018-07-05 22:05:23 +08:00
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
if (callback_)
|
2018-08-02 00:27:45 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
callback_();
|
2018-08-02 00:27:45 +08:00
|
|
|
}
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
if (run_times_ == total_times_)
|
2018-07-05 22:05:23 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
stopped_ = true;
|
2018-08-02 00:27:45 +08:00
|
|
|
return;
|
2018-07-05 22:05:23 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-07 18:20:07 +08:00
|
|
|
void e2d::Task::ResetTime()
|
|
|
|
|
{
|
|
|
|
|
last_time_ = Time::Now();
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
bool e2d::Task::IsReady() const
|
2018-07-05 22:05:23 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
if (running_)
|
2018-07-05 22:05:23 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
if (delay_.Milliseconds() == 0)
|
2018-07-05 22:05:23 +08:00
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-09-04 22:42:34 +08:00
|
|
|
if (Time::Now() - last_time_ >= delay_)
|
2018-07-05 22:05:23 +08:00
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
bool e2d::Task::IsRunning() const
|
2018-07-05 22:05:23 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
return running_;
|
2018-07-05 22:05:23 +08:00
|
|
|
}
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
const e2d::String& e2d::Task::GetName() const
|
2018-07-05 22:05:23 +08:00
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
return name_;
|
2018-07-05 22:05:23 +08:00
|
|
|
}
|