Нет описания

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