mapview.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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()->hasImage())
  312. {
  313. logGlobal->info("Drag'n'drop: dispatching object");
  314. QVariant vdata = event->mimeData()->imageData();
  315. auto data = vdata.toJsonObject();
  316. if(!data.empty())
  317. {
  318. auto preview = data["preview"];
  319. if(preview != QJsonValue::Undefined)
  320. {
  321. auto objId = data["id"].toInt();
  322. auto objSubId = data["subid"].toInt();
  323. auto templateId = data["template"].toInt();
  324. auto factory = VLC->objtypeh->getHandlerFor(objId, objSubId);
  325. auto templ = factory->getTemplates()[templateId];
  326. controller->discardObject(sc->level);
  327. controller->createObject(sc->level, factory->create(templ));
  328. }
  329. }
  330. event->acceptProposedAction();
  331. }
  332. }
  333. void MapView::dropEvent(QDropEvent * event)
  334. {
  335. if(!controller || !controller->map())
  336. return;
  337. auto * sc = static_cast<MapScene*>(scene());
  338. if(!sc)
  339. return;
  340. if(sc->selectionObjectsView.newObject)
  341. {
  342. QString errorMsg;
  343. if(controller->canPlaceObject(sc->level, sc->selectionObjectsView.newObject, errorMsg))
  344. {
  345. controller->commitObjectCreate(sc->level);
  346. }
  347. else
  348. {
  349. controller->discardObject(sc->level);
  350. QMessageBox::information(this, "Can't place object", errorMsg);
  351. }
  352. }
  353. event->acceptProposedAction();
  354. }
  355. void MapView::dragMoveEvent(QDragMoveEvent * event)
  356. {
  357. auto * sc = static_cast<MapScene*>(scene());
  358. if(!sc)
  359. return;
  360. auto rect = event->answerRect();
  361. auto pos = mapToScene(rect.bottomRight()); //TODO: do we need to check size?
  362. int3 tile(pos.x() / 32 + 1, pos.y() / 32 + 1, sc->level);
  363. if(sc->selectionObjectsView.newObject)
  364. {
  365. sc->selectionObjectsView.shift = QPoint(tile.x, tile.y);
  366. sc->selectionObjectsView.selectObject(sc->selectionObjectsView.newObject);
  367. sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
  368. sc->selectionObjectsView.draw();
  369. }
  370. event->acceptProposedAction();
  371. }
  372. void MapView::dragLeaveEvent(QDragLeaveEvent * event)
  373. {
  374. if(!controller || !controller->map())
  375. return;
  376. auto * sc = static_cast<MapScene*>(scene());
  377. if(!sc)
  378. return;
  379. controller->discardObject(sc->level);
  380. }
  381. bool MapView::viewportEvent(QEvent *event)
  382. {
  383. if(auto * sc = static_cast<MapScene*>(scene()))
  384. {
  385. auto rect = mapToScene(viewport()->geometry()).boundingRect();
  386. controller->miniScene(sc->level)->viewport.setViewport(rect.x() / 32, rect.y() / 32, rect.width() / 32, rect.height() / 32);
  387. }
  388. return QGraphicsView::viewportEvent(event);
  389. }
  390. MapSceneBase::MapSceneBase(int lvl):
  391. QGraphicsScene(nullptr),
  392. level(lvl)
  393. {
  394. }
  395. void MapSceneBase::initialize(MapController & controller)
  396. {
  397. for(auto * layer : getAbstractLayers())
  398. layer->initialize(controller);
  399. }
  400. void MapSceneBase::updateViews()
  401. {
  402. for(auto * layer : getAbstractLayers())
  403. layer->update();
  404. }
  405. MapScene::MapScene(int lvl):
  406. MapSceneBase(lvl),
  407. gridView(this),
  408. passabilityView(this),
  409. selectionTerrainView(this),
  410. terrainView(this),
  411. objectsView(this),
  412. selectionObjectsView(this),
  413. isTerrainSelected(false),
  414. isObjectSelected(false)
  415. {
  416. connect(&selectionTerrainView, &SelectionTerrainLayer::selectionMade, this, &MapScene::terrainSelected);
  417. connect(&selectionObjectsView, &SelectionObjectsLayer::selectionMade, this, &MapScene::objectSelected);
  418. }
  419. std::list<AbstractLayer *> MapScene::getAbstractLayers()
  420. {
  421. //sequence is important because it defines rendering order
  422. return {
  423. &terrainView,
  424. &objectsView,
  425. &gridView,
  426. &passabilityView,
  427. &selectionTerrainView,
  428. &selectionObjectsView
  429. };
  430. }
  431. void MapScene::updateViews()
  432. {
  433. MapSceneBase::updateViews();
  434. terrainView.show(true);
  435. objectsView.show(true);
  436. selectionTerrainView.show(true);
  437. selectionObjectsView.show(true);
  438. }
  439. void MapScene::terrainSelected(bool anythingSelected)
  440. {
  441. isTerrainSelected = anythingSelected;
  442. emit selected(isTerrainSelected || isObjectSelected);
  443. }
  444. void MapScene::objectSelected(bool anythingSelected)
  445. {
  446. isObjectSelected = anythingSelected;
  447. emit selected(isTerrainSelected || isObjectSelected);
  448. }
  449. MinimapScene::MinimapScene(int lvl):
  450. MapSceneBase(lvl),
  451. minimapView(this),
  452. viewport(this)
  453. {
  454. }
  455. std::list<AbstractLayer *> MinimapScene::getAbstractLayers()
  456. {
  457. //sequence is important because it defines rendering order
  458. return {
  459. &minimapView,
  460. &viewport
  461. };
  462. }
  463. void MinimapScene::updateViews()
  464. {
  465. MapSceneBase::updateViews();
  466. minimapView.show(true);
  467. viewport.show(true);
  468. }