2022-09-01 03:51:29 +02:00
|
|
|
#include "StdInc.h"
|
2022-08-31 20:05:57 +02:00
|
|
|
#include "mapview.h"
|
|
|
|
#include "mainwindow.h"
|
|
|
|
#include <QGraphicsSceneMouseEvent>
|
2022-09-05 02:19:21 +02:00
|
|
|
#include "mapcontroller.h"
|
2022-08-31 20:05:57 +02:00
|
|
|
|
2022-09-05 02:19:21 +02:00
|
|
|
MapView::MapView(QWidget * parent):
|
2022-09-01 03:51:29 +02:00
|
|
|
QGraphicsView(parent),
|
2022-09-01 15:08:03 +02:00
|
|
|
selectionTool(MapView::SelectionTool::None)
|
2022-08-31 20:05:57 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-05 02:19:21 +02:00
|
|
|
void MapView::setController(MapController * ctrl)
|
2022-08-31 20:05:57 +02:00
|
|
|
{
|
2022-09-05 02:19:21 +02:00
|
|
|
controller = ctrl;
|
2022-08-31 20:05:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapView::mouseMoveEvent(QMouseEvent *mouseEvent)
|
|
|
|
{
|
|
|
|
this->update();
|
|
|
|
|
2022-09-01 03:51:29 +02:00
|
|
|
auto * sc = static_cast<MapScene*>(scene());
|
|
|
|
if(!sc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto pos = mapToScene(mouseEvent->pos()); //TODO: do we need to check size?
|
|
|
|
int3 tile(pos.x() / 32, pos.y() / 32, sc->level);
|
|
|
|
|
|
|
|
if(tile == tilePrev) //do not redraw
|
|
|
|
return;
|
|
|
|
|
|
|
|
tilePrev = tile;
|
|
|
|
|
|
|
|
//main->setStatusMessage(QString("x: %1 y: %2").arg(QString::number(pos.x()), QString::number(pos.y())));
|
|
|
|
|
|
|
|
switch(selectionTool)
|
|
|
|
{
|
|
|
|
case MapView::SelectionTool::Brush:
|
2022-09-03 06:12:07 +02:00
|
|
|
if(mouseEvent->buttons() & Qt::RightButton)
|
2022-09-01 03:51:29 +02:00
|
|
|
sc->selectionTerrainView.erase(tile);
|
2022-09-03 06:12:07 +02:00
|
|
|
else if(mouseEvent->buttons() == Qt::LeftButton)
|
2022-09-01 03:51:29 +02:00
|
|
|
sc->selectionTerrainView.select(tile);
|
|
|
|
sc->selectionTerrainView.draw();
|
|
|
|
break;
|
2022-08-31 20:05:57 +02:00
|
|
|
|
2022-09-03 15:43:20 +02:00
|
|
|
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;
|
|
|
|
|
2022-09-01 03:51:29 +02:00
|
|
|
case MapView::SelectionTool::Area:
|
2022-09-03 15:43:20 +02:00
|
|
|
if(mouseEvent->buttons() & Qt::RightButton || !mouseEvent->buttons() & Qt::LeftButton)
|
2022-09-03 06:12:07 +02:00
|
|
|
break;
|
|
|
|
|
2022-09-01 03:51:29 +02:00
|
|
|
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-09-01 16:07:36 +02:00
|
|
|
|
|
|
|
case MapView::SelectionTool::None:
|
2022-09-03 06:12:07 +02:00
|
|
|
if(mouseEvent->buttons() & Qt::RightButton)
|
|
|
|
break;
|
|
|
|
|
2022-09-01 16:07:36 +02:00
|
|
|
auto sh = tile - tileStart;
|
|
|
|
sc->selectionObjectsView.shift = QPoint(sh.x, sh.y);
|
2022-09-03 06:12:07 +02:00
|
|
|
|
2022-09-04 02:12:33 +02:00
|
|
|
if(sh.x || sh.y)
|
2022-09-03 06:12:07 +02:00
|
|
|
{
|
2022-09-04 02:12:33 +02:00
|
|
|
if(sc->selectionObjectsView.newObject)
|
|
|
|
{
|
|
|
|
sc->selectionObjectsView.shift = QPoint(tile.x, tile.y);
|
|
|
|
sc->selectionObjectsView.selectObject(sc->selectionObjectsView.newObject);
|
|
|
|
sc->selectionObjectsView.selectionMode = 2;
|
|
|
|
}
|
|
|
|
else if(mouseEvent->buttons() & Qt::LeftButton)
|
|
|
|
{
|
|
|
|
if(sc->selectionObjectsView.selectionMode == 1)
|
|
|
|
{
|
|
|
|
sc->selectionObjectsView.clear();
|
|
|
|
sc->selectionObjectsView.selectObjects(tileStart.x, tileStart.y, tile.x, tile.y);
|
|
|
|
}
|
|
|
|
}
|
2022-09-03 06:12:07 +02:00
|
|
|
}
|
|
|
|
|
2022-09-01 16:07:36 +02:00
|
|
|
sc->selectionObjectsView.draw();
|
|
|
|
break;
|
2022-09-01 03:51:29 +02:00
|
|
|
}
|
2022-08-31 20:05:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapView::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
this->update();
|
2022-09-01 03:51:29 +02:00
|
|
|
|
|
|
|
auto * sc = static_cast<MapScene*>(scene());
|
|
|
|
if(!sc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
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:
|
2022-09-01 15:08:03 +02:00
|
|
|
sc->selectionObjectsView.clear();
|
|
|
|
sc->selectionObjectsView.draw();
|
2022-09-03 06:12:07 +02:00
|
|
|
|
|
|
|
if(event->button() == Qt::RightButton)
|
2022-09-01 03:51:29 +02:00
|
|
|
sc->selectionTerrainView.erase(tileStart);
|
2022-09-03 06:12:07 +02:00
|
|
|
else if(event->button() == Qt::LeftButton)
|
2022-09-01 03:51:29 +02:00
|
|
|
sc->selectionTerrainView.select(tileStart);
|
|
|
|
sc->selectionTerrainView.draw();
|
|
|
|
break;
|
|
|
|
|
2022-09-03 15:43:20 +02:00
|
|
|
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;
|
|
|
|
|
2022-09-01 03:51:29 +02:00
|
|
|
case MapView::SelectionTool::Area:
|
2022-09-03 06:12:07 +02:00
|
|
|
if(event->button() == Qt::RightButton)
|
|
|
|
break;
|
|
|
|
|
2022-09-01 03:51:29 +02:00
|
|
|
sc->selectionTerrainView.clear();
|
|
|
|
sc->selectionTerrainView.draw();
|
2022-09-01 15:08:03 +02:00
|
|
|
sc->selectionObjectsView.clear();
|
|
|
|
sc->selectionObjectsView.draw();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MapView::SelectionTool::None:
|
|
|
|
sc->selectionTerrainView.clear();
|
|
|
|
sc->selectionTerrainView.draw();
|
2022-09-03 06:12:07 +02:00
|
|
|
|
|
|
|
if(sc->selectionObjectsView.newObject && sc->selectionObjectsView.isSelected(sc->selectionObjectsView.newObject))
|
|
|
|
{
|
2022-09-05 02:19:21 +02:00
|
|
|
if(event->button() == Qt::RightButton)
|
|
|
|
controller->discardObject(sc->level);
|
2022-09-03 06:12:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(event->button() == Qt::LeftButton)
|
2022-09-04 02:12:33 +02:00
|
|
|
{
|
|
|
|
auto * obj = sc->selectionObjectsView.selectObjectAt(tileStart.x, tileStart.y);
|
|
|
|
if(obj)
|
|
|
|
{
|
|
|
|
if(sc->selectionObjectsView.isSelected(obj))
|
|
|
|
{
|
|
|
|
sc->selectionObjectsView.selectionMode = 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sc->selectionObjectsView.clear();
|
|
|
|
sc->selectionObjectsView.selectionMode = 2;
|
|
|
|
sc->selectionObjectsView.selectObject(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sc->selectionObjectsView.clear();
|
|
|
|
sc->selectionObjectsView.selectionMode = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sc->selectionObjectsView.shift = QPoint(0, 0);
|
2022-09-03 06:12:07 +02:00
|
|
|
sc->selectionObjectsView.draw();
|
|
|
|
}
|
2022-09-01 03:51:29 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//main->setStatusMessage(QString("x: %1 y: %2").arg(QString::number(event->pos().x()), QString::number(event->pos().y())));
|
2022-08-31 20:05:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapView::mouseReleaseEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
this->update();
|
2022-09-01 16:07:36 +02:00
|
|
|
|
|
|
|
auto * sc = static_cast<MapScene*>(scene());
|
2022-09-05 02:19:21 +02:00
|
|
|
if(!sc || !controller)
|
2022-09-01 16:07:36 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
switch(selectionTool)
|
|
|
|
{
|
|
|
|
case MapView::SelectionTool::None:
|
2022-09-03 06:12:07 +02:00
|
|
|
if(event->button() == Qt::RightButton)
|
|
|
|
break;
|
2022-09-02 23:04:28 +02:00
|
|
|
//switch position
|
2022-09-05 02:19:21 +02:00
|
|
|
if(sc->selectionObjectsView.selectionMode == 2 && !sc->selectionObjectsView.shift.isNull())
|
2022-09-02 23:04:28 +02:00
|
|
|
{
|
2022-09-05 02:19:21 +02:00
|
|
|
controller->commitObjectShiftOrCreate(sc->level);
|
2022-09-02 23:04:28 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-09-04 02:12:33 +02:00
|
|
|
sc->selectionObjectsView.selectionMode = 0;
|
2022-09-02 23:04:28 +02:00
|
|
|
sc->selectionObjectsView.shift = QPoint(0, 0);
|
|
|
|
sc->selectionObjectsView.draw();
|
2022-09-04 02:12:33 +02:00
|
|
|
//check if we have only one object
|
|
|
|
auto selection = sc->selectionObjectsView.getSelection();
|
|
|
|
if(selection.size() == 1)
|
2022-08-31 20:05:57 +02:00
|
|
|
{
|
2022-09-05 02:19:21 +02:00
|
|
|
emit openObjectProperties(*selection.begin());
|
2022-08-31 20:05:57 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-05 02:19:21 +02:00
|
|
|
break;
|
2022-08-31 20:05:57 +02:00
|
|
|
}
|
2022-09-02 23:04:28 +02:00
|
|
|
}
|
|
|
|
|
2022-09-05 02:19:21 +02:00
|
|
|
MapScene::MapScene(int lev):
|
|
|
|
QGraphicsScene(nullptr),
|
|
|
|
gridView(this),
|
|
|
|
passabilityView(this),
|
|
|
|
selectionTerrainView(this),
|
|
|
|
terrainView(this),
|
|
|
|
objectsView(this),
|
|
|
|
selectionObjectsView(this),
|
|
|
|
level(lev)
|
2022-09-01 15:08:03 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-05 02:19:21 +02:00
|
|
|
std::list<AbstractLayer *> MapScene::getAbstractLayers()
|
2022-09-03 06:12:07 +02:00
|
|
|
{
|
2022-09-05 02:19:21 +02:00
|
|
|
//sequence is important because it defines rendering order
|
|
|
|
return {
|
|
|
|
&terrainView,
|
|
|
|
&objectsView,
|
|
|
|
&gridView,
|
|
|
|
&passabilityView,
|
|
|
|
&selectionTerrainView,
|
|
|
|
&selectionObjectsView
|
|
|
|
};
|
2022-09-03 06:12:07 +02:00
|
|
|
}
|
|
|
|
|
2022-09-05 02:19:21 +02:00
|
|
|
void MapScene::initialize(MapController & controller)
|
2022-09-03 06:12:07 +02:00
|
|
|
{
|
2022-09-05 02:19:21 +02:00
|
|
|
for(auto * layer : getAbstractLayers())
|
|
|
|
layer->initialize(controller);
|
2022-09-03 06:12:07 +02:00
|
|
|
}
|
|
|
|
|
2022-09-05 02:19:21 +02:00
|
|
|
void MapScene::updateViews()
|
2022-09-04 02:12:33 +02:00
|
|
|
{
|
2022-09-05 02:19:21 +02:00
|
|
|
for(auto * layer : getAbstractLayers())
|
|
|
|
layer->update();
|
2022-09-04 02:12:33 +02:00
|
|
|
|
2022-09-05 02:19:21 +02:00
|
|
|
terrainView.show(true);
|
|
|
|
objectsView.show(true);
|
|
|
|
selectionTerrainView.show(true);
|
|
|
|
selectionObjectsView.show(true);
|
2022-09-01 15:08:03 +02:00
|
|
|
}
|