scenelayer.cpp 19 KB

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