1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00

Minimap camera controls

This commit is contained in:
nordsoft 2022-09-05 16:28:47 +04:00
parent 1e15a7c9c8
commit f0f0a1567d
5 changed files with 112 additions and 9 deletions

View File

@ -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);
}

View File

@ -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<MapScene*>(scene());
auto * sc = static_cast<MinimapScene*>(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<MapScene*>(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<AbstractLayer *> 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);
}

View File

@ -33,6 +33,7 @@ public:
void updateViews() override;
MinimapLayer minimapView;
MinimapViewLayer viewport;
protected:
virtual std::list<AbstractLayer *> getAbstractLayers();
@ -76,8 +77,14 @@ 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;
@ -97,11 +104,11 @@ public:
void dimensions();
public slots:
void mouseMoveEvent(QMouseEvent * mouseEvent) override;
void mousePressEvent(QMouseEvent* event) override;
void cameraPositionChange(const QPoint & newPosition);
signals:
void cameraPositionChanged(const QPoint & newPosition);
void cameraPositionChanged(const QPointF & newPosition);
private:
MapController * controller = nullptr;

View File

@ -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();
}

View File

@ -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