scenelayer.cpp 13 KB

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