Aucune description

editor.cpp 40KB

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