This commit is contained in:
Nomango 2020-07-20 16:55:56 +08:00
parent f560a78fc8
commit f8dd0a8ed1
3 changed files with 38 additions and 18 deletions

View File

@ -250,11 +250,12 @@ String Duration::ToString() const
return String("0s"); return String("0s");
} }
String result; StringStream stream;
int64_t total_ms = milliseconds_; int64_t total_ms = milliseconds_;
if (total_ms < 0) if (total_ms < 0)
{ {
result.append("-"); stream << "-";
total_ms = -total_ms; total_ms = -total_ms;
} }
@ -265,23 +266,22 @@ String Duration::ToString() const
if (hour) if (hour)
{ {
result.append(std::to_string(hour)).append("h"); stream << hour << 'h' << min << 'm';
result.append(std::to_string(min)).append("m");
} }
else if (min) else if (min)
{ {
result.append(std::to_string(min)).append("m"); stream << min << 'm';
} }
if (ms != 0) if (ms != 0)
{ {
result.append(std::to_string(static_cast<float>(sec + ms) / 1000.f)).append("s"); stream << float(sec) + float(ms) / 1000.f << 's';
} }
else if (sec != 0) else if (sec != 0)
{ {
result.append(std::to_string(sec)).append("s"); stream << sec << 's';
} }
return result; return stream.str();
} }
bool Duration::operator==(const Duration& other) const bool Duration::operator==(const Duration& other) const

View File

@ -62,8 +62,31 @@ Task::Task()
{ {
} }
void Task::Start()
{
if (!running_)
{
running_ = true;
if (ticker_)
ticker_->Resume();
}
}
void Task::Stop()
{
if (running_)
{
running_ = false;
if (ticker_)
ticker_->Pause();
}
}
void Task::Update(Duration dt) void Task::Update(Duration dt)
{ {
if (!running_ || removeable_)
return;
if (!ticker_ || ticker_->GetTotalTickCount() == 0) if (!ticker_ || ticker_->GetTotalTickCount() == 0)
{ {
Remove(); Remove();

View File

@ -129,16 +129,6 @@ private:
Callback callback_; Callback callback_;
}; };
inline void Task::Start()
{
running_ = true;
}
inline void Task::Stop()
{
running_ = false;
}
inline void Task::Remove() inline void Task::Remove()
{ {
removeable_ = true; removeable_ = true;
@ -162,6 +152,13 @@ inline TickerPtr Task::GetTicker() const
inline void Task::SetTicker(TickerPtr ticker) inline void Task::SetTicker(TickerPtr ticker)
{ {
ticker_ = ticker; ticker_ = ticker;
if (ticker_)
{
if (running_)
ticker_->Resume();
else
ticker_->Pause();
}
} }
inline Task::Callback Task::GetCallback() const inline Task::Callback Task::GetCallback() const