atrooney-online-2/src/aoclocklabel.cpp
Crystalwarrior 510c0f4b17
Add timer packets to demo playback (#494)
When the demo skips by some number of seconds, the timer will also skip forward by that duration.

Co-authored-by: in1tiate <32779090+in1tiate@users.noreply.github.com>
Co-authored-by: oldmud0 <oldmud0@users.noreply.github.com>
2021-03-20 21:12:44 -05:00

73 lines
1.5 KiB
C++

#include "aoclocklabel.h"
AOClockLabel::AOClockLabel(QWidget *parent) : QLabel(parent) {}
void AOClockLabel::start()
{
timer.start(1000 / 60, this);
}
void AOClockLabel::start(qint64 msecs)
{
this->set(msecs);
this->start();
}
void AOClockLabel::set(qint64 msecs, bool update_text)
{
target_time = QDateTime::currentDateTime().addMSecs(msecs);
if (update_text)
{
if (QDateTime::currentDateTime() >= target_time)
{
this->setText("00:00:00.000");
}
else
{
qint64 ms_left = QDateTime::currentDateTime().msecsTo(target_time);
QTime timeleft = QTime(0, 0).addMSecs(ms_left % (1000 * 3600 * 24));
QString timestring = timeleft.toString("hh:mm:ss.zzz");
this->setText(timestring);
}
}
}
void AOClockLabel::pause()
{
timer.stop();
}
void AOClockLabel::stop()
{
this->setText("00:00:00.000");
timer.stop();
}
void AOClockLabel::skip(qint64 msecs)
{
qint64 ms_left = QDateTime::currentDateTime().msecsTo(target_time);
this->set(ms_left - msecs, true);
}
bool AOClockLabel::active()
{
return timer.isActive();
}
void AOClockLabel::timerEvent(QTimerEvent *event)
{
if (event->timerId() == timer.timerId()) {
if (QDateTime::currentDateTime() >= target_time)
{
this->stop();
return;
}
qint64 ms_left = QDateTime::currentDateTime().msecsTo(target_time);
QTime timeleft = QTime(0, 0).addMSecs(ms_left % (1000 * 3600 * 24));
QString timestring = timeleft.toString("hh:mm:ss.zzz");
this->setText(timestring);
} else {
QWidget::timerEvent(event);
}
}