Ingen beskrivning

map.cpp 25KB

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