Add ability to save tileset changes

This commit is contained in:
Marcus Huderle 2018-10-02 19:01:15 -05:00
parent 428548b7e1
commit 8d38783833
14 changed files with 97 additions and 40 deletions

View file

@ -13,6 +13,7 @@ public:
uint8_t behavior;
uint8_t layerType;
Metatile *copy();
static int getBlockIndex(int);
};

View file

@ -26,6 +26,8 @@ public:
QList<Metatile*> *metatiles = nullptr;
QList<QList<QRgb>> *palettes = nullptr;
Tileset* copy();
static Tileset* getBlockTileset(int, Tileset*, Tileset*);
static Metatile* getMetatile(int, Tileset*, Tileset*);
static QList<QList<QRgb>> getBlockPalettes(Tileset*, Tileset*);

View file

@ -68,8 +68,8 @@ public:
void updateDiveMap(QString mapName);
void updateEmergeMap(QString mapName);
void setSelectedConnectionFromMap(QString mapName);
void updatePrimaryTileset(QString tilesetLabel);
void updateSecondaryTileset(QString tilesetLabel);
void updatePrimaryTileset(QString tilesetLabel, bool forceLoad = false);
void updateSecondaryTileset(QString tilesetLabel, bool forceLoad = false);
void toggleBorderVisibility(bool visible);
Tileset *getCurrentMapPrimaryTileset();

View file

@ -43,6 +43,7 @@ private slots:
void onLoadMapRequested(QString, QString);
void onMapChanged(Map *map);
void onMapNeedsRedrawing();
void onTilesetsSaved(QString, QString);
void on_action_Save_triggered();
void on_tabWidget_2_currentChanged(int index);

View file

@ -48,8 +48,8 @@ public:
Map* getMap(QString);
QMap<QString, Tileset*> *tileset_cache = nullptr;
Tileset* loadTileset(QString);
Tileset* getTileset(QString);
Tileset* loadTileset(QString, Tileset *tileset = nullptr);
Tileset* getTileset(QString, bool forceLoad = false);
Blockdata* readBlockdata(QString);
void loadBlockdata(Map*);

View file

@ -55,13 +55,16 @@ private:
int paletteId;
bool tileXFlip;
bool tileYFlip;
QString primaryTilesetLabel;
QString secondaryTilesetLabel;
Tileset *primaryTileset;
Tileset *secondaryTileset;
QGraphicsScene *metatilesScene = nullptr;
QGraphicsScene *tilesScene = nullptr;
QGraphicsScene *selectedTileScene = nullptr;
QGraphicsPixmapItem *selectedTilePixmapItem = nullptr;
QGraphicsScene *metatileLayersScene = nullptr;
signals:
void tilesetsSaved(QString, QString);
};
#endif // TILESETEDITOR_H

View file

@ -26,8 +26,8 @@ protected:
void hoverLeaveEvent(QGraphicsSceneHoverEvent*);
private:
Tileset *primaryTileset;
Tileset *secondaryTileset;
Tileset *primaryTileset = nullptr;
Tileset *secondaryTileset = nullptr;
uint16_t selectedMetatile;
int numMetatilesWide;
void updateSelectedMetatile();

View file

