No Description

editor.cpp 47KB

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