Ei kuvausta

map.cpp 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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. void Map::setDimensions(int newWidth, int newHeight) {
  393. int oldWidth = getWidth();
  394. int oldHeight = getHeight();
  395. Blockdata* newBlockData = new Blockdata;
  396. for (int y = 0; y < newHeight; y++)
  397. for (int x = 0; x < newWidth; x++) {
  398. if (x < oldWidth && y < oldHeight) {
  399. int index = y * oldWidth + x;
  400. newBlockData->addBlock(layout->blockdata->blocks->value(index));
  401. } else {
  402. newBlockData->addBlock(0);
  403. }
  404. }
  405. layout->blockdata->copyFrom(newBlockData);
  406. layout->width = QString::number(newWidth);
  407. layout->height = QString::number(newHeight);
  408. commit();
  409. emit mapChanged(this);
  410. }
  411. Block* Map::getBlock(int x, int y) {
  412. if (layout->blockdata && layout->blockdata->blocks) {
  413. if (x >= 0 && x < getWidth())
  414. if (y >= 0 && y < getHeight()) {
  415. int i = y * getWidth() + x;
  416. return new Block(layout->blockdata->blocks->value(i));
  417. }
  418. }
  419. return NULL;
  420. }
  421. void Map::_setBlock(int x, int y, Block block) {
  422. int i = y * getWidth() + x;
  423. if (layout->blockdata && layout->blockdata->blocks) {
  424. layout->blockdata->blocks->replace(i, block);
  425. }
  426. }
  427. void Map::_floodFill(int x, int y, uint tile) {
  428. QList<QPoint> todo;
  429. todo.append(QPoint(x, y));
  430. while (todo.length()) {
  431. QPoint point = todo.takeAt(0);
  432. x = point.x();
  433. y = point.y();
  434. Block *block = getBlock(x, y);
  435. if (block == NULL) {
  436. continue;
  437. }
  438. uint old_tile = block->tile;
  439. if (old_tile == tile) {
  440. continue;
  441. }
  442. block->tile = tile;
  443. _setBlock(x, y, *block);
  444. if ((block = getBlock(x + 1, y)) && block->tile == old_tile) {
  445. todo.append(QPoint(x + 1, y));
  446. }
  447. if ((block = getBlock(x - 1, y)) && block->tile == old_tile) {
  448. todo.append(QPoint(x - 1, y));
  449. }
  450. if ((block = getBlock(x, y + 1)) && block->tile == old_tile) {
  451. todo.append(QPoint(x, y + 1));
  452. }
  453. if ((block = getBlock(x, y - 1)) && block->tile == old_tile) {
  454. todo.append(QPoint(x, y - 1));
  455. }
  456. }
  457. }
  458. void Map::_floodFillCollision(int x, int y, uint collision) {
  459. QList<QPoint> todo;
  460. todo.append(QPoint(x, y));
  461. while (todo.length()) {
  462. QPoint point = todo.takeAt(0);
  463. x = point.x();
  464. y = point.y();
  465. Block *block = getBlock(x, y);
  466. if (block == NULL) {
  467. continue;
  468. }
  469. uint old_coll = block->collision;
  470. if (old_coll == collision) {
  471. continue;
  472. }
  473. block->collision = collision;
  474. _setBlock(x, y, *block);
  475. if ((block = getBlock(x + 1, y)) && block->collision == old_coll) {
  476. todo.append(QPoint(x + 1, y));
  477. }
  478. if ((block = getBlock(x - 1, y)) && block->collision == old_coll) {
  479. todo.append(QPoint(x - 1, y));
  480. }
  481. if ((block = getBlock(x, y + 1)) && block->collision == old_coll) {
  482. todo.append(QPoint(x, y + 1));
  483. }
  484. if ((block = getBlock(x, y - 1)) && block->collision == old_coll) {
  485. todo.append(QPoint(x, y - 1));
  486. }
  487. }
  488. }
  489. void Map::_floodFillElevation(int x, int y, uint elevation) {
  490. QList<QPoint> todo;
  491. todo.append(QPoint(x, y));
  492. while (todo.length()) {
  493. QPoint point = todo.takeAt(0);
  494. x = point.x();
  495. y = point.y();
  496. Block *block = getBlock(x, y);
  497. if (block == NULL) {
  498. continue;
  499. }
  500. uint old_z = block->elevation;
  501. if (old_z == elevation) {
  502. continue;
  503. }
  504. Block block_(*block);
  505. block_.elevation = elevation;
  506. _setBlock(x, y, block_);
  507. if ((block = getBlock(x + 1, y)) && block->elevation == old_z) {
  508. todo.append(QPoint(x + 1, y));
  509. }
  510. if ((block = getBlock(x - 1, y)) && block->elevation == old_z) {
  511. todo.append(QPoint(x - 1, y));
  512. }
  513. if ((block = getBlock(x, y + 1)) && block->elevation == old_z) {
  514. todo.append(QPoint(x, y + 1));
  515. }
  516. if ((block = getBlock(x, y - 1)) && block->elevation == old_z) {
  517. todo.append(QPoint(x, y - 1));
  518. }
  519. }
  520. }
  521. void Map::_floodFillCollisionElevation(int x, int y, uint collision, uint elevation) {
  522. QList<QPoint> todo;
  523. todo.append(QPoint(x, y));
  524. while (todo.length()) {
  525. QPoint point = todo.takeAt(0);
  526. x = point.x();
  527. y = point.y();
  528. Block *block = getBlock(x, y);
  529. if (block == NULL) {
  530. continue;
  531. }
  532. uint old_coll = block->collision;
  533. uint old_elev = block->elevation;
  534. if (old_coll == collision && old_elev == elevation) {
  535. continue;
  536. }
  537. block->collision = collision;
  538. block->elevation = elevation;
  539. _setBlock(x, y, *block);
  540. if ((block = getBlock(x + 1, y)) && block->collision == old_coll && block->elevation == old_elev) {
  541. todo.append(QPoint(x + 1, y));
  542. }
  543. if ((block = getBlock(x - 1, y)) && block->collision == old_coll && block->elevation == old_elev) {
  544. todo.append(QPoint(x - 1, y));
  545. }
  546. if ((block = getBlock(x, y + 1)) && block->collision == old_coll && block->elevation == old_elev) {
  547. todo.append(QPoint(x, y + 1));
  548. }
  549. if ((block = getBlock(x, y - 1)) && block->collision == old_coll && block->elevation == old_elev) {
  550. todo.append(QPoint(x, y - 1));
  551. }
  552. }
  553. }
  554. void Map::undo() {
  555. if (layout->blockdata) {
  556. Blockdata *commit = history.back();
  557. if (commit != NULL) {
  558. layout->blockdata->copyFrom(commit);
  559. emit mapChanged(this);
  560. }
  561. }
  562. }
  563. void Map::redo() {
  564. if (layout->blockdata) {
  565. Blockdata *commit = history.next();
  566. if (commit != NULL) {
  567. layout->blockdata->copyFrom(commit);
  568. emit mapChanged(this);
  569. }
  570. }
  571. }
  572. void Map::commit() {
  573. if (layout->blockdata) {
  574. if (!layout->blockdata->equals(history.current())) {
  575. Blockdata* commit = layout->blockdata->copy();
  576. history.push(commit);
  577. emit mapChanged(this);
  578. }
  579. }
  580. }
  581. void Map::setBlock(int x, int y, Block block) {
  582. Block *old_block = getBlock(x, y);
  583. if (old_block && (*old_block) != block) {
  584. _setBlock(x, y, block);
  585. commit();
  586. }
  587. }
  588. void Map::floodFill(int x, int y, uint tile) {
  589. Block *block = getBlock(x, y);
  590. if (block && block->tile != tile) {
  591. _floodFill(x, y, tile);
  592. commit();
  593. }
  594. }
  595. void Map::floodFillCollision(int x, int y, uint collision) {
  596. Block *block = getBlock(x, y);
  597. if (block && block->collision != collision) {
  598. _floodFillCollision(x, y, collision);
  599. commit();
  600. }
  601. }
  602. void Map::floodFillElevation(int x, int y, uint elevation) {
  603. Block *block = getBlock(x, y);
  604. if (block && block->elevation != elevation) {
  605. _floodFillElevation(x, y, elevation);
  606. commit();
  607. }
  608. }
  609. void Map::floodFillCollisionElevation(int x, int y, uint collision, uint elevation) {
  610. Block *block = getBlock(x, y);
  611. if (block && (block->collision != collision || block->elevation != elevation)) {
  612. _floodFillCollisionElevation(x, y, collision, elevation);
  613. commit();
  614. }
  615. }
  616. QList<Event *> Map::getAllEvents() {
  617. QList<Event*> all;
  618. for (QList<Event*> list : events.values()) {
  619. all += list;
  620. }
  621. return all;
  622. }
  623. void Map::removeEvent(Event *event) {
  624. for (QString key : events.keys()) {
  625. events[key].removeAll(event);
  626. }
  627. }
  628. void Map::addEvent(Event *event) {
  629. events[event->get("event_group_type")].append(event);
  630. }
  631. bool Map::hasUnsavedChanges() {
  632. return !history.isSaved() || !isPersistedToFile || layout->has_unsaved_changes;
  633. }
  634. void Map::hoveredTileChanged(int x, int y, int block) {
  635. emit statusBarMessage(QString("X: %1, Y: %2, Block: 0x%3")
  636. .arg(x)
  637. .arg(y)
  638. .arg(QString("%1").arg(block, 3, 16, QChar('0')).toUpper()));
  639. }
  640. void Map::clearHoveredTile() {
  641. emit statusBarMessage(QString(""));
  642. }
  643. void Map::hoveredMetatileChanged(int blockIndex) {
  644. int tile = getSelectedBlockIndex(blockIndex);
  645. emit statusBarMessage(QString("Block: 0x%1")
  646. .arg(QString("%1").arg(tile, 3, 16, QChar('0')).toUpper()));
  647. }
  648. void Map::clearHoveredMetatile() {
  649. emit statusBarMessage(QString(""));
  650. }
  651. void Map::hoveredCollisionTileChanged(int collision) {
  652. emit statusBarMessage(QString("Collision: %1").arg(collision));
  653. }
  654. void Map::clearHoveredCollisionTile() {
  655. emit statusBarMessage(QString(""));
  656. }
  657. void Map::hoveredElevationTileChanged(int elevation) {
  658. emit statusBarMessage(QString("Elevation: %1").arg(elevation));
  659. }
  660. void Map::clearHoveredElevationTile() {
  661. emit statusBarMessage(QString(""));
  662. }