2018-12-28 18:26:18 +00:00
|
|
|
#include "tilemaptileselector.h"
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
void TilemapTileSelector::draw() {
|
2022-03-01 20:32:44 +00:00
|
|
|
size_t width_ = this->tileset.width();
|
2018-12-28 18:26:18 +00:00
|
|
|
this->pixelWidth = width_;
|
2022-03-01 20:32:44 +00:00
|
|
|
size_t height_ = this->tileset.height();
|
2018-12-28 18:26:18 +00:00
|
|
|
this->pixelHeight = height_;
|
2019-01-05 04:04:14 +00:00
|
|
|
size_t ntiles_ = (width_/8) * (height_/8);
|
2018-12-28 18:26:18 +00:00
|
|
|
|
|
|
|
this->numTilesWide = width_ / 8;
|
|
|
|
this->numTiles = ntiles_;
|
|
|
|
|
2022-03-01 20:32:44 +00:00
|
|
|
this->setPixmap(QPixmap::fromImage(tileset));
|
2018-12-28 18:26:18 +00:00
|
|
|
this->drawSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TilemapTileSelector::select(unsigned tileId) {
|
|
|
|
QPoint coords = this->getTileIdCoords(tileId);
|
|
|
|
SelectablePixmapItem::select(coords.x(), coords.y(), 0, 0);
|
|
|
|
this->selectedTile = tileId;
|
|
|
|
emit selectedTileChanged(tileId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TilemapTileSelector::updateSelectedTile() {
|
|
|
|
QPoint origin = this->getSelectionStart();
|
|
|
|
this->selectedTile = this->getTileId(origin.x(), origin.y());
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned TilemapTileSelector::getTileId(int x, int y) {
|
2019-08-07 04:35:02 +01:00
|
|
|
unsigned index = y * this->numTilesWide + x;
|
2018-12-28 18:26:18 +00:00
|
|
|
return index < this->numTiles ? index : this->numTiles % index;
|
|
|
|
}
|
|
|
|
|
2019-01-28 18:47:20 +00:00
|
|
|
QPoint TilemapTileSelector::getTileIdCoords(unsigned tileId) {
|
|
|
|
int index = tileId < this->numTiles ? tileId : this->numTiles % tileId;
|
|
|
|
return QPoint(index % this->numTilesWide, index / this->numTilesWide);
|
|
|
|
}
|
|
|
|
|
2022-03-01 20:32:44 +00:00
|
|
|
QImage TilemapTileSelector::tileImg(shared_ptr<TilemapTile> tile) {
|
|
|
|
// TODO: this is slow on the entries tab, so maybe do not do all of the redraw for every section change
|
|
|
|
unsigned tileId = tile->id();
|
2019-01-28 18:47:20 +00:00
|
|
|
QPoint pos = getTileIdCoords(tileId);
|
2022-03-01 20:32:44 +00:00
|
|
|
|
|
|
|
QImage tilesetImage = this->tileset;
|
|
|
|
tilesetImage.convertTo(QImage::Format::Format_Indexed8);
|
|
|
|
|
|
|
|
// TODO: bounds check on the palette copying
|
|
|
|
|
|
|
|
switch(this->format) {
|
|
|
|
case TilemapFormat::Plain:
|
|
|
|
{
|
|
|
|
// TODO: even allow palettes for Plain tiles?
|
|
|
|
// 1 x palette x any colors
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TilemapFormat::BPP_4:
|
|
|
|
{
|
|
|
|
// before Qt 6, the color table is a QVector which is deprecated now, and this method does not exits
|
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
tilesetImage.setColorTable(this->palette.toVector().mid(tile->palette() * 16, 16));
|
|
|
|
#else
|
|
|
|
tilesetImage.setColorTable(this->palette.mid(tile->palette() * 16, 16));
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TilemapFormat::BPP_8:
|
|
|
|
{
|
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
tilesetImage.setColorTable(this->palette.toVector());
|
|
|
|
#else
|
|
|
|
tilesetImage.setColorTable(this->palette);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// take a tile from the tileset
|
|
|
|
QImage img = tilesetImage.copy(pos.x() * 8, pos.y() * 8, 8, 8);
|
|
|
|
img = img.mirrored(tile->hFlip(), tile->vFlip());
|
|
|
|
return img;
|
2019-01-28 18:47:20 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 18:26:18 +00:00
|
|
|
void TilemapTileSelector::mousePressEvent(QGraphicsSceneMouseEvent *event) {
|
|
|
|
SelectablePixmapItem::mousePressEvent(event);
|
|
|
|
this->updateSelectedTile();
|
|
|
|
emit selectedTileChanged(this->selectedTile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TilemapTileSelector::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
|
|
|
|
SelectablePixmapItem::mouseMoveEvent(event);
|
|
|
|
this->updateSelectedTile();
|
|
|
|
emit hoveredTileChanged(this->selectedTile);
|
|
|
|
emit selectedTileChanged(this->selectedTile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TilemapTileSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
|
|
|
|
SelectablePixmapItem::mouseReleaseEvent(event);
|
|
|
|
this->updateSelectedTile();
|
|
|
|
emit selectedTileChanged(this->selectedTile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TilemapTileSelector::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
|
|
|
|
QPoint pos = this->getCellPos(event->pos());
|
|
|
|
unsigned tileId = this->getTileId(pos.x(), pos.y());
|
|
|
|
emit this->hoveredTileChanged(tileId);
|
|
|
|
}
|
|
|
|
|
2019-08-07 04:35:02 +01:00
|
|
|
void TilemapTileSelector::hoverLeaveEvent(QGraphicsSceneHoverEvent *) {
|
2018-12-28 18:26:18 +00:00
|
|
|
emit this->hoveredTileCleared();
|
|
|
|
}
|