implement delete button per blueprint
This commit is contained in:
@@ -23,7 +23,6 @@ BlueprintPanel::BlueprintPanel(Simulation* sim, const GameConfig* config, QWidge
|
||||
, m_sim(sim)
|
||||
, m_config(config)
|
||||
, m_currentBlocks(0)
|
||||
, m_deleteMode(false)
|
||||
, m_activeIndex(-1)
|
||||
{
|
||||
QVBoxLayout* layout = new QVBoxLayout(this);
|
||||
@@ -35,11 +34,6 @@ BlueprintPanel::BlueprintPanel(Simulation* sim, const GameConfig* config, QWidge
|
||||
m_createBtn->setEnabled(false);
|
||||
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);
|
||||
scrollArea->setWidgetResizable(true);
|
||||
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||
@@ -54,7 +48,6 @@ BlueprintPanel::BlueprintPanel(Simulation* sim, const GameConfig* config, QWidge
|
||||
layout->addWidget(scrollArea, 1);
|
||||
|
||||
connect(m_createBtn, &QPushButton::clicked, this, &BlueprintPanel::onCreateClicked);
|
||||
connect(m_deleteBtn, &QPushButton::clicked, this, &BlueprintPanel::onDeleteClicked);
|
||||
|
||||
QHBoxLayout* ioLayout = new QHBoxLayout();
|
||||
m_saveBtn = new QPushButton("Save", this);
|
||||
@@ -108,39 +101,25 @@ void BlueprintPanel::onCreateClicked()
|
||||
rebuildButtons();
|
||||
}
|
||||
|
||||
void BlueprintPanel::onDeleteClicked()
|
||||
void BlueprintPanel::onDeleteBlueprintClicked(int index)
|
||||
{
|
||||
if (!m_deleteMode)
|
||||
if (m_activeIndex == index)
|
||||
{
|
||||
m_deleteMode = true;
|
||||
m_deleteBtn->setChecked(true);
|
||||
if (m_activeIndex >= 0)
|
||||
{
|
||||
clearActiveBlueprintButton();
|
||||
m_activeIndex = -1;
|
||||
emit exitBlueprintModeRequested();
|
||||
}
|
||||
}
|
||||
else
|
||||
else if (m_activeIndex > index)
|
||||
{
|
||||
m_deleteMode = false;
|
||||
m_deleteBtn->setChecked(false);
|
||||
m_activeIndex--;
|
||||
}
|
||||
m_blueprints.erase(m_blueprints.begin() + index);
|
||||
rebuildButtons();
|
||||
}
|
||||
|
||||
void BlueprintPanel::onBlueprintButtonClicked(int index)
|
||||
{
|
||||
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)
|
||||
{
|
||||
clearActiveBlueprintButton();
|
||||
@@ -231,10 +210,11 @@ int BlueprintPanel::computeBlueprintCost(const Blueprint& bp) const
|
||||
|
||||
void BlueprintPanel::rebuildButtons()
|
||||
{
|
||||
for (QPushButton* btn : m_blueprintButtons)
|
||||
while (m_buttonsLayout->count() > 1)
|
||||
{
|
||||
m_buttonsLayout->removeWidget(btn);
|
||||
delete btn;
|
||||
QLayoutItem* item = m_buttonsLayout->takeAt(0);
|
||||
if (item->widget()) { delete item->widget(); }
|
||||
delete item;
|
||||
}
|
||||
m_blueprintButtons.clear();
|
||||
|
||||
@@ -244,15 +224,31 @@ void BlueprintPanel::rebuildButtons()
|
||||
const int cost = computeBlueprintCost(bp);
|
||||
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->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;
|
||||
connect(btn, &QPushButton::clicked, this, [this, capturedIndex]() {
|
||||
onBlueprintButtonClicked(capturedIndex);
|
||||
});
|
||||
connect(delBtn, &QPushButton::clicked, this, [this, capturedIndex]() {
|
||||
onDeleteBlueprintClicked(capturedIndex);
|
||||
});
|
||||
|
||||
m_blueprintButtons.push_back(btn);
|
||||
}
|
||||
@@ -310,11 +306,6 @@ void BlueprintPanel::onLoadClicked()
|
||||
emit exitBlueprintModeRequested();
|
||||
m_activeIndex = -1;
|
||||
}
|
||||
if (m_deleteMode)
|
||||
{
|
||||
m_deleteMode = false;
|
||||
m_deleteBtn->setChecked(false);
|
||||
}
|
||||
|
||||
m_blueprints = std::move(loaded);
|
||||
rebuildButtons();
|
||||
|
||||
@@ -32,7 +32,7 @@ signals:
|
||||
|
||||
private slots:
|
||||
void onCreateClicked();
|
||||
void onDeleteClicked();
|
||||
void onDeleteBlueprintClicked(int index);
|
||||
void onBlueprintButtonClicked(int index);
|
||||
void onSaveClicked();
|
||||
void onLoadClicked();
|
||||
@@ -47,12 +47,10 @@ private:
|
||||
const GameConfig* m_config;
|
||||
std::vector<EntityId> m_selectedIds;
|
||||
int m_currentBlocks;
|
||||
bool m_deleteMode;
|
||||
int m_activeIndex;
|
||||
std::vector<Blueprint> m_blueprints;
|
||||
std::vector<QPushButton*> m_blueprintButtons;
|
||||
QPushButton* m_createBtn;
|
||||
QPushButton* m_deleteBtn;
|
||||
QPushButton* m_saveBtn;
|
||||
QPushButton* m_loadBtn;
|
||||
QWidget* m_buttonsContainer;
|
||||
|
||||
Reference in New Issue
Block a user