Add scripting api for metatile layer opacity

This commit is contained in:
Marcus Huderle 2020-07-01 20:43:19 -05:00
parent dac4a0e8f7
commit 96ca895daa
10 changed files with 43 additions and 20 deletions

View file

@ -58,6 +58,7 @@ public:
QMap<QString, QList<Event*>> events;
QList<MapConnection*> connections;
QList<int> metatileLayerOrder;
QList<float> metatileLayerOpacity;
void setName(QString mapName);
static QString mapConstantFromName(QString mapName);
static QString objectEventsLabelFromName(QString mapName);

View file

@ -106,6 +106,8 @@ public:
Q_INVOKABLE void log(QString message);
Q_INVOKABLE QList<int> getMetatileLayerOrder();
Q_INVOKABLE void setMetatileLayerOrder(QList<int> order);
Q_INVOKABLE QList<float> getMetatileLayerOpacity();
Q_INVOKABLE void setMetatileLayerOpacity(QList<float> order);
public slots:

View file

@ -8,8 +8,7 @@
QImage getCollisionMetatileImage(Block);
QImage getCollisionMetatileImage(int, int);
QImage getMetatileImage(uint16_t, Tileset*, Tileset*, QList<int>, bool useTruePalettes = false);
QImage getMetatileImage(uint16_t tile, Tileset*, Tileset*, bool useTruePalettes = false);
QImage getMetatileImage(uint16_t, Tileset*, Tileset*, QList<int>, QList<float>, bool useTruePalettes = false);
QImage getTileImage(uint16_t, Tileset*, Tileset*);
QImage getPalettedTileImage(uint16_t, Tileset*, Tileset*, int, bool useTruePalettes = false);
QImage getGreyscaleTileImage(uint16_t tile, Tileset *primaryTileset, Tileset *secondaryTileset);

View file

@ -158,7 +158,7 @@ QPixmap Map::renderCollision(qreal opacity, bool ignoreCache) {
}
changed_any = true;
Block block = layout->blockdata->blocks->value(i);
QImage metatile_image = getMetatileImage(block.tile, layout->tileset_primary, layout->tileset_secondary, metatileLayerOrder);
QImage metatile_image = getMetatileImage(block.tile, layout->tileset_primary, layout->tileset_secondary, metatileLayerOrder, metatileLayerOpacity);
QImage collision_metatile_image = getCollisionMetatileImage(block);
int map_y = width_ ? i / width_ : 0;
int map_x = width_ ? i % width_ : 0;
@ -206,7 +206,8 @@ QPixmap Map::render(bool ignoreCache = false, MapLayout * fromLayout) {
block.tile,
fromLayout ? fromLayout->tileset_primary : layout->tileset_primary,
fromLayout ? fromLayout->tileset_secondary : layout->tileset_secondary,
metatileLayerOrder
metatileLayerOrder,
metatileLayerOpacity
);
int map_y = width_ ? i / width_ : 0;
int map_x = width_ ? i % width_ : 0;
@ -247,7 +248,7 @@ QPixmap Map::renderBorder(bool ignoreCache) {
changed_any = true;
Block block = layout->border->blocks->value(i);
uint16_t tile = block.tile;
QImage metatile_image = getMetatileImage(tile, layout->tileset_primary, layout->tileset_secondary, metatileLayerOrder);
QImage metatile_image = getMetatileImage(tile, layout->tileset_primary, layout->tileset_secondary, metatileLayerOrder, metatileLayerOpacity);
int map_y = width_ ? i / width_ : 0;
int map_x = width_ ? i % width_ : 0;
painter.drawImage(QPoint(map_x * 16, map_y * 16), metatile_image);

View file

@ -520,3 +520,16 @@ void MainWindow::setMetatileLayerOrder(QList<int> order) {
this->editor->map->metatileLayerOrder = order;
this->refreshAfterPalettePreviewChange();
}
QList<float> MainWindow::getMetatileLayerOpacity() {
if (!this->editor || !this->editor->map)
return QList<float>();
return this->editor->map->metatileLayerOpacity;
}
void MainWindow::setMetatileLayerOpacity(QList<float> order) {
if (!this->editor || !this->editor->map)
return;
this->editor->map->metatileLayerOpacity = order;
this->refreshAfterPalettePreviewChange();
}

View file

@ -35,7 +35,12 @@ void BorderMetatilesPixmapItem::draw() {
int x = i * 16;
int y = j * 16;
int index = j * width + i;
QImage metatile_image = getMetatileImage(blocks->value(index).tile, map->layout->tileset_primary, map->layout->tileset_secondary, map->metatileLayerOrder);
QImage metatile_image = getMetatileImage(
blocks->value(index).tile,
map->layout->tileset_primary,
map->layout->tileset_secondary,
map->metatileLayerOrder,
map->metatileLayerOpacity);
QPoint metatile_origin = QPoint(x, y);
painter.drawImage(metatile_origin, metatile_image);
}

View file

@ -19,7 +19,8 @@ void CurrentSelectedMetatilesPixmapItem::draw() {
selectedMetatiles->at(index),
map->layout->tileset_primary,
map->layout->tileset_secondary,
map->metatileLayerOrder);
map->metatileLayerOrder,
map->metatileLayerOpacity);
QPoint metatile_origin = QPoint(x, y);
painter.drawImage(metatile_origin, metatile_image);
}

