porymap/src/ui/citymappixmapitem.cpp

113 lines
2.9 KiB
C++
Raw Normal View History

2019-01-05 22:59:57 +00:00
#include "citymappixmapitem.h"
#include "imageproviders.h"
#include "config.h"
#include "log.h"
2019-01-05 22:59:57 +00:00
#include <QFile>
#include <QPainter>
#include <QDebug>
void CityMapPixmapItem::init() {
width_ = 10;
height_ = 10;
2019-01-05 22:59:57 +00:00
QFile binFile(file);
if (!binFile.open(QIODevice::ReadOnly)) return;
data = binFile.readAll();
if (projectConfig.getBaseGameVersion() == BaseGameVersion::pokeruby) {
for (int i = 0; i < data.size(); i++)
data[i] = data[i] ^ 0x80;
}
2019-01-05 22:59:57 +00:00
binFile.close();
}
void CityMapPixmapItem::draw() {
QImage image(width_ * 8, height_ * 8, QImage::Format_RGBA8888);
2019-01-05 22:59:57 +00:00
// TODO: construct temporary tile from this based on the id?
// QPainter painter(&image);
// for (int i = 0; i < data.size() / 2; i++) {
// QImage img = this->tile_selector->tileImg(data[i * 2]);// need to skip every other tile
// int x = i % width_;
// int y = i / width_;
// QPoint pos = QPoint(x * 8, y * 8);
// painter.drawImage(pos, img);
// }
// painter.end();
2019-01-05 22:59:57 +00:00
this->setPixmap(QPixmap::fromImage(image));
}
void CityMapPixmapItem::save() {
QFile binFile(file);
if (!binFile.open(QIODevice::WriteOnly)) {
logError(QString("Cannot save city map tilemap to %1.").arg(file));
return;
}
if (projectConfig.getBaseGameVersion() == BaseGameVersion::pokeruby) {
for (int i = 0; i < data.size(); i++)
data[i] = data[i] ^ 0x80;
}
2019-01-05 22:59:57 +00:00
binFile.write(data);
binFile.close();
}
void CityMapPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
QPointF pos = event->pos();
int x = static_cast<int>(pos.x()) / 8;
int y = static_cast<int>(pos.y()) / 8;
int index = getIndexAt(x, y);
2019-01-05 22:59:57 +00:00
data[index] = static_cast<uint8_t>(this->tile_selector->selectedTile);
draw();
}
void CityMapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
emit mouseEvent(event, this);
}
2019-01-22 20:06:49 +00:00
void CityMapPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
QPointF pos = event->pos();
int x = static_cast<int>(pos.x()) / 8;
int y = static_cast<int>(pos.y()) / 8;
if (x < width_ && x >= 0
&& y < height_ && y >= 0) {
emit this->hoveredRegionMapTileChanged(x, y);
emit mouseEvent(event, this);
}
}
void CityMapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
emit mouseEvent(event, this);
}
QVector<uint8_t> CityMapPixmapItem::getTiles() {
QVector<uint8_t> tiles;
for (auto tile : data) {
tiles.append(tile);
}
return tiles;
}
void CityMapPixmapItem::setTiles(QVector<uint8_t> tiles) {
QByteArray newData;
for (auto tile : tiles) {
newData.append(tile);
}
this->data = newData;
}
int CityMapPixmapItem::getIndexAt(int x, int y) {
return 2 * (x + y * this->width_);
}
int CityMapPixmapItem::width() {
return this->width_;
}
int CityMapPixmapItem::height() {
return this->height_;
}