Skip to content

Commit

Permalink
update tile & metatile usage counts automatically when painting
Browse files Browse the repository at this point in the history
  • Loading branch information
garakmon committed Feb 19, 2025
1 parent 3e13986 commit 089d90c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
13 changes: 13 additions & 0 deletions include/ui/eventfilters.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,16 @@ class MapSceneEventFilter : public QObject {
void wheelZoom(int delta);
public slots:
};



/// Emits a signal when a window gets activated / regains focus
class ActiveWindowFilter : public QObject {
Q_OBJECT
public:
ActiveWindowFilter(QObject *parent) : QObject(parent) {}
virtual ~ActiveWindowFilter() {}
bool eventFilter(QObject *obj, QEvent *event) override;
signals:
void activated();
};
1 change: 1 addition & 0 deletions include/ui/tileseteditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public slots:
void onSelectedMetatileChanged(uint16_t);

private slots:
void onWindowActivated();
void onHoveredMetatileChanged(uint16_t);
void onHoveredMetatileCleared();
void onHoveredTileChanged(uint16_t);
Expand Down
9 changes: 9 additions & 0 deletions src/ui/eventfilters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,12 @@ bool MapSceneEventFilter::eventFilter(QObject*, QEvent *event) {
}
return false;
}



bool ActiveWindowFilter::eventFilter(QObject*, QEvent *event) {
if (event->type() == QEvent::WindowActivate) {
emit activated();
}
return false;
}
22 changes: 21 additions & 1 deletion src/ui/tileseteditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "shortcut.h"
#include "filedialog.h"
#include "validator.h"
#include "eventfilters.h"
#include <QMessageBox>
#include <QDialogButtonBox>
#include <QCloseEvent>
Expand Down Expand Up @@ -40,6 +41,10 @@ TilesetEditor::TilesetEditor(Project *project, Layout *layout, QWidget *parent)
ui->spinBox_paletteSelector->setMaximum(Project::getNumPalettesTotal() - 1);
ui->lineEdit_metatileLabel->setValidator(new IdentifierValidator(this));

ActiveWindowFilter *filter = new ActiveWindowFilter(this);
connect(filter, &ActiveWindowFilter::activated, this, &TilesetEditor::onWindowActivated);
this->installEventFilter(filter);

setAttributesUi();
initMetatileSelector();
initMetatileLayersItem();
Expand Down Expand Up @@ -300,6 +305,16 @@ void TilesetEditor::restoreWindowState() {
this->ui->splitter->restoreState(geometry.value("tileset_editor_splitter_state"));
}

void TilesetEditor::onWindowActivated() {
// User may have made layout edits since window was last focused, so update counts
if (this->metatileSelector) {
if (this->metatileSelector->selectorShowUnused || this->metatileSelector->selectorShowCounts) {
countMetatileUsage();
this->metatileSelector->draw();
}
}
}

void TilesetEditor::initMetatileHistory() {
metatileHistory.clear();
MetatileHistoryItem *commit = new MetatileHistoryItem(0, nullptr, new Metatile(), QString(), QString());
Expand Down Expand Up @@ -455,13 +470,18 @@ void TilesetEditor::onMetatileLayerTileChanged(int x, int y) {
tile.xflip = tiles.at(selectedTileIndex).xflip;
tile.yflip = tiles.at(selectedTileIndex).yflip;
tile.palette = tiles.at(selectedTileIndex).palette;
if (this->tileSelector->showUnused) {
this->tileSelector->usedTiles[tile.tileId] += 1;
this->tileSelector->usedTiles[prevMetatile->tiles[tileIndex].tileId] -= 1;
}
}
selectedTileIndex++;
}
}

this->metatileSelector->draw();
this->metatileLayersItem->draw();
this->tileSelector->draw();
this->commitMetatileChange(prevMetatile);
}

Expand Down Expand Up @@ -1049,7 +1069,7 @@ void TilesetEditor::on_actionShow_Tileset_Divider_triggered(bool checked) {

void TilesetEditor::countMetatileUsage() {
// do not double count
metatileSelector->usedMetatiles.fill(0);
this->metatileSelector->usedMetatiles.fill(0);

for (auto layout : this->project->mapLayouts.values()) {
bool usesPrimary = false;
Expand Down
2 changes: 1 addition & 1 deletion src/ui/tileseteditortileselector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ void TilesetEditorTileSelector::drawUnused() {
unusedPainter.setOpacity(0.5);

for (int tile = 0; tile < this->usedTiles.size(); tile++) {
if (!usedTiles[tile]) {
if (!this->usedTiles[tile]) {
unusedPainter.drawPixmap((tile % 16) * 16, (tile / 16) * 16, redX);
}
}
Expand Down

0 comments on commit 089d90c

Please sign in to comment.