설명 없음

editor.cpp 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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());
  267. }
  268. void MetatilesPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  269. updateCurHoveredMetatile(event->pos());
  270. updateSelection(event->pos());
  271. }
  272. void MetatilesPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  273. updateSelection(event->pos());
  274. }
  275. void MetatilesPixmapItem::updateSelection(QPointF pos) {
  276. int x = ((int)pos.x()) / 16;
  277. int y = ((int)pos.y()) / 16;
  278. int width = pixmap().width() / 16;
  279. int height = pixmap().height() / 16;
  280. if ((x >= 0 && x < width) && (y >=0 && y < height)) {
  281. int baseTileX = x < map->paint_metatile_initial_x ? x : map->paint_metatile_initial_x;
  282. int baseTileY = y < map->paint_metatile_initial_y ? y : map->paint_metatile_initial_y;
  283. map->paint_tile = baseTileY * 8 + baseTileX;
  284. map->paint_tile_width = abs(map->paint_metatile_initial_x - x) + 1;
  285. map->paint_tile_height = abs(map->paint_metatile_initial_y - y) + 1;
  286. emit map->paintTileChanged(map);
  287. }
  288. }
  289. void CollisionMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
  290. int x = ((int)pos.x()) / 16;
  291. int y = ((int)pos.y()) / 16;
  292. int width = pixmap().width() / 16;
  293. int height = pixmap().height() / 16;
  294. if (x < 0 || x >= width || y < 0 || y >= height) {
  295. map->clearHoveredCollisionTile();
  296. } else {
  297. int collision = y * width + x;
  298. map->hoveredCollisionTileChanged(collision);
  299. }
  300. }
  301. void ElevationMetatilesPixmapItem::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->clearHoveredElevationTile();
  308. } else {
  309. int elevation = y * width + x;
  310. map->hoveredElevationTileChanged(elevation);
  311. }
  312. }
  313. void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
  314. if (map) {
  315. QPointF pos = event->pos();
  316. int x = (int)(pos.x()) / 16;
  317. int y = (int)(pos.y()) / 16;
  318. // Snap the selected position to the top-left of the block boundary.
  319. // This allows painting via dragging the mouse to tile the painted region.
  320. int xDiff = x - map->paint_tile_initial_x;
  321. int yDiff = y - map->paint_tile_initial_y;
  322. if (xDiff < 0 && xDiff % map->paint_tile_width != 0) xDiff -= map->paint_tile_width;
  323. if (yDiff < 0 && yDiff % map->paint_tile_height != 0) yDiff -= map->paint_tile_height;
  324. x = map->paint_tile_initial_x + (xDiff / map->paint_tile_width) * map->paint_tile_width;
  325. y = map->paint_tile_initial_y + (yDiff / map->paint_tile_height) * map->paint_tile_height;
  326. for (int i = 0; i < map->paint_tile_width && i + x < map->getWidth(); i++)
  327. for (int j = 0; j < map->paint_tile_height && j + y < map->getHeight(); j++) {
  328. int actualX = i + x;
  329. int actualY = j + y;
  330. Block *block = map->getBlock(actualX, actualY);
  331. if (block) {
  332. block->tile = map->paint_tile + i + (j * 8);
  333. map->_setBlock(actualX, actualY, *block);
  334. }
  335. }
  336. if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  337. map->commit();
  338. }
  339. draw();
  340. }
  341. }
  342. void MapPixmapItem::floodFill(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. map->floodFill(x, y, map->paint_tile);
  348. draw();
  349. }
  350. }
  351. void MapPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
  352. QPointF pos = event->pos();
  353. int x = (int)(pos.x()) / 16;
  354. int y = (int)(pos.y()) / 16;
  355. Block *block = map->getBlock(x, y);
  356. if (block) {
  357. map->paint_tile = block->tile;
  358. map->paint_tile_width = 1;
  359. map->paint_tile_height = 1;
  360. emit map->paintTileChanged(map);
  361. }
  362. }
  363. #define SWAP(a, b) do { if (a != b) { a ^= b; b ^= a; a ^= b; } } while (0)
  364. void MapPixmapItem::select(QGraphicsSceneMouseEvent *event) {
  365. QPointF pos = event->pos();
  366. int x = (int)(pos.x()) / 16;
  367. int y = (int)(pos.y()) / 16;
  368. if (event->type() == QEvent::GraphicsSceneMousePress) {
  369. selection_origin = QPoint(x, y);
  370. selection.clear();
  371. } else if (event->type() == QEvent::GraphicsSceneMouseMove) {
  372. if (event->buttons() & Qt::LeftButton) {
  373. selection.clear();
  374. selection.append(QPoint(x, y));
  375. }
  376. } else if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  377. if (!selection.isEmpty()) {
  378. QPoint pos = selection.last();
  379. int x1 = selection_origin.x();
  380. int y1 = selection_origin.y();
  381. int x2 = pos.x();
  382. int y2 = pos.y();
  383. if (x1 > x2) SWAP(x1, x2);
  384. if (y1 > y2) SWAP(y1, y2);
  385. selection.clear();
  386. for (int y = y1; y <= y2; y++) {
  387. for (int x = x1; x <= x2; x++) {
  388. selection.append(QPoint(x, y));
  389. }
  390. }
  391. qDebug() << QString("selected (%1, %2) -> (%3, %4)").arg(x1).arg(y1).arg(x2).arg(y2);
  392. }
  393. }
  394. }
  395. void MapPixmapItem::draw() {
  396. if (map) {
  397. setPixmap(map->render());
  398. }
  399. }
  400. void MapPixmapItem::undo() {
  401. if (map) {
  402. map->undo();
  403. draw();
  404. }
  405. }
  406. void MapPixmapItem::redo() {
  407. if (map) {
  408. map->redo();
  409. draw();
  410. }
  411. }
  412. void MapPixmapItem::updateCurHoveredTile(QPointF pos) {
  413. int x = ((int)pos.x()) / 16;
  414. int y = ((int)pos.y()) / 16;
  415. int blockIndex = y * map->getWidth() + x;
  416. if (x < 0 || x >= map->getWidth() || y < 0 || y >= map->getHeight()) {
  417. map->clearHoveredTile();
  418. } else {
  419. int tile = map->blockdata->blocks->at(blockIndex).tile;
  420. map->hoveredTileChanged(x, y, tile);
  421. }
  422. }
  423. void MapPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
  424. updateCurHoveredTile(event->pos());
  425. }
  426. void MapPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
  427. map->clearHoveredTile();
  428. }
  429. void MapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  430. QPointF pos = event->pos();
  431. int x = ((int)pos.x()) / 16;
  432. int y = ((int)pos.y()) / 16;
  433. map->paint_tile_initial_x = x;
  434. map->paint_tile_initial_y = y;
  435. emit mouseEvent(event, this);
  436. }
  437. void MapPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  438. updateCurHoveredTile(event->pos());
  439. emit mouseEvent(event, this);
  440. }
  441. void MapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  442. emit mouseEvent(event, this);
  443. }
  444. void CollisionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  445. emit mouseEvent(event, this);
  446. }
  447. void CollisionPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  448. emit mouseEvent(event, this);
  449. }
  450. void CollisionPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  451. emit mouseEvent(event, this);
  452. }
  453. void CollisionPixmapItem::draw() {
  454. if (map) {
  455. setPixmap(map->renderCollision());
  456. }
  457. }
  458. void CollisionPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
  459. if (map) {
  460. QPointF pos = event->pos();
  461. int x = (int)(pos.x()) / 16;
  462. int y = (int)(pos.y()) / 16;
  463. Block *block = map->getBlock(x, y);
  464. if (block) {
  465. if (map->paint_collision >= 0) {
  466. block->collision = map->paint_collision;
  467. }
  468. if (map->paint_elevation >= 0) {
  469. block->elevation = map->paint_elevation;
  470. }
  471. map->_setBlock(x, y, *block);
  472. }
  473. if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  474. map->commit();
  475. }
  476. draw();
  477. }
  478. }
  479. void CollisionPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
  480. if (map) {
  481. QPointF pos = event->pos();
  482. int x = (int)(pos.x()) / 16;
  483. int y = (int)(pos.y()) / 16;
  484. bool collision = map->paint_collision >= 0;
  485. bool elevation = map->paint_elevation >= 0;
  486. if (collision && elevation) {
  487. map->floodFillCollisionElevation(x, y, map->paint_collision, map->paint_elevation);
  488. } else if (collision) {
  489. map->floodFillCollision(x, y, map->paint_collision);
  490. } else if (elevation) {
  491. map->floodFillElevation(x, y, map->paint_elevation);
  492. }
  493. draw();
  494. }
  495. }
  496. void CollisionPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
  497. QPointF pos = event->pos();
  498. int x = (int)(pos.x()) / 16;
  499. int y = (int)(pos.y()) / 16;
  500. Block *block = map->getBlock(x, y);
  501. if (block) {
  502. map->paint_collision = block->collision;
  503. map->paint_elevation = block->elevation;
  504. emit map->paintCollisionChanged(map);
  505. }
  506. }
  507. void DraggablePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *mouse) {
  508. active = true;
  509. clicking = true;
  510. last_x = (mouse->pos().x() + this->pos().x()) / 16;
  511. last_y = (mouse->pos().y() + this->pos().y()) / 16;
  512. //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("y"));
  513. }
  514. void DraggablePixmapItem::move(int x, int y) {
  515. event->setX(event->x() + x);
  516. event->setY(event->y() + y);
  517. updatePosition();
  518. emitPositionChanged();
  519. }
  520. void DraggablePixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouse) {
  521. if (active) {
  522. int x = (mouse->pos().x() + this->pos().x()) / 16;
  523. int y = (mouse->pos().y() + this->pos().y()) / 16;
  524. if (x != last_x || y != last_y) {
  525. clicking = false;
  526. if (editor->selected_events->contains(this)) {
  527. for (DraggablePixmapItem *item : *editor->selected_events) {
  528. item->move(x - last_x, y - last_y);
  529. }
  530. } else {
  531. move(x - last_x, y - last_y);
  532. }
  533. last_x = x;
  534. last_y = y;
  535. //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("x"));
  536. }
  537. }
  538. }
  539. void DraggablePixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouse) {
  540. if (clicking) {
  541. this->editor->selectMapObject(this, mouse->modifiers() & Qt::ControlModifier);
  542. this->editor->updateSelectedObjects();
  543. }
  544. active = false;
  545. }
  546. QList<DraggablePixmapItem *> *Editor::getObjects() {
  547. QList<DraggablePixmapItem *> *list = new QList<DraggablePixmapItem *>;
  548. for (Event *event : map->getAllEvents()) {
  549. for (QGraphicsItem *child : objects_group->childItems()) {
  550. DraggablePixmapItem *item = (DraggablePixmapItem *)child;
  551. if (item->event == event) {
  552. list->append(item);
  553. break;
  554. }
  555. }
  556. }
  557. return list;
  558. }
  559. void Editor::redrawObject(DraggablePixmapItem *item) {
  560. if (item) {
  561. item->setPixmap(item->event->pixmap);
  562. if (selected_events && selected_events->contains(item)) {
  563. QImage image = item->pixmap().toImage();
  564. QPainter painter(&image);
  565. painter.setPen(QColor(250, 100, 25));
  566. painter.drawRect(0, 0, image.width() - 1, image.height() - 1);
  567. painter.end();
  568. item->setPixmap(QPixmap::fromImage(image));
  569. }
  570. }
  571. }
  572. void Editor::updateSelectedObjects() {
  573. for (DraggablePixmapItem *item : *(getObjects())) {
  574. redrawObject(item);
  575. }
  576. emit selectedObjectsChanged();
  577. }
  578. void Editor::selectMapObject(DraggablePixmapItem *object) {
  579. selectMapObject(object, false);
  580. }
  581. void Editor::selectMapObject(DraggablePixmapItem *object, bool toggle) {
  582. if (selected_events && object) {
  583. if (selected_events->contains(object)) {
  584. if (toggle) {
  585. selected_events->removeOne(object);
  586. }
  587. } else {
  588. if (!toggle) {
  589. selected_events->clear();
  590. }
  591. selected_events->append(object);
  592. }
  593. updateSelectedObjects();
  594. }
  595. }
  596. DraggablePixmapItem* Editor::addNewEvent() {
  597. return addNewEvent("object");
  598. }
  599. DraggablePixmapItem* Editor::addNewEvent(QString event_type) {
  600. if (project && map) {
  601. Event *event = new Event;
  602. event->put("map_name", map->name);
  603. event->put("event_type", event_type);
  604. map->addEvent(event);
  605. project->loadObjectPixmaps(map->getAllEvents());
  606. DraggablePixmapItem *object = addMapObject(event);
  607. return object;
  608. }
  609. return NULL;
  610. }
  611. void Editor::deleteEvent(Event *event) {
  612. Map *map = project->getMap(event->get("map_name"));
  613. if (map) {
  614. map->removeEvent(event);
  615. }
  616. //selected_events->removeAll(event);
  617. //updateSelectedObjects();
  618. }
  619. // dunno how to detect bubbling. QMouseEvent::isAccepted seems to always be true
  620. // check if selected_events changed instead. this has the side effect of deselecting
  621. // when you click on a selected event, since selected_events doesn't change.
  622. QList<DraggablePixmapItem *> selected_events_test;
  623. bool clicking = false;
  624. void Editor::objectsView_onMousePress(QMouseEvent *event) {
  625. clicking = true;
  626. selected_events_test = *selected_events;
  627. }
  628. void Editor::objectsView_onMouseMove(QMouseEvent *event) {
  629. clicking = false;
  630. }
  631. void Editor::objectsView_onMouseRelease(QMouseEvent *event) {
  632. if (clicking) {
  633. if (selected_events_test.length()) {
  634. if (selected_events_test.length() == selected_events->length()) {
  635. bool deselect = true;
  636. for (int i = 0; i < selected_events_test.length(); i++) {
  637. if (selected_events_test.at(i) != selected_events->at(i)) {
  638. deselect = false;
  639. break;
  640. }
  641. }
  642. if (deselect) {
  643. selected_events->clear();
  644. updateSelectedObjects();
  645. }
  646. }
  647. }
  648. clicking = false;
  649. }
  650. }