Brak opisu

editor.cpp 46KB

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