74 lines
2.0 KiB
C++
74 lines
2.0 KiB
C++
#include "HeaderBar.h"
|
|
|
|
#include <cmath>
|
|
#include <string>
|
|
|
|
#include <QHBoxLayout>
|
|
#include <QLabel>
|
|
#include <QPushButton>
|
|
#include <QSignalMapper>
|
|
|
|
#include "Tick.h"
|
|
|
|
const double HeaderBar::kSpeeds[] = { 0.0, 0.5, 1.0, 2.0, 4.0 };
|
|
const int HeaderBar::kSpeedCount = 5;
|
|
|
|
HeaderBar::HeaderBar(QWidget* parent)
|
|
: QWidget(parent)
|
|
{
|
|
QHBoxLayout* layout = new QHBoxLayout(this);
|
|
layout->setContentsMargins(8, 4, 8, 4);
|
|
layout->setSpacing(8);
|
|
|
|
m_timeLabel = new QLabel("00:00", this);
|
|
m_blocksLabel = new QLabel("Blocks: 0", this);
|
|
layout->addWidget(m_timeLabel);
|
|
layout->addWidget(m_blocksLabel);
|
|
layout->addStretch();
|
|
|
|
const char* labels[] = { "0x", "0.5x", "1x", "2x", "4x" };
|
|
QSignalMapper* mapper = new QSignalMapper(this);
|
|
for (int i = 0; i < kSpeedCount; ++i)
|
|
{
|
|
QPushButton* btn = new QPushButton(labels[i], this);
|
|
btn->setCheckable(true);
|
|
btn->setChecked(i == 2);
|
|
layout->addWidget(btn);
|
|
m_speedButtons.push_back(btn);
|
|
mapper->setMapping(btn, i);
|
|
connect(btn, &QPushButton::clicked, mapper, qOverload<>(&QSignalMapper::map));
|
|
}
|
|
connect(mapper, qOverload<int>(&QSignalMapper::mapped), this, &HeaderBar::onSpeedButton);
|
|
|
|
setFixedHeight(sizeHint().height());
|
|
}
|
|
|
|
void HeaderBar::onStateUpdated(Tick tick, int buildingBlocks, double gameSpeed)
|
|
{
|
|
const int totalSeconds = static_cast<int>(ticksToSeconds(tick));
|
|
const int minutes = totalSeconds / 60;
|
|
const int seconds = totalSeconds % 60;
|
|
|
|
m_timeLabel->setText(
|
|
QString("%1:%2")
|
|
.arg(minutes, 2, 10, QChar('0'))
|
|
.arg(seconds, 2, 10, QChar('0')));
|
|
|
|
m_blocksLabel->setText(QString("Blocks: %1").arg(buildingBlocks));
|
|
|
|
for (int i = 0; i < kSpeedCount; ++i)
|
|
{
|
|
const bool active = (std::abs(kSpeeds[i] - gameSpeed) < 0.001);
|
|
m_speedButtons[static_cast<std::size_t>(i)]->setChecked(active);
|
|
}
|
|
}
|
|
|
|
void HeaderBar::onSpeedButton(int index)
|
|
{
|
|
if (index >= 0 && index < kSpeedCount)
|
|
{
|
|
emit speedChanged(kSpeeds[index]);
|
|
}
|
|
}
|
|
|