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 <QImage>
#include <QHash> #include <QHash>
struct MetatileLabelPair {
QString normal;
QString shared;
};
class Tileset class Tileset
{ {
public: public:
@ -36,8 +41,9 @@ public:
static Tileset* getMetatileTileset(int, Tileset*, Tileset*); static Tileset* getMetatileTileset(int, Tileset*, Tileset*);
static Tileset* getTileTileset(int, Tileset*, Tileset*); static Tileset* getTileTileset(int, Tileset*, Tileset*);
static Metatile* getMetatile(int, Tileset*, Tileset*); static Metatile* getMetatile(int, Tileset*, Tileset*);
static Tileset* getMetatileLabelTileset(int, Tileset*, Tileset*, bool * isShared = nullptr); static Tileset* getMetatileLabelTileset(int, Tileset*, Tileset*);
static QString getMetatileLabel(int, Tileset *, Tileset *, bool * isShared = nullptr); static QString getMetatileLabel(int, Tileset *, Tileset *);
static MetatileLabelPair getMetatileLabelPair(int metatileId, Tileset *primaryTileset, Tileset *secondaryTileset);
static bool setMetatileLabel(int, QString, Tileset *, Tileset *); static bool setMetatileLabel(int, QString, Tileset *, Tileset *);
QString getMetatileLabelPrefix(); QString getMetatileLabelPrefix();
static QString getMetatileLabelPrefix(const QString &name); 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 // 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 // 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 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 *mainTileset = nullptr;
Tileset *alternateTileset = nullptr; Tileset *alternateTileset = nullptr;
if (isShared) *isShared = false;
if (metatileId < Project::getNumMetatilesPrimary()) { if (metatileId < Project::getNumMetatilesPrimary()) {
mainTileset = primaryTileset; mainTileset = primaryTileset;
alternateTileset = secondaryTileset; alternateTileset = secondaryTileset;
@ -106,22 +105,42 @@ Tileset* Tileset::getMetatileLabelTileset(int metatileId, Tileset *primaryTilese
alternateTileset = primaryTileset; alternateTileset = primaryTileset;
} }
if (mainTileset && mainTileset->metatileLabels.contains(metatileId)) { if (mainTileset && !mainTileset->metatileLabels.value(metatileId).isEmpty()) {
return mainTileset; return mainTileset;
} else if (alternateTileset && alternateTileset->metatileLabels.contains(metatileId)) { } else if (alternateTileset && !alternateTileset->metatileLabels.value(metatileId).isEmpty()) {
if (isShared) *isShared = true;
return alternateTileset; return alternateTileset;
} }
return nullptr; return nullptr;
} }
QString Tileset::getMetatileLabel(int metatileId, Tileset *primaryTileset, Tileset *secondaryTileset, bool * isShared) { // If the metatile has a label in the tileset it belongs to, return that label.
Tileset * tileset = Tileset::getMetatileLabelTileset(metatileId, primaryTileset, secondaryTileset, isShared); // 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) if (!tileset)
return QString(); return QString();
return tileset->metatileLabels.value(metatileId); 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) { bool Tileset::setMetatileLabel(int metatileId, QString label, Tileset *primaryTileset, Tileset *secondaryTileset) {
Tileset *tileset = Tileset::getMetatileTileset(metatileId, primaryTileset, secondaryTileset); Tileset *tileset = Tileset::getMetatileTileset(metatileId, primaryTileset, secondaryTileset);
if (!tileset) if (!tileset)

View file

@ -914,12 +914,16 @@ void Project::saveTilesetMetatileLabels(Tileset *primaryTileset, Tileset *second
// Add the new labels. // Add the new labels.
for (int metatileId : primaryTileset->metatileLabels.keys()) { 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); defines.insert(defineName, metatileId);
definesFileModified = true; definesFileModified = true;
} }
for (int metatileId : secondaryTileset->metatileLabels.keys()) { 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); defines.insert(defineName, metatileId);
definesFileModified = true; definesFileModified = true;
} }

View file

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