1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-18 17:40:48 +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->setScene(controller.miniScene(0));
ui->minimapView->setController(&controller); ui->minimapView->setController(&controller);
connect(ui->minimapView, &MinimapView::cameraPositionChanged, ui->mapView, &MapView::cameraChanged);
scenePreview = new QGraphicsScene(this); scenePreview = new QGraphicsScene(this);
ui->objectPreview->setScene(scenePreview); ui->objectPreview->setScene(scenePreview);
@ -771,4 +772,3 @@ void MainWindow::on_actionPlayers_settings_triggered()
auto mapSettingsDialog = new PlayerSettings(*controller.map(), this); auto mapSettingsDialog = new PlayerSettings(*controller.map(), this);
mapSettingsDialog->setModal(true); mapSettingsDialog->setModal(true);
} }

View File

@ -22,17 +22,28 @@ void MinimapView::setController(MapController * ctrl)
controller = ctrl; controller = ctrl;
} }
void MinimapView::mousePressEvent(QMouseEvent *event) void MinimapView::mouseMoveEvent(QMouseEvent *mouseEvent)
{ {
this->update(); this->update();
auto * sc = static_cast<MapScene*>(scene()); auto * sc = static_cast<MinimapScene*>(scene());
if(!sc) if(!sc)
return; 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): 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) void MapView::setController(MapController * ctrl)
{ {
controller = 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): MapSceneBase::MapSceneBase(int lvl):
QGraphicsScene(nullptr), QGraphicsScene(nullptr),
level(lvl) level(lvl)
@ -358,7 +388,8 @@ void MapScene::updateViews()
MinimapScene::MinimapScene(int lvl): MinimapScene::MinimapScene(int lvl):
MapSceneBase(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 //sequence is important because it defines rendering order
return { return {
&minimapView &minimapView,
&viewport
}; };
} }
@ -375,4 +407,5 @@ void MinimapScene::updateViews()
MapSceneBase::updateViews(); MapSceneBase::updateViews();
minimapView.show(true); minimapView.show(true);
viewport.show(true);
} }

View File

@ -33,6 +33,7 @@ public:
void updateViews() override; void updateViews() override;
MinimapLayer minimapView; MinimapLayer minimapView;
MinimapViewLayer viewport;
protected: protected:
virtual std::list<AbstractLayer *> getAbstractLayers(); virtual std::list<AbstractLayer *> getAbstractLayers();
@ -76,8 +77,14 @@ public slots:
void mousePressEvent(QMouseEvent *event) override; void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override;
void cameraChanged(const QPointF & pos);
signals: signals:
void openObjectProperties(CGObjectInstance *); void openObjectProperties(CGObjectInstance *);
//void viewportChanged(const QRectF & rect);
protected:
bool viewportEvent(QEvent *event) override;
private: private:
MapController * controller = nullptr; MapController * controller = nullptr;
@ -97,11 +104,11 @@ public:
void dimensions(); void dimensions();
public slots: public slots:
void mousePressEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent * mouseEvent) override;
void cameraPositionChange(const QPoint & newPosition); void mousePressEvent(QMouseEvent* event) override;
signals: signals:
void cameraPositionChanged(const QPoint & newPosition); void cameraPositionChanged(const QPointF & newPosition);
private: private:
MapController * controller = nullptr; MapController * controller = nullptr;

View File

@ -496,3 +496,46 @@ void MinimapLayer::update()
redraw(); 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; 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 #endif // SCENELAYER_H