Нема описа

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