Keine Beschreibung

map.cpp 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. #include "map.h"
  2. #include <QTime>
  3. #include <QDebug>
  4. #include <QPainter>
  5. #include <QImage>
  6. #include <QRegularExpression>
  7. Map::Map(QObject *parent) : QObject(parent)
  8. {
  9. paint_tile_index = 1;
  10. paint_collision = 0;
  11. paint_elevation = 3;
  12. }
  13. void Map::setName(QString mapName) {
  14. name = mapName;
  15. constantName = mapConstantFromName(mapName);
  16. }
  17. QString Map::mapConstantFromName(QString mapName) {
  18. // Transform map names of the form 'GraniteCave_B1F` into map constants like 'MAP_GRANITE_CAVE_B1F'.
  19. QString nameWithUnderscores = mapName.replace(QRegularExpression("([a-z])([A-Z])"), "\\1_\\2");
  20. QString withMapAndUppercase = "MAP_" + nameWithUnderscores.toUpper();
  21. QString constantName = withMapAndUppercase.replace(QRegularExpression("_+"), "_");
  22. // Handle special cases.
  23. // SSTidal needs to be SS_TIDAL, rather than SSTIDAL
  24. constantName = constantName.replace("SSTIDAL", "SS_TIDAL");
  25. return constantName;
  26. }
  27. QString Map::objectEventsLabelFromName(QString mapName)
  28. {
  29. return QString("%1_EventObjects").arg(mapName);
  30. }
  31. QString Map::warpEventsLabelFromName(QString mapName)
  32. {
  33. return QString("%1_MapWarps").arg(mapName);
  34. }
  35. QString Map::coordEventsLabelFromName(QString mapName)
  36. {
  37. return QString("%1_MapCoordEvents").arg(mapName);
  38. }
  39. QString Map::bgEventsLabelFromName(QString mapName)
  40. {
  41. return QString("%1_MapBGEvents").arg(mapName);
  42. }
  43. int Map::getWidth() {
  44. return layout->width.toInt(nullptr, 0);
  45. }
  46. int Map::getHeight() {
  47. return layout->height.toInt(nullptr, 0);
  48. }
  49. int Map::getSelectedBlockIndex(int index) {
  50. if (index < layout->tileset_primary->metatiles->length()) {
  51. return index;
  52. } else {
  53. return 0x200 + (index - layout->tileset_primary->metatiles->length());
  54. }
  55. }
  56. int Map::getDisplayedBlockIndex(int index) {
  57. if (index < layout->tileset_primary->metatiles->length()) {
  58. return index;
  59. } else {
  60. return index - 0x200 + layout->tileset_primary->metatiles->length();
  61. }
  62. }
  63. QImage Map::getCollisionMetatileImage(Block block) {
  64. return getCollisionMetatileImage(block.collision);
  65. }
  66. QImage Map::getCollisionMetatileImage(int collision) {
  67. QImage metatile_image(16, 16, QImage::Format_RGBA8888);
  68. QColor color;
  69. if (collision == 0) {
  70. color.setGreen(0xff);
  71. } else if (collision == 1) {
  72. color.setRed(0xff);
  73. } else if (collision == 2) {
  74. color.setBlue(0xff);
  75. } else if (collision == 3) {
  76. // black
  77. }
  78. metatile_image.fill(color);
  79. return metatile_image;
  80. }
  81. QImage Map::getElevationMetatileImage(Block block) {
  82. return getElevationMetatileImage(block.elevation);
  83. }
  84. QImage Map::getElevationMetatileImage(int elevation) {
  85. QImage metatile_image(16, 16, QImage::Format_RGBA8888);
  86. QColor color;
  87. if (elevation < 15) {
  88. uint saturation = (elevation + 1) * 16 + 15;
  89. color.setGreen(saturation);
  90. color.setRed(saturation);
  91. color.setBlue(saturation);
  92. } else {
  93. color.setGreen(0xd0);
  94. color.setBlue(0xd0);
  95. color.setRed(0);
  96. }
  97. metatile_image.fill(color);
  98. //QPainter painter(&metatile_image);
  99. //painter.end();
  100. return metatile_image;
  101. }
  102. bool Map::blockChanged(int i, Blockdata *cache) {
  103. if (cache == NULL || cache == nullptr) {
  104. return true;
  105. }
  106. if (layout->blockdata == NULL || layout->blockdata == nullptr) {
  107. return true;
  108. }
  109. if (cache->blocks == NULL || cache->blocks == nullptr) {
  110. return true;
  111. }
  112. if (layout->blockdata->blocks == NULL || layout->blockdata->blocks == nullptr) {
  113. return true;
  114. }
  115. if (cache->blocks->length() <= i) {
  116. return true;
  117. }
  118. if (layout->blockdata->blocks->length() <= i) {
  119. return true;
  120. }
  121. return layout->blockdata->blocks->value(i) != cache->blocks->value(i);
  122. }
  123. void Map::cacheBorder() {
  124. if (layout->cached_border) delete layout->cached_border;
  125. layout->cached_border = new Blockdata;
  126. if (layout->border && layout->border->blocks) {
  127. for (int i = 0; i < layout->border->blocks->length(); i++) {
  128. Block block = layout->border->blocks->value(i);
  129. layout->cached_border->blocks->append(block);
  130. }
  131. }
  132. }
  133. void Map::cacheBlockdata() {
  134. if (layout->cached_blockdata) delete layout->cached_blockdata;
  135. layout->cached_blockdata = new Blockdata;
  136. if (layout->blockdata && layout->blockdata->blocks) {
  137. for (int i = 0; i < layout->blockdata->blocks->length(); i++) {
  138. Block block = layout->blockdata->blocks->value(i);
  139. layout->cached_blockdata->blocks->append(block);
  140. }
  141. }
  142. }
  143. void Map::cacheCollision() {
  144. if (layout->cached_collision) delete layout->cached_collision;
  145. layout->cached_collision = new Blockdata;
  146. if (layout->blockdata && layout->blockdata->blocks) {
  147. for (int i = 0; i < layout->blockdata->blocks->length(); i++) {
  148. Block block = layout->blockdata->blocks->value(i);
  149. layout->cached_collision->blocks->append(block);
  150. }
  151. }
  152. }
  153. QPixmap Map::renderCollision(bool ignoreCache) {
  154. bool changed_any = false;
  155. int width_ = getWidth();
  156. int height_ = getHeight();
  157. if (
  158. collision_image.isNull()
  159. || collision_image.width() != width_ * 16
  160. || collision_image.height() != height_ * 16
  161. ) {
  162. collision_image = QImage(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  163. changed_any = true;
  164. }
  165. if (!(layout->blockdata && layout->blockdata->blocks && width_ && height_)) {
  166. collision_pixmap = collision_pixmap.fromImage(collision_image);
  167. return collision_pixmap;
  168. }
  169. QPainter painter(&collision_image);
  170. for (int i = 0; i < layout->blockdata->blocks->length(); i++) {
  171. if (!ignoreCache && layout->cached_collision && !blockChanged(i, layout->cached_collision)) {
  172. continue;
  173. }
  174. changed_any = true;
  175. Block block = layout->blockdata->blocks->value(i);
  176. QImage metatile_image = Metatile::getMetatileImage(block.tile, layout->tileset_primary, layout->tileset_secondary);
  177. QImage collision_metatile_image = getCollisionMetatileImage(block);
  178. QImage elevation_metatile_image = getElevationMetatileImage(block);
  179. int map_y = width_ ? i / width_ : 0;
  180. int map_x = width_ ? i % width_ : 0;
  181. QPoint metatile_origin = QPoint(map_x * 16, map_y * 16);
  182. painter.setOpacity(1);
  183. painter.drawImage(metatile_origin, metatile_image);
  184. painter.save();
  185. if (block.elevation == 15) {
  186. painter.setOpacity(0.5);
  187. } else if (block.elevation == 0) {
  188. painter.setOpacity(0);
  189. } else {
  190. painter.setOpacity(1);//(block.elevation / 16.0) * 0.8);
  191. painter.setCompositionMode(QPainter::CompositionMode_Overlay);
  192. }
  193. painter.drawImage(metatile_origin, elevation_metatile_image);
  194. painter.restore();
  195. painter.save();
  196. if (block.collision == 0) {
  197. painter.setOpacity(0.1);
  198. } else {
  199. painter.setOpacity(0.4);
  200. }
  201. painter.drawImage(metatile_origin, collision_metatile_image);
  202. painter.restore();
  203. painter.save();
  204. painter.setOpacity(0.6);
  205. painter.setPen(QColor(255, 255, 255, 192));
  206. painter.setFont(QFont("Helvetica", 8));
  207. painter.drawText(QPoint(metatile_origin.x(), metatile_origin.y() + 8), QString("%1").arg(block.elevation));
  208. painter.restore();
  209. }
  210. painter.end();
  211. cacheCollision();
  212. if (changed_any) {
  213. collision_pixmap = collision_pixmap.fromImage(collision_image);
  214. }
  215. return collision_pixmap;
  216. }
  217. QPixmap Map::render(bool ignoreCache = false) {
  218. bool changed_any = false;
  219. int width_ = getWidth();
  220. int height_ = getHeight();
  221. if (
  222. image.isNull()
  223. || image.width() != width_ * 16
  224. || image.height() != height_ * 16
  225. ) {
  226. image = QImage(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  227. changed_any = true;
  228. }
  229. if (!(layout->blockdata && layout->blockdata->blocks && width_ && height_)) {
  230. pixmap = pixmap.fromImage(image);
  231. return pixmap;
  232. }
  233. QPainter painter(&image);
  234. for (int i = 0; i < layout->blockdata->blocks->length(); i++) {
  235. if (!ignoreCache && !blockChanged(i, layout->cached_blockdata)) {
  236. continue;
  237. }
  238. changed_any = true;
  239. Block block = layout->blockdata->blocks->value(i);
  240. QImage metatile_image = Metatile::getMetatileImage(block.tile, layout->tileset_primary, layout->tileset_secondary);
  241. int map_y = width_ ? i / width_ : 0;
  242. int map_x = width_ ? i % width_ : 0;
  243. QPoint metatile_origin = QPoint(map_x * 16, map_y * 16);
  244. painter.drawImage(metatile_origin, metatile_image);
  245. }
  246. painter.end();
  247. if (changed_any) {
  248. cacheBlockdata();
  249. pixmap = pixmap.fromImage(image);
  250. }
  251. return pixmap;
  252. }
  253. QPixmap Map::renderBorder() {
  254. bool changed_any = false;
  255. int width_ = 2;
  256. int height_ = 2;
  257. if (layout->border_image.isNull()) {
  258. layout->border_image = QImage(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  259. changed_any = true;
  260. }
  261. if (!(layout->border && layout->border->blocks)) {
  262. layout->border_pixmap = layout->border_pixmap.fromImage(layout->border_image);
  263. return layout->border_pixmap;
  264. }
  265. QPainter painter(&layout->border_image);
  266. for (int i = 0; i < layout->border->blocks->length(); i++) {
  267. if (!blockChanged(i, layout->cached_border)) {
  268. continue;
  269. }
  270. changed_any = true;
  271. Block block = layout->border->blocks->value(i);
  272. QImage metatile_image = Metatile::getMetatileImage(block.tile, layout->tileset_primary, layout->tileset_secondary);
  273. int map_y = i / width_;
  274. int map_x = i % width_;
  275. painter.drawImage(QPoint(map_x * 16, map_y * 16), metatile_image);
  276. }
  277. painter.end();
  278. if (changed_any) {
  279. cacheBorder();
  280. layout->border_pixmap = layout->border_pixmap.fromImage(layout->border_image);
  281. }
  282. return layout->border_pixmap;
  283. }
  284. QPixmap Map::renderConnection(Connection connection) {
  285. render();
  286. int x, y, w, h;
  287. if (connection.direction == "up") {
  288. x = 0;
  289. y = getHeight() - 6;
  290. w = getWidth();
  291. h = 6;
  292. } else if (connection.direction == "down") {
  293. x = 0;
  294. y = 0;
  295. w = getWidth();
  296. h = 6;
  297. } else if (connection.direction == "left") {
  298. x = getWidth() - 6;
  299. y = 0;
  300. w = 6;
  301. h = getHeight();
  302. } else if (connection.direction == "right") {
  303. x = 0;
  304. y = 0;
  305. w = 6;
  306. h = getHeight();
  307. } else {
  308. // this should not happen
  309. x = 0;
  310. y = 0;
  311. w = getWidth();
  312. h = getHeight();
  313. }
  314. QImage connection_image = image.copy(x * 16, y * 16, w * 16, h * 16);
  315. //connection_image = connection_image.convertToFormat(QImage::Format_Grayscale8);
  316. return QPixmap::fromImage(connection_image);
  317. }
  318. QPixmap Map::renderCollisionMetatiles() {
  319. int length_ = 4;
  320. int height_ = 1;
  321. int width_ = length_ / height_;
  322. QImage image(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  323. QPainter painter(&image);
  324. for (int i = 0; i < length_; i++) {
  325. int y = i / width_;
  326. int x = i % width_;
  327. QPoint origin(x * 16, y * 16);
  328. QImage metatile_image = getCollisionMetatileImage(i);
  329. painter.drawImage(origin, metatile_image);
  330. }
  331. drawSelection(paint_collision, width_, 1, 1, &painter);
  332. painter.end();
  333. return QPixmap::fromImage(image);
  334. }
  335. QPixmap Map::renderElevationMetatiles() {
  336. int length_ = 16;
  337. int height_ = 2;
  338. int width_ = length_ / height_;
  339. QImage image(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  340. QPainter painter(&image);
  341. for (int i = 0; i < length_; i++) {
  342. int y = i / width_;
  343. int x = i % width_;
  344. QPoint origin(x * 16, y * 16);
  345. QImage metatile_image = getElevationMetatileImage(i);
  346. painter.drawImage(origin, metatile_image);
  347. }
  348. drawSelection(paint_elevation, width_, 1, 1, &painter);
  349. painter.end();
  350. return QPixmap::fromImage(image);
  351. }
  352. void Map::drawSelection(int i, int w, int selectionWidth, int selectionHeight, QPainter *painter) {
  353. int x = i % w;
  354. int y = i / w;
  355. painter->save();
  356. QColor penColor = smart_paths_enabled ? QColor(0xff, 0x0, 0xff) : QColor(0xff, 0xff, 0xff);
  357. painter->setPen(penColor);
  358. int rectWidth = selectionWidth * 16;
  359. int rectHeight = selectionHeight * 16;
  360. painter->drawRect(x * 16, y * 16, rectWidth - 1, rectHeight -1);
  361. painter->setPen(QColor(0, 0, 0));
  362. painter->drawRect(x * 16 - 1, y * 16 - 1, rectWidth + 1, rectHeight + 1);
  363. painter->drawRect(x * 16 + 1, y * 16 + 1, rectWidth - 3, rectHeight - 3);
  364. painter->restore();
  365. }
  366. QPixmap Map::renderMetatiles() {
  367. if (!layout->tileset_primary || !layout->tileset_primary->metatiles
  368. || !layout->tileset_secondary || !layout->tileset_secondary->metatiles) {
  369. return QPixmap();
  370. }
  371. int primary_length = layout->tileset_primary->metatiles->length();
  372. int length_ = primary_length + layout->tileset_secondary->metatiles->length();
  373. int width_ = 8;
  374. int height_ = length_ / width_;
  375. QImage image(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  376. QPainter painter(&image);
  377. for (int i = 0; i < length_; i++) {
  378. int tile = i;
  379. if (i >= primary_length) {
  380. tile += 0x200 - primary_length;
  381. }
  382. QImage metatile_image = Metatile::getMetatileImage(tile, layout->tileset_primary, layout->tileset_secondary);
  383. int map_y = i / width_;
  384. int map_x = i % width_;
  385. QPoint metatile_origin = QPoint(map_x * 16, map_y * 16);
  386. painter.drawImage(metatile_origin, metatile_image);
  387. }
  388. drawSelection(paint_tile_index, width_, paint_tile_width, paint_tile_height, &painter);
  389. painter.end();
  390. return QPixmap::fromImage(image);
  391. }
  392. Block* Map::getBlock(int x, int y) {
  393. if (layout->blockdata && layout->blockdata->blocks) {
  394. if (x >= 0 && x < getWidth())
  395. if (y >= 0 && y < getHeight()) {
  396. int i = y * getWidth() + x;
  397. return new Block(layout->blockdata->blocks->value(i));
  398. }
  399. }
  400. return NULL;
  401. }
  402. void Map::_setBlock(int x, int y, Block block) {
  403. int i = y * getWidth() + x;
  404. if (layout->blockdata && layout->blockdata->blocks) {
  405. layout->blockdata->blocks->replace(i, block);
  406. }
  407. }
  408. void Map::_floodFill(int x, int y, uint tile) {
  409. QList<QPoint> todo;
  410. todo.append(QPoint(x, y));
  411. while (todo.length()) {
  412. QPoint point = todo.takeAt(0);
  413. x = point.x();
  414. y = point.y();
  415. Block *block = getBlock(x, y);
  416. if (block == NULL) {
  417. continue;
  418. }
  419. uint old_tile = block->tile;
  420. if (old_tile == tile) {
  421. continue;
  422. }
  423. block->tile = tile;
  424. _setBlock(x, y, *block);
  425. if ((block = getBlock(x + 1, y)) && block->tile == old_tile) {
  426. todo.append(QPoint(x + 1, y));
  427. }
  428. if ((block = getBlock(x - 1, y)) && block->tile == old_tile) {
  429. todo.append(QPoint(x - 1, y));
  430. }
  431. if ((block = getBlock(x, y + 1)) && block->tile == old_tile) {
  432. todo.append(QPoint(x, y + 1));
  433. }
  434. if ((block = getBlock(x, y - 1)) && block->tile == old_tile) {
  435. todo.append(QPoint(x, y - 1));
  436. }
  437. }
  438. }
  439. void Map::_floodFillCollision(int x, int y, uint collision) {
  440. QList<QPoint> todo;
  441. todo.append(QPoint(x, y));
  442. while (todo.length()) {
  443. QPoint point = todo.takeAt(0);
  444. x = point.x();
  445. y = point.y();
  446. Block *block = getBlock(x, y);
  447. if (block == NULL) {
  448. continue;
  449. }
  450. uint old_coll = block->collision;
  451. if (old_coll == collision) {
  452. continue;
  453. }
  454. block->collision = collision;
  455. _setBlock(x, y, *block);
  456. if ((block = getBlock(x + 1, y)) && block->collision == old_coll) {
  457. todo.append(QPoint(x + 1, y));
  458. }
  459. if ((block = getBlock(x - 1, y)) && block->collision == old_coll) {
  460. todo.append(QPoint(x - 1, y));
  461. }
  462. if ((block = getBlock(x, y + 1)) && block->collision == old_coll) {
  463. todo.append(QPoint(x, y + 1));
  464. }
  465. if ((block = getBlock(x, y - 1)) && block->collision == old_coll) {
  466. todo.append(QPoint(x, y - 1));
  467. }
  468. }
  469. }
  470. void Map::_floodFillElevation(int x, int y, uint elevation) {
  471. QList<QPoint> todo;
  472. todo.append(QPoint(x, y));
  473. while (todo.length()) {
  474. QPoint point = todo.takeAt(0);
  475. x = point.x();
  476. y = point.y();
  477. Block *block = getBlock(x, y);
  478. if (block == NULL) {
  479. continue;
  480. }
  481. uint old_z = block->elevation;
  482. if (old_z == elevation) {
  483. continue;
  484. }
  485. Block block_(*block);
  486. block_.elevation = elevation;
  487. _setBlock(x, y, block_);
  488. if ((block = getBlock(x + 1, y)) && block->elevation == old_z) {
  489. todo.append(QPoint(x + 1, y));
  490. }
  491. if ((block = getBlock(x - 1, y)) && block->elevation == old_z) {
  492. todo.append(QPoint(x - 1, y));
  493. }
  494. if ((block = getBlock(x, y + 1)) && block->elevation == old_z) {
  495. todo.append(QPoint(x, y + 1));
  496. }
  497. if ((block = getBlock(x, y - 1)) && block->elevation == old_z) {
  498. todo.append(QPoint(x, y - 1));
  499. }
  500. }
  501. }
  502. void Map::_floodFillCollisionElevation(int x, int y, uint collision, uint elevation) {
  503. QList<QPoint> todo;
  504. todo.append(QPoint(x, y));
  505. while (todo.length()) {
  506. QPoint point = todo.takeAt(0);
  507. x = point.x();
  508. y = point.y();
  509. Block *block = getBlock(x, y);
  510. if (block == NULL) {
  511. continue;
  512. }
  513. uint old_coll = block->collision;
  514. uint old_elev = block->elevation;
  515. if (old_coll == collision && old_elev == elevation) {
  516. continue;
  517. }
  518. block->collision = collision;
  519. block->elevation = elevation;
  520. _setBlock(x, y, *block);
  521. if ((block = getBlock(x + 1, y)) && block->collision == old_coll && block->elevation == old_elev) {
  522. todo.append(QPoint(x + 1, y));
  523. }
  524. if ((block = getBlock(x - 1, y)) && block->collision == old_coll && block->elevation == old_elev) {
  525. todo.append(QPoint(x - 1, y));
  526. }
  527. if ((block = getBlock(x, y + 1)) && block->collision == old_coll && block->elevation == old_elev) {
  528. todo.append(QPoint(x, y + 1));
  529. }
  530. if ((block = getBlock(x, y - 1)) && block->collision == old_coll && block->elevation == old_elev) {
  531. todo.append(QPoint(x, y - 1));
  532. }
  533. }
  534. }
  535. void Map::undo() {
  536. if (layout->blockdata) {
  537. Blockdata *commit = history.back();
  538. if (commit != NULL) {
  539. layout->blockdata->copyFrom(commit);
  540. emit mapChanged(this);
  541. }
  542. }
  543. }
  544. void Map::redo() {
  545. if (layout->blockdata) {
  546. Blockdata *commit = history.next();
  547. if (commit != NULL) {
  548. layout->blockdata->copyFrom(commit);
  549. emit mapChanged(this);
  550. }
  551. }
  552. }
  553. void Map::commit() {
  554. if (layout->blockdata) {
  555. if (!layout->blockdata->equals(history.current())) {
  556. Blockdata* commit = layout->blockdata->copy();
  557. history.push(commit);
  558. emit mapChanged(this);
  559. }
  560. }
  561. }
  562. void Map::setBlock(int x, int y, Block block) {
  563. Block *old_block = getBlock(x, y);
  564. if (old_block && (*old_block) != block) {
  565. _setBlock(x, y, block);
  566. commit();
  567. }
  568. }
  569. void Map::floodFill(int x, int y, uint tile) {
  570. Block *block = getBlock(x, y);
  571. if (block && block->tile != tile) {
  572. _floodFill(x, y, tile);
  573. commit();
  574. }
  575. }
  576. void Map::floodFillCollision(int x, int y, uint collision) {
  577. Block *block = getBlock(x, y);
  578. if (block && block->collision != collision) {
  579. _floodFillCollision(x, y, collision);
  580. commit();
  581. }
  582. }
  583. void Map::floodFillElevation(int x, int y, uint elevation) {
  584. Block *block = getBlock(x, y);
  585. if (block && block->elevation != elevation) {
  586. _floodFillElevation(x, y, elevation);
  587. commit();
  588. }
  589. }
  590. void Map::floodFillCollisionElevation(int x, int y, uint collision, uint elevation) {
  591. Block *block = getBlock(x, y);
  592. if (block && (block->collision != collision || block->elevation != elevation)) {
  593. _floodFillCollisionElevation(x, y, collision, elevation);
  594. commit();
  595. }
  596. }
  597. QList<Event *> Map::getAllEvents() {
  598. QList<Event*> all;
  599. for (QList<Event*> list : events.values()) {
  600. all += list;
  601. }
  602. return all;
  603. }
  604. void Map::removeEvent(Event *event) {
  605. for (QString key : events.keys()) {
  606. events[key].removeAll(event);
  607. }
  608. }
  609. void Map::addEvent(Event *event) {
  610. events[event->get("event_group_type")].append(event);
  611. }
  612. bool Map::hasUnsavedChanges() {
  613. return !history.isSaved() || !isPersistedToFile || layout->has_unsaved_changes;
  614. }
  615. void Map::hoveredTileChanged(int x, int y, int block) {
  616. emit statusBarMessage(QString("X: %1, Y: %2, Block: 0x%3")
  617. .arg(x)
  618. .arg(y)
  619. .arg(QString("%1").arg(block, 3, 16, QChar('0')).toUpper()));
  620. }
  621. void Map::clearHoveredTile() {
  622. emit statusBarMessage(QString(""));
  623. }
  624. void Map::hoveredMetatileChanged(int blockIndex) {
  625. int tile = getSelectedBlockIndex(blockIndex);
  626. emit statusBarMessage(QString("Block: 0x%1")
  627. .arg(QString("%1").arg(tile, 3, 16, QChar('0')).toUpper()));
  628. }
  629. void Map::clearHoveredMetatile() {
  630. emit statusBarMessage(QString(""));
  631. }
  632. void Map::hoveredCollisionTileChanged(int collision) {
  633. emit statusBarMessage(QString("Collision: %1").arg(collision));
  634. }
  635. void Map::clearHoveredCollisionTile() {
  636. emit statusBarMessage(QString(""));
  637. }
  638. void Map::hoveredElevationTileChanged(int elevation) {
  639. emit statusBarMessage(QString("Elevation: %1").arg(elevation));
  640. }
  641. void Map::clearHoveredElevationTile() {
  642. emit statusBarMessage(QString(""));
  643. }