implement delete button per blueprint

This commit is contained in:
2026-04-29 23:02:07 +02:00
parent ad49daa1f6
commit 1e7f602865
2 changed files with 31 additions and 42 deletions

View File

@@ -23,7 +23,6 @@ BlueprintPanel::BlueprintPanel(Simulation* sim, const GameConfig* config, QWidge
, m_sim(sim) , m_sim(sim)
, m_config(config) , m_config(config)
, m_currentBlocks(0) , m_currentBlocks(0)
, m_deleteMode(false)
, m_activeIndex(-1) , m_activeIndex(-1)
{ {
QVBoxLayout* layout = new QVBoxLayout(this); QVBoxLayout* layout = new QVBoxLayout(this);
@@ -35,11 +34,6 @@ BlueprintPanel::BlueprintPanel(Simulation* sim, const GameConfig* config, QWidge
m_createBtn->setEnabled(false); m_createBtn->setEnabled(false);
layout->addWidget(m_createBtn); layout->addWidget(m_createBtn);
m_deleteBtn = new QPushButton("Delete Blueprint", this);
m_deleteBtn->setFixedHeight(48);
m_deleteBtn->setCheckable(true);
layout->addWidget(m_deleteBtn);
QScrollArea* scrollArea = new QScrollArea(this); QScrollArea* scrollArea = new QScrollArea(this);
scrollArea->setWidgetResizable(true); scrollArea->setWidgetResizable(true);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
@@ -54,7 +48,6 @@ BlueprintPanel::BlueprintPanel(Simulation* sim, const GameConfig* config, QWidge
layout->addWidget(scrollArea, 1); layout->addWidget(scrollArea, 1);
connect(m_createBtn, &QPushButton::clicked, this, &BlueprintPanel::onCreateClicked); connect(m_createBtn, &QPushButton::clicked, this, &BlueprintPanel::onCreateClicked);
connect(m_deleteBtn, &QPushButton::clicked, this, &BlueprintPanel::onDeleteClicked);
QHBoxLayout* ioLayout = new QHBoxLayout(); QHBoxLayout* ioLayout = new QHBoxLayout();
m_saveBtn = new QPushButton("Save", this); m_saveBtn = new QPushButton("Save", this);
@@ -108,39 +101,25 @@ void BlueprintPanel::onCreateClicked()
rebuildButtons(); rebuildButtons();
} }
void BlueprintPanel::onDeleteClicked() void BlueprintPanel::onDeleteBlueprintClicked(int index)
{ {
if (!m_deleteMode) if (m_activeIndex == index)
{ {
m_deleteMode = true; m_activeIndex = -1;
m_deleteBtn->setChecked(true); emit exitBlueprintModeRequested();
if (m_activeIndex >= 0)
{
clearActiveBlueprintButton();
emit exitBlueprintModeRequested();
}
} }
else else if (m_activeIndex > index)
{ {
m_deleteMode = false; m_activeIndex--;
m_deleteBtn->setChecked(false);
} }
m_blueprints.erase(m_blueprints.begin() + index);
rebuildButtons();
} }
void BlueprintPanel::onBlueprintButtonClicked(int index) void BlueprintPanel::onBlueprintButtonClicked(int index)
{ {
if (index < 0 || index >= static_cast<int>(m_blueprints.size())) { return; } if (index < 0 || index >= static_cast<int>(m_blueprints.size())) { return; }
if (m_deleteMode)
{
m_blueprints.erase(m_blueprints.begin() + index);
m_deleteMode = false;
m_deleteBtn->setChecked(false);
m_activeIndex = -1;
rebuildButtons();
return;
}
if (m_activeIndex == index) if (m_activeIndex == index)
{ {
clearActiveBlueprintButton(); clearActiveBlueprintButton();
@@ -231,10 +210,11 @@ int BlueprintPanel::computeBlueprintCost(const Blueprint& bp) const
void BlueprintPanel::rebuildButtons() void BlueprintPanel::rebuildButtons()
{ {
for (QPushButton* btn : m_blueprintButtons) while (m_buttonsLayout->count() > 1)
{ {
m_buttonsLayout->removeWidget(btn); QLayoutItem* item = m_buttonsLayout->takeAt(0);
delete btn; if (item->widget()) { delete item->widget(); }
delete item;
} }
m_blueprintButtons.clear(); m_blueprintButtons.clear();
@@ -244,15 +224,31 @@ void BlueprintPanel::rebuildButtons()
const int cost = computeBlueprintCost(bp); const int cost = computeBlueprintCost(bp);
const QString label = bp.name + "\n" + QString::number(cost) + " Blocks"; const QString label = bp.name + "\n" + QString::number(cost) + " Blocks";
QPushButton* btn = new QPushButton(label, m_buttonsContainer); QWidget* row = new QWidget(m_buttonsContainer);
QHBoxLayout* rowLayout = new QHBoxLayout(row);
rowLayout->setContentsMargins(0, 0, 0, 0);
rowLayout->setSpacing(4);
QPushButton* btn = new QPushButton(label, row);
btn->setCheckable(true); btn->setCheckable(true);
btn->setFixedHeight(48); btn->setFixedHeight(48);
m_buttonsLayout->insertWidget(i, btn);
QPushButton* delBtn = new QPushButton("\xc3\x97", row);
delBtn->setFixedWidth(28);
delBtn->setFixedHeight(48);
rowLayout->addWidget(btn, 1);
rowLayout->addWidget(delBtn, 0);
m_buttonsLayout->insertWidget(i, row);
const int capturedIndex = i; const int capturedIndex = i;
connect(btn, &QPushButton::clicked, this, [this, capturedIndex]() { connect(btn, &QPushButton::clicked, this, [this, capturedIndex]() {
onBlueprintButtonClicked(capturedIndex); onBlueprintButtonClicked(capturedIndex);
}); });
connect(delBtn, &QPushButton::clicked, this, [this, capturedIndex]() {
onDeleteBlueprintClicked(capturedIndex);
});
m_blueprintButtons.push_back(btn); m_blueprintButtons.push_back(btn);
} }
@@ -310,11 +306,6 @@ void BlueprintPanel::onLoadClicked()
emit exitBlueprintModeRequested(); emit exitBlueprintModeRequested();
m_activeIndex = -1; m_activeIndex = -1;
} }
if (m_deleteMode)
{
m_deleteMode = false;
m_deleteBtn->setChecked(false);
}
m_blueprints = std::move(loaded); m_blueprints = std::move(loaded);
rebuildButtons(); rebuildButtons();

View File

@@ -32,7 +32,7 @@ signals:
private slots: private slots:
void onCreateClicked(); void onCreateClicked();
void onDeleteClicked(); void onDeleteBlueprintClicked(int index);
void onBlueprintButtonClicked(int index); void onBlueprintButtonClicked(int index);
void onSaveClicked(); void onSaveClicked();
void onLoadClicked(); void onLoadClicked();
@@ -47,12 +47,10 @@ private:
const GameConfig* m_config; const GameConfig* m_config;
std::vector<EntityId> m_selectedIds; std::vector<EntityId> m_selectedIds;
int m_currentBlocks; int m_currentBlocks;
bool m_deleteMode;
int m_activeIndex; int m_activeIndex;
std::vector<Blueprint> m_blueprints; std::vector<Blueprint> m_blueprints;
std::vector<QPushButton*> m_blueprintButtons; std::vector<QPushButton*> m_blueprintButtons;
QPushButton* m_createBtn; QPushButton* m_createBtn;
QPushButton* m_deleteBtn;
QPushButton* m_saveBtn; QPushButton* m_saveBtn;
QPushButton* m_loadBtn; QPushButton* m_loadBtn;
QWidget* m_buttonsContainer; QWidget* m_buttonsContainer;