mapview.cpp 14 KB

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