No Description

editor.cpp 57KB

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