From f0f0a1567df1919af23702fd8b9ebbc228555d42 Mon Sep 17 00:00:00 2001 From: nordsoft Date: Mon, 5 Sep 2022 16:28:47 +0400 Subject: [PATCH] Minimap camera controls --- mapeditor/mainwindow.cpp | 2 +- mapeditor/mapview.cpp | 43 +++++++++++++++++++++++++++++++++++----- mapeditor/mapview.h | 13 +++++++++--- mapeditor/scenelayer.cpp | 43 ++++++++++++++++++++++++++++++++++++++++ mapeditor/scenelayer.h | 20 +++++++++++++++++++ 5 files changed, 112 insertions(+), 9 deletions(-) diff --git a/mapeditor/mainwindow.cpp b/mapeditor/mainwindow.cpp index 4969075a6..16101a07f 100644 --- a/mapeditor/mainwindow.cpp +++ b/mapeditor/mainwindow.cpp @@ -122,6 +122,7 @@ MainWindow::MainWindow(QWidget *parent) : ui->minimapView->setScene(controller.miniScene(0)); ui->minimapView->setController(&controller); + connect(ui->minimapView, &MinimapView::cameraPositionChanged, ui->mapView, &MapView::cameraChanged); scenePreview = new QGraphicsScene(this); ui->objectPreview->setScene(scenePreview); @@ -771,4 +772,3 @@ void MainWindow::on_actionPlayers_settings_triggered() auto mapSettingsDialog = new PlayerSettings(*controller.map(), this); mapSettingsDialog->setModal(true); } - diff --git a/mapeditor/mapview.cpp b/mapeditor/mapview.cpp index 3a64e0dc0..304ce379a 100644 --- a/mapeditor/mapview.cpp +++ b/mapeditor/mapview.cpp @@ -22,17 +22,28 @@ void MinimapView::setController(MapController * ctrl) controller = ctrl; } -void MinimapView::mousePressEvent(QMouseEvent *event) +void MinimapView::mouseMoveEvent(QMouseEvent *mouseEvent) { this->update(); - auto * sc = static_cast(scene()); + auto * sc = static_cast(scene()); if(!sc) return; + + int w = sc->viewport.viewportWidth(); + int h = sc->viewport.viewportHeight(); + auto pos = mapToScene(mouseEvent->pos()); + pos.setX(pos.x() - w / 2); + pos.setY(pos.y() - h / 2); + + QPointF point = pos * 32; + + emit cameraPositionChanged(point); } -void MinimapView::cameraPositionChange(const QPoint & newPosition) +void MinimapView::mousePressEvent(QMouseEvent* event) { + mouseMoveEvent(event); } MapView::MapView(QWidget * parent): @@ -41,6 +52,14 @@ MapView::MapView(QWidget * parent): { } +void MapView::cameraChanged(const QPointF & pos) +{ + //ui->mapView->translate(pos.x(), pos.y()); + horizontalScrollBar()->setValue(pos.x()); + verticalScrollBar()->setValue(pos.y()); +} + + void MapView::setController(MapController * ctrl) { controller = ctrl; @@ -304,6 +323,17 @@ void MapView::mouseReleaseEvent(QMouseEvent *event) } } +bool MapView::viewportEvent(QEvent *event) +{ + if(auto * sc = static_cast(scene())) + { + //auto rect = sceneRect(); + auto rect = mapToScene(viewport()->geometry()).boundingRect(); + controller->miniScene(sc->level)->viewport.setViewport(rect.x() / 32, rect.y() / 32, rect.width() / 32, rect.height() / 32); + } + return QGraphicsView::viewportEvent(event); +} + MapSceneBase::MapSceneBase(int lvl): QGraphicsScene(nullptr), level(lvl) @@ -358,7 +388,8 @@ void MapScene::updateViews() MinimapScene::MinimapScene(int lvl): MapSceneBase(lvl), - minimapView(this) + minimapView(this), + viewport(this) { } @@ -366,7 +397,8 @@ std::list MinimapScene::getAbstractLayers() { //sequence is important because it defines rendering order return { - &minimapView + &minimapView, + &viewport }; } @@ -375,4 +407,5 @@ void MinimapScene::updateViews() MapSceneBase::updateViews(); minimapView.show(true); + viewport.show(true); } diff --git a/mapeditor/mapview.h b/mapeditor/mapview.h index 288f28bbd..43012f9b4 100644 --- a/mapeditor/mapview.h +++ b/mapeditor/mapview.h @@ -33,6 +33,7 @@ public: void updateViews() override; MinimapLayer minimapView; + MinimapViewLayer viewport; protected: virtual std::list getAbstractLayers(); @@ -76,9 +77,15 @@ public slots: void mousePressEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override; + void cameraChanged(const QPointF & pos); + signals: void openObjectProperties(CGObjectInstance *); + //void viewportChanged(const QRectF & rect); +protected: + bool viewportEvent(QEvent *event) override; + private: MapController * controller = nullptr; QPointF mouseStart; @@ -97,11 +104,11 @@ public: void dimensions(); public slots: - void mousePressEvent(QMouseEvent *event) override; - void cameraPositionChange(const QPoint & newPosition); + void mouseMoveEvent(QMouseEvent * mouseEvent) override; + void mousePressEvent(QMouseEvent* event) override; signals: - void cameraPositionChanged(const QPoint & newPosition); + void cameraPositionChanged(const QPointF & newPosition); private: MapController * controller = nullptr; diff --git a/mapeditor/scenelayer.cpp b/mapeditor/scenelayer.cpp index 496e8c49c..669d4dfdc 100644 --- a/mapeditor/scenelayer.cpp +++ b/mapeditor/scenelayer.cpp @@ -496,3 +496,46 @@ void MinimapLayer::update() redraw(); } + +MinimapViewLayer::MinimapViewLayer(MapSceneBase * s): AbstractLayer(s) +{ +} + +void MinimapViewLayer::update() +{ + if(!map) + return; + + pixmap.reset(new QPixmap(map->width, map->height)); + pixmap->fill(QColor(0, 0, 0, 0)); + + QPainter painter(pixmap.get()); + painter.setPen(QColor(255, 255, 255)); + painter.drawRect(x, y, w, h); + + redraw(); +} + +void MinimapViewLayer::draw() +{ + if(!map) + return; + + pixmap->fill(QColor(0, 0, 0, 0)); + + //maybe not optimal but ok + QPainter painter(pixmap.get()); + painter.setPen(QColor(255, 255, 255)); + painter.drawRect(x, y, w, h); + + redraw(); +} + +void MinimapViewLayer::setViewport(int _x, int _y, int _w, int _h) +{ + x = _x; + y = _y; + w = _w; + h = _h; + draw(); +} diff --git a/mapeditor/scenelayer.h b/mapeditor/scenelayer.h index 17b9a5ba5..a5318f818 100644 --- a/mapeditor/scenelayer.h +++ b/mapeditor/scenelayer.h @@ -135,4 +135,24 @@ public: void update() override; }; +class MinimapViewLayer: public AbstractLayer +{ +public: + MinimapViewLayer(MapSceneBase * s); + + void setViewport(int x, int y, int w, int h); + + void draw(); + void update() override; + + int viewportX() const {return x;} + int viewportY() const {return y;} + int viewportWidth() const {return w;} + int viewportHeight() const {return h;} + +private: + int x = 0, y = 0, w = 1, h = 1; + +}; + #endif // SCENELAYER_H