make HeaderBar read block stock and expansion cost from the simulation

This commit is contained in:
2026-08-03 21:04:43 +02:00
parent 932b57720c
commit 594c3b93c5
3 changed files with 27 additions and 19 deletions

View File

@@ -17,6 +17,7 @@
#include "EventManager.h" #include "EventManager.h"
#include "IconCaption.h" #include "IconCaption.h"
#include "ItemIconCache.h" #include "ItemIconCache.h"
#include "Simulation.h"
#include "SpeedChangeRequestedEvent.h" #include "SpeedChangeRequestedEvent.h"
#include "Tick.h" #include "Tick.h"
@@ -30,11 +31,12 @@ namespace
const double HeaderBar::kSpeeds[] = { 0.0, 0.5, 1.0, 2.0, 10.0 }; const double HeaderBar::kSpeeds[] = { 0.0, 0.5, 1.0, 2.0, 10.0 };
const int HeaderBar::kSpeedCount = 5; const int HeaderBar::kSpeedCount = 5;
HeaderBar::HeaderBar(const GameConfig* config, const std::string& itemsIconDir, HeaderBar::HeaderBar(const Simulation* sim, const GameConfig* config,
QWidget* parent) const std::string& itemsIconDir, QWidget* parent)
: QWidget(parent) : QWidget(parent)
, m_itemIcons(std::make_unique<ItemIconCache>( , m_itemIcons(std::make_unique<ItemIconCache>(
QString::fromStdString(itemsIconDir))) QString::fromStdString(itemsIconDir)))
, m_sim(sim)
{ {
QHBoxLayout* layout = new QHBoxLayout(this); QHBoxLayout* layout = new QHBoxLayout(this);
layout->setContentsMargins(8, 4, 8, 4); layout->setContentsMargins(8, 4, 8, 4);
@@ -115,16 +117,14 @@ void HeaderBar::handleEvent(std::shared_ptr<const TickAdvancedEvent> event)
.arg(totalSeconds % 60, 2, 10, QChar('0'))); .arg(totalSeconds % 60, 2, 10, QChar('0')));
} }
void HeaderBar::handleEvent(std::shared_ptr<const BuildingBlocksChangedEvent> event) void HeaderBar::handleEvent(std::shared_ptr<const BuildingBlocksChangedEvent> /*event*/)
{ {
m_blocks = event->blocks;
updateBlocksLabel(); updateBlocksLabel();
updateExpandButton(); updateExpandButton();
} }
void HeaderBar::handleEvent(std::shared_ptr<const ExpansionCostChangedEvent> event) void HeaderBar::handleEvent(std::shared_ptr<const ExpansionCostChangedEvent> /*event*/)
{ {
m_expansionCost = event->cost;
updateExpandButton(); updateExpandButton();
} }
@@ -138,32 +138,37 @@ QPixmap HeaderBar::blockIcon() const
void HeaderBar::updateBlocksLabel() void HeaderBar::updateBlocksLabel()
{ {
const int blocks = m_sim->getBuildingBlocksStock();
const QPixmap icon = blockIcon(); const QPixmap icon = blockIcon();
if (icon.isNull()) if (icon.isNull())
{ {
// Fallback text form when no building_block icon exists (REQ-UI-BLOCKS-ICON). // 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; return;
} }
m_blocksLabel->setPixmap(renderCaptionWithIcon( 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))); m_blocksLabel->palette().color(QPalette::WindowText)));
} }
void HeaderBar::updateExpandButton() 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(); const QPixmap icon = blockIcon();
if (icon.isNull()) if (icon.isNull())
{ {
// Fallback text form when no building_block icon exists (REQ-UI-EXPAND-BUTTON). // Fallback text form when no building_block icon exists (REQ-UI-EXPAND-BUTTON).
m_expandButton->setIcon(QIcon()); m_expandButton->setIcon(QIcon());
m_expandButton->setText(tr("Expand: %1 Blocks").arg(m_expansionCost)); m_expandButton->setText(tr("Expand: %1 Blocks").arg(expansionCost));
return; 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 QPalette& pal = m_expandButton->palette();
const QPixmap normal = renderCaptionWithIcon( const QPixmap normal = renderCaptionWithIcon(
text, icon, m_expandButton->font(), pal.color(QPalette::ButtonText)); text, icon, m_expandButton->font(), pal.color(QPalette::ButtonText));

View File

@@ -20,6 +20,7 @@
class QLabel; class QLabel;
class QPushButton; class QPushButton;
class ItemIconCache; class ItemIconCache;
class Simulation;
class HeaderBar : public QWidget, class HeaderBar : public QWidget,
public CombinedEventHandler<TickAdvancedEvent, public CombinedEventHandler<TickAdvancedEvent,
@@ -35,8 +36,8 @@ public:
// itemsIconDir holds the per-item icon SVGs (REQ-UI-ITEM-ICON); used to show the // itemsIconDir holds the per-item icon SVGs (REQ-UI-ITEM-ICON); used to show the
// building_block icon in the stock display and expand button (REQ-UI-BLOCKS-ICON, // building_block icon in the stock display and expand button (REQ-UI-BLOCKS-ICON,
// REQ-UI-EXPAND-BUTTON). // REQ-UI-EXPAND-BUTTON).
HeaderBar(const GameConfig* config, const std::string& itemsIconDir, HeaderBar(const Simulation* sim, const GameConfig* config,
QWidget* parent = nullptr); const std::string& itemsIconDir, QWidget* parent = nullptr);
~HeaderBar() override; ~HeaderBar() override;
private slots: private slots:
@@ -54,9 +55,10 @@ private:
// expansion cost and building block stock (REQ-UI-EXPAND-BUTTON). // expansion cost and building block stock (REQ-UI-EXPAND-BUTTON).
void updateExpandButton(); void updateExpandButton();
// Refreshes the building blocks stock display from m_blocks: `Stock: <n>` with // Refreshes the building blocks stock display from the simulation:
// the building_block icon after it, or the `Stock: <n> Blocks` text fallback when // `Stock: <n>` with the building_block icon after it, or the
// no icon file exists (REQ-UI-BLOCKS-ICON). // `Stock: <n> Blocks` text fallback when no icon file exists
// (REQ-UI-BLOCKS-ICON).
void updateBlocksLabel(); void updateBlocksLabel();
// The building_block icon at the header's text height, or a null pixmap when no // The building_block icon at the header's text height, or a null pixmap when no
@@ -73,8 +75,9 @@ private:
std::unique_ptr<ItemIconCache> m_itemIcons; std::unique_ptr<ItemIconCache> m_itemIcons;
int m_blocks = 0; // The simulation is the single source of truth for the block stock and the
int m_expansionCost = 0; // expansion cost; the change events are only refresh signals.
const Simulation* m_sim;
static const double kSpeeds[]; static const double kSpeeds[];
static const int kSpeedCount; static const int kSpeedCount;

View File

@@ -47,7 +47,7 @@ MainWindow::MainWindow(Simulation* sim, const std::string& configDir,
const std::string itemsIconDir = QDir::cleanPath( const std::string itemsIconDir = QDir::cleanPath(
QString::fromStdString(m_configDir) + "/../icons/items").toStdString(); 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_gameWorldView = new GameWorldView(sim, &sim->getConfig(), &m_visuals, m_configDir,
m_replay.get(), this); m_replay.get(), this);