No Description

map.cpp 23KB

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