porymap/src/ui/bordermetatilespixmapitem.cpp

60 lines
2.1 KiB
C++
Raw Normal View History

2018-09-27 00:30:05 +01:00
#include "bordermetatilespixmapitem.h"
#include "imageproviders.h"
#include "metatile.h"
2020-07-29 20:51:04 +01:00
#include "editcommands.h"
2018-09-27 00:30:05 +01:00
#include <QPainter>
void BorderMetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
QList<uint16_t> *selectedMetatiles = this->metatileSelector->getSelectedMetatiles();
QPoint selectionDimensions = this->metatileSelector->getSelectionDimensions();
QPoint pos = Metatile::coordFromPixmapCoord(event->pos());
2020-03-14 07:44:55 +00:00
int width = map->getBorderWidth();
int height = map->getBorderHeight();
2018-09-27 00:30:05 +01:00
2021-02-14 21:34:17 +00:00
Blockdata oldBorder = map->layout->border;
2020-07-29 20:51:04 +01:00
for (int i = 0; i < selectionDimensions.x() && (i + pos.x()) < width; i++) {
for (int j = 0; j < selectionDimensions.y() && (j + pos.y()) < height; j++) {
int blockIndex = (j + pos.y()) * width + (i + pos.x());
2018-09-27 00:30:05 +01:00
uint16_t tile = selectedMetatiles->at(j * selectionDimensions.x() + i);
2021-02-14 21:34:17 +00:00
map->layout->border[blockIndex].tile = tile;
2018-09-27 00:30:05 +01:00
}
}
2021-02-14 21:56:23 +00:00
if (map->layout->border != oldBorder) {
map->editHistory.push(new PaintBorder(map, oldBorder, map->layout->border, 0));
}
2020-07-29 20:51:04 +01:00
2018-09-27 00:30:05 +01:00
emit borderMetatilesChanged();
}
void BorderMetatilesPixmapItem::draw() {
2020-07-29 20:51:04 +01:00
map->setBorderItem(this);
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);
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;
QImage metatile_image = getMetatileImage(
2021-02-14 21:34:17 +00:00
map->layout->border.value(index).tile,
map->layout->tileset_primary,
map->layout->tileset_secondary,
map->metatileLayerOrder,
map->metatileLayerOpacity);
2018-09-27 00:30:05 +01:00
QPoint metatile_origin = QPoint(x, y);
painter.drawImage(metatile_origin, metatile_image);
}
}
painter.end();
this->setPixmap(QPixmap::fromImage(image));
2020-07-29 20:51:04 +01:00
emit borderMetatilesChanged();
2018-09-27 00:30:05 +01:00
}