Нема описа

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