Sin descripción

editor.cpp 46KB

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