scenelayer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. std::set<const CGObjectInstance *> drawen;
  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);
  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);
  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->getPosition().x - i, object->getPosition().y - j);
  360. }
  361. }
  362. }
  363. SelectionObjectsLayer::SelectionObjectsLayer(MapSceneBase * s): AbstractLayer(s), newObject(nullptr)
  364. {
  365. }
  366. void SelectionObjectsLayer::update()
  367. {
  368. if(!map)
  369. return;
  370. selectedObjects.clear();
  371. onSelection();
  372. shift = QPoint();
  373. delete newObject;
  374. newObject = nullptr;
  375. pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
  376. //pixmap->fill(QColor(0, 0, 0, 0));
  377. draw();
  378. }
  379. void SelectionObjectsLayer::draw()
  380. {
  381. if(!pixmap)
  382. return;
  383. pixmap->fill(Qt::transparent);
  384. QPainter painter(pixmap.get());
  385. painter.setCompositionMode(QPainter::CompositionMode_Source);
  386. painter.setPen(Qt::white);
  387. for(auto * obj : selectedObjects)
  388. {
  389. if(obj != newObject)
  390. {
  391. QRect bbox(obj->getPosition().x, obj->getPosition().y, 1, 1);
  392. for(auto & t : obj->getBlockedPos())
  393. {
  394. QPoint topLeft(std::min(t.x, bbox.topLeft().x()), std::min(t.y, bbox.topLeft().y()));
  395. bbox.setTopLeft(topLeft);
  396. QPoint bottomRight(std::max(t.x, bbox.bottomRight().x()), std::max(t.y, bbox.bottomRight().y()));
  397. bbox.setBottomRight(bottomRight);
  398. }
  399. painter.setOpacity(1.0);
  400. painter.drawRect(bbox.x() * 32, bbox.y() * 32, bbox.width() * 32, bbox.height() * 32);
  401. }
  402. //show translation
  403. if(selectionMode == SelectionMode::MOVEMENT && (shift.x() || shift.y()))
  404. {
  405. painter.setOpacity(0.7);
  406. auto newPos = QPoint(obj->getPosition().x, obj->getPosition().y) + shift;
  407. handler->drawObjectAt(painter, obj, newPos.x(), newPos.y());
  408. }
  409. }
  410. redraw();
  411. }
  412. CGObjectInstance * SelectionObjectsLayer::selectObjectAt(int x, int y, const CGObjectInstance * ignore) const
  413. {
  414. if(!map || !map->isInTheMap(int3(x, y, scene->level)))
  415. return nullptr;
  416. auto & objects = handler->getObjects(x, y, scene->level);
  417. //visitable is most important
  418. for(auto & object : objects)
  419. {
  420. if(!object.obj || object.obj == ignore)
  421. continue;
  422. if(object.obj->visitableAt(x, y))
  423. {
  424. return object.obj;
  425. }
  426. }
  427. //if not visitable tile - try to get blocked
  428. for(auto & object : objects)
  429. {
  430. if(!object.obj || object.obj == ignore)
  431. continue;
  432. if(object.obj->blockingAt(x, y))
  433. {
  434. return object.obj;
  435. }
  436. }
  437. //finally, we can take any object
  438. for(auto & object : objects)
  439. {
  440. if(!object.obj || object.obj == ignore)
  441. continue;
  442. if(object.obj->coveringAt(x, y))
  443. {
  444. return object.obj;
  445. }
  446. }
  447. return nullptr;
  448. }
  449. void SelectionObjectsLayer::selectObjects(int x1, int y1, int x2, int y2)
  450. {
  451. if(!map)
  452. return;
  453. if(x1 > x2)
  454. std::swap(x1, x2);
  455. if(y1 > y2)
  456. std::swap(y1, y2);
  457. for(int j = y1; j < y2; ++j)
  458. {
  459. for(int i = x1; i < x2; ++i)
  460. {
  461. if(map->isInTheMap(int3(i, j, scene->level)))
  462. {
  463. for(auto & o : handler->getObjects(i, j, scene->level))
  464. selectObject(o.obj, false); //do not inform about each object added
  465. }
  466. }
  467. }
  468. onSelection();
  469. }
  470. void SelectionObjectsLayer::selectObject(CGObjectInstance * obj, bool inform /* = true */)
  471. {
  472. selectedObjects.insert(obj);
  473. if (inform)
  474. {
  475. onSelection();
  476. }
  477. }
  478. void SelectionObjectsLayer::deselectObject(CGObjectInstance * obj)
  479. {
  480. selectedObjects.erase(obj);
  481. }
  482. bool SelectionObjectsLayer::isSelected(const CGObjectInstance * obj) const
  483. {
  484. return selectedObjects.count(const_cast<CGObjectInstance*>(obj));
  485. }
  486. std::set<CGObjectInstance*> SelectionObjectsLayer::getSelection() const
  487. {
  488. return selectedObjects;
  489. }
  490. void SelectionObjectsLayer::clear()
  491. {
  492. selectedObjects.clear();
  493. onSelection();
  494. shift.setX(0);
  495. shift.setY(0);
  496. }
  497. void SelectionObjectsLayer::onSelection()
  498. {
  499. emit selectionMade(!selectedObjects.empty());
  500. }
  501. MinimapLayer::MinimapLayer(MapSceneBase * s): AbstractLayer(s)
  502. {
  503. }
  504. void MinimapLayer::update()
  505. {
  506. if(!map)
  507. return;
  508. pixmap.reset(new QPixmap(map->width, map->height));
  509. QPainter painter(pixmap.get());
  510. //coordinate transfomation
  511. for(int j = 0; j < map->height; ++j)
  512. {
  513. for(int i = 0; i < map->width; ++i)
  514. {
  515. handler->drawMinimapTile(painter, i, j, scene->level);
  516. }
  517. }
  518. redraw();
  519. }
  520. MinimapViewLayer::MinimapViewLayer(MapSceneBase * s): AbstractLayer(s)
  521. {
  522. }
  523. void MinimapViewLayer::update()
  524. {
  525. if(!map)
  526. return;
  527. pixmap.reset(new QPixmap(map->width, map->height));
  528. draw();
  529. }
  530. void MinimapViewLayer::draw()
  531. {
  532. if(!map)
  533. return;
  534. pixmap->fill(Qt::transparent);
  535. //maybe not optimal but ok
  536. QPainter painter(pixmap.get());
  537. painter.setPen(Qt::white);
  538. painter.drawRect(x, y, w, h);
  539. redraw();
  540. }
  541. void MinimapViewLayer::setViewport(int _x, int _y, int _w, int _h)
  542. {
  543. x = _x;
  544. y = _y;
  545. w = _w;
  546. h = _h;
  547. draw();
  548. }