Нет описания

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