scenelayer.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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. isShown = show;
  29. redraw();
  30. }
  31. int AbstractLayer::mapWidthPx() const
  32. {
  33. return map ? map->width * tileSize : 0;
  34. }
  35. int AbstractLayer::mapHeightPx() const
  36. {
  37. return map ? map->height * tileSize : 0;
  38. }
  39. int AbstractLayer::toInt(double value) const
  40. {
  41. return static_cast<int>(std::round(value)); // is rounded explicitly in order to avoid rounding down unprecise double values
  42. }
  43. AbstractFixedLayer::AbstractFixedLayer(MapSceneBase * s): AbstractLayer(s)
  44. {
  45. }
  46. void AbstractFixedLayer::redraw()
  47. {
  48. if(item)
  49. {
  50. if(pixmap && isShown)
  51. item->setPixmap(*pixmap);
  52. else
  53. item->setPixmap(emptyPixmap);
  54. }
  55. else
  56. {
  57. if(pixmap && isShown)
  58. item.reset(scene->addPixmap(*pixmap));
  59. else
  60. item.reset(scene->addPixmap(emptyPixmap));
  61. }
  62. }
  63. AbstractViewportLayer::AbstractViewportLayer(MapSceneBase * s): AbstractLayer(s)
  64. {
  65. }
  66. void AbstractViewportLayer::createLayer()
  67. {
  68. QList<QGraphicsItem *>emptyList;
  69. items.reset(scene->createItemGroup(emptyList));
  70. }
  71. void AbstractViewportLayer::setViewport(const QRectF & viewPort)
  72. {
  73. if(!map)
  74. return;
  75. if (items->boundingRect().contains(viewPort))
  76. return;
  77. std::vector<QGraphicsItem *> outOfScreenSectors;
  78. for (QGraphicsItem * sector : getAllSectors())
  79. {
  80. if (!viewPort.intersects(sector->sceneBoundingRect()))
  81. outOfScreenSectors.push_back(sector);
  82. }
  83. for (QGraphicsItem * sector : outOfScreenSectors)
  84. {
  85. removeSector(sector);
  86. }
  87. std::vector<QRectF> newAreas;
  88. int left = toInt(viewPort.left());
  89. int right = toInt(viewPort.right());
  90. int top = toInt(viewPort.top());
  91. int bottom = toInt(viewPort.bottom());
  92. int startX = left - (left % sectorSize);
  93. int limitX = std::min(right + (sectorSize - right % sectorSize), mapWidthPx());
  94. int startY = top - (top % sectorSize);
  95. int limitY = std::min(bottom + (sectorSize - bottom % sectorSize), mapHeightPx());
  96. for (int x = startX; x < limitX; x += sectorSize)
  97. {
  98. for (int y = startY; y < limitY; y += sectorSize)
  99. {
  100. int width = x + sectorSize < limitX ? sectorSize : limitX - x;
  101. int height = y + sectorSize < limitY ? sectorSize : limitY - y;
  102. QRectF area(x, y, width, height);
  103. if (!items->boundingRect().intersects(area))
  104. newAreas.emplace_back(area);
  105. }
  106. }
  107. for(QRectF newSection : newAreas)
  108. {
  109. QGraphicsItem * sector = draw(newSection);
  110. if (sector)
  111. addSector(sector);
  112. }
  113. }
  114. void AbstractViewportLayer::update()
  115. {
  116. redraw();
  117. }
  118. void AbstractViewportLayer::redraw()
  119. {
  120. std::set<QGraphicsItem *> allSectors;
  121. for (auto * sector : getAllSectors())
  122. allSectors.insert(sector);
  123. redrawSectors(allSectors);
  124. }
  125. void AbstractViewportLayer::redraw(const std::vector<int3> & tiles)
  126. {
  127. std::set<QGraphicsItem *> sectorsToRedraw = getContainingSectors(tiles);
  128. redrawSectors(sectorsToRedraw);
  129. }
  130. void AbstractViewportLayer::redrawWithSurroundingTiles(const std::vector<int3> & tiles)
  131. {
  132. int maxX = 0;
  133. int maxY = 0;
  134. int minX = INT_MAX;
  135. int minY = INT_MAX;
  136. for (const int3 tile : tiles)
  137. {
  138. maxX = std::max(tile.x, maxX);
  139. maxY = std::max(tile.y, maxY);
  140. minX = std::min(tile.x, minX);
  141. minY = std::min(tile.y, minY);
  142. }
  143. QRectF bounds((minX - 2) * tileSize, (minY - 2) * tileSize, (maxX - minX + 4) * tileSize, (maxY - minY + 4) * tileSize); //tiles start with 1, QRectF from 0
  144. redraw({bounds});
  145. }
  146. void AbstractViewportLayer::redraw(const std::set<CGObjectInstance *> & objects)
  147. {
  148. std::vector<QRectF> areas(objects.size());
  149. for (const CGObjectInstance * object : objects)
  150. {
  151. areas.push_back(getObjectArea(object));
  152. }
  153. redraw(areas);
  154. }
  155. void AbstractViewportLayer::redraw(const std::vector<QRectF> & areas)
  156. {
  157. std::set<QGraphicsItem *> intersectingSectors;
  158. for (QGraphicsItem * existingSector : getAllSectors())
  159. {
  160. for (auto area : areas)
  161. {
  162. if (existingSector->sceneBoundingRect().intersects(area))
  163. {
  164. intersectingSectors.insert(existingSector);
  165. }
  166. }
  167. }
  168. redrawSectors(intersectingSectors);
  169. }
  170. QRectF AbstractViewportLayer::getObjectArea(const CGObjectInstance * object) const
  171. {
  172. auto pos = object->pos;
  173. int x = ((pos.x + 1) * tileSize) - (object->getWidth() * tileSize); //Qt set 0,0 point on the top right corner, CGObjectInstance on the bottom left
  174. int y = ((pos.y + 1) * tileSize) - (object->getHeight() * tileSize);
  175. QRectF objectArea(x, y, object->getWidth() * tileSize, object->getHeight() * tileSize);
  176. return objectArea;
  177. }
  178. void AbstractViewportLayer::addSector(QGraphicsItem * sector)
  179. {
  180. items->addToGroup(sector);
  181. }
  182. void AbstractViewportLayer::removeSector(QGraphicsItem * sector)
  183. {
  184. items->removeFromGroup(sector);
  185. delete sector;
  186. }
  187. void AbstractViewportLayer::redrawSectors(std::set<QGraphicsItem *> & sectors)
  188. {
  189. std::set<QGraphicsItem *> sectorsToRemove;
  190. for (QGraphicsItem * existingSectors : getAllSectors())
  191. {
  192. for (QGraphicsItem * sector : sectors)
  193. {
  194. if (existingSectors->sceneBoundingRect().contains(sector->sceneBoundingRect()))
  195. sectorsToRemove.insert(existingSectors);
  196. }
  197. }
  198. for (QGraphicsItem * sectorToRemove : sectorsToRemove)
  199. {
  200. addSector(draw(sectorToRemove->sceneBoundingRect()));
  201. removeSector(sectorToRemove);
  202. }
  203. }
  204. const QList<QGraphicsItem *> AbstractViewportLayer::getAllSectors() const //returning const is necessary to avoid "range-loop might detach Qt container" problem
  205. {
  206. QList<QGraphicsItem *> emptyList;
  207. return items ? items->childItems() : emptyList;
  208. }
  209. std::set<QGraphicsItem *> AbstractViewportLayer::getContainingSectors(const std::vector<int3> & tiles) const
  210. {
  211. std::set<QGraphicsItem *> result;
  212. for (QGraphicsItem * existingSector : getAllSectors()) {
  213. for (const int3 tile : tiles)
  214. {
  215. if (existingSector->sceneBoundingRect().contains(QPointF(tile.x * tileSize, tile.y * tileSize)))
  216. {
  217. result.insert(existingSector);
  218. break;
  219. }
  220. }
  221. }
  222. return result;
  223. }
  224. std::set<QGraphicsItem *> AbstractViewportLayer::getIntersectingSectors(const std::vector<QRectF> & areas) const
  225. {
  226. std::set<QGraphicsItem *> result;
  227. for (QGraphicsItem * existingSector : getAllSectors()) {
  228. for (QRectF area : areas)
  229. {
  230. if (existingSector->sceneBoundingRect().intersects(area))
  231. {
  232. result.insert(existingSector);
  233. }
  234. }
  235. }
  236. return result;
  237. }
  238. EmptyLayer::EmptyLayer(MapSceneBase * s): AbstractFixedLayer(s)
  239. {
  240. isShown = true;
  241. }
  242. void EmptyLayer::update()
  243. {
  244. if(!map)
  245. return;
  246. pixmap = std::make_unique<QPixmap>(map->width * 32, map->height * 32);
  247. redraw();
  248. }
  249. GridLayer::GridLayer(MapSceneBase * s): AbstractViewportLayer(s)
  250. {
  251. }
  252. QGraphicsItem * GridLayer::draw(const QRectF & section)
  253. {
  254. QPixmap pixmap(toInt(section.width()), toInt(section.height()));
  255. pixmap.fill(Qt::transparent);
  256. if (isShown)
  257. {
  258. QPainter painter(&pixmap);
  259. painter.setPen(QColor(0, 0, 0, 190));
  260. for(int j = 0; j <= pixmap.height(); j += tileSize)
  261. {
  262. painter.drawLine(0, j, pixmap.width(), j);
  263. }
  264. for(int i = 0; i <= pixmap.width(); i += tileSize)
  265. {
  266. painter.drawLine(i, 0, i, pixmap.height());
  267. }
  268. }
  269. QGraphicsItem * result = scene->addPixmap(pixmap);
  270. result->setPos(section.x(), section.y());
  271. return result;
  272. }
  273. PassabilityLayer::PassabilityLayer(MapSceneBase * s): AbstractViewportLayer(s)
  274. {
  275. }
  276. QGraphicsItem * PassabilityLayer::draw(const QRectF & section)
  277. {
  278. QPixmap pixmap(toInt(section.width()), toInt(section.height()));
  279. pixmap.fill(Qt::transparent);
  280. if(isShown)
  281. {
  282. QPainter painter(&pixmap);
  283. for(int j = 0; j <= pixmap.height(); j += tileSize)
  284. {
  285. for(int i = 0; i < pixmap.width(); i += tileSize)
  286. {
  287. auto tl = map->getTile(int3(toInt(section.x())/tileSize + i/tileSize, toInt(section.y())/tileSize + j/tileSize, scene->level));
  288. if(tl.blocked() || tl.visitable())
  289. {
  290. painter.fillRect(i, j, 31, 31, tl.visitable() ? QColor(200, 200, 0, 64) : QColor(255, 0, 0, 64));
  291. }
  292. }
  293. }
  294. }
  295. QGraphicsItem * result = scene->addPixmap(pixmap);
  296. result->setPos(section.x(), section.y());
  297. return result;
  298. }
  299. ObjectPickerLayer::ObjectPickerLayer(MapSceneBase * s): AbstractViewportLayer(s)
  300. {
  301. }
  302. void ObjectPickerLayer::highlight(const std::function<bool(const CGObjectInstance *)> & predicate)
  303. {
  304. if(!map)
  305. return;
  306. for(int j = 0; j < map->height; ++j)
  307. {
  308. for(int i = 0; i < map->width; ++i)
  309. {
  310. auto tl = map->getTile(int3(i, j, scene->level));
  311. ObjectInstanceID objID = tl.topVisitableObj();
  312. if(!objID.hasValue() && !tl.blockingObjects.empty())
  313. objID = tl.blockingObjects.front();
  314. if (objID.hasValue())
  315. {
  316. const CGObjectInstance * obj = map->getObject(objID);
  317. if(obj && predicate(obj))
  318. possibleObjects.insert(obj);
  319. }
  320. }
  321. }
  322. isActive = true;
  323. }
  324. bool ObjectPickerLayer::isVisible() const
  325. {
  326. return isShown && isActive;
  327. }
  328. void ObjectPickerLayer::clear()
  329. {
  330. possibleObjects.clear();
  331. isActive = false;
  332. }
  333. QGraphicsItem * ObjectPickerLayer::draw(const QRectF & section)
  334. {
  335. int offsetX = toInt(section.x());
  336. int offsetY = toInt(section.y());
  337. QPixmap pixmap(toInt(section.width()), toInt(section.height()));
  338. pixmap.fill(Qt::transparent);
  339. if(isVisible())
  340. pixmap.fill(QColor(255, 255, 255, 128));
  341. QPainter painter(&pixmap);
  342. painter.setCompositionMode(QPainter::CompositionMode_Source);
  343. for(const auto * obj : possibleObjects)
  344. {
  345. if(obj->pos.z != scene->level)
  346. continue;
  347. for(const auto & pos : obj->getBlockedPos())
  348. painter.fillRect(pos.x * tileSize - offsetX, pos.y * tileSize - offsetY, tileSize, tileSize, QColor(255, 211, 0, 64));
  349. }
  350. QGraphicsItem * result = scene->addPixmap(pixmap);
  351. result->setPos(section.x(), section.y());
  352. return result;
  353. }
  354. void ObjectPickerLayer::select(const CGObjectInstance * obj)
  355. {
  356. if(obj && possibleObjects.count(obj))
  357. {
  358. clear();
  359. Q_EMIT selectionMade(obj);
  360. }
  361. }
  362. void ObjectPickerLayer::discard()
  363. {
  364. clear();
  365. Q_EMIT selectionMade(nullptr);
  366. }
  367. SelectionTerrainLayer::SelectionTerrainLayer(MapSceneBase * s): AbstractViewportLayer(s)
  368. {
  369. }
  370. QGraphicsItem * SelectionTerrainLayer::draw(const QRectF & section)
  371. {
  372. int offsetX = toInt(section.x());
  373. int offsetY = toInt(section.y());
  374. QPixmap pixmap(toInt(section.width()), toInt(section.height()));
  375. pixmap.fill(Qt::transparent);
  376. QPainter painter(&pixmap);
  377. painter.setCompositionMode(QPainter::CompositionMode_Source);
  378. for(const auto & t : area)
  379. {
  380. if(section.contains(t.x * tileSize, t.y * tileSize))
  381. painter.fillRect(t.x * tileSize - offsetX, t.y * tileSize - offsetY, 31, 31, QColor(128, 128, 128, 96));
  382. }
  383. QGraphicsPixmapItem * result = scene->addPixmap(pixmap);
  384. result->setPos(section.x(), section.y());
  385. return result;
  386. }
  387. void SelectionTerrainLayer::select(const std::vector<int3> & tiles)
  388. {
  389. for (int3 tile : tiles)
  390. {
  391. if(!area.count(tile))
  392. {
  393. area.insert(tile);
  394. }
  395. }
  396. redraw(tiles);
  397. onSelection();
  398. }
  399. void SelectionTerrainLayer::erase(const std::vector<int3> & tiles)
  400. {
  401. for (int3 tile : tiles)
  402. {
  403. if(area.count(tile))
  404. {
  405. area.erase(tile);
  406. }
  407. }
  408. redraw(tiles);
  409. onSelection();
  410. }
  411. void SelectionTerrainLayer::clear()
  412. {
  413. area.clear();
  414. onSelection();
  415. redraw();
  416. }
  417. const std::set<int3> & SelectionTerrainLayer::selection() const
  418. {
  419. return area;
  420. }
  421. void SelectionTerrainLayer::onSelection()
  422. {
  423. Q_EMIT selectionMade(!area.empty());
  424. }
  425. TerrainLayer::TerrainLayer(MapSceneBase * s): AbstractViewportLayer(s)
  426. {
  427. }
  428. void TerrainLayer::redrawTerrain(const std::vector<int3> & tiles)
  429. {
  430. redrawWithSurroundingTiles(tiles);
  431. }
  432. QGraphicsItem * TerrainLayer::draw(const QRectF & section)
  433. {
  434. int left = toInt(section.left());
  435. int right = toInt(section.right());
  436. int top = toInt(section.top());
  437. int bottom = toInt(section.bottom());
  438. QPixmap pixmap(toInt(section.width()), toInt(section.height()));
  439. pixmap.fill(Qt::transparent);
  440. QPainter painter(&pixmap);
  441. QPointF offset = section.topLeft();
  442. for(int x = left/tileSize; x < right/tileSize; ++x)
  443. {
  444. for(int y = top/tileSize; y < bottom/tileSize; ++y)
  445. {
  446. handler->drawTerrainTile(painter, x, y, scene->level, offset);
  447. handler->drawRiver(painter, x, y, scene->level, offset);
  448. handler->drawRoad(painter, x, y, scene->level, offset);
  449. }
  450. }
  451. QGraphicsPixmapItem * result = scene->addPixmap(pixmap);
  452. result->setPos(section.x(), section.y());
  453. return result;
  454. }
  455. ObjectsLayer::ObjectsLayer(MapSceneBase * s): AbstractViewportLayer(s)
  456. {
  457. }
  458. QGraphicsItem * ObjectsLayer::draw(const QRectF & section)
  459. {
  460. QPixmap pixmap(toInt(section.width()), toInt(section.height()));
  461. pixmap.fill(Qt::transparent);
  462. if (isShown)
  463. {
  464. QPainter painter(&pixmap);
  465. handler->drawObjects(painter, section, scene->level, lockedObjects);
  466. }
  467. QGraphicsPixmapItem * result = scene->addPixmap(pixmap);
  468. result->setPos(section.x(), section.y());
  469. return result;
  470. }
  471. void ObjectsLayer::redrawObjects(const std::set<CGObjectInstance *> & objects)
  472. {
  473. redraw(objects);
  474. }
  475. void ObjectsLayer::setLockObject(const CGObjectInstance * object, bool lock)
  476. {
  477. if(lock)
  478. lockedObjects.insert(object);
  479. else
  480. lockedObjects.erase(object);
  481. QRectF area = getObjectArea(object);
  482. redraw({area});
  483. }
  484. void ObjectsLayer::unlockAll()
  485. {
  486. lockedObjects.clear();
  487. redraw();
  488. }
  489. SelectionObjectsLayer::SelectionObjectsLayer(MapSceneBase * s): AbstractViewportLayer(s), newObject(nullptr)
  490. {
  491. }
  492. QGraphicsItem * SelectionObjectsLayer::draw(const QRectF & section)
  493. {
  494. QPixmap pixmap(toInt(section.width()), toInt(section.height()));
  495. pixmap.fill(Qt::transparent);
  496. if (isShown)
  497. {
  498. QPainter painter(&pixmap);
  499. painter.setCompositionMode(QPainter::CompositionMode_Source);
  500. painter.setPen(Qt::white);
  501. QPointF offset = section.topLeft();
  502. for(auto * obj : selectedObjects)
  503. {
  504. auto objectArea = getObjectArea(obj);
  505. if(obj != newObject.get() && section.intersects(objectArea))
  506. {
  507. auto pos = obj->anchorPos();
  508. QRectF bbox(pos.x, pos.y, 1, 1);
  509. for(const auto & t : obj->getBlockedPos())
  510. {
  511. QPointF topLeft(std::min(t.x * 1.0, bbox.topLeft().x()), std::min(t.y * 1.0, bbox.topLeft().y()));
  512. bbox.setTopLeft(topLeft);
  513. QPointF bottomRight(std::max(t.x * 1.0, bbox.bottomRight().x()), std::max(t.y * 1.0, bbox.bottomRight().y()));
  514. bbox.setBottomRight(bottomRight);
  515. }
  516. //selection box's size was decreased by 1 px to get rid of a persistent bug
  517. //with displaying a box on a border of two sectors. Bite me.
  518. painter.setOpacity(1.0);
  519. QRectF rect((bbox.x() * tileSize + 1) - offset.x(), (bbox.y() * tileSize + 1) - offset.y(), (bbox.width() * tileSize) - 2, (bbox.height() * tileSize) - 2);
  520. painter.drawRect(rect);
  521. }
  522. if(selectionMode == SelectionMode::MOVEMENT && (shift.x() || shift.y()))
  523. {
  524. objectArea.moveTo(objectArea.topLeft() + (shift * tileSize));
  525. if (section.intersects(objectArea))
  526. {
  527. painter.setOpacity(0.7);
  528. auto newPos = QPoint(obj->anchorPos().x, obj->anchorPos().y) + shift;
  529. handler->drawObjectAt(painter, obj, newPos.x(), newPos.y(), offset);
  530. }
  531. }
  532. }
  533. }
  534. QGraphicsPixmapItem * result = scene->addPixmap(pixmap);
  535. result->setPos(section.x(), section.y());
  536. return result;
  537. }
  538. CGObjectInstance * SelectionObjectsLayer::selectObjectAt(int x, int y, const CGObjectInstance * ignore) const
  539. {
  540. if(!map || !map->isInTheMap(int3(x, y, scene->level)))
  541. return nullptr;
  542. auto & objects = handler->getObjects(x, y, scene->level);
  543. //visitable is most important
  544. for(auto & object : objects)
  545. {
  546. if(!object.obj || object.obj == ignore || lockedObjects.count(object.obj))
  547. continue;
  548. if(object.obj->visitableAt(int3(x, y, scene->level)))
  549. {
  550. return const_cast<CGObjectInstance*>(object.obj);
  551. }
  552. }
  553. //if not visitable tile - try to get blocked
  554. for(auto & object : objects)
  555. {
  556. if(!object.obj || object.obj == ignore || lockedObjects.count(object.obj))
  557. continue;
  558. if(object.obj->blockingAt(int3(x, y, scene->level)))
  559. {
  560. return const_cast<CGObjectInstance*>(object.obj);
  561. }
  562. }
  563. //finally, we can take any object
  564. for(auto & object : objects)
  565. {
  566. if(!object.obj || object.obj == ignore || lockedObjects.count(object.obj))
  567. continue;
  568. if(object.obj->coveringAt(int3(x, y, scene->level)))
  569. {
  570. return const_cast<CGObjectInstance*>(object.obj);
  571. }
  572. }
  573. return nullptr;
  574. }
  575. void SelectionObjectsLayer::selectObjects(int x1, int y1, int x2, int y2)
  576. {
  577. if(!map)
  578. return;
  579. if(x1 > x2)
  580. std::swap(x1, x2);
  581. if(y1 > y2)
  582. std::swap(y1, y2);
  583. std::set<CGObjectInstance *> selectedObjects;
  584. for(int j = y1; j < y2; ++j)
  585. {
  586. for(int i = x1; i < x2; ++i)
  587. {
  588. if(map->isInTheMap(int3(i, j, scene->level)))
  589. {
  590. for(auto & o : handler->getObjects(i, j, scene->level))
  591. if(!lockedObjects.count(o.obj))
  592. {
  593. selectedObjects.insert(const_cast<CGObjectInstance*>(o.obj));
  594. }
  595. }
  596. }
  597. }
  598. selectObjects(selectedObjects);
  599. }
  600. void SelectionObjectsLayer::selectObject(CGObjectInstance * obj)
  601. {
  602. selectedObjects.insert(obj);
  603. onSelection();
  604. redraw({obj});
  605. }
  606. void SelectionObjectsLayer::selectObjects(const std::set<CGObjectInstance *> & objs)
  607. {
  608. for (CGObjectInstance * obj : objs)
  609. {
  610. selectedObjects.insert(obj);
  611. }
  612. onSelection();
  613. redraw(objs);
  614. }
  615. void SelectionObjectsLayer::deselectObject(CGObjectInstance * obj)
  616. {
  617. selectedObjects.erase(obj);
  618. redraw({obj});
  619. }
  620. bool SelectionObjectsLayer::isSelected(const CGObjectInstance * obj) const
  621. {
  622. return selectedObjects.count(const_cast<CGObjectInstance*>(obj));
  623. }
  624. std::set<CGObjectInstance*> SelectionObjectsLayer::getSelection() const
  625. {
  626. return selectedObjects;
  627. }
  628. void SelectionObjectsLayer::clear()
  629. {
  630. selectedObjects.clear();
  631. shift.setX(0);
  632. shift.setY(0);
  633. redraw();
  634. }
  635. void SelectionObjectsLayer::onSelection()
  636. {
  637. Q_EMIT selectionMade(!selectedObjects.empty());
  638. }
  639. void SelectionObjectsLayer::setShift(int x, int y)
  640. {
  641. std::vector<QRectF>areas;
  642. if(shift.x() || shift.y())
  643. {
  644. for (auto * selectedObject : selectedObjects)
  645. {
  646. QRectF formerArea = getObjectArea(selectedObject);
  647. formerArea.moveTo(formerArea.topLeft() + (shift * tileSize));
  648. areas.emplace_back(formerArea);
  649. }
  650. }
  651. shift = QPoint(x, y);
  652. for (auto * selectedObject : selectedObjects)
  653. {
  654. QRectF area = getObjectArea(selectedObject);
  655. area.moveTo(area.topLeft() + (shift * tileSize));
  656. areas.emplace_back(area);
  657. }
  658. redraw(areas);
  659. }
  660. void SelectionObjectsLayer::setLockObject(CGObjectInstance * object, bool lock)
  661. {
  662. if(lock)
  663. lockedObjects.insert(object);
  664. else
  665. lockedObjects.erase(object);
  666. redraw({object});
  667. }
  668. void SelectionObjectsLayer::unlockAll()
  669. {
  670. lockedObjects.clear();
  671. }
  672. MinimapLayer::MinimapLayer(MapSceneBase * s): AbstractFixedLayer(s)
  673. {
  674. }
  675. void MinimapLayer::update()
  676. {
  677. if(!map)
  678. return;
  679. pixmap = std::make_unique<QPixmap>(map->width, map->height);
  680. QPainter painter(pixmap.get());
  681. //coordinate transformation
  682. for(int j = 0; j < map->height; ++j)
  683. {
  684. for(int i = 0; i < map->width; ++i)
  685. {
  686. handler->drawMinimapTile(painter, i, j, scene->level);
  687. }
  688. }
  689. redraw();
  690. }
  691. MinimapViewLayer::MinimapViewLayer(MapSceneBase * s): AbstractFixedLayer(s)
  692. {
  693. }
  694. void MinimapViewLayer::update()
  695. {
  696. if(!map)
  697. return;
  698. pixmap = std::make_unique<QPixmap>(map->width, map->height);
  699. draw();
  700. }
  701. void MinimapViewLayer::draw()
  702. {
  703. if(!map)
  704. return;
  705. pixmap->fill(Qt::transparent);
  706. //maybe not optimal but ok
  707. QPainter painter(pixmap.get());
  708. painter.setPen(Qt::white);
  709. painter.drawRect(x, y, w, h);
  710. redraw();
  711. }
  712. void MinimapViewLayer::setViewport(int _x, int _y, int _w, int _h)
  713. {
  714. x = _x;
  715. y = _y;
  716. w = _w;
  717. h = _h;
  718. draw();
  719. }