Properly filter available prefabs based on current map's tilesets

This commit is contained in:
Marcus Huderle 2022-09-09 20:09:28 -05:00
parent 4232410a79
commit 32cba33326
3 changed files with 23 additions and 6 deletions

View file

@ -22,6 +22,7 @@ class Prefab
public:
void initPrefabUI(MetatileSelector *selector, QWidget *prefabWidget, QLabel *emptyPrefabLabel, Map *map);
void addPrefab(MetatileSelection selection, Map *map, QString name);
void updatePrefabUi(Map *map);
private:
MetatileSelector *selector;
@ -29,7 +30,6 @@ private:
QLabel *emptyPrefabLabel;
QList<PrefabItem> items;
void loadPrefabs();
void updatePrefabUi(Map *map);
QList<PrefabItem> getPrefabsForTilesets(QString primaryTileset, QString secondaryTileset);
};

View file

@ -667,6 +667,7 @@ bool MainWindow::setMap(QString map_name, bool scrollTreeView) {
updateMapList();
Scripting::cb_MapOpened(map_name);
prefab.updatePrefabUi(editor->map);
updateTilesetEditor();
return true;
}
@ -1696,6 +1697,8 @@ void MainWindow::on_mapViewTab_tabBarClicked(int index)
editor->setEditingMap();
} else if (index == 1) {
editor->setEditingCollision();
} else if (index == 2) {
editor->setEditingMap();
}
editor->setCursorRectVisible(false);
}
@ -2893,6 +2896,7 @@ void MainWindow::on_comboBox_PrimaryTileset_currentTextChanged(const QString &ti
redrawMapScene();
on_horizontalSlider_MetatileZoom_valueChanged(ui->horizontalSlider_MetatileZoom->value());
updateTilesetEditor();
prefab.updatePrefabUi(editor->map);
markMapEdited();
}
}
@ -2904,6 +2908,7 @@ void MainWindow::on_comboBox_SecondaryTileset_currentTextChanged(const QString &
redrawMapScene();
on_horizontalSlider_MetatileZoom_valueChanged(ui->horizontalSlider_MetatileZoom->value());
updateTilesetEditor();
prefab.updatePrefabUi(editor->map);
markMapEdited();
}
}

View file

@ -5,6 +5,7 @@
#include "parseutil.h"
#include "currentselectedmetatilespixmapitem.h"
#include "log.h"
#include "project.h"
#include <QJsonDocument>
#include <QJsonObject>
@ -88,17 +89,16 @@ QList<PrefabItem> Prefab::getPrefabsForTilesets(QString primaryTileset, QString
}
void Prefab::initPrefabUI(MetatileSelector *selector, QWidget *prefabWidget, QLabel *emptyPrefabLabel, Map *map) {
logInfo("initPrefabUI");
this->selector = selector;
this->prefabWidget = prefabWidget;
this->emptyPrefabLabel = emptyPrefabLabel;
this->loadPrefabs();
logInfo("initPrefabUI loaded prefabs");
this->updatePrefabUi(map);
logInfo("initPrefabUI after updatePrefabUi");
}
void Prefab::updatePrefabUi(Map *map) {
if (!this->selector)
return;
// Cleanup the PrefabFrame to have a clean slate.
auto layout = this->prefabWidget->layout();
while (layout && layout->count() > 1) {
@ -149,11 +149,23 @@ void Prefab::updatePrefabUi(Map *map) {
}
void Prefab::addPrefab(MetatileSelection selection, Map *map, QString name) {
bool usesPrimaryTileset = false;
bool usesSecondaryTileset = false;
for (auto metatile : selection.metatileItems) {
if (!metatile.enabled)
continue;
if (metatile.metatileId < Project::getNumMetatilesPrimary()) {
usesPrimaryTileset = true;
} else if (metatile.metatileId < Project::getNumMetatilesTotal()) {
usesSecondaryTileset = true;
}
}
this->items.append(PrefabItem{
QUuid::createUuid(),
name,
map->layout->tileset_primary_label,
map->layout->tileset_secondary_label,
usesPrimaryTileset ? map->layout->tileset_primary_label : "",
usesSecondaryTileset ? map->layout->tileset_secondary_label: "",
selection
});
this->updatePrefabUi(map);