No Description

editor.cpp 57KB

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