From 93cf929f29dea490ed60e5300cacdd99886c988e Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Sat, 9 Sep 2023 20:09:50 -0400 Subject: Add the standalone portion of mapman. --- mapman/src/painter.cpp | 113 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 mapman/src/painter.cpp (limited to 'mapman/src/painter.cpp') diff --git a/mapman/src/painter.cpp b/mapman/src/painter.cpp new file mode 100644 index 0000000..876184e --- /dev/null +++ b/mapman/src/painter.cpp @@ -0,0 +1,113 @@ +#include "painter.hpp" +#include "library.hpp" +#include "utils.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include + +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(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); + 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); + 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; + } + 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); + int x = pos / hc; + int y = pos % hc; + p->setPos(y * 128, x * 128); + slices[pos] = p; + } + if (user_input) + emit map_id_changed(pos, populated, id); +} + +void map_painter::set_map_library(map_library *lib) { l = lib; } -- cgit v1.2.3