No Description

editor.cpp 47KB

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