View file

@ -14,19 +14,16 @@ QImage getCollisionMetatileImage(int collision, int elevation) {
return collisionImage.toImage();
}
static QList<int> defaultLayerOrder = QList<int>({0, 1, 2});
QImage getMetatileImage(uint16_t tile, Tileset *primaryTileset, Tileset *secondaryTileset, bool useTruePalettes) {
return getMetatileImage(
tile,
primaryTileset,
secondaryTileset,
defaultLayerOrder,
useTruePalettes);
}
QImage getMetatileImage(uint16_t tile, Tileset *primaryTileset, Tileset *secondaryTileset, QList<int> layerOrder, bool useTruePalettes) {
QImage getMetatileImage(
uint16_t tile,
Tileset *primaryTileset,
Tileset *secondaryTileset,
QList<int> layerOrder,
QList<float> layerOpacity,
bool useTruePalettes)
{
QImage metatile_image(16, 16, QImage::Format_RGBA8888);
metatile_image.fill(Qt::black);
Metatile* metatile = Tileset::getMetatile(tile, primaryTileset, secondaryTileset);
if (!metatile || !metatile->tiles) {
@ -56,6 +53,7 @@ QImage getMetatileImage(uint16_t tile, Tileset *primaryTileset, Tileset *seconda
// being drawn unless they're on the bottom layer, in which case we need
// a placeholder because garbage will be drawn otherwise.
if (l == 0) {
metatile_painter.setOpacity(1.0);
metatile_painter.fillRect(x * 8, y * 8, 8, 8, palettes.value(0).value(0));
}
continue;
@ -79,6 +77,8 @@ QImage getMetatileImage(uint16_t tile, Tileset *primaryTileset, Tileset *seconda
}
QPoint origin = QPoint(x*8, y*8);
float opacity = layerOpacity.size() >= numLayers ? layerOpacity[layer] : 1.0;
metatile_painter.setOpacity(opacity);
metatile_painter.drawImage(origin, tile_image.mirrored(tile_.xflip, tile_.yflip));
}
metatile_painter.end();

View file

@ -31,7 +31,7 @@ void MetatileSelector::draw() {
if (i >= primaryLength) {
tile += Project::getNumMetatilesPrimary() - primaryLength;
}
QImage metatile_image = getMetatileImage(tile, this->primaryTileset, this->secondaryTileset, map->metatileLayerOrder);
QImage metatile_image = getMetatileImage(tile, this->primaryTileset, this->secondaryTileset, map->metatileLayerOrder, map->metatileLayerOpacity);
int map_y = i / this->numMetatilesWide;
int map_x = i % this->numMetatilesWide;
QPoint metatile_origin = QPoint(map_x * 16, map_y * 16);

View file

@ -28,6 +28,7 @@ void TilesetEditorMetatileSelector::draw() {
this->primaryTileset,
this->secondaryTileset,
map->metatileLayerOrder,
map->metatileLayerOpacity,
true)
.scaled(32, 32);
int map_y = i / this->numMetatilesWide;