Ingen beskrivning

map.cpp 26KB

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