Brak opisu

editor.cpp 46KB

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