No Description

editor.cpp 47KB

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