mapview.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*
  2. * mapview.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "mapview.h"
  12. #include "mainwindow.h"
  13. #include <QGraphicsSceneMouseEvent>
  14. #include "mapcontroller.h"
  15. #include "../lib/mapObjectConstructors/AObjectTypeHandler.h"
  16. #include "../lib/mapObjectConstructors/CObjectClassesHandler.h"
  17. #include "../lib/mapping/CMap.h"
  18. MinimapView::MinimapView(QWidget * parent):
  19. QGraphicsView(parent)
  20. {
  21. // Disable scrollbars
  22. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  23. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  24. }
  25. void MinimapView::dimensions()
  26. {
  27. fitInView(0, 0, controller->map()->width, controller->map()->height, Qt::KeepAspectRatio);
  28. }
  29. void MinimapView::setController(MapController * ctrl)
  30. {
  31. controller = ctrl;
  32. }
  33. void MinimapView::mouseMoveEvent(QMouseEvent *mouseEvent)
  34. {
  35. this->update();
  36. auto * sc = static_cast<MinimapScene*>(scene());
  37. if(!sc)
  38. return;
  39. int w = sc->viewport.viewportWidth();
  40. int h = sc->viewport.viewportHeight();
  41. auto pos = mapToScene(mouseEvent->pos());
  42. pos.setX(pos.x() - w / 2);
  43. pos.setY(pos.y() - h / 2);
  44. QPointF point = pos * 32;
  45. emit cameraPositionChanged(point);
  46. }
  47. void MinimapView::mousePressEvent(QMouseEvent* event)
  48. {
  49. mouseMoveEvent(event);
  50. }
  51. MapView::MapView(QWidget * parent):
  52. QGraphicsView(parent),
  53. selectionTool(MapView::SelectionTool::None)
  54. {
  55. }
  56. void MapView::cameraChanged(const QPointF & pos)
  57. {
  58. horizontalScrollBar()->setValue(pos.x());
  59. verticalScrollBar()->setValue(pos.y());
  60. }
  61. void MapView::setController(MapController * ctrl)
  62. {
  63. controller = ctrl;
  64. }
  65. void MapView::mouseMoveEvent(QMouseEvent *mouseEvent)
  66. {
  67. this->update();
  68. auto * sc = static_cast<MapScene*>(scene());
  69. if(!sc || !controller->map())
  70. return;
  71. auto pos = mapToScene(mouseEvent->pos()); //TODO: do we need to check size?
  72. int3 tile(pos.x() / 32, pos.y() / 32, sc->level);
  73. //if scene will be scrolled without mouse movement, selection, object moving and rubber band will not be updated
  74. //to change this behavior, all this logic should be placed in viewportEvent
  75. if(rubberBand)
  76. rubberBand->setGeometry(QRect(mapFromScene(mouseStart), mouseEvent->pos()).normalized());
  77. if(tile == tilePrev) //do not redraw
  78. return;
  79. tilePrev = tile;
  80. //TODO: cast parent->parent to MainWindow in order to show coordinates or another way to do it?
  81. //main->setStatusMessage(QString("x: %1 y: %2").arg(tile.x, tile.y));
  82. switch(selectionTool)
  83. {
  84. case MapView::SelectionTool::Brush:
  85. if(mouseEvent->buttons() & Qt::RightButton)
  86. sc->selectionTerrainView.erase(tile);
  87. else if(mouseEvent->buttons() == Qt::LeftButton)
  88. sc->selectionTerrainView.select(tile);
  89. sc->selectionTerrainView.draw();
  90. break;
  91. case MapView::SelectionTool::Brush2:
  92. {
  93. std::array<int3, 4> extra{ int3{0, 0, 0}, int3{1, 0, 0}, int3{0, 1, 0}, int3{1, 1, 0} };
  94. for(auto & e : extra)
  95. {
  96. if(mouseEvent->buttons() & Qt::RightButton)
  97. sc->selectionTerrainView.erase(tile + e);
  98. else if(mouseEvent->buttons() == Qt::LeftButton)
  99. sc->selectionTerrainView.select(tile + e);
  100. }
  101. }
  102. sc->selectionTerrainView.draw();
  103. break;
  104. case MapView::SelectionTool::Brush4:
  105. {
  106. std::array<int3, 16> extra{
  107. int3{-1, -1, 0}, int3{0, -1, 0}, int3{1, -1, 0}, int3{2, -1, 0},
  108. int3{-1, 0, 0}, int3{0, 0, 0}, int3{1, 0, 0}, int3{2, 0, 0},
  109. int3{-1, 1, 0}, int3{0, 1, 0}, int3{1, 1, 0}, int3{2, 1, 0},
  110. int3{-1, 2, 0}, int3{0, 2, 0}, int3{1, 2, 0}, int3{2, 2, 0}
  111. };
  112. for(auto & e : extra)
  113. {
  114. if(mouseEvent->buttons() & Qt::RightButton)
  115. sc->selectionTerrainView.erase(tile + e);
  116. else if(mouseEvent->buttons() == Qt::LeftButton)
  117. sc->selectionTerrainView.select(tile + e);
  118. }
  119. }
  120. sc->selectionTerrainView.draw();
  121. break;
  122. case MapView::SelectionTool::Area:
  123. if(mouseEvent->buttons() & Qt::RightButton || !(mouseEvent->buttons() & Qt::LeftButton))
  124. break;
  125. sc->selectionTerrainView.clear();
  126. for(int j = std::min(tile.y, tileStart.y); j < std::max(tile.y, tileStart.y); ++j)
  127. {
  128. for(int i = std::min(tile.x, tileStart.x); i < std::max(tile.x, tileStart.x); ++i)
  129. {
  130. sc->selectionTerrainView.select(int3(i, j, sc->level));
  131. }
  132. }
  133. sc->selectionTerrainView.draw();
  134. break;
  135. case MapView::SelectionTool::Lasso:
  136. if(mouseEvent->buttons() == Qt::LeftButton)
  137. {
  138. sc->selectionTerrainView.select(tile);
  139. sc->selectionTerrainView.draw();
  140. }
  141. break;
  142. case MapView::SelectionTool::None:
  143. if(mouseEvent->buttons() & Qt::RightButton)
  144. break;
  145. auto sh = tile - tileStart;
  146. sc->selectionObjectsView.shift = QPoint(sh.x, sh.y);
  147. if(sh.x || sh.y)
  148. {
  149. if(!sc->selectionObjectsView.newObject && (mouseEvent->buttons() & Qt::LeftButton))
  150. {
  151. if(sc->selectionObjectsView.selectionMode == SelectionObjectsLayer::SELECTION)
  152. {
  153. sc->selectionObjectsView.clear();
  154. sc->selectionObjectsView.selectObjects(tileStart.x, tileStart.y, tile.x, tile.y);
  155. }
  156. }
  157. }
  158. sc->selectionObjectsView.draw();
  159. break;
  160. }
  161. }
  162. void MapView::mousePressEvent(QMouseEvent *event)
  163. {
  164. this->update();
  165. auto * sc = static_cast<MapScene*>(scene());
  166. if(!sc || !controller->map())
  167. return;
  168. mouseStart = mapToScene(event->pos());
  169. tileStart = tilePrev = int3(mouseStart.x() / 32, mouseStart.y() / 32, sc->level);
  170. if(sc->selectionTerrainView.selection().count(tileStart))
  171. pressedOnSelected = true;
  172. else
  173. pressedOnSelected = false;
  174. switch(selectionTool)
  175. {
  176. case MapView::SelectionTool::Brush:
  177. sc->selectionObjectsView.clear();
  178. sc->selectionObjectsView.draw();
  179. if(event->button() == Qt::RightButton)
  180. sc->selectionTerrainView.erase(tileStart);
  181. else if(event->button() == Qt::LeftButton)
  182. sc->selectionTerrainView.select(tileStart);
  183. sc->selectionTerrainView.draw();
  184. break;
  185. case MapView::SelectionTool::Brush2:
  186. sc->selectionObjectsView.clear();
  187. sc->selectionObjectsView.draw();
  188. {
  189. std::array<int3, 4> extra{ int3{0, 0, 0}, int3{1, 0, 0}, int3{0, 1, 0}, int3{1, 1, 0} };
  190. for(auto & e : extra)
  191. {
  192. if(event->button() == Qt::RightButton)
  193. sc->selectionTerrainView.erase(tileStart + e);
  194. else if(event->button() == Qt::LeftButton)
  195. sc->selectionTerrainView.select(tileStart + e);
  196. }
  197. }
  198. sc->selectionTerrainView.draw();
  199. break;
  200. case MapView::SelectionTool::Brush4:
  201. sc->selectionObjectsView.clear();
  202. sc->selectionObjectsView.draw();
  203. {
  204. std::array<int3, 16> extra{
  205. int3{-1, -1, 0}, int3{0, -1, 0}, int3{1, -1, 0}, int3{2, -1, 0},
  206. int3{-1, 0, 0}, int3{0, 0, 0}, int3{1, 0, 0}, int3{2, 0, 0},
  207. int3{-1, 1, 0}, int3{0, 1, 0}, int3{1, 1, 0}, int3{2, 1, 0},
  208. int3{-1, 2, 0}, int3{0, 2, 0}, int3{1, 2, 0}, int3{2, 2, 0}
  209. };
  210. for(auto & e : extra)
  211. {
  212. if(event->button() == Qt::RightButton)
  213. sc->selectionTerrainView.erase(tileStart + e);
  214. else if(event->button() == Qt::LeftButton)
  215. sc->selectionTerrainView.select(tileStart + e);
  216. }
  217. }
  218. sc->selectionTerrainView.draw();
  219. break;
  220. case MapView::SelectionTool::Area:
  221. case MapView::SelectionTool::Lasso:
  222. if(event->button() == Qt::RightButton)
  223. break;
  224. sc->selectionTerrainView.clear();
  225. sc->selectionTerrainView.draw();
  226. sc->selectionObjectsView.clear();
  227. sc->selectionObjectsView.draw();
  228. break;
  229. case MapView::SelectionTool::None:
  230. sc->selectionTerrainView.clear();
  231. sc->selectionTerrainView.draw();
  232. if(sc->selectionObjectsView.newObject && sc->selectionObjectsView.isSelected(sc->selectionObjectsView.newObject))
  233. {
  234. if(event->button() == Qt::RightButton)
  235. controller->discardObject(sc->level);
  236. }
  237. else
  238. {
  239. if(event->button() == Qt::LeftButton)
  240. {
  241. //when paste, new object could be beyond initial object so we need to test two objects in order to select new one
  242. //if object is pasted at place where is multiple objects then proper selection is not guaranteed
  243. auto * firstSelectedObject = sc->selectionObjectsView.selectObjectAt(tileStart.x, tileStart.y);
  244. auto * secondSelectedObject = sc->selectionObjectsView.selectObjectAt(tileStart.x, tileStart.y, firstSelectedObject);
  245. if(firstSelectedObject)
  246. {
  247. if(sc->selectionObjectsView.isSelected(firstSelectedObject))
  248. {
  249. if(qApp->keyboardModifiers() & Qt::ControlModifier)
  250. {
  251. sc->selectionObjectsView.deselectObject(firstSelectedObject);
  252. sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::SELECTION;
  253. }
  254. else
  255. sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
  256. }
  257. else
  258. {
  259. if(!secondSelectedObject || !sc->selectionObjectsView.isSelected(secondSelectedObject))
  260. {
  261. if(!(qApp->keyboardModifiers() & Qt::ControlModifier))
  262. sc->selectionObjectsView.clear();
  263. sc->selectionObjectsView.selectObject(firstSelectedObject);
  264. }
  265. sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
  266. }
  267. }
  268. else
  269. {
  270. sc->selectionObjectsView.clear();
  271. sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::SELECTION;
  272. if(!rubberBand)
  273. rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
  274. rubberBand->setGeometry(QRect(mapFromScene(mouseStart), QSize()));
  275. rubberBand->show();
  276. }
  277. }
  278. sc->selectionObjectsView.shift = QPoint(0, 0);
  279. sc->selectionObjectsView.draw();
  280. }
  281. break;
  282. }
  283. //main->setStatusMessage(QString("x: %1 y: %2").arg(QString::number(event->pos().x()), QString::number(event->pos().y())));
  284. }
  285. void MapView::mouseReleaseEvent(QMouseEvent *event)
  286. {
  287. this->update();
  288. auto * sc = static_cast<MapScene*>(scene());
  289. if(!sc || !controller->map())
  290. return;
  291. if(rubberBand)
  292. rubberBand->hide();
  293. switch(selectionTool)
  294. {
  295. case MapView::SelectionTool::Lasso: {
  296. if(event->button() == Qt::RightButton)
  297. break;
  298. //key: y position of tile
  299. //value.first: x position of left tile
  300. //value.second: x postiion of right tile
  301. std::map<int, std::pair<int, int>> selectionRangeMapX, selectionRangeMapY;
  302. for(auto & t : sc->selectionTerrainView.selection())
  303. {
  304. auto pairIter = selectionRangeMapX.find(t.y);
  305. if(pairIter == selectionRangeMapX.end())
  306. selectionRangeMapX[t.y] = std::make_pair(t.x, t.x);
  307. else
  308. {
  309. pairIter->second.first = std::min(pairIter->second.first, t.x);
  310. pairIter->second.second = std::max(pairIter->second.second, t.x);
  311. }
  312. pairIter = selectionRangeMapY.find(t.x);
  313. if(pairIter == selectionRangeMapY.end())
  314. selectionRangeMapY[t.x] = std::make_pair(t.y, t.y);
  315. else
  316. {
  317. pairIter->second.first = std::min(pairIter->second.first, t.y);
  318. pairIter->second.second = std::max(pairIter->second.second, t.y);
  319. }
  320. }
  321. std::set<int3> selectionByX, selectionByY;
  322. std::vector<int3> finalSelection;
  323. for(auto & selectionRange : selectionRangeMapX)
  324. {
  325. for(int i = selectionRange.second.first; i < selectionRange.second.second; ++i)
  326. selectionByX.insert(int3(i, selectionRange.first, sc->level));
  327. }
  328. for(auto & selectionRange : selectionRangeMapY)
  329. {
  330. for(int i = selectionRange.second.first; i < selectionRange.second.second; ++i)
  331. selectionByY.insert(int3(selectionRange.first, i, sc->level));
  332. }
  333. std::set_intersection(selectionByX.begin(), selectionByX.end(), selectionByY.begin(), selectionByY.end(), std::back_inserter(finalSelection));
  334. for(auto & lassoTile : finalSelection)
  335. sc->selectionTerrainView.select(lassoTile);
  336. sc->selectionTerrainView.draw();
  337. break;
  338. }
  339. case MapView::SelectionTool::None:
  340. if(event->button() == Qt::RightButton)
  341. break;
  342. //switch position
  343. bool tab = false;
  344. if(sc->selectionObjectsView.selectionMode == SelectionObjectsLayer::MOVEMENT)
  345. {
  346. controller->commitObjectShift(sc->level);
  347. }
  348. else
  349. {
  350. sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
  351. sc->selectionObjectsView.shift = QPoint(0, 0);
  352. sc->selectionObjectsView.draw();
  353. tab = true;
  354. //check if we have only one object
  355. }
  356. auto selection = sc->selectionObjectsView.getSelection();
  357. if(selection.size() == 1)
  358. {
  359. emit openObjectProperties(*selection.begin(), tab);
  360. }
  361. break;
  362. }
  363. }
  364. void MapView::dragEnterEvent(QDragEnterEvent * event)
  365. {
  366. if(!controller || !controller->map())
  367. return;
  368. auto * sc = static_cast<MapScene*>(scene());
  369. if(!sc)
  370. return;
  371. if(event->mimeData()->hasImage())
  372. {
  373. logGlobal->info("Drag'n'drop: dispatching object");
  374. QVariant vdata = event->mimeData()->imageData();
  375. auto data = vdata.toJsonObject();
  376. if(!data.empty())
  377. {
  378. auto preview = data["preview"];
  379. if(preview != QJsonValue::Undefined)
  380. {
  381. auto objId = data["id"].toInt();
  382. auto objSubId = data["subid"].toInt();
  383. auto templateId = data["template"].toInt();
  384. auto factory = VLC->objtypeh->getHandlerFor(objId, objSubId);
  385. auto templ = factory->getTemplates()[templateId];
  386. controller->discardObject(sc->level);
  387. controller->createObject(sc->level, factory->create(templ));
  388. }
  389. }
  390. event->acceptProposedAction();
  391. }
  392. }
  393. void MapView::dropEvent(QDropEvent * event)
  394. {
  395. if(!controller || !controller->map())
  396. return;
  397. auto * sc = static_cast<MapScene*>(scene());
  398. if(!sc)
  399. return;
  400. if(sc->selectionObjectsView.newObject)
  401. {
  402. QString errorMsg;
  403. if(controller->canPlaceObject(sc->level, sc->selectionObjectsView.newObject, errorMsg))
  404. {
  405. controller->commitObjectCreate(sc->level);
  406. }
  407. else
  408. {
  409. controller->discardObject(sc->level);
  410. QMessageBox::information(this, tr("Can't place object"), errorMsg);
  411. }
  412. }
  413. event->acceptProposedAction();
  414. }
  415. void MapView::dragMoveEvent(QDragMoveEvent * event)
  416. {
  417. auto * sc = static_cast<MapScene*>(scene());
  418. if(!sc)
  419. return;
  420. auto rect = event->answerRect();
  421. auto pos = mapToScene(rect.bottomRight()); //TODO: do we need to check size?
  422. int3 tile(pos.x() / 32 + 1, pos.y() / 32 + 1, sc->level);
  423. if(sc->selectionObjectsView.newObject)
  424. {
  425. sc->selectionObjectsView.shift = QPoint(tile.x, tile.y);
  426. sc->selectionObjectsView.selectObject(sc->selectionObjectsView.newObject);
  427. sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
  428. sc->selectionObjectsView.draw();
  429. }
  430. event->acceptProposedAction();
  431. }
  432. void MapView::dragLeaveEvent(QDragLeaveEvent * event)
  433. {
  434. if(!controller || !controller->map())
  435. return;
  436. auto * sc = static_cast<MapScene*>(scene());
  437. if(!sc)
  438. return;
  439. controller->discardObject(sc->level);
  440. }
  441. bool MapView::viewportEvent(QEvent *event)
  442. {
  443. if(auto * sc = static_cast<MapScene*>(scene()))
  444. {
  445. auto rect = mapToScene(viewport()->geometry()).boundingRect();
  446. controller->miniScene(sc->level)->viewport.setViewport(rect.x() / 32, rect.y() / 32, rect.width() / 32, rect.height() / 32);
  447. }
  448. return QGraphicsView::viewportEvent(event);
  449. }
  450. MapSceneBase::MapSceneBase(int lvl):
  451. QGraphicsScene(nullptr),
  452. level(lvl)
  453. {
  454. }
  455. void MapSceneBase::initialize(MapController & controller)
  456. {
  457. for(auto * layer : getAbstractLayers())
  458. layer->initialize(controller);
  459. }
  460. void MapSceneBase::updateViews()
  461. {
  462. for(auto * layer : getAbstractLayers())
  463. layer->update();
  464. }
  465. MapScene::MapScene(int lvl):
  466. MapSceneBase(lvl),
  467. gridView(this),
  468. passabilityView(this),
  469. selectionTerrainView(this),
  470. terrainView(this),
  471. objectsView(this),
  472. selectionObjectsView(this),
  473. isTerrainSelected(false),
  474. isObjectSelected(false)
  475. {
  476. connect(&selectionTerrainView, &SelectionTerrainLayer::selectionMade, this, &MapScene::terrainSelected);
  477. connect(&selectionObjectsView, &SelectionObjectsLayer::selectionMade, this, &MapScene::objectSelected);
  478. }
  479. std::list<AbstractLayer *> MapScene::getAbstractLayers()
  480. {
  481. //sequence is important because it defines rendering order
  482. return {
  483. &terrainView,
  484. &objectsView,
  485. &gridView,
  486. &passabilityView,
  487. &selectionTerrainView,
  488. &selectionObjectsView
  489. };
  490. }
  491. void MapScene::updateViews()
  492. {
  493. MapSceneBase::updateViews();
  494. terrainView.show(true);
  495. objectsView.show(true);
  496. selectionTerrainView.show(true);
  497. selectionObjectsView.show(true);
  498. }
  499. void MapScene::terrainSelected(bool anythingSelected)
  500. {
  501. isTerrainSelected = anythingSelected;
  502. emit selected(isTerrainSelected || isObjectSelected);
  503. }
  504. void MapScene::objectSelected(bool anythingSelected)
  505. {
  506. isObjectSelected = anythingSelected;
  507. emit selected(isTerrainSelected || isObjectSelected);
  508. }
  509. MinimapScene::MinimapScene(int lvl):
  510. MapSceneBase(lvl),
  511. minimapView(this),
  512. viewport(this)
  513. {
  514. }
  515. std::list<AbstractLayer *> MinimapScene::getAbstractLayers()
  516. {
  517. //sequence is important because it defines rendering order
  518. return {
  519. &minimapView,
  520. &viewport
  521. };
  522. }
  523. void MinimapScene::updateViews()
  524. {
  525. MapSceneBase::updateViews();
  526. minimapView.show(true);
  527. viewport.show(true);
  528. }