Nav apraksta

editor.cpp 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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 MovementPermissionsPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {
  302. QPointF pos = event->pos();
  303. int x = ((int)pos.x()) / 16;
  304. int y = ((int)pos.y()) / 16;
  305. int width = pixmap().width() / 16;
  306. int height = pixmap().height() / 16;
  307. if ((x >= 0 && x < width) && (y >=0 && y < height)) {
  308. pick(y * width + x);
  309. }
  310. }
  311. void MovementPermissionsPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event) {
  312. updateCurHoveredMetatile(event->pos());
  313. mousePressEvent(event);
  314. }
  315. void MovementPermissionsPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) {
  316. mousePressEvent(event);
  317. }
  318. void CollisionMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
  319. int x = ((int)pos.x()) / 16;
  320. int y = ((int)pos.y()) / 16;
  321. int width = pixmap().width() / 16;
  322. int height = pixmap().height() / 16;
  323. if (x < 0 || x >= width || y < 0 || y >= height) {
  324. map->clearHoveredCollisionTile();
  325. } else {
  326. int collision = y * width + x;
  327. map->hoveredCollisionTileChanged(collision);
  328. }
  329. }
  330. void ElevationMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
  331. int x = ((int)pos.x()) / 16;
  332. int y = ((int)pos.y()) / 16;
  333. int width = pixmap().width() / 16;
  334. int height = pixmap().height() / 16;
  335. if (x < 0 || x >= width || y < 0 || y >= height) {
  336. map->clearHoveredElevationTile();
  337. } else {
  338. int elevation = y * width + x;
  339. map->hoveredElevationTileChanged(elevation);
  340. }
  341. }
  342. void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
  343. if (map) {
  344. QPointF pos = event->pos();
  345. int x = (int)(pos.x()) / 16;
  346. int y = (int)(pos.y()) / 16;
  347. if (map->smart_paths_enabled) {
  348. paintSmartPath(x, y);
  349. } else {
  350. paintNormal(x, y);
  351. }
  352. if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  353. map->commit();
  354. }
  355. draw();
  356. }
  357. }
  358. void MapPixmapItem::paintNormal(int x, int y) {
  359. // Snap the selected position to the top-left of the block boundary.
  360. // This allows painting via dragging the mouse to tile the painted region.
  361. int xDiff = x - map->paint_tile_initial_x;
  362. int yDiff = y - map->paint_tile_initial_y;
  363. if (xDiff < 0 && xDiff % map->paint_tile_width != 0) xDiff -= map->paint_tile_width;
  364. if (yDiff < 0 && yDiff % map->paint_tile_height != 0) yDiff -= map->paint_tile_height;
  365. x = map->paint_tile_initial_x + (xDiff / map->paint_tile_width) * map->paint_tile_width;
  366. y = map->paint_tile_initial_y + (yDiff / map->paint_tile_height) * map->paint_tile_height;
  367. for (int i = 0; i < map->paint_tile_width && i + x < map->getWidth(); i++)
  368. for (int j = 0; j < map->paint_tile_height && j + y < map->getHeight(); j++) {
  369. int actualX = i + x;
  370. int actualY = j + y;
  371. Block *block = map->getBlock(actualX, actualY);
  372. if (block) {
  373. block->tile = map->paint_tile + i + (j * 8);
  374. map->_setBlock(actualX, actualY, *block);
  375. }
  376. }
  377. }
  378. // These are tile offsets from the top-left tile in the 3x3 smart path selection.
  379. // Each entry is for one possibility from the marching squares value for a tile.
  380. // (Marching Squares: https://en.wikipedia.org/wiki/Marching_squares)
  381. QList<int> MapPixmapItem::smartPathTable = QList<int>({
  382. 8 + 1, // 0000
  383. 8 + 1, // 0001
  384. 8 + 1, // 0010
  385. 16 + 0, // 0011
  386. 8 + 1, // 0100
  387. 8 + 1, // 0101
  388. 0 + 0, // 0110
  389. 8 + 0, // 0111
  390. 8 + 1, // 1000
  391. 16 + 2, // 1001
  392. 8 + 1, // 1010
  393. 16 + 1, // 1011
  394. 0 + 2, // 1100
  395. 8 + 2, // 1101
  396. 0 + 1, // 1110
  397. 8 + 1, // 1111
  398. });
  399. #define IS_SMART_PATH_TILE(block) ((block->tile >= map->paint_tile && block->tile < map->paint_tile + 3) \
  400. || (block->tile >= map->paint_tile + 8 && block->tile < map->paint_tile + 11) \
  401. || (block->tile >= map->paint_tile + 16 && block->tile < map->paint_tile + 19))
  402. void MapPixmapItem::paintSmartPath(int x, int y) {
  403. // Smart path should never be enabled without a 3x3 block selection.
  404. if (map->paint_tile_width != 3 || map->paint_tile_height != 3) return;
  405. // Shift to the middle tile of the smart path selection.
  406. int openTile = map->paint_tile + 8 + 1;
  407. // Fill the region with the open tile.
  408. for (int i = -1; i <= 1 && i + x < map->getWidth() && i + x >= 0; i++)
  409. for (int j = -1; j <= 1 && j + y < map->getHeight() && j + y >= 0; j++) {
  410. int actualX = i + x;
  411. int actualY = j + y;
  412. Block *block = map->getBlock(actualX, actualY);
  413. if (block) {
  414. block->tile = openTile;
  415. map->_setBlock(actualX, actualY, *block);
  416. }
  417. }
  418. // Go back and resolve the edge tiles
  419. for (int i = -2; i <= 2 && i + x < map->getWidth() && i + x >= 0; i++)
  420. for (int j = -2; j <= 2 && j + y < map->getHeight() && j + y >= 0; j++) {
  421. // Ignore the corners, which can't possible be affected by the smart path.
  422. if ((i == -2 && j == -2) || (i == 2 && j == -2) ||
  423. (i == -2 && j == 2) || (i == 2 && j == 2))
  424. continue;
  425. // Ignore tiles that aren't part of the smart path set.
  426. int actualX = i + x;
  427. int actualY = j + y;
  428. Block *block = map->getBlock(actualX, actualY);
  429. if (!block || !IS_SMART_PATH_TILE(block)) {
  430. continue;
  431. }
  432. int id = 0;
  433. Block *top = map->getBlock(actualX, actualY - 1);
  434. Block *right = map->getBlock(actualX + 1, actualY);
  435. Block *bottom = map->getBlock(actualX, actualY + 1);
  436. Block *left = map->getBlock(actualX - 1, actualY);
  437. // Get marching squares value, to determine which tile to use.
  438. if (top && IS_SMART_PATH_TILE(top))
  439. id += 1;
  440. if (right && IS_SMART_PATH_TILE(right))
  441. id += 2;
  442. if (bottom && IS_SMART_PATH_TILE(bottom))
  443. id += 4;
  444. if (left && IS_SMART_PATH_TILE(left))
  445. id += 8;
  446. if (block) {
  447. qDebug() << "tile: " << block->tile << "base: " << map->paint_tile << "id: " << id;
  448. }
  449. block->tile = map->paint_tile + smartPathTable[id];;
  450. map->_setBlock(actualX, actualY, *block);
  451. }
  452. }
  453. void MapPixmapItem::floodFill(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. map->floodFill(x, y, map->paint_tile);
  459. draw();
  460. }
  461. }
  462. void MapPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
  463. QPointF pos = event->pos();
  464. int x = (int)(pos.x()) / 16;
  465. int y = (int)(pos.y()) / 16;
  466. Block *block = map->getBlock(x, y);
  467. if (block) {
  468. map->paint_tile = block->tile;
  469. map->paint_tile_width = 1;
  470. map->paint_tile_height = 1;
  471. emit map->paintTileChanged(map);
  472. }
  473. }
  474. #define SWAP(a, b) do { if (a != b) { a ^= b; b ^= a; a ^= b; } } while (0)
  475. void MapPixmapItem::select(QGraphicsSceneMouseEvent *event) {
  476. QPointF pos = event->pos();
  477. int x = (int)(pos.x()) / 16;
  478. int y = (int)(pos.y()) / 16;
  479. if (event->type() == QEvent::GraphicsSceneMousePress) {
  480. selection_origin = QPoint(x, y);
  481. selection.clear();
  482. } else if (event->type() == QEvent::GraphicsSceneMouseMove) {
  483. if (event->buttons() & Qt::LeftButton) {
  484. selection.clear();
  485. selection.append(QPoint(x, y));
  486. }
  487. } else if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  488. if (!selection.isEmpty()) {
  489. QPoint pos = selection.last();
  490. int x1 = selection_origin.x();
  491. int y1 = selection_origin.y();
  492. int x2 = pos.x();
  493. int y2 = pos.y();
  494. if (x1 > x2) SWAP(x1, x2);
  495. if (y1 > y2) SWAP(y1, y2);
  496. selection.clear();
  497. for (int y = y1; y <= y2; y++) {
  498. for (int x = x1; x <= x2; x++) {
  499. selection.append(QPoint(x, y));
  500. }
  501. }
  502. qDebug() << QString("selected (%1, %2) -> (%3, %4)").arg(x1).arg(y1).arg(x2).arg(y2);
  503. }
  504. }
  505. }
  506. void MapPixmapItem::draw() {
  507. if (map) {
  508. setPixmap(map->render());
  509. }
  510. }
  511. void MapPixmapItem::undo() {
  512. if (map) {
  513. map->undo();
  514. draw();
  515. }
  516. }
  517. void MapPixmapItem::redo() {
  518. if (map) {
  519. map->redo();
  520. draw();
  521. }
  522. }
  523. void MapPixmapItem::updateCurHoveredTile(QPointF pos) {
  524. int x = ((int)pos.x()) / 16;
  525. int y = ((int)pos.y()) / 16;
  526. int blockIndex = y * map->getWidth() + x;
  527. if (x < 0 || x >= map->getWidth() || y < 0 || y >= map->getHeight()) {
  528. map->clearHoveredTile();
  529. } else {
  530. int tile = map->blockdata->blocks->at(blockIndex).tile;
  531. map->hoveredTileChanged(x, y, tile);
  532. }
  533. }
  534. void MapPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
  535. updateCurHoveredTile(event->pos());
  536. }
  537. void MapPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
  538. map->clearHoveredTile();
  539. }
  540. void MapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  541. QPointF pos = event->pos();
  542. int x = ((int)pos.x()) / 16;
  543. int y = ((int)pos.y()) / 16;
  544. map->paint_tile_initial_x = x;
  545. map->paint_tile_initial_y = y;
  546. emit mouseEvent(event, this);
  547. }
  548. void MapPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  549. updateCurHoveredTile(event->pos());
  550. emit mouseEvent(event, this);
  551. }
  552. void MapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  553. emit mouseEvent(event, this);
  554. }
  555. void CollisionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  556. emit mouseEvent(event, this);
  557. }
  558. void CollisionPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  559. emit mouseEvent(event, this);
  560. }
  561. void CollisionPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  562. emit mouseEvent(event, this);
  563. }
  564. void CollisionPixmapItem::draw() {
  565. if (map) {
  566. setPixmap(map->renderCollision());
  567. }
  568. }
  569. void CollisionPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
  570. if (map) {
  571. QPointF pos = event->pos();
  572. int x = (int)(pos.x()) / 16;
  573. int y = (int)(pos.y()) / 16;
  574. Block *block = map->getBlock(x, y);
  575. if (block) {
  576. if (map->paint_collision >= 0) {
  577. block->collision = map->paint_collision;
  578. }
  579. if (map->paint_elevation >= 0) {
  580. block->elevation = map->paint_elevation;
  581. }
  582. map->_setBlock(x, y, *block);
  583. }
  584. if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  585. map->commit();
  586. }
  587. draw();
  588. }
  589. }
  590. void CollisionPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
  591. if (map) {
  592. QPointF pos = event->pos();
  593. int x = (int)(pos.x()) / 16;
  594. int y = (int)(pos.y()) / 16;
  595. bool collision = map->paint_collision >= 0;
  596. bool elevation = map->paint_elevation >= 0;
  597. if (collision && elevation) {
  598. map->floodFillCollisionElevation(x, y, map->paint_collision, map->paint_elevation);
  599. } else if (collision) {
  600. map->floodFillCollision(x, y, map->paint_collision);
  601. } else if (elevation) {
  602. map->floodFillElevation(x, y, map->paint_elevation);
  603. }
  604. draw();
  605. }
  606. }
  607. void CollisionPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
  608. QPointF pos = event->pos();
  609. int x = (int)(pos.x()) / 16;
  610. int y = (int)(pos.y()) / 16;
  611. Block *block = map->getBlock(x, y);
  612. if (block) {
  613. map->paint_collision = block->collision;
  614. map->paint_elevation = block->elevation;
  615. emit map->paintCollisionChanged(map);
  616. }
  617. }
  618. void DraggablePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *mouse) {
  619. active = true;
  620. clicking = true;
  621. last_x = (mouse->pos().x() + this->pos().x()) / 16;
  622. last_y = (mouse->pos().y() + this->pos().y()) / 16;
  623. //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("y"));
  624. }
  625. void DraggablePixmapItem::move(int x, int y) {
  626. event->setX(event->x() + x);
  627. event->setY(event->y() + y);
  628. updatePosition();
  629. emitPositionChanged();
  630. }
  631. void DraggablePixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouse) {
  632. if (active) {
  633. int x = (mouse->pos().x() + this->pos().x()) / 16;
  634. int y = (mouse->pos().y() + this->pos().y()) / 16;
  635. if (x != last_x || y != last_y) {
  636. clicking = false;
  637. if (editor->selected_events->contains(this)) {
  638. for (DraggablePixmapItem *item : *editor->selected_events) {
  639. item->move(x - last_x, y - last_y);
  640. }
  641. } else {
  642. move(x - last_x, y - last_y);
  643. }
  644. last_x = x;
  645. last_y = y;
  646. //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("x"));
  647. }
  648. }
  649. }
  650. void DraggablePixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouse) {
  651. if (clicking) {
  652. this->editor->selectMapObject(this, mouse->modifiers() & Qt::ControlModifier);
  653. this->editor->updateSelectedObjects();
  654. }
  655. active = false;
  656. }
  657. QList<DraggablePixmapItem *> *Editor::getObjects() {
  658. QList<DraggablePixmapItem *> *list = new QList<DraggablePixmapItem *>;
  659. for (Event *event : map->getAllEvents()) {
  660. for (QGraphicsItem *child : objects_group->childItems()) {
  661. DraggablePixmapItem *item = (DraggablePixmapItem *)child;
  662. if (item->event == event) {
  663. list->append(item);
  664. break;
  665. }
  666. }
  667. }
  668. return list;
  669. }
  670. void Editor::redrawObject(DraggablePixmapItem *item) {
  671. if (item) {
  672. item->setPixmap(item->event->pixmap);
  673. if (selected_events && selected_events->contains(item)) {
  674. QImage image = item->pixmap().toImage();
  675. QPainter painter(&image);
  676. painter.setPen(QColor(250, 100, 25));
  677. painter.drawRect(0, 0, image.width() - 1, image.height() - 1);
  678. painter.end();
  679. item->setPixmap(QPixmap::fromImage(image));
  680. }
  681. }
  682. }
  683. void Editor::updateSelectedObjects() {
  684. for (DraggablePixmapItem *item : *(getObjects())) {
  685. redrawObject(item);
  686. }
  687. emit selectedObjectsChanged();
  688. }
  689. void Editor::selectMapObject(DraggablePixmapItem *object) {
  690. selectMapObject(object, false);
  691. }
  692. void Editor::selectMapObject(DraggablePixmapItem *object, bool toggle) {
  693. if (selected_events && object) {
  694. if (selected_events->contains(object)) {
  695. if (toggle) {
  696. selected_events->removeOne(object);
  697. }
  698. } else {
  699. if (!toggle) {
  700. selected_events->clear();
  701. }
  702. selected_events->append(object);
  703. }
  704. updateSelectedObjects();
  705. }
  706. }
  707. DraggablePixmapItem* Editor::addNewEvent() {
  708. return addNewEvent("object");
  709. }
  710. DraggablePixmapItem* Editor::addNewEvent(QString event_type) {
  711. if (project && map) {
  712. Event *event = new Event;
  713. event->put("map_name", map->name);
  714. event->put("event_type", event_type);
  715. map->addEvent(event);
  716. project->loadObjectPixmaps(map->getAllEvents());
  717. DraggablePixmapItem *object = addMapObject(event);
  718. return object;
  719. }
  720. return NULL;
  721. }
  722. void Editor::deleteEvent(Event *event) {
  723. Map *map = project->getMap(event->get("map_name"));
  724. if (map) {
  725. map->removeEvent(event);
  726. }
  727. //selected_events->removeAll(event);
  728. //updateSelectedObjects();
  729. }
  730. // dunno how to detect bubbling. QMouseEvent::isAccepted seems to always be true
  731. // check if selected_events changed instead. this has the side effect of deselecting
  732. // when you click on a selected event, since selected_events doesn't change.
  733. QList<DraggablePixmapItem *> selected_events_test;
  734. bool clicking = false;
  735. void Editor::objectsView_onMousePress(QMouseEvent *event) {
  736. clicking = true;
  737. selected_events_test = *selected_events;
  738. }
  739. void Editor::objectsView_onMouseMove(QMouseEvent *event) {
  740. clicking = false;
  741. }
  742. void Editor::objectsView_onMouseRelease(QMouseEvent *event) {
  743. if (clicking) {
  744. if (selected_events_test.length()) {
  745. if (selected_events_test.length() == selected_events->length()) {
  746. bool deselect = true;
  747. for (int i = 0; i < selected_events_test.length(); i++) {
  748. if (selected_events_test.at(i) != selected_events->at(i)) {
  749. deselect = false;
  750. break;
  751. }
  752. }
  753. if (deselect) {
  754. selected_events->clear();
  755. updateSelectedObjects();
  756. }
  757. }
  758. }
  759. clicking = false;
  760. }
  761. }