No Description

editor.cpp 57KB

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