mapview.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. emit currentCoordinates(tile.x, tile.y);
  81. switch(selectionTool)
  82. {
  83. case MapView::SelectionTool::Brush:
  84. if(mouseEvent->buttons() & Qt::RightButton)
  85. sc->selectionTerrainView.erase(tile);
  86. else if(mouseEvent->buttons() == Qt::LeftButton)
  87. sc->selectionTerrainView.select(tile);
  88. sc->selectionTerrainView.draw();
  89. break;
  90. case MapView::SelectionTool::Brush2:
  91. {
  92. std::array<int3, 4> extra{ int3{0, 0, 0}, int3{1, 0, 0}, int3{0, 1, 0}, int3{1, 1, 0} };
  93. for(auto & e : extra)
  94. {
  95. if(mouseEvent->buttons() & Qt::RightButton)
  96. sc->selectionTerrainView.erase(tile + e);
  97. else if(mouseEvent->buttons() == Qt::LeftButton)
  98. sc->selectionTerrainView.select(tile + e);
  99. }
  100. }
  101. sc->selectionTerrainView.draw();
  102. break;
  103. case MapView::SelectionTool::Brush4:
  104. {
  105. std::array<int3, 16> extra{
  106. int3{-1, -1, 0}, int3{0, -1, 0}, int3{1, -1, 0}, int3{2, -1, 0},
  107. int3{-1, 0, 0}, int3{0, 0, 0}, int3{1, 0, 0}, int3{2, 0, 0},
  108. int3{-1, 1, 0}, int3{0, 1, 0}, int3{1, 1, 0}, int3{2, 1, 0},
  109. int3{-1, 2, 0}, int3{0, 2, 0}, int3{1, 2, 0}, int3{2, 2, 0}
  110. };
  111. for(auto & e : extra)
  112. {
  113. if(mouseEvent->buttons() & Qt::RightButton)
  114. sc->selectionTerrainView.erase(tile + e);
  115. else if(mouseEvent->buttons() == Qt::LeftButton)
  116. sc->selectionTerrainView.select(tile + e);
  117. }
  118. }
  119. sc->selectionTerrainView.draw();
  120. break;
  121. case MapView::SelectionTool::Area:
  122. if(mouseEvent->buttons() & Qt::RightButton || !(mouseEvent->buttons() & Qt::LeftButton))
  123. break;
  124. sc->selectionTerrainView.clear();
  125. for(int j = std::min(tile.y, tileStart.y); j < std::max(tile.y, tileStart.y); ++j)
  126. {
  127. for(int i = std::min(tile.x, tileStart.x); i < std::max(tile.x, tileStart.x); ++i)
  128. {
  129. sc->selectionTerrainView.select(int3(i, j, sc->level));
  130. }
  131. }
  132. sc->selectionTerrainView.draw();
  133. break;
  134. case MapView::SelectionTool::Lasso:
  135. if(mouseEvent->buttons() == Qt::LeftButton)
  136. {
  137. sc->selectionTerrainView.select(tile);
  138. sc->selectionTerrainView.draw();
  139. }
  140. break;
  141. case MapView::SelectionTool::None:
  142. if(mouseEvent->buttons() & Qt::RightButton)
  143. break;
  144. auto sh = tile - tileStart;
  145. sc->selectionObjectsView.shift = QPoint(sh.x, sh.y);
  146. if(sh.x || sh.y)
  147. {
  148. if(!sc->selectionObjectsView.newObject && (mouseEvent->buttons() & Qt::LeftButton))
  149. {
  150. if(sc->selectionObjectsView.selectionMode == SelectionObjectsLayer::SELECTION)
  151. {
  152. sc->selectionObjectsView.clear();
  153. sc->selectionObjectsView.selectObjects(tileStart.x, tileStart.y, tile.x, tile.y);
  154. }
  155. }
  156. }
  157. sc->selectionObjectsView.draw();
  158. break;
  159. }
  160. }
  161. void MapView::mousePressEvent(QMouseEvent *event)
  162. {
  163. this->update();
  164. auto * sc = static_cast<MapScene*>(scene());
  165. if(!sc || !controller->map())
  166. return;
  167. if(sc->objectPickerView.isVisible())
  168. return;
  169. mouseStart = mapToScene(event->pos());
  170. tileStart = tilePrev = int3(mouseStart.x() / 32, mouseStart.y() / 32, sc->level);
  171. if(sc->selectionTerrainView.selection().count(tileStart))
  172. pressedOnSelected = true;
  173. else
  174. pressedOnSelected = false;
  175. switch(selectionTool)
  176. {
  177. case MapView::SelectionTool::Brush:
  178. sc->selectionObjectsView.clear();
  179. sc->selectionObjectsView.draw();
  180. if(event->button() == Qt::RightButton)
  181. sc->selectionTerrainView.erase(tileStart);
  182. else if(event->button() == Qt::LeftButton)
  183. sc->selectionTerrainView.select(tileStart);
  184. sc->selectionTerrainView.draw();
  185. break;
  186. case MapView::SelectionTool::Brush2:
  187. sc->selectionObjectsView.clear();
  188. sc->selectionObjectsView.draw();
  189. {
  190. std::array<int3, 4> extra{ int3{0, 0, 0}, int3{1, 0, 0}, int3{0, 1, 0}, int3{1, 1, 0} };
  191. for(auto & e : extra)
  192. {
  193. if(event->button() == Qt::RightButton)
  194. sc->selectionTerrainView.erase(tileStart + e);
  195. else if(event->button() == Qt::LeftButton)
  196. sc->selectionTerrainView.select(tileStart + e);
  197. }
  198. }
  199. sc->selectionTerrainView.draw();
  200. break;
  201. case MapView::SelectionTool::Brush4:
  202. sc->selectionObjectsView.clear();
  203. sc->selectionObjectsView.draw();
  204. {
  205. std::array<int3, 16> extra{
  206. int3{-1, -1, 0}, int3{0, -1, 0}, int3{1, -1, 0}, int3{2, -1, 0},
  207. int3{-1, 0, 0}, int3{0, 0, 0}, int3{1, 0, 0}, int3{2, 0, 0},
  208. int3{-1, 1, 0}, int3{0, 1, 0}, int3{1, 1, 0}, int3{2, 1, 0},
  209. int3{-1, 2, 0}, int3{0, 2, 0}, int3{1, 2, 0}, int3{2, 2, 0}
  210. };
  211. for(auto & e : extra)
  212. {
  213. if(event->button() == Qt::RightButton)
  214. sc->selectionTerrainView.erase(tileStart + e);
  215. else if(event->button() == Qt::LeftButton)
  216. sc->selectionTerrainView.select(tileStart + e);
  217. }
  218. }
  219. sc->selectionTerrainView.draw();
  220. break;
  221. case MapView::SelectionTool::Area:
  222. case MapView::SelectionTool::Lasso:
  223. if(event->button() == Qt::RightButton)
  224. break;
  225. sc->selectionTerrainView.clear();
  226. sc->selectionTerrainView.draw();
  227. sc->selectionObjectsView.clear();
  228. sc->selectionObjectsView.draw();
  229. break;
  230. case MapView::SelectionTool::None:
  231. sc->selectionTerrainView.clear();
  232. sc->selectionTerrainView.draw();
  233. if(sc->selectionObjectsView.newObject && sc->selectionObjectsView.isSelected(sc->selectionObjectsView.newObject))
  234. {
  235. if(event->button() == Qt::RightButton)
  236. controller->discardObject(sc->level);
  237. }
  238. else
  239. {
  240. if(event->button() == Qt::LeftButton)
  241. {
  242. //when paste, new object could be beyond initial object so we need to test two objects in order to select new one
  243. //if object is pasted at place where is multiple objects then proper selection is not guaranteed
  244. auto * firstSelectedObject = sc->selectionObjectsView.selectObjectAt(tileStart.x, tileStart.y);
  245. auto * secondSelectedObject = sc->selectionObjectsView.selectObjectAt(tileStart.x, tileStart.y, firstSelectedObject);
  246. if(firstSelectedObject)
  247. {
  248. if(sc->selectionObjectsView.isSelected(firstSelectedObject))
  249. {
  250. if(qApp->keyboardModifiers() & Qt::ControlModifier)
  251. {
  252. sc->selectionObjectsView.deselectObject(firstSelectedObject);
  253. sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::SELECTION;
  254. }
  255. else
  256. sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
  257. }
  258. else
  259. {
  260. if(!secondSelectedObject || !sc->selectionObjectsView.isSelected(secondSelectedObject))
  261. {
  262. if(!(qApp->keyboardModifiers() & Qt::ControlModifier))
  263. sc->selectionObjectsView.clear();
  264. sc->selectionObjectsView.selectObject(firstSelectedObject);
  265. }
  266. sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
  267. }
  268. }
  269. else
  270. {
  271. sc->selectionObjectsView.clear();
  272. sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::SELECTION;
  273. if(!rubberBand)
  274. rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
  275. rubberBand->setGeometry(QRect(mapFromScene(mouseStart), QSize()));
  276. rubberBand->show();
  277. }
  278. }
  279. sc->selectionObjectsView.shift = QPoint(0, 0);
  280. sc->selectionObjectsView.draw();
  281. }
  282. break;
  283. }
  284. //main->setStatusMessage(QString("x: %1 y: %2").arg(QString::number(event->pos().x()), QString::number(event->pos().y())));
  285. }
  286. void MapView::mouseReleaseEvent(QMouseEvent *event)
  287. {
  288. this->update();
  289. auto * sc = static_cast<MapScene*>(scene());
  290. if(!sc || !controller->map())
  291. return;
  292. if(rubberBand)
  293. rubberBand->hide();
  294. if(sc->objectPickerView.isVisible())
  295. {
  296. if(event->button() == Qt::RightButton)
  297. sc->objectPickerView.discard();
  298. if(event->button() == Qt::LeftButton)
  299. {
  300. mouseStart = mapToScene(event->pos());
  301. tileStart = tilePrev = int3(mouseStart.x() / 32, mouseStart.y() / 32, sc->level);
  302. if(auto * pickedObject = sc->selectionObjectsView.selectObjectAt(tileStart.x, tileStart.y))
  303. sc->objectPickerView.select(pickedObject);
  304. }
  305. return;
  306. }
  307. switch(selectionTool)
  308. {
  309. case MapView::SelectionTool::Lasso: {
  310. if(event->button() == Qt::RightButton)
  311. break;
  312. //key: y position of tile
  313. //value.first: x position of left tile
  314. //value.second: x postiion of right tile
  315. std::map<int, std::pair<int, int>> selectionRangeMapX, selectionRangeMapY;
  316. for(auto & t : sc->selectionTerrainView.selection())
  317. {
  318. auto pairIter = selectionRangeMapX.find(t.y);
  319. if(pairIter == selectionRangeMapX.end())
  320. selectionRangeMapX[t.y] = std::make_pair(t.x, t.x);
  321. else
  322. {
  323. pairIter->second.first = std::min(pairIter->second.first, t.x);
  324. pairIter->second.second = std::max(pairIter->second.second, t.x);
  325. }
  326. pairIter = selectionRangeMapY.find(t.x);
  327. if(pairIter == selectionRangeMapY.end())
  328. selectionRangeMapY[t.x] = std::make_pair(t.y, t.y);
  329. else
  330. {
  331. pairIter->second.first = std::min(pairIter->second.first, t.y);
  332. pairIter->second.second = std::max(pairIter->second.second, t.y);
  333. }
  334. }
  335. std::set<int3> selectionByX, selectionByY;
  336. std::vector<int3> finalSelection;
  337. for(auto & selectionRange : selectionRangeMapX)
  338. {
  339. for(int i = selectionRange.second.first; i < selectionRange.second.second; ++i)
  340. selectionByX.insert(int3(i, selectionRange.first, sc->level));
  341. }
  342. for(auto & selectionRange : selectionRangeMapY)
  343. {
  344. for(int i = selectionRange.second.first; i < selectionRange.second.second; ++i)
  345. selectionByY.insert(int3(selectionRange.first, i, sc->level));
  346. }
  347. std::set_intersection(selectionByX.begin(), selectionByX.end(), selectionByY.begin(), selectionByY.end(), std::back_inserter(finalSelection));
  348. for(auto & lassoTile : finalSelection)
  349. sc->selectionTerrainView.select(lassoTile);
  350. sc->selectionTerrainView.draw();
  351. break;
  352. }
  353. case MapView::SelectionTool::None:
  354. if(event->button() == Qt::RightButton)
  355. break;
  356. //switch position
  357. bool tab = false;
  358. if(sc->selectionObjectsView.selectionMode == SelectionObjectsLayer::MOVEMENT)
  359. {
  360. tab = sc->selectionObjectsView.shift.isNull();
  361. controller->commitObjectShift(sc->level);
  362. }
  363. else
  364. {
  365. sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
  366. sc->selectionObjectsView.shift = QPoint(0, 0);
  367. sc->selectionObjectsView.draw();
  368. tab = true;
  369. }
  370. auto selection = sc->selectionObjectsView.getSelection();
  371. if(selection.size() == 1)
  372. {
  373. emit openObjectProperties(*selection.begin(), tab);
  374. }
  375. break;
  376. }
  377. }
  378. void MapView::dragEnterEvent(QDragEnterEvent * event)
  379. {
  380. if(!controller || !controller->map())
  381. return;
  382. auto * sc = static_cast<MapScene*>(scene());
  383. if(!sc)
  384. return;
  385. if(event->mimeData()->hasImage())
  386. {
  387. logGlobal->info("Drag'n'drop: dispatching object");
  388. QVariant vdata = event->mimeData()->imageData();
  389. auto data = vdata.toJsonObject();
  390. if(!data.empty())
  391. {
  392. auto preview = data["preview"];
  393. if(preview != QJsonValue::Undefined)
  394. {
  395. auto objId = data["id"].toInt();
  396. auto objSubId = data["subid"].toInt();
  397. auto templateId = data["template"].toInt();
  398. auto factory = VLC->objtypeh->getHandlerFor(objId, objSubId);
  399. auto templ = factory->getTemplates()[templateId];
  400. controller->discardObject(sc->level);
  401. controller->createObject(sc->level, factory->create(templ));
  402. }
  403. }
  404. event->acceptProposedAction();
  405. }
  406. }
  407. void MapView::dropEvent(QDropEvent * event)
  408. {
  409. if(!controller || !controller->map())
  410. return;
  411. auto * sc = static_cast<MapScene*>(scene());
  412. if(!sc)
  413. return;
  414. if(sc->selectionObjectsView.newObject)
  415. {
  416. QString errorMsg;
  417. if(controller->canPlaceObject(sc->level, sc->selectionObjectsView.newObject, errorMsg))
  418. {
  419. auto * obj = sc->selectionObjectsView.newObject;
  420. controller->commitObjectCreate(sc->level);
  421. emit openObjectProperties(obj, false);
  422. }
  423. else
  424. {
  425. controller->discardObject(sc->level);
  426. QMessageBox::information(this, tr("Can't place object"), errorMsg);
  427. }
  428. }
  429. event->acceptProposedAction();
  430. }
  431. void MapView::dragMoveEvent(QDragMoveEvent * event)
  432. {
  433. auto * sc = static_cast<MapScene*>(scene());
  434. if(!sc)
  435. return;
  436. auto rect = event->answerRect();
  437. auto pos = mapToScene(rect.bottomRight()); //TODO: do we need to check size?
  438. int3 tile(pos.x() / 32 + 1, pos.y() / 32 + 1, sc->level);
  439. if(sc->selectionObjectsView.newObject)
  440. {
  441. sc->selectionObjectsView.shift = QPoint(tile.x, tile.y);
  442. sc->selectionObjectsView.selectObject(sc->selectionObjectsView.newObject);
  443. sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
  444. sc->selectionObjectsView.draw();
  445. }
  446. event->acceptProposedAction();
  447. }
  448. void MapView::dragLeaveEvent(QDragLeaveEvent * event)
  449. {
  450. if(!controller || !controller->map())
  451. return;
  452. auto * sc = static_cast<MapScene*>(scene());
  453. if(!sc)
  454. return;
  455. controller->discardObject(sc->level);
  456. }
  457. bool MapView::viewportEvent(QEvent *event)
  458. {
  459. if(auto * sc = static_cast<MapScene*>(scene()))
  460. {
  461. auto rect = mapToScene(viewport()->geometry()).boundingRect();
  462. controller->miniScene(sc->level)->viewport.setViewport(rect.x() / 32, rect.y() / 32, rect.width() / 32, rect.height() / 32);
  463. }
  464. return QGraphicsView::viewportEvent(event);
  465. }
  466. MapSceneBase::MapSceneBase(int lvl):
  467. QGraphicsScene(nullptr),
  468. level(lvl)
  469. {
  470. }
  471. void MapSceneBase::initialize(MapController & controller)
  472. {
  473. for(auto * layer : getAbstractLayers())
  474. layer->initialize(controller);
  475. }
  476. void MapSceneBase::updateViews()
  477. {
  478. for(auto * layer : getAbstractLayers())
  479. layer->update();
  480. }
  481. MapScene::MapScene(int lvl):
  482. MapSceneBase(lvl),
  483. gridView(this),
  484. passabilityView(this),
  485. selectionTerrainView(this),
  486. terrainView(this),
  487. objectsView(this),
  488. selectionObjectsView(this),
  489. objectPickerView(this),
  490. isTerrainSelected(false),
  491. isObjectSelected(false)
  492. {
  493. connect(&selectionTerrainView, &SelectionTerrainLayer::selectionMade, this, &MapScene::terrainSelected);
  494. connect(&selectionObjectsView, &SelectionObjectsLayer::selectionMade, this, &MapScene::objectSelected);
  495. }
  496. std::list<AbstractLayer *> MapScene::getAbstractLayers()
  497. {
  498. //sequence is important because it defines rendering order
  499. return {
  500. &terrainView,
  501. &objectsView,
  502. &gridView,
  503. &passabilityView,
  504. &objectPickerView,
  505. &selectionTerrainView,
  506. &selectionObjectsView
  507. };
  508. }
  509. void MapScene::updateViews()
  510. {
  511. MapSceneBase::updateViews();
  512. terrainView.show(true);
  513. objectsView.show(true);
  514. selectionTerrainView.show(true);
  515. selectionObjectsView.show(true);
  516. objectPickerView.show(true);
  517. }
  518. void MapScene::terrainSelected(bool anythingSelected)
  519. {
  520. isTerrainSelected = anythingSelected;
  521. emit selected(isTerrainSelected || isObjectSelected);
  522. }
  523. void MapScene::objectSelected(bool anythingSelected)
  524. {
  525. isObjectSelected = anythingSelected;
  526. emit selected(isTerrainSelected || isObjectSelected);
  527. }
  528. MinimapScene::MinimapScene(int lvl):
  529. MapSceneBase(lvl),
  530. minimapView(this),
  531. viewport(this)
  532. {
  533. }
  534. std::list<AbstractLayer *> MinimapScene::getAbstractLayers()
  535. {
  536. //sequence is important because it defines rendering order
  537. return {
  538. &minimapView,
  539. &viewport
  540. };
  541. }
  542. void MinimapScene::updateViews()
  543. {
  544. MapSceneBase::updateViews();
  545. minimapView.show(true);
  546. viewport.show(true);
  547. }