Нет описания

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. #include "editor.h"
  2. #include <QCheckBox>
  3. #include <QPainter>
  4. #include <QMouseEvent>
  5. Editor::Editor(Ui::MainWindow* ui)
  6. {
  7. this->ui = ui;
  8. selected_events = new QList<DraggablePixmapItem*>;
  9. }
  10. void Editor::saveProject() {
  11. if (project) {
  12. project->saveAllMaps();
  13. project->saveAllDataStructures();
  14. }
  15. }
  16. void Editor::save() {
  17. if (project && map) {
  18. project->saveMap(map);
  19. project->saveAllDataStructures();
  20. }
  21. }
  22. void Editor::undo() {
  23. if (current_view) {
  24. ((MapPixmapItem*)current_view)->undo();
  25. }
  26. }
  27. void Editor::redo() {
  28. if (current_view) {
  29. ((MapPixmapItem*)current_view)->redo();
  30. }
  31. }
  32. void Editor::setEditingMap() {
  33. current_view = map_item;
  34. if (map_item) {
  35. displayMapConnections();
  36. map_item->draw();
  37. map_item->setVisible(true);
  38. map_item->setEnabled(true);
  39. setConnectionsVisibility(true);
  40. }
  41. if (collision_item) {
  42. collision_item->setVisible(false);
  43. }
  44. if (objects_group) {
  45. objects_group->setVisible(false);
  46. }
  47. setBorderItemsVisible(true);
  48. setConnectionItemsVisible(false);
  49. }
  50. void Editor::setEditingCollision() {
  51. current_view = collision_item;
  52. if (collision_item) {
  53. collision_item->setVisible(true);
  54. setConnectionsVisibility(true);
  55. }
  56. if (map_item) {
  57. map_item->setVisible(false);
  58. }
  59. if (objects_group) {
  60. objects_group->setVisible(false);
  61. }
  62. setBorderItemsVisible(true);
  63. setConnectionItemsVisible(false);
  64. }
  65. void Editor::setEditingObjects() {
  66. current_view = map_item;
  67. if (objects_group) {
  68. objects_group->setVisible(true);
  69. }
  70. if (map_item) {
  71. map_item->setVisible(true);
  72. map_item->setEnabled(false);
  73. setConnectionsVisibility(true);
  74. }
  75. if (collision_item) {
  76. collision_item->setVisible(false);
  77. }
  78. setBorderItemsVisible(true);
  79. setConnectionItemsVisible(false);
  80. }
  81. void Editor::setEditingConnections() {
  82. current_view = map_item;
  83. if (map_item) {
  84. map_item->draw();
  85. map_item->setVisible(true);
  86. map_item->setEnabled(false);
  87. populateConnectionMapPickers();
  88. ui->label_NumConnections->setText(QString::number(map->connections.length()));
  89. setConnectionsVisibility(false);
  90. setDiveEmergeControls();
  91. if (current_connection_edit_item) {
  92. onConnectionOffsetChanged(current_connection_edit_item->connection->offset.toInt());
  93. updateConnectionMap(current_connection_edit_item->connection->map_name, current_connection_edit_item->connection->direction);
  94. }
  95. }
  96. if (collision_item) {
  97. collision_item->setVisible(false);
  98. }
  99. if (objects_group) {
  100. objects_group->setVisible(false);
  101. }
  102. setBorderItemsVisible(true, 0.4);
  103. setConnectionItemsVisible(true);
  104. }
  105. void Editor::setDiveEmergeControls() {
  106. ui->comboBox_DiveMap->blockSignals(true);
  107. ui->comboBox_EmergeMap->blockSignals(true);
  108. ui->comboBox_DiveMap->setCurrentText("");
  109. ui->comboBox_EmergeMap->setCurrentText("");
  110. for (Connection* connection : map->connections) {
  111. if (connection->direction == "dive") {
  112. ui->comboBox_DiveMap->setCurrentText(connection->map_name);
  113. } else if (connection->direction == "emerge") {
  114. ui->comboBox_EmergeMap->setCurrentText(connection->map_name);
  115. }
  116. }
  117. ui->comboBox_DiveMap->blockSignals(false);
  118. ui->comboBox_EmergeMap->blockSignals(false);
  119. }
  120. void Editor::populateConnectionMapPickers() {
  121. ui->comboBox_ConnectedMap->blockSignals(true);
  122. ui->comboBox_DiveMap->blockSignals(true);
  123. ui->comboBox_EmergeMap->blockSignals(true);
  124. ui->comboBox_ConnectedMap->clear();
  125. ui->comboBox_ConnectedMap->addItems(*project->mapNames);
  126. ui->comboBox_DiveMap->clear();
  127. ui->comboBox_DiveMap->addItems(*project->mapNames);
  128. ui->comboBox_EmergeMap->clear();
  129. ui->comboBox_EmergeMap->addItems(*project->mapNames);
  130. ui->comboBox_ConnectedMap->blockSignals(false);
  131. ui->comboBox_DiveMap->blockSignals(true);
  132. ui->comboBox_EmergeMap->blockSignals(true);
  133. }
  134. void Editor::setConnectionItemsVisible(bool visible) {
  135. for (ConnectionPixmapItem* item : connection_edit_items) {
  136. item->setVisible(visible);
  137. item->setEnabled(visible);
  138. }
  139. }
  140. void Editor::setBorderItemsVisible(bool visible, qreal opacity) {
  141. for (QGraphicsPixmapItem* item : borderItems) {
  142. item->setVisible(visible);
  143. item->setOpacity(opacity);
  144. }
  145. }
  146. void Editor::setCurrentConnectionDirection(QString curDirection) {
  147. if (!current_connection_edit_item)
  148. return;
  149. current_connection_edit_item->connection->direction = curDirection;
  150. Map *connected_map = project->getMap(current_connection_edit_item->connection->map_name);
  151. QPixmap pixmap = connected_map->renderConnection(*current_connection_edit_item->connection);
  152. int offset = current_connection_edit_item->connection->offset.toInt(nullptr, 0);
  153. current_connection_edit_item->initialOffset = offset;
  154. int x = 0, y = 0;
  155. if (current_connection_edit_item->connection->direction == "up") {
  156. x = offset * 16;
  157. y = -pixmap.height();
  158. } else if (current_connection_edit_item->connection->direction == "down") {
  159. x = offset * 16;
  160. y = map->getHeight() * 16;
  161. } else if (current_connection_edit_item->connection->direction == "left") {
  162. x = -pixmap.width();
  163. y = offset * 16;
  164. } else if (current_connection_edit_item->connection->direction == "right") {
  165. x = map->getWidth() * 16;
  166. y = offset * 16;
  167. }
  168. current_connection_edit_item->basePixmap = pixmap;
  169. QPainter painter(&pixmap);
  170. painter.setPen(QColor(255, 0, 255));
  171. painter.drawRect(0, 0, pixmap.width() - 1, pixmap.height() - 1);
  172. painter.end();
  173. current_connection_edit_item->setPixmap(pixmap);
  174. current_connection_edit_item->initialX = x;
  175. current_connection_edit_item->initialY = y;
  176. current_connection_edit_item->blockSignals(true);
  177. current_connection_edit_item->setX(x);
  178. current_connection_edit_item->setY(y);
  179. current_connection_edit_item->setZValue(-1);
  180. current_connection_edit_item->blockSignals(false);
  181. setConnectionEditControlValues(current_connection_edit_item->connection);
  182. }
  183. void Editor::onConnectionOffsetChanged(int newOffset) {
  184. ui->spinBox_ConnectionOffset->blockSignals(true);
  185. ui->spinBox_ConnectionOffset->setValue(newOffset);
  186. ui->spinBox_ConnectionOffset->blockSignals(false);
  187. }
  188. void Editor::setConnectionEditControlValues(Connection* connection) {
  189. QString mapName = connection ? connection->map_name : "";
  190. QString direction = connection ? connection->direction : "";
  191. int offset = connection ? connection->offset.toInt() : 0;
  192. ui->comboBox_ConnectedMap->blockSignals(true);
  193. ui->comboBox_ConnectionDirection->blockSignals(true);
  194. ui->spinBox_ConnectionOffset->blockSignals(true);
  195. ui->comboBox_ConnectedMap->setCurrentText(mapName);
  196. ui->comboBox_ConnectionDirection->setCurrentText(direction);
  197. ui->spinBox_ConnectionOffset->setValue(offset);
  198. ui->comboBox_ConnectedMap->blockSignals(false);
  199. ui->comboBox_ConnectionDirection->blockSignals(false);
  200. ui->spinBox_ConnectionOffset->blockSignals(false);
  201. }
  202. void Editor::setConnectionEditControlsEnabled(bool enabled) {
  203. ui->comboBox_ConnectionDirection->setEnabled(enabled);
  204. ui->comboBox_ConnectedMap->setEnabled(enabled);
  205. ui->spinBox_ConnectionOffset->setEnabled(enabled);
  206. if (!enabled) {
  207. setConnectionEditControlValues(false);
  208. }
  209. }
  210. void Editor::onConnectionItemSelected(ConnectionPixmapItem* connectionItem) {
  211. if (!connectionItem)
  212. return;
  213. for (ConnectionPixmapItem* item : connection_edit_items) {
  214. bool isSelectedItem = item == connectionItem;
  215. int zValue = isSelectedItem ? 0 : -1;
  216. qreal opacity = isSelectedItem ? 1 : 0.75;
  217. item->setZValue(zValue);
  218. item->render(opacity);
  219. if (isSelectedItem) {
  220. QPixmap pixmap = item->pixmap();
  221. QPainter painter(&pixmap);
  222. painter.setPen(QColor(255, 0, 255));
  223. painter.drawRect(0, 0, pixmap.width() - 1, pixmap.height() - 1);
  224. painter.end();
  225. item->setPixmap(pixmap);
  226. }
  227. }
  228. current_connection_edit_item = connectionItem;
  229. setConnectionEditControlsEnabled(true);
  230. setConnectionEditControlValues(current_connection_edit_item->connection);
  231. ui->spinBox_ConnectionOffset->setMaximum(current_connection_edit_item->getMaxOffset());
  232. ui->spinBox_ConnectionOffset->setMinimum(current_connection_edit_item->getMinOffset());
  233. }
  234. void Editor::setSelectedConnectionFromMap(QString mapName) {
  235. // Search for the first connection that connects to the given map map.
  236. for (ConnectionPixmapItem* item : connection_edit_items) {
  237. if (item->connection->map_name == mapName) {
  238. onConnectionItemSelected(item);
  239. break;
  240. }
  241. }
  242. }
  243. void Editor::onConnectionItemDoubleClicked(ConnectionPixmapItem* connectionItem) {
  244. emit loadMapRequested(connectionItem->connection->map_name, map->name);
  245. }
  246. void Editor::onConnectionDirectionChanged(QString newDirection) {
  247. ui->comboBox_ConnectionDirection->blockSignals(true);
  248. ui->comboBox_ConnectionDirection->setCurrentText(newDirection);
  249. ui->comboBox_ConnectionDirection->blockSignals(false);
  250. }
  251. void Editor::setConnectionsVisibility(bool visible) {
  252. for (QGraphicsPixmapItem* item : map->connection_items) {
  253. item->setVisible(visible);
  254. item->setActive(visible);
  255. }
  256. }
  257. void Editor::setMap(QString map_name) {
  258. if (map_name.isNull()) {
  259. return;
  260. }
  261. if (project) {
  262. map = project->loadMap(map_name);
  263. displayMap();
  264. selected_events->clear();
  265. updateSelectedObjects();
  266. }
  267. }
  268. void Editor::mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item) {
  269. if (map_edit_mode == "paint") {
  270. item->paint(event);
  271. } else if (map_edit_mode == "fill") {
  272. item->floodFill(event);
  273. } else if (map_edit_mode == "pick") {
  274. item->pick(event);
  275. } else if (map_edit_mode == "select") {
  276. item->select(event);
  277. }
  278. }
  279. void Editor::mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixmapItem *item) {
  280. if (map_edit_mode == "paint") {
  281. item->paint(event);
  282. } else if (map_edit_mode == "fill") {
  283. item->floodFill(event);
  284. } else if (map_edit_mode == "pick") {
  285. item->pick(event);
  286. } else if (map_edit_mode == "select") {
  287. item->select(event);
  288. }
  289. }
  290. void Editor::displayMap() {
  291. scene = new QGraphicsScene;
  292. map_item = new MapPixmapItem(map);
  293. connect(map_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*,MapPixmapItem*)),
  294. this, SLOT(mouseEvent_map(QGraphicsSceneMouseEvent*,MapPixmapItem*)));
  295. map_item->draw();
  296. scene->addItem(map_item);
  297. collision_item = new CollisionPixmapItem(map);
  298. connect(collision_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*,CollisionPixmapItem*)),
  299. this, SLOT(mouseEvent_collision(QGraphicsSceneMouseEvent*,CollisionPixmapItem*)));
  300. collision_item->draw();
  301. scene->addItem(collision_item);
  302. objects_group = new EventGroup;
  303. scene->addItem(objects_group);
  304. if (map_item) {
  305. map_item->setVisible(false);
  306. }
  307. if (collision_item) {
  308. collision_item->setVisible(false);
  309. }
  310. if (objects_group) {
  311. objects_group->setVisible(false);
  312. }
  313. int tw = 16;
  314. int th = 16;
  315. scene->setSceneRect(
  316. -6 * tw,
  317. -6 * th,
  318. map_item->pixmap().width() + 12 * tw,
  319. map_item->pixmap().height() + 12 * th
  320. );
  321. displayMetatiles();
  322. displayCollisionMetatiles();
  323. displayElevationMetatiles();
  324. displayMapObjects();
  325. displayMapConnections();
  326. displayMapBorder();
  327. displayMapGrid();
  328. }
  329. void Editor::displayMetatiles() {
  330. scene_metatiles = new QGraphicsScene;
  331. metatiles_item = new MetatilesPixmapItem(map);
  332. metatiles_item->draw();
  333. scene_metatiles->addItem(metatiles_item);
  334. }
  335. void Editor::displayCollisionMetatiles() {
  336. scene_collision_metatiles = new QGraphicsScene;
  337. collision_metatiles_item = new CollisionMetatilesPixmapItem(map);
  338. collision_metatiles_item->draw();
  339. scene_collision_metatiles->addItem(collision_metatiles_item);
  340. }
  341. void Editor::displayElevationMetatiles() {
  342. scene_elevation_metatiles = new QGraphicsScene;
  343. elevation_metatiles_item = new ElevationMetatilesPixmapItem(map);
  344. elevation_metatiles_item->draw();
  345. scene_elevation_metatiles->addItem(elevation_metatiles_item);
  346. }
  347. void Editor::displayMapObjects() {
  348. for (QGraphicsItem *child : objects_group->childItems()) {
  349. objects_group->removeFromGroup(child);
  350. }
  351. QList<Event *> events = map->getAllEvents();
  352. project->loadObjectPixmaps(events);
  353. for (Event *event : events) {
  354. addMapObject(event);
  355. }
  356. //objects_group->setFiltersChildEvents(false);
  357. objects_group->setHandlesChildEvents(false);
  358. emit objectsChanged();
  359. }
  360. DraggablePixmapItem *Editor::addMapObject(Event *event) {
  361. DraggablePixmapItem *object = new DraggablePixmapItem(event);
  362. object->editor = this;
  363. objects_group->addToGroup(object);
  364. return object;
  365. }
  366. void Editor::displayMapConnections() {
  367. for (QGraphicsPixmapItem* item : map->connection_items) {
  368. scene->removeItem(item);
  369. delete item;
  370. }
  371. map->connection_items.clear();
  372. for (ConnectionPixmapItem* item : connection_edit_items) {
  373. delete item;
  374. }
  375. current_connection_edit_item = NULL;
  376. connection_edit_items.clear();
  377. for (Connection *connection : map->connections) {
  378. if (connection->direction == "dive" || connection->direction == "emerge") {
  379. continue;
  380. }
  381. createConnectionItem(connection, false);
  382. }
  383. if (!connection_edit_items.empty()) {
  384. onConnectionItemSelected(connection_edit_items.first());
  385. }
  386. }
  387. void Editor::createConnectionItem(Connection* connection, bool hide) {
  388. Map *connected_map = project->getMap(connection->map_name);
  389. QPixmap pixmap = connected_map->renderConnection(*connection);
  390. int offset = connection->offset.toInt(nullptr, 0);
  391. int x = 0, y = 0;
  392. if (connection->direction == "up") {
  393. x = offset * 16;
  394. y = -pixmap.height();
  395. } else if (connection->direction == "down") {
  396. x = offset * 16;
  397. y = map->getHeight() * 16;
  398. } else if (connection->direction == "left") {
  399. x = -pixmap.width();
  400. y = offset * 16;
  401. } else if (connection->direction == "right") {
  402. x = map->getWidth() * 16;
  403. y = offset * 16;
  404. }
  405. QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
  406. item->setZValue(-1);
  407. item->setX(x);
  408. item->setY(y);
  409. scene->addItem(item);
  410. map->connection_items.append(item);
  411. item->setVisible(!hide);
  412. ConnectionPixmapItem *connection_edit_item = new ConnectionPixmapItem(pixmap, connection, x, y, map->getWidth(), map->getHeight());
  413. connection_edit_item->setX(x);
  414. connection_edit_item->setY(y);
  415. connection_edit_item->setZValue(-1);
  416. scene->addItem(connection_edit_item);
  417. connect(connection_edit_item, SIGNAL(connectionMoved(int)), this, SLOT(onConnectionOffsetChanged(int)));
  418. connect(connection_edit_item, SIGNAL(connectionItemSelected(ConnectionPixmapItem*)), this, SLOT(onConnectionItemSelected(ConnectionPixmapItem*)));
  419. connect(connection_edit_item, SIGNAL(connectionItemDoubleClicked(ConnectionPixmapItem*)), this, SLOT(onConnectionItemDoubleClicked(ConnectionPixmapItem*)));
  420. connection_edit_items.append(connection_edit_item);
  421. }
  422. void Editor::displayMapBorder() {
  423. QPixmap pixmap = map->renderBorder();
  424. for (int y = -6; y < map->getHeight() + 6; y += 2)
  425. for (int x = -6; x < map->getWidth() + 6; x += 2) {
  426. QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
  427. item->setX(x * 16);
  428. item->setY(y * 16);
  429. item->setZValue(-2);
  430. scene->addItem(item);
  431. borderItems.append(item);
  432. }
  433. }
  434. void Editor::displayMapGrid() {
  435. int pixelWidth = map->getWidth() * 16;
  436. int pixelHeight = map->getHeight() * 16;
  437. for (int i = 0; i <= map->getWidth(); i++) {
  438. int x = i * 16;
  439. QGraphicsLineItem *line = scene->addLine(x, 0, x, pixelHeight);
  440. line->setVisible(ui->checkBox_ToggleGrid->isChecked());
  441. connect(ui->checkBox_ToggleGrid, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);});
  442. }
  443. for (int j = 0; j <= map->getHeight(); j++) {
  444. int y = j * 16;
  445. QGraphicsLineItem *line = scene->addLine(0, y, pixelWidth, y);
  446. line->setVisible(ui->checkBox_ToggleGrid->isChecked());
  447. connect(ui->checkBox_ToggleGrid, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);});
  448. }
  449. }
  450. void Editor::updateConnectionOffset(int offset) {
  451. if (!current_connection_edit_item)
  452. return;
  453. current_connection_edit_item->blockSignals(true);
  454. offset = qMin(offset, current_connection_edit_item->getMaxOffset());
  455. offset = qMax(offset, current_connection_edit_item->getMinOffset());
  456. current_connection_edit_item->connection->offset = QString::number(offset);
  457. if (current_connection_edit_item->connection->direction == "up" || current_connection_edit_item->connection->direction == "down") {
  458. current_connection_edit_item->setX(current_connection_edit_item->initialX + (offset - current_connection_edit_item->initialOffset) * 16);
  459. } else {
  460. current_connection_edit_item->setY(current_connection_edit_item->initialY + (offset - current_connection_edit_item->initialOffset) * 16);
  461. }
  462. current_connection_edit_item->blockSignals(false);
  463. }
  464. void Editor::updateConnectionMap(QString mapName, QString direction) {
  465. if (!mapName.isEmpty() && !project->mapNames->contains(mapName)) {
  466. qDebug() << "Invalid map name " << mapName << " specified for connection.";
  467. return;
  468. }
  469. if (!current_connection_edit_item)
  470. return;
  471. if (mapName.isEmpty()) {
  472. removeCurrentConnection();
  473. return;
  474. } else {
  475. setConnectionEditControlsEnabled(true);
  476. }
  477. current_connection_edit_item->connection->map_name = mapName;
  478. setCurrentConnectionDirection(direction);
  479. }
  480. void Editor::addNewConnection() {
  481. // Find direction with least number of connections.
  482. QMap<QString, int> directionCounts = QMap<QString, int>({{"up", 0}, {"right", 0}, {"down", 0}, {"left", 0}});
  483. for (Connection* connection : map->connections) {
  484. directionCounts[connection->direction]++;
  485. }
  486. QString minDirection = "up";
  487. int minCount = INT_MAX;
  488. for (QString direction : directionCounts.keys()) {
  489. if (directionCounts[direction] < minCount) {
  490. minDirection = direction;
  491. minCount = directionCounts[direction];
  492. }
  493. }
  494. Connection* newConnection = new Connection;
  495. newConnection->direction = minDirection;
  496. newConnection->offset = "0";
  497. newConnection->map_name = project->mapNames->first();
  498. map->connections.append(newConnection);
  499. createConnectionItem(newConnection, true);
  500. onConnectionItemSelected(connection_edit_items.last());
  501. ui->label_NumConnections->setText(QString::number(map->connections.length()));
  502. }
  503. void Editor::removeCurrentConnection() {
  504. if (!current_connection_edit_item)
  505. return;
  506. map->connections.removeOne(current_connection_edit_item->connection);
  507. connection_edit_items.removeOne(current_connection_edit_item);
  508. scene->removeItem(current_connection_edit_item);
  509. delete current_connection_edit_item;
  510. current_connection_edit_item = NULL;
  511. setConnectionEditControlsEnabled(false);
  512. ui->spinBox_ConnectionOffset->setValue(0);
  513. ui->label_NumConnections->setText(QString::number(map->connections.length()));
  514. if (connection_edit_items.length() > 0) {
  515. onConnectionItemSelected(connection_edit_items.last());
  516. }
  517. }
  518. void Editor::updateDiveMap(QString mapName) {
  519. updateDiveEmergeMap(mapName, "dive");
  520. }
  521. void Editor::updateEmergeMap(QString mapName) {
  522. updateDiveEmergeMap(mapName, "emerge");
  523. }
  524. void Editor::updateDiveEmergeMap(QString mapName, QString direction) {
  525. if (!mapName.isEmpty() && !project->mapNamesToMapConstants->contains(mapName)) {
  526. qDebug() << "Invalid " << direction << " map connection: " << mapName;
  527. return;
  528. }
  529. Connection* connection = NULL;
  530. for (Connection* conn : map->connections) {
  531. if (conn->direction == direction) {
  532. connection = conn;
  533. break;
  534. }
  535. }
  536. if (mapName.isEmpty()) {
  537. // Remove dive/emerge connection
  538. if (connection) {
  539. map->connections.removeOne(connection);
  540. }
  541. } else {
  542. if (!connection) {
  543. connection = new Connection;
  544. connection->direction = direction;
  545. connection->offset = "0";
  546. map->connections.append(connection);
  547. }
  548. connection->map_name = mapName;
  549. }
  550. ui->label_NumConnections->setText(QString::number(map->connections.length()));
  551. }
  552. void MetatilesPixmapItem::paintTileChanged(Map *map) {
  553. draw();
  554. }
  555. void MetatilesPixmapItem::draw() {
  556. setPixmap(map->renderMetatiles());
  557. }
  558. void MetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
  559. int x = ((int)pos.x()) / 16;
  560. int y = ((int)pos.y()) / 16;
  561. int width = pixmap().width() / 16;
  562. int height = pixmap().height() / 16;
  563. if (x < 0 || x >= width || y < 0 || y >= height) {
  564. map->clearHoveredMetatile();
  565. } else {
  566. int block = y * width + x;
  567. map->hoveredMetatileChanged(block);
  568. }
  569. }
  570. void MetatilesPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
  571. updateCurHoveredMetatile(event->pos());
  572. }
  573. void MetatilesPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
  574. map->clearHoveredMetatile();
  575. }
  576. void MetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  577. QPointF pos = event->pos();
  578. int x = ((int)pos.x()) / 16;
  579. int y = ((int)pos.y()) / 16;
  580. map->paint_metatile_initial_x = x;
  581. map->paint_metatile_initial_y = y;
  582. updateSelection(event->pos(), event->button());
  583. }
  584. void MetatilesPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  585. updateCurHoveredMetatile(event->pos());
  586. Qt::MouseButton button = event->button();
  587. if (button == Qt::MouseButton::NoButton) {
  588. Qt::MouseButtons heldButtons = event->buttons();
  589. if (heldButtons & Qt::RightButton) {
  590. button = Qt::RightButton;
  591. } else if (heldButtons & Qt::LeftButton) {
  592. button = Qt::LeftButton;
  593. }
  594. }
  595. updateSelection(event->pos(), button);
  596. }
  597. void MetatilesPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  598. updateSelection(event->pos(), event->button());
  599. }
  600. void MetatilesPixmapItem::updateSelection(QPointF pos, Qt::MouseButton button) {
  601. int x = ((int)pos.x()) / 16;
  602. int y = ((int)pos.y()) / 16;
  603. int width = pixmap().width() / 16;
  604. int height = pixmap().height() / 16;
  605. if ((x >= 0 && x < width) && (y >=0 && y < height)) {
  606. int baseTileX = x < map->paint_metatile_initial_x ? x : map->paint_metatile_initial_x;
  607. int baseTileY = y < map->paint_metatile_initial_y ? y : map->paint_metatile_initial_y;
  608. map->paint_tile = baseTileY * 8 + baseTileX;
  609. map->paint_tile_width = abs(map->paint_metatile_initial_x - x) + 1;
  610. map->paint_tile_height = abs(map->paint_metatile_initial_y - y) + 1;
  611. map->smart_paths_enabled = button == Qt::RightButton
  612. && map->paint_tile_width == 3
  613. && map->paint_tile_height == 3;
  614. emit map->paintTileChanged(map);
  615. }
  616. }
  617. void CollisionMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
  618. int x = ((int)pos.x()) / 16;
  619. int y = ((int)pos.y()) / 16;
  620. int width = pixmap().width() / 16;
  621. int height = pixmap().height() / 16;
  622. if (x < 0 || x >= width || y < 0 || y >= height) {
  623. map->clearHoveredCollisionTile();
  624. } else {
  625. int collision = y * width + x;
  626. map->hoveredCollisionTileChanged(collision);
  627. }
  628. }
  629. int ConnectionPixmapItem::getMinOffset() {
  630. if (connection->direction == "up" || connection->direction == "down")
  631. return 1 - (this->pixmap().width() / 16);
  632. else
  633. return 1 - (this->pixmap().height() / 16);
  634. }
  635. int ConnectionPixmapItem::getMaxOffset() {
  636. if (connection->direction == "up" || connection->direction == "down")
  637. return baseMapWidth - 1;
  638. else
  639. return baseMapHeight - 1;
  640. }
  641. QVariant ConnectionPixmapItem::itemChange(GraphicsItemChange change, const QVariant &value)
  642. {
  643. if (change == ItemPositionChange) {
  644. QPointF newPos = value.toPointF();
  645. qreal x, y;
  646. int newOffset = initialOffset;
  647. if (connection->direction == "up" || connection->direction == "down") {
  648. x = round(newPos.x() / 16) * 16;
  649. newOffset += (x - initialX) / 16;
  650. newOffset = qMin(newOffset, this->getMaxOffset());
  651. newOffset = qMax(newOffset, this->getMinOffset());
  652. x = newOffset * 16;
  653. }
  654. else {
  655. x = initialX;
  656. }
  657. if (connection->direction == "right" || connection->direction == "left") {
  658. y = round(newPos.y() / 16) * 16;
  659. newOffset += (y - initialY) / 16;
  660. newOffset = qMin(newOffset, this->getMaxOffset());
  661. newOffset = qMax(newOffset, this->getMinOffset());
  662. y = newOffset * 16;
  663. }
  664. else {
  665. y = initialY;
  666. }
  667. emit connectionMoved(newOffset);
  668. connection->offset = QString::number(newOffset);
  669. return QPointF(x, y);
  670. }
  671. else {
  672. return QGraphicsItem::itemChange(change, value);
  673. }
  674. }
  675. void ConnectionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {
  676. emit connectionItemSelected(this);
  677. }
  678. void ConnectionPixmapItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent*) {
  679. emit connectionItemDoubleClicked(this);
  680. }
  681. void ElevationMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
  682. int x = ((int)pos.x()) / 16;
  683. int y = ((int)pos.y()) / 16;
  684. int width = pixmap().width() / 16;
  685. int height = pixmap().height() / 16;
  686. if (x < 0 || x >= width || y < 0 || y >= height) {
  687. map->clearHoveredElevationTile();
  688. } else {
  689. int elevation = y * width + x;
  690. map->hoveredElevationTileChanged(elevation);
  691. }
  692. }
  693. void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
  694. if (map) {
  695. QPointF pos = event->pos();
  696. int x = (int)(pos.x()) / 16;
  697. int y = (int)(pos.y()) / 16;
  698. if (map->smart_paths_enabled) {
  699. paintSmartPath(x, y);
  700. } else {
  701. paintNormal(x, y);
  702. }
  703. if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  704. map->commit();
  705. }
  706. draw();
  707. }
  708. }
  709. void MapPixmapItem::paintNormal(int x, int y) {
  710. // Snap the selected position to the top-left of the block boundary.
  711. // This allows painting via dragging the mouse to tile the painted region.
  712. int xDiff = x - map->paint_tile_initial_x;
  713. int yDiff = y - map->paint_tile_initial_y;
  714. if (xDiff < 0 && xDiff % map->paint_tile_width != 0) xDiff -= map->paint_tile_width;
  715. if (yDiff < 0 && yDiff % map->paint_tile_height != 0) yDiff -= map->paint_tile_height;
  716. x = map->paint_tile_initial_x + (xDiff / map->paint_tile_width) * map->paint_tile_width;
  717. y = map->paint_tile_initial_y + (yDiff / map->paint_tile_height) * map->paint_tile_height;
  718. for (int i = 0; i < map->paint_tile_width && i + x < map->getWidth(); i++)
  719. for (int j = 0; j < map->paint_tile_height && j + y < map->getHeight(); j++) {
  720. int actualX = i + x;
  721. int actualY = j + y;
  722. Block *block = map->getBlock(actualX, actualY);
  723. if (block) {
  724. block->tile = map->paint_tile + i + (j * 8);
  725. map->_setBlock(actualX, actualY, *block);
  726. }
  727. }
  728. }
  729. // These are tile offsets from the top-left tile in the 3x3 smart path selection.
  730. // Each entry is for one possibility from the marching squares value for a tile.
  731. // (Marching Squares: https://en.wikipedia.org/wiki/Marching_squares)
  732. QList<int> MapPixmapItem::smartPathTable = QList<int>({
  733. 8 + 1, // 0000
  734. 8 + 1, // 0001
  735. 8 + 1, // 0010
  736. 16 + 0, // 0011
  737. 8 + 1, // 0100
  738. 8 + 1, // 0101
  739. 0 + 0, // 0110
  740. 8 + 0, // 0111
  741. 8 + 1, // 1000
  742. 16 + 2, // 1001
  743. 8 + 1, // 1010
  744. 16 + 1, // 1011
  745. 0 + 2, // 1100
  746. 8 + 2, // 1101
  747. 0 + 1, // 1110
  748. 8 + 1, // 1111
  749. });
  750. #define IS_SMART_PATH_TILE(block) ((block->tile >= map->paint_tile && block->tile < map->paint_tile + 3) \
  751. || (block->tile >= map->paint_tile + 8 && block->tile < map->paint_tile + 11) \
  752. || (block->tile >= map->paint_tile + 16 && block->tile < map->paint_tile + 19))
  753. void MapPixmapItem::paintSmartPath(int x, int y) {
  754. // Smart path should never be enabled without a 3x3 block selection.
  755. if (map->paint_tile_width != 3 || map->paint_tile_height != 3) return;
  756. // Shift to the middle tile of the smart path selection.
  757. int openTile = map->paint_tile + 8 + 1;
  758. // Fill the region with the open tile.
  759. for (int i = -1; i <= 1 && i + x < map->getWidth() && i + x >= 0; i++)
  760. for (int j = -1; j <= 1 && j + y < map->getHeight() && j + y >= 0; j++) {
  761. int actualX = i + x;
  762. int actualY = j + y;
  763. Block *block = map->getBlock(actualX, actualY);
  764. if (block) {
  765. block->tile = openTile;
  766. map->_setBlock(actualX, actualY, *block);
  767. }
  768. }
  769. // Go back and resolve the edge tiles
  770. for (int i = -2; i <= 2 && i + x < map->getWidth() && i + x >= 0; i++)
  771. for (int j = -2; j <= 2 && j + y < map->getHeight() && j + y >= 0; j++) {
  772. // Ignore the corners, which can't possible be affected by the smart path.
  773. if ((i == -2 && j == -2) || (i == 2 && j == -2) ||
  774. (i == -2 && j == 2) || (i == 2 && j == 2))
  775. continue;
  776. // Ignore tiles that aren't part of the smart path set.
  777. int actualX = i + x;
  778. int actualY = j + y;
  779. Block *block = map->getBlock(actualX, actualY);
  780. if (!block || !IS_SMART_PATH_TILE(block)) {
  781. continue;
  782. }
  783. int id = 0;
  784. Block *top = map->getBlock(actualX, actualY - 1);
  785. Block *right = map->getBlock(actualX + 1, actualY);
  786. Block *bottom = map->getBlock(actualX, actualY + 1);
  787. Block *left = map->getBlock(actualX - 1, actualY);
  788. // Get marching squares value, to determine which tile to use.
  789. if (top && IS_SMART_PATH_TILE(top))
  790. id += 1;
  791. if (right && IS_SMART_PATH_TILE(right))
  792. id += 2;
  793. if (bottom && IS_SMART_PATH_TILE(bottom))
  794. id += 4;
  795. if (left && IS_SMART_PATH_TILE(left))
  796. id += 8;
  797. block->tile = map->paint_tile + smartPathTable[id];;
  798. map->_setBlock(actualX, actualY, *block);
  799. }
  800. }
  801. void MapPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
  802. if (map) {
  803. QPointF pos = event->pos();
  804. int x = (int)(pos.x()) / 16;
  805. int y = (int)(pos.y()) / 16;
  806. map->floodFill(x, y, map->paint_tile);
  807. draw();
  808. }
  809. }
  810. void MapPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
  811. QPointF pos = event->pos();
  812. int x = (int)(pos.x()) / 16;
  813. int y = (int)(pos.y()) / 16;
  814. Block *block = map->getBlock(x, y);
  815. if (block) {
  816. map->paint_tile = block->tile;
  817. map->paint_tile_width = 1;
  818. map->paint_tile_height = 1;
  819. emit map->paintTileChanged(map);
  820. }
  821. }
  822. #define SWAP(a, b) do { if (a != b) { a ^= b; b ^= a; a ^= b; } } while (0)
  823. void MapPixmapItem::select(QGraphicsSceneMouseEvent *event) {
  824. QPointF pos = event->pos();
  825. int x = (int)(pos.x()) / 16;
  826. int y = (int)(pos.y()) / 16;
  827. if (event->type() == QEvent::GraphicsSceneMousePress) {
  828. selection_origin = QPoint(x, y);
  829. selection.clear();
  830. } else if (event->type() == QEvent::GraphicsSceneMouseMove) {
  831. if (event->buttons() & Qt::LeftButton) {
  832. selection.clear();
  833. selection.append(QPoint(x, y));
  834. }
  835. } else if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  836. if (!selection.isEmpty()) {
  837. QPoint pos = selection.last();
  838. int x1 = selection_origin.x();
  839. int y1 = selection_origin.y();
  840. int x2 = pos.x();
  841. int y2 = pos.y();
  842. if (x1 > x2) SWAP(x1, x2);
  843. if (y1 > y2) SWAP(y1, y2);
  844. selection.clear();
  845. for (int y = y1; y <= y2; y++) {
  846. for (int x = x1; x <= x2; x++) {
  847. selection.append(QPoint(x, y));
  848. }
  849. }
  850. qDebug() << QString("selected (%1, %2) -> (%3, %4)").arg(x1).arg(y1).arg(x2).arg(y2);
  851. }
  852. }
  853. }
  854. void MapPixmapItem::draw() {
  855. if (map) {
  856. setPixmap(map->render());
  857. }
  858. }
  859. void MapPixmapItem::undo() {
  860. if (map) {
  861. map->undo();
  862. draw();
  863. }
  864. }
  865. void MapPixmapItem::redo() {
  866. if (map) {
  867. map->redo();
  868. draw();
  869. }
  870. }
  871. void MapPixmapItem::updateCurHoveredTile(QPointF pos) {
  872. int x = ((int)pos.x()) / 16;
  873. int y = ((int)pos.y()) / 16;
  874. int blockIndex = y * map->getWidth() + x;
  875. if (x < 0 || x >= map->getWidth() || y < 0 || y >= map->getHeight()) {
  876. map->clearHoveredTile();
  877. } else {
  878. int tile = map->blockdata->blocks->at(blockIndex).tile;
  879. map->hoveredTileChanged(x, y, tile);
  880. }
  881. }
  882. void MapPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
  883. updateCurHoveredTile(event->pos());
  884. }
  885. void MapPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
  886. map->clearHoveredTile();
  887. }
  888. void MapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  889. QPointF pos = event->pos();
  890. int x = ((int)pos.x()) / 16;
  891. int y = ((int)pos.y()) / 16;
  892. map->paint_tile_initial_x = x;
  893. map->paint_tile_initial_y = y;
  894. emit mouseEvent(event, this);
  895. }
  896. void MapPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  897. updateCurHoveredTile(event->pos());
  898. emit mouseEvent(event, this);
  899. }
  900. void MapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  901. emit mouseEvent(event, this);
  902. }
  903. void CollisionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  904. emit mouseEvent(event, this);
  905. }
  906. void CollisionPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  907. emit mouseEvent(event, this);
  908. }
  909. void CollisionPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  910. emit mouseEvent(event, this);
  911. }
  912. void CollisionPixmapItem::draw() {
  913. if (map) {
  914. setPixmap(map->renderCollision());
  915. }
  916. }
  917. void CollisionPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
  918. if (map) {
  919. QPointF pos = event->pos();
  920. int x = (int)(pos.x()) / 16;
  921. int y = (int)(pos.y()) / 16;
  922. Block *block = map->getBlock(x, y);
  923. if (block) {
  924. if (map->paint_collision >= 0) {
  925. block->collision = map->paint_collision;
  926. }
  927. if (map->paint_elevation >= 0) {
  928. block->elevation = map->paint_elevation;
  929. }
  930. map->_setBlock(x, y, *block);
  931. }
  932. if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  933. map->commit();
  934. }
  935. draw();
  936. }
  937. }
  938. void CollisionPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
  939. if (map) {
  940. QPointF pos = event->pos();
  941. int x = (int)(pos.x()) / 16;
  942. int y = (int)(pos.y()) / 16;
  943. bool collision = map->paint_collision >= 0;
  944. bool elevation = map->paint_elevation >= 0;
  945. if (collision && elevation) {
  946. map->floodFillCollisionElevation(x, y, map->paint_collision, map->paint_elevation);
  947. } else if (collision) {
  948. map->floodFillCollision(x, y, map->paint_collision);
  949. } else if (elevation) {
  950. map->floodFillElevation(x, y, map->paint_elevation);
  951. }
  952. draw();
  953. }
  954. }
  955. void CollisionPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
  956. QPointF pos = event->pos();
  957. int x = (int)(pos.x()) / 16;
  958. int y = (int)(pos.y()) / 16;
  959. Block *block = map->getBlock(x, y);
  960. if (block) {
  961. map->paint_collision = block->collision;
  962. map->paint_elevation = block->elevation;
  963. emit map->paintCollisionChanged(map);
  964. }
  965. }
  966. void DraggablePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *mouse) {
  967. active = true;
  968. clicking = true;
  969. last_x = (mouse->pos().x() + this->pos().x()) / 16;
  970. last_y = (mouse->pos().y() + this->pos().y()) / 16;
  971. //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("y"));
  972. }
  973. void DraggablePixmapItem::move(int x, int y) {
  974. event->setX(event->x() + x);
  975. event->setY(event->y() + y);
  976. updatePosition();
  977. emitPositionChanged();
  978. }
  979. void DraggablePixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouse) {
  980. if (active) {
  981. int x = (mouse->pos().x() + this->pos().x()) / 16;
  982. int y = (mouse->pos().y() + this->pos().y()) / 16;
  983. if (x != last_x || y != last_y) {
  984. clicking = false;
  985. if (editor->selected_events->contains(this)) {
  986. for (DraggablePixmapItem *item : *editor->selected_events) {
  987. item->move(x - last_x, y - last_y);
  988. }
  989. } else {
  990. move(x - last_x, y - last_y);
  991. }
  992. last_x = x;
  993. last_y = y;
  994. //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("x"));
  995. }
  996. }
  997. }
  998. void DraggablePixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouse) {
  999. if (clicking) {
  1000. this->editor->selectMapObject(this, mouse->modifiers() & Qt::ControlModifier);
  1001. this->editor->updateSelectedObjects();
  1002. }
  1003. active = false;
  1004. }
  1005. QList<DraggablePixmapItem *> *Editor::getObjects() {
  1006. QList<DraggablePixmapItem *> *list = new QList<DraggablePixmapItem *>;
  1007. for (Event *event : map->getAllEvents()) {
  1008. for (QGraphicsItem *child : objects_group->childItems()) {
  1009. DraggablePixmapItem *item = (DraggablePixmapItem *)child;
  1010. if (item->event == event) {
  1011. list->append(item);
  1012. break;
  1013. }
  1014. }
  1015. }
  1016. return list;
  1017. }
  1018. void Editor::redrawObject(DraggablePixmapItem *item) {
  1019. if (item) {
  1020. item->setPixmap(item->event->pixmap);
  1021. if (selected_events && selected_events->contains(item)) {
  1022. QImage image = item->pixmap().toImage();
  1023. QPainter painter(&image);
  1024. painter.setPen(QColor(250, 100, 25));
  1025. painter.drawRect(0, 0, image.width() - 1, image.height() - 1);
  1026. painter.end();
  1027. item->setPixmap(QPixmap::fromImage(image));
  1028. }
  1029. }
  1030. }
  1031. void Editor::updateSelectedObjects() {
  1032. for (DraggablePixmapItem *item : *(getObjects())) {
  1033. redrawObject(item);
  1034. }
  1035. emit selectedObjectsChanged();
  1036. }
  1037. void Editor::selectMapObject(DraggablePixmapItem *object) {
  1038. selectMapObject(object, false);
  1039. }
  1040. void Editor::selectMapObject(DraggablePixmapItem *object, bool toggle) {
  1041. if (selected_events && object) {
  1042. if (selected_events->contains(object)) {
  1043. if (toggle) {
  1044. selected_events->removeOne(object);
  1045. }
  1046. } else {
  1047. if (!toggle) {
  1048. selected_events->clear();
  1049. }
  1050. selected_events->append(object);
  1051. }
  1052. updateSelectedObjects();
  1053. }
  1054. }
  1055. DraggablePixmapItem* Editor::addNewEvent() {
  1056. return addNewEvent("object");
  1057. }
  1058. DraggablePixmapItem* Editor::addNewEvent(QString event_type) {
  1059. if (project && map) {
  1060. Event *event = new Event;
  1061. event->put("map_name", map->name);
  1062. event->put("event_type", event_type);
  1063. map->addEvent(event);
  1064. project->loadObjectPixmaps(map->getAllEvents());
  1065. DraggablePixmapItem *object = addMapObject(event);
  1066. return object;
  1067. }
  1068. return NULL;
  1069. }
  1070. void Editor::deleteEvent(Event *event) {
  1071. Map *map = project->getMap(event->get("map_name"));
  1072. if (map) {
  1073. map->removeEvent(event);
  1074. }
  1075. //selected_events->removeAll(event);
  1076. //updateSelectedObjects();
  1077. }
  1078. // dunno how to detect bubbling. QMouseEvent::isAccepted seems to always be true
  1079. // check if selected_events changed instead. this has the side effect of deselecting
  1080. // when you click on a selected event, since selected_events doesn't change.
  1081. QList<DraggablePixmapItem *> selected_events_test;
  1082. bool clicking = false;
  1083. void Editor::objectsView_onMousePress(QMouseEvent *event) {
  1084. clicking = true;
  1085. selected_events_test = *selected_events;
  1086. }
  1087. void Editor::objectsView_onMouseMove(QMouseEvent *event) {
  1088. clicking = false;
  1089. }
  1090. void Editor::objectsView_onMouseRelease(QMouseEvent *event) {
  1091. if (clicking) {
  1092. if (selected_events_test.length()) {
  1093. if (selected_events_test.length() == selected_events->length()) {
  1094. bool deselect = true;
  1095. for (int i = 0; i < selected_events_test.length(); i++) {
  1096. if (selected_events_test.at(i) != selected_events->at(i)) {
  1097. deselect = false;
  1098. break;
  1099. }
  1100. }
  1101. if (deselect) {
  1102. selected_events->clear();
  1103. updateSelectedObjects();
  1104. }
  1105. }
  1106. }
  1107. clicking = false;
  1108. }
  1109. }