#include "painter.hpp"
#include "library.hpp"
#include "utils.hpp"
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsSimpleTextItem>
#include <QGraphicsRectItem>
#include <QGraphicsPixmapItem>
#include <QGraphicsSceneDragDropEvent>
#include <QMimeData>
#include <QPixmapCache>
class drop_rect : public QGraphicsRectItem
{
public:
drop_rect(int pos, map_painter *painter);
signals:
void dropped(int pos, int id);
protected:
void dragEnterEvent(QGraphicsSceneDragDropEvent *e);
void dropEvent(QGraphicsSceneDragDropEvent *e);
private:
int p;
map_painter *pt;
};
drop_rect::drop_rect(int pos, map_painter *painter) :
QGraphicsRectItem(QRectF(0, 0, 128, 128), nullptr),
p(pos),
pt(painter)
{
setAcceptDrops(true);
setPen(QPen(Qt::GlobalColor::black, 4));
}
void drop_rect::dragEnterEvent(QGraphicsSceneDragDropEvent *e)
{
auto ba = e->mimeData()->data("application/x-map-id");
e->setAccepted(ba.length() >= 4);
}
void drop_rect::dropEvent(QGraphicsSceneDragDropEvent *e)
{
auto ba = e->mimeData()->data("application/x-map-id");
if (ba.length() >= 4)
{
e->setDropAction(Qt::DropAction::CopyAction);
pt->set_map_id(p, true, *reinterpret_cast<int*>(ba.data()), true);
}
else e->setDropAction(Qt::DropAction::IgnoreAction);
}
map_painter::map_painter() : l(nullptr)
{
s = new QGraphicsScene(0, 0, 0, 0);
v = new QGraphicsView(s);
hc = vc = 0;
}
map_painter::~map_painter()
{
delete s;
delete v;
}
void map_painter::set_dimension(int h, int v)
{
hc = h;
vc = v;
s->setSceneRect(0, 0, hc * 128, vc * 128);
s->clear();
slices.clear();
slices.resize(hc * vc, nullptr);
bgspr.clear();
bgspr.resize(hc * vc, {nullptr, nullptr});
for (int i = 0; i < vc; ++i)
for (int j = 0; j < hc; ++j)
{
auto t = s->addSimpleText(QString::number(i * hc + j));
t->setPos(j * 128 + 64 - t->boundingRect().width() / 2, i * 128 + 64 - t->boundingRect().height() / 2);
auto r = new drop_rect(i * hc + j, this);
s->addItem(r);
bgspr[i * hc + j] = {t, r};
r->setPos(j * 128, i * 128);
}
}
void map_painter::set_map_id(int pos, bool populated, int id, bool user_input)
{
if (!l || pos >= hc * vc || pos < 0) return;
if (slices[pos])
{
s->removeItem(slices[pos]);
delete slices[pos];
slices[pos] = nullptr;
}
auto [t, r] = bgspr[pos];
if (populated)
{
QPixmap pm;
if (!QPixmapCache::find(QString("map_%1").arg(id), &pm))
{
pm = pixmap_of_map_data(l->get_map(id).map_data);
QPixmapCache::insert(QString("map_%1").arg(id), pm);
}
auto p = s->addPixmap(pm);
auto m = l->get_map(id);
p->setToolTip(QString("%1\nId #%2%3")
.arg(m.custom_name.empty() ? QString("Map") : QString::fromStdString(m.custom_name))
.arg(m.id)
.arg(m.locked ? "\nLocked" : ""));
int x = pos / hc;
int y = pos % hc;
p->setPos(y * 128, x * 128);
slices[pos] = p;
static_cast<QAbstractGraphicsShapeItem*>(t)->setPen(QColor(Qt::GlobalColor::transparent));
static_cast<QAbstractGraphicsShapeItem*>(r)->setPen(QColor(Qt::GlobalColor::transparent));
}
if (user_input)
emit map_id_changed(pos, populated, id);
}
void map_painter::set_map_library(map_library *lib) { l = lib; }