Нет описания

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. #include "editor.h"
  2. #include <QCheckBox>
  3. #include <QPainter>
  4. #include <QMouseEvent>
  5. Editor::Editor()
  6. {
  7. selected_events = new QList<DraggablePixmapItem*>;
  8. }
  9. void Editor::saveProject() {
  10. if (project) {
  11. project->saveAllMaps();
  12. project->saveAllDataStructures();
  13. }
  14. }
  15. void Editor::save() {
  16. if (project && map) {
  17. project->saveMap(map);
  18. project->saveAllDataStructures();
  19. }
  20. }
  21. void Editor::undo() {
  22. if (current_view) {
  23. ((MapPixmapItem*)current_view)->undo();
  24. }
  25. }
  26. void Editor::redo() {
  27. if (current_view) {
  28. ((MapPixmapItem*)current_view)->redo();
  29. }
  30. }
  31. void Editor::setEditingMap() {
  32. current_view = map_item;
  33. if (map_item) {
  34. map_item->draw();
  35. map_item->setVisible(true);
  36. map_item->setEnabled(true);
  37. setConnectionsVisibility(true);
  38. }
  39. if (collision_item) {
  40. collision_item->setVisible(false);
  41. }
  42. if (objects_group) {
  43. objects_group->setVisible(false);
  44. }
  45. }
  46. void Editor::setEditingCollision() {
  47. current_view = collision_item;
  48. if (collision_item) {
  49. collision_item->setVisible(true);
  50. setConnectionsVisibility(true);
  51. }
  52. if (map_item) {
  53. map_item->setVisible(false);
  54. }
  55. if (objects_group) {
  56. objects_group->setVisible(false);
  57. }
  58. }
  59. void Editor::setEditingObjects() {
  60. current_view = map_item;
  61. if (objects_group) {
  62. objects_group->setVisible(true);
  63. }
  64. if (map_item) {
  65. map_item->setVisible(true);
  66. map_item->setEnabled(false);
  67. setConnectionsVisibility(true);
  68. }
  69. if (collision_item) {
  70. collision_item->setVisible(false);
  71. }
  72. }
  73. void Editor::setEditingConnections(QString direction) {
  74. current_view = map_item;
  75. if (map_item) {
  76. map_item->draw();
  77. map_item->setVisible(true);
  78. map_item->setEnabled(true);
  79. setConnectionsVisibility(false);
  80. showCurrentConnectionMap(direction);
  81. }
  82. if (collision_item) {
  83. collision_item->setVisible(false);
  84. }
  85. if (objects_group) {
  86. objects_group->setVisible(false);
  87. }
  88. }
  89. void Editor::showCurrentConnectionMap(QString curDirection) {
  90. bool connectionExists = false;
  91. for (Connection* connection : map->connections) {
  92. if (connection->direction != curDirection) continue;
  93. if (connection_item) {
  94. scene->removeItem(connection_item);
  95. delete connection_item;
  96. connection_item = NULL;
  97. }
  98. connectionExists = true;
  99. Map *connected_map = project->getMap(connection->map_name);
  100. QPixmap pixmap = connected_map->renderConnection(*connection);
  101. int offset = connection->offset.toInt(nullptr, 0);
  102. int x = 0, y = 0;
  103. if (connection->direction == "up") {
  104. x = offset * 16;
  105. y = -pixmap.height();
  106. } else if (connection->direction == "down") {
  107. x = offset * 16;
  108. y = map->getHeight() * 16;
  109. } else if (connection->direction == "left") {
  110. x = -pixmap.width();
  111. y = offset * 16;
  112. } else if (connection->direction == "right") {
  113. x = map->getWidth() * 16;
  114. y = offset * 16;
  115. }
  116. connection_item = new ConnectionPixmapItem(pixmap, connection, x, y);
  117. connection_item->setX(x);
  118. connection_item->setY(y);
  119. connection_item->setZValue(21);
  120. scene->addItem(connection_item);
  121. scene->setSceneRect(0, 0, pixmap.width() + map_item->pixmap().width(), pixmap.height() + map_item->pixmap().height());
  122. connect(connection_item, SIGNAL(connectionMoved(int)), this, SLOT(onConnectionOffsetChanged(int)));
  123. onConnectionOffsetChanged(connection->offset.toInt());
  124. break;
  125. }
  126. if (!connectionExists && connection_item) {
  127. scene->removeItem(connection_item);
  128. delete connection_item;
  129. connection_item = NULL;
  130. }
  131. }
  132. void Editor::onConnectionOffsetChanged(int newOffset) {
  133. emit connectionOffsetChanged(newOffset);
  134. }
  135. void Editor::setConnectionsVisibility(bool visible) {
  136. for (QGraphicsPixmapItem* item : map->connection_items) {
  137. item->setVisible(visible);
  138. item->setActive(visible);
  139. }
  140. }
  141. void Editor::setMap(QString map_name) {
  142. if (map_name.isNull()) {
  143. return;
  144. }
  145. if (project) {
  146. map = project->loadMap(map_name);
  147. displayMap();
  148. selected_events->clear();
  149. updateSelectedObjects();
  150. }
  151. }
  152. void Editor::mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item) {
  153. if (map_edit_mode == "paint") {
  154. item->paint(event);
  155. } else if (map_edit_mode == "fill") {
  156. item->floodFill(event);
  157. } else if (map_edit_mode == "pick") {
  158. item->pick(event);
  159. } else if (map_edit_mode == "select") {
  160. item->select(event);
  161. }
  162. }
  163. void Editor::mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixmapItem *item) {
  164. if (map_edit_mode == "paint") {
  165. item->paint(event);
  166. } else if (map_edit_mode == "fill") {
  167. item->floodFill(event);
  168. } else if (map_edit_mode == "pick") {
  169. item->pick(event);
  170. } else if (map_edit_mode == "select") {
  171. item->select(event);
  172. }
  173. }
  174. void Editor::displayMap() {
  175. scene = new QGraphicsScene;
  176. map_item = new MapPixmapItem(map);
  177. connect(map_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*,MapPixmapItem*)),
  178. this, SLOT(mouseEvent_map(QGraphicsSceneMouseEvent*,MapPixmapItem*)));
  179. map_item->draw();
  180. scene->addItem(map_item);
  181. collision_item = new CollisionPixmapItem(map);
  182. connect(collision_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*,CollisionPixmapItem*)),
  183. this, SLOT(mouseEvent_collision(QGraphicsSceneMouseEvent*,CollisionPixmapItem*)));
  184. collision_item->draw();
  185. scene->addItem(collision_item);
  186. objects_group = new EventGroup;
  187. scene->addItem(objects_group);
  188. if (map_item) {
  189. map_item->setVisible(false);
  190. }
  191. if (collision_item) {
  192. collision_item->setVisible(false);
  193. }
  194. if (objects_group) {
  195. objects_group->setVisible(false);
  196. }
  197. int tw = 16;
  198. int th = 16;
  199. scene->setSceneRect(
  200. -6 * tw,
  201. -6 * th,
  202. map_item->pixmap().width() + 12 * tw,
  203. map_item->pixmap().height() + 12 * th
  204. );
  205. displayMetatiles();
  206. displayCollisionMetatiles();
  207. displayElevationMetatiles();
  208. displayMapObjects();
  209. displayMapConnections();
  210. displayMapBorder();
  211. displayMapGrid();
  212. }
  213. void Editor::displayMetatiles() {
  214. scene_metatiles = new QGraphicsScene;
  215. metatiles_item = new MetatilesPixmapItem(map);
  216. metatiles_item->draw();
  217. scene_metatiles->addItem(metatiles_item);
  218. }
  219. void Editor::displayCollisionMetatiles() {
  220. scene_collision_metatiles = new QGraphicsScene;
  221. collision_metatiles_item = new CollisionMetatilesPixmapItem(map);
  222. collision_metatiles_item->draw();
  223. scene_collision_metatiles->addItem(collision_metatiles_item);
  224. }
  225. void Editor::displayElevationMetatiles() {
  226. scene_elevation_metatiles = new QGraphicsScene;
  227. elevation_metatiles_item = new ElevationMetatilesPixmapItem(map);
  228. elevation_metatiles_item->draw();
  229. scene_elevation_metatiles->addItem(elevation_metatiles_item);
  230. }
  231. void Editor::displayMapObjects() {
  232. for (QGraphicsItem *child : objects_group->childItems()) {
  233. objects_group->removeFromGroup(child);
  234. }
  235. QList<Event *> events = map->getAllEvents();
  236. project->loadObjectPixmaps(events);
  237. for (Event *event : events) {
  238. addMapObject(event);
  239. }
  240. //objects_group->setFiltersChildEvents(false);
  241. objects_group->setHandlesChildEvents(false);
  242. emit objectsChanged();
  243. }
  244. DraggablePixmapItem *Editor::addMapObject(Event *event) {
  245. DraggablePixmapItem *object = new DraggablePixmapItem(event);
  246. object->editor = this;
  247. objects_group->addToGroup(object);
  248. return object;
  249. }
  250. void Editor::displayMapConnections() {
  251. for (Connection *connection : map->connections) {
  252. if (connection->direction == "dive" || connection->direction == "emerge") {
  253. continue;
  254. }
  255. Map *connected_map = project->getMap(connection->map_name);
  256. QPixmap pixmap = connected_map->renderConnection(*connection);
  257. int offset = connection->offset.toInt(nullptr, 0);
  258. int x = 0, y = 0;
  259. if (connection->direction == "up") {
  260. x = offset * 16;
  261. y = -pixmap.height();
  262. } else if (connection->direction == "down") {
  263. x = offset * 16;
  264. y = map->getHeight() * 16;
  265. } else if (connection->direction == "left") {
  266. x = -pixmap.width();
  267. y = offset * 16;
  268. } else if (connection->direction == "right") {
  269. x = map->getWidth() * 16;
  270. y = offset * 16;
  271. }
  272. QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
  273. item->setZValue(-1);
  274. item->setX(x);
  275. item->setY(y);
  276. scene->addItem(item);
  277. map->connection_items.insert(connection->direction, item);
  278. }
  279. }
  280. void Editor::displayMapBorder() {
  281. QPixmap pixmap = map->renderBorder();
  282. for (int y = -6; y < map->getHeight() + 6; y += 2)
  283. for (int x = -6; x < map->getWidth() + 6; x += 2) {
  284. QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
  285. item->setX(x * 16);
  286. item->setY(y * 16);
  287. item->setZValue(-2);
  288. scene->addItem(item);
  289. }
  290. }
  291. void Editor::displayMapGrid() {
  292. int pixelWidth = map->getWidth() * 16;
  293. int pixelHeight = map->getHeight() * 16;
  294. for (int i = 0; i <= map->getWidth(); i++) {
  295. int x = i * 16;
  296. QGraphicsLineItem *line = scene->addLine(x, 0, x, pixelHeight);
  297. line->setVisible(gridToggleCheckbox->isChecked());
  298. connect(gridToggleCheckbox, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);});
  299. }
  300. for (int j = 0; j <= map->getHeight(); j++) {
  301. int y = j * 16;
  302. QGraphicsLineItem *line = scene->addLine(0, y, pixelWidth, y);
  303. line->setVisible(gridToggleCheckbox->isChecked());
  304. connect(gridToggleCheckbox, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);});
  305. }
  306. }
  307. void Editor::updateConnectionOffset(int offset) {
  308. connection_item->blockSignals(true);
  309. connection_item->connection->offset = QString::number(offset);
  310. if (connection_item->connection->direction == "up" || connection_item->connection->direction == "down") {
  311. connection_item->setX(connection_item->initialX + (offset - connection_item->initialOffset) * 16);
  312. } else {
  313. connection_item->setY(connection_item->initialY + (offset - connection_item->initialOffset) * 16);
  314. }
  315. connection_item->blockSignals(false);
  316. }
  317. void MetatilesPixmapItem::paintTileChanged(Map *map) {
  318. draw();
  319. }
  320. void MetatilesPixmapItem::draw() {
  321. setPixmap(map->renderMetatiles());
  322. }
  323. void MetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
  324. int x = ((int)pos.x()) / 16;
  325. int y = ((int)pos.y()) / 16;
  326. int width = pixmap().width() / 16;
  327. int height = pixmap().height() / 16;
  328. if (x < 0 || x >= width || y < 0 || y >= height) {
  329. map->clearHoveredMetatile();
  330. } else {
  331. int block = y * width + x;
  332. map->hoveredMetatileChanged(block);
  333. }
  334. }
  335. void MetatilesPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
  336. updateCurHoveredMetatile(event->pos());
  337. }
  338. void MetatilesPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
  339. map->clearHoveredMetatile();
  340. }
  341. void MetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  342. QPointF pos = event->pos();
  343. int x = ((int)pos.x()) / 16;
  344. int y = ((int)pos.y()) / 16;
  345. map->paint_metatile_initial_x = x;
  346. map->paint_metatile_initial_y = y;
  347. updateSelection(event->pos(), event->button());
  348. }
  349. void MetatilesPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  350. updateCurHoveredMetatile(event->pos());
  351. Qt::MouseButton button = event->button();
  352. if (button == Qt::MouseButton::NoButton) {
  353. Qt::MouseButtons heldButtons = event->buttons();
  354. if (heldButtons & Qt::RightButton) {
  355. button = Qt::RightButton;
  356. } else if (heldButtons & Qt::LeftButton) {
  357. button = Qt::LeftButton;
  358. }
  359. }
  360. updateSelection(event->pos(), button);
  361. }
  362. void MetatilesPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  363. updateSelection(event->pos(), event->button());
  364. }
  365. void MetatilesPixmapItem::updateSelection(QPointF pos, Qt::MouseButton button) {
  366. int x = ((int)pos.x()) / 16;
  367. int y = ((int)pos.y()) / 16;
  368. int width = pixmap().width() / 16;
  369. int height = pixmap().height() / 16;
  370. if ((x >= 0 && x < width) && (y >=0 && y < height)) {
  371. int baseTileX = x < map->paint_metatile_initial_x ? x : map->paint_metatile_initial_x;
  372. int baseTileY = y < map->paint_metatile_initial_y ? y : map->paint_metatile_initial_y;
  373. map->paint_tile = baseTileY * 8 + baseTileX;
  374. map->paint_tile_width = abs(map->paint_metatile_initial_x - x) + 1;
  375. map->paint_tile_height = abs(map->paint_metatile_initial_y - y) + 1;
  376. map->smart_paths_enabled = button == Qt::RightButton
  377. && map->paint_tile_width == 3
  378. && map->paint_tile_height == 3;
  379. emit map->paintTileChanged(map);
  380. }
  381. }
  382. void CollisionMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
  383. int x = ((int)pos.x()) / 16;
  384. int y = ((int)pos.y()) / 16;
  385. int width = pixmap().width() / 16;
  386. int height = pixmap().height() / 16;
  387. if (x < 0 || x >= width || y < 0 || y >= height) {
  388. map->clearHoveredCollisionTile();
  389. } else {
  390. int collision = y * width + x;
  391. map->hoveredCollisionTileChanged(collision);
  392. }
  393. }
  394. QVariant ConnectionPixmapItem::itemChange(GraphicsItemChange change, const QVariant &value)
  395. {
  396. if (change == ItemPositionChange) {
  397. QPointF newPos = value.toPointF();
  398. qreal x, y;
  399. int newOffset = initialOffset;
  400. if (connection->direction == "up" || connection->direction == "down") {
  401. x = round(newPos.x() / 16) * 16;
  402. newOffset += (x - initialX) / 16;
  403. }
  404. else {
  405. x = initialX;
  406. }
  407. if (connection->direction == "right" || connection->direction == "left") {
  408. y = round(newPos.y() / 16) * 16;
  409. newOffset += (y - initialY) / 16;
  410. }
  411. else {
  412. y = initialY;
  413. }
  414. emit connectionMoved(newOffset);
  415. connection->offset = QString::number(newOffset);
  416. return QPointF(x, y);
  417. }
  418. else {
  419. return QGraphicsItem::itemChange(change, value);
  420. }
  421. }
  422. void ConnectionPixmapItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
  423. QPointF pos = event->pos();
  424. qDebug() << "enter: " << pos.x() << ", " << pos.y();
  425. }
  426. void ConnectionPixmapItem::dragMoveEvent(QGraphicsSceneDragDropEvent *event) {
  427. QPointF pos = event->pos();
  428. qDebug() << "drag: " << pos.x() << ", " << pos.y();
  429. }
  430. void ConnectionPixmapItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) {
  431. QPointF pos = event->pos();
  432. qDebug() << "leave: " << pos.x() << ", " << pos.y();
  433. }
  434. void ConnectionPixmapItem::dropEvent(QGraphicsSceneDragDropEvent *event) {
  435. QPointF pos = event->pos();
  436. qDebug() << "drop: " << pos.x() << ", " << pos.y();
  437. }
  438. void ConnectionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {
  439. QPointF pos = event->pos();
  440. }
  441. void ElevationMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
  442. int x = ((int)pos.x()) / 16;
  443. int y = ((int)pos.y()) / 16;
  444. int width = pixmap().width() / 16;
  445. int height = pixmap().height() / 16;
  446. if (x < 0 || x >= width || y < 0 || y >= height) {
  447. map->clearHoveredElevationTile();
  448. } else {
  449. int elevation = y * width + x;
  450. map->hoveredElevationTileChanged(elevation);
  451. }
  452. }
  453. void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
  454. if (map) {
  455. QPointF pos = event->pos();
  456. int x = (int)(pos.x()) / 16;
  457. int y = (int)(pos.y()) / 16;
  458. if (map->smart_paths_enabled) {
  459. paintSmartPath(x, y);
  460. } else {
  461. paintNormal(x, y);
  462. }
  463. if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  464. map->commit();
  465. }
  466. draw();
  467. }
  468. }
  469. void MapPixmapItem::paintNormal(int x, int y) {
  470. // Snap the selected position to the top-left of the block boundary.
  471. // This allows painting via dragging the mouse to tile the painted region.
  472. int xDiff = x - map->paint_tile_initial_x;
  473. int yDiff = y - map->paint_tile_initial_y;
  474. if (xDiff < 0 && xDiff % map->paint_tile_width != 0) xDiff -= map->paint_tile_width;
  475. if (yDiff < 0 && yDiff % map->paint_tile_height != 0) yDiff -= map->paint_tile_height;
  476. x = map->paint_tile_initial_x + (xDiff / map->paint_tile_width) * map->paint_tile_width;
  477. y = map->paint_tile_initial_y + (yDiff / map->paint_tile_height) * map->paint_tile_height;
  478. for (int i = 0; i < map->paint_tile_width && i + x < map->getWidth(); i++)
  479. for (int j = 0; j < map->paint_tile_height && j + y < map->getHeight(); j++) {
  480. int actualX = i + x;
  481. int actualY = j + y;
  482. Block *block = map->getBlock(actualX, actualY);
  483. if (block) {
  484. block->tile = map->paint_tile + i + (j * 8);
  485. map->_setBlock(actualX, actualY, *block);
  486. }
  487. }
  488. }
  489. // These are tile offsets from the top-left tile in the 3x3 smart path selection.
  490. // Each entry is for one possibility from the marching squares value for a tile.
  491. // (Marching Squares: https://en.wikipedia.org/wiki/Marching_squares)
  492. QList<int> MapPixmapItem::smartPathTable = QList<int>({
  493. 8 + 1, // 0000
  494. 8 + 1, // 0001
  495. 8 + 1, // 0010
  496. 16 + 0, // 0011
  497. 8 + 1, // 0100
  498. 8 + 1, // 0101
  499. 0 + 0, // 0110
  500. 8 + 0, // 0111
  501. 8 + 1, // 1000
  502. 16 + 2, // 1001
  503. 8 + 1, // 1010
  504. 16 + 1, // 1011
  505. 0 + 2, // 1100
  506. 8 + 2, // 1101
  507. 0 + 1, // 1110
  508. 8 + 1, // 1111
  509. });
  510. #define IS_SMART_PATH_TILE(block) ((block->tile >= map->paint_tile && block->tile < map->paint_tile + 3) \
  511. || (block->tile >= map->paint_tile + 8 && block->tile < map->paint_tile + 11) \
  512. || (block->tile >= map->paint_tile + 16 && block->tile < map->paint_tile + 19))
  513. void MapPixmapItem::paintSmartPath(int x, int y) {
  514. // Smart path should never be enabled without a 3x3 block selection.
  515. if (map->paint_tile_width != 3 || map->paint_tile_height != 3) return;
  516. // Shift to the middle tile of the smart path selection.
  517. int openTile = map->paint_tile + 8 + 1;
  518. // Fill the region with the open tile.
  519. for (int i = -1; i <= 1 && i + x < map->getWidth() && i + x >= 0; i++)
  520. for (int j = -1; j <= 1 && j + y < map->getHeight() && j + y >= 0; j++) {
  521. int actualX = i + x;
  522. int actualY = j + y;
  523. Block *block = map->getBlock(actualX, actualY);
  524. if (block) {
  525. block->tile = openTile;
  526. map->_setBlock(actualX, actualY, *block);
  527. }
  528. }
  529. // Go back and resolve the edge tiles
  530. for (int i = -2; i <= 2 && i + x < map->getWidth() && i + x >= 0; i++)
  531. for (int j = -2; j <= 2 && j + y < map->getHeight() && j + y >= 0; j++) {
  532. // Ignore the corners, which can't possible be affected by the smart path.
  533. if ((i == -2 && j == -2) || (i == 2 && j == -2) ||
  534. (i == -2 && j == 2) || (i == 2 && j == 2))
  535. continue;
  536. // Ignore tiles that aren't part of the smart path set.
  537. int actualX = i + x;
  538. int actualY = j + y;
  539. Block *block = map->getBlock(actualX, actualY);
  540. if (!block || !IS_SMART_PATH_TILE(block)) {
  541. continue;
  542. }
  543. int id = 0;
  544. Block *top = map->getBlock(actualX, actualY - 1);
  545. Block *right = map->getBlock(actualX + 1, actualY);
  546. Block *bottom = map->getBlock(actualX, actualY + 1);
  547. Block *left = map->getBlock(actualX - 1, actualY);
  548. // Get marching squares value, to determine which tile to use.
  549. if (top && IS_SMART_PATH_TILE(top))
  550. id += 1;
  551. if (right && IS_SMART_PATH_TILE(right))
  552. id += 2;
  553. if (bottom && IS_SMART_PATH_TILE(bottom))
  554. id += 4;
  555. if (left && IS_SMART_PATH_TILE(left))
  556. id += 8;
  557. if (block) {
  558. qDebug() << "tile: " << block->tile << "base: " << map->paint_tile << "id: " << id;
  559. }
  560. block->tile = map->paint_tile + smartPathTable[id];;
  561. map->_setBlock(actualX, actualY, *block);
  562. }
  563. }
  564. void MapPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
  565. if (map) {
  566. QPointF pos = event->pos();
  567. int x = (int)(pos.x()) / 16;
  568. int y = (int)(pos.y()) / 16;
  569. map->floodFill(x, y, map->paint_tile);
  570. draw();
  571. }
  572. }
  573. void MapPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
  574. QPointF pos = event->pos();
  575. int x = (int)(pos.x()) / 16;
  576. int y = (int)(pos.y()) / 16;
  577. Block *block = map->getBlock(x, y);
  578. if (block) {
  579. map->paint_tile = block->tile;
  580. map->paint_tile_width = 1;
  581. map->paint_tile_height = 1;
  582. emit map->paintTileChanged(map);
  583. }
  584. }
  585. #define SWAP(a, b) do { if (a != b) { a ^= b; b ^= a; a ^= b; } } while (0)
  586. void MapPixmapItem::select(QGraphicsSceneMouseEvent *event) {
  587. QPointF pos = event->pos();
  588. int x = (int)(pos.x()) / 16;
  589. int y = (int)(pos.y()) / 16;
  590. if (event->type() == QEvent::GraphicsSceneMousePress) {
  591. selection_origin = QPoint(x, y);
  592. selection.clear();
  593. } else if (event->type() == QEvent::GraphicsSceneMouseMove) {
  594. if (event->buttons() & Qt::LeftButton) {
  595. selection.clear();
  596. selection.append(QPoint(x, y));
  597. }
  598. } else if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  599. if (!selection.isEmpty()) {
  600. QPoint pos = selection.last();
  601. int x1 = selection_origin.x();
  602. int y1 = selection_origin.y();
  603. int x2 = pos.x();
  604. int y2 = pos.y();
  605. if (x1 > x2) SWAP(x1, x2);
  606. if (y1 > y2) SWAP(y1, y2);
  607. selection.clear();
  608. for (int y = y1; y <= y2; y++) {
  609. for (int x = x1; x <= x2; x++) {
  610. selection.append(QPoint(x, y));
  611. }
  612. }
  613. qDebug() << QString("selected (%1, %2) -> (%3, %4)").arg(x1).arg(y1).arg(x2).arg(y2);
  614. }
  615. }
  616. }
  617. void MapPixmapItem::draw() {
  618. if (map) {
  619. setPixmap(map->render());
  620. }
  621. }
  622. void MapPixmapItem::undo() {
  623. if (map) {
  624. map->undo();
  625. draw();
  626. }
  627. }
  628. void MapPixmapItem::redo() {
  629. if (map) {
  630. map->redo();
  631. draw();
  632. }
  633. }
  634. void MapPixmapItem::updateCurHoveredTile(QPointF pos) {
  635. int x = ((int)pos.x()) / 16;
  636. int y = ((int)pos.y()) / 16;
  637. int blockIndex = y * map->getWidth() + x;
  638. if (x < 0 || x >= map->getWidth() || y < 0 || y >= map->getHeight()) {
  639. map->clearHoveredTile();
  640. } else {
  641. int tile = map->blockdata->blocks->at(blockIndex).tile;
  642. map->hoveredTileChanged(x, y, tile);
  643. }
  644. }
  645. void MapPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
  646. updateCurHoveredTile(event->pos());
  647. }
  648. void MapPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
  649. map->clearHoveredTile();
  650. }
  651. void MapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  652. QPointF pos = event->pos();
  653. int x = ((int)pos.x()) / 16;
  654. int y = ((int)pos.y()) / 16;
  655. map->paint_tile_initial_x = x;
  656. map->paint_tile_initial_y = y;
  657. emit mouseEvent(event, this);
  658. }
  659. void MapPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  660. updateCurHoveredTile(event->pos());
  661. emit mouseEvent(event, this);
  662. }
  663. void MapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  664. emit mouseEvent(event, this);
  665. }
  666. void CollisionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  667. emit mouseEvent(event, this);
  668. }
  669. void CollisionPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  670. emit mouseEvent(event, this);
  671. }
  672. void CollisionPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  673. emit mouseEvent(event, this);
  674. }
  675. void CollisionPixmapItem::draw() {
  676. if (map) {
  677. setPixmap(map->renderCollision());
  678. }
  679. }
  680. void CollisionPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
  681. if (map) {
  682. QPointF pos = event->pos();
  683. int x = (int)(pos.x()) / 16;
  684. int y = (int)(pos.y()) / 16;
  685. Block *block = map->getBlock(x, y);
  686. if (block) {
  687. if (map->paint_collision >= 0) {
  688. block->collision = map->paint_collision;
  689. }
  690. if (map->paint_elevation >= 0) {
  691. block->elevation = map->paint_elevation;
  692. }
  693. map->_setBlock(x, y, *block);
  694. }
  695. if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  696. map->commit();
  697. }
  698. draw();
  699. }
  700. }
  701. void CollisionPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
  702. if (map) {
  703. QPointF pos = event->pos();
  704. int x = (int)(pos.x()) / 16;
  705. int y = (int)(pos.y()) / 16;
  706. bool collision = map->paint_collision >= 0;
  707. bool elevation = map->paint_elevation >= 0;
  708. if (collision && elevation) {
  709. map->floodFillCollisionElevation(x, y, map->paint_collision, map->paint_elevation);
  710. } else if (collision) {
  711. map->floodFillCollision(x, y, map->paint_collision);
  712. } else if (elevation) {
  713. map->floodFillElevation(x, y, map->paint_elevation);
  714. }
  715. draw();
  716. }
  717. }
  718. void CollisionPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
  719. QPointF pos = event->pos();
  720. int x = (int)(pos.x()) / 16;
  721. int y = (int)(pos.y()) / 16;
  722. Block *block = map->getBlock(x, y);
  723. if (block) {
  724. map->paint_collision = block->collision;
  725. map->paint_elevation = block->elevation;
  726. emit map->paintCollisionChanged(map);
  727. }
  728. }
  729. void DraggablePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *mouse) {
  730. active = true;
  731. clicking = true;
  732. last_x = (mouse->pos().x() + this->pos().x()) / 16;
  733. last_y = (mouse->pos().y() + this->pos().y()) / 16;
  734. //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("y"));
  735. }
  736. void DraggablePixmapItem::move(int x, int y) {
  737. event->setX(event->x() + x);
  738. event->setY(event->y() + y);
  739. updatePosition();
  740. emitPositionChanged();
  741. }
  742. void DraggablePixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouse) {
  743. if (active) {
  744. int x = (mouse->pos().x() + this->pos().x()) / 16;
  745. int y = (mouse->pos().y() + this->pos().y()) / 16;
  746. if (x != last_x || y != last_y) {
  747. clicking = false;
  748. if (editor->selected_events->contains(this)) {
  749. for (DraggablePixmapItem *item : *editor->selected_events) {
  750. item->move(x - last_x, y - last_y);
  751. }
  752. } else {
  753. move(x - last_x, y - last_y);
  754. }
  755. last_x = x;
  756. last_y = y;
  757. //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("x"));
  758. }
  759. }
  760. }
  761. void DraggablePixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouse) {
  762. if (clicking) {
  763. this->editor->selectMapObject(this, mouse->modifiers() & Qt::ControlModifier);
  764. this->editor->updateSelectedObjects();
  765. }
  766. active = false;
  767. }
  768. QList<DraggablePixmapItem *> *Editor::getObjects() {
  769. QList<DraggablePixmapItem *> *list = new QList<DraggablePixmapItem *>;
  770. for (Event *event : map->getAllEvents()) {
  771. for (QGraphicsItem *child : objects_group->childItems()) {
  772. DraggablePixmapItem *item = (DraggablePixmapItem *)child;
  773. if (item->event == event) {
  774. list->append(item);
  775. break;
  776. }
  777. }
  778. }
  779. return list;
  780. }
  781. void Editor::redrawObject(DraggablePixmapItem *item) {
  782. if (item) {
  783. item->setPixmap(item->event->pixmap);
  784. if (selected_events && selected_events->contains(item)) {
  785. QImage image = item->pixmap().toImage();
  786. QPainter painter(&image);
  787. painter.setPen(QColor(250, 100, 25));
  788. painter.drawRect(0, 0, image.width() - 1, image.height() - 1);
  789. painter.end();
  790. item->setPixmap(QPixmap::fromImage(image));
  791. }
  792. }
  793. }
  794. void Editor::updateSelectedObjects() {
  795. for (DraggablePixmapItem *item : *(getObjects())) {
  796. redrawObject(item);
  797. }
  798. emit selectedObjectsChanged();
  799. }
  800. void Editor::selectMapObject(DraggablePixmapItem *object) {
  801. selectMapObject(object, false);
  802. }
  803. void Editor::selectMapObject(DraggablePixmapItem *object, bool toggle) {
  804. if (selected_events && object) {
  805. if (selected_events->contains(object)) {
  806. if (toggle) {
  807. selected_events->removeOne(object);
  808. }
  809. } else {
  810. if (!toggle) {
  811. selected_events->clear();
  812. }
  813. selected_events->append(object);
  814. }
  815. updateSelectedObjects();
  816. }
  817. }
  818. DraggablePixmapItem* Editor::addNewEvent() {
  819. return addNewEvent("object");
  820. }
  821. DraggablePixmapItem* Editor::addNewEvent(QString event_type) {
  822. if (project && map) {
  823. Event *event = new Event;
  824. event->put("map_name", map->name);
  825. event->put("event_type", event_type);
  826. map->addEvent(event);
  827. project->loadObjectPixmaps(map->getAllEvents());
  828. DraggablePixmapItem *object = addMapObject(event);
  829. return object;
  830. }
  831. return NULL;
  832. }
  833. void Editor::deleteEvent(Event *event) {
  834. Map *map = project->getMap(event->get("map_name"));
  835. if (map) {
  836. map->removeEvent(event);
  837. }
  838. //selected_events->removeAll(event);
  839. //updateSelectedObjects();
  840. }
  841. // dunno how to detect bubbling. QMouseEvent::isAccepted seems to always be true
  842. // check if selected_events changed instead. this has the side effect of deselecting
  843. // when you click on a selected event, since selected_events doesn't change.
  844. QList<DraggablePixmapItem *> selected_events_test;
  845. bool clicking = false;
  846. void Editor::objectsView_onMousePress(QMouseEvent *event) {
  847. clicking = true;
  848. selected_events_test = *selected_events;
  849. }
  850. void Editor::objectsView_onMouseMove(QMouseEvent *event) {
  851. clicking = false;
  852. }
  853. void Editor::objectsView_onMouseRelease(QMouseEvent *event) {
  854. if (clicking) {
  855. if (selected_events_test.length()) {
  856. if (selected_events_test.length() == selected_events->length()) {
  857. bool deselect = true;
  858. for (int i = 0; i < selected_events_test.length(); i++) {
  859. if (selected_events_test.at(i) != selected_events->at(i)) {
  860. deselect = false;
  861. break;
  862. }
  863. }
  864. if (deselect) {
  865. selected_events->clear();
  866. updateSelectedObjects();
  867. }
  868. }
  869. }
  870. clicking = false;
  871. }
  872. }