mirror of
https://github.com/vcmi/vcmi.git
synced 2025-10-08 23:22:25 +02:00
Improve editor performance
This commit is contained in:
@@ -295,6 +295,7 @@ MainWindow::MainWindow(QWidget* parent) :
|
||||
|
||||
mapLevel = combo->currentIndex();
|
||||
ui->mapView->setScene(controller.scene(mapLevel));
|
||||
ui->mapView->setViewports();
|
||||
ui->minimapView->setScene(controller.miniScene(mapLevel));
|
||||
});
|
||||
layout->addWidget(combo, c == ui->menuView ? 1 : 0);
|
||||
@@ -1323,9 +1324,10 @@ void MainWindow::on_actionUpdate_appearance_triggered()
|
||||
controller.scene(mapLevel)->selectionObjectsView.deselectObject(obj);
|
||||
continue;
|
||||
}
|
||||
|
||||
std::vector<int3> selectedTiles;
|
||||
for(auto & offset : obj->appearance->getBlockedOffsets())
|
||||
controller.scene(mapLevel)->selectionTerrainView.select(obj->pos + offset);
|
||||
selectedTiles.push_back(obj->pos + offset);
|
||||
controller.scene(mapLevel)->selectionTerrainView.select(selectedTiles);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -1484,8 +1486,6 @@ void MainWindow::on_actionLock_triggered()
|
||||
}
|
||||
controller.scene(mapLevel)->selectionObjectsView.clear();
|
||||
}
|
||||
controller.scene(mapLevel)->objectsView.update();
|
||||
controller.scene(mapLevel)->selectionObjectsView.update();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1497,7 +1497,6 @@ void MainWindow::on_actionUnlock_triggered()
|
||||
controller.scene(mapLevel)->selectionObjectsView.unlockAll();
|
||||
controller.scene(mapLevel)->objectsView.unlockAll();
|
||||
}
|
||||
controller.scene(mapLevel)->objectsView.update();
|
||||
}
|
||||
|
||||
|
||||
|
@@ -229,7 +229,7 @@ void MapController::setMap(std::unique_ptr<CMap> cmap)
|
||||
_miniscenes[i].reset(new MinimapScene(i));
|
||||
}
|
||||
resetMapHandler();
|
||||
sceneForceUpdate();
|
||||
initializeMap();
|
||||
|
||||
connectScenes();
|
||||
|
||||
@@ -256,21 +256,24 @@ void MapController::initObstaclePainters(CMap * map)
|
||||
}
|
||||
}
|
||||
|
||||
void MapController::initializeMap()
|
||||
{
|
||||
for(int i = 0; i < _map->mapLevels; i++)
|
||||
{
|
||||
_scenes[i]->createMap();
|
||||
_miniscenes[i]->createMap();
|
||||
}
|
||||
}
|
||||
|
||||
void MapController::sceneForceUpdate()
|
||||
{
|
||||
for(int i = 0; i < _map->mapLevels; i++)
|
||||
{
|
||||
_scenes[i]->updateViews();
|
||||
_miniscenes[i]->updateViews();
|
||||
_scenes[i]->updateMap();
|
||||
_miniscenes[i]->updateMap();
|
||||
}
|
||||
}
|
||||
|
||||
void MapController::sceneForceUpdate(int level)
|
||||
{
|
||||
_scenes[level]->updateViews();
|
||||
_miniscenes[level]->updateViews();
|
||||
}
|
||||
|
||||
void MapController::resetMapHandler()
|
||||
{
|
||||
if(!_mapHandler)
|
||||
@@ -293,16 +296,13 @@ void MapController::commitTerrainChange(int level, const TerrainId & terrain)
|
||||
return;
|
||||
|
||||
_scenes[level]->selectionTerrainView.clear();
|
||||
_scenes[level]->selectionTerrainView.draw();
|
||||
|
||||
_map->getEditManager()->getTerrainSelection().setSelection(v);
|
||||
_map->getEditManager()->drawTerrain(terrain, terrainDecorationPercentageLevel, &CRandomGenerator::getDefault());
|
||||
|
||||
for(auto & t : v)
|
||||
_scenes[level]->terrainView.setDirty(t);
|
||||
_scenes[level]->terrainView.draw();
|
||||
_scenes[level]->terrainView.redrawTerrain(v);
|
||||
|
||||
_miniscenes[level]->updateViews();
|
||||
_miniscenes[level]->updateMap();
|
||||
main->mapChanged();
|
||||
}
|
||||
|
||||
@@ -314,7 +314,6 @@ void MapController::commitRoadOrRiverChange(int level, ui8 type, bool isRoad)
|
||||
return;
|
||||
|
||||
_scenes[level]->selectionTerrainView.clear();
|
||||
_scenes[level]->selectionTerrainView.draw();
|
||||
|
||||
_map->getEditManager()->getTerrainSelection().setSelection(v);
|
||||
if(isRoad)
|
||||
@@ -322,11 +321,9 @@ void MapController::commitRoadOrRiverChange(int level, ui8 type, bool isRoad)
|
||||
else
|
||||
_map->getEditManager()->drawRiver(RiverId(type), &CRandomGenerator::getDefault());
|
||||
|
||||
for(auto & t : v)
|
||||
_scenes[level]->terrainView.setDirty(t);
|
||||
_scenes[level]->terrainView.draw();
|
||||
_scenes[level]->terrainView.redrawTerrain(v);
|
||||
|
||||
_miniscenes[level]->updateViews();
|
||||
_miniscenes[level]->updateMap();
|
||||
main->mapChanged();
|
||||
}
|
||||
|
||||
@@ -351,15 +348,13 @@ void MapController::commitObjectErase(int level)
|
||||
{
|
||||
//invalidate tiles under objects
|
||||
_mapHandler->removeObject(obj);
|
||||
_scenes[level]->objectsView.setDirty(obj);
|
||||
}
|
||||
_scenes[level]->objectsView.redrawObjects(selectedObjects);
|
||||
|
||||
_scenes[level]->selectionObjectsView.clear();
|
||||
_scenes[level]->objectsView.draw();
|
||||
_scenes[level]->selectionObjectsView.draw();
|
||||
_scenes[level]->passabilityView.update();
|
||||
_scenes[level]->passabilityView.redraw();
|
||||
|
||||
_miniscenes[level]->updateViews();
|
||||
_miniscenes[level]->updateMap();
|
||||
main->mapChanged();
|
||||
}
|
||||
|
||||
@@ -407,11 +402,11 @@ void MapController::pasteFromClipboard(int level)
|
||||
if(!errors.isEmpty())
|
||||
QMessageBox::warning(main, QObject::tr("Can't place object"), errors.join('\n'));
|
||||
|
||||
_scenes[level]->objectsView.draw();
|
||||
_scenes[level]->passabilityView.update();
|
||||
_scenes[level]->selectionObjectsView.draw();
|
||||
_scenes[level]->objectsView.redraw();
|
||||
_scenes[level]->passabilityView.redraw();
|
||||
_scenes[level]->selectionObjectsView.redraw();
|
||||
|
||||
_miniscenes[level]->updateViews();
|
||||
_miniscenes[level]->updateMap();
|
||||
main->mapChanged();
|
||||
}
|
||||
|
||||
@@ -421,9 +416,8 @@ bool MapController::discardObject(int level) const
|
||||
if(_scenes[level]->selectionObjectsView.newObject)
|
||||
{
|
||||
_scenes[level]->selectionObjectsView.newObject.reset();
|
||||
_scenes[level]->selectionObjectsView.shift = QPoint(0, 0);
|
||||
_scenes[level]->selectionObjectsView.setShift(0, 0);
|
||||
_scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
|
||||
_scenes[level]->selectionObjectsView.draw();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -433,7 +427,7 @@ void MapController::createObject(int level, std::shared_ptr<CGObjectInstance> ob
|
||||
{
|
||||
_scenes[level]->selectionObjectsView.newObject = obj;
|
||||
_scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
|
||||
_scenes[level]->selectionObjectsView.draw();
|
||||
_scenes[level]->selectionObjectsView.redraw();
|
||||
}
|
||||
|
||||
void MapController::commitObstacleFill(int level)
|
||||
@@ -463,29 +457,26 @@ void MapController::commitObstacleFill(int level)
|
||||
for(auto o : sel.second->placeObstacles(CRandomGenerator::getDefault()))
|
||||
{
|
||||
_mapHandler->invalidate(o.get());
|
||||
_scenes[level]->objectsView.setDirty(o.get());
|
||||
_scenes[level]->objectsView.redrawObjects({o.get()});
|
||||
}
|
||||
}
|
||||
|
||||
_scenes[level]->selectionTerrainView.clear();
|
||||
_scenes[level]->selectionTerrainView.draw();
|
||||
_scenes[level]->objectsView.draw();
|
||||
_scenes[level]->objectsView.update();
|
||||
_scenes[level]->passabilityView.update();
|
||||
|
||||
_miniscenes[level]->updateViews();
|
||||
_miniscenes[level]->updateMap();
|
||||
main->mapChanged();
|
||||
}
|
||||
|
||||
void MapController::commitObjectChange(int level)
|
||||
{
|
||||
for( auto * o : _scenes[level]->selectionObjectsView.getSelection())
|
||||
_scenes[level]->objectsView.setDirty(o);
|
||||
_scenes[level]->objectsView.redrawObjects(_scenes[level]->selectionObjectsView.getSelection());
|
||||
|
||||
_scenes[level]->objectsView.draw();
|
||||
_scenes[level]->selectionObjectsView.draw();
|
||||
_scenes[level]->passabilityView.update();
|
||||
_scenes[level]->selectionObjectsView.redraw();
|
||||
_scenes[level]->passabilityView.redraw();
|
||||
|
||||
_miniscenes[level]->updateViews();
|
||||
_miniscenes[level]->updateMap();
|
||||
main->mapChanged();
|
||||
}
|
||||
|
||||
@@ -502,29 +493,30 @@ void MapController::commitObjectShift(int level)
|
||||
bool makeShift = !shift.isNull();
|
||||
if(makeShift)
|
||||
{
|
||||
for(auto * obj : _scenes[level]->selectionObjectsView.getSelection())
|
||||
std::set<CGObjectInstance*> movedObjects = _scenes[level]->selectionObjectsView.getSelection();
|
||||
for(auto * obj : movedObjects)
|
||||
{
|
||||
int3 pos = obj->pos;
|
||||
pos.z = level;
|
||||
pos.x += shift.x(); pos.y += shift.y();
|
||||
|
||||
_scenes[level]->objectsView.setDirty(obj); //set dirty before movement
|
||||
_map->getEditManager()->moveObject(obj, pos);
|
||||
_mapHandler->invalidate(obj);
|
||||
}
|
||||
_scenes[level]->objectsView.redrawObjects(movedObjects);
|
||||
}
|
||||
|
||||
_scenes[level]->selectionObjectsView.newObject = nullptr;
|
||||
_scenes[level]->selectionObjectsView.shift = QPoint(0, 0);
|
||||
_scenes[level]->selectionObjectsView.setShift(0, 0);
|
||||
_scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
|
||||
|
||||
if(makeShift)
|
||||
{
|
||||
_scenes[level]->objectsView.draw();
|
||||
_scenes[level]->selectionObjectsView.draw();
|
||||
_scenes[level]->passabilityView.update();
|
||||
_scenes[level]->objectsView.redraw();
|
||||
_scenes[level]->passabilityView.redraw();
|
||||
_scenes[level]->selectionObjectsView.redraw();
|
||||
|
||||
_miniscenes[level]->updateViews();
|
||||
_miniscenes[level]->updateMap();
|
||||
main->mapChanged();
|
||||
}
|
||||
}
|
||||
@@ -547,16 +539,14 @@ void MapController::commitObjectCreate(int level)
|
||||
|
||||
_map->getEditManager()->insertObject(newObj);
|
||||
_mapHandler->invalidate(newObj.get());
|
||||
_scenes[level]->objectsView.setDirty(newObj.get());
|
||||
_scenes[level]->objectsView.redrawObjects({newObj.get()});
|
||||
|
||||
_scenes[level]->selectionObjectsView.newObject = nullptr;
|
||||
_scenes[level]->selectionObjectsView.shift = QPoint(0, 0);
|
||||
_scenes[level]->selectionObjectsView.setShift(0, 0);
|
||||
_scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
|
||||
_scenes[level]->objectsView.draw();
|
||||
_scenes[level]->selectionObjectsView.draw();
|
||||
_scenes[level]->passabilityView.update();
|
||||
_scenes[level]->passabilityView.redraw();
|
||||
|
||||
_miniscenes[level]->updateViews();
|
||||
_miniscenes[level]->updateMap();
|
||||
main->mapChanged();
|
||||
}
|
||||
|
||||
|
@@ -48,8 +48,8 @@ public:
|
||||
|
||||
void resetMapHandler();
|
||||
|
||||
void initializeMap();
|
||||
void sceneForceUpdate();
|
||||
void sceneForceUpdate(int level);
|
||||
|
||||
void commitTerrainChange(int level, const TerrainId & terrain);
|
||||
void commitRoadOrRiverChange(int level, ui8 type, bool isRoad);
|
||||
|
@@ -109,18 +109,17 @@ void MapHandler::initTerrainGraphics()
|
||||
loadFlipped(roadAnimations, roadImages, roadFiles);
|
||||
}
|
||||
|
||||
void MapHandler::drawTerrainTile(QPainter & painter, int x, int y, int z)
|
||||
void MapHandler::drawTerrainTile(QPainter & painter, int x, int y, int z, QPointF offset)
|
||||
{
|
||||
const auto & tinfo = map->getTile(int3(x, y, z));
|
||||
|
||||
auto terrainName = tinfo.getTerrain()->getJsonKey();
|
||||
if(terrainImages.at(terrainName).size() <= tinfo.terView)
|
||||
return;
|
||||
|
||||
painter.drawImage(x * tileSize, y * tileSize, flippedImage(terrainImages.at(terrainName)[tinfo.terView], tinfo.extTileFlags));
|
||||
painter.drawImage(x * tileSize - offset.x(), y * tileSize - offset.y(), flippedImage(terrainImages.at(terrainName)[tinfo.terView], tinfo.extTileFlags));
|
||||
}
|
||||
|
||||
void MapHandler::drawRoad(QPainter & painter, int x, int y, int z)
|
||||
void MapHandler::drawRoad(QPainter & painter, int x, int y, int z, QPointF offset)
|
||||
{
|
||||
const auto & tinfo = map->getTile(int3(x, y, z));
|
||||
auto * tinfoUpper = map->isInTheMap(int3(x, y - 1, z)) ? &map->getTile(int3(x, y - 1, z)) : nullptr;
|
||||
@@ -132,7 +131,7 @@ void MapHandler::drawRoad(QPainter & painter, int x, int y, int z)
|
||||
{
|
||||
const QRect source{0, tileSize / 2, tileSize, tileSize / 2};
|
||||
const ui8 rotationFlags = tinfoUpper->extTileFlags >> 4;
|
||||
painter.drawImage(QPoint(x * tileSize, y * tileSize), flippedImage(roadImages.at(roadName)[tinfoUpper->roadDir], rotationFlags), source);
|
||||
painter.drawImage(QPoint(x * tileSize - offset.x(), y * tileSize - offset.y()), flippedImage(roadImages.at(roadName)[tinfoUpper->roadDir], rotationFlags), source);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,12 +142,12 @@ void MapHandler::drawRoad(QPainter & painter, int x, int y, int z)
|
||||
{
|
||||
const QRect source{0, 0, tileSize, tileSize / 2};
|
||||
const ui8 rotationFlags = tinfo.extTileFlags >> 4;
|
||||
painter.drawImage(QPoint(x * tileSize, y * tileSize + tileSize / 2), flippedImage(roadImages.at(roadName)[tinfo.roadDir], rotationFlags), source);
|
||||
painter.drawImage(QPoint(x * tileSize - offset.x(), y * tileSize + tileSize / 2 - offset.y()), flippedImage(roadImages.at(roadName)[tinfo.roadDir], rotationFlags), source);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MapHandler::drawRiver(QPainter & painter, int x, int y, int z)
|
||||
void MapHandler::drawRiver(QPainter & painter, int x, int y, int z, QPointF offset)
|
||||
{
|
||||
const auto & tinfo = map->getTile(int3(x, y, z));
|
||||
|
||||
@@ -162,7 +161,7 @@ void MapHandler::drawRiver(QPainter & painter, int x, int y, int z)
|
||||
return;
|
||||
|
||||
const ui8 rotationFlags = tinfo.extTileFlags >> 2;
|
||||
painter.drawImage(x * tileSize, y * tileSize, flippedImage(riverImages.at(riverName)[tinfo.riverDir], rotationFlags));
|
||||
painter.drawImage(x * tileSize - offset.x(), y * tileSize - offset.y(), flippedImage(riverImages.at(riverName)[tinfo.riverDir], rotationFlags));
|
||||
}
|
||||
|
||||
void setPlayerColor(QImage * sur, PlayerColor player)
|
||||
@@ -366,7 +365,7 @@ std::vector<ObjectRect> & MapHandler::getObjects(int x, int y, int z)
|
||||
return tileObjects[index(x, y, z)];
|
||||
}
|
||||
|
||||
void MapHandler::drawObjects(QPainter & painter, int x, int y, int z, const std::set<const CGObjectInstance *> & locked)
|
||||
void MapHandler::drawObjects(QPainter & painter, int x, int y, int z, QPointF offset, const std::set<const CGObjectInstance *> & locked)
|
||||
{
|
||||
painter.setRenderHint(QPainter::Antialiasing, false);
|
||||
painter.setRenderHint(QPainter::SmoothPixmapTransform, false);
|
||||
@@ -390,7 +389,7 @@ void MapHandler::drawObjects(QPainter & painter, int x, int y, int z, const std:
|
||||
{
|
||||
auto pos = obj->anchorPos();
|
||||
|
||||
painter.drawImage(QPoint(x * tileSize, y * tileSize), *objData.objBitmap, object.rect, Qt::AutoColor | Qt::NoOpaqueDetection);
|
||||
painter.drawImage(QPoint(x * tileSize - offset.x(), y * tileSize - offset.y()), *objData.objBitmap, object.rect, Qt::AutoColor | Qt::NoOpaqueDetection);
|
||||
|
||||
if(locked.count(obj))
|
||||
{
|
||||
@@ -402,13 +401,13 @@ void MapHandler::drawObjects(QPainter & painter, int x, int y, int z, const std:
|
||||
if(objData.flagBitmap)
|
||||
{
|
||||
if(x == pos.x && y == pos.y)
|
||||
painter.drawImage(QPoint((x - 2) * tileSize, (y - 1) * tileSize), *objData.flagBitmap);
|
||||
painter.drawImage(QPoint((x - 2) * tileSize - offset.x(), (y - 1) * tileSize - offset.y()), *objData.flagBitmap);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MapHandler::drawObjectAt(QPainter & painter, const CGObjectInstance * obj, int x, int y)
|
||||
void MapHandler::drawObjectAt(QPainter & painter, const CGObjectInstance * obj, int x, int y, QPointF offset)
|
||||
{
|
||||
if (!obj)
|
||||
{
|
||||
@@ -424,10 +423,10 @@ void MapHandler::drawObjectAt(QPainter & painter, const CGObjectInstance * obj,
|
||||
|
||||
if (objData.objBitmap)
|
||||
{
|
||||
painter.drawImage(QPoint((x + 1) * tileSize - objData.objBitmap->width(), (y + 1) * tileSize - objData.objBitmap->height()), *objData.objBitmap);
|
||||
painter.drawImage(QPoint((x + 1) * tileSize - objData.objBitmap->width() - offset.x(), (y + 1) * tileSize - objData.objBitmap->height() - offset.y()), *objData.objBitmap);
|
||||
|
||||
if (objData.flagBitmap)
|
||||
painter.drawImage(QPoint((x + 1) * tileSize - objData.objBitmap->width(), (y + 1) * tileSize - objData.objBitmap->height()), *objData.flagBitmap);
|
||||
painter.drawImage(QPoint((x + 1) * tileSize - objData.objBitmap->width() - offset.x(), (y + 1) * tileSize - objData.objBitmap->height() - offset.y()), *objData.flagBitmap);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -92,11 +92,11 @@ public:
|
||||
|
||||
void reset(const CMap * Map);
|
||||
|
||||
void drawTerrainTile(QPainter & painter, int x, int y, int z);
|
||||
void drawTerrainTile(QPainter & painter, int x, int y, int z, QPointF offset);
|
||||
/// draws a river segment on current tile
|
||||
void drawRiver(QPainter & painter, int x, int y, int z);
|
||||
void drawRiver(QPainter & painter, int x, int y, int z, QPointF offset);
|
||||
/// draws a road segment on current tile
|
||||
void drawRoad(QPainter & painter, int x, int y, int z);
|
||||
void drawRoad(QPainter & painter, int x, int y, int z, QPointF offset);
|
||||
|
||||
std::set<int3> invalidate(const CGObjectInstance *); //invalidates object rects
|
||||
void invalidateObjects(); //invalidates all objects on the map
|
||||
@@ -111,8 +111,8 @@ public:
|
||||
std::set<int3> addObject(const CGObjectInstance * object);
|
||||
|
||||
/// draws all objects on current tile (higher-level logic, unlike other draw*** methods)
|
||||
void drawObjects(QPainter & painter, int x, int y, int z, const std::set<const CGObjectInstance *> & locked);
|
||||
void drawObjectAt(QPainter & painter, const CGObjectInstance * object, int x, int y);
|
||||
void drawObjects(QPainter & painter, int x, int y, int z, QPointF offset, const std::set<const CGObjectInstance *> & locked);
|
||||
void drawObjectAt(QPainter & painter, const CGObjectInstance * object, int x, int y, QPointF offset);
|
||||
|
||||
void drawMinimapTile(QPainter & painter, int x, int y, int z);
|
||||
|
||||
|
@@ -59,6 +59,8 @@ MapView::MapView(QWidget * parent):
|
||||
QGraphicsView(parent),
|
||||
selectionTool(MapView::SelectionTool::None)
|
||||
{
|
||||
connect(horizontalScrollBar(), &QScrollBar::valueChanged, this, &MapView::setViewports);
|
||||
connect(verticalScrollBar(), &QScrollBar::valueChanged, this, &MapView::setViewports);
|
||||
}
|
||||
|
||||
void MapView::cameraChanged(const QPointF & pos)
|
||||
@@ -71,6 +73,11 @@ void MapView::setController(MapController * ctrl)
|
||||
controller = ctrl;
|
||||
}
|
||||
|
||||
void MapView::resizeEvent(QResizeEvent * event)
|
||||
{
|
||||
setViewports();
|
||||
}
|
||||
|
||||
void MapView::mouseMoveEvent(QMouseEvent *mouseEvent)
|
||||
{
|
||||
this->update();
|
||||
@@ -96,24 +103,24 @@ void MapView::mouseMoveEvent(QMouseEvent *mouseEvent)
|
||||
{
|
||||
case MapView::SelectionTool::Brush:
|
||||
if(mouseEvent->buttons() & Qt::RightButton)
|
||||
sc->selectionTerrainView.erase(tile);
|
||||
sc->selectionTerrainView.erase({tile});
|
||||
else if(mouseEvent->buttons() == Qt::LeftButton)
|
||||
sc->selectionTerrainView.select(tile);
|
||||
sc->selectionTerrainView.draw();
|
||||
sc->selectionTerrainView.select({tile});
|
||||
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} };
|
||||
std::vector<int3> tiles;
|
||||
for(auto & e : extra)
|
||||
{
|
||||
tiles.push_back(tile + e);
|
||||
}
|
||||
if(mouseEvent->buttons() & Qt::RightButton)
|
||||
sc->selectionTerrainView.erase(tile + e);
|
||||
sc->selectionTerrainView.erase(tiles);
|
||||
else if(mouseEvent->buttons() == Qt::LeftButton)
|
||||
sc->selectionTerrainView.select(tile + e);
|
||||
sc->selectionTerrainView.select(tiles);
|
||||
}
|
||||
}
|
||||
sc->selectionTerrainView.draw();
|
||||
break;
|
||||
|
||||
case MapView::SelectionTool::Brush4:
|
||||
@@ -124,33 +131,37 @@ void MapView::mouseMoveEvent(QMouseEvent *mouseEvent)
|
||||
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}
|
||||
};
|
||||
std::vector<int3> tiles;
|
||||
for(auto & e : extra)
|
||||
{
|
||||
tiles.push_back(tile + e);
|
||||
}
|
||||
if(mouseEvent->buttons() & Qt::RightButton)
|
||||
sc->selectionTerrainView.erase(tile + e);
|
||||
sc->selectionTerrainView.erase(tiles);
|
||||
else if(mouseEvent->buttons() == Qt::LeftButton)
|
||||
sc->selectionTerrainView.select(tile + e);
|
||||
sc->selectionTerrainView.select(tiles);
|
||||
}
|
||||
}
|
||||
sc->selectionTerrainView.draw();
|
||||
break;
|
||||
|
||||
case MapView::SelectionTool::Area:
|
||||
{
|
||||
if(mouseEvent->buttons() & Qt::RightButton || !(mouseEvent->buttons() & Qt::LeftButton))
|
||||
break;
|
||||
|
||||
sc->selectionTerrainView.clear();
|
||||
std::vector<int3> selectedTiles;
|
||||
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));
|
||||
selectedTiles.emplace_back(i, j, sc->level);
|
||||
}
|
||||
}
|
||||
sc->selectionTerrainView.draw();
|
||||
sc->selectionTerrainView.select(selectedTiles);
|
||||
break;
|
||||
|
||||
}
|
||||
case MapView::SelectionTool::Line:
|
||||
{
|
||||
{
|
||||
assert(tile.z == tileStart.z);
|
||||
const auto diff = tile - tileStart;
|
||||
@@ -174,39 +185,45 @@ void MapView::mouseMoveEvent(QMouseEvent *mouseEvent)
|
||||
|
||||
if(mouseEvent->buttons() == Qt::LeftButton)
|
||||
{
|
||||
for(auto & ts : temporaryTiles)
|
||||
sc->selectionTerrainView.erase(ts);
|
||||
std::vector<int3>erasedTiles(temporaryTiles.begin(), temporaryTiles.end());
|
||||
sc->selectionTerrainView.erase(erasedTiles);
|
||||
|
||||
std::vector<int3>selectedTiles;
|
||||
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);
|
||||
selectedTiles.push_back(ts);
|
||||
}
|
||||
sc->selectionTerrainView.select(selectedTiles);
|
||||
}
|
||||
if(mouseEvent->buttons() == Qt::RightButton)
|
||||
{
|
||||
for(auto & ts : temporaryTiles)
|
||||
sc->selectionTerrainView.select(ts);
|
||||
std::vector<int3>selectedTiles(temporaryTiles.begin(), temporaryTiles.end());
|
||||
sc->selectionTerrainView.select(selectedTiles);
|
||||
|
||||
std::vector<int3>erasedTiles;
|
||||
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);
|
||||
erasedTiles.push_back(ts);
|
||||
}
|
||||
sc->selectionTerrainView.erase(selectedTiles);
|
||||
}
|
||||
sc->selectionTerrainView.draw();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
case MapView::SelectionTool::Lasso:
|
||||
{
|
||||
if(mouseEvent->buttons() == Qt::LeftButton)
|
||||
{
|
||||
std::vector<int3>tiles;
|
||||
for(auto i = tilePrev; i != tile;)
|
||||
{
|
||||
int length = std::numeric_limits<int>::max();
|
||||
@@ -220,18 +237,19 @@ void MapView::mouseMoveEvent(QMouseEvent *mouseEvent)
|
||||
}
|
||||
}
|
||||
i += dir;
|
||||
sc->selectionTerrainView.select(i);
|
||||
tiles.push_back(i);
|
||||
}
|
||||
sc->selectionTerrainView.draw();
|
||||
sc->selectionTerrainView.select(tiles);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case MapView::SelectionTool::None:
|
||||
if(mouseEvent->buttons() & Qt::RightButton)
|
||||
break;
|
||||
|
||||
auto sh = tile - tileStart;
|
||||
sc->selectionObjectsView.shift = QPoint(sh.x, sh.y);
|
||||
sc->selectionObjectsView.setShift(sh.x, sh.y);
|
||||
|
||||
if(sh.x || sh.y)
|
||||
{
|
||||
@@ -244,8 +262,6 @@ void MapView::mouseMoveEvent(QMouseEvent *mouseEvent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sc->selectionObjectsView.draw();
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -276,34 +292,31 @@ void MapView::mousePressEvent(QMouseEvent *event)
|
||||
case MapView::SelectionTool::Brush:
|
||||
case MapView::SelectionTool::Line:
|
||||
sc->selectionObjectsView.clear();
|
||||
sc->selectionObjectsView.draw();
|
||||
|
||||
if(event->button() == Qt::RightButton)
|
||||
sc->selectionTerrainView.erase(tileStart);
|
||||
sc->selectionTerrainView.erase({tileStart});
|
||||
else if(event->button() == Qt::LeftButton)
|
||||
sc->selectionTerrainView.select(tileStart);
|
||||
sc->selectionTerrainView.draw();
|
||||
sc->selectionTerrainView.select({tileStart});
|
||||
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} };
|
||||
std::vector<int3> tiles;
|
||||
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);
|
||||
tiles.push_back(tileStart + e);
|
||||
}
|
||||
if(event->buttons() & Qt::RightButton)
|
||||
sc->selectionTerrainView.erase(tiles);
|
||||
else if(event->buttons() == Qt::LeftButton)
|
||||
sc->selectionTerrainView.select(tiles);
|
||||
}
|
||||
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},
|
||||
@@ -311,15 +324,16 @@ void MapView::mousePressEvent(QMouseEvent *event)
|
||||
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}
|
||||
};
|
||||
std::vector<int3> tiles;
|
||||
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);
|
||||
tiles.push_back(tileStart + e);
|
||||
}
|
||||
if(event->buttons() & Qt::RightButton)
|
||||
sc->selectionTerrainView.erase(tiles);
|
||||
else if(event->buttons() == Qt::LeftButton)
|
||||
sc->selectionTerrainView.select(tiles);
|
||||
}
|
||||
sc->selectionTerrainView.draw();
|
||||
break;
|
||||
|
||||
case MapView::SelectionTool::Area:
|
||||
@@ -328,9 +342,7 @@ void MapView::mousePressEvent(QMouseEvent *event)
|
||||
break;
|
||||
|
||||
sc->selectionTerrainView.clear();
|
||||
sc->selectionTerrainView.draw();
|
||||
sc->selectionObjectsView.clear();
|
||||
sc->selectionObjectsView.draw();
|
||||
break;
|
||||
|
||||
case MapView::SelectionTool::Fill:
|
||||
@@ -339,6 +351,7 @@ void MapView::mousePressEvent(QMouseEvent *event)
|
||||
break;
|
||||
|
||||
std::vector<int3> queue;
|
||||
std::set<int3> tilesToFill;
|
||||
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} };
|
||||
@@ -347,13 +360,12 @@ void MapView::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
auto tile = queue.back();
|
||||
queue.pop_back();
|
||||
if(event->button() == Qt::LeftButton)
|
||||
sc->selectionTerrainView.select(tile);
|
||||
else
|
||||
sc->selectionTerrainView.erase(tile);
|
||||
tilesToFill.insert(tile);
|
||||
for(auto & d : dirs)
|
||||
{
|
||||
auto tilen = tile + d;
|
||||
if (tilesToFill.count(tilen))
|
||||
continue;
|
||||
if(!controller->map()->isInTheMap(tilen))
|
||||
continue;
|
||||
if(event->button() == Qt::LeftButton)
|
||||
@@ -374,17 +386,19 @@ void MapView::mousePressEvent(QMouseEvent *event)
|
||||
queue.push_back(tilen);
|
||||
}
|
||||
}
|
||||
std::vector<int3> result(tilesToFill.begin(), tilesToFill.end());
|
||||
|
||||
if(event->button() == Qt::LeftButton)
|
||||
sc->selectionTerrainView.select(result);
|
||||
else
|
||||
sc->selectionTerrainView.erase(result);
|
||||
|
||||
sc->selectionTerrainView.draw();
|
||||
sc->selectionObjectsView.clear();
|
||||
sc->selectionObjectsView.draw();
|
||||
break;
|
||||
}
|
||||
|
||||
case MapView::SelectionTool::None:
|
||||
sc->selectionTerrainView.clear();
|
||||
sc->selectionTerrainView.draw();
|
||||
|
||||
if(sc->selectionObjectsView.newObject && sc->selectionObjectsView.isSelected(sc->selectionObjectsView.newObject.get()))
|
||||
{
|
||||
@@ -433,8 +447,7 @@ void MapView::mousePressEvent(QMouseEvent *event)
|
||||
rubberBand->show();
|
||||
}
|
||||
}
|
||||
sc->selectionObjectsView.shift = QPoint(0, 0);
|
||||
sc->selectionObjectsView.draw();
|
||||
sc->selectionObjectsView.setShift(0, 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -474,7 +487,7 @@ void MapView::mouseReleaseEvent(QMouseEvent *event)
|
||||
case MapView::SelectionTool::Lasso: {
|
||||
if(event->button() == Qt::RightButton)
|
||||
break;
|
||||
|
||||
std::vector<int3>initialTiles;
|
||||
//connect with initial tile
|
||||
for(auto i = tilePrev; i != tileStart;)
|
||||
{
|
||||
@@ -489,8 +502,9 @@ void MapView::mouseReleaseEvent(QMouseEvent *event)
|
||||
}
|
||||
}
|
||||
i += dir;
|
||||
sc->selectionTerrainView.select(i);
|
||||
initialTiles.push_back(i);
|
||||
}
|
||||
sc->selectionTerrainView.select(initialTiles);
|
||||
|
||||
//key: y position of tile
|
||||
//value.first: x position of left tile
|
||||
@@ -532,10 +546,8 @@ void MapView::mouseReleaseEvent(QMouseEvent *event)
|
||||
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.select(finalSelection);
|
||||
|
||||
sc->selectionTerrainView.draw();
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -556,8 +568,7 @@ void MapView::mouseReleaseEvent(QMouseEvent *event)
|
||||
else
|
||||
{
|
||||
sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
|
||||
sc->selectionObjectsView.shift = QPoint(0, 0);
|
||||
sc->selectionObjectsView.draw();
|
||||
sc->selectionObjectsView.setShift(0, 0);
|
||||
tab = true;
|
||||
}
|
||||
auto selection = sc->selectionObjectsView.getSelection();
|
||||
@@ -642,10 +653,9 @@ void MapView::dragMoveEvent(QDragMoveEvent * event)
|
||||
|
||||
if(sc->selectionObjectsView.newObject)
|
||||
{
|
||||
sc->selectionObjectsView.shift = QPoint(tile.x, tile.y);
|
||||
sc->selectionObjectsView.selectObject(sc->selectionObjectsView.newObject.get());
|
||||
sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
|
||||
sc->selectionObjectsView.draw();
|
||||
sc->selectionObjectsView.selectObject(sc->selectionObjectsView.newObject.get());
|
||||
sc->selectionObjectsView.setShift(tile.x, tile.y);
|
||||
}
|
||||
|
||||
event->acceptProposedAction();
|
||||
@@ -663,15 +673,17 @@ void MapView::dragLeaveEvent(QDragLeaveEvent * event)
|
||||
controller->discardObject(sc->level);
|
||||
}
|
||||
|
||||
|
||||
bool MapView::viewportEvent(QEvent *event)
|
||||
void MapView::setViewports()
|
||||
{
|
||||
if(auto * sc = static_cast<MapScene*>(scene()))
|
||||
if(auto * sc = dynamic_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);
|
||||
for (auto * layer : sc->getDynamicLayers())
|
||||
{
|
||||
layer->setViewport(rect);
|
||||
}
|
||||
}
|
||||
return QGraphicsView::viewportEvent(event);
|
||||
}
|
||||
|
||||
MapSceneBase::MapSceneBase(int lvl):
|
||||
@@ -682,18 +694,31 @@ MapSceneBase::MapSceneBase(int lvl):
|
||||
|
||||
void MapSceneBase::initialize(MapController & controller)
|
||||
{
|
||||
for(auto * layer : getAbstractLayers())
|
||||
for(auto * layer : getStaticLayers())
|
||||
layer->initialize(controller);
|
||||
for(auto * layer : getDynamicLayers())
|
||||
layer->initialize(controller);
|
||||
}
|
||||
|
||||
void MapSceneBase::updateViews()
|
||||
void MapSceneBase::createMap()
|
||||
{
|
||||
for(auto * layer : getAbstractLayers())
|
||||
for(auto * layer : getStaticLayers())
|
||||
layer->update();
|
||||
for(auto * layer : getDynamicLayers())
|
||||
layer->createLayer();
|
||||
}
|
||||
|
||||
void MapSceneBase::updateMap()
|
||||
{
|
||||
for(auto * layer : getStaticLayers())
|
||||
layer->update();
|
||||
for(auto * layer : getDynamicLayers())
|
||||
layer->redraw();
|
||||
}
|
||||
|
||||
MapScene::MapScene(int lvl):
|
||||
MapSceneBase(lvl),
|
||||
emptyLayer(this),
|
||||
gridView(this),
|
||||
passabilityView(this),
|
||||
selectionTerrainView(this),
|
||||
@@ -708,7 +733,14 @@ MapScene::MapScene(int lvl):
|
||||
connect(&selectionObjectsView, &SelectionObjectsLayer::selectionMade, this, &MapScene::objectSelected);
|
||||
}
|
||||
|
||||
std::list<AbstractLayer *> MapScene::getAbstractLayers()
|
||||
std::list<AbstractFixedLayer *> MapScene::getStaticLayers()
|
||||
{
|
||||
return {
|
||||
&emptyLayer
|
||||
};
|
||||
}
|
||||
|
||||
std::list<AbstractViewportLayer *> MapScene::getDynamicLayers()
|
||||
{
|
||||
//sequence is important because it defines rendering order
|
||||
return {
|
||||
@@ -722,9 +754,9 @@ std::list<AbstractLayer *> MapScene::getAbstractLayers()
|
||||
};
|
||||
}
|
||||
|
||||
void MapScene::updateViews()
|
||||
void MapScene::createMap()
|
||||
{
|
||||
MapSceneBase::updateViews();
|
||||
MapSceneBase::createMap();
|
||||
|
||||
terrainView.show(true);
|
||||
objectsView.show(true);
|
||||
@@ -752,7 +784,7 @@ MinimapScene::MinimapScene(int lvl):
|
||||
{
|
||||
}
|
||||
|
||||
std::list<AbstractLayer *> MinimapScene::getAbstractLayers()
|
||||
std::list<AbstractFixedLayer *> MinimapScene::getStaticLayers()
|
||||
{
|
||||
//sequence is important because it defines rendering order
|
||||
return {
|
||||
@@ -761,9 +793,15 @@ std::list<AbstractLayer *> MinimapScene::getAbstractLayers()
|
||||
};
|
||||
}
|
||||
|
||||
void MinimapScene::updateViews()
|
||||
std::list<AbstractViewportLayer *> MinimapScene::getDynamicLayers()
|
||||
{
|
||||
MapSceneBase::updateViews();
|
||||
//Nothing here
|
||||
return {};
|
||||
}
|
||||
|
||||
void MinimapScene::createMap()
|
||||
{
|
||||
MapSceneBase::createMap();
|
||||
|
||||
minimapView.show(true);
|
||||
viewport.show(true);
|
||||
|
@@ -31,12 +31,11 @@ public:
|
||||
MapSceneBase(int lvl);
|
||||
|
||||
const int level;
|
||||
|
||||
virtual void updateViews();
|
||||
virtual void createMap();
|
||||
virtual void updateMap();
|
||||
virtual void initialize(MapController &);
|
||||
|
||||
protected:
|
||||
virtual std::list<AbstractLayer *> getAbstractLayers() = 0;
|
||||
virtual std::list<AbstractFixedLayer *> getStaticLayers() = 0;
|
||||
virtual std::list<AbstractViewportLayer *> getDynamicLayers() = 0;
|
||||
};
|
||||
|
||||
class MinimapScene : public MapSceneBase
|
||||
@@ -44,13 +43,12 @@ class MinimapScene : public MapSceneBase
|
||||
public:
|
||||
MinimapScene(int lvl);
|
||||
|
||||
void updateViews() override;
|
||||
void createMap() override;
|
||||
|
||||
MinimapLayer minimapView;
|
||||
MinimapViewLayer viewport;
|
||||
|
||||
protected:
|
||||
std::list<AbstractLayer *> getAbstractLayers() override;
|
||||
std::list<AbstractFixedLayer *> getStaticLayers() override;
|
||||
std::list<AbstractViewportLayer *> getDynamicLayers() override;
|
||||
};
|
||||
|
||||
class MapScene : public MapSceneBase
|
||||
@@ -59,8 +57,11 @@ class MapScene : public MapSceneBase
|
||||
public:
|
||||
MapScene(int lvl);
|
||||
|
||||
void updateViews() override;
|
||||
void createMap() override;
|
||||
std::list<AbstractFixedLayer *> getStaticLayers() override;
|
||||
std::list<AbstractViewportLayer *> getDynamicLayers() override;
|
||||
|
||||
EmptyLayer emptyLayer;
|
||||
GridLayer gridView;
|
||||
PassabilityLayer passabilityView;
|
||||
SelectionTerrainLayer selectionTerrainView;
|
||||
@@ -77,8 +78,6 @@ public slots:
|
||||
void objectSelected(bool anything);
|
||||
|
||||
protected:
|
||||
std::list<AbstractLayer *> getAbstractLayers() override;
|
||||
|
||||
bool isTerrainSelected;
|
||||
bool isObjectSelected;
|
||||
|
||||
@@ -100,6 +99,7 @@ public:
|
||||
SelectionTool selectionTool;
|
||||
|
||||
public slots:
|
||||
void resizeEvent (QResizeEvent * event) override;
|
||||
void mouseMoveEvent(QMouseEvent * mouseEvent) override;
|
||||
void mousePressEvent(QMouseEvent *event) override;
|
||||
void mouseReleaseEvent(QMouseEvent *event) override;
|
||||
@@ -107,8 +107,8 @@ public slots:
|
||||
void dragMoveEvent(QDragMoveEvent *event) override;
|
||||
void dragLeaveEvent(QDragLeaveEvent *event) override;
|
||||
void dropEvent(QDropEvent * event) override;
|
||||
|
||||
void cameraChanged(const QPointF & pos);
|
||||
void setViewports();
|
||||
|
||||
signals:
|
||||
void openObjectProperties(CGObjectInstance *, bool switchTab);
|
||||
@@ -116,7 +116,6 @@ signals:
|
||||
//void viewportChanged(const QRectF & rect);
|
||||
|
||||
protected:
|
||||
bool viewportEvent(QEvent *event) override;
|
||||
|
||||
private:
|
||||
MapController * controller = nullptr;
|
||||
|
@@ -29,15 +29,32 @@ void AbstractLayer::initialize(MapController & controller)
|
||||
|
||||
void AbstractLayer::show(bool show)
|
||||
{
|
||||
if(isShown == show)
|
||||
return;
|
||||
|
||||
isShown = show;
|
||||
|
||||
redraw();
|
||||
}
|
||||
|
||||
void AbstractLayer::redraw()
|
||||
|
||||
int AbstractLayer::mapWidthPx() const
|
||||
{
|
||||
return map ? map->width * tileSize : 0;
|
||||
}
|
||||
|
||||
int AbstractLayer::mapHeightPx() const
|
||||
{
|
||||
return map ? map->height * tileSize : 0;
|
||||
}
|
||||
|
||||
int AbstractLayer::toInt(double value) const
|
||||
{
|
||||
return static_cast<int>(std::round(value)); // is rounded explicitly in order to avoid rounding down unprecise double values
|
||||
}
|
||||
|
||||
AbstractFixedLayer::AbstractFixedLayer(MapSceneBase * s): AbstractLayer(s)
|
||||
{
|
||||
}
|
||||
|
||||
void AbstractFixedLayer::redraw()
|
||||
{
|
||||
if(item)
|
||||
{
|
||||
@@ -55,65 +72,284 @@ void AbstractLayer::redraw()
|
||||
}
|
||||
}
|
||||
|
||||
GridLayer::GridLayer(MapSceneBase * s): AbstractLayer(s)
|
||||
AbstractViewportLayer::AbstractViewportLayer(MapSceneBase * s): AbstractLayer(s)
|
||||
{
|
||||
}
|
||||
|
||||
void GridLayer::update()
|
||||
void AbstractViewportLayer::createLayer()
|
||||
{
|
||||
QList<QGraphicsItem *>emptyList;
|
||||
items.reset(scene->createItemGroup(emptyList));
|
||||
}
|
||||
|
||||
void AbstractViewportLayer::setViewport(const QRectF & viewPort)
|
||||
{
|
||||
if(!map)
|
||||
return;
|
||||
if (items->boundingRect().contains(viewPort))
|
||||
return;
|
||||
|
||||
std::vector<QGraphicsItem *> outOfScreenSectors;
|
||||
for (QGraphicsItem * sector : getAllSectors())
|
||||
{
|
||||
if (!viewPort.intersects(sector->sceneBoundingRect()))
|
||||
outOfScreenSectors.push_back(sector);
|
||||
}
|
||||
for (QGraphicsItem * sector : outOfScreenSectors)
|
||||
{
|
||||
removeSector(sector);
|
||||
}
|
||||
|
||||
std::vector<QRectF> newAreas;
|
||||
|
||||
int left = toInt(viewPort.left());
|
||||
int right = toInt(viewPort.right());
|
||||
int top = toInt(viewPort.top());
|
||||
int bottom = toInt(viewPort.bottom());
|
||||
int startX = left - (left % sectorSize);
|
||||
int limitX = std::min(right + (sectorSize - right % sectorSize), mapWidthPx());
|
||||
int startY = top - (top % sectorSize);
|
||||
int limitY = std::min(bottom + (sectorSize - bottom % sectorSize), mapHeightPx());
|
||||
|
||||
for (int x = startX; x < limitX; x += sectorSize)
|
||||
{
|
||||
for (int y = startY; y < limitY; y += sectorSize)
|
||||
{
|
||||
int width = x + sectorSize < limitX ? sectorSize : limitX - x;
|
||||
int height = y + sectorSize < limitY ? sectorSize : limitY - y;
|
||||
QRectF area(x, y, width, height);
|
||||
if (!items->boundingRect().intersects(area))
|
||||
newAreas.emplace_back(area);
|
||||
}
|
||||
}
|
||||
|
||||
for(QRectF newSection : newAreas)
|
||||
{
|
||||
QGraphicsItem * sector = draw(newSection);
|
||||
if (sector)
|
||||
addSector(sector);
|
||||
}
|
||||
}
|
||||
|
||||
void AbstractViewportLayer::update()
|
||||
{
|
||||
redraw();
|
||||
}
|
||||
|
||||
void AbstractViewportLayer::redraw()
|
||||
{
|
||||
std::set<QGraphicsItem *> allSectors;
|
||||
for (auto * sector : getAllSectors())
|
||||
allSectors.insert(sector);
|
||||
redrawSectors(allSectors);
|
||||
}
|
||||
|
||||
void AbstractViewportLayer::redraw(const std::vector<int3> & tiles)
|
||||
{
|
||||
std::set<QGraphicsItem *> sectorsToRedraw = getContainingSectors(tiles);
|
||||
redrawSectors(sectorsToRedraw);
|
||||
}
|
||||
|
||||
void AbstractViewportLayer::redrawWithSurroundingTiles(const std::vector<int3> & tiles)
|
||||
{
|
||||
int maxX = 0;
|
||||
int maxY = 0;
|
||||
int minX = INT_MAX;
|
||||
int minY = INT_MAX;
|
||||
for (const int3 tile : tiles)
|
||||
{
|
||||
maxX = std::max(tile.x, maxX);
|
||||
maxY = std::max(tile.y, maxY);
|
||||
minX = std::min(tile.x, minX);
|
||||
minY = std::min(tile.y, minY);
|
||||
}
|
||||
|
||||
QRectF bounds((minX - 2) * tileSize, (minY - 2) * tileSize, (maxX - minX + 4) * tileSize, (maxY - minY + 4) * tileSize); //tiles start with 1, QRectF from 0
|
||||
redraw({bounds});
|
||||
}
|
||||
|
||||
void AbstractViewportLayer::redraw(const std::set<CGObjectInstance *> & objects)
|
||||
{
|
||||
std::vector<QRectF> areas(objects.size());
|
||||
for (const CGObjectInstance * object : objects)
|
||||
{
|
||||
areas.push_back(getObjectArea(object));
|
||||
}
|
||||
redraw(areas);
|
||||
}
|
||||
|
||||
void AbstractViewportLayer::redraw(const std::vector<QRectF> & areas)
|
||||
{
|
||||
std::set<QGraphicsItem *> intersectingSectors;
|
||||
for (QGraphicsItem * existingSector : getAllSectors())
|
||||
{
|
||||
for (auto area : areas)
|
||||
{
|
||||
if (existingSector->sceneBoundingRect().intersects(area))
|
||||
{
|
||||
intersectingSectors.insert(existingSector);
|
||||
}
|
||||
}
|
||||
}
|
||||
redrawSectors(intersectingSectors);
|
||||
}
|
||||
|
||||
QRectF AbstractViewportLayer::getObjectArea(const CGObjectInstance * object)
|
||||
{
|
||||
auto pos = object->pos;
|
||||
int x = ((pos.x + 1) * tileSize) - (object->getWidth() * tileSize); //Qt set 0,0 point on the top right corner, CGObjectInstance on the bottom left
|
||||
int y = ((pos.y + 1) * tileSize) - (object->getHeight() * tileSize);
|
||||
QRectF objectArea(x, y, object->getWidth() * tileSize, object->getHeight() * tileSize);
|
||||
return objectArea;
|
||||
}
|
||||
|
||||
void AbstractViewportLayer::addSector(QGraphicsItem * sector)
|
||||
{
|
||||
items->addToGroup(sector);
|
||||
}
|
||||
|
||||
void AbstractViewportLayer::removeSector(QGraphicsItem * sector)
|
||||
{
|
||||
items->removeFromGroup(sector);
|
||||
delete sector;
|
||||
}
|
||||
|
||||
void AbstractViewportLayer::redrawSectors(std::set<QGraphicsItem *> & sectors)
|
||||
{
|
||||
std::set<QGraphicsItem *> sectorsToRemove;
|
||||
|
||||
for (QGraphicsItem * existingSectors : getAllSectors())
|
||||
{
|
||||
for (QGraphicsItem * sector : sectors)
|
||||
{
|
||||
if (existingSectors->sceneBoundingRect().contains(sector->sceneBoundingRect()))
|
||||
sectorsToRemove.insert(existingSectors);
|
||||
}
|
||||
}
|
||||
for (QGraphicsItem * sectorToRemove : sectorsToRemove)
|
||||
{
|
||||
addSector(draw(sectorToRemove->sceneBoundingRect()));
|
||||
removeSector(sectorToRemove);
|
||||
}
|
||||
}
|
||||
|
||||
const QList<QGraphicsItem *> AbstractViewportLayer::getAllSectors() //returning const is necessary to avoid "range-loop might detach Qt container" problem
|
||||
{
|
||||
QList<QGraphicsItem *> emptyList;
|
||||
return items ? items->childItems() : emptyList;
|
||||
}
|
||||
|
||||
std::set<QGraphicsItem *> AbstractViewportLayer::getContainingSectors(const std::vector<int3> & tiles)
|
||||
{
|
||||
std::set<QGraphicsItem *> result;
|
||||
for (QGraphicsItem * existingSector : getAllSectors()) {
|
||||
for (const int3 tile : tiles)
|
||||
{
|
||||
if (existingSector->sceneBoundingRect().contains(QPointF(tile.x * tileSize, tile.y * tileSize)))
|
||||
{
|
||||
result.insert(existingSector);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::set<QGraphicsItem *> AbstractViewportLayer::getIntersectingSectors(const std::vector<QRectF> & areas)
|
||||
{
|
||||
std::set<QGraphicsItem *> result;
|
||||
for (QGraphicsItem * existingSector : getAllSectors()) {
|
||||
for (QRectF area : areas)
|
||||
{
|
||||
if (existingSector->sceneBoundingRect().intersects(area))
|
||||
{
|
||||
result.insert(existingSector);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
EmptyLayer::EmptyLayer(MapSceneBase * s): AbstractFixedLayer(s)
|
||||
{
|
||||
isShown = true;
|
||||
}
|
||||
|
||||
void EmptyLayer::update()
|
||||
{
|
||||
if(!map)
|
||||
return;
|
||||
|
||||
pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
|
||||
pixmap->fill(Qt::transparent);
|
||||
QPainter painter(pixmap.get());
|
||||
pixmap = std::make_unique<QPixmap>(map->width * 32, map->height * 32);
|
||||
redraw();
|
||||
}
|
||||
|
||||
GridLayer::GridLayer(MapSceneBase * s): AbstractViewportLayer(s)
|
||||
{
|
||||
}
|
||||
|
||||
QGraphicsItem * GridLayer::draw(const QRectF & section)
|
||||
{
|
||||
QPixmap pixmap(toInt(section.width()), toInt(section.height()));
|
||||
pixmap.fill(Qt::transparent);
|
||||
if (isShown)
|
||||
{
|
||||
QPainter painter(&pixmap);
|
||||
painter.setPen(QColor(0, 0, 0, 190));
|
||||
|
||||
for(int j = 0; j < map->height; ++j)
|
||||
for(int j = 0; j <= pixmap.height(); j += tileSize)
|
||||
{
|
||||
painter.drawLine(0, j * 32, map->width * 32 - 1, j * 32);
|
||||
painter.drawLine(0, j, pixmap.width(), j);
|
||||
}
|
||||
for(int i = 0; i < map->width; ++i)
|
||||
for(int i = 0; i <= pixmap.width(); i += tileSize)
|
||||
{
|
||||
painter.drawLine(i * 32, 0, i * 32, map->height * 32 - 1);
|
||||
painter.drawLine(i, 0, i, pixmap.height());
|
||||
}
|
||||
}
|
||||
|
||||
redraw();
|
||||
QGraphicsItem * result = scene->addPixmap(pixmap);
|
||||
result->setPos(section.x(), section.y());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
PassabilityLayer::PassabilityLayer(MapSceneBase * s): AbstractLayer(s)
|
||||
PassabilityLayer::PassabilityLayer(MapSceneBase * s): AbstractViewportLayer(s)
|
||||
{
|
||||
}
|
||||
|
||||
void PassabilityLayer::update()
|
||||
QGraphicsItem * PassabilityLayer::draw(const QRectF & section)
|
||||
{
|
||||
if(!map)
|
||||
return;
|
||||
|
||||
pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
|
||||
pixmap->fill(Qt::transparent);
|
||||
QPixmap pixmap(toInt(section.width()), toInt(section.height()));
|
||||
pixmap.fill(Qt::transparent);
|
||||
|
||||
QPainter painter(pixmap.get());
|
||||
for(int j = 0; j < map->height; ++j)
|
||||
if(isShown)
|
||||
{
|
||||
for(int i = 0; i < map->width; ++i)
|
||||
QPainter painter(&pixmap);
|
||||
for(int j = 0; j <= pixmap.height(); j += tileSize)
|
||||
{
|
||||
auto tl = map->getTile(int3(i, j, scene->level));
|
||||
for(int i = 0; i < pixmap.width(); i += tileSize)
|
||||
{
|
||||
auto tl = map->getTile(int3(toInt(section.x())/tileSize + i/tileSize, toInt(section.y())/tileSize + j/tileSize, scene->level));
|
||||
if(tl.blocked() || tl.visitable())
|
||||
{
|
||||
painter.fillRect(i * 32, j * 32, 31, 31, tl.visitable() ? QColor(200, 200, 0, 64) : QColor(255, 0, 0, 64));
|
||||
painter.fillRect(i, j, 31, 31, tl.visitable() ? QColor(200, 200, 0, 64) : QColor(255, 0, 0, 64));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
redraw();
|
||||
QGraphicsItem * result = scene->addPixmap(pixmap);
|
||||
result->setPos(section.x(), section.y());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
ObjectPickerLayer::ObjectPickerLayer(MapSceneBase * s): AbstractLayer(s)
|
||||
ObjectPickerLayer::ObjectPickerLayer(MapSceneBase * s): AbstractViewportLayer(s)
|
||||
{
|
||||
}
|
||||
|
||||
void ObjectPickerLayer::highlight(std::function<bool(const CGObjectInstance *)> predicate)
|
||||
void ObjectPickerLayer::highlight(const std::function<bool(const CGObjectInstance *)> & predicate)
|
||||
{
|
||||
if(!map)
|
||||
return;
|
||||
@@ -151,29 +387,33 @@ void ObjectPickerLayer::clear()
|
||||
isActive = false;
|
||||
}
|
||||
|
||||
void ObjectPickerLayer::update()
|
||||
QGraphicsItem * ObjectPickerLayer::draw(const QRectF & section)
|
||||
{
|
||||
if(!map)
|
||||
return;
|
||||
|
||||
pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
|
||||
pixmap->fill(Qt::transparent);
|
||||
if(isActive)
|
||||
pixmap->fill(QColor(255, 255, 255, 128));
|
||||
int offsetX = toInt(section.x());
|
||||
int offsetY = toInt(section.y());
|
||||
QPixmap pixmap(toInt(section.width()), toInt(section.height()));
|
||||
pixmap.fill(Qt::transparent);
|
||||
|
||||
if(isVisible())
|
||||
pixmap.fill(QColor(255, 255, 255, 128));
|
||||
|
||||
|
||||
QPainter painter(pixmap.get());
|
||||
QPainter painter(&pixmap);
|
||||
painter.setCompositionMode(QPainter::CompositionMode_Source);
|
||||
for(auto * obj : possibleObjects)
|
||||
for(const auto * obj : possibleObjects)
|
||||
{
|
||||
if(obj->pos.z != scene->level)
|
||||
continue;
|
||||
|
||||
for(auto & pos : obj->getBlockedPos())
|
||||
painter.fillRect(pos.x * 32, pos.y * 32, 32, 32, QColor(255, 211, 0, 64));
|
||||
for(const auto & pos : obj->getBlockedPos())
|
||||
painter.fillRect(pos.x * tileSize - offsetX, pos.y * tileSize - offsetY, tileSize, tileSize, QColor(255, 211, 0, 64));
|
||||
}
|
||||
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
|
||||
redraw();
|
||||
|
||||
QGraphicsItem * result = scene->addPixmap(pixmap);
|
||||
result->setPos(section.x(), section.y());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void ObjectPickerLayer::select(const CGObjectInstance * obj)
|
||||
@@ -181,92 +421,73 @@ void ObjectPickerLayer::select(const CGObjectInstance * obj)
|
||||
if(obj && possibleObjects.count(obj))
|
||||
{
|
||||
clear();
|
||||
selectionMade(obj);
|
||||
Q_EMIT selectionMade(obj);
|
||||
}
|
||||
}
|
||||
|
||||
void ObjectPickerLayer::discard()
|
||||
{
|
||||
clear();
|
||||
selectionMade(nullptr);
|
||||
Q_EMIT selectionMade(nullptr);
|
||||
}
|
||||
|
||||
SelectionTerrainLayer::SelectionTerrainLayer(MapSceneBase * s): AbstractLayer(s)
|
||||
SelectionTerrainLayer::SelectionTerrainLayer(MapSceneBase * s): AbstractViewportLayer(s)
|
||||
{
|
||||
}
|
||||
|
||||
void SelectionTerrainLayer::update()
|
||||
QGraphicsItem * SelectionTerrainLayer::draw(const QRectF & section)
|
||||
{
|
||||
if(!map)
|
||||
return;
|
||||
int offsetX = toInt(section.x());
|
||||
int offsetY = toInt(section.y());
|
||||
QPixmap pixmap(toInt(section.width()), toInt(section.height()));
|
||||
pixmap.fill(Qt::transparent);
|
||||
|
||||
area.clear();
|
||||
areaAdd.clear();
|
||||
areaErase.clear();
|
||||
onSelection();
|
||||
QPainter painter(&pixmap);
|
||||
|
||||
pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
|
||||
pixmap->fill(Qt::transparent);
|
||||
|
||||
redraw();
|
||||
}
|
||||
|
||||
void SelectionTerrainLayer::draw()
|
||||
{
|
||||
if(!pixmap)
|
||||
return;
|
||||
|
||||
QPainter painter(pixmap.get());
|
||||
painter.setCompositionMode(QPainter::CompositionMode_Source);
|
||||
for(auto & t : areaAdd)
|
||||
for(const auto & t : area)
|
||||
{
|
||||
painter.fillRect(t.x * 32, t.y * 32, 31, 31, QColor(128, 128, 128, 96));
|
||||
}
|
||||
for(auto & t : areaErase)
|
||||
{
|
||||
painter.fillRect(t.x * 32, t.y * 32, 31, 31, QColor(0, 0, 0, 0));
|
||||
if(section.contains(t.x * tileSize, t.y * tileSize))
|
||||
painter.fillRect(t.x * tileSize - offsetX, t.y * tileSize - offsetY, 31, 31, QColor(128, 128, 128, 96));
|
||||
}
|
||||
|
||||
areaAdd.clear();
|
||||
areaErase.clear();
|
||||
QGraphicsPixmapItem * result = scene->addPixmap(pixmap);
|
||||
result->setPos(section.x(), section.y());
|
||||
|
||||
redraw();
|
||||
return result;
|
||||
}
|
||||
|
||||
void SelectionTerrainLayer::select(const int3 & tile)
|
||||
void SelectionTerrainLayer::select(const std::vector<int3> & tiles)
|
||||
{
|
||||
for (int3 tile : tiles)
|
||||
{
|
||||
if(!map || !map->isInTheMap(tile))
|
||||
return;
|
||||
|
||||
if(!area.count(tile))
|
||||
{
|
||||
area.insert(tile);
|
||||
areaAdd.insert(tile);
|
||||
areaErase.erase(tile);
|
||||
}
|
||||
}
|
||||
redraw(tiles);
|
||||
onSelection();
|
||||
}
|
||||
|
||||
void SelectionTerrainLayer::erase(const int3 & tile)
|
||||
void SelectionTerrainLayer::erase(const std::vector<int3> & tiles)
|
||||
{
|
||||
for (int3 tile : tiles)
|
||||
{
|
||||
if(!map || !map->isInTheMap(tile))
|
||||
return;
|
||||
|
||||
if(area.count(tile))
|
||||
{
|
||||
area.erase(tile);
|
||||
areaErase.insert(tile);
|
||||
areaAdd.erase(tile);
|
||||
}
|
||||
}
|
||||
redraw(tiles);
|
||||
onSelection();
|
||||
}
|
||||
|
||||
void SelectionTerrainLayer::clear()
|
||||
{
|
||||
areaErase = area;
|
||||
areaAdd.clear();
|
||||
area.clear();
|
||||
onSelection();
|
||||
redraw();
|
||||
}
|
||||
|
||||
const std::set<int3> & SelectionTerrainLayer::selection() const
|
||||
@@ -276,157 +497,87 @@ const std::set<int3> & SelectionTerrainLayer::selection() const
|
||||
|
||||
void SelectionTerrainLayer::onSelection()
|
||||
{
|
||||
selectionMade(!area.empty());
|
||||
Q_EMIT selectionMade(!area.empty());
|
||||
}
|
||||
|
||||
|
||||
TerrainLayer::TerrainLayer(MapSceneBase * s): AbstractLayer(s)
|
||||
TerrainLayer::TerrainLayer(MapSceneBase * s): AbstractViewportLayer(s)
|
||||
{
|
||||
}
|
||||
|
||||
void TerrainLayer::update()
|
||||
void TerrainLayer::redrawTerrain(const std::vector<int3> & tiles)
|
||||
{
|
||||
if(!map)
|
||||
return;
|
||||
|
||||
pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
|
||||
draw(false);
|
||||
redrawWithSurroundingTiles(tiles);
|
||||
}
|
||||
|
||||
void TerrainLayer::setDirty(const int3 & tile)
|
||||
QGraphicsItem * TerrainLayer::draw(const QRectF & section)
|
||||
{
|
||||
dirty.insert(tile);
|
||||
int left = toInt(section.left());
|
||||
int right = toInt(section.right());
|
||||
int top = toInt(section.top());
|
||||
int bottom = toInt(section.bottom());
|
||||
QPixmap pixmap(toInt(section.width()), toInt(section.height()));
|
||||
pixmap.fill(Qt::transparent);
|
||||
|
||||
QPainter painter(&pixmap);
|
||||
|
||||
QPointF offset = section.topLeft();
|
||||
|
||||
for(int x = left/tileSize; x < right/tileSize; ++x)
|
||||
{
|
||||
for(int y = top/tileSize; y < bottom/tileSize; ++y)
|
||||
{
|
||||
handler->drawTerrainTile(painter, x, y, scene->level, offset);
|
||||
handler->drawRiver(painter, x, y, scene->level, offset);
|
||||
handler->drawRoad(painter, x, y, scene->level, offset);
|
||||
}
|
||||
}
|
||||
|
||||
void TerrainLayer::draw(bool onlyDirty)
|
||||
{
|
||||
if(!pixmap)
|
||||
return;
|
||||
QGraphicsPixmapItem * result = scene->addPixmap(pixmap);
|
||||
result->setPos(section.x(), section.y());
|
||||
|
||||
if(!map)
|
||||
return;
|
||||
return result;
|
||||
}
|
||||
|
||||
QPainter painter(pixmap.get());
|
||||
//painter.setCompositionMode(QPainter::CompositionMode_Source);
|
||||
ObjectsLayer::ObjectsLayer(MapSceneBase * s): AbstractViewportLayer(s)
|
||||
{
|
||||
}
|
||||
|
||||
if(onlyDirty)
|
||||
QGraphicsItem * ObjectsLayer::draw(const QRectF & section)
|
||||
{
|
||||
std::set<int3> forRedrawing(dirty);
|
||||
std::set<int3> neighbours;
|
||||
for(auto & t : dirty)
|
||||
int left = toInt(section.left());
|
||||
int right = toInt(section.right());
|
||||
int top = toInt(section.top());
|
||||
int bottom = toInt(section.bottom());
|
||||
QPixmap pixmap(toInt(section.width()), toInt(section.height()));
|
||||
pixmap.fill(Qt::transparent);
|
||||
|
||||
if (isShown)
|
||||
{
|
||||
for(auto & tt : int3::getDirs())
|
||||
QPainter painter(&pixmap);
|
||||
|
||||
QPointF offset = section.topLeft();
|
||||
|
||||
int margin = 2; // margin is necessary to properly display flags on heroes on a border between two sections
|
||||
|
||||
for(int x = (left - margin)/tileSize; x < (right + margin)/tileSize; ++x)
|
||||
{
|
||||
if(map->isInTheMap(t + tt))
|
||||
neighbours.insert(t + tt);
|
||||
}
|
||||
}
|
||||
for(auto & t : neighbours)
|
||||
for(int y = (top - margin)/tileSize; y < (bottom + margin)/tileSize; ++y)
|
||||
{
|
||||
for(auto & tt : int3::getDirs())
|
||||
{
|
||||
forRedrawing.insert(t);
|
||||
if(map->isInTheMap(t + tt))
|
||||
forRedrawing.insert(t + tt);
|
||||
}
|
||||
}
|
||||
for(auto & t : forRedrawing)
|
||||
{
|
||||
handler->drawTerrainTile(painter, t.x, t.y, scene->level);
|
||||
handler->drawRiver(painter, t.x, t.y, scene->level);
|
||||
handler->drawRoad(painter, t.x, t.y, scene->level);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(int j = 0; j < map->height; ++j)
|
||||
{
|
||||
for(int i = 0; i < map->width; ++i)
|
||||
{
|
||||
handler->drawTerrainTile(painter, i, j, scene->level);
|
||||
handler->drawRiver(painter, i, j, scene->level);
|
||||
handler->drawRoad(painter, i, j, scene->level);
|
||||
handler->drawObjects(painter, x, y, scene->level, offset, lockedObjects);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dirty.clear();
|
||||
redraw();
|
||||
QGraphicsPixmapItem * result = scene->addPixmap(pixmap);
|
||||
result->setPos(section.x(), section.y());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
ObjectsLayer::ObjectsLayer(MapSceneBase * s): AbstractLayer(s)
|
||||
void ObjectsLayer::redrawObjects(const std::set<CGObjectInstance *> & objects)
|
||||
{
|
||||
}
|
||||
|
||||
void ObjectsLayer::update()
|
||||
{
|
||||
if(!map)
|
||||
return;
|
||||
|
||||
pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
|
||||
pixmap->fill(Qt::transparent);
|
||||
draw(false);
|
||||
}
|
||||
|
||||
void ObjectsLayer::draw(bool onlyDirty)
|
||||
{
|
||||
if(!pixmap)
|
||||
return;
|
||||
|
||||
if(!map)
|
||||
return;
|
||||
|
||||
QPainter painter(pixmap.get());
|
||||
|
||||
if(onlyDirty)
|
||||
{
|
||||
//objects could be modified
|
||||
for(auto * obj : objDirty)
|
||||
setDirty(obj);
|
||||
|
||||
//clear tiles which will be redrawn. It's needed because some object could be replaced
|
||||
painter.setCompositionMode(QPainter::CompositionMode_Source);
|
||||
for(auto & p : dirty)
|
||||
painter.fillRect(p.x * 32, p.y * 32, 32, 32, Qt::transparent);
|
||||
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
|
||||
|
||||
for(auto & p : dirty)
|
||||
handler->drawObjects(painter, p.x, p.y, p.z, lockedObjects);
|
||||
}
|
||||
else
|
||||
{
|
||||
pixmap->fill(Qt::transparent);
|
||||
for(int j = 0; j < map->height; ++j)
|
||||
{
|
||||
for(int i = 0; i < map->width; ++i)
|
||||
{
|
||||
handler->drawObjects(painter, i, j, scene->level, lockedObjects);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dirty.clear();
|
||||
redraw();
|
||||
}
|
||||
|
||||
void ObjectsLayer::setDirty(int x, int y)
|
||||
{
|
||||
int3 pos(x, y, scene->level);
|
||||
if(map->isInTheMap(pos))
|
||||
dirty.insert(pos);
|
||||
}
|
||||
|
||||
void ObjectsLayer::setDirty(const CGObjectInstance * object)
|
||||
{
|
||||
objDirty.insert(object);
|
||||
//mark tiles under object as dirty
|
||||
for(int j = 0; j < object->getHeight(); ++j)
|
||||
{
|
||||
for(int i = 0; i < object->getWidth(); ++i)
|
||||
{
|
||||
setDirty(object->anchorPos().x - i, object->anchorPos().y - j);
|
||||
}
|
||||
}
|
||||
redraw(objects);
|
||||
}
|
||||
|
||||
void ObjectsLayer::setLockObject(const CGObjectInstance * object, bool lock)
|
||||
@@ -442,64 +593,63 @@ void ObjectsLayer::unlockAll()
|
||||
lockedObjects.clear();
|
||||
}
|
||||
|
||||
SelectionObjectsLayer::SelectionObjectsLayer(MapSceneBase * s): AbstractLayer(s), newObject(nullptr)
|
||||
SelectionObjectsLayer::SelectionObjectsLayer(MapSceneBase * s): AbstractViewportLayer(s), newObject(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
void SelectionObjectsLayer::update()
|
||||
|
||||
QGraphicsItem * SelectionObjectsLayer::draw(const QRectF & section)
|
||||
{
|
||||
if(!map)
|
||||
return;
|
||||
QPixmap pixmap(toInt(section.width()), toInt(section.height()));
|
||||
pixmap.fill(Qt::transparent);
|
||||
|
||||
selectedObjects.clear();
|
||||
onSelection();
|
||||
shift = QPoint();
|
||||
newObject.reset();
|
||||
|
||||
pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
|
||||
//pixmap->fill(QColor(0, 0, 0, 0));
|
||||
|
||||
draw();
|
||||
}
|
||||
|
||||
void SelectionObjectsLayer::draw()
|
||||
if (isShown)
|
||||
{
|
||||
if(!pixmap)
|
||||
return;
|
||||
|
||||
pixmap->fill(Qt::transparent);
|
||||
|
||||
QPainter painter(pixmap.get());
|
||||
QPainter painter(&pixmap);
|
||||
painter.setCompositionMode(QPainter::CompositionMode_Source);
|
||||
painter.setPen(Qt::white);
|
||||
|
||||
QPointF offset = section.topLeft();
|
||||
|
||||
for(auto * obj : selectedObjects)
|
||||
{
|
||||
if(obj != newObject.get())
|
||||
auto objectArea = getObjectArea(obj);
|
||||
if(obj != newObject.get() && section.intersects(objectArea))
|
||||
{
|
||||
QRect bbox(obj->anchorPos().x, obj->anchorPos().y, 1, 1);
|
||||
for(auto & t : obj->getBlockedPos())
|
||||
auto pos = obj->anchorPos();
|
||||
QRectF bbox(pos.x, pos.y, 1, 1);
|
||||
for(const auto & t : obj->getBlockedPos())
|
||||
{
|
||||
QPoint topLeft(std::min(t.x, bbox.topLeft().x()), std::min(t.y, bbox.topLeft().y()));
|
||||
QPointF topLeft(std::min(t.x * 1.0, bbox.topLeft().x()), std::min(t.y * 1.0, bbox.topLeft().y()));
|
||||
bbox.setTopLeft(topLeft);
|
||||
QPoint bottomRight(std::max(t.x, bbox.bottomRight().x()), std::max(t.y, bbox.bottomRight().y()));
|
||||
QPointF bottomRight(std::max(t.x * 1.0, bbox.bottomRight().x()), std::max(t.y * 1.0, bbox.bottomRight().y()));
|
||||
bbox.setBottomRight(bottomRight);
|
||||
}
|
||||
//selection box's size was decreased by 1 px to get rid of a persistent bug
|
||||
//with displaying a box on a border of two sectors. Bite me.
|
||||
|
||||
painter.setOpacity(1.0);
|
||||
painter.drawRect(bbox.x() * 32, bbox.y() * 32, bbox.width() * 32, bbox.height() * 32);
|
||||
QRectF rect((bbox.x() * tileSize + 1) - offset.x(), (bbox.y() * tileSize + 1) - offset.y(), (bbox.width() * tileSize) - 2, (bbox.height() * tileSize) - 2);
|
||||
painter.drawRect(rect);
|
||||
}
|
||||
|
||||
//show translation
|
||||
if(selectionMode == SelectionMode::MOVEMENT && (shift.x() || shift.y()))
|
||||
{
|
||||
objectArea.moveTo(objectArea.topLeft() + (shift * tileSize));
|
||||
if (section.intersects(objectArea))
|
||||
{
|
||||
painter.setOpacity(0.7);
|
||||
auto newPos = QPoint(obj->anchorPos().x, obj->anchorPos().y) + shift;
|
||||
handler->drawObjectAt(painter, obj, newPos.x(), newPos.y());
|
||||
handler->drawObjectAt(painter, obj, newPos.x(), newPos.y(), offset);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
redraw();
|
||||
QGraphicsPixmapItem * result = scene->addPixmap(pixmap);
|
||||
result->setPos(section.x(), section.y());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
CGObjectInstance * SelectionObjectsLayer::selectObjectAt(int x, int y, const CGObjectInstance * ignore) const
|
||||
@@ -559,6 +709,7 @@ void SelectionObjectsLayer::selectObjects(int x1, int y1, int x2, int y2)
|
||||
if(y1 > y2)
|
||||
std::swap(y1, y2);
|
||||
|
||||
std::set<CGObjectInstance *> selectedObjects;
|
||||
for(int j = y1; j < y2; ++j)
|
||||
{
|
||||
for(int i = x1; i < x2; ++i)
|
||||
@@ -567,25 +718,36 @@ void SelectionObjectsLayer::selectObjects(int x1, int y1, int x2, int y2)
|
||||
{
|
||||
for(auto & o : handler->getObjects(i, j, scene->level))
|
||||
if(!lockedObjects.count(o.obj))
|
||||
selectObject(const_cast<CGObjectInstance*>(o.obj), false); //do not inform about each object added
|
||||
{
|
||||
selectedObjects.insert(const_cast<CGObjectInstance*>(o.obj));
|
||||
}
|
||||
}
|
||||
}
|
||||
onSelection();
|
||||
}
|
||||
selectObjects(selectedObjects);
|
||||
}
|
||||
|
||||
void SelectionObjectsLayer::selectObject(CGObjectInstance * obj, bool inform /* = true */)
|
||||
void SelectionObjectsLayer::selectObject(CGObjectInstance * obj)
|
||||
{
|
||||
selectedObjects.insert(obj);
|
||||
if (inform)
|
||||
{
|
||||
onSelection();
|
||||
redraw({obj});
|
||||
}
|
||||
|
||||
void SelectionObjectsLayer::selectObjects(const std::set<CGObjectInstance *> & objs)
|
||||
{
|
||||
for (CGObjectInstance * obj : objs)
|
||||
{
|
||||
selectedObjects.insert(obj);
|
||||
}
|
||||
onSelection();
|
||||
redraw(objs);
|
||||
}
|
||||
|
||||
void SelectionObjectsLayer::deselectObject(CGObjectInstance * obj)
|
||||
{
|
||||
selectedObjects.erase(obj);
|
||||
redraw({obj});
|
||||
}
|
||||
|
||||
bool SelectionObjectsLayer::isSelected(const CGObjectInstance * obj) const
|
||||
@@ -601,22 +763,47 @@ std::set<CGObjectInstance*> SelectionObjectsLayer::getSelection() const
|
||||
void SelectionObjectsLayer::clear()
|
||||
{
|
||||
selectedObjects.clear();
|
||||
onSelection();
|
||||
shift.setX(0);
|
||||
shift.setY(0);
|
||||
redraw();
|
||||
}
|
||||
|
||||
void SelectionObjectsLayer::onSelection()
|
||||
{
|
||||
selectionMade(!selectedObjects.empty());
|
||||
Q_EMIT selectionMade(!selectedObjects.empty());
|
||||
}
|
||||
|
||||
void SelectionObjectsLayer::setLockObject(const CGObjectInstance * object, bool lock)
|
||||
void SelectionObjectsLayer::setShift(int x, int y)
|
||||
{
|
||||
std::vector<QRectF>areas;
|
||||
|
||||
if(shift.x() || shift.y())
|
||||
{
|
||||
for (auto * selectedObject : selectedObjects)
|
||||
{
|
||||
QRectF formerArea = getObjectArea(selectedObject);
|
||||
formerArea.moveTo(formerArea.topLeft() + (shift * tileSize));
|
||||
areas.emplace_back(formerArea);
|
||||
}
|
||||
}
|
||||
|
||||
shift = QPoint(x, y);
|
||||
for (auto * selectedObject : selectedObjects)
|
||||
{
|
||||
QRectF area = getObjectArea(selectedObject);
|
||||
area.moveTo(area.topLeft() + (shift * tileSize));
|
||||
areas.emplace_back(area);
|
||||
}
|
||||
redraw(areas);
|
||||
}
|
||||
|
||||
void SelectionObjectsLayer::setLockObject(CGObjectInstance * object, bool lock)
|
||||
{
|
||||
if(lock)
|
||||
lockedObjects.insert(object);
|
||||
else
|
||||
lockedObjects.erase(object);
|
||||
redraw({object});
|
||||
}
|
||||
|
||||
void SelectionObjectsLayer::unlockAll()
|
||||
@@ -624,7 +811,7 @@ void SelectionObjectsLayer::unlockAll()
|
||||
lockedObjects.clear();
|
||||
}
|
||||
|
||||
MinimapLayer::MinimapLayer(MapSceneBase * s): AbstractLayer(s)
|
||||
MinimapLayer::MinimapLayer(MapSceneBase * s): AbstractFixedLayer(s)
|
||||
{
|
||||
|
||||
}
|
||||
@@ -634,7 +821,7 @@ void MinimapLayer::update()
|
||||
if(!map)
|
||||
return;
|
||||
|
||||
pixmap.reset(new QPixmap(map->width, map->height));
|
||||
pixmap = std::make_unique<QPixmap>(map->width, map->height);
|
||||
|
||||
QPainter painter(pixmap.get());
|
||||
//coordinate transformation
|
||||
@@ -649,7 +836,7 @@ void MinimapLayer::update()
|
||||
redraw();
|
||||
}
|
||||
|
||||
MinimapViewLayer::MinimapViewLayer(MapSceneBase * s): AbstractLayer(s)
|
||||
MinimapViewLayer::MinimapViewLayer(MapSceneBase * s): AbstractFixedLayer(s)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -658,7 +845,7 @@ void MinimapViewLayer::update()
|
||||
if(!map)
|
||||
return;
|
||||
|
||||
pixmap.reset(new QPixmap(map->width, map->height));
|
||||
pixmap = std::make_unique<QPixmap>(map->width, map->height);
|
||||
|
||||
draw();
|
||||
}
|
||||
|
@@ -28,19 +28,31 @@ class AbstractLayer : public QObject
|
||||
Q_OBJECT
|
||||
public:
|
||||
AbstractLayer(MapSceneBase * s);
|
||||
|
||||
virtual void update() = 0;
|
||||
|
||||
void show(bool show);
|
||||
void redraw();
|
||||
void initialize(MapController & controller);
|
||||
void show(bool show);
|
||||
virtual void update() = 0;
|
||||
virtual void redraw() = 0;
|
||||
|
||||
protected:
|
||||
int mapWidthPx() const;
|
||||
int mapHeightPx() const;
|
||||
int toInt(double value) const;
|
||||
|
||||
MapSceneBase * scene;
|
||||
CMap * map = nullptr;
|
||||
MapHandler * handler = nullptr;
|
||||
bool isShown = false;
|
||||
const int tileSize = 32;
|
||||
};
|
||||
|
||||
class AbstractFixedLayer : public AbstractLayer
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
AbstractFixedLayer(MapSceneBase * s);
|
||||
void redraw();
|
||||
|
||||
protected:
|
||||
std::unique_ptr<QPixmap> pixmap;
|
||||
QPixmap emptyPixmap;
|
||||
|
||||
@@ -48,40 +60,79 @@ private:
|
||||
std::unique_ptr<QGraphicsPixmapItem> item;
|
||||
};
|
||||
|
||||
class AbstractViewportLayer : public AbstractLayer
|
||||
{
|
||||
public:
|
||||
AbstractViewportLayer(MapSceneBase * s);
|
||||
void createLayer();
|
||||
void setViewport(const QRectF & _viewPort);
|
||||
|
||||
class GridLayer: public AbstractLayer
|
||||
void update();
|
||||
void redraw();
|
||||
protected:
|
||||
virtual QGraphicsItem * draw(const QRectF & area) = 0;
|
||||
void redraw(const std::vector<int3> & tiles);
|
||||
void redrawWithSurroundingTiles(const std::vector<int3> & tiles);
|
||||
void redraw(const std::set<CGObjectInstance *> & objects);
|
||||
void redraw(const std::vector<QRectF> & areas);
|
||||
QRectF getObjectArea(const CGObjectInstance * object);
|
||||
private:
|
||||
void addSector(QGraphicsItem * item);
|
||||
void removeSector(QGraphicsItem * item);
|
||||
void redrawSectors(std::set<QGraphicsItem *> & items);
|
||||
const QList<QGraphicsItem *> getAllSectors();
|
||||
|
||||
std::set<QGraphicsItem *> getContainingSectors(const std::vector<int3> & tiles);
|
||||
std::set<QGraphicsItem *> getIntersectingSectors(const std::vector<QRectF> & areas);
|
||||
std::unique_ptr<QGraphicsItemGroup> items;
|
||||
const int sectorSizeInTiles = 10;
|
||||
const int sectorSize = sectorSizeInTiles * tileSize;
|
||||
};
|
||||
|
||||
class EmptyLayer: public AbstractFixedLayer
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
EmptyLayer(MapSceneBase * s);
|
||||
|
||||
void update() override;
|
||||
};
|
||||
|
||||
class GridLayer: public AbstractViewportLayer
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
GridLayer(MapSceneBase * s);
|
||||
|
||||
void update() override;
|
||||
protected:
|
||||
QGraphicsItem * draw(const QRectF & section) override;
|
||||
};
|
||||
|
||||
class PassabilityLayer: public AbstractLayer
|
||||
class PassabilityLayer: public AbstractViewportLayer
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
PassabilityLayer(MapSceneBase * s);
|
||||
|
||||
void update() override;
|
||||
protected:
|
||||
QGraphicsItem * draw(const QRectF & section) override;
|
||||
};
|
||||
|
||||
class SelectionTerrainLayer: public AbstractLayer
|
||||
class SelectionTerrainLayer: public AbstractViewportLayer
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
SelectionTerrainLayer(MapSceneBase* s);
|
||||
|
||||
void update() override;
|
||||
|
||||
void draw();
|
||||
void select(const int3 & tile);
|
||||
void erase(const int3 & tile);
|
||||
void select(const std::vector<int3> & tiles);
|
||||
void erase(const std::vector<int3> & tiles);
|
||||
void clear();
|
||||
|
||||
const std::set<int3> & selection() const;
|
||||
|
||||
protected:
|
||||
QGraphicsItem * draw(const QRectF & section) override;
|
||||
|
||||
signals:
|
||||
void selectionMade(bool anythingSelected);
|
||||
|
||||
@@ -94,52 +145,39 @@ private:
|
||||
};
|
||||
|
||||
|
||||
class TerrainLayer: public AbstractLayer
|
||||
class TerrainLayer: public AbstractViewportLayer
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
TerrainLayer(MapSceneBase * s);
|
||||
void redrawTerrain(const std::vector<int3> & tiles);
|
||||
|
||||
void update() override;
|
||||
|
||||
void draw(bool onlyDirty = true);
|
||||
void setDirty(const int3 & tile);
|
||||
|
||||
private:
|
||||
std::set<int3> dirty;
|
||||
protected:
|
||||
QGraphicsItem * draw(const QRectF & section) override;
|
||||
};
|
||||
|
||||
|
||||
class ObjectsLayer: public AbstractLayer
|
||||
class ObjectsLayer: public AbstractViewportLayer
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
ObjectsLayer(MapSceneBase * s);
|
||||
|
||||
void update() override;
|
||||
|
||||
void draw(bool onlyDirty = true);
|
||||
|
||||
void setDirty(int x, int y);
|
||||
void setDirty(const CGObjectInstance * object);
|
||||
|
||||
void redrawObjects(const std::set<CGObjectInstance *> & objects);
|
||||
void setLockObject(const CGObjectInstance * object, bool lock);
|
||||
void unlockAll();
|
||||
|
||||
protected:
|
||||
QGraphicsItem * draw(const QRectF & section) override;
|
||||
private:
|
||||
std::set<const CGObjectInstance *> objDirty;
|
||||
std::set<const CGObjectInstance *> lockedObjects;
|
||||
std::set<int3> dirty;
|
||||
};
|
||||
|
||||
|
||||
class ObjectPickerLayer: public AbstractLayer
|
||||
class ObjectPickerLayer: public AbstractViewportLayer
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
ObjectPickerLayer(MapSceneBase * s);
|
||||
|
||||
void update() override;
|
||||
bool isVisible() const;
|
||||
|
||||
template<class T>
|
||||
@@ -148,12 +186,14 @@ public:
|
||||
highlight([](const CGObjectInstance * o){ return dynamic_cast<T*>(o); });
|
||||
}
|
||||
|
||||
void highlight(std::function<bool(const CGObjectInstance *)> predicate);
|
||||
void highlight(const std::function<bool(const CGObjectInstance *)> & predicate);
|
||||
|
||||
void clear();
|
||||
|
||||
void select(const CGObjectInstance *);
|
||||
void discard();
|
||||
protected:
|
||||
QGraphicsItem * draw(const QRectF & section) override;
|
||||
|
||||
signals:
|
||||
void selectionMade(const CGObjectInstance *);
|
||||
@@ -164,7 +204,7 @@ private:
|
||||
};
|
||||
|
||||
|
||||
class SelectionObjectsLayer: public AbstractLayer
|
||||
class SelectionObjectsLayer: public AbstractViewportLayer
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
@@ -175,37 +215,36 @@ public:
|
||||
|
||||
SelectionObjectsLayer(MapSceneBase* s);
|
||||
|
||||
void update() override;
|
||||
|
||||
void draw();
|
||||
|
||||
CGObjectInstance * selectObjectAt(int x, int y, const CGObjectInstance * ignore = nullptr) const;
|
||||
void selectObjects(int x1, int y1, int x2, int y2);
|
||||
void selectObject(CGObjectInstance *, bool inform = true);
|
||||
void selectObject(CGObjectInstance *);
|
||||
void deselectObject(CGObjectInstance *);
|
||||
bool isSelected(const CGObjectInstance *) const;
|
||||
std::set<CGObjectInstance*> getSelection() const;
|
||||
void clear();
|
||||
|
||||
void setLockObject(const CGObjectInstance * object, bool lock);
|
||||
void setShift(int x, int y);
|
||||
void setLockObject(CGObjectInstance * object, bool lock);
|
||||
void unlockAll();
|
||||
|
||||
QPoint shift;
|
||||
std::shared_ptr<CGObjectInstance> newObject;
|
||||
//FIXME: magic number
|
||||
QPoint shift;
|
||||
SelectionMode selectionMode = SelectionMode::NOTHING;
|
||||
|
||||
protected:
|
||||
QGraphicsItem * draw(const QRectF & section) override;
|
||||
|
||||
signals:
|
||||
void selectionMade(bool anythingSelected);
|
||||
|
||||
private:
|
||||
void selectObjects(const std::set<CGObjectInstance *> & objs);
|
||||
std::set<CGObjectInstance *> selectedObjects;
|
||||
std::set<const CGObjectInstance *> lockedObjects;
|
||||
|
||||
void onSelection();
|
||||
};
|
||||
|
||||
class MinimapLayer: public AbstractLayer
|
||||
class MinimapLayer: public AbstractFixedLayer
|
||||
{
|
||||
public:
|
||||
MinimapLayer(MapSceneBase * s);
|
||||
@@ -213,7 +252,7 @@ public:
|
||||
void update() override;
|
||||
};
|
||||
|
||||
class MinimapViewLayer: public AbstractLayer
|
||||
class MinimapViewLayer: public AbstractFixedLayer
|
||||
{
|
||||
public:
|
||||
MinimapViewLayer(MapSceneBase * s);
|
||||
|
Reference in New Issue
Block a user