scenelayer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /*
  2. * scenelayer.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 "scenelayer.h"
  12. #include "mainwindow.h"
  13. #include "../lib/mapping/CMapEditManager.h"
  14. #include "../lib/mapping/CMap.h"
  15. #include "inspector/inspector.h"
  16. #include "mapview.h"
  17. #include "mapcontroller.h"
  18. AbstractLayer::AbstractLayer(MapSceneBase * s): scene(s)
  19. {
  20. }
  21. void AbstractLayer::initialize(MapController & controller)
  22. {
  23. map = controller.map();
  24. handler = controller.mapHandler();
  25. }
  26. void AbstractLayer::show(bool show)
  27. {
  28. if(isShown == show)
  29. return;
  30. isShown = show;
  31. redraw();
  32. }
  33. void AbstractLayer::redraw()
  34. {
  35. if(item)
  36. {
  37. if(pixmap && isShown)
  38. item->setPixmap(*pixmap);
  39. else
  40. item->setPixmap(emptyPixmap);
  41. }
  42. else
  43. {
  44. if(pixmap && isShown)
  45. item.reset(scene->addPixmap(*pixmap));
  46. else
  47. item.reset(scene->addPixmap(emptyPixmap));
  48. }
  49. }
  50. GridLayer::GridLayer(MapSceneBase * s): AbstractLayer(s)
  51. {
  52. }
  53. void GridLayer::update()
  54. {
  55. if(!map)
  56. return;
  57. pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
  58. pixmap->fill(Qt::transparent);
  59. QPainter painter(pixmap.get());
  60. painter.setPen(QColor(0, 0, 0, 190));
  61. for(int j = 0; j < map->height; ++j)
  62. {
  63. painter.drawLine(0, j * 32, map->width * 32 - 1, j * 32);
  64. }
  65. for(int i = 0; i < map->width; ++i)
  66. {
  67. painter.drawLine(i * 32, 0, i * 32, map->height * 32 - 1);
  68. }
  69. redraw();
  70. }
  71. PassabilityLayer::PassabilityLayer(MapSceneBase * s): AbstractLayer(s)
  72. {
  73. }
  74. void PassabilityLayer::update()
  75. {
  76. if(!map)
  77. return;
  78. pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
  79. pixmap->fill(Qt::transparent);
  80. if(scene->level == 0 || map->twoLevel)
  81. {
  82. QPainter painter(pixmap.get());
  83. for(int j = 0; j < map->height; ++j)
  84. {
  85. for(int i = 0; i < map->width; ++i)
  86. {
  87. auto tl = map->getTile(int3(i, j, scene->level));
  88. if(tl.blocked() || tl.visitable())
  89. {
  90. painter.fillRect(i * 32, j * 32, 31, 31, tl.visitable() ? QColor(200, 200, 0, 64) : QColor(255, 0, 0, 64));
  91. }
  92. }
  93. }
  94. }
  95. redraw();
  96. }
  97. ObjectPickerLayer::ObjectPickerLayer(MapSceneBase * s): AbstractLayer(s)
  98. {
  99. }
  100. void ObjectPickerLayer::highlight(std::function<bool(const CGObjectInstance *)> predicate)
  101. {
  102. if(!map)
  103. return;
  104. if(scene->level == 0 || map->twoLevel)
  105. {
  106. for(int j = 0; j < map->height; ++j)
  107. {
  108. for(int i = 0; i < map->width; ++i)
  109. {
  110. auto tl = map->getTile(int3(i, j, scene->level));
  111. ObjectInstanceID objID = tl.topVisitableObj();
  112. if(!objID.hasValue() && !tl.blockingObjects.empty())
  113. objID = tl.blockingObjects.front();
  114. if (objID.hasValue())
  115. {
  116. const CGObjectInstance * obj = map->getObject(objID);
  117. if(obj && predicate(obj))
  118. possibleObjects.insert(obj);
  119. }
  120. }
  121. }
  122. }
  123. isActive = true;
  124. }
  125. bool ObjectPickerLayer::isVisible() const
  126. {
  127. return isShown && isActive;
  128. }
  129. void ObjectPickerLayer::clear()
  130. {
  131. possibleObjects.clear();
  132. isActive = false;
  133. }
  134. void ObjectPickerLayer::update()
  135. {
  136. if(!map)
  137. return;
  138. pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
  139. pixmap->fill(Qt::transparent);
  140. if(isActive)
  141. pixmap->fill(QColor(255, 255, 255, 128));
  142. QPainter painter(pixmap.get());
  143. painter.setCompositionMode(QPainter::CompositionMode_Source);
  144. for(auto * obj : possibleObjects)
  145. {
  146. if(obj->pos.z != scene->level)
  147. continue;
  148. for(auto & pos : obj->getBlockedPos())
  149. painter.fillRect(pos.x * 32, pos.y * 32, 32, 32, QColor(255, 211, 0, 64));
  150. }
  151. painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
  152. redraw();
  153. }
  154. void ObjectPickerLayer::select(const CGObjectInstance * obj)
  155. {
  156. if(obj && possibleObjects.count(obj))
  157. {
  158. clear();
  159. selectionMade(obj);
  160. }
  161. }
  162. void ObjectPickerLayer::discard()
  163. {
  164. clear();
  165. selectionMade(nullptr);
  166. }
  167. SelectionTerrainLayer::SelectionTerrainLayer(MapSceneBase * s): AbstractLayer(s)
  168. {
  169. }
  170. void SelectionTerrainLayer::update()
  171. {
  172. if(!map)
  173. return;
  174. area.clear();
  175. areaAdd.clear();
  176. areaErase.clear();
  177. onSelection();
  178. pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
  179. pixmap->fill(Qt::transparent);
  180. redraw();
  181. }
  182. void SelectionTerrainLayer::draw()
  183. {
  184. if(!pixmap)
  185. return;
  186. QPainter painter(pixmap.get());
  187. painter.setCompositionMode(QPainter::CompositionMode_Source);
  188. for(auto & t : areaAdd)
  189. {
  190. painter.fillRect(t.x * 32, t.y * 32, 31, 31, QColor(128, 128, 128, 96));
  191. }
  192. for(auto & t : areaErase)
  193. {
  194. painter.fillRect(t.x * 32, t.y * 32, 31, 31, QColor(0, 0, 0, 0));
  195. }
  196. areaAdd.clear();
  197. areaErase.clear();
  198. redraw();
  199. }
  200. void SelectionTerrainLayer::select(const int3 & tile)
  201. {
  202. if(!map || !map->isInTheMap(tile))
  203. return;
  204. if(!area.count(tile))
  205. {
  206. area.insert(tile);
  207. areaAdd.insert(tile);
  208. areaErase.erase(tile);
  209. }
  210. onSelection();
  211. }
  212. void SelectionTerrainLayer::erase(const int3 & tile)
  213. {
  214. if(!map || !map->isInTheMap(tile))
  215. return;
  216. if(area.count(tile))
  217. {
  218. area.erase(tile);
  219. areaErase.insert(tile);
  220. areaAdd.erase(tile);
  221. }
  222. onSelection();
  223. }
  224. void SelectionTerrainLayer::clear()
  225. {
  226. areaErase = area;
  227. areaAdd.clear();
  228. area.clear();
  229. onSelection();
  230. }
  231. const std::set<int3> & SelectionTerrainLayer::selection() const
  232. {
  233. return area;
  234. }
  235. void SelectionTerrainLayer::onSelection()
  236. {
  237. selectionMade(!area.empty());
  238. }
  239. TerrainLayer::TerrainLayer(MapSceneBase * s): AbstractLayer(s)
  240. {
  241. }
  242. void TerrainLayer::update()
  243. {
  244. if(!map)
  245. return;
  246. pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
  247. draw(false);
  248. }
  249. void TerrainLayer::setDirty(const int3 & tile)
  250. {
  251. dirty.insert(tile);
  252. }
  253. void TerrainLayer::draw(bool onlyDirty)
  254. {
  255. if(!pixmap)
  256. return;
  257. if(!map)
  258. return;
  259. QPainter painter(pixmap.get());
  260. //painter.setCompositionMode(QPainter::CompositionMode_Source);
  261. if(onlyDirty)
  262. {
  263. std::set<int3> forRedrawing(dirty);
  264. std::set<int3> neighbours;
  265. for(auto & t : dirty)
  266. {
  267. for(auto & tt : int3::getDirs())
  268. {
  269. if(map->isInTheMap(t + tt))
  270. neighbours.insert(t + tt);
  271. }
  272. }
  273. for(auto & t : neighbours)
  274. {
  275. for(auto & tt : int3::getDirs())
  276. {
  277. forRedrawing.insert(t);
  278. if(map->isInTheMap(t + tt))
  279. forRedrawing.insert(t + tt);
  280. }
  281. }
  282. for(auto & t : forRedrawing)
  283. {
  284. handler->drawTerrainTile(painter, t.x, t.y, scene->level);
  285. handler->drawRiver(painter, t.x, t.y, scene->level);
  286. handler->drawRoad(painter, t.x, t.y, scene->level);
  287. }
  288. }
  289. else
  290. {
  291. for(int j = 0; j < map->height; ++j)
  292. {
  293. for(int i = 0; i < map->width; ++i)
  294. {
  295. handler->drawTerrainTile(painter, i, j, scene->level);
  296. handler->drawRiver(painter, i, j, scene->level);
  297. handler->drawRoad(painter, i, j, scene->level);
  298. }
  299. }
  300. }
  301. dirty.clear();
  302. redraw();
  303. }
  304. ObjectsLayer::ObjectsLayer(MapSceneBase * s): AbstractLayer(s)
  305. {
  306. }
  307. void ObjectsLayer::update()
  308. {
  309. if(!map)
  310. return;
  311. pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
  312. pixmap->fill(Qt::transparent);
  313. draw(false);
  314. }
  315. void ObjectsLayer::draw(bool onlyDirty)
  316. {
  317. if(!pixmap)
  318. return;
  319. if(!map)
  320. return;
  321. QPainter painter(pixmap.get());
  322. if(onlyDirty)
  323. {
  324. //objects could be modified
  325. for(auto * obj : objDirty)
  326. setDirty(obj);
  327. //clear tiles which will be redrawn. It's needed because some object could be replaced
  328. painter.setCompositionMode(QPainter::CompositionMode_Source);
  329. for(auto & p : dirty)
  330. painter.fillRect(p.x * 32, p.y * 32, 32, 32, Qt::transparent);
  331. painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
  332. for(auto & p : dirty)
  333. handler->drawObjects(painter, p.x, p.y, p.z, lockedObjects);
  334. }
  335. else
  336. {
  337. pixmap->fill(Qt::transparent);
  338. for(int j = 0; j < map->height; ++j)
  339. {
  340. for(int i = 0; i < map->width; ++i)
  341. {
  342. handler->drawObjects(painter, i, j, scene->level, lockedObjects);
  343. }
  344. }
  345. }
  346. dirty.clear();
  347. redraw();
  348. }
  349. void ObjectsLayer::setDirty(int x, int y)
  350. {
  351. int3 pos(x, y, scene->level);
  352. if(map->isInTheMap(pos))
  353. dirty.insert(pos);
  354. }
  355. void ObjectsLayer::setDirty(const CGObjectInstance * object)
  356. {
  357. objDirty.insert(object);
  358. //mark tiles under object as dirty
  359. for(int j = 0; j < object->getHeight(); ++j)
  360. {
  361. for(int i = 0; i < object->getWidth(); ++i)
  362. {
  363. setDirty(object->anchorPos().x - i, object->anchorPos().y - j);
  364. }
  365. }
  366. }
  367. void ObjectsLayer::setLockObject(const CGObjectInstance * object, bool lock)
  368. {
  369. if(lock)
  370. lockedObjects.insert(object);
  371. else
  372. lockedObjects.erase(object);
  373. }
  374. void ObjectsLayer::unlockAll()
  375. {
  376. lockedObjects.clear();
  377. }
  378. SelectionObjectsLayer::SelectionObjectsLayer(MapSceneBase * s): AbstractLayer(s), newObject(nullptr)
  379. {
  380. }
  381. void SelectionObjectsLayer::update()
  382. {
  383. if(!map)
  384. return;
  385. selectedObjects.clear();
  386. onSelection();
  387. shift = QPoint();
  388. newObject.reset();
  389. pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
  390. //pixmap->fill(QColor(0, 0, 0, 0));
  391. draw();
  392. }
  393. void SelectionObjectsLayer::draw()
  394. {
  395. if(!pixmap)
  396. return;
  397. pixmap->fill(Qt::transparent);
  398. QPainter painter(pixmap.get());
  399. painter.setCompositionMode(QPainter::CompositionMode_Source);
  400. painter.setPen(Qt::white);
  401. for(auto * obj : selectedObjects)
  402. {
  403. if(obj != newObject.get())
  404. {
  405. QRect bbox(obj->anchorPos().x, obj->anchorPos().y, 1, 1);
  406. for(auto & t : obj->getBlockedPos())
  407. {
  408. QPoint topLeft(std::min(t.x, bbox.topLeft().x()), std::min(t.y, bbox.topLeft().y()));
  409. bbox.setTopLeft(topLeft);
  410. QPoint bottomRight(std::max(t.x, bbox.bottomRight().x()), std::max(t.y, bbox.bottomRight().y()));
  411. bbox.setBottomRight(bottomRight);
  412. }
  413. painter.setOpacity(1.0);
  414. painter.drawRect(bbox.x() * 32, bbox.y() * 32, bbox.width() * 32, bbox.height() * 32);
  415. }
  416. //show translation
  417. if(selectionMode == SelectionMode::MOVEMENT && (shift.x() || shift.y()))
  418. {
  419. painter.setOpacity(0.7);
  420. auto newPos = QPoint(obj->anchorPos().x, obj->anchorPos().y) + shift;
  421. handler->drawObjectAt(painter, obj, newPos.x(), newPos.y());
  422. }
  423. }
  424. redraw();
  425. }
  426. CGObjectInstance * SelectionObjectsLayer::selectObjectAt(int x, int y, const CGObjectInstance * ignore) const
  427. {
  428. if(!map || !map->isInTheMap(int3(x, y, scene->level)))
  429. return nullptr;
  430. auto & objects = handler->getObjects(x, y, scene->level);
  431. //visitable is most important
  432. for(auto & object : objects)
  433. {
  434. if(!object.obj || object.obj == ignore || lockedObjects.count(object.obj))
  435. continue;
  436. if(object.obj->visitableAt(int3(x, y, scene->level)))
  437. {
  438. return const_cast<CGObjectInstance*>(object.obj);
  439. }
  440. }
  441. //if not visitable tile - try to get blocked
  442. for(auto & object : objects)
  443. {
  444. if(!object.obj || object.obj == ignore || lockedObjects.count(object.obj))
  445. continue;
  446. if(object.obj->blockingAt(int3(x, y, scene->level)))
  447. {
  448. return const_cast<CGObjectInstance*>(object.obj);
  449. }
  450. }
  451. //finally, we can take any object
  452. for(auto & object : objects)
  453. {
  454. if(!object.obj || object.obj == ignore || lockedObjects.count(object.obj))
  455. continue;
  456. if(object.obj->coveringAt(int3(x, y, scene->level)))
  457. {
  458. return const_cast<CGObjectInstance*>(object.obj);
  459. }
  460. }
  461. return nullptr;
  462. }
  463. void SelectionObjectsLayer::selectObjects(int x1, int y1, int x2, int y2)
  464. {
  465. if(!map)
  466. return;
  467. if(x1 > x2)
  468. std::swap(x1, x2);
  469. if(y1 > y2)
  470. std::swap(y1, y2);
  471. for(int j = y1; j < y2; ++j)
  472. {
  473. for(int i = x1; i < x2; ++i)
  474. {
  475. if(map->isInTheMap(int3(i, j, scene->level)))
  476. {
  477. for(auto & o : handler->getObjects(i, j, scene->level))
  478. if(!lockedObjects.count(o.obj))
  479. selectObject(const_cast<CGObjectInstance*>(o.obj), false); //do not inform about each object added
  480. }
  481. }
  482. }
  483. onSelection();
  484. }
  485. void SelectionObjectsLayer::selectObject(CGObjectInstance * obj, bool inform /* = true */)
  486. {
  487. selectedObjects.insert(obj);
  488. if (inform)
  489. {
  490. onSelection();
  491. }
  492. }
  493. void SelectionObjectsLayer::deselectObject(CGObjectInstance * obj)
  494. {
  495. selectedObjects.erase(obj);
  496. }
  497. bool SelectionObjectsLayer::isSelected(const CGObjectInstance * obj) const
  498. {
  499. return selectedObjects.count(const_cast<CGObjectInstance*>(obj));
  500. }
  501. std::set<CGObjectInstance*> SelectionObjectsLayer::getSelection() const
  502. {
  503. return selectedObjects;
  504. }
  505. void SelectionObjectsLayer::clear()
  506. {
  507. selectedObjects.clear();
  508. onSelection();
  509. shift.setX(0);
  510. shift.setY(0);
  511. }
  512. void SelectionObjectsLayer::onSelection()
  513. {
  514. selectionMade(!selectedObjects.empty());
  515. }
  516. void SelectionObjectsLayer::setLockObject(const CGObjectInstance * object, bool lock)
  517. {
  518. if(lock)
  519. lockedObjects.insert(object);
  520. else
  521. lockedObjects.erase(object);
  522. }
  523. void SelectionObjectsLayer::unlockAll()
  524. {
  525. lockedObjects.clear();
  526. }
  527. MinimapLayer::MinimapLayer(MapSceneBase * s): AbstractLayer(s)
  528. {
  529. }
  530. void MinimapLayer::update()
  531. {
  532. if(!map)
  533. return;
  534. pixmap.reset(new QPixmap(map->width, map->height));
  535. QPainter painter(pixmap.get());
  536. //coordinate transformation
  537. for(int j = 0; j < map->height; ++j)
  538. {
  539. for(int i = 0; i < map->width; ++i)
  540. {
  541. handler->drawMinimapTile(painter, i, j, scene->level);
  542. }
  543. }
  544. redraw();
  545. }
  546. MinimapViewLayer::MinimapViewLayer(MapSceneBase * s): AbstractLayer(s)
  547. {
  548. }
  549. void MinimapViewLayer::update()
  550. {
  551. if(!map)
  552. return;
  553. pixmap.reset(new QPixmap(map->width, map->height));
  554. draw();
  555. }
  556. void MinimapViewLayer::draw()
  557. {
  558. if(!map)
  559. return;
  560. pixmap->fill(Qt::transparent);
  561. //maybe not optimal but ok
  562. QPainter painter(pixmap.get());
  563. painter.setPen(Qt::white);
  564. painter.drawRect(x, y, w, h);
  565. redraw();
  566. }
  567. void MinimapViewLayer::setViewport(int _x, int _y, int _w, int _h)
  568. {
  569. x = _x;
  570. y = _y;
  571. w = _w;
  572. h = _h;
  573. draw();
  574. }