Ingen beskrivning

editor.cpp 21KB

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