scenelayer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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. emit selectionMade(obj);
  156. }
  157. }
  158. void ObjectPickerLayer::discard()
  159. {
  160. clear();
  161. emit 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. emit 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), neighbours;
  260. for(auto & t : dirty)
  261. {
  262. for(auto & tt : int3::getDirs())
  263. {
  264. if(map->isInTheMap(t + tt))
  265. neighbours.insert(t + tt);
  266. }
  267. }
  268. for(auto & t : neighbours)
  269. {
  270. for(auto & tt : int3::getDirs())
  271. {
  272. forRedrawing.insert(t);
  273. if(map->isInTheMap(t + tt))
  274. forRedrawing.insert(t + tt);
  275. }
  276. }
  277. for(auto & t : forRedrawing)
  278. {
  279. handler->drawTerrainTile(painter, t.x, t.y, scene->level);
  280. handler->drawRiver(painter, t.x, t.y, scene->level);
  281. handler->drawRoad(painter, t.x, t.y, scene->level);
  282. }
  283. }
  284. else
  285. {
  286. for(int j = 0; j < map->height; ++j)
  287. {
  288. for(int i = 0; i < map->width; ++i)
  289. {
  290. handler->drawTerrainTile(painter, i, j, scene->level);
  291. handler->drawRiver(painter, i, j, scene->level);
  292. handler->drawRoad(painter, i, j, scene->level);
  293. }
  294. }
  295. }
  296. dirty.clear();
  297. redraw();
  298. }
  299. ObjectsLayer::ObjectsLayer(MapSceneBase * s): AbstractLayer(s)
  300. {
  301. }
  302. void ObjectsLayer::update()
  303. {
  304. if(!map)
  305. return;
  306. pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
  307. pixmap->fill(Qt::transparent);
  308. draw(false);
  309. }
  310. void ObjectsLayer::draw(bool onlyDirty)
  311. {
  312. if(!pixmap)
  313. return;
  314. if(!map)
  315. return;
  316. QPainter painter(pixmap.get());
  317. if(onlyDirty)
  318. {
  319. //objects could be modified
  320. for(auto * obj : objDirty)
  321. setDirty(obj);
  322. //clear tiles which will be redrawn. It's needed because some object could be replaced
  323. painter.setCompositionMode(QPainter::CompositionMode_Source);
  324. for(auto & p : dirty)
  325. painter.fillRect(p.x * 32, p.y * 32, 32, 32, Qt::transparent);
  326. painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
  327. for(auto & p : dirty)
  328. handler->drawObjects(painter, p.x, p.y, p.z, lockedObjects);
  329. }
  330. else
  331. {
  332. pixmap->fill(Qt::transparent);
  333. for(int j = 0; j < map->height; ++j)
  334. {
  335. for(int i = 0; i < map->width; ++i)
  336. {
  337. handler->drawObjects(painter, i, j, scene->level, lockedObjects);
  338. }
  339. }
  340. }
  341. dirty.clear();
  342. redraw();
  343. }
  344. void ObjectsLayer::setDirty(int x, int y)
  345. {
  346. int3 pos(x, y, scene->level);
  347. if(map->isInTheMap(pos))
  348. dirty.insert(pos);
  349. }
  350. void ObjectsLayer::setDirty(const CGObjectInstance * object)
  351. {
  352. objDirty.insert(object);
  353. //mark tiles under object as dirty
  354. for(int j = 0; j < object->getHeight(); ++j)
  355. {
  356. for(int i = 0; i < object->getWidth(); ++i)
  357. {
  358. setDirty(object->getPosition().x - i, object->getPosition().y - j);
  359. }
  360. }
  361. }
  362. void ObjectsLayer::setLockObject(const CGObjectInstance * object, bool lock)
  363. {
  364. if(lock)
  365. lockedObjects.insert(object);
  366. else
  367. lockedObjects.erase(object);
  368. }
  369. void ObjectsLayer::unlockAll()
  370. {
  371. lockedObjects.clear();
  372. }
  373. SelectionObjectsLayer::SelectionObjectsLayer(MapSceneBase * s): AbstractLayer(s), newObject(nullptr)
  374. {
  375. }
  376. void SelectionObjectsLayer::update()
  377. {
  378. if(!map)
  379. return;
  380. selectedObjects.clear();
  381. onSelection();
  382. shift = QPoint();
  383. delete newObject;
  384. newObject = nullptr;
  385. pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
  386. //pixmap->fill(QColor(0, 0, 0, 0));
  387. draw();
  388. }
  389. void SelectionObjectsLayer::draw()
  390. {
  391. if(!pixmap)
  392. return;
  393. pixmap->fill(Qt::transparent);
  394. QPainter painter(pixmap.get());
  395. painter.setCompositionMode(QPainter::CompositionMode_Source);
  396. painter.setPen(Qt::white);
  397. for(auto * obj : selectedObjects)
  398. {
  399. if(obj != newObject)
  400. {
  401. QRect bbox(obj->getPosition().x, obj->getPosition().y, 1, 1);
  402. for(auto & t : obj->getBlockedPos())
  403. {
  404. QPoint topLeft(std::min(t.x, bbox.topLeft().x()), std::min(t.y, bbox.topLeft().y()));
  405. bbox.setTopLeft(topLeft);
  406. QPoint bottomRight(std::max(t.x, bbox.bottomRight().x()), std::max(t.y, bbox.bottomRight().y()));
  407. bbox.setBottomRight(bottomRight);
  408. }
  409. painter.setOpacity(1.0);
  410. painter.drawRect(bbox.x() * 32, bbox.y() * 32, bbox.width() * 32, bbox.height() * 32);
  411. }
  412. //show translation
  413. if(selectionMode == SelectionMode::MOVEMENT && (shift.x() || shift.y()))
  414. {
  415. painter.setOpacity(0.7);
  416. auto newPos = QPoint(obj->getPosition().x, obj->getPosition().y) + shift;
  417. handler->drawObjectAt(painter, obj, newPos.x(), newPos.y());
  418. }
  419. }
  420. redraw();
  421. }
  422. CGObjectInstance * SelectionObjectsLayer::selectObjectAt(int x, int y, const CGObjectInstance * ignore) const
  423. {
  424. if(!map || !map->isInTheMap(int3(x, y, scene->level)))
  425. return nullptr;
  426. auto & objects = handler->getObjects(x, y, scene->level);
  427. //visitable is most important
  428. for(auto & object : objects)
  429. {
  430. if(!object.obj || object.obj == ignore || lockedObjects.count(object.obj))
  431. continue;
  432. if(object.obj->visitableAt(x, y))
  433. {
  434. return const_cast<CGObjectInstance*>(object.obj);
  435. }
  436. }
  437. //if not visitable tile - try to get blocked
  438. for(auto & object : objects)
  439. {
  440. if(!object.obj || object.obj == ignore || lockedObjects.count(object.obj))
  441. continue;
  442. if(object.obj->blockingAt(x, y))
  443. {
  444. return const_cast<CGObjectInstance*>(object.obj);
  445. }
  446. }
  447. //finally, we can take any object
  448. for(auto & object : objects)
  449. {
  450. if(!object.obj || object.obj == ignore || lockedObjects.count(object.obj))
  451. continue;
  452. if(object.obj->coveringAt(x, y))
  453. {
  454. return const_cast<CGObjectInstance*>(object.obj);
  455. }
  456. }
  457. return nullptr;
  458. }
  459. void SelectionObjectsLayer::selectObjects(int x1, int y1, int x2, int y2)
  460. {
  461. if(!map)
  462. return;
  463. if(x1 > x2)
  464. std::swap(x1, x2);
  465. if(y1 > y2)
  466. std::swap(y1, y2);
  467. for(int j = y1; j < y2; ++j)
  468. {
  469. for(int i = x1; i < x2; ++i)
  470. {
  471. if(map->isInTheMap(int3(i, j, scene->level)))
  472. {
  473. for(auto & o : handler->getObjects(i, j, scene->level))
  474. if(!lockedObjects.count(o.obj))
  475. selectObject(const_cast<CGObjectInstance*>(o.obj), false); //do not inform about each object added
  476. }
  477. }
  478. }
  479. onSelection();
  480. }
  481. void SelectionObjectsLayer::selectObject(CGObjectInstance * obj, bool inform /* = true */)
  482. {
  483. selectedObjects.insert(obj);
  484. if (inform)
  485. {
  486. onSelection();
  487. }
  488. }
  489. void SelectionObjectsLayer::deselectObject(CGObjectInstance * obj)
  490. {
  491. selectedObjects.erase(obj);
  492. }
  493. bool SelectionObjectsLayer::isSelected(const CGObjectInstance * obj) const
  494. {
  495. return selectedObjects.count(const_cast<CGObjectInstance*>(obj));
  496. }
  497. std::set<CGObjectInstance*> SelectionObjectsLayer::getSelection() const
  498. {
  499. return selectedObjects;
  500. }
  501. void SelectionObjectsLayer::clear()
  502. {
  503. selectedObjects.clear();
  504. onSelection();
  505. shift.setX(0);
  506. shift.setY(0);
  507. }
  508. void SelectionObjectsLayer::onSelection()
  509. {
  510. emit selectionMade(!selectedObjects.empty());
  511. }
  512. void SelectionObjectsLayer::setLockObject(const CGObjectInstance * object, bool lock)
  513. {
  514. if(lock)
  515. lockedObjects.insert(object);
  516. else
  517. lockedObjects.erase(object);
  518. }
  519. void SelectionObjectsLayer::unlockAll()
  520. {
  521. lockedObjects.clear();
  522. }
  523. MinimapLayer::MinimapLayer(MapSceneBase * s): AbstractLayer(s)
  524. {
  525. }
  526. void MinimapLayer::update()
  527. {
  528. if(!map)
  529. return;
  530. pixmap.reset(new QPixmap(map->width, map->height));
  531. QPainter painter(pixmap.get());
  532. //coordinate transfomation
  533. for(int j = 0; j < map->height; ++j)
  534. {
  535. for(int i = 0; i < map->width; ++i)
  536. {
  537. handler->drawMinimapTile(painter, i, j, scene->level);
  538. }
  539. }
  540. redraw();
  541. }
  542. MinimapViewLayer::MinimapViewLayer(MapSceneBase * s): AbstractLayer(s)
  543. {
  544. }
  545. void MinimapViewLayer::update()
  546. {
  547. if(!map)
  548. return;
  549. pixmap.reset(new QPixmap(map->width, map->height));
  550. draw();
  551. }
  552. void MinimapViewLayer::draw()
  553. {
  554. if(!map)
  555. return;
  556. pixmap->fill(Qt::transparent);
  557. //maybe not optimal but ok
  558. QPainter painter(pixmap.get());
  559. painter.setPen(Qt::white);
  560. painter.drawRect(x, y, w, h);
  561. redraw();
  562. }
  563. void MinimapViewLayer::setViewport(int _x, int _y, int _w, int _h)
  564. {
  565. x = _x;
  566. y = _y;
  567. w = _w;
  568. h = _h;
  569. draw();
  570. }