Açıklama Yok

editor.cpp 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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_index = 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->getSelectedBlockIndex(map->paint_tile_index + 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) ((map->getDisplayedBlockIndex(block->tile) >= map->paint_tile_index && map->getDisplayedBlockIndex(block->tile) < map->paint_tile_index + 3) \
  400. || (map->getDisplayedBlockIndex(block->tile) >= map->paint_tile_index + 8 && map->getDisplayedBlockIndex(block->tile) < map->paint_tile_index + 11) \
  401. || (map->getDisplayedBlockIndex(block->tile) >= map->paint_tile_index + 16 && map->getDisplayedBlockIndex(block->tile) < map->paint_tile_index + 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_index + 8 + 1;
  407. // Fill the region with the open tile.
  408. for (int i = -1; i <= 1; i++)
  409. for (int j = -1; j <= 1; j++) {
  410. // Check if in map bounds.
  411. if (!(i + x < map->getWidth() && i + x >= 0 && j + y < map->getHeight() && j + y >= 0))
  412. continue;
  413. int actualX = i + x;
  414. int actualY = j + y;
  415. Block *block = map->getBlock(actualX, actualY);
  416. if (block) {
  417. block->tile = map->getSelectedBlockIndex(openTile);
  418. map->_setBlock(actualX, actualY, *block);
  419. }
  420. }
  421. // Go back and resolve the edge tiles
  422. for (int i = -2; i <= 2; i++)
  423. for (int j = -2; j <= 2; j++) {
  424. // Check if in map bounds.
  425. if (!(i + x < map->getWidth() && i + x >= 0 && j + y < map->getHeight() && j + y >= 0))
  426. continue;
  427. // Ignore the corners, which can't possible be affected by the smart path.
  428. if ((i == -2 && j == -2) || (i == 2 && j == -2) ||
  429. (i == -2 && j == 2) || (i == 2 && j == 2))
  430. continue;
  431. // Ignore tiles that aren't part of the smart path set.
  432. int actualX = i + x;
  433. int actualY = j + y;
  434. Block *block = map->getBlock(actualX, actualY);
  435. if (!block || !IS_SMART_PATH_TILE(block)) {
  436. continue;
  437. }
  438. int id = 0;
  439. Block *top = map->getBlock(actualX, actualY - 1);
  440. Block *right = map->getBlock(actualX + 1, actualY);
  441. Block *bottom = map->getBlock(actualX, actualY + 1);
  442. Block *left = map->getBlock(actualX - 1, actualY);
  443. // Get marching squares value, to determine which tile to use.
  444. if (top && IS_SMART_PATH_TILE(top))
  445. id += 1;
  446. if (right && IS_SMART_PATH_TILE(right))
  447. id += 2;
  448. if (bottom && IS_SMART_PATH_TILE(bottom))
  449. id += 4;
  450. if (left && IS_SMART_PATH_TILE(left))
  451. id += 8;
  452. block->tile = map->getSelectedBlockIndex(map->paint_tile_index + smartPathTable[id]);
  453. map->_setBlock(actualX, actualY, *block);
  454. }
  455. }
  456. void MapPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
  457. if (map) {
  458. QPointF pos = event->pos();
  459. int x = (int)(pos.x()) / 16;
  460. int y = (int)(pos.y()) / 16;
  461. map->floodFill(x, y, map->getSelectedBlockIndex(map->paint_tile_index));
  462. draw();
  463. }
  464. }
  465. void MapPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
  466. QPointF pos = event->pos();
  467. int x = (int)(pos.x()) / 16;
  468. int y = (int)(pos.y()) / 16;
  469. Block *block = map->getBlock(x, y);
  470. if (block) {
  471. map->paint_tile_index = map->getDisplayedBlockIndex(block->tile);
  472. map->paint_tile_width = 1;
  473. map->paint_tile_height = 1;
  474. emit map->paintTileChanged(map);
  475. }
  476. }
  477. #define SWAP(a, b) do { if (a != b) { a ^= b; b ^= a; a ^= b; } } while (0)
  478. void MapPixmapItem::select(QGraphicsSceneMouseEvent *event) {
  479. QPointF pos = event->pos();
  480. int x = (int)(pos.x()) / 16;
  481. int y = (int)(pos.y()) / 16;
  482. if (event->type() == QEvent::GraphicsSceneMousePress) {
  483. selection_origin = QPoint(x, y);
  484. selection.clear();
  485. } else if (event->type() == QEvent::GraphicsSceneMouseMove) {
  486. if (event->buttons() & Qt::LeftButton) {
  487. selection.clear();
  488. selection.append(QPoint(x, y));
  489. }
  490. } else if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  491. if (!selection.isEmpty()) {
  492. QPoint pos = selection.last();
  493. int x1 = selection_origin.x();
  494. int y1 = selection_origin.y();
  495. int x2 = pos.x();
  496. int y2 = pos.y();
  497. if (x1 > x2) SWAP(x1, x2);
  498. if (y1 > y2) SWAP(y1, y2);
  499. selection.clear();
  500. for (int y = y1; y <= y2; y++) {
  501. for (int x = x1; x <= x2; x++) {
  502. selection.append(QPoint(x, y));
  503. }
  504. }
  505. qDebug() << QString("selected (%1, %2) -> (%3, %4)").arg(x1).arg(y1).arg(x2).arg(y2);
  506. }
  507. }
  508. }
  509. void MapPixmapItem::draw() {
  510. if (map) {
  511. setPixmap(map->render());
  512. }
  513. }
  514. void MapPixmapItem::undo() {
  515. if (map) {
  516. map->undo();
  517. draw();
  518. }
  519. }
  520. void MapPixmapItem::redo() {
  521. if (map) {
  522. map->redo();
  523. draw();
  524. }
  525. }
  526. void MapPixmapItem::updateCurHoveredTile(QPointF pos) {
  527. int x = ((int)pos.x()) / 16;
  528. int y = ((int)pos.y()) / 16;
  529. int blockIndex = y * map->getWidth() + x;
  530. if (x < 0 || x >= map->getWidth() || y < 0 || y >= map->getHeight()) {
  531. map->clearHoveredTile();
  532. } else {
  533. int tile = map->blockdata->blocks->at(blockIndex).tile;
  534. map->hoveredTileChanged(x, y, tile);
  535. }
  536. }
  537. void MapPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
  538. updateCurHoveredTile(event->pos());
  539. }
  540. void MapPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
  541. map->clearHoveredTile();
  542. }
  543. void MapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  544. QPointF pos = event->pos();
  545. int x = ((int)pos.x()) / 16;
  546. int y = ((int)pos.y()) / 16;
  547. map->paint_tile_initial_x = x;
  548. map->paint_tile_initial_y = y;
  549. emit mouseEvent(event, this);
  550. }
  551. void MapPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  552. updateCurHoveredTile(event->pos());
  553. emit mouseEvent(event, this);
  554. }
  555. void MapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  556. emit mouseEvent(event, this);
  557. }
  558. void CollisionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  559. emit mouseEvent(event, this);
  560. }
  561. void CollisionPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  562. emit mouseEvent(event, this);
  563. }
  564. void CollisionPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  565. emit mouseEvent(event, this);
  566. }
  567. void CollisionPixmapItem::draw() {
  568. if (map) {
  569. setPixmap(map->renderCollision());
  570. }
  571. }
  572. void CollisionPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
  573. if (map) {
  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. if (map->paint_collision >= 0) {
  580. block->collision = map->paint_collision;
  581. }
  582. if (map->paint_elevation >= 0) {
  583. block->elevation = map->paint_elevation;
  584. }
  585. map->_setBlock(x, y, *block);
  586. }
  587. if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  588. map->commit();
  589. }
  590. draw();
  591. }
  592. }
  593. void CollisionPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
  594. if (map) {
  595. QPointF pos = event->pos();
  596. int x = (int)(pos.x()) / 16;
  597. int y = (int)(pos.y()) / 16;
  598. bool collision = map->paint_collision >= 0;
  599. bool elevation = map->paint_elevation >= 0;
  600. if (collision && elevation) {
  601. map->floodFillCollisionElevation(x, y, map->paint_collision, map->paint_elevation);
  602. } else if (collision) {
  603. map->floodFillCollision(x, y, map->paint_collision);
  604. } else if (elevation) {
  605. map->floodFillElevation(x, y, map->paint_elevation);
  606. }
  607. draw();
  608. }
  609. }
  610. void CollisionPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
  611. QPointF pos = event->pos();
  612. int x = (int)(pos.x()) / 16;
  613. int y = (int)(pos.y()) / 16;
  614. Block *block = map->getBlock(x, y);
  615. if (block) {
  616. map->paint_collision = block->collision;
  617. map->paint_elevation = block->elevation;
  618. emit map->paintCollisionChanged(map);
  619. }
  620. }
  621. void DraggablePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *mouse) {
  622. active = true;
  623. clicking = true;
  624. last_x = (mouse->pos().x() + this->pos().x()) / 16;
  625. last_y = (mouse->pos().y() + this->pos().y()) / 16;
  626. //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("y"));
  627. }
  628. void DraggablePixmapItem::move(int x, int y) {
  629. event->setX(event->x() + x);
  630. event->setY(event->y() + y);
  631. updatePosition();
  632. emitPositionChanged();
  633. }
  634. void DraggablePixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouse) {
  635. if (active) {
  636. int x = (mouse->pos().x() + this->pos().x()) / 16;
  637. int y = (mouse->pos().y() + this->pos().y()) / 16;
  638. if (x != last_x || y != last_y) {
  639. clicking = false;
  640. if (editor->selected_events->contains(this)) {
  641. for (DraggablePixmapItem *item : *editor->selected_events) {
  642. item->move(x - last_x, y - last_y);
  643. }
  644. } else {
  645. move(x - last_x, y - last_y);
  646. }
  647. last_x = x;
  648. last_y = y;
  649. //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("x"));
  650. }
  651. }
  652. }
  653. void DraggablePixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouse) {
  654. if (clicking) {
  655. this->editor->selectMapObject(this, mouse->modifiers() & Qt::ControlModifier);
  656. this->editor->updateSelectedObjects();
  657. }
  658. active = false;
  659. }
  660. QList<DraggablePixmapItem *> *Editor::getObjects() {
  661. QList<DraggablePixmapItem *> *list = new QList<DraggablePixmapItem *>;
  662. for (Event *event : map->getAllEvents()) {
  663. for (QGraphicsItem *child : objects_group->childItems()) {
  664. DraggablePixmapItem *item = (DraggablePixmapItem *)child;
  665. if (item->event == event) {
  666. list->append(item);
  667. break;
  668. }
  669. }
  670. }
  671. return list;
  672. }
  673. void Editor::redrawObject(DraggablePixmapItem *item) {
  674. if (item) {
  675. item->setPixmap(item->event->pixmap);
  676. if (selected_events && selected_events->contains(item)) {
  677. QImage image = item->pixmap().toImage();
  678. QPainter painter(&image);
  679. painter.setPen(QColor(250, 100, 25));
  680. painter.drawRect(0, 0, image.width() - 1, image.height() - 1);
  681. painter.end();
  682. item->setPixmap(QPixmap::fromImage(image));
  683. }
  684. }
  685. }
  686. void Editor::updateSelectedObjects() {
  687. for (DraggablePixmapItem *item : *(getObjects())) {
  688. redrawObject(item);
  689. }
  690. emit selectedObjectsChanged();
  691. }
  692. void Editor::selectMapObject(DraggablePixmapItem *object) {
  693. selectMapObject(object, false);
  694. }
  695. void Editor::selectMapObject(DraggablePixmapItem *object, bool toggle) {
  696. if (selected_events && object) {
  697. if (selected_events->contains(object)) {
  698. if (toggle) {
  699. selected_events->removeOne(object);
  700. }
  701. } else {
  702. if (!toggle) {
  703. selected_events->clear();
  704. }
  705. selected_events->append(object);
  706. }
  707. updateSelectedObjects();
  708. }
  709. }
  710. DraggablePixmapItem* Editor::addNewEvent() {
  711. return addNewEvent("object");
  712. }
  713. DraggablePixmapItem* Editor::addNewEvent(QString event_type) {
  714. if (project && map) {
  715. Event *event = new Event;
  716. event->put("map_name", map->name);
  717. event->put("event_type", event_type);
  718. map->addEvent(event);
  719. project->loadObjectPixmaps(map->getAllEvents());
  720. DraggablePixmapItem *object = addMapObject(event);
  721. return object;
  722. }
  723. return NULL;
  724. }
  725. void Editor::deleteEvent(Event *event) {
  726. Map *map = project->getMap(event->get("map_name"));
  727. if (map) {
  728. map->removeEvent(event);
  729. }
  730. //selected_events->removeAll(event);
  731. //updateSelectedObjects();
  732. }
  733. // dunno how to detect bubbling. QMouseEvent::isAccepted seems to always be true
  734. // check if selected_events changed instead. this has the side effect of deselecting
  735. // when you click on a selected event, since selected_events doesn't change.
  736. QList<DraggablePixmapItem *> selected_events_test;
  737. bool clicking = false;
  738. void Editor::objectsView_onMousePress(QMouseEvent *event) {
  739. clicking = true;
  740. selected_events_test = *selected_events;
  741. }
  742. void Editor::objectsView_onMouseMove(QMouseEvent *event) {
  743. clicking = false;
  744. }
  745. void Editor::objectsView_onMouseRelease(QMouseEvent *event) {
  746. if (clicking) {
  747. if (selected_events_test.length()) {
  748. if (selected_events_test.length() == selected_events->length()) {
  749. bool deselect = true;
  750. for (int i = 0; i < selected_events_test.length(); i++) {
  751. if (selected_events_test.at(i) != selected_events->at(i)) {
  752. deselect = false;
  753. break;
  754. }
  755. }
  756. if (deselect) {
  757. selected_events->clear();
  758. updateSelectedObjects();
  759. }
  760. }
  761. }
  762. clicking = false;
  763. }
  764. }