scenelayer.cpp 13 KB

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