Add ability to delete a prefab item from the list

This commit is contained in:
Marcus Huderle 2022-09-09 19:37:25 -05:00
parent 96ad32c6db
commit 4232410a79
3 changed files with 26 additions and 6 deletions

View file

@ -71,7 +71,7 @@
<number>6</number>
</property>
<item>
<widget class="QPushButton" name="pushButton">
<widget class="QPushButton" name="pushButton_DeleteItem">
<property name="text">
<string/>
</property>

View file

@ -6,9 +6,11 @@
#include <QString>
#include <QLabel>
#include <QUuid>
struct PrefabItem
{
QUuid id;
QString name;
QString primaryTileset;
QString secondaryTileset;

View file

@ -72,7 +72,7 @@ void Prefab::loadPrefabs() {
selection.collisionItems[index].elevation = metatileObj["elevation"].toInt();
}
this->items.append(PrefabItem{name, primaryTileset, secondaryTileset, selection});
this->items.append(PrefabItem{QUuid::createUuid(), name, primaryTileset, secondaryTileset, selection});
}
}
@ -116,7 +116,7 @@ void Prefab::updatePrefabUi(Map *map) {
}
emptyPrefabLabel->setVisible(false);
for (auto item : this->items) {
for (auto item : prefabs) {
PrefabFrame *frame = new PrefabFrame();
frame->ui->label_Name->setText(item.name);
@ -126,18 +126,36 @@ void Prefab::updatePrefabUi(Map *map) {
frame->ui->graphicsView_Prefab->setScene(scene);
frame->ui->graphicsView_Prefab->setFixedSize(scene->itemsBoundingRect().width() + 2,
scene->itemsBoundingRect().height() + 2);
prefabWidget->layout()->addWidget(frame);
QObject::connect(frame->ui->graphicsView_Prefab, &ClickableGraphicsView::clicked, [=](){
// Clicking on the prefab graphics item selects it for painting.
QObject::connect(frame->ui->graphicsView_Prefab, &ClickableGraphicsView::clicked, [this, item](){
selector->setDirectSelection(item.selection);
});
// Clicking the delete button removes it from the list of known prefabs and updates the UI.
QObject::connect(frame->ui->pushButton_DeleteItem, &QPushButton::clicked, [this, item, map](){
for (int i = 0; i < this->items.size(); i++) {
if (this->items[i].id == item.id) {
this->items.removeAt(i);
this->updatePrefabUi(map);
break;
}
}
});
}
auto spacer = new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::Expanding);
prefabWidget->layout()->addItem(spacer);
}
void Prefab::addPrefab(MetatileSelection selection, Map *map, QString name) {
this->items.append(PrefabItem{name, map->layout->tileset_primary_label, map->layout->tileset_secondary_label, selection});
this->items.append(PrefabItem{
QUuid::createUuid(),
name,
map->layout->tileset_primary_label,
map->layout->tileset_secondary_label,
selection
});
this->updatePrefabUi(map);
}