scenelayer.cpp 11 KB

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