scenelayer.cpp 11 KB

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