Extract image providers

This commit is contained in:
Marcus Huderle 2018-09-25 18:13:33 -05:00
parent afc25805d3
commit 06720258f9
9 changed files with 103 additions and 85 deletions

View file

@ -19,60 +19,6 @@ Tileset* Tileset::getBlockTileset(int metatile_index, Tileset *primaryTileset, T
}
}
QImage Tileset::getMetatileImage(int tile, Tileset *primaryTileset, Tileset *secondaryTileset) {
QImage metatile_image(16, 16, QImage::Format_RGBA8888);
Metatile* metatile = Tileset::getMetatile(tile, primaryTileset, secondaryTileset);
if (!metatile || !metatile->tiles) {
metatile_image.fill(0xffffffff);
return metatile_image;
}
Tileset* blockTileset = Tileset::getBlockTileset(tile, primaryTileset, secondaryTileset);
if (!blockTileset) {
metatile_image.fill(0xffffffff);
return metatile_image;
}
QList<QList<QRgb>> palettes = Tileset::getBlockPalettes(primaryTileset, secondaryTileset);
QPainter metatile_painter(&metatile_image);
for (int layer = 0; layer < 2; layer++)
for (int y = 0; y < 2; y++)
for (int x = 0; x < 2; x++) {
Tile tile_ = metatile->tiles->value((y * 2) + x + (layer * 4));
QImage tile_image = Tileset::getMetatileTile(tile_.tile, primaryTileset, secondaryTileset);
if (tile_image.isNull()) {
// Some metatiles specify tiles that are outside the valid range.
// These are treated as completely transparent, so they can be skipped without
// being drawn.
continue;
}
// Colorize the metatile tiles with its palette.
if (tile_.palette < palettes.length()) {
QList<QRgb> palette = palettes.value(tile_.palette);
for (int j = 0; j < palette.length(); j++) {
tile_image.setColor(j, palette.value(j));
}
} else {
qDebug() << "Tile is referring to invalid palette number: " << tile_.palette;
}
// The top layer of the metatile has its first color displayed at transparent.
if (layer > 0) {
QColor color(tile_image.color(0));
color.setAlpha(0);
tile_image.setColor(0, color.rgba());
}
QPoint origin = QPoint(x*8, y*8);
metatile_painter.drawImage(origin, tile_image.mirrored(tile_.xflip == 1, tile_.yflip == 1));
}
metatile_painter.end();
return metatile_image;
}
Metatile* Tileset::getMetatile(int index, Tileset *primaryTileset, Tileset *secondaryTileset) {
Tileset *tileset = Tileset::getBlockTileset(index, primaryTileset, secondaryTileset);
int local_index = Metatile::getBlockIndex(index);
@ -83,15 +29,6 @@ Metatile* Tileset::getMetatile(int index, Tileset *primaryTileset, Tileset *seco
return metatile;
}
QImage Tileset::getMetatileTile(int tile, Tileset *primaryTileset, Tileset *secondaryTileset) {
Tileset *tileset = Tileset::getBlockTileset(tile, primaryTileset, secondaryTileset);
int local_index = Metatile::getBlockIndex(tile);
if (!tileset || !tileset->tiles) {
return QImage();
}
return tileset->tiles->value(local_index, QImage());
}
QList<QList<QRgb>> Tileset::getBlockPalettes(Tileset *primaryTileset, Tileset *secondaryTileset) {
QList<QList<QRgb>> palettes;
for (int i = 0; i < Project::getNumPalettesPrimary(); i++) {

View file

@ -25,9 +25,7 @@ public:
QList<QList<QRgb>> *palettes = nullptr;
static Tileset* getBlockTileset(int, Tileset*, Tileset*);
static QImage getMetatileImage(int, Tileset*, Tileset*);
static Metatile* getMetatile(int, Tileset*, Tileset*);
static QImage getMetatileTile(int, Tileset*, Tileset*);
static QList<QList<QRgb>> getBlockPalettes(Tileset*, Tileset*);
};

View file

@ -1,5 +1,6 @@
#include "editor.h"
#include "event.h"
#include "imageproviders.h"
#include "mapconnection.h"
#include <QCheckBox>
#include <QPainter>
@ -942,7 +943,7 @@ void BorderMetatilesPixmapItem::draw() {
int x = i * 16;
int y = j * 16;
int index = j * 2 + i;
QImage metatile_image = Tileset::getMetatileImage(blocks->value(index).tile, map->layout->tileset_primary, map->layout->tileset_secondary);
QImage metatile_image = getMetatileImage(blocks->value(index).tile, map->layout->tileset_primary, map->layout->tileset_secondary);
QPoint metatile_origin = QPoint(x, y);
painter.drawImage(metatile_origin, metatile_image);
}
@ -965,7 +966,7 @@ void CurrentSelectedMetatilesPixmapItem::draw() {
int x = i * 16;
int y = j * 16;
int index = j * selectionDimensions.x() + i;
QImage metatile_image = Tileset::getMetatileImage(selectedMetatiles->at(index), map->layout->tileset_primary, map->layout->tileset_secondary);
QImage metatile_image = getMetatileImage(selectedMetatiles->at(index), map->layout->tileset_primary, map->layout->tileset_secondary);
QPoint metatile_origin = QPoint(x, y);
painter.drawImage(metatile_origin, metatile_image);
}

18
map.cpp
View file

@ -2,6 +2,7 @@
#include "historyitem.h"
#include "map.h"
#include "project.h"
#include "imageproviders.h"
#include <QTime>
#include <QDebug>
@ -76,17 +77,6 @@ int Map::getDisplayedBlockIndex(int index) {
}
}
QImage Map::getCollisionMetatileImage(Block block) {
return getCollisionMetatileImage(block.collision, block.elevation);
}
QImage Map::getCollisionMetatileImage(int collision, int elevation) {
int x = collision * 16;
int y = elevation * 16;
QPixmap collisionImage = QPixmap(":/images/collisions.png").copy(x, y, 16, 16);
return collisionImage.toImage();
}
bool Map::blockChanged(int i, Blockdata *cache) {
if (!cache)
return true;
@ -160,7 +150,7 @@ QPixmap Map::renderCollision(bool ignoreCache) {
}
changed_any = true;
Block block = layout->blockdata->blocks->value(i);
QImage metatile_image = Tileset::getMetatileImage(block.tile, layout->tileset_primary, layout->tileset_secondary);
QImage metatile_image = getMetatileImage(block.tile, layout->tileset_primary, layout->tileset_secondary);
QImage collision_metatile_image = getCollisionMetatileImage(block);
int map_y = width_ ? i / width_ : 0;
int map_x = width_ ? i % width_ : 0;
@ -204,7 +194,7 @@ QPixmap Map::render(bool ignoreCache = false) {
}
changed_any = true;
Block block = layout->blockdata->blocks->value(i);
QImage metatile_image = Tileset::getMetatileImage(block.tile, layout->tileset_primary, layout->tileset_secondary);
QImage metatile_image = getMetatileImage(block.tile, layout->tileset_primary, layout->tileset_secondary);
int map_y = width_ ? i / width_ : 0;
int map_x = width_ ? i % width_ : 0;
QPoint metatile_origin = QPoint(map_x * 16, map_y * 16);
@ -238,7 +228,7 @@ QPixmap Map::renderBorder() {
}
changed_any = true;
Block block = layout->border->blocks->value(i);
QImage metatile_image = Tileset::getMetatileImage(block.tile, layout->tileset_primary, layout->tileset_secondary);
QImage metatile_image = getMetatileImage(block.tile, layout->tileset_primary, layout->tileset_secondary);
int map_y = i / width_;
int map_x = i % width_;
painter.drawImage(QPoint(map_x * 16, map_y * 16), metatile_image);

4
map.h
View file

@ -57,12 +57,10 @@ public:
uint16_t getSelectedBlockIndex(int);
int getDisplayedBlockIndex(int);
QPixmap render(bool ignoreCache);
QPixmap renderCollision(bool ignoreCache);
QImage collision_image;
QPixmap collision_pixmap;
QImage getCollisionMetatileImage(Block);
QImage getCollisionMetatileImage(int, int);
void drawSelection(int i, int w, int selectionWidth, int selectionHeight, QPainter *painter, int gridWidth);

View file

@ -25,6 +25,7 @@ SOURCES += core/block.cpp \
core/tile.cpp \
core/tileset.cpp \
ui/eventpropertiesframe.cpp \
ui/imageproviders.cpp \
ui/metatileselector.cpp \
ui/movementpermissionsselector.cpp \
ui/neweventtoolbutton.cpp \
@ -51,6 +52,7 @@ HEADERS += core/block.h \
core/tile.h \
core/tileset.h \
ui/eventpropertiesframe.h \
ui/imageproviders.h \
ui/metatileselector.h \
ui/movementpermissionsselector.h \
ui/neweventtoolbutton.h \

77
ui/imageproviders.cpp Normal file
View file

@ -0,0 +1,77 @@
#include "imageproviders.h"
#include <QPainter>
#include <QDebug>
QImage getCollisionMetatileImage(Block block) {
return getCollisionMetatileImage(block.collision, block.elevation);
}
QImage getCollisionMetatileImage(int collision, int elevation) {
int x = collision * 16;
int y = elevation * 16;
QPixmap collisionImage = QPixmap(":/images/collisions.png").copy(x, y, 16, 16);
return collisionImage.toImage();
}
QImage getMetatileImage(int tile, Tileset *primaryTileset, Tileset *secondaryTileset) {
QImage metatile_image(16, 16, QImage::Format_RGBA8888);
Metatile* metatile = Tileset::getMetatile(tile, primaryTileset, secondaryTileset);
if (!metatile || !metatile->tiles) {
metatile_image.fill(0xffffffff);
return metatile_image;
}
Tileset* blockTileset = Tileset::getBlockTileset(tile, primaryTileset, secondaryTileset);
if (!blockTileset) {
metatile_image.fill(0xffffffff);
return metatile_image;
}
QList<QList<QRgb>> palettes = Tileset::getBlockPalettes(primaryTileset, secondaryTileset);
QPainter metatile_painter(&metatile_image);
for (int layer = 0; layer < 2; layer++)
for (int y = 0; y < 2; y++)
for (int x = 0; x < 2; x++) {
Tile tile_ = metatile->tiles->value((y * 2) + x + (layer * 4));
QImage tile_image = getTileImage(tile_.tile, primaryTileset, secondaryTileset);
if (tile_image.isNull()) {
// Some metatiles specify tiles that are outside the valid range.
// These are treated as completely transparent, so they can be skipped without
// being drawn.
continue;
}
// Colorize the metatile tiles with its palette.
if (tile_.palette < palettes.length()) {
QList<QRgb> palette = palettes.value(tile_.palette);
for (int j = 0; j < palette.length(); j++) {
tile_image.setColor(j, palette.value(j));
}
} else {
qDebug() << "Tile is referring to invalid palette number: " << tile_.palette;
}
// The top layer of the metatile has its first color displayed at transparent.
if (layer > 0) {
QColor color(tile_image.color(0));
color.setAlpha(0);
tile_image.setColor(0, color.rgba());
}
QPoint origin = QPoint(x*8, y*8);
metatile_painter.drawImage(origin, tile_image.mirrored(tile_.xflip == 1, tile_.yflip == 1));
}
metatile_painter.end();
return metatile_image;
}
QImage getTileImage(int tile, Tileset *primaryTileset, Tileset *secondaryTileset) {
Tileset *tileset = Tileset::getBlockTileset(tile, primaryTileset, secondaryTileset);
int local_index = Metatile::getBlockIndex(tile);
if (!tileset || !tileset->tiles) {
return QImage();
}
return tileset->tiles->value(local_index, QImage());
}

14
ui/imageproviders.h Normal file
View file

@ -0,0 +1,14 @@
#ifndef IMAGEPROVIDERS_H
#define IMAGEPROVIDERS_H
#include "block.h"
#include "tileset.h"
#include <QImage>
#include <QPixmap>
QImage getCollisionMetatileImage(Block);
QImage getCollisionMetatileImage(int, int);
QImage getMetatileImage(int, Tileset*, Tileset*);
QImage getTileImage(int, Tileset*, Tileset*);
#endif // IMAGEPROVIDERS_H

View file

@ -1,3 +1,4 @@
#include "imageproviders.h"
#include "metatileselector.h"
#include "project.h"
#include <QPainter>
@ -26,7 +27,7 @@ void MetatileSelector::draw() {
if (i >= primaryLength) {
tile += Project::getNumMetatilesPrimary() - primaryLength;
}
QImage metatile_image = Tileset::getMetatileImage(tile, this->primaryTileset, this->secondaryTileset);
QImage metatile_image = getMetatileImage(tile, this->primaryTileset, this->secondaryTileset);
int map_y = i / this->numMetatilesWide;
int map_x = i % this->numMetatilesWide;
QPoint metatile_origin = QPoint(map_x * 16, map_y * 16);