scenelayer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. pixmap->fill(Qt::transparent);
  250. QPainter painter(pixmap.get());
  251. std::set<const CGObjectInstance *> drawen;
  252. for(int j = 0; j < map->height; ++j)
  253. {
  254. for(int i = 0; i < map->width; ++i)
  255. {
  256. handler->drawObjects(painter, i, j, scene->level);
  257. /*auto & objects = main->getMapHandler()->getObjects(i, j, scene->level);
  258. for(auto & object : objects)
  259. {
  260. if(!object.obj || drawen.count(object.obj))
  261. continue;
  262. if(!onlyDirty || dirty.count(object.obj))
  263. {
  264. main->getMapHandler()->drawObject(painter, object);
  265. drawen.insert(object.obj);
  266. }
  267. }*/
  268. }
  269. }
  270. dirty.clear();
  271. redraw();
  272. }
  273. void ObjectsLayer::setDirty(int x, int y)
  274. {
  275. /*auto & objects = main->getMapHandler()->getObjects(x, y, scene->level);
  276. for(auto & object : objects)
  277. {
  278. if(object.obj)
  279. dirty.insert(object.obj);
  280. }*/
  281. }
  282. void ObjectsLayer::setDirty(const CGObjectInstance * object)
  283. {
  284. }
  285. SelectionObjectsLayer::SelectionObjectsLayer(MapSceneBase * s): AbstractLayer(s), newObject(nullptr)
  286. {
  287. }
  288. void SelectionObjectsLayer::update()
  289. {
  290. if(!map)
  291. return;
  292. selectedObjects.clear();
  293. onSelection();
  294. shift = QPoint();
  295. delete newObject;
  296. newObject = nullptr;
  297. pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
  298. //pixmap->fill(QColor(0, 0, 0, 0));
  299. draw();
  300. }
  301. void SelectionObjectsLayer::draw()
  302. {
  303. if(!pixmap)
  304. return;
  305. pixmap->fill(Qt::transparent);
  306. QPainter painter(pixmap.get());
  307. painter.setCompositionMode(QPainter::CompositionMode_Source);
  308. painter.setPen(Qt::white);
  309. for(auto * obj : selectedObjects)
  310. {
  311. if(obj != newObject)
  312. {
  313. QRect bbox(obj->getPosition().x, obj->getPosition().y, 1, 1);
  314. for(auto & t : obj->getBlockedPos())
  315. {
  316. QPoint topLeft(std::min(t.x, bbox.topLeft().x()), std::min(t.y, bbox.topLeft().y()));
  317. bbox.setTopLeft(topLeft);
  318. QPoint bottomRight(std::max(t.x, bbox.bottomRight().x()), std::max(t.y, bbox.bottomRight().y()));
  319. bbox.setBottomRight(bottomRight);
  320. }
  321. painter.setOpacity(1.0);
  322. painter.drawRect(bbox.x() * 32, bbox.y() * 32, bbox.width() * 32, bbox.height() * 32);
  323. }
  324. //show translation
  325. if(selectionMode == SelectionMode::MOVEMENT && (shift.x() || shift.y()))
  326. {
  327. painter.setOpacity(0.7);
  328. auto newPos = QPoint(obj->getPosition().x, obj->getPosition().y) + shift;
  329. handler->drawObjectAt(painter, obj, newPos.x(), newPos.y());
  330. }
  331. }
  332. redraw();
  333. }
  334. CGObjectInstance * SelectionObjectsLayer::selectObjectAt(int x, int y) const
  335. {
  336. if(!map || !map->isInTheMap(int3(x, y, scene->level)))
  337. return nullptr;
  338. auto & objects = handler->getObjects(x, y, scene->level);
  339. //visitable is most important
  340. for(auto & object : objects)
  341. {
  342. if(!object.obj)
  343. continue;
  344. if(object.obj->visitableAt(x, y))
  345. {
  346. return object.obj;
  347. }
  348. }
  349. //if not visitable tile - try to get blocked
  350. for(auto & object : objects)
  351. {
  352. if(!object.obj)
  353. continue;
  354. if(object.obj->blockingAt(x, y))
  355. {
  356. return object.obj;
  357. }
  358. }
  359. //finally, we can take any object
  360. for(auto & object : objects)
  361. {
  362. if(!object.obj)
  363. continue;
  364. if(object.obj->coveringAt(x, y))
  365. {
  366. return object.obj;
  367. }
  368. }
  369. return nullptr;
  370. }
  371. void SelectionObjectsLayer::selectObjects(int x1, int y1, int x2, int y2)
  372. {
  373. if(!map)
  374. return;
  375. if(x1 > x2)
  376. std::swap(x1, x2);
  377. if(y1 > y2)
  378. std::swap(y1, y2);
  379. for(int j = y1; j < y2; ++j)
  380. {
  381. for(int i = x1; i < x2; ++i)
  382. {
  383. for(auto & o : handler->getObjects(i, j, scene->level))
  384. selectObject(o.obj, false); //do not inform about each object added
  385. }
  386. }
  387. onSelection();
  388. }
  389. void SelectionObjectsLayer::selectObject(CGObjectInstance * obj, bool inform /* = true */)
  390. {
  391. selectedObjects.insert(obj);
  392. if (inform)
  393. {
  394. onSelection();
  395. }
  396. }
  397. void SelectionObjectsLayer::deselectObject(CGObjectInstance * obj)
  398. {
  399. selectedObjects.erase(obj);
  400. }
  401. bool SelectionObjectsLayer::isSelected(const CGObjectInstance * obj) const
  402. {
  403. return selectedObjects.count(const_cast<CGObjectInstance*>(obj));
  404. }
  405. std::set<CGObjectInstance*> SelectionObjectsLayer::getSelection() const
  406. {
  407. return selectedObjects;
  408. }
  409. void SelectionObjectsLayer::clear()
  410. {
  411. selectedObjects.clear();
  412. onSelection();
  413. shift.setX(0);
  414. shift.setY(0);
  415. }
  416. void SelectionObjectsLayer::onSelection()
  417. {
  418. emit selectionMade(!selectedObjects.empty());
  419. }
  420. MinimapLayer::MinimapLayer(MapSceneBase * s): AbstractLayer(s)
  421. {
  422. }
  423. void MinimapLayer::update()
  424. {
  425. if(!map)
  426. return;
  427. pixmap.reset(new QPixmap(map->width, map->height));
  428. QPainter painter(pixmap.get());
  429. //coordinate transfomation
  430. for(int j = 0; j < map->height; ++j)
  431. {
  432. for(int i = 0; i < map->width; ++i)
  433. {
  434. handler->drawMinimapTile(painter, i, j, scene->level);
  435. }
  436. }
  437. redraw();
  438. }
  439. MinimapViewLayer::MinimapViewLayer(MapSceneBase * s): AbstractLayer(s)
  440. {
  441. }
  442. void MinimapViewLayer::update()
  443. {
  444. if(!map)
  445. return;
  446. pixmap.reset(new QPixmap(map->width, map->height));
  447. draw();
  448. }
  449. void MinimapViewLayer::draw()
  450. {
  451. if(!map)
  452. return;
  453. pixmap->fill(Qt::transparent);
  454. //maybe not optimal but ok
  455. QPainter painter(pixmap.get());
  456. painter.setPen(Qt::white);
  457. painter.drawRect(x, y, w, h);
  458. redraw();
  459. }
  460. void MinimapViewLayer::setViewport(int _x, int _y, int _w, int _h)
  461. {
  462. x = _x;
  463. y = _y;
  464. w = _w;
  465. h = _h;
  466. draw();
  467. }