2022-10-12 23:51:55 +02:00
|
|
|
/*
|
|
|
|
* mapview.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-09-18 01:23:17 +02:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "mapview.h"
|
|
|
|
#include "mainwindow.h"
|
|
|
|
#include <QGraphicsSceneMouseEvent>
|
|
|
|
#include "mapcontroller.h"
|
2023-06-02 20:47:37 +02:00
|
|
|
#include "../lib/mapObjectConstructors/AObjectTypeHandler.h"
|
|
|
|
#include "../lib/mapObjectConstructors/CObjectClassesHandler.h"
|
2023-05-24 01:05:59 +02:00
|
|
|
#include "../lib/mapping/CMap.h"
|
2024-05-17 00:05:51 +02:00
|
|
|
#include "../lib/VCMI_Lib.h"
|
2022-09-18 01:23:17 +02:00
|
|
|
|
2023-06-02 20:47:37 +02:00
|
|
|
|
2022-09-18 01:23:17 +02:00
|
|
|
MinimapView::MinimapView(QWidget * parent):
|
|
|
|
QGraphicsView(parent)
|
|
|
|
{
|
|
|
|
// Disable scrollbars
|
|
|
|
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MinimapView::dimensions()
|
|
|
|
{
|
|
|
|
fitInView(0, 0, controller->map()->width, controller->map()->height, Qt::KeepAspectRatio);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MinimapView::setController(MapController * ctrl)
|
|
|
|
{
|
|
|
|
controller = ctrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MinimapView::mouseMoveEvent(QMouseEvent *mouseEvent)
|
|
|
|
{
|
|
|
|
this->update();
|
|
|
|
|
|
|
|
auto * sc = static_cast<MinimapScene*>(scene());
|
|
|
|
if(!sc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto pos = mapToScene(mouseEvent->pos());
|
2023-10-13 05:21:09 +02:00
|
|
|
pos *= 32;
|
|
|
|
emit cameraPositionChanged(pos);
|
2022-09-18 01:23:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MinimapView::mousePressEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
mouseMoveEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
MapView::MapView(QWidget * parent):
|
|
|
|
QGraphicsView(parent),
|
|
|
|
selectionTool(MapView::SelectionTool::None)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapView::cameraChanged(const QPointF & pos)
|
|
|
|
{
|
2023-10-13 05:21:09 +02:00
|
|
|
centerOn(pos);
|
2022-09-18 01:23:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapView::setController(MapController * ctrl)
|
|
|
|
{
|
|
|
|
controller = ctrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapView::mouseMoveEvent(QMouseEvent *mouseEvent)
|
|
|
|
{
|
|
|
|
this->update();
|
|
|
|
|
|
|
|
auto * sc = static_cast<MapScene*>(scene());
|
|
|
|
if(!sc || !controller->map())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto pos = mapToScene(mouseEvent->pos()); //TODO: do we need to check size?
|
|
|
|
int3 tile(pos.x() / 32, pos.y() / 32, sc->level);
|
2022-12-04 12:51:01 +02:00
|
|
|
|
|
|
|
//if scene will be scrolled without mouse movement, selection, object moving and rubber band will not be updated
|
|
|
|
//to change this behavior, all this logic should be placed in viewportEvent
|
|
|
|
if(rubberBand)
|
|
|
|
rubberBand->setGeometry(QRect(mapFromScene(mouseStart), mouseEvent->pos()).normalized());
|
|
|
|
|
2022-09-18 01:23:17 +02:00
|
|
|
if(tile == tilePrev) //do not redraw
|
|
|
|
return;
|
|
|
|
|
2023-10-08 20:25:59 +02:00
|
|
|
emit currentCoordinates(tile.x, tile.y);
|
2022-09-18 01:23:17 +02:00
|
|
|
|
|
|
|
switch(selectionTool)
|
|
|
|
{
|
|
|
|
case MapView::SelectionTool::Brush:
|
|
|
|
if(mouseEvent->buttons() & Qt::RightButton)
|
|
|
|
sc->selectionTerrainView.erase(tile);
|
|
|
|
else if(mouseEvent->buttons() == Qt::LeftButton)
|
|
|
|
sc->selectionTerrainView.select(tile);
|
|
|
|
sc->selectionTerrainView.draw();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MapView::SelectionTool::Brush2:
|
|
|
|
{
|
|
|
|
std::array<int3, 4> extra{ int3{0, 0, 0}, int3{1, 0, 0}, int3{0, 1, 0}, int3{1, 1, 0} };
|
|
|
|
for(auto & e : extra)
|
|
|
|
{
|
|
|
|
if(mouseEvent->buttons() & Qt::RightButton)
|
|
|
|
sc->selectionTerrainView.erase(tile + e);
|
|
|
|
else if(mouseEvent->buttons() == Qt::LeftButton)
|
|
|
|
sc->selectionTerrainView.select(tile + e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sc->selectionTerrainView.draw();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MapView::SelectionTool::Brush4:
|
|
|
|
{
|
|
|
|
std::array<int3, 16> extra{
|
|
|
|
int3{-1, -1, 0}, int3{0, -1, 0}, int3{1, -1, 0}, int3{2, -1, 0},
|
|
|
|
int3{-1, 0, 0}, int3{0, 0, 0}, int3{1, 0, 0}, int3{2, 0, 0},
|
|
|
|
int3{-1, 1, 0}, int3{0, 1, 0}, int3{1, 1, 0}, int3{2, 1, 0},
|
|
|
|
int3{-1, 2, 0}, int3{0, 2, 0}, int3{1, 2, 0}, int3{2, 2, 0}
|
|
|
|
};
|
|
|
|
for(auto & e : extra)
|
|
|
|
{
|
|
|
|
if(mouseEvent->buttons() & Qt::RightButton)
|
|
|
|
sc->selectionTerrainView.erase(tile + e);
|
|
|
|
else if(mouseEvent->buttons() == Qt::LeftButton)
|
|
|
|
sc->selectionTerrainView.select(tile + e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sc->selectionTerrainView.draw();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MapView::SelectionTool::Area:
|
2022-10-08 20:55:15 +02:00
|
|
|
if(mouseEvent->buttons() & Qt::RightButton || !(mouseEvent->buttons() & Qt::LeftButton))
|
2022-09-18 01:23:17 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
sc->selectionTerrainView.clear();
|
|
|
|
for(int j = std::min(tile.y, tileStart.y); j < std::max(tile.y, tileStart.y); ++j)
|
|
|
|
{
|
|
|
|
for(int i = std::min(tile.x, tileStart.x); i < std::max(tile.x, tileStart.x); ++i)
|
|
|
|
{
|
|
|
|
sc->selectionTerrainView.select(int3(i, j, sc->level));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sc->selectionTerrainView.draw();
|
|
|
|
break;
|
2022-12-27 23:14:10 +02:00
|
|
|
|
2023-10-14 02:58:13 +02:00
|
|
|
case MapView::SelectionTool::Line:
|
|
|
|
{
|
|
|
|
assert(tile.z == tileStart.z);
|
|
|
|
const auto diff = tile - tileStart;
|
|
|
|
if(diff == int3{})
|
|
|
|
break;
|
|
|
|
|
|
|
|
const int edge = std::max(abs(diff.x), abs(diff.y));
|
|
|
|
int distMin = std::numeric_limits<int>::max();
|
|
|
|
int3 dir;
|
|
|
|
|
|
|
|
for(auto & d : int3::getDirs())
|
|
|
|
{
|
|
|
|
if(tile.dist2d(d * edge + tileStart) < distMin)
|
|
|
|
{
|
|
|
|
distMin = tile.dist2d(d * edge + tileStart);
|
|
|
|
dir = d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(dir != int3{});
|
|
|
|
|
|
|
|
if(mouseEvent->buttons() == Qt::LeftButton)
|
|
|
|
{
|
|
|
|
for(auto & ts : temporaryTiles)
|
|
|
|
sc->selectionTerrainView.erase(ts);
|
|
|
|
|
|
|
|
for(auto ts = tileStart; ts.dist2d(tileStart) < edge; ts += dir)
|
|
|
|
{
|
|
|
|
if(!controller->map()->isInTheMap(ts))
|
|
|
|
break;
|
|
|
|
if(!sc->selectionTerrainView.selection().count(ts))
|
|
|
|
temporaryTiles.insert(ts);
|
|
|
|
sc->selectionTerrainView.select(ts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(mouseEvent->buttons() == Qt::RightButton)
|
|
|
|
{
|
|
|
|
for(auto & ts : temporaryTiles)
|
|
|
|
sc->selectionTerrainView.select(ts);
|
|
|
|
|
|
|
|
for(auto ts = tileStart; ts.dist2d(tileStart) < edge; ts += dir)
|
|
|
|
{
|
|
|
|
if(!controller->map()->isInTheMap(ts))
|
|
|
|
break;
|
|
|
|
if(sc->selectionTerrainView.selection().count(ts))
|
|
|
|
temporaryTiles.insert(ts);
|
|
|
|
sc->selectionTerrainView.erase(ts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sc->selectionTerrainView.draw();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-12-27 23:14:10 +02:00
|
|
|
case MapView::SelectionTool::Lasso:
|
|
|
|
if(mouseEvent->buttons() == Qt::LeftButton)
|
|
|
|
{
|
2023-10-24 23:58:26 +02:00
|
|
|
for(auto i = tilePrev; i != tile;)
|
|
|
|
{
|
|
|
|
int length = std::numeric_limits<int>::max();
|
|
|
|
int3 dir;
|
|
|
|
for(auto & d : int3::getDirs())
|
|
|
|
{
|
|
|
|
if(tile.dist2dSQ(i + d) < length)
|
|
|
|
{
|
|
|
|
dir = d;
|
|
|
|
length = tile.dist2dSQ(i + d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i += dir;
|
|
|
|
sc->selectionTerrainView.select(i);
|
|
|
|
}
|
2022-12-27 23:14:10 +02:00
|
|
|
sc->selectionTerrainView.draw();
|
|
|
|
}
|
|
|
|
break;
|
2022-09-18 01:23:17 +02:00
|
|
|
|
|
|
|
case MapView::SelectionTool::None:
|
|
|
|
if(mouseEvent->buttons() & Qt::RightButton)
|
|
|
|
break;
|
|
|
|
|
|
|
|
auto sh = tile - tileStart;
|
|
|
|
sc->selectionObjectsView.shift = QPoint(sh.x, sh.y);
|
|
|
|
|
|
|
|
if(sh.x || sh.y)
|
|
|
|
{
|
2022-12-04 12:51:01 +02:00
|
|
|
if(!sc->selectionObjectsView.newObject && (mouseEvent->buttons() & Qt::LeftButton))
|
2022-09-18 01:23:17 +02:00
|
|
|
{
|
2022-10-12 23:40:52 +02:00
|
|
|
if(sc->selectionObjectsView.selectionMode == SelectionObjectsLayer::SELECTION)
|
2022-09-18 01:23:17 +02:00
|
|
|
{
|
|
|
|
sc->selectionObjectsView.clear();
|
|
|
|
sc->selectionObjectsView.selectObjects(tileStart.x, tileStart.y, tile.x, tile.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sc->selectionObjectsView.draw();
|
|
|
|
break;
|
|
|
|
}
|
2023-10-24 23:58:26 +02:00
|
|
|
|
|
|
|
tilePrev = tile;
|
2022-09-18 01:23:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapView::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
this->update();
|
|
|
|
|
|
|
|
auto * sc = static_cast<MapScene*>(scene());
|
|
|
|
if(!sc || !controller->map())
|
|
|
|
return;
|
2023-09-11 18:01:53 +02:00
|
|
|
|
|
|
|
if(sc->objectPickerView.isVisible())
|
|
|
|
return;
|
2022-09-18 01:23:17 +02:00
|
|
|
|
|
|
|
mouseStart = mapToScene(event->pos());
|
|
|
|
tileStart = tilePrev = int3(mouseStart.x() / 32, mouseStart.y() / 32, sc->level);
|
|
|
|
|
|
|
|
if(sc->selectionTerrainView.selection().count(tileStart))
|
|
|
|
pressedOnSelected = true;
|
|
|
|
else
|
|
|
|
pressedOnSelected = false;
|
|
|
|
|
|
|
|
switch(selectionTool)
|
|
|
|
{
|
|
|
|
case MapView::SelectionTool::Brush:
|
2023-10-14 02:58:13 +02:00
|
|
|
case MapView::SelectionTool::Line:
|
2022-09-18 01:23:17 +02:00
|
|
|
sc->selectionObjectsView.clear();
|
|
|
|
sc->selectionObjectsView.draw();
|
|
|
|
|
|
|
|
if(event->button() == Qt::RightButton)
|
|
|
|
sc->selectionTerrainView.erase(tileStart);
|
|
|
|
else if(event->button() == Qt::LeftButton)
|
|
|
|
sc->selectionTerrainView.select(tileStart);
|
|
|
|
sc->selectionTerrainView.draw();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MapView::SelectionTool::Brush2:
|
|
|
|
sc->selectionObjectsView.clear();
|
|
|
|
sc->selectionObjectsView.draw();
|
|
|
|
{
|
|
|
|
std::array<int3, 4> extra{ int3{0, 0, 0}, int3{1, 0, 0}, int3{0, 1, 0}, int3{1, 1, 0} };
|
|
|
|
for(auto & e : extra)
|
|
|
|
{
|
|
|
|
if(event->button() == Qt::RightButton)
|
|
|
|
sc->selectionTerrainView.erase(tileStart + e);
|
|
|
|
else if(event->button() == Qt::LeftButton)
|
|
|
|
sc->selectionTerrainView.select(tileStart + e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sc->selectionTerrainView.draw();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MapView::SelectionTool::Brush4:
|
|
|
|
sc->selectionObjectsView.clear();
|
|
|
|
sc->selectionObjectsView.draw();
|
|
|
|
{
|
|
|
|
std::array<int3, 16> extra{
|
|
|
|
int3{-1, -1, 0}, int3{0, -1, 0}, int3{1, -1, 0}, int3{2, -1, 0},
|
|
|
|
int3{-1, 0, 0}, int3{0, 0, 0}, int3{1, 0, 0}, int3{2, 0, 0},
|
|
|
|
int3{-1, 1, 0}, int3{0, 1, 0}, int3{1, 1, 0}, int3{2, 1, 0},
|
|
|
|
int3{-1, 2, 0}, int3{0, 2, 0}, int3{1, 2, 0}, int3{2, 2, 0}
|
|
|
|
};
|
|
|
|
for(auto & e : extra)
|
|
|
|
{
|
|
|
|
if(event->button() == Qt::RightButton)
|
|
|
|
sc->selectionTerrainView.erase(tileStart + e);
|
|
|
|
else if(event->button() == Qt::LeftButton)
|
|
|
|
sc->selectionTerrainView.select(tileStart + e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sc->selectionTerrainView.draw();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MapView::SelectionTool::Area:
|
2022-12-27 23:14:10 +02:00
|
|
|
case MapView::SelectionTool::Lasso:
|
2022-09-18 01:23:17 +02:00
|
|
|
if(event->button() == Qt::RightButton)
|
|
|
|
break;
|
|
|
|
|
|
|
|
sc->selectionTerrainView.clear();
|
|
|
|
sc->selectionTerrainView.draw();
|
|
|
|
sc->selectionObjectsView.clear();
|
|
|
|
sc->selectionObjectsView.draw();
|
|
|
|
break;
|
2023-10-14 02:58:13 +02:00
|
|
|
|
|
|
|
case MapView::SelectionTool::Fill:
|
|
|
|
{
|
|
|
|
if(event->button() != Qt::RightButton && event->button() != Qt::LeftButton)
|
|
|
|
break;
|
|
|
|
|
|
|
|
std::vector<int3> queue;
|
|
|
|
queue.push_back(tileStart);
|
|
|
|
|
|
|
|
const std::array<int3, 4> dirs{ int3{1, 0, 0}, int3{-1, 0, 0}, int3{0, 1, 0}, int3{0, -1, 0} };
|
|
|
|
|
|
|
|
while(!queue.empty())
|
|
|
|
{
|
|
|
|
auto tile = queue.back();
|
|
|
|
queue.pop_back();
|
|
|
|
if(event->button() == Qt::LeftButton)
|
|
|
|
sc->selectionTerrainView.select(tile);
|
|
|
|
else
|
|
|
|
sc->selectionTerrainView.erase(tile);
|
|
|
|
for(auto & d : dirs)
|
|
|
|
{
|
|
|
|
auto tilen = tile + d;
|
|
|
|
if(!controller->map()->isInTheMap(tilen))
|
|
|
|
continue;
|
|
|
|
if(event->button() == Qt::LeftButton)
|
|
|
|
{
|
|
|
|
if(controller->map()->getTile(tile).roadType
|
|
|
|
&& controller->map()->getTile(tile).roadType != controller->map()->getTile(tilen).roadType)
|
|
|
|
continue;
|
|
|
|
else if(controller->map()->getTile(tile).riverType
|
|
|
|
&& controller->map()->getTile(tile).riverType != controller->map()->getTile(tilen).riverType)
|
|
|
|
continue;
|
|
|
|
else if(controller->map()->getTile(tile).terType != controller->map()->getTile(tilen).terType)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(event->button() == Qt::LeftButton && sc->selectionTerrainView.selection().count(tilen))
|
|
|
|
continue;
|
|
|
|
if(event->button() == Qt::RightButton && !sc->selectionTerrainView.selection().count(tilen))
|
|
|
|
continue;
|
|
|
|
queue.push_back(tilen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sc->selectionTerrainView.draw();
|
|
|
|
sc->selectionObjectsView.clear();
|
|
|
|
sc->selectionObjectsView.draw();
|
|
|
|
break;
|
|
|
|
}
|
2022-09-18 01:23:17 +02:00
|
|
|
|
|
|
|
case MapView::SelectionTool::None:
|
|
|
|
sc->selectionTerrainView.clear();
|
|
|
|
sc->selectionTerrainView.draw();
|
|
|
|
|
|
|
|
if(sc->selectionObjectsView.newObject && sc->selectionObjectsView.isSelected(sc->selectionObjectsView.newObject))
|
|
|
|
{
|
|
|
|
if(event->button() == Qt::RightButton)
|
|
|
|
controller->discardObject(sc->level);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(event->button() == Qt::LeftButton)
|
|
|
|
{
|
2022-12-27 02:04:09 +02:00
|
|
|
//when paste, new object could be beyond initial object so we need to test two objects in order to select new one
|
|
|
|
//if object is pasted at place where is multiple objects then proper selection is not guaranteed
|
|
|
|
auto * firstSelectedObject = sc->selectionObjectsView.selectObjectAt(tileStart.x, tileStart.y);
|
|
|
|
auto * secondSelectedObject = sc->selectionObjectsView.selectObjectAt(tileStart.x, tileStart.y, firstSelectedObject);
|
|
|
|
if(firstSelectedObject)
|
2022-09-18 01:23:17 +02:00
|
|
|
{
|
2022-12-27 02:04:09 +02:00
|
|
|
if(sc->selectionObjectsView.isSelected(firstSelectedObject))
|
2022-09-18 01:23:17 +02:00
|
|
|
{
|
|
|
|
if(qApp->keyboardModifiers() & Qt::ControlModifier)
|
|
|
|
{
|
2022-12-27 02:04:09 +02:00
|
|
|
sc->selectionObjectsView.deselectObject(firstSelectedObject);
|
2022-10-12 23:40:52 +02:00
|
|
|
sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::SELECTION;
|
2022-09-18 01:23:17 +02:00
|
|
|
}
|
|
|
|
else
|
2022-10-12 23:40:52 +02:00
|
|
|
sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
|
2022-09-18 01:23:17 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-27 02:04:09 +02:00
|
|
|
if(!secondSelectedObject || !sc->selectionObjectsView.isSelected(secondSelectedObject))
|
2022-12-04 23:32:50 +02:00
|
|
|
{
|
|
|
|
if(!(qApp->keyboardModifiers() & Qt::ControlModifier))
|
|
|
|
sc->selectionObjectsView.clear();
|
2022-12-27 02:04:09 +02:00
|
|
|
sc->selectionObjectsView.selectObject(firstSelectedObject);
|
2022-12-04 23:32:50 +02:00
|
|
|
}
|
2022-10-12 23:40:52 +02:00
|
|
|
sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
|
2022-09-18 01:23:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sc->selectionObjectsView.clear();
|
2022-10-12 23:40:52 +02:00
|
|
|
sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::SELECTION;
|
2022-12-04 12:51:01 +02:00
|
|
|
|
|
|
|
if(!rubberBand)
|
|
|
|
rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
|
|
|
|
rubberBand->setGeometry(QRect(mapFromScene(mouseStart), QSize()));
|
|
|
|
rubberBand->show();
|
2022-09-18 01:23:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sc->selectionObjectsView.shift = QPoint(0, 0);
|
|
|
|
sc->selectionObjectsView.draw();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//main->setStatusMessage(QString("x: %1 y: %2").arg(QString::number(event->pos().x()), QString::number(event->pos().y())));
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapView::mouseReleaseEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
this->update();
|
|
|
|
|
|
|
|
auto * sc = static_cast<MapScene*>(scene());
|
|
|
|
if(!sc || !controller->map())
|
|
|
|
return;
|
2022-12-04 12:51:01 +02:00
|
|
|
|
|
|
|
if(rubberBand)
|
|
|
|
rubberBand->hide();
|
2023-09-11 18:01:53 +02:00
|
|
|
|
|
|
|
if(sc->objectPickerView.isVisible())
|
|
|
|
{
|
|
|
|
if(event->button() == Qt::RightButton)
|
|
|
|
sc->objectPickerView.discard();
|
|
|
|
|
|
|
|
if(event->button() == Qt::LeftButton)
|
|
|
|
{
|
|
|
|
mouseStart = mapToScene(event->pos());
|
|
|
|
tileStart = tilePrev = int3(mouseStart.x() / 32, mouseStart.y() / 32, sc->level);
|
|
|
|
if(auto * pickedObject = sc->selectionObjectsView.selectObjectAt(tileStart.x, tileStart.y))
|
|
|
|
sc->objectPickerView.select(pickedObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2022-09-18 01:23:17 +02:00
|
|
|
|
|
|
|
switch(selectionTool)
|
|
|
|
{
|
2022-12-27 23:14:10 +02:00
|
|
|
case MapView::SelectionTool::Lasso: {
|
|
|
|
if(event->button() == Qt::RightButton)
|
|
|
|
break;
|
2023-10-24 23:58:26 +02:00
|
|
|
|
|
|
|
//connect with initial tile
|
|
|
|
for(auto i = tilePrev; i != tileStart;)
|
|
|
|
{
|
|
|
|
int length = std::numeric_limits<int>::max();
|
|
|
|
int3 dir;
|
|
|
|
for(auto & d : int3::getDirs())
|
|
|
|
{
|
|
|
|
if(tileStart.dist2dSQ(i + d) < length)
|
|
|
|
{
|
|
|
|
dir = d;
|
|
|
|
length = tileStart.dist2dSQ(i + d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i += dir;
|
|
|
|
sc->selectionTerrainView.select(i);
|
|
|
|
}
|
2022-12-27 23:14:10 +02:00
|
|
|
|
|
|
|
//key: y position of tile
|
|
|
|
//value.first: x position of left tile
|
|
|
|
//value.second: x postiion of right tile
|
2024-01-10 00:38:54 +02:00
|
|
|
std::map<int, std::pair<int, int>> selectionRangeMapX;
|
|
|
|
std::map<int, std::pair<int, int>> selectionRangeMapY;
|
2022-12-27 23:14:10 +02:00
|
|
|
for(auto & t : sc->selectionTerrainView.selection())
|
|
|
|
{
|
|
|
|
auto pairIter = selectionRangeMapX.find(t.y);
|
|
|
|
if(pairIter == selectionRangeMapX.end())
|
|
|
|
selectionRangeMapX[t.y] = std::make_pair(t.x, t.x);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pairIter->second.first = std::min(pairIter->second.first, t.x);
|
|
|
|
pairIter->second.second = std::max(pairIter->second.second, t.x);
|
|
|
|
}
|
|
|
|
|
|
|
|
pairIter = selectionRangeMapY.find(t.x);
|
|
|
|
if(pairIter == selectionRangeMapY.end())
|
|
|
|
selectionRangeMapY[t.x] = std::make_pair(t.y, t.y);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pairIter->second.first = std::min(pairIter->second.first, t.y);
|
|
|
|
pairIter->second.second = std::max(pairIter->second.second, t.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-10 00:38:54 +02:00
|
|
|
std::set<int3> selectionByX;
|
|
|
|
std::set<int3> selectionByY;
|
2022-12-27 23:14:10 +02:00
|
|
|
std::vector<int3> finalSelection;
|
|
|
|
for(auto & selectionRange : selectionRangeMapX)
|
|
|
|
{
|
|
|
|
for(int i = selectionRange.second.first; i < selectionRange.second.second; ++i)
|
|
|
|
selectionByX.insert(int3(i, selectionRange.first, sc->level));
|
|
|
|
}
|
|
|
|
for(auto & selectionRange : selectionRangeMapY)
|
|
|
|
{
|
|
|
|
for(int i = selectionRange.second.first; i < selectionRange.second.second; ++i)
|
|
|
|
selectionByY.insert(int3(selectionRange.first, i, sc->level));
|
|
|
|
}
|
|
|
|
std::set_intersection(selectionByX.begin(), selectionByX.end(), selectionByY.begin(), selectionByY.end(), std::back_inserter(finalSelection));
|
|
|
|
for(auto & lassoTile : finalSelection)
|
|
|
|
sc->selectionTerrainView.select(lassoTile);
|
|
|
|
|
|
|
|
sc->selectionTerrainView.draw();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-10-14 02:58:13 +02:00
|
|
|
case MapView::SelectionTool::Line:
|
|
|
|
temporaryTiles.clear();
|
|
|
|
break;
|
|
|
|
|
2022-09-18 01:23:17 +02:00
|
|
|
case MapView::SelectionTool::None:
|
|
|
|
if(event->button() == Qt::RightButton)
|
|
|
|
break;
|
|
|
|
//switch position
|
|
|
|
bool tab = false;
|
2022-10-12 23:40:52 +02:00
|
|
|
if(sc->selectionObjectsView.selectionMode == SelectionObjectsLayer::MOVEMENT)
|
2022-09-18 01:23:17 +02:00
|
|
|
{
|
2023-09-10 16:57:41 +02:00
|
|
|
tab = sc->selectionObjectsView.shift.isNull();
|
2022-12-04 04:45:39 +02:00
|
|
|
controller->commitObjectShift(sc->level);
|
2022-09-18 01:23:17 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-12 23:40:52 +02:00
|
|
|
sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
|
2022-09-18 01:23:17 +02:00
|
|
|
sc->selectionObjectsView.shift = QPoint(0, 0);
|
|
|
|
sc->selectionObjectsView.draw();
|
|
|
|
tab = true;
|
|
|
|
}
|
|
|
|
auto selection = sc->selectionObjectsView.getSelection();
|
|
|
|
if(selection.size() == 1)
|
|
|
|
{
|
|
|
|
emit openObjectProperties(*selection.begin(), tab);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 04:45:39 +02:00
|
|
|
void MapView::dragEnterEvent(QDragEnterEvent * event)
|
|
|
|
{
|
|
|
|
if(!controller || !controller->map())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto * sc = static_cast<MapScene*>(scene());
|
|
|
|
if(!sc)
|
|
|
|
return;
|
|
|
|
|
2022-12-21 02:28:56 +02:00
|
|
|
if(event->mimeData()->hasImage())
|
2022-12-04 04:45:39 +02:00
|
|
|
{
|
2022-12-21 02:28:56 +02:00
|
|
|
logGlobal->info("Drag'n'drop: dispatching object");
|
|
|
|
QVariant vdata = event->mimeData()->imageData();
|
2022-12-04 18:48:21 +02:00
|
|
|
auto data = vdata.toJsonObject();
|
2022-12-04 04:45:39 +02:00
|
|
|
if(!data.empty())
|
|
|
|
{
|
|
|
|
auto preview = data["preview"];
|
|
|
|
if(preview != QJsonValue::Undefined)
|
|
|
|
{
|
|
|
|
auto objId = data["id"].toInt();
|
|
|
|
auto objSubId = data["subid"].toInt();
|
|
|
|
auto templateId = data["template"].toInt();
|
|
|
|
auto factory = VLC->objtypeh->getHandlerFor(objId, objSubId);
|
|
|
|
auto templ = factory->getTemplates()[templateId];
|
|
|
|
controller->discardObject(sc->level);
|
2024-01-01 16:37:48 +02:00
|
|
|
controller->createObject(sc->level, factory->create(nullptr, templ));
|
2022-12-04 04:45:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapView::dropEvent(QDropEvent * event)
|
|
|
|
{
|
|
|
|
if(!controller || !controller->map())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto * sc = static_cast<MapScene*>(scene());
|
|
|
|
if(!sc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(sc->selectionObjectsView.newObject)
|
|
|
|
{
|
|
|
|
QString errorMsg;
|
|
|
|
if(controller->canPlaceObject(sc->level, sc->selectionObjectsView.newObject, errorMsg))
|
|
|
|
{
|
2023-09-10 16:57:41 +02:00
|
|
|
auto * obj = sc->selectionObjectsView.newObject;
|
2022-12-04 04:45:39 +02:00
|
|
|
controller->commitObjectCreate(sc->level);
|
2023-09-10 16:57:41 +02:00
|
|
|
emit openObjectProperties(obj, false);
|
2022-12-04 04:45:39 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
controller->discardObject(sc->level);
|
2023-07-27 19:01:20 +02:00
|
|
|
QMessageBox::information(this, tr("Can't place object"), errorMsg);
|
2022-12-04 04:45:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapView::dragMoveEvent(QDragMoveEvent * event)
|
|
|
|
{
|
|
|
|
auto * sc = static_cast<MapScene*>(scene());
|
|
|
|
if(!sc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto rect = event->answerRect();
|
|
|
|
auto pos = mapToScene(rect.bottomRight()); //TODO: do we need to check size?
|
|
|
|
int3 tile(pos.x() / 32 + 1, pos.y() / 32 + 1, sc->level);
|
|
|
|
|
|
|
|
if(sc->selectionObjectsView.newObject)
|
|
|
|
{
|
|
|
|
sc->selectionObjectsView.shift = QPoint(tile.x, tile.y);
|
|
|
|
sc->selectionObjectsView.selectObject(sc->selectionObjectsView.newObject);
|
|
|
|
sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
|
|
|
|
sc->selectionObjectsView.draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapView::dragLeaveEvent(QDragLeaveEvent * event)
|
|
|
|
{
|
|
|
|
if(!controller || !controller->map())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto * sc = static_cast<MapScene*>(scene());
|
|
|
|
if(!sc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
controller->discardObject(sc->level);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-18 01:23:17 +02:00
|
|
|
bool MapView::viewportEvent(QEvent *event)
|
|
|
|
{
|
|
|
|
if(auto * sc = static_cast<MapScene*>(scene()))
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapSceneBase::initialize(MapController & controller)
|
|
|
|
{
|
|
|
|
for(auto * layer : getAbstractLayers())
|
|
|
|
layer->initialize(controller);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapSceneBase::updateViews()
|
|
|
|
{
|
|
|
|
for(auto * layer : getAbstractLayers())
|
|
|
|
layer->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
MapScene::MapScene(int lvl):
|
|
|
|
MapSceneBase(lvl),
|
|
|
|
gridView(this),
|
|
|
|
passabilityView(this),
|
|
|
|
selectionTerrainView(this),
|
|
|
|
terrainView(this),
|
|
|
|
objectsView(this),
|
|
|
|
selectionObjectsView(this),
|
2023-09-11 18:01:53 +02:00
|
|
|
objectPickerView(this),
|
2022-09-18 01:23:17 +02:00
|
|
|
isTerrainSelected(false),
|
|
|
|
isObjectSelected(false)
|
|
|
|
{
|
|
|
|
connect(&selectionTerrainView, &SelectionTerrainLayer::selectionMade, this, &MapScene::terrainSelected);
|
|
|
|
connect(&selectionObjectsView, &SelectionObjectsLayer::selectionMade, this, &MapScene::objectSelected);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::list<AbstractLayer *> MapScene::getAbstractLayers()
|
|
|
|
{
|
|
|
|
//sequence is important because it defines rendering order
|
|
|
|
return {
|
|
|
|
&terrainView,
|
|
|
|
&objectsView,
|
|
|
|
&gridView,
|
|
|
|
&passabilityView,
|
2023-09-11 18:01:53 +02:00
|
|
|
&objectPickerView,
|
2022-09-18 01:23:17 +02:00
|
|
|
&selectionTerrainView,
|
|
|
|
&selectionObjectsView
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapScene::updateViews()
|
|
|
|
{
|
|
|
|
MapSceneBase::updateViews();
|
|
|
|
|
|
|
|
terrainView.show(true);
|
|
|
|
objectsView.show(true);
|
|
|
|
selectionTerrainView.show(true);
|
|
|
|
selectionObjectsView.show(true);
|
2023-09-11 18:01:53 +02:00
|
|
|
objectPickerView.show(true);
|
2022-09-18 01:23:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapScene::terrainSelected(bool anythingSelected)
|
|
|
|
{
|
|
|
|
isTerrainSelected = anythingSelected;
|
|
|
|
emit selected(isTerrainSelected || isObjectSelected);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapScene::objectSelected(bool anythingSelected)
|
|
|
|
{
|
|
|
|
isObjectSelected = anythingSelected;
|
|
|
|
emit selected(isTerrainSelected || isObjectSelected);
|
|
|
|
}
|
|
|
|
|
|
|
|
MinimapScene::MinimapScene(int lvl):
|
|
|
|
MapSceneBase(lvl),
|
|
|
|
minimapView(this),
|
|
|
|
viewport(this)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::list<AbstractLayer *> MinimapScene::getAbstractLayers()
|
|
|
|
{
|
|
|
|
//sequence is important because it defines rendering order
|
|
|
|
return {
|
|
|
|
&minimapView,
|
|
|
|
&viewport
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void MinimapScene::updateViews()
|
|
|
|
{
|
|
|
|
MapSceneBase::updateViews();
|
|
|
|
|
|
|
|
minimapView.show(true);
|
|
|
|
viewport.show(true);
|
|
|
|
}
|