Nenhuma descrição

editor.cpp 47KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  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_index = 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 MovementPermissionsPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {
  699. QPointF pos = event->pos();
  700. int x = ((int)pos.x()) / 16;
  701. int y = ((int)pos.y()) / 16;
  702. int width = pixmap().width() / 16;
  703. int height = pixmap().height() / 16;
  704. if ((x >= 0 && x < width) && (y >=0 && y < height)) {
  705. pick(y * width + x);
  706. }
  707. }
  708. void MovementPermissionsPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event) {
  709. updateCurHoveredMetatile(event->pos());
  710. mousePressEvent(event);
  711. }
  712. void MovementPermissionsPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) {
  713. mousePressEvent(event);
  714. }
  715. void CollisionMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
  716. int x = ((int)pos.x()) / 16;
  717. int y = ((int)pos.y()) / 16;
  718. int width = pixmap().width() / 16;
  719. int height = pixmap().height() / 16;
  720. if (x < 0 || x >= width || y < 0 || y >= height) {
  721. map->clearHoveredCollisionTile();
  722. } else {
  723. int collision = y * width + x;
  724. map->hoveredCollisionTileChanged(collision);
  725. }
  726. }
  727. int ConnectionPixmapItem::getMinOffset() {
  728. if (connection->direction == "up" || connection->direction == "down")
  729. return 1 - (this->pixmap().width() / 16);
  730. else
  731. return 1 - (this->pixmap().height() / 16);
  732. }
  733. int ConnectionPixmapItem::getMaxOffset() {
  734. if (connection->direction == "up" || connection->direction == "down")
  735. return baseMapWidth - 1;
  736. else
  737. return baseMapHeight - 1;
  738. }
  739. QVariant ConnectionPixmapItem::itemChange(GraphicsItemChange change, const QVariant &value)
  740. {
  741. if (change == ItemPositionChange) {
  742. QPointF newPos = value.toPointF();
  743. qreal x, y;
  744. int newOffset = initialOffset;
  745. if (connection->direction == "up" || connection->direction == "down") {
  746. x = round(newPos.x() / 16) * 16;
  747. newOffset += (x - initialX) / 16;
  748. newOffset = qMin(newOffset, this->getMaxOffset());
  749. newOffset = qMax(newOffset, this->getMinOffset());
  750. x = newOffset * 16;
  751. }
  752. else {
  753. x = initialX;
  754. }
  755. if (connection->direction == "right" || connection->direction == "left") {
  756. y = round(newPos.y() / 16) * 16;
  757. newOffset += (y - initialY) / 16;
  758. newOffset = qMin(newOffset, this->getMaxOffset());
  759. newOffset = qMax(newOffset, this->getMinOffset());
  760. y = newOffset * 16;
  761. }
  762. else {
  763. y = initialY;
  764. }
  765. connection->offset = QString::number(newOffset);
  766. emit connectionMoved(connection);
  767. return QPointF(x, y);
  768. }
  769. else {
  770. return QGraphicsItem::itemChange(change, value);
  771. }
  772. }
  773. void ConnectionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {
  774. emit connectionItemSelected(this);
  775. }
  776. void ConnectionPixmapItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent*) {
  777. emit connectionItemDoubleClicked(this);
  778. }
  779. void ElevationMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
  780. int x = ((int)pos.x()) / 16;
  781. int y = ((int)pos.y()) / 16;
  782. int width = pixmap().width() / 16;
  783. int height = pixmap().height() / 16;
  784. if (x < 0 || x >= width || y < 0 || y >= height) {
  785. map->clearHoveredElevationTile();
  786. } else {
  787. int elevation = y * width + x;
  788. map->hoveredElevationTileChanged(elevation);
  789. }
  790. }
  791. void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
  792. if (map) {
  793. QPointF pos = event->pos();
  794. int x = (int)(pos.x()) / 16;
  795. int y = (int)(pos.y()) / 16;
  796. if (map->smart_paths_enabled) {
  797. paintSmartPath(x, y);
  798. } else {
  799. paintNormal(x, y);
  800. }
  801. if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  802. map->commit();
  803. }
  804. draw();
  805. }
  806. }
  807. void MapPixmapItem::paintNormal(int x, int y) {
  808. // Snap the selected position to the top-left of the block boundary.
  809. // This allows painting via dragging the mouse to tile the painted region.
  810. int xDiff = x - map->paint_tile_initial_x;
  811. int yDiff = y - map->paint_tile_initial_y;
  812. if (xDiff < 0 && xDiff % map->paint_tile_width != 0) xDiff -= map->paint_tile_width;
  813. if (yDiff < 0 && yDiff % map->paint_tile_height != 0) yDiff -= map->paint_tile_height;
  814. x = map->paint_tile_initial_x + (xDiff / map->paint_tile_width) * map->paint_tile_width;
  815. y = map->paint_tile_initial_y + (yDiff / map->paint_tile_height) * map->paint_tile_height;
  816. for (int i = 0; i < map->paint_tile_width && i + x < map->getWidth(); i++)
  817. for (int j = 0; j < map->paint_tile_height && j + y < map->getHeight(); j++) {
  818. int actualX = i + x;
  819. int actualY = j + y;
  820. Block *block = map->getBlock(actualX, actualY);
  821. if (block) {
  822. block->tile = map->getSelectedBlockIndex(map->paint_tile_index + i + (j * 8));
  823. map->_setBlock(actualX, actualY, *block);
  824. }
  825. }
  826. }
  827. // These are tile offsets from the top-left tile in the 3x3 smart path selection.
  828. // Each entry is for one possibility from the marching squares value for a tile.
  829. // (Marching Squares: https://en.wikipedia.org/wiki/Marching_squares)
  830. QList<int> MapPixmapItem::smartPathTable = QList<int>({
  831. 8 + 1, // 0000
  832. 8 + 1, // 0001
  833. 8 + 1, // 0010
  834. 16 + 0, // 0011
  835. 8 + 1, // 0100
  836. 8 + 1, // 0101
  837. 0 + 0, // 0110
  838. 8 + 0, // 0111
  839. 8 + 1, // 1000
  840. 16 + 2, // 1001
  841. 8 + 1, // 1010
  842. 16 + 1, // 1011
  843. 0 + 2, // 1100
  844. 8 + 2, // 1101
  845. 0 + 1, // 1110
  846. 8 + 1, // 1111
  847. });
  848. #define IS_SMART_PATH_TILE(block) ((map->getDisplayedBlockIndex(block->tile) >= map->paint_tile_index && map->getDisplayedBlockIndex(block->tile) < map->paint_tile_index + 3) \
  849. || (map->getDisplayedBlockIndex(block->tile) >= map->paint_tile_index + 8 && map->getDisplayedBlockIndex(block->tile) < map->paint_tile_index + 11) \
  850. || (map->getDisplayedBlockIndex(block->tile) >= map->paint_tile_index + 16 && map->getDisplayedBlockIndex(block->tile) < map->paint_tile_index + 19))
  851. void MapPixmapItem::paintSmartPath(int x, int y) {
  852. // Smart path should never be enabled without a 3x3 block selection.
  853. if (map->paint_tile_width != 3 || map->paint_tile_height != 3) return;
  854. // Shift to the middle tile of the smart path selection.
  855. int openTile = map->paint_tile_index + 8 + 1;
  856. // Fill the region with the open tile.
  857. for (int i = -1; i <= 1; i++)
  858. for (int j = -1; j <= 1; j++) {
  859. // Check if in map bounds.
  860. if (!(i + x < map->getWidth() && i + x >= 0 && j + y < map->getHeight() && j + y >= 0))
  861. continue;
  862. int actualX = i + x;
  863. int actualY = j + y;
  864. Block *block = map->getBlock(actualX, actualY);
  865. if (block) {
  866. block->tile = map->getSelectedBlockIndex(openTile);
  867. map->_setBlock(actualX, actualY, *block);
  868. }
  869. }
  870. // Go back and resolve the edge tiles
  871. for (int i = -2; i <= 2; i++)
  872. for (int j = -2; j <= 2; j++) {
  873. // Check if in map bounds.
  874. if (!(i + x < map->getWidth() && i + x >= 0 && j + y < map->getHeight() && j + y >= 0))
  875. continue;
  876. // Ignore the corners, which can't possible be affected by the smart path.
  877. if ((i == -2 && j == -2) || (i == 2 && j == -2) ||
  878. (i == -2 && j == 2) || (i == 2 && j == 2))
  879. continue;
  880. // Ignore tiles that aren't part of the smart path set.
  881. int actualX = i + x;
  882. int actualY = j + y;
  883. Block *block = map->getBlock(actualX, actualY);
  884. if (!block || !IS_SMART_PATH_TILE(block)) {
  885. continue;
  886. }
  887. int id = 0;
  888. Block *top = map->getBlock(actualX, actualY - 1);
  889. Block *right = map->getBlock(actualX + 1, actualY);
  890. Block *bottom = map->getBlock(actualX, actualY + 1);
  891. Block *left = map->getBlock(actualX - 1, actualY);
  892. // Get marching squares value, to determine which tile to use.
  893. if (top && IS_SMART_PATH_TILE(top))
  894. id += 1;
  895. if (right && IS_SMART_PATH_TILE(right))
  896. id += 2;
  897. if (bottom && IS_SMART_PATH_TILE(bottom))
  898. id += 4;
  899. if (left && IS_SMART_PATH_TILE(left))
  900. id += 8;
  901. block->tile = map->getSelectedBlockIndex(map->paint_tile_index + smartPathTable[id]);
  902. map->_setBlock(actualX, actualY, *block);
  903. }
  904. }
  905. void MapPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
  906. if (map) {
  907. QPointF pos = event->pos();
  908. int x = (int)(pos.x()) / 16;
  909. int y = (int)(pos.y()) / 16;
  910. map->floodFill(x, y, map->getSelectedBlockIndex(map->paint_tile_index));
  911. draw();
  912. }
  913. }
  914. void MapPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
  915. QPointF pos = event->pos();
  916. int x = (int)(pos.x()) / 16;
  917. int y = (int)(pos.y()) / 16;
  918. Block *block = map->getBlock(x, y);
  919. if (block) {
  920. map->paint_tile_index = map->getDisplayedBlockIndex(block->tile);
  921. map->paint_tile_width = 1;
  922. map->paint_tile_height = 1;
  923. emit map->paintTileChanged(map);
  924. }
  925. }
  926. #define SWAP(a, b) do { if (a != b) { a ^= b; b ^= a; a ^= b; } } while (0)
  927. void MapPixmapItem::select(QGraphicsSceneMouseEvent *event) {
  928. QPointF pos = event->pos();
  929. int x = (int)(pos.x()) / 16;
  930. int y = (int)(pos.y()) / 16;
  931. if (event->type() == QEvent::GraphicsSceneMousePress) {
  932. selection_origin = QPoint(x, y);
  933. selection.clear();
  934. } else if (event->type() == QEvent::GraphicsSceneMouseMove) {
  935. if (event->buttons() & Qt::LeftButton) {
  936. selection.clear();
  937. selection.append(QPoint(x, y));
  938. }
  939. } else if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  940. if (!selection.isEmpty()) {
  941. QPoint pos = selection.last();
  942. int x1 = selection_origin.x();
  943. int y1 = selection_origin.y();
  944. int x2 = pos.x();
  945. int y2 = pos.y();
  946. if (x1 > x2) SWAP(x1, x2);
  947. if (y1 > y2) SWAP(y1, y2);
  948. selection.clear();
  949. for (int y = y1; y <= y2; y++) {
  950. for (int x = x1; x <= x2; x++) {
  951. selection.append(QPoint(x, y));
  952. }
  953. }
  954. qDebug() << QString("selected (%1, %2) -> (%3, %4)").arg(x1).arg(y1).arg(x2).arg(y2);
  955. }
  956. }
  957. }
  958. void MapPixmapItem::draw() {
  959. if (map) {
  960. setPixmap(map->render());
  961. }
  962. }
  963. void MapPixmapItem::undo() {
  964. if (map) {
  965. map->undo();
  966. draw();
  967. }
  968. }
  969. void MapPixmapItem::redo() {
  970. if (map) {
  971. map->redo();
  972. draw();
  973. }
  974. }
  975. void MapPixmapItem::updateCurHoveredTile(QPointF pos) {
  976. int x = ((int)pos.x()) / 16;
  977. int y = ((int)pos.y()) / 16;
  978. int blockIndex = y * map->getWidth() + x;
  979. if (x < 0 || x >= map->getWidth() || y < 0 || y >= map->getHeight()) {
  980. map->clearHoveredTile();
  981. } else {
  982. int tile = map->blockdata->blocks->at(blockIndex).tile;
  983. map->hoveredTileChanged(x, y, tile);
  984. }
  985. }
  986. void MapPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
  987. updateCurHoveredTile(event->pos());
  988. }
  989. void MapPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
  990. map->clearHoveredTile();
  991. }
  992. void MapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  993. QPointF pos = event->pos();
  994. int x = ((int)pos.x()) / 16;
  995. int y = ((int)pos.y()) / 16;
  996. map->paint_tile_initial_x = x;
  997. map->paint_tile_initial_y = y;
  998. emit mouseEvent(event, this);
  999. }
  1000. void MapPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  1001. updateCurHoveredTile(event->pos());
  1002. emit mouseEvent(event, this);
  1003. }
  1004. void MapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  1005. emit mouseEvent(event, this);
  1006. }
  1007. void CollisionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  1008. emit mouseEvent(event, this);
  1009. }
  1010. void CollisionPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  1011. emit mouseEvent(event, this);
  1012. }
  1013. void CollisionPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  1014. emit mouseEvent(event, this);
  1015. }
  1016. void CollisionPixmapItem::draw() {
  1017. if (map) {
  1018. setPixmap(map->renderCollision());
  1019. }
  1020. }
  1021. void CollisionPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
  1022. if (map) {
  1023. QPointF pos = event->pos();
  1024. int x = (int)(pos.x()) / 16;
  1025. int y = (int)(pos.y()) / 16;
  1026. Block *block = map->getBlock(x, y);
  1027. if (block) {
  1028. if (map->paint_collision >= 0) {
  1029. block->collision = map->paint_collision;
  1030. }
  1031. if (map->paint_elevation >= 0) {
  1032. block->elevation = map->paint_elevation;
  1033. }
  1034. map->_setBlock(x, y, *block);
  1035. }
  1036. if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  1037. map->commit();
  1038. }
  1039. draw();
  1040. }
  1041. }
  1042. void CollisionPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
  1043. if (map) {
  1044. QPointF pos = event->pos();
  1045. int x = (int)(pos.x()) / 16;
  1046. int y = (int)(pos.y()) / 16;
  1047. bool collision = map->paint_collision >= 0;
  1048. bool elevation = map->paint_elevation >= 0;
  1049. if (collision && elevation) {
  1050. map->floodFillCollisionElevation(x, y, map->paint_collision, map->paint_elevation);
  1051. } else if (collision) {
  1052. map->floodFillCollision(x, y, map->paint_collision);
  1053. } else if (elevation) {
  1054. map->floodFillElevation(x, y, map->paint_elevation);
  1055. }
  1056. draw();
  1057. }
  1058. }
  1059. void CollisionPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
  1060. QPointF pos = event->pos();
  1061. int x = (int)(pos.x()) / 16;
  1062. int y = (int)(pos.y()) / 16;
  1063. Block *block = map->getBlock(x, y);
  1064. if (block) {
  1065. map->paint_collision = block->collision;
  1066. map->paint_elevation = block->elevation;
  1067. emit map->paintCollisionChanged(map);
  1068. }
  1069. }
  1070. void DraggablePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *mouse) {
  1071. active = true;
  1072. clicking = true;
  1073. last_x = (mouse->pos().x() + this->pos().x()) / 16;
  1074. last_y = (mouse->pos().y() + this->pos().y()) / 16;
  1075. //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("y"));
  1076. }
  1077. void DraggablePixmapItem::move(int x, int y) {
  1078. event->setX(event->x() + x);
  1079. event->setY(event->y() + y);
  1080. updatePosition();
  1081. emitPositionChanged();
  1082. }
  1083. void DraggablePixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouse) {
  1084. if (active) {
  1085. int x = (mouse->pos().x() + this->pos().x()) / 16;
  1086. int y = (mouse->pos().y() + this->pos().y()) / 16;
  1087. if (x != last_x || y != last_y) {
  1088. clicking = false;
  1089. if (editor->selected_events->contains(this)) {
  1090. for (DraggablePixmapItem *item : *editor->selected_events) {
  1091. item->move(x - last_x, y - last_y);
  1092. }
  1093. } else {
  1094. move(x - last_x, y - last_y);
  1095. }
  1096. last_x = x;
  1097. last_y = y;
  1098. //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("x"));
  1099. }
  1100. }
  1101. }
  1102. void DraggablePixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouse) {
  1103. if (clicking) {
  1104. this->editor->selectMapObject(this, mouse->modifiers() & Qt::ControlModifier);
  1105. this->editor->updateSelectedObjects();
  1106. }
  1107. active = false;
  1108. }
  1109. QList<DraggablePixmapItem *> *Editor::getObjects() {
  1110. QList<DraggablePixmapItem *> *list = new QList<DraggablePixmapItem *>;
  1111. for (Event *event : map->getAllEvents()) {
  1112. for (QGraphicsItem *child : objects_group->childItems()) {
  1113. DraggablePixmapItem *item = (DraggablePixmapItem *)child;
  1114. if (item->event == event) {
  1115. list->append(item);
  1116. break;
  1117. }
  1118. }
  1119. }
  1120. return list;
  1121. }
  1122. void Editor::redrawObject(DraggablePixmapItem *item) {
  1123. if (item) {
  1124. item->setPixmap(item->event->pixmap);
  1125. if (selected_events && selected_events->contains(item)) {
  1126. QImage image = item->pixmap().toImage();
  1127. QPainter painter(&image);
  1128. painter.setPen(QColor(250, 100, 25));
  1129. painter.drawRect(0, 0, image.width() - 1, image.height() - 1);
  1130. painter.end();
  1131. item->setPixmap(QPixmap::fromImage(image));
  1132. }
  1133. }
  1134. }
  1135. void Editor::updateSelectedObjects() {
  1136. for (DraggablePixmapItem *item : *(getObjects())) {
  1137. redrawObject(item);
  1138. }
  1139. emit selectedObjectsChanged();
  1140. }
  1141. void Editor::selectMapObject(DraggablePixmapItem *object) {
  1142. selectMapObject(object, false);
  1143. }
  1144. void Editor::selectMapObject(DraggablePixmapItem *object, bool toggle) {
  1145. if (selected_events && object) {
  1146. if (selected_events->contains(object)) {
  1147. if (toggle) {
  1148. selected_events->removeOne(object);
  1149. }
  1150. } else {
  1151. if (!toggle) {
  1152. selected_events->clear();
  1153. }
  1154. selected_events->append(object);
  1155. }
  1156. updateSelectedObjects();
  1157. }
  1158. }
  1159. DraggablePixmapItem* Editor::addNewEvent() {
  1160. return addNewEvent("object");
  1161. }
  1162. DraggablePixmapItem* Editor::addNewEvent(QString event_type) {
  1163. if (project && map) {
  1164. Event *event = new Event;
  1165. event->put("map_name", map->name);
  1166. event->put("event_type", event_type);
  1167. map->addEvent(event);
  1168. project->loadObjectPixmaps(map->getAllEvents());
  1169. DraggablePixmapItem *object = addMapObject(event);
  1170. return object;
  1171. }
  1172. return NULL;
  1173. }
  1174. void Editor::deleteEvent(Event *event) {
  1175. Map *map = project->getMap(event->get("map_name"));
  1176. if (map) {
  1177. map->removeEvent(event);
  1178. }
  1179. //selected_events->removeAll(event);
  1180. //updateSelectedObjects();
  1181. }
  1182. // dunno how to detect bubbling. QMouseEvent::isAccepted seems to always be true
  1183. // check if selected_events changed instead. this has the side effect of deselecting
  1184. // when you click on a selected event, since selected_events doesn't change.
  1185. QList<DraggablePixmapItem *> selected_events_test;
  1186. bool clicking = false;
  1187. void Editor::objectsView_onMousePress(QMouseEvent *event) {
  1188. clicking = true;
  1189. selected_events_test = *selected_events;
  1190. }
  1191. void Editor::objectsView_onMouseMove(QMouseEvent *event) {
  1192. clicking = false;
  1193. }
  1194. void Editor::objectsView_onMouseRelease(QMouseEvent *event) {
  1195. if (clicking) {
  1196. if (selected_events_test.length()) {
  1197. if (selected_events_test.length() == selected_events->length()) {
  1198. bool deselect = true;
  1199. for (int i = 0; i < selected_events_test.length(); i++) {
  1200. if (selected_events_test.at(i) != selected_events->at(i)) {
  1201. deselect = false;
  1202. break;
  1203. }
  1204. }
  1205. if (deselect) {
  1206. selected_events->clear();
  1207. updateSelectedObjects();
  1208. }
  1209. }
  1210. }
  1211. clicking = false;
  1212. }
  1213. }