No Description

editor.cpp 27KB

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