No Description

editor.cpp 52KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  1. #include "editor.h"
  2. #include "event.h"
  3. #include <QCheckBox>
  4. #include <QPainter>
  5. #include <QMouseEvent>
  6. Editor::Editor(Ui::MainWindow* ui)
  7. {
  8. this->ui = ui;
  9. selected_events = new QList<DraggablePixmapItem*>;
  10. }
  11. void Editor::saveProject() {
  12. if (project) {
  13. project->saveAllMaps();
  14. project->saveAllDataStructures();
  15. }
  16. }
  17. void Editor::save() {
  18. if (project && map) {
  19. project->saveMap(map);
  20. project->saveAllDataStructures();
  21. }
  22. }
  23. void Editor::undo() {
  24. if (current_view) {
  25. ((MapPixmapItem*)current_view)->undo();
  26. }
  27. }
  28. void Editor::redo() {
  29. if (current_view) {
  30. ((MapPixmapItem*)current_view)->redo();
  31. }
  32. }
  33. void Editor::setEditingMap() {
  34. current_view = map_item;
  35. if (map_item) {
  36. displayMapConnections();
  37. map_item->draw();
  38. map_item->setVisible(true);
  39. map_item->setEnabled(true);
  40. setConnectionsVisibility(true);
  41. }
  42. if (collision_item) {
  43. collision_item->setVisible(false);
  44. }
  45. if (events_group) {
  46. events_group->setVisible(false);
  47. }
  48. setBorderItemsVisible(true);
  49. setConnectionItemsVisible(false);
  50. }
  51. void Editor::setEditingCollision() {
  52. current_view = collision_item;
  53. if (collision_item) {
  54. collision_item->draw();
  55. collision_item->setVisible(true);
  56. setConnectionsVisibility(true);
  57. }
  58. if (map_item) {
  59. map_item->setVisible(false);
  60. }
  61. if (events_group) {
  62. events_group->setVisible(false);
  63. }
  64. setBorderItemsVisible(true);
  65. setConnectionItemsVisible(false);
  66. }
  67. void Editor::setEditingObjects() {
  68. current_view = map_item;
  69. if (events_group) {
  70. events_group->setVisible(true);
  71. }
  72. if (map_item) {
  73. map_item->setVisible(true);
  74. map_item->setEnabled(false);
  75. setConnectionsVisibility(true);
  76. }
  77. if (collision_item) {
  78. collision_item->setVisible(false);
  79. }
  80. setBorderItemsVisible(true);
  81. setConnectionItemsVisible(false);
  82. }
  83. void Editor::setEditingConnections() {
  84. current_view = map_item;
  85. if (map_item) {
  86. map_item->draw();
  87. map_item->setVisible(true);
  88. map_item->setEnabled(false);
  89. populateConnectionMapPickers();
  90. ui->label_NumConnections->setText(QString::number(map->connections.length()));
  91. setConnectionsVisibility(false);
  92. setDiveEmergeControls();
  93. setConnectionEditControlsEnabled(selected_connection_item != NULL);
  94. if (selected_connection_item) {
  95. onConnectionOffsetChanged(selected_connection_item->connection->offset.toInt());
  96. setConnectionMap(selected_connection_item->connection->map_name);
  97. setCurrentConnectionDirection(selected_connection_item->connection->direction);
  98. }
  99. }
  100. if (collision_item) {
  101. collision_item->setVisible(false);
  102. }
  103. if (events_group) {
  104. events_group->setVisible(false);
  105. }
  106. setBorderItemsVisible(true, 0.4);
  107. setConnectionItemsVisible(true);
  108. }
  109. void Editor::setDiveEmergeControls() {
  110. ui->comboBox_DiveMap->blockSignals(true);
  111. ui->comboBox_EmergeMap->blockSignals(true);
  112. ui->comboBox_DiveMap->setCurrentText("");
  113. ui->comboBox_EmergeMap->setCurrentText("");
  114. for (Connection* connection : map->connections) {
  115. if (connection->direction == "dive") {
  116. ui->comboBox_DiveMap->setCurrentText(connection->map_name);
  117. } else if (connection->direction == "emerge") {
  118. ui->comboBox_EmergeMap->setCurrentText(connection->map_name);
  119. }
  120. }
  121. ui->comboBox_DiveMap->blockSignals(false);
  122. ui->comboBox_EmergeMap->blockSignals(false);
  123. }
  124. void Editor::populateConnectionMapPickers() {
  125. ui->comboBox_ConnectedMap->blockSignals(true);
  126. ui->comboBox_DiveMap->blockSignals(true);
  127. ui->comboBox_EmergeMap->blockSignals(true);
  128. ui->comboBox_ConnectedMap->clear();
  129. ui->comboBox_ConnectedMap->addItems(*project->mapNames);
  130. ui->comboBox_DiveMap->clear();
  131. ui->comboBox_DiveMap->addItems(*project->mapNames);
  132. ui->comboBox_EmergeMap->clear();
  133. ui->comboBox_EmergeMap->addItems(*project->mapNames);
  134. ui->comboBox_ConnectedMap->blockSignals(false);
  135. ui->comboBox_DiveMap->blockSignals(true);
  136. ui->comboBox_EmergeMap->blockSignals(true);
  137. }
  138. void Editor::setConnectionItemsVisible(bool visible) {
  139. for (ConnectionPixmapItem* item : connection_edit_items) {
  140. item->setVisible(visible);
  141. item->setEnabled(visible);
  142. }
  143. }
  144. void Editor::setBorderItemsVisible(bool visible, qreal opacity) {
  145. for (QGraphicsPixmapItem* item : borderItems) {
  146. item->setVisible(visible);
  147. item->setOpacity(opacity);
  148. }
  149. }
  150. void Editor::setCurrentConnectionDirection(QString curDirection) {
  151. if (!selected_connection_item)
  152. return;
  153. selected_connection_item->connection->direction = curDirection;
  154. Map *connected_map = project->getMap(selected_connection_item->connection->map_name);
  155. QPixmap pixmap = connected_map->renderConnection(*selected_connection_item->connection);
  156. int offset = selected_connection_item->connection->offset.toInt(nullptr, 0);
  157. selected_connection_item->initialOffset = offset;
  158. int x = 0, y = 0;
  159. if (selected_connection_item->connection->direction == "up") {
  160. x = offset * 16;
  161. y = -pixmap.height();
  162. } else if (selected_connection_item->connection->direction == "down") {
  163. x = offset * 16;
  164. y = map->getHeight() * 16;
  165. } else if (selected_connection_item->connection->direction == "left") {
  166. x = -pixmap.width();
  167. y = offset * 16;
  168. } else if (selected_connection_item->connection->direction == "right") {
  169. x = map->getWidth() * 16;
  170. y = offset * 16;
  171. }
  172. selected_connection_item->basePixmap = pixmap;
  173. QPainter painter(&pixmap);
  174. painter.setPen(QColor(255, 0, 255));
  175. painter.drawRect(0, 0, pixmap.width() - 1, pixmap.height() - 1);
  176. painter.end();
  177. selected_connection_item->setPixmap(pixmap);
  178. selected_connection_item->initialX = x;
  179. selected_connection_item->initialY = y;
  180. selected_connection_item->blockSignals(true);
  181. selected_connection_item->setX(x);
  182. selected_connection_item->setY(y);
  183. selected_connection_item->setZValue(-1);
  184. selected_connection_item->blockSignals(false);
  185. setConnectionEditControlValues(selected_connection_item->connection);
  186. }
  187. void Editor::updateCurrentConnectionDirection(QString curDirection) {
  188. if (!selected_connection_item)
  189. return;
  190. QString originalDirection = selected_connection_item->connection->direction;
  191. setCurrentConnectionDirection(curDirection);
  192. updateMirroredConnectionDirection(selected_connection_item->connection, originalDirection);
  193. }
  194. void Editor::onConnectionMoved(Connection* connection) {
  195. updateMirroredConnectionOffset(connection);
  196. onConnectionOffsetChanged(connection->offset.toInt());
  197. }
  198. void Editor::onConnectionOffsetChanged(int newOffset) {
  199. ui->spinBox_ConnectionOffset->blockSignals(true);
  200. ui->spinBox_ConnectionOffset->setValue(newOffset);
  201. ui->spinBox_ConnectionOffset->blockSignals(false);
  202. }
  203. void Editor::setConnectionEditControlValues(Connection* connection) {
  204. QString mapName = connection ? connection->map_name : "";
  205. QString direction = connection ? connection->direction : "";
  206. int offset = connection ? connection->offset.toInt() : 0;
  207. ui->comboBox_ConnectedMap->blockSignals(true);
  208. ui->comboBox_ConnectionDirection->blockSignals(true);
  209. ui->spinBox_ConnectionOffset->blockSignals(true);
  210. ui->comboBox_ConnectedMap->setCurrentText(mapName);
  211. ui->comboBox_ConnectionDirection->setCurrentText(direction);
  212. ui->spinBox_ConnectionOffset->setValue(offset);
  213. ui->comboBox_ConnectedMap->blockSignals(false);
  214. ui->comboBox_ConnectionDirection->blockSignals(false);
  215. ui->spinBox_ConnectionOffset->blockSignals(false);
  216. }
  217. void Editor::setConnectionEditControlsEnabled(bool enabled) {
  218. ui->comboBox_ConnectionDirection->setEnabled(enabled);
  219. ui->comboBox_ConnectedMap->setEnabled(enabled);
  220. ui->spinBox_ConnectionOffset->setEnabled(enabled);
  221. if (!enabled) {
  222. setConnectionEditControlValues(false);
  223. }
  224. }
  225. void Editor::onConnectionItemSelected(ConnectionPixmapItem* connectionItem) {
  226. if (!connectionItem)
  227. return;
  228. for (ConnectionPixmapItem* item : connection_edit_items) {
  229. bool isSelectedItem = item == connectionItem;
  230. int zValue = isSelectedItem ? 0 : -1;
  231. qreal opacity = isSelectedItem ? 1 : 0.75;
  232. item->setZValue(zValue);
  233. item->render(opacity);
  234. if (isSelectedItem) {
  235. QPixmap pixmap = item->pixmap();
  236. QPainter painter(&pixmap);
  237. painter.setPen(QColor(255, 0, 255));
  238. painter.drawRect(0, 0, pixmap.width() - 1, pixmap.height() - 1);
  239. painter.end();
  240. item->setPixmap(pixmap);
  241. }
  242. }
  243. selected_connection_item = connectionItem;
  244. setConnectionEditControlsEnabled(true);
  245. setConnectionEditControlValues(selected_connection_item->connection);
  246. ui->spinBox_ConnectionOffset->setMaximum(selected_connection_item->getMaxOffset());
  247. ui->spinBox_ConnectionOffset->setMinimum(selected_connection_item->getMinOffset());
  248. onConnectionOffsetChanged(selected_connection_item->connection->offset.toInt());
  249. }
  250. void Editor::setSelectedConnectionFromMap(QString mapName) {
  251. // Search for the first connection that connects to the given map map.
  252. for (ConnectionPixmapItem* item : connection_edit_items) {
  253. if (item->connection->map_name == mapName) {
  254. onConnectionItemSelected(item);
  255. break;
  256. }
  257. }
  258. }
  259. void Editor::onConnectionItemDoubleClicked(ConnectionPixmapItem* connectionItem) {
  260. emit loadMapRequested(connectionItem->connection->map_name, map->name);
  261. }
  262. void Editor::onConnectionDirectionChanged(QString newDirection) {
  263. ui->comboBox_ConnectionDirection->blockSignals(true);
  264. ui->comboBox_ConnectionDirection->setCurrentText(newDirection);
  265. ui->comboBox_ConnectionDirection->blockSignals(false);
  266. }
  267. void Editor::onBorderMetatilesChanged() {
  268. displayMapBorder();
  269. }
  270. void Editor::setConnectionsVisibility(bool visible) {
  271. for (QGraphicsPixmapItem* item : connection_items) {
  272. item->setVisible(visible);
  273. item->setActive(visible);
  274. }
  275. }
  276. void Editor::setMap(QString map_name) {
  277. if (map_name.isNull()) {
  278. return;
  279. }
  280. if (project) {
  281. map = project->loadMap(map_name);
  282. displayMap();
  283. selected_events->clear();
  284. updateSelectedEvents();
  285. }
  286. }
  287. void Editor::mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item) {
  288. if (map_edit_mode == "paint") {
  289. item->paint(event);
  290. } else if (map_edit_mode == "fill") {
  291. item->floodFill(event);
  292. } else if (map_edit_mode == "pick") {
  293. item->pick(event);
  294. } else if (map_edit_mode == "select") {
  295. item->select(event);
  296. }
  297. }
  298. void Editor::mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixmapItem *item) {
  299. if (map_edit_mode == "paint") {
  300. item->paint(event);
  301. } else if (map_edit_mode == "fill") {
  302. item->floodFill(event);
  303. } else if (map_edit_mode == "pick") {
  304. item->pick(event);
  305. } else if (map_edit_mode == "select") {
  306. item->select(event);
  307. }
  308. }
  309. void Editor::displayMap() {
  310. if (!scene)
  311. scene = new QGraphicsScene;
  312. if (map_item && scene) {
  313. scene->removeItem(map_item);
  314. delete map_item;
  315. }
  316. map_item = new MapPixmapItem(map);
  317. connect(map_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*,MapPixmapItem*)),
  318. this, SLOT(mouseEvent_map(QGraphicsSceneMouseEvent*,MapPixmapItem*)));
  319. map_item->draw(true);
  320. scene->addItem(map_item);
  321. if (collision_item && scene) {
  322. scene->removeItem(collision_item);
  323. delete collision_item;
  324. }
  325. collision_item = new CollisionPixmapItem(map);
  326. connect(collision_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*,CollisionPixmapItem*)),
  327. this, SLOT(mouseEvent_collision(QGraphicsSceneMouseEvent*,CollisionPixmapItem*)));
  328. collision_item->draw(true);
  329. scene->addItem(collision_item);
  330. int tw = 16;
  331. int th = 16;
  332. scene->setSceneRect(
  333. -6 * tw,
  334. -6 * th,
  335. map_item->pixmap().width() + 12 * tw,
  336. map_item->pixmap().height() + 12 * th
  337. );
  338. displayMetatiles();
  339. displayBorderMetatiles();
  340. displayCollisionMetatiles();
  341. displayElevationMetatiles();
  342. displayMapEvents();
  343. displayMapConnections();
  344. displayMapBorder();
  345. displayMapGrid();
  346. if (map_item) {
  347. map_item->setVisible(false);
  348. }
  349. if (collision_item) {
  350. collision_item->setVisible(false);
  351. }
  352. if (events_group) {
  353. events_group->setVisible(false);
  354. }
  355. }
  356. void Editor::displayMetatiles() {
  357. if (metatiles_item && metatiles_item->scene()) {
  358. metatiles_item->scene()->removeItem(metatiles_item);
  359. delete metatiles_item;
  360. }
  361. scene_metatiles = new QGraphicsScene;
  362. metatiles_item = new MetatilesPixmapItem(map);
  363. metatiles_item->draw();
  364. scene_metatiles->addItem(metatiles_item);
  365. }
  366. void Editor::displayBorderMetatiles() {
  367. if (selected_border_metatiles_item && selected_border_metatiles_item->scene()) {
  368. selected_border_metatiles_item->scene()->removeItem(selected_border_metatiles_item);
  369. delete selected_border_metatiles_item;
  370. }
  371. scene_selected_border_metatiles = new QGraphicsScene;
  372. selected_border_metatiles_item = new BorderMetatilesPixmapItem(map);
  373. selected_border_metatiles_item->draw();
  374. scene_selected_border_metatiles->addItem(selected_border_metatiles_item);
  375. connect(selected_border_metatiles_item, SIGNAL(borderMetatilesChanged()), this, SLOT(onBorderMetatilesChanged()));
  376. }
  377. void Editor::displayCollisionMetatiles() {
  378. if (collision_metatiles_item && collision_metatiles_item->scene()) {
  379. collision_metatiles_item->scene()->removeItem(collision_metatiles_item);
  380. delete collision_metatiles_item;
  381. }
  382. scene_collision_metatiles = new QGraphicsScene;
  383. collision_metatiles_item = new CollisionMetatilesPixmapItem(map);
  384. collision_metatiles_item->draw();
  385. scene_collision_metatiles->addItem(collision_metatiles_item);
  386. }
  387. void Editor::displayElevationMetatiles() {
  388. if (elevation_metatiles_item && elevation_metatiles_item->scene()) {
  389. elevation_metatiles_item->scene()->removeItem(elevation_metatiles_item);
  390. delete elevation_metatiles_item;
  391. }
  392. scene_elevation_metatiles = new QGraphicsScene;
  393. elevation_metatiles_item = new ElevationMetatilesPixmapItem(map);
  394. elevation_metatiles_item->draw();
  395. scene_elevation_metatiles->addItem(elevation_metatiles_item);
  396. }
  397. void Editor::displayMapEvents() {
  398. if (events_group) {
  399. for (QGraphicsItem *child : events_group->childItems()) {
  400. events_group->removeFromGroup(child);
  401. delete child;
  402. }
  403. if (events_group->scene()) {
  404. events_group->scene()->removeItem(events_group);
  405. }
  406. delete events_group;
  407. }
  408. events_group = new EventGroup;
  409. scene->addItem(events_group);
  410. QList<Event *> events = map->getAllEvents();
  411. project->loadEventPixmaps(events);
  412. for (Event *event : events) {
  413. addMapEvent(event);
  414. }
  415. //objects_group->setFiltersChildEvents(false);
  416. events_group->setHandlesChildEvents(false);
  417. emit objectsChanged();
  418. }
  419. DraggablePixmapItem *Editor::addMapEvent(Event *event) {
  420. DraggablePixmapItem *object = new DraggablePixmapItem(event);
  421. object->editor = this;
  422. events_group->addToGroup(object);
  423. return object;
  424. }
  425. void Editor::displayMapConnections() {
  426. for (QGraphicsPixmapItem* item : connection_items) {
  427. if (item->scene()) {
  428. item->scene()->removeItem(item);
  429. }
  430. delete item;
  431. }
  432. connection_items.clear();
  433. for (ConnectionPixmapItem* item : connection_edit_items) {
  434. if (item->scene()) {
  435. item->scene()->removeItem(item);
  436. }
  437. delete item;
  438. }
  439. selected_connection_item = NULL;
  440. connection_edit_items.clear();
  441. for (Connection *connection : map->connections) {
  442. if (connection->direction == "dive" || connection->direction == "emerge") {
  443. continue;
  444. }
  445. createConnectionItem(connection, false);
  446. }
  447. if (!connection_edit_items.empty()) {
  448. onConnectionItemSelected(connection_edit_items.first());
  449. }
  450. }
  451. void Editor::createConnectionItem(Connection* connection, bool hide) {
  452. Map *connected_map = project->getMap(connection->map_name);
  453. QPixmap pixmap = connected_map->renderConnection(*connection);
  454. int offset = connection->offset.toInt(nullptr, 0);
  455. int x = 0, y = 0;
  456. if (connection->direction == "up") {
  457. x = offset * 16;
  458. y = -pixmap.height();
  459. } else if (connection->direction == "down") {
  460. x = offset * 16;
  461. y = map->getHeight() * 16;
  462. } else if (connection->direction == "left") {
  463. x = -pixmap.width();
  464. y = offset * 16;
  465. } else if (connection->direction == "right") {
  466. x = map->getWidth() * 16;
  467. y = offset * 16;
  468. }
  469. QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
  470. item->setZValue(-1);
  471. item->setX(x);
  472. item->setY(y);
  473. scene->addItem(item);
  474. connection_items.append(item);
  475. item->setVisible(!hide);
  476. ConnectionPixmapItem *connection_edit_item = new ConnectionPixmapItem(pixmap, connection, x, y, map->getWidth(), map->getHeight());
  477. connection_edit_item->setX(x);
  478. connection_edit_item->setY(y);
  479. connection_edit_item->setZValue(-1);
  480. scene->addItem(connection_edit_item);
  481. connect(connection_edit_item, SIGNAL(connectionMoved(Connection*)), this, SLOT(onConnectionMoved(Connection*)));
  482. connect(connection_edit_item, SIGNAL(connectionItemSelected(ConnectionPixmapItem*)), this, SLOT(onConnectionItemSelected(ConnectionPixmapItem*)));
  483. connect(connection_edit_item, SIGNAL(connectionItemDoubleClicked(ConnectionPixmapItem*)), this, SLOT(onConnectionItemDoubleClicked(ConnectionPixmapItem*)));
  484. connection_edit_items.append(connection_edit_item);
  485. }
  486. void Editor::displayMapBorder() {
  487. for (QGraphicsPixmapItem* item : borderItems) {
  488. if (item->scene()) {
  489. item->scene()->removeItem(item);
  490. }
  491. delete item;
  492. }
  493. borderItems.clear();
  494. QPixmap pixmap = map->renderBorder();
  495. for (int y = -6; y < map->getHeight() + 6; y += 2)
  496. for (int x = -6; x < map->getWidth() + 6; x += 2) {
  497. QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
  498. item->setX(x * 16);
  499. item->setY(y * 16);
  500. item->setZValue(-2);
  501. scene->addItem(item);
  502. borderItems.append(item);
  503. }
  504. }
  505. void Editor::displayMapGrid() {
  506. for (QGraphicsLineItem* item : gridLines) {
  507. if (item && item->scene()) {
  508. item->scene()->removeItem(item);
  509. }
  510. delete item;
  511. }
  512. gridLines.clear();
  513. ui->checkBox_ToggleGrid->disconnect();
  514. int pixelWidth = map->getWidth() * 16;
  515. int pixelHeight = map->getHeight() * 16;
  516. for (int i = 0; i <= map->getWidth(); i++) {
  517. int x = i * 16;
  518. QGraphicsLineItem *line = scene->addLine(x, 0, x, pixelHeight);
  519. line->setVisible(ui->checkBox_ToggleGrid->isChecked());
  520. gridLines.append(line);
  521. connect(ui->checkBox_ToggleGrid, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);});
  522. }
  523. for (int j = 0; j <= map->getHeight(); j++) {
  524. int y = j * 16;
  525. QGraphicsLineItem *line = scene->addLine(0, y, pixelWidth, y);
  526. line->setVisible(ui->checkBox_ToggleGrid->isChecked());
  527. gridLines.append(line);
  528. connect(ui->checkBox_ToggleGrid, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);});
  529. }
  530. }
  531. void Editor::updateConnectionOffset(int offset) {
  532. if (!selected_connection_item)
  533. return;
  534. selected_connection_item->blockSignals(true);
  535. offset = qMin(offset, selected_connection_item->getMaxOffset());
  536. offset = qMax(offset, selected_connection_item->getMinOffset());
  537. selected_connection_item->connection->offset = QString::number(offset);
  538. if (selected_connection_item->connection->direction == "up" || selected_connection_item->connection->direction == "down") {
  539. selected_connection_item->setX(selected_connection_item->initialX + (offset - selected_connection_item->initialOffset) * 16);
  540. } else if (selected_connection_item->connection->direction == "left" || selected_connection_item->connection->direction == "right") {
  541. selected_connection_item->setY(selected_connection_item->initialY + (offset - selected_connection_item->initialOffset) * 16);
  542. }
  543. selected_connection_item->blockSignals(false);
  544. updateMirroredConnectionOffset(selected_connection_item->connection);
  545. }
  546. void Editor::setConnectionMap(QString mapName) {
  547. if (!mapName.isEmpty() && !project->mapNames->contains(mapName)) {
  548. qDebug() << "Invalid map name " << mapName << " specified for connection.";
  549. return;
  550. }
  551. if (!selected_connection_item)
  552. return;
  553. if (mapName.isEmpty()) {
  554. removeCurrentConnection();
  555. return;
  556. }
  557. QString originalMapName = selected_connection_item->connection->map_name;
  558. setConnectionEditControlsEnabled(true);
  559. selected_connection_item->connection->map_name = mapName;
  560. setCurrentConnectionDirection(selected_connection_item->connection->direction);
  561. updateMirroredConnectionMap(selected_connection_item->connection, originalMapName);
  562. }
  563. void Editor::addNewConnection() {
  564. // Find direction with least number of connections.
  565. QMap<QString, int> directionCounts = QMap<QString, int>({{"up", 0}, {"right", 0}, {"down", 0}, {"left", 0}});
  566. for (Connection* connection : map->connections) {
  567. directionCounts[connection->direction]++;
  568. }
  569. QString minDirection = "up";
  570. int minCount = INT_MAX;
  571. for (QString direction : directionCounts.keys()) {
  572. if (directionCounts[direction] < minCount) {
  573. minDirection = direction;
  574. minCount = directionCounts[direction];
  575. }
  576. }
  577. // Don't connect the map to itself.
  578. QString defaultMapName = project->mapNames->first();
  579. if (defaultMapName == map->name) {
  580. defaultMapName = project->mapNames->value(1);
  581. }
  582. Connection* newConnection = new Connection;
  583. newConnection->direction = minDirection;
  584. newConnection->offset = "0";
  585. newConnection->map_name = defaultMapName;
  586. map->connections.append(newConnection);
  587. createConnectionItem(newConnection, true);
  588. onConnectionItemSelected(connection_edit_items.last());
  589. ui->label_NumConnections->setText(QString::number(map->connections.length()));
  590. updateMirroredConnection(newConnection, newConnection->direction, newConnection->map_name);
  591. }
  592. void Editor::updateMirroredConnectionOffset(Connection* connection) {
  593. updateMirroredConnection(connection, connection->direction, connection->map_name);
  594. }
  595. void Editor::updateMirroredConnectionDirection(Connection* connection, QString originalDirection) {
  596. updateMirroredConnection(connection, originalDirection, connection->map_name);
  597. }
  598. void Editor::updateMirroredConnectionMap(Connection* connection, QString originalMapName) {
  599. updateMirroredConnection(connection, connection->direction, originalMapName);
  600. }
  601. void Editor::removeMirroredConnection(Connection* connection) {
  602. updateMirroredConnection(connection, connection->direction, connection->map_name, true);
  603. }
  604. void Editor::updateMirroredConnection(Connection* connection, QString originalDirection, QString originalMapName, bool isDelete) {
  605. if (!ui->checkBox_MirrorConnections->isChecked())
  606. return;
  607. static QMap<QString, QString> oppositeDirections = QMap<QString, QString>({
  608. {"up", "down"}, {"right", "left"},
  609. {"down", "up"}, {"left", "right"},
  610. {"dive", "emerge"},{"emerge", "dive"}});
  611. QString oppositeDirection = oppositeDirections.value(originalDirection);
  612. // Find the matching connection in the connected map.
  613. QMap<QString, Map*> *mapcache = project->map_cache;
  614. Connection* mirrorConnection = NULL;
  615. Map* otherMap = project->getMap(originalMapName);
  616. for (Connection* conn : otherMap->connections) {
  617. if (conn->direction == oppositeDirection && conn->map_name == map->name) {
  618. mirrorConnection = conn;
  619. }
  620. }
  621. if (isDelete) {
  622. if (mirrorConnection) {
  623. otherMap->connections.removeOne(mirrorConnection);
  624. delete mirrorConnection;
  625. }
  626. return;
  627. }
  628. if (connection->direction != originalDirection || connection->map_name != originalMapName) {
  629. if (mirrorConnection) {
  630. otherMap->connections.removeOne(mirrorConnection);
  631. delete mirrorConnection;
  632. mirrorConnection = NULL;
  633. otherMap = project->getMap(connection->map_name);
  634. }
  635. }
  636. // Create a new mirrored connection, if a matching one doesn't already exist.
  637. if (!mirrorConnection) {
  638. mirrorConnection = new Connection;
  639. mirrorConnection->direction = oppositeDirections.value(connection->direction);
  640. mirrorConnection->map_name = map->name;
  641. otherMap->connections.append(mirrorConnection);
  642. }
  643. mirrorConnection->offset = QString::number(-connection->offset.toInt());
  644. }
  645. void Editor::removeCurrentConnection() {
  646. if (!selected_connection_item)
  647. return;
  648. map->connections.removeOne(selected_connection_item->connection);
  649. connection_edit_items.removeOne(selected_connection_item);
  650. removeMirroredConnection(selected_connection_item->connection);
  651. if (selected_connection_item && selected_connection_item->scene()) {
  652. selected_connection_item->scene()->removeItem(selected_connection_item);
  653. delete selected_connection_item;
  654. }
  655. selected_connection_item = NULL;
  656. setConnectionEditControlsEnabled(false);
  657. ui->spinBox_ConnectionOffset->setValue(0);
  658. ui->label_NumConnections->setText(QString::number(map->connections.length()));
  659. if (connection_edit_items.length() > 0) {
  660. onConnectionItemSelected(connection_edit_items.last());
  661. }
  662. }
  663. void Editor::updateDiveMap(QString mapName) {
  664. updateDiveEmergeMap(mapName, "dive");
  665. }
  666. void Editor::updateEmergeMap(QString mapName) {
  667. updateDiveEmergeMap(mapName, "emerge");
  668. }
  669. void Editor::updateDiveEmergeMap(QString mapName, QString direction) {
  670. if (!mapName.isEmpty() && !project->mapNamesToMapConstants->contains(mapName)) {
  671. qDebug() << "Invalid " << direction << " map connection: " << mapName;
  672. return;
  673. }
  674. Connection* connection = NULL;
  675. for (Connection* conn : map->connections) {
  676. if (conn->direction == direction) {
  677. connection = conn;
  678. break;
  679. }
  680. }
  681. if (mapName.isEmpty()) {
  682. // Remove dive/emerge connection
  683. if (connection) {
  684. map->connections.removeOne(connection);
  685. removeMirroredConnection(connection);
  686. }
  687. } else {
  688. if (!connection) {
  689. connection = new Connection;
  690. connection->direction = direction;
  691. connection->offset = "0";
  692. connection->map_name = mapName;
  693. map->connections.append(connection);
  694. updateMirroredConnection(connection, connection->direction, connection->map_name);
  695. } else {
  696. QString originalMapName = connection->map_name;
  697. connection->map_name = mapName;
  698. updateMirroredConnectionMap(connection, originalMapName);
  699. }
  700. }
  701. ui->label_NumConnections->setText(QString::number(map->connections.length()));
  702. }
  703. void Editor::updatePrimaryTileset(QString tilesetLabel)
  704. {
  705. map->layout->tileset_primary_label = tilesetLabel;
  706. map->layout->tileset_primary = project->getTileset(tilesetLabel);
  707. emit tilesetChanged(map->name);
  708. }
  709. void Editor::updateSecondaryTileset(QString tilesetLabel)
  710. {
  711. map->layout->tileset_secondary_label = tilesetLabel;
  712. map->layout->tileset_secondary = project->getTileset(tilesetLabel);
  713. emit tilesetChanged(map->name);
  714. }
  715. void MetatilesPixmapItem::paintTileChanged(Map *map) {
  716. draw();
  717. }
  718. void MetatilesPixmapItem::draw() {
  719. setPixmap(map->renderMetatiles());
  720. }
  721. void MetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
  722. int x = ((int)pos.x()) / 16;
  723. int y = ((int)pos.y()) / 16;
  724. int width = pixmap().width() / 16;
  725. int height = pixmap().height() / 16;
  726. if (x < 0 || x >= width || y < 0 || y >= height) {
  727. map->clearHoveredMetatile();
  728. } else {
  729. int block = y * width + x;
  730. map->hoveredMetatileChanged(block);
  731. }
  732. }
  733. void MetatilesPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
  734. updateCurHoveredMetatile(event->pos());
  735. }
  736. void MetatilesPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
  737. map->clearHoveredMetatile();
  738. }
  739. void MetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  740. QPointF pos = event->pos();
  741. int x = ((int)pos.x()) / 16;
  742. int y = ((int)pos.y()) / 16;
  743. map->paint_metatile_initial_x = x;
  744. map->paint_metatile_initial_y = y;
  745. updateSelection(event->pos(), event->button());
  746. }
  747. void MetatilesPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  748. updateCurHoveredMetatile(event->pos());
  749. Qt::MouseButton button = event->button();
  750. if (button == Qt::MouseButton::NoButton) {
  751. Qt::MouseButtons heldButtons = event->buttons();
  752. if (heldButtons & Qt::RightButton) {
  753. button = Qt::RightButton;
  754. } else if (heldButtons & Qt::LeftButton) {
  755. button = Qt::LeftButton;
  756. }
  757. }
  758. updateSelection(event->pos(), button);
  759. }
  760. void MetatilesPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  761. updateSelection(event->pos(), event->button());
  762. }
  763. void MetatilesPixmapItem::updateSelection(QPointF pos, Qt::MouseButton button) {
  764. int x = ((int)pos.x()) / 16;
  765. int y = ((int)pos.y()) / 16;
  766. int width = pixmap().width() / 16;
  767. int height = pixmap().height() / 16;
  768. if ((x >= 0 && x < width) && (y >=0 && y < height)) {
  769. int baseTileX = x < map->paint_metatile_initial_x ? x : map->paint_metatile_initial_x;
  770. int baseTileY = y < map->paint_metatile_initial_y ? y : map->paint_metatile_initial_y;
  771. map->paint_tile_index = baseTileY * 8 + baseTileX;
  772. map->paint_tile_width = abs(map->paint_metatile_initial_x - x) + 1;
  773. map->paint_tile_height = abs(map->paint_metatile_initial_y - y) + 1;
  774. map->smart_paths_enabled = button == Qt::RightButton
  775. && map->paint_tile_width == 3
  776. && map->paint_tile_height == 3;
  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) {
  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. map->floodFill(x, y, map->getSelectedBlockIndex(map->paint_tile_index));
  1024. draw();
  1025. }
  1026. }
  1027. void MapPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
  1028. QPointF pos = event->pos();
  1029. int x = (int)(pos.x()) / 16;
  1030. int y = (int)(pos.y()) / 16;
  1031. Block *block = map->getBlock(x, y);
  1032. if (block) {
  1033. map->paint_tile_index = map->getDisplayedBlockIndex(block->tile);
  1034. map->paint_tile_width = 1;
  1035. map->paint_tile_height = 1;
  1036. emit map->paintTileChanged(map);
  1037. }
  1038. }
  1039. #define SWAP(a, b) do { if (a != b) { a ^= b; b ^= a; a ^= b; } } while (0)
  1040. void MapPixmapItem::select(QGraphicsSceneMouseEvent *event) {
  1041. QPointF pos = event->pos();
  1042. int x = (int)(pos.x()) / 16;
  1043. int y = (int)(pos.y()) / 16;
  1044. if (event->type() == QEvent::GraphicsSceneMousePress) {
  1045. selection_origin = QPoint(x, y);
  1046. selection.clear();
  1047. } else if (event->type() == QEvent::GraphicsSceneMouseMove) {
  1048. if (event->buttons() & Qt::LeftButton) {
  1049. selection.clear();
  1050. selection.append(QPoint(x, y));
  1051. }
  1052. } else if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  1053. if (!selection.isEmpty()) {
  1054. QPoint pos = selection.last();
  1055. int x1 = selection_origin.x();
  1056. int y1 = selection_origin.y();
  1057. int x2 = pos.x();
  1058. int y2 = pos.y();
  1059. if (x1 > x2) SWAP(x1, x2);
  1060. if (y1 > y2) SWAP(y1, y2);
  1061. selection.clear();
  1062. for (int y = y1; y <= y2; y++) {
  1063. for (int x = x1; x <= x2; x++) {
  1064. selection.append(QPoint(x, y));
  1065. }
  1066. }
  1067. qDebug() << QString("selected (%1, %2) -> (%3, %4)").arg(x1).arg(y1).arg(x2).arg(y2);
  1068. }
  1069. }
  1070. }
  1071. void MapPixmapItem::draw(bool ignoreCache) {
  1072. if (map) {
  1073. setPixmap(map->render(ignoreCache));
  1074. }
  1075. }
  1076. void MapPixmapItem::undo() {
  1077. if (map) {
  1078. map->undo();
  1079. draw();
  1080. }
  1081. }
  1082. void MapPixmapItem::redo() {
  1083. if (map) {
  1084. map->redo();
  1085. draw();
  1086. }
  1087. }
  1088. void MapPixmapItem::updateCurHoveredTile(QPointF pos) {
  1089. int x = ((int)pos.x()) / 16;
  1090. int y = ((int)pos.y()) / 16;
  1091. int blockIndex = y * map->getWidth() + x;
  1092. if (x < 0 || x >= map->getWidth() || y < 0 || y >= map->getHeight()) {
  1093. map->clearHoveredTile();
  1094. } else {
  1095. int tile = map->layout->blockdata->blocks->at(blockIndex).tile;
  1096. map->hoveredTileChanged(x, y, tile);
  1097. }
  1098. }
  1099. void MapPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
  1100. updateCurHoveredTile(event->pos());
  1101. }
  1102. void MapPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
  1103. map->clearHoveredTile();
  1104. }
  1105. void MapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  1106. QPointF pos = event->pos();
  1107. int x = ((int)pos.x()) / 16;
  1108. int y = ((int)pos.y()) / 16;
  1109. map->paint_tile_initial_x = x;
  1110. map->paint_tile_initial_y = y;
  1111. emit mouseEvent(event, this);
  1112. }
  1113. void MapPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  1114. updateCurHoveredTile(event->pos());
  1115. emit mouseEvent(event, this);
  1116. }
  1117. void MapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  1118. emit mouseEvent(event, this);
  1119. }
  1120. void CollisionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  1121. emit mouseEvent(event, this);
  1122. }
  1123. void CollisionPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  1124. emit mouseEvent(event, this);
  1125. }
  1126. void CollisionPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  1127. emit mouseEvent(event, this);
  1128. }
  1129. void CollisionPixmapItem::draw(bool ignoreCache) {
  1130. if (map) {
  1131. setPixmap(map->renderCollision(ignoreCache));
  1132. }
  1133. }
  1134. void CollisionPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
  1135. if (map) {
  1136. QPointF pos = event->pos();
  1137. int x = (int)(pos.x()) / 16;
  1138. int y = (int)(pos.y()) / 16;
  1139. Block *block = map->getBlock(x, y);
  1140. if (block) {
  1141. if (map->paint_collision >= 0) {
  1142. block->collision = map->paint_collision;
  1143. }
  1144. if (map->paint_elevation >= 0) {
  1145. block->elevation = map->paint_elevation;
  1146. }
  1147. map->_setBlock(x, y, *block);
  1148. }
  1149. if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  1150. map->commit();
  1151. }
  1152. draw();
  1153. }
  1154. }
  1155. void CollisionPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
  1156. if (map) {
  1157. QPointF pos = event->pos();
  1158. int x = (int)(pos.x()) / 16;
  1159. int y = (int)(pos.y()) / 16;
  1160. bool collision = map->paint_collision >= 0;
  1161. bool elevation = map->paint_elevation >= 0;
  1162. if (collision && elevation) {
  1163. map->floodFillCollisionElevation(x, y, map->paint_collision, map->paint_elevation);
  1164. } else if (collision) {
  1165. map->floodFillCollision(x, y, map->paint_collision);
  1166. } else if (elevation) {
  1167. map->floodFillElevation(x, y, map->paint_elevation);
  1168. }
  1169. draw();
  1170. }
  1171. }
  1172. void CollisionPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
  1173. QPointF pos = event->pos();
  1174. int x = (int)(pos.x()) / 16;
  1175. int y = (int)(pos.y()) / 16;
  1176. Block *block = map->getBlock(x, y);
  1177. if (block) {
  1178. map->paint_collision = block->collision;
  1179. map->paint_elevation = block->elevation;
  1180. emit map->paintCollisionChanged(map);
  1181. }
  1182. }
  1183. void DraggablePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *mouse) {
  1184. active = true;
  1185. clicking = true;
  1186. last_x = (mouse->pos().x() + this->pos().x()) / 16;
  1187. last_y = (mouse->pos().y() + this->pos().y()) / 16;
  1188. //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("y"));
  1189. }
  1190. void DraggablePixmapItem::move(int x, int y) {
  1191. event->setX(event->x() + x);
  1192. event->setY(event->y() + y);
  1193. updatePosition();
  1194. emitPositionChanged();
  1195. }
  1196. void DraggablePixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouse) {
  1197. if (active) {
  1198. int x = (mouse->pos().x() + this->pos().x()) / 16;
  1199. int y = (mouse->pos().y() + this->pos().y()) / 16;
  1200. if (x != last_x || y != last_y) {
  1201. clicking = false;
  1202. if (editor->selected_events->contains(this)) {
  1203. for (DraggablePixmapItem *item : *editor->selected_events) {
  1204. item->move(x - last_x, y - last_y);
  1205. }
  1206. } else {
  1207. move(x - last_x, y - last_y);
  1208. }
  1209. last_x = x;
  1210. last_y = y;
  1211. //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("x"));
  1212. }
  1213. }
  1214. }
  1215. void DraggablePixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouse) {
  1216. if (clicking) {
  1217. this->editor->selectMapEvent(this, mouse->modifiers() & Qt::ControlModifier);
  1218. this->editor->updateSelectedEvents();
  1219. }
  1220. active = false;
  1221. }
  1222. QList<DraggablePixmapItem *> *Editor::getObjects() {
  1223. QList<DraggablePixmapItem *> *list = new QList<DraggablePixmapItem *>;
  1224. for (Event *event : map->getAllEvents()) {
  1225. for (QGraphicsItem *child : events_group->childItems()) {
  1226. DraggablePixmapItem *item = (DraggablePixmapItem *)child;
  1227. if (item->event == event) {
  1228. list->append(item);
  1229. break;
  1230. }
  1231. }
  1232. }
  1233. return list;
  1234. }
  1235. void Editor::redrawObject(DraggablePixmapItem *item) {
  1236. if (item) {
  1237. item->setPixmap(item->event->pixmap);
  1238. if (selected_events && selected_events->contains(item)) {
  1239. QImage image = item->pixmap().toImage();
  1240. QPainter painter(&image);
  1241. painter.setPen(QColor(250, 100, 25));
  1242. painter.drawRect(0, 0, image.width() - 1, image.height() - 1);
  1243. painter.end();
  1244. item->setPixmap(QPixmap::fromImage(image));
  1245. }
  1246. }
  1247. }
  1248. void Editor::updateSelectedEvents() {
  1249. for (DraggablePixmapItem *item : *(getObjects())) {
  1250. redrawObject(item);
  1251. }
  1252. emit selectedObjectsChanged();
  1253. }
  1254. void Editor::selectMapEvent(DraggablePixmapItem *object) {
  1255. selectMapEvent(object, false);
  1256. }
  1257. void Editor::selectMapEvent(DraggablePixmapItem *object, bool toggle) {
  1258. if (selected_events && object) {
  1259. if (selected_events->contains(object)) {
  1260. if (toggle) {
  1261. selected_events->removeOne(object);
  1262. }
  1263. } else {
  1264. if (!toggle) {
  1265. selected_events->clear();
  1266. }
  1267. selected_events->append(object);
  1268. }
  1269. updateSelectedEvents();
  1270. }
  1271. }
  1272. DraggablePixmapItem* Editor::addNewEvent(QString event_type) {
  1273. if (project && map) {
  1274. Event *event = Event::createNewEvent(event_type, map->name);
  1275. event->put("map_name", map->name);
  1276. map->addEvent(event);
  1277. project->loadEventPixmaps(map->getAllEvents());
  1278. DraggablePixmapItem *object = addMapEvent(event);
  1279. return object;
  1280. }
  1281. return NULL;
  1282. }
  1283. void Editor::deleteEvent(Event *event) {
  1284. Map *map = project->getMap(event->get("map_name"));
  1285. if (map) {
  1286. map->removeEvent(event);
  1287. }
  1288. //selected_events->removeAll(event);
  1289. //updateSelectedObjects();
  1290. }
  1291. // dunno how to detect bubbling. QMouseEvent::isAccepted seems to always be true
  1292. // check if selected_events changed instead. this has the side effect of deselecting
  1293. // when you click on a selected event, since selected_events doesn't change.
  1294. QList<DraggablePixmapItem *> selected_events_test;
  1295. bool clicking = false;
  1296. void Editor::objectsView_onMousePress(QMouseEvent *event) {
  1297. clicking = true;
  1298. selected_events_test = *selected_events;
  1299. }
  1300. void Editor::objectsView_onMouseMove(QMouseEvent *event) {
  1301. clicking = false;
  1302. }
  1303. void Editor::objectsView_onMouseRelease(QMouseEvent *event) {
  1304. if (clicking) {
  1305. if (selected_events_test.length()) {
  1306. if (selected_events_test.length() == selected_events->length()) {
  1307. bool deselect = true;
  1308. for (int i = 0; i < selected_events_test.length(); i++) {
  1309. if (selected_events_test.at(i) != selected_events->at(i)) {
  1310. deselect = false;
  1311. break;
  1312. }
  1313. }
  1314. if (deselect) {
  1315. selected_events->clear();
  1316. updateSelectedEvents();
  1317. }
  1318. }
  1319. }
  1320. clicking = false;
  1321. }
  1322. }