From 594c3b93c59766472167b9ae89a4dfe1797b5848 Mon Sep 17 00:00:00 2001 From: mlangkabel Date: Mon, 3 Aug 2026 21:04:43 +0200 Subject: [PATCH] make HeaderBar read block stock and expansion cost from the simulation --- src/ui/HeaderBar.cpp | 27 ++++++++++++++++----------- src/ui/HeaderBar.h | 17 ++++++++++------- src/ui/MainWindow.cpp | 2 +- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/ui/HeaderBar.cpp b/src/ui/HeaderBar.cpp index b034626..c221082 100644 --- a/src/ui/HeaderBar.cpp +++ b/src/ui/HeaderBar.cpp @@ -17,6 +17,7 @@ #include "EventManager.h" #include "IconCaption.h" #include "ItemIconCache.h" +#include "Simulation.h" #include "SpeedChangeRequestedEvent.h" #include "Tick.h" @@ -30,11 +31,12 @@ namespace const double HeaderBar::kSpeeds[] = { 0.0, 0.5, 1.0, 2.0, 10.0 }; const int HeaderBar::kSpeedCount = 5; -HeaderBar::HeaderBar(const GameConfig* config, const std::string& itemsIconDir, - QWidget* parent) +HeaderBar::HeaderBar(const Simulation* sim, const GameConfig* config, + const std::string& itemsIconDir, QWidget* parent) : QWidget(parent) , m_itemIcons(std::make_unique( QString::fromStdString(itemsIconDir))) + , m_sim(sim) { QHBoxLayout* layout = new QHBoxLayout(this); layout->setContentsMargins(8, 4, 8, 4); @@ -115,16 +117,14 @@ void HeaderBar::handleEvent(std::shared_ptr event) .arg(totalSeconds % 60, 2, 10, QChar('0'))); } -void HeaderBar::handleEvent(std::shared_ptr event) +void HeaderBar::handleEvent(std::shared_ptr /*event*/) { - m_blocks = event->blocks; updateBlocksLabel(); updateExpandButton(); } -void HeaderBar::handleEvent(std::shared_ptr event) +void HeaderBar::handleEvent(std::shared_ptr /*event*/) { - m_expansionCost = event->cost; updateExpandButton(); } @@ -138,32 +138,37 @@ QPixmap HeaderBar::blockIcon() const void HeaderBar::updateBlocksLabel() { + const int blocks = m_sim->getBuildingBlocksStock(); + const QPixmap icon = blockIcon(); if (icon.isNull()) { // Fallback text form when no building_block icon exists (REQ-UI-BLOCKS-ICON). - m_blocksLabel->setText(tr("Stock: %1 Blocks").arg(m_blocks)); + m_blocksLabel->setText(tr("Stock: %1 Blocks").arg(blocks)); return; } m_blocksLabel->setPixmap(renderCaptionWithIcon( - tr("Stock: %1").arg(m_blocks), icon, font(), + tr("Stock: %1").arg(blocks), icon, font(), m_blocksLabel->palette().color(QPalette::WindowText))); } void HeaderBar::updateExpandButton() { - m_expandButton->setEnabled(m_blocks >= m_expansionCost); + const int blocks = m_sim->getBuildingBlocksStock(); + const int expansionCost = m_sim->getCurrentExpansionCost(); + + m_expandButton->setEnabled(blocks >= expansionCost); const QPixmap icon = blockIcon(); if (icon.isNull()) { // Fallback text form when no building_block icon exists (REQ-UI-EXPAND-BUTTON). m_expandButton->setIcon(QIcon()); - m_expandButton->setText(tr("Expand: %1 Blocks").arg(m_expansionCost)); + m_expandButton->setText(tr("Expand: %1 Blocks").arg(expansionCost)); return; } - const QString text = tr("Expand: %1").arg(m_expansionCost); + const QString text = tr("Expand: %1").arg(expansionCost); const QPalette& pal = m_expandButton->palette(); const QPixmap normal = renderCaptionWithIcon( text, icon, m_expandButton->font(), pal.color(QPalette::ButtonText)); diff --git a/src/ui/HeaderBar.h b/src/ui/HeaderBar.h index 865c0e7..118e298 100644 --- a/src/ui/HeaderBar.h +++ b/src/ui/HeaderBar.h @@ -20,6 +20,7 @@ class QLabel; class QPushButton; class ItemIconCache; +class Simulation; class HeaderBar : public QWidget, public CombinedEventHandler` with - // the building_block icon after it, or the `Stock: Blocks` text fallback when - // no icon file exists (REQ-UI-BLOCKS-ICON). + // Refreshes the building blocks stock display from the simulation: + // `Stock: ` with the building_block icon after it, or the + // `Stock: Blocks` text fallback when no icon file exists + // (REQ-UI-BLOCKS-ICON). void updateBlocksLabel(); // The building_block icon at the header's text height, or a null pixmap when no @@ -73,8 +75,9 @@ private: std::unique_ptr m_itemIcons; - int m_blocks = 0; - int m_expansionCost = 0; + // The simulation is the single source of truth for the block stock and the + // expansion cost; the change events are only refresh signals. + const Simulation* m_sim; static const double kSpeeds[]; static const int kSpeedCount; diff --git a/src/ui/MainWindow.cpp b/src/ui/MainWindow.cpp index f572a29..ae97fc8 100644 --- a/src/ui/MainWindow.cpp +++ b/src/ui/MainWindow.cpp @@ -47,7 +47,7 @@ MainWindow::MainWindow(Simulation* sim, const std::string& configDir, const std::string itemsIconDir = QDir::cleanPath( QString::fromStdString(m_configDir) + "/../icons/items").toStdString(); - m_headerBar = new HeaderBar(&sim->getConfig(), itemsIconDir, this); + m_headerBar = new HeaderBar(sim, &sim->getConfig(), itemsIconDir, this); m_gameWorldView = new GameWorldView(sim, &sim->getConfig(), &m_visuals, m_configDir, m_replay.get(), this);