No Description

editor.cpp 57KB

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