porymap/src/ui/bordermetatilespixmapitem.cpp

47 lines
1.7 KiB
C++
Raw Normal View History

2018-09-27 00:30:05 +01:00
#include "bordermetatilespixmapitem.h"
#include "imageproviders.h"
#include <QPainter>
void BorderMetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
QList<uint16_t> *selectedMetatiles = this->metatileSelector->getSelectedMetatiles();
QPoint selectionDimensions = this->metatileSelector->getSelectionDimensions();
QPointF pos = event->pos();
int x = static_cast<int>(pos.x()) / 16;
int y = static_cast<int>(pos.y()) / 16;
2020-03-14 07:44:55 +00:00
int width = map->getBorderWidth();
int height = map->getBorderHeight();
2018-09-27 00:30:05 +01:00
2020-03-14 07:44:55 +00:00
for (int i = 0; i < selectionDimensions.x() && (i + x) < width; i++) {
for (int j = 0; j < selectionDimensions.y() && (j + y) < height; j++) {
int blockIndex = (j + y) * width + (i + x);
2018-09-27 00:30:05 +01:00
uint16_t tile = selectedMetatiles->at(j * selectionDimensions.x() + i);
(*map->layout->border->blocks)[blockIndex].tile = tile;
}
}
draw();
emit borderMetatilesChanged();
}
void BorderMetatilesPixmapItem::draw() {
2020-03-14 07:44:55 +00:00
int width = map->getBorderWidth();
int height = map->getBorderHeight();
QImage image(16 * width, 16 * height, QImage::Format_RGBA8888);
2018-09-27 00:30:05 +01:00
QPainter painter(&image);
QVector<Block> *blocks = map->layout->border->blocks;
2018-09-27 00:30:05 +01:00
2020-03-14 07:44:55 +00:00
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
2018-09-27 00:30:05 +01:00
int x = i * 16;
int y = j * 16;
2020-03-14 07:44:55 +00:00
int index = j * width + i;
2018-09-27 00:30:05 +01:00
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);
}
}
painter.end();
this->setPixmap(QPixmap::fromImage(image));
}