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"
|
2022-09-04 02:12:33 +02:00
|
|
|
#include "inspector.h"
|
2022-08-31 20:05:57 +02:00
|
|
|
#include <QGraphicsSceneMouseEvent>
|
|
|
|
|
2022-09-02 23:04:28 +02:00
|
|
|
#include "../lib/mapping/CMapEditManager.h"
|
|
|
|
|
2022-08-31 20:05:57 +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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapView::setMain(MainWindow * m)
|
|
|
|
{
|
|
|
|
main = m;
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
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());
|
|
|
|
if(!sc)
|
|
|
|
return;
|
|
|
|
|
2022-09-03 06:12:07 +02:00
|
|
|
if(sc->selectionObjectsView.newObject)
|
|
|
|
{
|
|
|
|
if(!sc->selectionObjectsView.isSelected(sc->selectionObjectsView.newObject) || event->button() == Qt::RightButton)
|
|
|
|
{
|
|
|
|
delete sc->selectionObjectsView.newObject;
|
|
|
|
sc->selectionObjectsView.newObject = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 16:07:36 +02:00
|
|
|
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-04 02:12:33 +02:00
|
|
|
if(sc->selectionObjectsView.selectionMode == 2 && sc->selectionObjectsView.applyShift())
|
2022-09-02 23:04:28 +02:00
|
|
|
{
|
2022-09-03 06:12:07 +02:00
|
|
|
sc->selectionObjectsView.newObject = nullptr;
|
2022-09-04 02:12:33 +02:00
|
|
|
sc->selectionObjectsView.selectionMode = 0;
|
|
|
|
sc->selectionObjectsView.shift = QPoint(0, 0);
|
2022-09-02 23:04:28 +02:00
|
|
|
main->resetMapHandler();
|
|
|
|
sc->updateViews();
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
main->loadInspector(*selection.begin());
|
2022-09-02 23:04:28 +02:00
|
|
|
}
|
2022-09-01 16:07:36 +02:00
|
|
|
break;
|
|
|
|
}
|
2022-08-31 20:05:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MapScene::MapScene(MainWindow *parent, int l):
|
|
|
|
QGraphicsScene(parent),
|
|
|
|
gridView(parent, this),
|
|
|
|
passabilityView(parent, this),
|
2022-09-01 03:51:29 +02:00
|
|
|
selectionTerrainView(parent, this),
|
|
|
|
terrainView(parent, this),
|
2022-09-01 13:54:39 +02:00
|
|
|
objectsView(parent, this),
|
2022-09-01 15:08:03 +02:00
|
|
|
selectionObjectsView(parent, this),
|
2022-08-31 20:05:57 +02:00
|
|
|
main(parent),
|
|
|
|
level(l)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-01 03:51:29 +02:00
|
|
|
void MapScene::updateViews()
|
2022-08-31 20:05:57 +02:00
|
|
|
{
|
2022-09-01 13:54:39 +02:00
|
|
|
//sequence is important because it defines rendering order
|
2022-09-01 03:51:29 +02:00
|
|
|
terrainView.update();
|
2022-09-01 13:54:39 +02:00
|
|
|
objectsView.update();
|
2022-08-31 20:05:57 +02:00
|
|
|
gridView.update();
|
|
|
|
passabilityView.update();
|
2022-09-01 03:51:29 +02:00
|
|
|
selectionTerrainView.update();
|
2022-09-01 15:08:03 +02:00
|
|
|
selectionObjectsView.update();
|
2022-09-01 13:04:34 +02:00
|
|
|
|
|
|
|
terrainView.show(true);
|
2022-09-01 13:54:39 +02:00
|
|
|
objectsView.show(true);
|
2022-09-01 13:04:34 +02:00
|
|
|
selectionTerrainView.show(true);
|
2022-09-01 15:08:03 +02:00
|
|
|
selectionObjectsView.show(true);
|
2022-08-31 20:05:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BasicView::BasicView(MainWindow * m, MapScene * s): main(m), scene(s)
|
|
|
|
{
|
|
|
|
if(main->getMap())
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BasicView::show(bool show)
|
|
|
|
{
|
|
|
|
if(isShown == show)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(show)
|
|
|
|
{
|
2022-09-01 13:04:34 +02:00
|
|
|
if(pixmap)
|
|
|
|
{
|
|
|
|
if(item)
|
|
|
|
item->setPixmap(*pixmap);
|
|
|
|
else
|
|
|
|
item.reset(scene->addPixmap(*pixmap));
|
|
|
|
}
|
2022-08-31 20:05:57 +02:00
|
|
|
else
|
2022-09-01 13:04:34 +02:00
|
|
|
{
|
|
|
|
if(item)
|
|
|
|
item->setPixmap(emptyPixmap);
|
|
|
|
else
|
|
|
|
item.reset(scene->addPixmap(emptyPixmap));
|
|
|
|
}
|
2022-08-31 20:05:57 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-09-01 13:04:34 +02:00
|
|
|
item->setPixmap(emptyPixmap);
|
2022-08-31 20:05:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isShown = show;
|
|
|
|
}
|
|
|
|
|
2022-09-01 03:51:29 +02:00
|
|
|
void BasicView::redraw()
|
|
|
|
{
|
|
|
|
if(item)
|
|
|
|
{
|
2022-09-01 13:04:34 +02:00
|
|
|
if(pixmap && isShown)
|
|
|
|
item->setPixmap(*pixmap);
|
|
|
|
else
|
|
|
|
item->setPixmap(emptyPixmap);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(pixmap && isShown)
|
|
|
|
item.reset(scene->addPixmap(*pixmap));
|
|
|
|
else
|
|
|
|
item.reset(scene->addPixmap(emptyPixmap));
|
2022-09-01 03:51:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 20:05:57 +02:00
|
|
|
GridView::GridView(MainWindow * m, MapScene * s): BasicView(m, s)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void GridView::update()
|
|
|
|
{
|
|
|
|
auto map = main->getMap();
|
2022-09-01 03:51:29 +02:00
|
|
|
if(!map)
|
|
|
|
return;
|
|
|
|
|
2022-08-31 20:05:57 +02:00
|
|
|
pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
|
|
|
|
pixmap->fill(QColor(0, 0, 0, 0));
|
|
|
|
QPainter painter(pixmap.get());
|
|
|
|
painter.setPen(QColor(0, 0, 0, 190));
|
|
|
|
|
|
|
|
for(int j = 0; j < map->height; ++j)
|
|
|
|
{
|
|
|
|
painter.drawLine(0, j * 32, map->width * 32 - 1, j * 32);
|
|
|
|
}
|
|
|
|
for(int i = 0; i < map->width; ++i)
|
|
|
|
{
|
|
|
|
painter.drawLine(i * 32, 0, i * 32, map->height * 32 - 1);
|
|
|
|
}
|
|
|
|
|
2022-09-01 13:04:34 +02:00
|
|
|
redraw();
|
2022-08-31 20:05:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PassabilityView::PassabilityView(MainWindow * m, MapScene * s): BasicView(m, s)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void PassabilityView::update()
|
|
|
|
{
|
|
|
|
auto map = main->getMap();
|
2022-09-01 03:51:29 +02:00
|
|
|
if(!map)
|
|
|
|
return;
|
|
|
|
|
2022-08-31 20:05:57 +02:00
|
|
|
pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
|
|
|
|
pixmap->fill(QColor(0, 0, 0, 0));
|
|
|
|
|
|
|
|
if(scene->level == 0 || map->twoLevel)
|
|
|
|
{
|
|
|
|
QPainter painter(pixmap.get());
|
|
|
|
for(int j = 0; j < map->height; ++j)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < map->width; ++i)
|
|
|
|
{
|
|
|
|
auto tl = map->getTile(int3(i, j, 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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 13:04:34 +02:00
|
|
|
redraw();
|
2022-08-31 20:05:57 +02:00
|
|
|
}
|
2022-09-01 03:51:29 +02:00
|
|
|
|
|
|
|
SelectionTerrainView::SelectionTerrainView(MainWindow * m, MapScene * s): BasicView(m, s)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionTerrainView::update()
|
|
|
|
{
|
|
|
|
auto map = main->getMap();
|
|
|
|
if(!map)
|
|
|
|
return;
|
|
|
|
|
|
|
|
area.clear();
|
|
|
|
areaAdd.clear();
|
|
|
|
areaErase.clear();
|
|
|
|
|
|
|
|
pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
|
|
|
|
pixmap->fill(QColor(0, 0, 0, 0));
|
|
|
|
|
2022-09-01 13:04:34 +02:00
|
|
|
redraw();
|
2022-09-01 03:51:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionTerrainView::draw()
|
|
|
|
{
|
|
|
|
if(!pixmap)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QPainter painter(pixmap.get());
|
|
|
|
painter.setCompositionMode(QPainter::CompositionMode_Source);
|
|
|
|
for(auto & t : areaAdd)
|
|
|
|
{
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
areaAdd.clear();
|
|
|
|
areaErase.clear();
|
|
|
|
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionTerrainView::select(const int3 & tile)
|
|
|
|
{
|
2022-09-03 15:43:20 +02:00
|
|
|
if(!main->getMap() || !main->getMap()->isInTheMap(tile))
|
|
|
|
return;
|
|
|
|
|
2022-09-01 03:51:29 +02:00
|
|
|
if(!area.count(tile))
|
|
|
|
{
|
|
|
|
area.insert(tile);
|
|
|
|
areaAdd.insert(tile);
|
|
|
|
areaErase.erase(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionTerrainView::erase(const int3 & tile)
|
|
|
|
{
|
2022-09-03 15:43:20 +02:00
|
|
|
if(!main->getMap() || !main->getMap()->isInTheMap(tile))
|
|
|
|
return;
|
|
|
|
|
2022-09-01 03:51:29 +02:00
|
|
|
if(area.count(tile))
|
|
|
|
{
|
|
|
|
area.erase(tile);
|
|
|
|
areaErase.insert(tile);
|
|
|
|
areaAdd.erase(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionTerrainView::clear()
|
|
|
|
{
|
|
|
|
areaErase = area;
|
|
|
|
areaAdd.clear();
|
|
|
|
area.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::set<int3> & SelectionTerrainView::selection() const
|
|
|
|
{
|
|
|
|
return area;
|
|
|
|
}
|
|
|
|
|
|
|
|
TerrainView::TerrainView(MainWindow * m, MapScene * s): BasicView(m, s)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void TerrainView::update()
|
|
|
|
{
|
|
|
|
auto map = main->getMap();
|
|
|
|
if(!map)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
|
|
|
|
draw(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TerrainView::setDirty(const int3 & tile)
|
|
|
|
{
|
|
|
|
dirty.insert(tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TerrainView::draw(bool onlyDirty)
|
|
|
|
{
|
|
|
|
if(!pixmap)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto map = main->getMap();
|
2022-09-01 13:54:39 +02:00
|
|
|
if(!map)
|
|
|
|
return;
|
2022-09-01 03:51:29 +02:00
|
|
|
|
|
|
|
QPainter painter(pixmap.get());
|
|
|
|
painter.setCompositionMode(QPainter::CompositionMode_Source);
|
|
|
|
|
|
|
|
if(onlyDirty)
|
|
|
|
{
|
|
|
|
std::set<int3> forRedrawing(dirty), neighbours;
|
|
|
|
for(auto & t : dirty)
|
|
|
|
{
|
|
|
|
for(auto & tt : int3::getDirs())
|
|
|
|
{
|
|
|
|
if(map->isInTheMap(t + tt))
|
|
|
|
neighbours.insert(t + tt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(auto & t : neighbours)
|
|
|
|
{
|
|
|
|
for(auto & tt : int3::getDirs())
|
|
|
|
{
|
|
|
|
forRedrawing.insert(t);
|
|
|
|
if(map->isInTheMap(t + tt))
|
|
|
|
forRedrawing.insert(t + tt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(auto & t : forRedrawing)
|
|
|
|
{
|
2022-09-03 06:12:07 +02:00
|
|
|
//TODO: fix water and roads
|
2022-09-01 03:51:29 +02:00
|
|
|
main->getMapHandler()->drawTerrainTile(painter, t.x, t.y, scene->level);
|
2022-09-03 06:12:07 +02:00
|
|
|
//main->getMapHandler()->drawRiver(painter, t.x, t.y, scene->level);
|
|
|
|
//main->getMapHandler()->drawRoad(painter, t.x, t.y, scene->level);
|
2022-09-01 03:51:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(int j = 0; j < map->height; ++j)
|
2022-09-03 06:12:07 +02:00
|
|
|
{
|
2022-09-01 03:51:29 +02:00
|
|
|
for(int i = 0; i < map->width; ++i)
|
2022-09-03 06:12:07 +02:00
|
|
|
{
|
|
|
|
//TODO: fix water and roads
|
2022-09-01 03:51:29 +02:00
|
|
|
main->getMapHandler()->drawTerrainTile(painter, i, j, scene->level);
|
2022-09-03 06:12:07 +02:00
|
|
|
//main->getMapHandler()->drawRiver(painter, i, j, scene->level);
|
|
|
|
//main->getMapHandler()->drawRoad(painter, i, j, scene->level);
|
|
|
|
}
|
|
|
|
}
|
2022-09-01 03:51:29 +02:00
|
|
|
}
|
|
|
|
|
2022-09-01 13:54:39 +02:00
|
|
|
dirty.clear();
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectsView::ObjectsView(MainWindow * m, MapScene * s): BasicView(m, s)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectsView::update()
|
|
|
|
{
|
|
|
|
auto map = main->getMap();
|
|
|
|
if(!map)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
|
|
|
|
pixmap->fill(QColor(0, 0, 0, 0));
|
|
|
|
draw(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectsView::draw(bool onlyDirty)
|
|
|
|
{
|
|
|
|
if(!pixmap)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto map = main->getMap();
|
|
|
|
if(!map)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pixmap->fill(QColor(0, 0, 0, 0));
|
|
|
|
QPainter painter(pixmap.get());
|
|
|
|
//painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
|
|
|
|
std::set<const CGObjectInstance *> drawen;
|
|
|
|
|
|
|
|
|
|
|
|
for(int j = 0; j < map->height; ++j)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < map->width; ++i)
|
|
|
|
{
|
|
|
|
main->getMapHandler()->drawObjects(painter, i, j, scene->level);
|
|
|
|
/*auto & objects = main->getMapHandler()->getObjects(i, j, scene->level);
|
|
|
|
for(auto & object : objects)
|
|
|
|
{
|
|
|
|
if(!object.obj || drawen.count(object.obj))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(!onlyDirty || dirty.count(object.obj))
|
|
|
|
{
|
|
|
|
main->getMapHandler()->drawObject(painter, object);
|
|
|
|
drawen.insert(object.obj);
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dirty.clear();
|
2022-09-01 03:51:29 +02:00
|
|
|
redraw();
|
|
|
|
}
|
2022-09-01 13:54:39 +02:00
|
|
|
|
|
|
|
void ObjectsView::setDirty(int x, int y)
|
|
|
|
{
|
|
|
|
auto & objects = main->getMapHandler()->getObjects(x, y, scene->level);
|
|
|
|
for(auto & object : objects)
|
|
|
|
{
|
|
|
|
if(object.obj)
|
|
|
|
dirty.insert(object.obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectsView::setDirty(const CGObjectInstance * object)
|
|
|
|
{
|
|
|
|
dirty.insert(object);
|
|
|
|
}
|
2022-09-01 15:08:03 +02:00
|
|
|
|
2022-09-03 06:12:07 +02:00
|
|
|
SelectionObjectsView::SelectionObjectsView(MainWindow * m, MapScene * s): BasicView(m, s), newObject(nullptr)
|
2022-09-01 15:08:03 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionObjectsView::update()
|
|
|
|
{
|
|
|
|
auto map = main->getMap();
|
|
|
|
if(!map)
|
|
|
|
return;
|
|
|
|
|
|
|
|
selectedObjects.clear();
|
2022-09-02 23:04:28 +02:00
|
|
|
shift = QPoint();
|
2022-09-03 06:12:07 +02:00
|
|
|
if(newObject)
|
|
|
|
delete newObject;
|
|
|
|
newObject = nullptr;
|
2022-09-01 15:08:03 +02:00
|
|
|
|
|
|
|
pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
|
|
|
|
//pixmap->fill(QColor(0, 0, 0, 0));
|
|
|
|
|
|
|
|
draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionObjectsView::draw()
|
|
|
|
{
|
|
|
|
if(!pixmap)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pixmap->fill(QColor(0, 0, 0, 0));
|
|
|
|
|
|
|
|
QPainter painter(pixmap.get());
|
|
|
|
painter.setCompositionMode(QPainter::CompositionMode_Source);
|
|
|
|
painter.setPen(QColor(255, 255, 255));
|
|
|
|
|
|
|
|
for(auto * obj : selectedObjects)
|
|
|
|
{
|
2022-09-03 06:12:07 +02:00
|
|
|
if(obj != newObject)
|
2022-09-01 15:08:03 +02:00
|
|
|
{
|
2022-09-03 06:12:07 +02:00
|
|
|
QRect bbox(obj->getPosition().x, obj->getPosition().y, 1, 1);
|
|
|
|
for(auto & t : obj->getBlockedPos())
|
|
|
|
{
|
|
|
|
QPoint topLeft(std::min(t.x, bbox.topLeft().x()), std::min(t.y, bbox.topLeft().y()));
|
|
|
|
bbox.setTopLeft(topLeft);
|
|
|
|
QPoint bottomRight(std::max(t.x, bbox.bottomRight().x()), std::max(t.y, bbox.bottomRight().y()));
|
|
|
|
bbox.setBottomRight(bottomRight);
|
|
|
|
}
|
2022-09-01 15:08:03 +02:00
|
|
|
|
2022-09-03 06:12:07 +02:00
|
|
|
painter.setOpacity(1.0);
|
|
|
|
painter.drawRect(bbox.x() * 32, bbox.y() * 32, bbox.width() * 32, bbox.height() * 32);
|
|
|
|
}
|
2022-09-01 16:07:36 +02:00
|
|
|
|
|
|
|
//show translation
|
2022-09-04 02:12:33 +02:00
|
|
|
if(selectionMode == 2 && (shift.x() || shift.y()))
|
2022-09-01 16:07:36 +02:00
|
|
|
{
|
|
|
|
painter.setOpacity(0.5);
|
|
|
|
auto newPos = QPoint(obj->getPosition().x, obj->getPosition().y) + shift;
|
|
|
|
main->getMapHandler()->drawObjectAt(painter, obj, newPos.x(), newPos.y());
|
|
|
|
}
|
2022-09-01 15:08:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
2022-09-04 02:12:33 +02:00
|
|
|
CGObjectInstance * SelectionObjectsView::selectObjectAt(int x, int y) const
|
2022-09-01 15:08:03 +02:00
|
|
|
{
|
2022-09-03 15:43:20 +02:00
|
|
|
if(!main->getMap() || !main->getMapHandler() || !main->getMap()->isInTheMap(int3(x, y, scene->level)))
|
2022-09-01 15:08:03 +02:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
auto & objects = main->getMapHandler()->getObjects(x, y, scene->level);
|
|
|
|
|
|
|
|
//visitable is most important
|
|
|
|
for(auto & object : objects)
|
|
|
|
{
|
2022-09-04 02:12:33 +02:00
|
|
|
if(!object.obj)
|
2022-09-01 15:08:03 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if(object.obj->visitableAt(x, y))
|
|
|
|
{
|
|
|
|
return object.obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//if not visitable tile - try to get blocked
|
|
|
|
for(auto & object : objects)
|
|
|
|
{
|
2022-09-04 02:12:33 +02:00
|
|
|
if(!object.obj)
|
2022-09-01 15:08:03 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if(object.obj->blockingAt(x, y))
|
|
|
|
{
|
|
|
|
return object.obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//finally, we can take any object
|
|
|
|
for(auto & object : objects)
|
|
|
|
{
|
2022-09-04 02:12:33 +02:00
|
|
|
if(!object.obj)
|
2022-09-01 15:08:03 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if(object.obj->coveringAt(x, y))
|
|
|
|
{
|
|
|
|
return object.obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-09-02 23:04:28 +02:00
|
|
|
bool SelectionObjectsView::applyShift()
|
|
|
|
{
|
|
|
|
if(shift.x() || shift.y())
|
|
|
|
{
|
|
|
|
for(auto * obj : selectedObjects)
|
|
|
|
{
|
|
|
|
int3 pos = obj->pos;
|
2022-09-03 06:12:07 +02:00
|
|
|
pos.z = main->getMapLevel();
|
2022-09-02 23:04:28 +02:00
|
|
|
pos.x += shift.x(); pos.y += shift.y();
|
2022-09-03 06:12:07 +02:00
|
|
|
|
|
|
|
if(obj == newObject)
|
|
|
|
{
|
|
|
|
newObject->pos = pos;
|
|
|
|
main->getMap()->getEditManager()->insertObject(newObject);
|
2022-09-04 02:12:33 +02:00
|
|
|
Initializer init(newObject);
|
2022-09-03 06:12:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
main->getMap()->getEditManager()->moveObject(obj, pos);
|
|
|
|
}
|
2022-09-02 23:04:28 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionObjectsView::deleteSelection()
|
|
|
|
{
|
|
|
|
for(auto * obj : selectedObjects)
|
|
|
|
{
|
|
|
|
main->getMap()->getEditManager()->removeObject(obj);
|
2022-09-03 06:12:07 +02:00
|
|
|
delete obj;
|
2022-09-02 23:04:28 +02:00
|
|
|
}
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2022-09-04 02:12:33 +02:00
|
|
|
void SelectionObjectsView::selectObjects(int x1, int y1, int x2, int y2)
|
2022-09-01 15:08:03 +02:00
|
|
|
{
|
2022-09-04 02:12:33 +02:00
|
|
|
if(!main->getMap() || !main->getMapHandler())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(x1 > x2)
|
|
|
|
std::swap(x1, x2);
|
|
|
|
|
|
|
|
if(y1 > y2)
|
|
|
|
std::swap(y1, y2);
|
|
|
|
|
|
|
|
for(int j = y1; j < y2; ++j)
|
|
|
|
{
|
|
|
|
for(int i = x1; i < x2; ++i)
|
|
|
|
{
|
|
|
|
for(auto & o : main->getMapHandler()->getObjects(i, j, scene->level))
|
|
|
|
selectObject(o.obj);
|
|
|
|
}
|
|
|
|
}
|
2022-09-01 15:08:03 +02:00
|
|
|
}
|
|
|
|
|
2022-09-03 06:12:07 +02:00
|
|
|
void SelectionObjectsView::selectObject(CGObjectInstance * obj)
|
|
|
|
{
|
|
|
|
selectedObjects.insert(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SelectionObjectsView::isSelected(const CGObjectInstance * obj) const
|
|
|
|
{
|
|
|
|
return selectedObjects.count(const_cast<CGObjectInstance*>(obj));
|
|
|
|
}
|
|
|
|
|
2022-09-04 02:12:33 +02:00
|
|
|
std::set<CGObjectInstance*> SelectionObjectsView::getSelection() const
|
|
|
|
{
|
|
|
|
return selectedObjects;
|
|
|
|
}
|
|
|
|
|
2022-09-01 15:08:03 +02:00
|
|
|
void SelectionObjectsView::clear()
|
|
|
|
{
|
|
|
|
selectedObjects.clear();
|
2022-09-01 16:07:36 +02:00
|
|
|
shift.setX(0);
|
|
|
|
shift.setY(0);
|
2022-09-01 15:08:03 +02:00
|
|
|
}
|