Keep tileset editor in sync with currently opened map

This commit is contained in:
Marcus Huderle 2018-10-02 19:01:18 -05:00
parent 8d38783833
commit 0c7ecebe0b
6 changed files with 36 additions and 8 deletions

View file

@ -157,6 +157,7 @@ private:
void initEditor();
void loadUserSettings();
void openRecentProject();
void updateTilesetEditor();
};
enum MapListUserRoles {

View file

@ -18,6 +18,7 @@ class TilesetEditor : public QMainWindow
public:
explicit TilesetEditor(Project*, QString, QString, QWidget *parent = nullptr);
~TilesetEditor();
void setTilesets(QString, QString);
private slots:
void onHoveredMetatileChanged(uint16_t);

View file

@ -33,6 +33,7 @@ private:
void updateSelectedMetatile();
uint16_t getMetatileId(int x, int y);
QPoint getMetatileIdCoords(uint16_t);
uint16_t getValidMetatileId(uint16_t);
signals:
void hoveredMetatileChanged(uint16_t);

View file

@ -196,6 +196,7 @@ void MainWindow::setMap(QString map_name, bool scrollTreeView) {
setRecentMap(map_name);
updateMapList();
updateTilesetEditor();
}
void MainWindow::redrawMapScene()
@ -529,6 +530,12 @@ void MainWindow::onTilesetChanged(QString mapName)
setMap(mapName);
}
void MainWindow::updateTilesetEditor() {
if (this->tilesetEditor) {
this->tilesetEditor->setTilesets(editor->ui->comboBox_PrimaryTileset->currentText(), editor->ui->comboBox_SecondaryTileset->currentText());
}
}
void MainWindow::currentMetatilesSelectionChanged()
{
ui->graphicsView_currentMetatileSelection->setFixedSize(editor->scene_current_metatile_selection_item->pixmap().width() + 2, editor->scene_current_metatile_selection_item->pixmap().height() + 2);

View file

@ -41,6 +41,21 @@ TilesetEditor::~TilesetEditor()
delete ui;
}
void TilesetEditor::setTilesets(QString primaryTilesetLabel, QString secondaryTilesetLabel) {
delete this->primaryTileset;
delete this->secondaryTileset;
Tileset *primaryTileset = project->getTileset(primaryTilesetLabel);
Tileset *secondaryTileset = project->getTileset(secondaryTilesetLabel);
this->primaryTileset = primaryTileset->copy();
this->secondaryTileset = secondaryTileset->copy();
this->metatileSelector->setTilesets(this->primaryTileset, this->secondaryTileset);
this->tileSelector->setTilesets(this->primaryTileset, this->secondaryTileset);
this->metatileLayersItem->setTilesets(this->primaryTileset, this->secondaryTileset);
this->metatileSelector->select(this->metatileSelector->getSelectedMetatile());
this->drawSelectedTile();
}
void TilesetEditor::initMetatileSelector()
{
this->metatileSelector = new TilesetEditorMetatileSelector(this->primaryTileset, this->secondaryTileset);

View file

@ -32,6 +32,7 @@ void TilesetEditorMetatileSelector::draw() {
}
void TilesetEditorMetatileSelector::select(uint16_t metatileId) {
metatileId = this->getValidMetatileId(metatileId);
QPoint coords = this->getMetatileIdCoords(metatileId);
SelectablePixmapItem::select(coords.x(), coords.y(), 0, 0);
this->selectedMetatile = metatileId;
@ -95,17 +96,19 @@ void TilesetEditorMetatileSelector::hoverLeaveEvent(QGraphicsSceneHoverEvent*) {
}
QPoint TilesetEditorMetatileSelector::getMetatileIdCoords(uint16_t metatileId) {
if (metatileId >= Project::getNumMetatilesTotal()
|| (metatileId < Project::getNumMetatilesPrimary() && metatileId >= this->primaryTileset->metatiles->length())
|| (metatileId < Project::getNumMetatilesTotal() && metatileId >= Project::getNumMetatilesPrimary() + this->secondaryTileset->metatiles->length()))
{
// Invalid metatile id.
return QPoint(0, 0);
}
int index = metatileId < Project::getNumMetatilesPrimary()
? metatileId
: metatileId - Project::getNumMetatilesPrimary() + this->primaryTileset->metatiles->length();
return QPoint(index % this->numMetatilesWide, index / this->numMetatilesWide);
}
uint16_t TilesetEditorMetatileSelector::getValidMetatileId(uint16_t metatileId) {
if (metatileId >= Project::getNumMetatilesTotal()
|| (metatileId < Project::getNumMetatilesPrimary() && metatileId >= this->primaryTileset->metatiles->length())
|| (metatileId < Project::getNumMetatilesTotal() && metatileId >= Project::getNumMetatilesPrimary() + this->secondaryTileset->metatiles->length()))
{
return 0;
}
return metatileId;
}