Açıklama Yok

editor.cpp 42KB

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