First Commit
This commit is contained in:
commit
3796ec5cd2
146 files changed
+4825
No files matched your search
@@ -0,0 +1,61 @@
|
||||
#include "countdown.h"
|
||||
|
||||
Countdown::Countdown(qint64 durationMs)
|
||||
: m_duration(qMax<qint64>(0, durationMs)), m_remainingAtStart(m_duration)
|
||||
{
|
||||
}
|
||||
|
||||
bool Countdown::setDuration(qint64 durationMs)
|
||||
{
|
||||
if (m_state == State::Running || m_state == State::Paused || durationMs < 0)
|
||||
return false;
|
||||
m_duration = durationMs;
|
||||
reset();
|
||||
return true;
|
||||
}
|
||||
|
||||
void Countdown::start(qint64 nowMs)
|
||||
{
|
||||
if (m_state == State::Running || m_duration <= 0)
|
||||
return;
|
||||
if (m_state != State::Paused)
|
||||
m_remainingAtStart = m_duration;
|
||||
m_segmentStarted = nowMs;
|
||||
m_state = State::Running;
|
||||
}
|
||||
|
||||
qint64 Countdown::remaining(qint64 nowMs) const
|
||||
{
|
||||
if (m_state == State::Finished)
|
||||
return 0;
|
||||
if (m_state != State::Running)
|
||||
return m_remainingAtStart;
|
||||
const qint64 elapsed = qMax<qint64>(0, nowMs - m_segmentStarted);
|
||||
return qMax<qint64>(0, m_remainingAtStart - elapsed);
|
||||
}
|
||||
|
||||
void Countdown::update(qint64 nowMs)
|
||||
{
|
||||
if (m_state == State::Running && remaining(nowMs) == 0) {
|
||||
m_remainingAtStart = 0;
|
||||
m_state = State::Finished;
|
||||
}
|
||||
}
|
||||
|
||||
void Countdown::pause(qint64 nowMs)
|
||||
{
|
||||
update(nowMs);
|
||||
if (m_state != State::Running)
|
||||
return;
|
||||
// 先保存这一刻的剩余时间,再切换状态,暂停期间不继续扣减。
|
||||
m_remainingAtStart = remaining(nowMs);
|
||||
m_state = State::Paused;
|
||||
}
|
||||
|
||||
void Countdown::reset()
|
||||
{
|
||||
m_remainingAtStart = m_duration;
|
||||
m_segmentStarted = 0;
|
||||
m_state = State::Ready;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user