Set shared metatile labels as placeholders

This commit is contained in:
GriffinR 2023-02-14 22:44:13 -05:00
parent 16e5e9fad8
commit 44dba9edc5
4 changed files with 50 additions and 28 deletions

View file

@ -7,6 +7,11 @@
#include <QImage>
#include <QHash>
struct MetatileLabelPair {
QString normal;
QString shared;
};
class Tileset
{
public:
@ -36,8 +41,9 @@ public:
static Tileset* getMetatileTileset(int, Tileset*, Tileset*);
static Tileset* getTileTileset(int, Tileset*, Tileset*);
static Metatile* getMetatile(int, Tileset*, Tileset*);
static Tileset* getMetatileLabelTileset(int, Tileset*, Tileset*, bool * isShared = nullptr);
static QString getMetatileLabel(int, Tileset *, Tileset *, bool * isShared = nullptr);
static Tileset* getMetatileLabelTileset(int, Tileset*, Tileset*);
static QString getMetatileLabel(int, Tileset *, Tileset *);
static MetatileLabelPair getMetatileLabelPair(int metatileId, Tileset *primaryTileset, Tileset *secondaryTileset);
static bool setMetatileLabel(int, QString, Tileset *, Tileset *);
QString getMetatileLabelPrefix();
static QString getMetatileLabelPrefix(const QString &name);

View file

@ -94,10 +94,9 @@ Metatile* Tileset::getMetatile(int metatileId, Tileset *primaryTileset, Tileset
// Metatile labels are stored per-tileset. When looking for a metatile label, first search in the tileset
// that the metatile belongs to. If one isn't found, search in the other tileset. Labels coming from the
// tileset that the metatile does not belong to are shared and cannot be edited via Porymap.
Tileset* Tileset::getMetatileLabelTileset(int metatileId, Tileset *primaryTileset, Tileset *secondaryTileset, bool * isShared) {
Tileset* Tileset::getMetatileLabelTileset(int metatileId, Tileset *primaryTileset, Tileset *secondaryTileset) {
Tileset *mainTileset = nullptr;
Tileset *alternateTileset = nullptr;
if (isShared) *isShared = false;
if (metatileId < Project::getNumMetatilesPrimary()) {
mainTileset = primaryTileset;
alternateTileset = secondaryTileset;
@ -106,22 +105,42 @@ Tileset* Tileset::getMetatileLabelTileset(int metatileId, Tileset *primaryTilese
alternateTileset = primaryTileset;
}
if (mainTileset && mainTileset->metatileLabels.contains(metatileId)) {
if (mainTileset && !mainTileset->metatileLabels.value(metatileId).isEmpty()) {
return mainTileset;
} else if (alternateTileset && alternateTileset->metatileLabels.contains(metatileId)) {
if (isShared) *isShared = true;
} else if (alternateTileset && !alternateTileset->metatileLabels.value(metatileId).isEmpty()) {
return alternateTileset;
}
return nullptr;
}
QString Tileset::getMetatileLabel(int metatileId, Tileset *primaryTileset, Tileset *secondaryTileset, bool * isShared) {
Tileset * tileset = Tileset::getMetatileLabelTileset(metatileId, primaryTileset, secondaryTileset, isShared);
// If the metatile has a label in the tileset it belongs to, return that label.
// If it doesn't, and the metatile has a label in the other tileset, return that label.
// Otherwise return an empty string.
QString Tileset::getMetatileLabel(int metatileId, Tileset *primaryTileset, Tileset *secondaryTileset) {
Tileset * tileset = Tileset::getMetatileLabelTileset(metatileId, primaryTileset, secondaryTileset);
if (!tileset)
return QString();
return tileset->metatileLabels.value(metatileId);
}
// Return the pair of possible metatile labels for the specified metatile.
// "normal" is the label for the tileset to which the metatile belongs.
// "shared" is the label for the tileset to which the metatile does not belong.
MetatileLabelPair Tileset::getMetatileLabelPair(int metatileId, Tileset *primaryTileset, Tileset *secondaryTileset) {
MetatileLabelPair labels;
QString primaryMetatileLabel = primaryTileset ? primaryTileset->metatileLabels.value(metatileId) : "";
QString secondaryMetatileLabel = secondaryTileset ? secondaryTileset->metatileLabels.value(metatileId) : "";
if (metatileId < Project::getNumMetatilesPrimary()) {
labels.normal = primaryMetatileLabel;
labels.shared = secondaryMetatileLabel;
} else if (metatileId < Project::getNumMetatilesTotal()) {
labels.normal = secondaryMetatileLabel;
labels.shared = primaryMetatileLabel;
}
return labels;
}
bool Tileset::setMetatileLabel(int metatileId, QString label, Tileset *primaryTileset, Tileset *secondaryTileset) {
Tileset *tileset = Tileset::getMetatileTileset(metatileId, primaryTileset, secondaryTileset);
if (!tileset)

View file

@ -914,12 +914,16 @@ void Project::saveTilesetMetatileLabels(Tileset *primaryTileset, Tileset *second
// Add the new labels.
for (int metatileId : primaryTileset->metatileLabels.keys()) {
QString defineName = QString("%1%2").arg(primaryPrefix, primaryTileset->metatileLabels.value(metatileId));
QString label = primaryTileset->metatileLabels.value(metatileId);
if (label.isEmpty()) continue;
QString defineName = QString("%1%2").arg(primaryPrefix, label);
defines.insert(defineName, metatileId);
definesFileModified = true;
}
for (int metatileId : secondaryTileset->metatileLabels.keys()) {
QString defineName = QString("%1%2").arg(secondaryPrefix, secondaryTileset->metatileLabels.value(metatileId));
QString label = secondaryTileset->metatileLabels.value(metatileId);
if (label.isEmpty()) continue;
QString defineName = QString("%1%2").arg(secondaryPrefix, label);
defines.insert(defineName, metatileId);
definesFileModified = true;
}

View file

@ -380,10 +380,9 @@ void TilesetEditor::onSelectedMetatileChanged(uint16_t metatileId) {
this->metatileLayersItem->draw();
this->ui->graphicsView_metatileLayers->setFixedSize(this->metatileLayersItem->pixmap().width() + 2, this->metatileLayersItem->pixmap().height() + 2);
bool isShared = false;
QString label = Tileset::getMetatileLabel(metatileId, this->primaryTileset, this->secondaryTileset, &isShared);
this->ui->lineEdit_metatileLabel->setText(label);
this->ui->lineEdit_metatileLabel->setReadOnly(isShared);
MetatileLabelPair labels = Tileset::getMetatileLabelPair(metatileId, this->primaryTileset, this->secondaryTileset);
this->ui->lineEdit_metatileLabel->setText(labels.normal);
this->ui->lineEdit_metatileLabel->setPlaceholderText(labels.shared);
setComboValue(this->ui->comboBox_metatileBehaviors, this->metatile->behavior);
setComboValue(this->ui->comboBox_layerType, this->metatile->layerType);
@ -535,8 +534,6 @@ void TilesetEditor::on_comboBox_metatileBehaviors_currentTextChanged(const QStri
void TilesetEditor::setMetatileLabel(QString label)
{
if (this->ui->lineEdit_metatileLabel->isReadOnly())
return;
this->ui->lineEdit_metatileLabel->setText(label);
commitMetatileLabel();
}
@ -552,7 +549,7 @@ void TilesetEditor::commitMetatileLabel()
// Only commit if the field has changed.
uint16_t metatileId = this->getSelectedMetatileId();
QString currentLabel = Tileset::getMetatileLabel(metatileId, this->primaryTileset, this->secondaryTileset);
QString currentLabel = Tileset::getMetatileLabelPair(metatileId, this->primaryTileset, this->secondaryTileset).normal;
QString newLabel = this->ui->lineEdit_metatileLabel->text();
if (currentLabel != newLabel) {
//Metatile *prevMetatile = new Metatile(*this->metatile);
@ -915,14 +912,9 @@ void TilesetEditor::copyMetatile(bool cut) {
else
*this->copiedMetatile = *toCopy;
// Don't try to copy the label unless it's a cut, these should be unique to each metatile
this->copiedMetatileLabel = "";
if (cut) {
bool isShared = false;
QString label = Tileset::getMetatileLabel(metatileId, this->primaryTileset, this->secondaryTileset, &isShared);
if (!isShared)
this->copiedMetatileLabel = label;
}
// Don't try to copy the label unless it's a cut, these should be unique to each metatile.
// Never copy the "shared" metatile label.
this->copiedMetatileLabel = cut ? Tileset::getMetatileLabelPair(metatileId, this->primaryTileset, this->secondaryTileset).normal : "";
}
void TilesetEditor::pasteMetatile(const Metatile * toPaste)
@ -1165,9 +1157,10 @@ void TilesetEditor::countTileUsage() {
}
void TilesetEditor::on_copyButton_metatileLabel_clicked() {
QString label = this->ui->lineEdit_metatileLabel->text();
uint16_t metatileId = this->getSelectedMetatileId();
QString label = Tileset::getMetatileLabel(metatileId, this->primaryTileset, this->secondaryTileset);
if (label.isEmpty()) return;
Tileset * tileset = Tileset::getMetatileLabelTileset(this->getSelectedMetatileId(), this->primaryTileset, this->secondaryTileset);
Tileset * tileset = Tileset::getMetatileLabelTileset(metatileId, this->primaryTileset, this->secondaryTileset);
if (tileset)
label.prepend(tileset->getMetatileLabelPrefix());
QGuiApplication::clipboard()->setText(label);