Нет описания

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