2
0

mapview.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. #include "../lib/GameLibrary.h"
  19. MinimapView::MinimapView(QWidget * parent):
  20. QGraphicsView(parent)
  21. {
  22. // Disable scrollbars
  23. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  24. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  25. }
  26. void MinimapView::dimensions()
  27. {
  28. fitInView(0, 0, controller->map()->width, controller->map()->height, Qt::KeepAspectRatio);
  29. }
  30. void MinimapView::setController(MapController * ctrl)
  31. {
  32. controller = ctrl;
  33. }
  34. void MinimapView::mouseMoveEvent(QMouseEvent *mouseEvent)
  35. {
  36. this->update();
  37. auto * sc = static_cast<MinimapScene*>(scene());
  38. if(!sc)
  39. return;
  40. auto pos = mapToScene(mouseEvent->pos());
  41. pos *= 32;
  42. cameraPositionChanged(pos);
  43. }
  44. void MinimapView::mousePressEvent(QMouseEvent* event)
  45. {
  46. mouseMoveEvent(event);
  47. }
  48. MapView::MapView(QWidget * parent):
  49. QGraphicsView(parent),
  50. selectionTool(MapView::SelectionTool::None)
  51. {
  52. connect(horizontalScrollBar(), &QScrollBar::valueChanged, this, &MapView::setViewports);
  53. connect(verticalScrollBar(), &QScrollBar::valueChanged, this, &MapView::setViewports);
  54. }
  55. void MapView::cameraChanged(const QPointF & pos)
  56. {
  57. centerOn(pos);
  58. }
  59. void MapView::setController(MapController * ctrl)
  60. {
  61. controller = ctrl;
  62. }
  63. void MapView::resizeEvent(QResizeEvent * event)
  64. {
  65. setViewports();
  66. }
  67. void MapView::mouseMoveEvent(QMouseEvent *mouseEvent)
  68. {
  69. this->update();
  70. auto * sc = static_cast<MapScene*>(scene());
  71. if(!sc || !controller->map())
  72. return;
  73. auto pos = mapToScene(mouseEvent->pos()); //TODO: do we need to check size?
  74. int3 tile(pos.x() / 32, pos.y() / 32, sc->level);
  75. //if scene will be scrolled without mouse movement, selection, object moving and rubber band will not be updated
  76. //to change this behavior, all this logic should be placed in viewportEvent
  77. if(rubberBand)
  78. rubberBand->setGeometry(QRect(mapFromScene(mouseStart), mouseEvent->pos()).normalized());
  79. if(tile == tilePrev) //do not redraw
  80. return;
  81. currentCoordinates(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. 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. std::vector<int3> tiles;
  94. for(auto & e : extra)
  95. {
  96. tiles.push_back(tile + e);
  97. }
  98. if(mouseEvent->buttons() & Qt::RightButton)
  99. sc->selectionTerrainView.erase(tiles);
  100. else if(mouseEvent->buttons() == Qt::LeftButton)
  101. sc->selectionTerrainView.select(tiles);
  102. }
  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. std::vector<int3> tiles;
  113. for(auto & e : extra)
  114. {
  115. tiles.push_back(tile + e);
  116. }
  117. if(mouseEvent->buttons() & Qt::RightButton)
  118. sc->selectionTerrainView.erase(tiles);
  119. else if(mouseEvent->buttons() == Qt::LeftButton)
  120. sc->selectionTerrainView.select(tiles);
  121. }
  122. break;
  123. case MapView::SelectionTool::Area:
  124. {
  125. if(mouseEvent->buttons() & Qt::RightButton || !(mouseEvent->buttons() & Qt::LeftButton))
  126. break;
  127. sc->selectionTerrainView.clear();
  128. std::vector<int3> selectedTiles;
  129. for(int j = std::min(tile.y, tileStart.y); j < std::max(tile.y, tileStart.y); ++j)
  130. {
  131. for(int i = std::min(tile.x, tileStart.x); i < std::max(tile.x, tileStart.x); ++i)
  132. {
  133. selectedTiles.emplace_back(i, j, sc->level);
  134. }
  135. }
  136. sc->selectionTerrainView.select(selectedTiles);
  137. break;
  138. }
  139. case MapView::SelectionTool::Line:
  140. {
  141. {
  142. assert(tile.z == tileStart.z);
  143. const auto diff = tile - tileStart;
  144. if(diff == int3{})
  145. break;
  146. const int edge = std::max(abs(diff.x), abs(diff.y));
  147. int distMin = std::numeric_limits<int>::max();
  148. int3 dir;
  149. for(auto & d : int3::getDirs())
  150. {
  151. if(tile.dist2d(d * edge + tileStart) < distMin)
  152. {
  153. distMin = tile.dist2d(d * edge + tileStart);
  154. dir = d;
  155. }
  156. }
  157. assert(dir != int3{});
  158. if(mouseEvent->buttons() == Qt::LeftButton)
  159. {
  160. std::vector<int3>erasedTiles(temporaryTiles.begin(), temporaryTiles.end());
  161. sc->selectionTerrainView.erase(erasedTiles);
  162. std::vector<int3>selectedTiles;
  163. for(auto ts = tileStart; ts.dist2d(tileStart) < edge; ts += dir)
  164. {
  165. if(!controller->map()->isInTheMap(ts))
  166. break;
  167. if(!sc->selectionTerrainView.selection().count(ts))
  168. temporaryTiles.insert(ts);
  169. selectedTiles.push_back(ts);
  170. }
  171. sc->selectionTerrainView.select(selectedTiles);
  172. }
  173. if(mouseEvent->buttons() == Qt::RightButton)
  174. {
  175. std::vector<int3>selectedTiles(temporaryTiles.begin(), temporaryTiles.end());
  176. sc->selectionTerrainView.select(selectedTiles);
  177. std::vector<int3>erasedTiles;
  178. for(auto ts = tileStart; ts.dist2d(tileStart) < edge; ts += dir)
  179. {
  180. if(!controller->map()->isInTheMap(ts))
  181. break;
  182. if(sc->selectionTerrainView.selection().count(ts))
  183. temporaryTiles.insert(ts);
  184. erasedTiles.push_back(ts);
  185. }
  186. sc->selectionTerrainView.erase(selectedTiles);
  187. }
  188. break;
  189. }
  190. }
  191. case MapView::SelectionTool::Lasso:
  192. {
  193. if(mouseEvent->buttons() == Qt::LeftButton)
  194. {
  195. std::vector<int3>tiles;
  196. for(auto i = tilePrev; i != tile;)
  197. {
  198. int length = std::numeric_limits<int>::max();
  199. int3 dir;
  200. for(auto & d : int3::getDirs())
  201. {
  202. if(tile.dist2dSQ(i + d) < length)
  203. {
  204. dir = d;
  205. length = tile.dist2dSQ(i + d);
  206. }
  207. }
  208. i += dir;
  209. tiles.push_back(i);
  210. }
  211. sc->selectionTerrainView.select(tiles);
  212. }
  213. break;
  214. }
  215. case MapView::SelectionTool::None:
  216. if(mouseEvent->buttons() & Qt::RightButton)
  217. break;
  218. auto sh = tile - tileStart;
  219. sc->selectionObjectsView.setShift(sh.x, sh.y);
  220. if(sh.x || sh.y)
  221. {
  222. if(!sc->selectionObjectsView.newObject && (mouseEvent->buttons() & Qt::LeftButton))
  223. {
  224. if(sc->selectionObjectsView.selectionMode == SelectionObjectsLayer::SELECTION)
  225. {
  226. sc->selectionObjectsView.clear();
  227. sc->selectionObjectsView.selectObjects(tileStart.x, tileStart.y, tile.x, tile.y);
  228. }
  229. }
  230. }
  231. break;
  232. }
  233. tilePrev = tile;
  234. }
  235. void MapView::mousePressEvent(QMouseEvent *event)
  236. {
  237. this->update();
  238. auto * sc = static_cast<MapScene*>(scene());
  239. if(!sc || !controller->map())
  240. return;
  241. if(sc->objectPickerView.isVisible())
  242. return;
  243. mouseStart = mapToScene(event->pos());
  244. tileStart = tilePrev = int3(mouseStart.x() / 32, mouseStart.y() / 32, sc->level);
  245. if(sc->selectionTerrainView.selection().count(tileStart))
  246. pressedOnSelected = true;
  247. else
  248. pressedOnSelected = false;
  249. switch(selectionTool)
  250. {
  251. case MapView::SelectionTool::Brush:
  252. case MapView::SelectionTool::Line:
  253. sc->selectionObjectsView.clear();
  254. if(event->button() == Qt::RightButton)
  255. sc->selectionTerrainView.erase({tileStart});
  256. else if(event->button() == Qt::LeftButton)
  257. sc->selectionTerrainView.select({tileStart});
  258. break;
  259. case MapView::SelectionTool::Brush2:
  260. sc->selectionObjectsView.clear();
  261. {
  262. std::array<int3, 4> extra{ int3{0, 0, 0}, int3{1, 0, 0}, int3{0, 1, 0}, int3{1, 1, 0} };
  263. std::vector<int3> tiles;
  264. for(auto & e : extra)
  265. {
  266. tiles.push_back(tileStart + e);
  267. }
  268. if(event->buttons() & Qt::RightButton)
  269. sc->selectionTerrainView.erase(tiles);
  270. else if(event->buttons() == Qt::LeftButton)
  271. sc->selectionTerrainView.select(tiles);
  272. }
  273. break;
  274. case MapView::SelectionTool::Brush4:
  275. sc->selectionObjectsView.clear();
  276. {
  277. std::array<int3, 16> extra{
  278. int3{-1, -1, 0}, int3{0, -1, 0}, int3{1, -1, 0}, int3{2, -1, 0},
  279. int3{-1, 0, 0}, int3{0, 0, 0}, int3{1, 0, 0}, int3{2, 0, 0},
  280. int3{-1, 1, 0}, int3{0, 1, 0}, int3{1, 1, 0}, int3{2, 1, 0},
  281. int3{-1, 2, 0}, int3{0, 2, 0}, int3{1, 2, 0}, int3{2, 2, 0}
  282. };
  283. std::vector<int3> tiles;
  284. for(auto & e : extra)
  285. {
  286. tiles.push_back(tileStart + e);
  287. }
  288. if(event->buttons() & Qt::RightButton)
  289. sc->selectionTerrainView.erase(tiles);
  290. else if(event->buttons() == Qt::LeftButton)
  291. sc->selectionTerrainView.select(tiles);
  292. }
  293. break;
  294. case MapView::SelectionTool::Area:
  295. case MapView::SelectionTool::Lasso:
  296. if(event->button() == Qt::RightButton)
  297. break;
  298. sc->selectionTerrainView.clear();
  299. sc->selectionObjectsView.clear();
  300. break;
  301. case MapView::SelectionTool::Fill:
  302. {
  303. if(event->button() != Qt::RightButton && event->button() != Qt::LeftButton)
  304. break;
  305. std::vector<int3> queue;
  306. std::set<int3> tilesToFill;
  307. queue.push_back(tileStart);
  308. const std::array<int3, 4> dirs{ int3{1, 0, 0}, int3{-1, 0, 0}, int3{0, 1, 0}, int3{0, -1, 0} };
  309. while(!queue.empty())
  310. {
  311. auto tile = queue.back();
  312. queue.pop_back();
  313. tilesToFill.insert(tile);
  314. for(auto & d : dirs)
  315. {
  316. auto tilen = tile + d;
  317. if (tilesToFill.count(tilen))
  318. continue;
  319. if(!controller->map()->isInTheMap(tilen))
  320. continue;
  321. if(event->button() == Qt::LeftButton)
  322. {
  323. if(controller->map()->getTile(tile).roadType
  324. && controller->map()->getTile(tile).roadType != controller->map()->getTile(tilen).roadType)
  325. continue;
  326. else if(controller->map()->getTile(tile).riverType
  327. && controller->map()->getTile(tile).riverType != controller->map()->getTile(tilen).riverType)
  328. continue;
  329. else if(controller->map()->getTile(tile).terrainType != controller->map()->getTile(tilen).terrainType)
  330. continue;
  331. }
  332. if(event->button() == Qt::LeftButton && sc->selectionTerrainView.selection().count(tilen))
  333. continue;
  334. if(event->button() == Qt::RightButton && !sc->selectionTerrainView.selection().count(tilen))
  335. continue;
  336. queue.push_back(tilen);
  337. }
  338. }
  339. std::vector<int3> result(tilesToFill.begin(), tilesToFill.end());
  340. if(event->button() == Qt::LeftButton)
  341. sc->selectionTerrainView.select(result);
  342. else
  343. sc->selectionTerrainView.erase(result);
  344. sc->selectionObjectsView.clear();
  345. break;
  346. }
  347. case MapView::SelectionTool::None:
  348. sc->selectionTerrainView.clear();
  349. if(sc->selectionObjectsView.newObject && sc->selectionObjectsView.isSelected(sc->selectionObjectsView.newObject.get()))
  350. {
  351. if(event->button() == Qt::RightButton)
  352. controller->discardObject(sc->level);
  353. }
  354. else
  355. {
  356. if(event->button() == Qt::LeftButton)
  357. {
  358. //when paste, new object could be beyond initial object so we need to test two objects in order to select new one
  359. //if object is pasted at place where is multiple objects then proper selection is not guaranteed
  360. auto * firstSelectedObject = sc->selectionObjectsView.selectObjectAt(tileStart.x, tileStart.y);
  361. auto * secondSelectedObject = sc->selectionObjectsView.selectObjectAt(tileStart.x, tileStart.y, firstSelectedObject);
  362. if(firstSelectedObject)
  363. {
  364. if(sc->selectionObjectsView.isSelected(firstSelectedObject))
  365. {
  366. if(qApp->keyboardModifiers() & Qt::ControlModifier)
  367. {
  368. sc->selectionObjectsView.deselectObject(firstSelectedObject);
  369. sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::SELECTION;
  370. }
  371. else
  372. sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
  373. }
  374. else
  375. {
  376. if(!secondSelectedObject || !sc->selectionObjectsView.isSelected(secondSelectedObject))
  377. {
  378. if(!(qApp->keyboardModifiers() & Qt::ControlModifier))
  379. sc->selectionObjectsView.clear();
  380. sc->selectionObjectsView.selectObject(firstSelectedObject);
  381. }
  382. sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
  383. }
  384. }
  385. else
  386. {
  387. sc->selectionObjectsView.clear();
  388. sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::SELECTION;
  389. if(!rubberBand)
  390. rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
  391. rubberBand->setGeometry(QRect(mapFromScene(mouseStart), QSize()));
  392. rubberBand->show();
  393. }
  394. }
  395. sc->selectionObjectsView.setShift(0, 0);
  396. }
  397. break;
  398. }
  399. //main->setStatusMessage(QString("x: %1 y: %2").arg(QString::number(event->pos().x()), QString::number(event->pos().y())));
  400. }
  401. void MapView::mouseReleaseEvent(QMouseEvent *event)
  402. {
  403. this->update();
  404. auto * sc = static_cast<MapScene*>(scene());
  405. if(!sc || !controller->map())
  406. return;
  407. if(rubberBand)
  408. rubberBand->hide();
  409. if(sc->objectPickerView.isVisible())
  410. {
  411. if(event->button() == Qt::RightButton)
  412. sc->objectPickerView.discard();
  413. if(event->button() == Qt::LeftButton)
  414. {
  415. mouseStart = mapToScene(event->pos());
  416. tileStart = tilePrev = int3(mouseStart.x() / 32, mouseStart.y() / 32, sc->level);
  417. if(auto * pickedObject = sc->selectionObjectsView.selectObjectAt(tileStart.x, tileStart.y))
  418. sc->objectPickerView.select(pickedObject);
  419. }
  420. return;
  421. }
  422. switch(selectionTool)
  423. {
  424. case MapView::SelectionTool::Lasso: {
  425. if(event->button() == Qt::RightButton)
  426. break;
  427. std::vector<int3>initialTiles;
  428. //connect with initial tile
  429. for(auto i = tilePrev; i != tileStart;)
  430. {
  431. int length = std::numeric_limits<int>::max();
  432. int3 dir;
  433. for(auto & d : int3::getDirs())
  434. {
  435. if(tileStart.dist2dSQ(i + d) < length)
  436. {
  437. dir = d;
  438. length = tileStart.dist2dSQ(i + d);
  439. }
  440. }
  441. i += dir;
  442. initialTiles.push_back(i);
  443. }
  444. sc->selectionTerrainView.select(initialTiles);
  445. //key: y position of tile
  446. //value.first: x position of left tile
  447. //value.second: x postiion of right tile
  448. std::map<int, std::pair<int, int>> selectionRangeMapX;
  449. std::map<int, std::pair<int, int>> selectionRangeMapY;
  450. for(auto & t : sc->selectionTerrainView.selection())
  451. {
  452. auto pairIter = selectionRangeMapX.find(t.y);
  453. if(pairIter == selectionRangeMapX.end())
  454. selectionRangeMapX[t.y] = std::make_pair(t.x, t.x);
  455. else
  456. {
  457. pairIter->second.first = std::min(pairIter->second.first, t.x);
  458. pairIter->second.second = std::max(pairIter->second.second, t.x);
  459. }
  460. pairIter = selectionRangeMapY.find(t.x);
  461. if(pairIter == selectionRangeMapY.end())
  462. selectionRangeMapY[t.x] = std::make_pair(t.y, t.y);
  463. else
  464. {
  465. pairIter->second.first = std::min(pairIter->second.first, t.y);
  466. pairIter->second.second = std::max(pairIter->second.second, t.y);
  467. }
  468. }
  469. std::set<int3> selectionByX;
  470. std::set<int3> selectionByY;
  471. std::vector<int3> finalSelection;
  472. for(auto & selectionRange : selectionRangeMapX)
  473. {
  474. for(int i = selectionRange.second.first; i < selectionRange.second.second; ++i)
  475. selectionByX.insert(int3(i, selectionRange.first, sc->level));
  476. }
  477. for(auto & selectionRange : selectionRangeMapY)
  478. {
  479. for(int i = selectionRange.second.first; i < selectionRange.second.second; ++i)
  480. selectionByY.insert(int3(selectionRange.first, i, sc->level));
  481. }
  482. std::set_intersection(selectionByX.begin(), selectionByX.end(), selectionByY.begin(), selectionByY.end(), std::back_inserter(finalSelection));
  483. sc->selectionTerrainView.select(finalSelection);
  484. break;
  485. }
  486. case MapView::SelectionTool::Line:
  487. temporaryTiles.clear();
  488. break;
  489. case MapView::SelectionTool::None:
  490. if(event->button() == Qt::RightButton)
  491. break;
  492. //switch position
  493. bool tab = false;
  494. if(sc->selectionObjectsView.selectionMode == SelectionObjectsLayer::MOVEMENT)
  495. {
  496. tab = sc->selectionObjectsView.shift.isNull();
  497. controller->commitObjectShift(sc->level);
  498. }
  499. else
  500. {
  501. sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
  502. sc->selectionObjectsView.setShift(0, 0);
  503. tab = true;
  504. }
  505. auto selection = sc->selectionObjectsView.getSelection();
  506. if(selection.size() == 1)
  507. {
  508. openObjectProperties(*selection.begin(), tab);
  509. }
  510. break;
  511. }
  512. }
  513. void MapView::dragEnterEvent(QDragEnterEvent * event)
  514. {
  515. if(!controller || !controller->map())
  516. return;
  517. auto * sc = static_cast<MapScene*>(scene());
  518. if(!sc)
  519. return;
  520. if(event->mimeData()->hasImage())
  521. {
  522. logGlobal->info("Drag'n'drop: dispatching object");
  523. QVariant vdata = event->mimeData()->imageData();
  524. auto data = vdata.toJsonObject();
  525. if(!data.empty())
  526. {
  527. auto preview = data["preview"];
  528. if(preview != QJsonValue::Undefined)
  529. {
  530. auto objId = data["id"].toInt();
  531. auto objSubId = data["subid"].toInt();
  532. auto templateId = data["template"].toInt();
  533. auto factory = LIBRARY->objtypeh->getHandlerFor(objId, objSubId);
  534. auto templ = factory->getTemplates()[templateId];
  535. controller->discardObject(sc->level);
  536. controller->createObject(sc->level, factory->create(controller->getCallback(), templ));
  537. }
  538. }
  539. event->acceptProposedAction();
  540. }
  541. }
  542. void MapView::dropEvent(QDropEvent * event)
  543. {
  544. if(!controller || !controller->map())
  545. return;
  546. auto * sc = static_cast<MapScene*>(scene());
  547. if(!sc)
  548. return;
  549. if(sc->selectionObjectsView.newObject)
  550. {
  551. QString errorMsg;
  552. if(controller->canPlaceObject(sc->selectionObjectsView.newObject.get(), errorMsg))
  553. {
  554. auto obj = sc->selectionObjectsView.newObject;
  555. controller->commitObjectCreate(sc->level);
  556. openObjectProperties(obj.get(), false);
  557. }
  558. else
  559. {
  560. controller->discardObject(sc->level);
  561. QMessageBox::information(this, tr("Can't place object"), errorMsg);
  562. }
  563. }
  564. event->acceptProposedAction();
  565. }
  566. void MapView::dragMoveEvent(QDragMoveEvent * event)
  567. {
  568. auto * sc = static_cast<MapScene*>(scene());
  569. if(!sc)
  570. return;
  571. auto rect = event->answerRect();
  572. auto pos = mapToScene(rect.bottomRight()); //TODO: do we need to check size?
  573. int3 tile(pos.x() / 32 + 1, pos.y() / 32 + 1, sc->level);
  574. if(sc->selectionObjectsView.newObject)
  575. {
  576. sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
  577. sc->selectionObjectsView.selectObject(sc->selectionObjectsView.newObject.get());
  578. sc->selectionObjectsView.setShift(tile.x, tile.y);
  579. }
  580. event->acceptProposedAction();
  581. }
  582. void MapView::dragLeaveEvent(QDragLeaveEvent * event)
  583. {
  584. if(!controller || !controller->map())
  585. return;
  586. auto * sc = static_cast<MapScene*>(scene());
  587. if(!sc)
  588. return;
  589. controller->discardObject(sc->level);
  590. }
  591. void MapView::setViewports()
  592. {
  593. if(auto * sc = dynamic_cast<MapScene*>(scene()))
  594. {
  595. auto rect = mapToScene(viewport()->geometry()).boundingRect();
  596. controller->miniScene(sc->level)->viewport.setViewport(rect.x() / 32, rect.y() / 32, rect.width() / 32, rect.height() / 32);
  597. for (auto * layer : sc->getDynamicLayers())
  598. {
  599. layer->setViewport(rect);
  600. }
  601. }
  602. }
  603. MapSceneBase::MapSceneBase(int lvl):
  604. QGraphicsScene(nullptr),
  605. level(lvl)
  606. {
  607. }
  608. void MapSceneBase::initialize(MapController & controller)
  609. {
  610. for(auto * layer : getStaticLayers())
  611. layer->initialize(controller);
  612. for(auto * layer : getDynamicLayers())
  613. layer->initialize(controller);
  614. }
  615. void MapSceneBase::createMap()
  616. {
  617. for(auto * layer : getStaticLayers())
  618. layer->update();
  619. for(auto * layer : getDynamicLayers())
  620. layer->createLayer();
  621. }
  622. void MapSceneBase::updateMap()
  623. {
  624. for(auto * layer : getStaticLayers())
  625. layer->update();
  626. for(auto * layer : getDynamicLayers())
  627. layer->redraw();
  628. }
  629. MapScene::MapScene(int lvl):
  630. MapSceneBase(lvl),
  631. emptyLayer(this),
  632. gridView(this),
  633. passabilityView(this),
  634. selectionTerrainView(this),
  635. terrainView(this),
  636. objectsView(this),
  637. selectionObjectsView(this),
  638. objectPickerView(this),
  639. isTerrainSelected(false),
  640. isObjectSelected(false)
  641. {
  642. connect(&selectionTerrainView, &SelectionTerrainLayer::selectionMade, this, &MapScene::terrainSelected);
  643. connect(&selectionObjectsView, &SelectionObjectsLayer::selectionMade, this, &MapScene::objectSelected);
  644. }
  645. std::list<AbstractFixedLayer *> MapScene::getStaticLayers()
  646. {
  647. return {
  648. &emptyLayer
  649. };
  650. }
  651. std::list<AbstractViewportLayer *> MapScene::getDynamicLayers()
  652. {
  653. //sequence is important because it defines rendering order
  654. return {
  655. &terrainView,
  656. &objectsView,
  657. &gridView,
  658. &passabilityView,
  659. &objectPickerView,
  660. &selectionTerrainView,
  661. &selectionObjectsView
  662. };
  663. }
  664. void MapScene::createMap()
  665. {
  666. MapSceneBase::createMap();
  667. terrainView.show(true);
  668. objectsView.show(true);
  669. selectionTerrainView.show(true);
  670. selectionObjectsView.show(true);
  671. objectPickerView.show(true);
  672. }
  673. void MapScene::terrainSelected(bool anythingSelected)
  674. {
  675. isTerrainSelected = anythingSelected;
  676. selected(isTerrainSelected || isObjectSelected);
  677. }
  678. void MapScene::objectSelected(bool anythingSelected)
  679. {
  680. isObjectSelected = anythingSelected;
  681. selected(isTerrainSelected || isObjectSelected);
  682. }
  683. MinimapScene::MinimapScene(int lvl):
  684. MapSceneBase(lvl),
  685. minimapView(this),
  686. viewport(this)
  687. {
  688. }
  689. std::list<AbstractFixedLayer *> MinimapScene::getStaticLayers()
  690. {
  691. //sequence is important because it defines rendering order
  692. return {
  693. &minimapView,
  694. &viewport
  695. };
  696. }
  697. std::list<AbstractViewportLayer *> MinimapScene::getDynamicLayers()
  698. {
  699. //Nothing here
  700. return {};
  701. }
  702. void MinimapScene::createMap()
  703. {
  704. MapSceneBase::createMap();
  705. minimapView.show(true);
  706. viewport.show(true);
  707. }