368 lines
9.2 KiB
C++
368 lines
9.2 KiB
C++
#include "SelectedBuildingPanel.h"
|
|
|
|
#include <cctype>
|
|
#include <map>
|
|
#include <string>
|
|
|
|
#include <QComboBox>
|
|
#include <QLabel>
|
|
#include <QPushButton>
|
|
#include <QVBoxLayout>
|
|
|
|
#include "Building.h"
|
|
#include "BuildingSystem.h"
|
|
#include "BuildingType.h"
|
|
#include "Simulation.h"
|
|
|
|
namespace
|
|
{
|
|
|
|
QString buildingTypeName(BuildingType type)
|
|
{
|
|
const std::string id = buildingTypeId(type);
|
|
QString result;
|
|
bool nextUpper = true;
|
|
for (char c : id)
|
|
{
|
|
if (c == '_')
|
|
{
|
|
result += ' ';
|
|
nextUpper = true;
|
|
}
|
|
else if (nextUpper)
|
|
{
|
|
result += static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
|
|
nextUpper = false;
|
|
}
|
|
else
|
|
{
|
|
result += c;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
bool isProductionBuilding(BuildingType type)
|
|
{
|
|
return type == BuildingType::Miner
|
|
|| type == BuildingType::Smelter
|
|
|| type == BuildingType::Assembler
|
|
|| type == BuildingType::ReprocessingPlant
|
|
|| type == BuildingType::Shipyard;
|
|
}
|
|
|
|
bool isBeltLike(BuildingType type)
|
|
{
|
|
return type == BuildingType::Belt || type == BuildingType::Splitter;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
SelectedBuildingPanel::SelectedBuildingPanel(Simulation* sim,
|
|
const GameConfig* config,
|
|
QWidget* parent)
|
|
: QWidget(parent)
|
|
, m_sim(sim)
|
|
, m_config(config)
|
|
, m_singleId(kInvalidEntityId)
|
|
{
|
|
m_layout = new QVBoxLayout(this);
|
|
m_layout->setContentsMargins(8, 8, 8, 8);
|
|
m_layout->setSpacing(4);
|
|
m_layout->setAlignment(Qt::AlignTop);
|
|
|
|
m_titleLabel = new QLabel(this);
|
|
m_recipeCombo = new QComboBox(this);
|
|
m_clearBeltBtn = new QPushButton("Clear Items", this);
|
|
m_buffersLabel = new QLabel(this);
|
|
m_buffersLabel->setWordWrap(true);
|
|
|
|
m_layout->addWidget(m_titleLabel);
|
|
m_layout->addWidget(m_recipeCombo);
|
|
m_layout->addWidget(m_clearBeltBtn);
|
|
m_layout->addWidget(m_buffersLabel);
|
|
|
|
connect(m_recipeCombo, SIGNAL(currentIndexChanged(int)),
|
|
this, SLOT(onRecipeChanged(int)));
|
|
connect(m_clearBeltBtn, SIGNAL(clicked()),
|
|
this, SLOT(onClearBelt()));
|
|
|
|
buildEmpty();
|
|
}
|
|
|
|
void SelectedBuildingPanel::onSelectionChanged(const std::vector<EntityId>& ids)
|
|
{
|
|
m_selection = ids;
|
|
rebuild();
|
|
}
|
|
|
|
void SelectedBuildingPanel::rebuild()
|
|
{
|
|
if (m_selection.empty())
|
|
{
|
|
buildEmpty();
|
|
}
|
|
else if (m_selection.size() == 1)
|
|
{
|
|
buildSingle(m_selection[0]);
|
|
}
|
|
else
|
|
{
|
|
buildMulti(m_selection);
|
|
}
|
|
}
|
|
|
|
void SelectedBuildingPanel::buildEmpty()
|
|
{
|
|
m_singleId = kInvalidEntityId;
|
|
m_titleLabel->hide();
|
|
m_recipeCombo->hide();
|
|
m_clearBeltBtn->hide();
|
|
m_buffersLabel->hide();
|
|
}
|
|
|
|
void SelectedBuildingPanel::buildSingle(EntityId id)
|
|
{
|
|
m_singleId = id;
|
|
|
|
const Building* b = m_sim->buildings().findBuilding(id);
|
|
if (!b)
|
|
{
|
|
buildEmpty();
|
|
return;
|
|
}
|
|
|
|
m_titleLabel->setText(buildingTypeName(b->type));
|
|
m_titleLabel->show();
|
|
m_buffersLabel->show();
|
|
|
|
if (isProductionBuilding(b->type))
|
|
{
|
|
m_recipeCombo->blockSignals(true);
|
|
m_recipeCombo->clear();
|
|
|
|
m_recipeCombo->addItem("(none)", QString());
|
|
|
|
if (b->type == BuildingType::Shipyard)
|
|
{
|
|
for (const ShipDef& def : m_config->ships.ships)
|
|
{
|
|
if (m_sim->isBlueprintUnlocked(def.id))
|
|
{
|
|
m_recipeCombo->addItem(
|
|
QString::fromStdString(def.id),
|
|
QString::fromStdString(def.id));
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (const RecipeDef& recipe : m_config->recipes.recipes)
|
|
{
|
|
if (recipe.building == b->type)
|
|
{
|
|
m_recipeCombo->addItem(
|
|
QString::fromStdString(recipe.id),
|
|
QString::fromStdString(recipe.id));
|
|
}
|
|
}
|
|
}
|
|
|
|
const int currentIdx = m_recipeCombo->findData(QString::fromStdString(b->recipeId));
|
|
m_recipeCombo->setCurrentIndex(currentIdx >= 0 ? currentIdx : 0);
|
|
m_recipeCombo->blockSignals(false);
|
|
m_recipeCombo->show();
|
|
}
|
|
else
|
|
{
|
|
m_recipeCombo->hide();
|
|
}
|
|
|
|
if (isBeltLike(b->type))
|
|
{
|
|
m_clearBeltBtn->show();
|
|
}
|
|
else
|
|
{
|
|
m_clearBeltBtn->hide();
|
|
}
|
|
|
|
refreshBuffers(b);
|
|
}
|
|
|
|
void SelectedBuildingPanel::refreshBuffers(const Building* b)
|
|
{
|
|
const RecipeDef* recipe = findRecipe(b);
|
|
const ShipDef* shipDef = (b->type == BuildingType::Shipyard)
|
|
? findShipDef(b->recipeId)
|
|
: nullptr;
|
|
|
|
QString bufText;
|
|
|
|
if (!b->inputBuffer.counts.empty())
|
|
{
|
|
bufText += "Input: ";
|
|
for (const std::pair<const ItemType, int>& entry : b->inputBuffer.counts)
|
|
{
|
|
int perCycle = 0;
|
|
if (recipe)
|
|
{
|
|
for (const RecipeIngredient& ing : recipe->inputs)
|
|
{
|
|
if (ing.item == entry.first.id) { perCycle = ing.amount; break; }
|
|
}
|
|
}
|
|
else if (shipDef)
|
|
{
|
|
for (const RecipeIngredient& mat : shipDef->blueprint.materials)
|
|
{
|
|
if (mat.item == entry.first.id) { perCycle = mat.amount; break; }
|
|
}
|
|
}
|
|
bufText += QString::fromStdString(entry.first.id)
|
|
+ ": " + QString::number(entry.second);
|
|
if (perCycle > 0)
|
|
{
|
|
bufText += "/" + QString::number(perCycle);
|
|
}
|
|
bufText += " ";
|
|
}
|
|
bufText += "\n";
|
|
}
|
|
|
|
if (recipe && !recipe->outputs.empty())
|
|
{
|
|
std::map<std::string, int> outCounts;
|
|
for (const Item& item : b->outputBuffer.items)
|
|
{
|
|
outCounts[item.type.id]++;
|
|
}
|
|
bufText += "Output: ";
|
|
for (const RecipeOutput& out : recipe->outputs)
|
|
{
|
|
const std::map<std::string, int>::const_iterator it =
|
|
outCounts.find(out.item);
|
|
const int count = (it != outCounts.end()) ? it->second : 0;
|
|
bufText += QString::fromStdString(out.item)
|
|
+ ": " + QString::number(count)
|
|
+ "/" + QString::number(out.amount) + " ";
|
|
}
|
|
}
|
|
else if (!b->outputBuffer.items.empty())
|
|
{
|
|
std::map<std::string, int> outCounts;
|
|
for (const Item& item : b->outputBuffer.items)
|
|
{
|
|
outCounts[item.type.id]++;
|
|
}
|
|
bufText += "Output: ";
|
|
for (const std::pair<const std::string, int>& entry : outCounts)
|
|
{
|
|
bufText += QString::fromStdString(entry.first)
|
|
+ ": " + QString::number(entry.second) + " ";
|
|
}
|
|
}
|
|
|
|
m_buffersLabel->setText(bufText);
|
|
}
|
|
|
|
const RecipeDef* SelectedBuildingPanel::findRecipe(const Building* b) const
|
|
{
|
|
if (b->recipeId.empty()) { return nullptr; }
|
|
for (const RecipeDef& r : m_config->recipes.recipes)
|
|
{
|
|
if (r.id == b->recipeId && r.building == b->type) { return &r; }
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
const ShipDef* SelectedBuildingPanel::findShipDef(const std::string& id) const
|
|
{
|
|
if (id.empty()) { return nullptr; }
|
|
for (const ShipDef& s : m_config->ships.ships)
|
|
{
|
|
if (s.id == id) { return &s; }
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
void SelectedBuildingPanel::onStateUpdated(Tick /*tick*/, int /*blocks*/, double /*speed*/)
|
|
{
|
|
if (m_singleId == kInvalidEntityId) { return; }
|
|
const Building* b = m_sim->buildings().findBuilding(m_singleId);
|
|
if (!b)
|
|
{
|
|
buildEmpty();
|
|
return;
|
|
}
|
|
refreshBuffers(b);
|
|
}
|
|
|
|
void SelectedBuildingPanel::buildMulti(const std::vector<EntityId>& ids)
|
|
{
|
|
m_singleId = kInvalidEntityId;
|
|
m_recipeCombo->hide();
|
|
m_clearBeltBtn->hide();
|
|
m_buffersLabel->hide();
|
|
|
|
std::map<BuildingType, int> counts;
|
|
for (EntityId id : ids)
|
|
{
|
|
const Building* b = m_sim->buildings().findBuilding(id);
|
|
if (b)
|
|
{
|
|
counts[b->type]++;
|
|
}
|
|
}
|
|
|
|
bool hasBelt = false;
|
|
QString text;
|
|
for (const std::pair<const BuildingType, int>& entry : counts)
|
|
{
|
|
text += buildingTypeName(entry.first) + ": "
|
|
+ QString::number(entry.second) + "\n";
|
|
if (isBeltLike(entry.first))
|
|
{
|
|
hasBelt = true;
|
|
}
|
|
}
|
|
m_titleLabel->setText(text.trimmed());
|
|
m_titleLabel->show();
|
|
|
|
if (hasBelt)
|
|
{
|
|
m_clearBeltBtn->show();
|
|
}
|
|
}
|
|
|
|
void SelectedBuildingPanel::onRecipeChanged(int comboIndex)
|
|
{
|
|
if (m_singleId == kInvalidEntityId)
|
|
{
|
|
return;
|
|
}
|
|
const QString recipeId = m_recipeCombo->itemData(comboIndex).toString();
|
|
m_sim->buildings().setRecipe(m_singleId, recipeId.toStdString());
|
|
}
|
|
|
|
void SelectedBuildingPanel::onClearBelt()
|
|
{
|
|
std::vector<QPoint> tiles;
|
|
for (EntityId id : m_selection)
|
|
{
|
|
const Building* b = m_sim->buildings().findBuilding(id);
|
|
if (b && isBeltLike(b->type))
|
|
{
|
|
for (const QPoint& cell : b->bodyCells)
|
|
{
|
|
tiles.push_back(cell);
|
|
}
|
|
}
|
|
}
|
|
if (!tiles.empty())
|
|
{
|
|
m_sim->belts().clearTiles(tiles);
|
|
}
|
|
}
|