make BlueprintPanel read the block stock from the simulation
BlueprintPanel was the second panel caching BuildingBlocksChangedEvent's payload as truth; it already held a Simulation*, so refreshButtonStates() now re-reads getBuildingBlocksStock() at the point of use, per the "events are refresh signals" rule. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YHcUerKAZKWNvSKJxYKbnG
This commit is contained in:
@@ -27,7 +27,6 @@ BlueprintPanel::BlueprintPanel(Simulation* sim, const GameConfig* config, QWidge
|
||||
: QWidget(parent)
|
||||
, m_sim(sim)
|
||||
, m_config(config)
|
||||
, m_currentBlocks(0)
|
||||
{
|
||||
QVBoxLayout* layout = new QVBoxLayout(this);
|
||||
layout->setContentsMargins(4, 4, 4, 4);
|
||||
@@ -71,9 +70,8 @@ void BlueprintPanel::onSelectionChanged(const std::vector<BuildingId>& ids)
|
||||
refreshButtonStates();
|
||||
}
|
||||
|
||||
void BlueprintPanel::handleEvent(std::shared_ptr<const BuildingBlocksChangedEvent> event)
|
||||
void BlueprintPanel::handleEvent(std::shared_ptr<const BuildingBlocksChangedEvent> /*event*/)
|
||||
{
|
||||
m_currentBlocks = event->blocks;
|
||||
refreshButtonStates();
|
||||
}
|
||||
|
||||
@@ -247,10 +245,12 @@ void BlueprintPanel::refreshButtonStates()
|
||||
// A construction site counts the same as an operational building (REQ-UI-BLUEPRINT-CREATE).
|
||||
m_createBtn->setEnabled(selectionHasPlaceableBuilding(*m_sim, m_selectedBuildingIds));
|
||||
|
||||
const int blocks = m_sim->getBuildingBlocksStock();
|
||||
|
||||
for (int i = 0; i < static_cast<int>(m_blueprintButtons.size()); ++i)
|
||||
{
|
||||
const int cost = computeBlueprintCost(m_blueprints[static_cast<std::size_t>(i)]);
|
||||
const bool canAfford = m_currentBlocks >= cost;
|
||||
const bool canAfford = blocks >= cost;
|
||||
m_blueprintButtons[static_cast<std::size_t>(i)]->setEnabled(
|
||||
canAfford || m_activeIndex == i);
|
||||
}
|
||||
|
||||
@@ -53,10 +53,11 @@ private:
|
||||
void loadFromDisk();
|
||||
void saveToDisk() const;
|
||||
|
||||
// The simulation is the single source of truth for the block stock; the
|
||||
// change event is only a refresh signal.
|
||||
Simulation* m_sim;
|
||||
const GameConfig* m_config;
|
||||
std::vector<BuildingId> m_selectedBuildingIds;
|
||||
int m_currentBlocks;
|
||||
std::optional<int> m_activeIndex; // nullopt = no blueprint selected
|
||||
std::vector<Blueprint> m_blueprints;
|
||||
std::vector<QPushButton*> m_blueprintButtons;
|
||||
|
||||
Reference in New Issue
Block a user