@ -226,7 +226,7 @@ QPixmap Map::renderBorder() {
}
QPixmap Map::renderConnection(MapConnection connection) {
render();
render(true);
int x, y, w, h;
if (connection.direction == "up") {
x = 0;

View file

@ -7,6 +7,17 @@ Metatile::Metatile()
tiles = new QList<Tile>;
}
Metatile* Metatile::copy() {
Metatile *copy = new Metatile;
copy->behavior = this->behavior;
copy->layerType = this->layerType;
copy->tiles = new QList<Tile>;
for (Tile tile : *this->tiles) {
copy->tiles->append(tile);
}
return copy;
}
int Metatile::getBlockIndex(int index) {
if (index < Project::getNumMetatilesPrimary()) {
return index;

View file

@ -11,6 +11,38 @@ Tileset::Tileset()
}
Tileset* Tileset::copy() {
Tileset *copy = new Tileset;
copy->name = this->name;
copy->is_compressed = this->is_compressed;
copy->is_secondary = this->is_secondary;
copy->padding = this->padding;
copy->tiles_label = this->tiles_label;
copy->palettes_label = this->palettes_label;
copy->metatiles_label = this->metatiles_label;
copy->metatiles_path = this->metatiles_path;
copy->callback_label = this->callback_label;
copy->metatile_attrs_label = this->metatile_attrs_label;
copy->metatile_attrs_path = this->metatile_attrs_path;
copy->tiles = new QList<QImage>;
for (QImage tile : *this->tiles) {
copy->tiles->append(tile.copy());
}
copy->metatiles = new QList<Metatile*>;
for (Metatile *metatile : *this->metatiles) {
copy->metatiles->append(metatile->copy());
}
copy->palettes = new QList<QList<QRgb>>;
for (QList<QRgb> palette : *this->palettes) {
QList<QRgb> copyPalette;
for (QRgb color : palette) {
copyPalette.append(color);
}
copy->palettes->append(copyPalette);
}
return copy;
}
Tileset* Tileset::getBlockTileset(int metatile_index, Tileset *primaryTileset, Tileset *secondaryTileset) {
if (metatile_index < Project::getNumMetatilesPrimary()) {
return primaryTileset;

View file

@ -961,22 +961,23 @@ void Editor::updateDiveEmergeMap(QString mapName, QString direction) {
ui->label_NumConnections->setText(QString::number(map->connections.length()));
}
void Editor::updatePrimaryTileset(QString tilesetLabel)
void Editor::updatePrimaryTileset(QString tilesetLabel, bool forceLoad)
{
if (map->layout->tileset_primary_label != tilesetLabel)
if (map->layout->tileset_primary_label != tilesetLabel || forceLoad)
{
qDebug() << "updatePrimaryTileset";
map->layout->tileset_primary_label = tilesetLabel;
map->layout->tileset_primary = project->getTileset(tilesetLabel);
map->layout->tileset_primary = project->getTileset(tilesetLabel, forceLoad);
emit tilesetChanged(map->name);
}
}
void Editor::updateSecondaryTileset(QString tilesetLabel)
void Editor::updateSecondaryTileset(QString tilesetLabel, bool forceLoad)
{
if (map->layout->tileset_secondary_label != tilesetLabel)
{
map->layout->tileset_secondary_label = tilesetLabel;
map->layout->tileset_secondary = project->getTileset(tilesetLabel);
map->layout->tileset_secondary = project->getTileset(tilesetLabel, forceLoad);
emit tilesetChanged(map->name);
}
}

View file

@ -1104,6 +1104,11 @@ void MainWindow::onMapNeedsRedrawing() {
redrawMapScene();
}
void MainWindow::onTilesetsSaved(QString primaryTilesetLabel, QString secondaryTilesetLabel) {
this->editor->updatePrimaryTileset(primaryTilesetLabel, true);
this->editor->updateSecondaryTileset(secondaryTilesetLabel, true);
}
void MainWindow::on_action_Export_Map_Image_triggered()
{
QString defaultFilepath = QString("%1/%2.png").arg(editor->project->root).arg(editor->map->name);
@ -1233,6 +1238,7 @@ void MainWindow::on_actionTileset_Editor_triggered()
{
if (!this->tilesetEditor) {
this->tilesetEditor = new TilesetEditor(this->editor->project, this->editor->map->layout->tileset_primary_label, this->editor->map->layout->tileset_secondary_label);
connect(this->tilesetEditor, SIGNAL(tilesetsSaved(QString, QString)), this, SLOT(onTilesetsSaved(QString, QString)));
}
if (!this->tilesetEditor->isVisible()) {

View file

@ -628,12 +628,14 @@ void Project::loadMapTilesets(Map* map) {
map->layout->tileset_secondary = getTileset(map->layout->tileset_secondary_label);
}
Tileset* Project::loadTileset(QString label) {
Tileset* Project::loadTileset(QString label, Tileset *tileset) {
ParseUtil *parser = new ParseUtil;
QString headers_text = readTextFile(root + "/data/tilesets/headers.inc");
QStringList *values = getLabelValues(parser->parseAsm(headers_text), label);
Tileset *tileset = new Tileset;
if (tileset == nullptr) {
tileset = new Tileset;
}
tileset->name = label;
tileset->is_compressed = values->value(0);
tileset->is_secondary = values->value(1);
@ -793,7 +795,8 @@ void Project::loadTilesetAssets(Tileset* tileset) {
if (tileset->name.isNull()) {
return;
}
QString dir_path = root + "/data/tilesets/" + category + "/" + tileset->name.replace("gTileset_", "").toLower();
QString tilesetName = tileset->name;
QString dir_path = root + "/data/tilesets/" + category + "/" + tilesetName.replace("gTileset_", "").toLower();
QString graphics_text = readTextFile(root + "/data/tilesets/graphics.inc");
QList<QStringList> *graphics = parser->parseAsm(graphics_text);
@ -958,11 +961,16 @@ Map* Project::getMap(QString map_name) {
}
}
Tileset* Project::getTileset(QString label) {
Tileset* Project::getTileset(QString label, bool forceLoad) {
Tileset *existingTileset = nullptr;
if (tileset_cache->contains(label)) {
existingTileset = tileset_cache->value(label);
}
if (existingTileset && !forceLoad) {
return tileset_cache->value(label);
} else {
Tileset *tileset = loadTileset(label);
Tileset *tileset = loadTileset(label, existingTileset);
return tileset;
}
}

View file

@ -13,8 +13,11 @@ TilesetEditor::TilesetEditor(Project *project, QString primaryTilesetLabel, QStr
this->tileXFlip = ui->checkBox_xFlip->isChecked();
this->tileYFlip = ui->checkBox_yFlip->isChecked();
this->paletteId = ui->spinBox_paletteSelector->value();
this->primaryTilesetLabel = primaryTilesetLabel;
this->secondaryTilesetLabel = secondaryTilesetLabel;
Tileset *primaryTileset = project->getTileset(primaryTilesetLabel);
Tileset *secondaryTileset = project->getTileset(secondaryTilesetLabel);
this->primaryTileset = primaryTileset->copy();
this->secondaryTileset = secondaryTileset->copy();
QList<QString> sortedBehaviors;
for (int num : project->metatileBehaviorMapInverse.keys()) {
@ -40,9 +43,7 @@ TilesetEditor::~TilesetEditor()
void TilesetEditor::initMetatileSelector()
{
Tileset *primaryTileset = this->project->getTileset(this->primaryTilesetLabel);
Tileset *secondaryTileset = this->project->getTileset(this->secondaryTilesetLabel);
this->metatileSelector = new TilesetEditorMetatileSelector(primaryTileset, secondaryTileset);
this->metatileSelector = new TilesetEditorMetatileSelector(this->primaryTileset, this->secondaryTileset);
connect(this->metatileSelector, SIGNAL(hoveredMetatileChanged(uint16_t)),
this, SLOT(onHoveredMetatileChanged(uint16_t)));
connect(this->metatileSelector, SIGNAL(hoveredMetatileCleared()),
@ -60,9 +61,7 @@ void TilesetEditor::initMetatileSelector()
void TilesetEditor::initTileSelector()
{
Tileset *primaryTileset = this->project->getTileset(this->primaryTilesetLabel);
Tileset *secondaryTileset = this->project->getTileset(this->secondaryTilesetLabel);
this->tileSelector = new TilesetEditorTileSelector(primaryTileset, secondaryTileset);
this->tileSelector = new TilesetEditorTileSelector(this->primaryTileset, this->secondaryTileset);
connect(this->tileSelector, SIGNAL(hoveredTileChanged(uint16_t)),
this, SLOT(onHoveredTileChanged(uint16_t)));
connect(this->tileSelector, SIGNAL(hoveredTileCleared()),
@ -91,19 +90,15 @@ void TilesetEditor::drawSelectedTile() {
}
this->selectedTileScene->clear();
Tileset *primaryTileset = this->project->getTileset(this->primaryTilesetLabel);
Tileset *secondaryTileset = this->project->getTileset(this->secondaryTilesetLabel);
QImage tileImage = getColoredTileImage(this->tileSelector->getSelectedTile(), primaryTileset, secondaryTileset, this->paletteId)
QImage tileImage = getColoredTileImage(this->tileSelector->getSelectedTile(), this->primaryTileset, this->secondaryTileset, this->paletteId)
.mirrored(this->tileXFlip, this->tileYFlip);
this->selectedTilePixmapItem = new QGraphicsPixmapItem(QPixmap::fromImage(tileImage).scaled(32, 32));
this->selectedTileScene->addItem(this->selectedTilePixmapItem);
}
void TilesetEditor::initMetatileLayersItem() {
Tileset *primaryTileset = this->project->getTileset(this->primaryTilesetLabel);
Tileset *secondaryTileset = this->project->getTileset(this->secondaryTilesetLabel);
Metatile *metatile = Tileset::getMetatile(this->metatileSelector->getSelectedMetatile(), primaryTileset, secondaryTileset);
this->metatileLayersItem = new MetatileLayersItem(metatile, primaryTileset, secondaryTileset);
Metatile *metatile = Tileset::getMetatile(this->metatileSelector->getSelectedMetatile(), this->primaryTileset, this->secondaryTileset);
this->metatileLayersItem = new MetatileLayersItem(metatile, this->primaryTileset, this->secondaryTileset);
connect(this->metatileLayersItem, SIGNAL(tileChanged(int)),
this, SLOT(onMetatileLayerTileChanged(int)));
@ -123,9 +118,7 @@ void TilesetEditor::onHoveredMetatileCleared() {
}
void TilesetEditor::onSelectedMetatileChanged(uint16_t metatileId) {
Tileset *primaryTileset = this->project->getTileset(this->primaryTilesetLabel);
Tileset *secondaryTileset = this->project->getTileset(this->secondaryTilesetLabel);
this->metatile = Tileset::getMetatile(metatileId, primaryTileset, secondaryTileset);
this->metatile = Tileset::getMetatile(metatileId, this->primaryTileset, this->secondaryTileset);
this->metatileLayersItem->setMetatile(metatile);
this->metatileLayersItem->draw();
this->ui->comboBox_metatileBehaviors->setCurrentIndex(this->ui->comboBox_metatileBehaviors->findData(this->metatile->behavior));
@ -193,7 +186,6 @@ void TilesetEditor::on_comboBox_layerType_currentIndexChanged(int layerType)
void TilesetEditor::on_actionSave_Tileset_triggered()
{
Tileset *primaryTileset = this->project->getTileset(this->primaryTilesetLabel);
Tileset *secondaryTileset = this->project->getTileset(this->secondaryTilesetLabel);
this->project->saveTilesets(primaryTileset, secondaryTileset);
this->project->saveTilesets(this->primaryTileset, this->secondaryTileset);
emit this->tilesetsSaved(this->primaryTileset->name, this->secondaryTileset->name);
}