No Description

editor.cpp 34KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. #include "editor.h"
  2. #include <QCheckBox>
  3. #include <QPainter>
  4. #include <QMouseEvent>
  5. Editor::Editor(Ui::MainWindow* ui)
  6. {
  7. this->ui = ui;
  8. selected_events = new QList<DraggablePixmapItem*>;
  9. }
  10. void Editor::saveProject() {
  11. if (project) {
  12. project->saveAllMaps();
  13. project->saveAllDataStructures();
  14. }
  15. }
  16. void Editor::save() {
  17. if (project && map) {
  18. project->saveMap(map);
  19. project->saveAllDataStructures();
  20. }
  21. }
  22. void Editor::undo() {
  23. if (current_view) {
  24. ((MapPixmapItem*)current_view)->undo();
  25. }
  26. }
  27. void Editor::redo() {
  28. if (current_view) {
  29. ((MapPixmapItem*)current_view)->redo();
  30. }
  31. }
  32. void Editor::setEditingMap() {
  33. current_view = map_item;
  34. if (map_item) {
  35. map_item->draw();
  36. map_item->setVisible(true);
  37. map_item->setEnabled(true);
  38. setConnectionsVisibility(true);
  39. }
  40. if (collision_item) {
  41. collision_item->setVisible(false);
  42. }
  43. if (objects_group) {
  44. objects_group->setVisible(false);
  45. }
  46. }
  47. void Editor::setEditingCollision() {
  48. current_view = collision_item;
  49. if (collision_item) {
  50. collision_item->setVisible(true);
  51. setConnectionsVisibility(true);
  52. }
  53. if (map_item) {
  54. map_item->setVisible(false);
  55. }
  56. if (objects_group) {
  57. objects_group->setVisible(false);
  58. }
  59. }
  60. void Editor::setEditingObjects() {
  61. current_view = map_item;
  62. if (objects_group) {
  63. objects_group->setVisible(true);
  64. }
  65. if (map_item) {
  66. map_item->setVisible(true);
  67. map_item->setEnabled(false);
  68. setConnectionsVisibility(true);
  69. }
  70. if (collision_item) {
  71. collision_item->setVisible(false);
  72. }
  73. }
  74. void Editor::setEditingConnections(QString direction) {
  75. current_view = map_item;
  76. if (map_item) {
  77. map_item->draw();
  78. map_item->setVisible(true);
  79. map_item->setEnabled(true);
  80. ui->comboBox_ConnectedMap->blockSignals(true);
  81. ui->comboBox_ConnectedMap->clear();
  82. ui->comboBox_ConnectedMap->addItems(*project->mapNames);
  83. ui->comboBox_ConnectedMap->blockSignals(false);
  84. setConnectionsVisibility(false);
  85. showCurrentConnectionMap(direction);
  86. }
  87. if (collision_item) {
  88. collision_item->setVisible(false);
  89. }
  90. if (objects_group) {
  91. objects_group->setVisible(false);
  92. }
  93. }
  94. void Editor::showCurrentConnectionMap(QString curDirection) {
  95. bool connectionExists = false;
  96. for (Connection* connection : map->connections) {
  97. if (connection->direction != curDirection) continue;
  98. if (connection_item) {
  99. scene->removeItem(connection_item);
  100. delete connection_item;
  101. connection_item = NULL;
  102. }
  103. connectionExists = true;
  104. Map *connected_map = project->getMap(connection->map_name);
  105. QPixmap pixmap = connected_map->renderConnection(*connection);
  106. int offset = connection->offset.toInt(nullptr, 0);
  107. int x = 0, y = 0;
  108. if (connection->direction == "up") {
  109. x = offset * 16;
  110. y = -pixmap.height();
  111. } else if (connection->direction == "down") {
  112. x = offset * 16;
  113. y = map->getHeight() * 16;
  114. } else if (connection->direction == "left") {
  115. x = -pixmap.width();
  116. y = offset * 16;
  117. } else if (connection->direction == "right") {
  118. x = map->getWidth() * 16;
  119. y = offset * 16;
  120. }
  121. QPainter painter(&pixmap);
  122. painter.setPen(QColor(255, 0, 255));
  123. painter.drawRect(0, 0, pixmap.width() - 1, pixmap.height() - 1);
  124. painter.end();
  125. connection_item = new ConnectionPixmapItem(pixmap, connection, x, y);
  126. connection_item->setX(x);
  127. connection_item->setY(y);
  128. connection_item->setZValue(21);
  129. scene->addItem(connection_item);
  130. scene->setSceneRect(0, 0, pixmap.width() + map_item->pixmap().width(), pixmap.height() + map_item->pixmap().height());
  131. connect(connection_item, SIGNAL(connectionMoved(int)), this, SLOT(onConnectionOffsetChanged(int)));
  132. onConnectionOffsetChanged(connection->offset.toInt());
  133. ui->comboBox_ConnectedMap->setCurrentText(connection->map_name);
  134. break;
  135. }
  136. if (!connectionExists) {
  137. if (connection_item) {
  138. scene->removeItem(connection_item);
  139. delete connection_item;
  140. connection_item = NULL;
  141. }
  142. ui->comboBox_ConnectedMap->setCurrentText("");
  143. ui->spinBox_ConnectionOffset->setDisabled(true);
  144. ui->spinBox_ConnectionOffset->setValue(0);
  145. } else {
  146. ui->spinBox_ConnectionOffset->setDisabled(false);
  147. }
  148. }
  149. void Editor::onConnectionOffsetChanged(int newOffset) {
  150. ui->spinBox_ConnectionOffset->blockSignals(true);
  151. ui->spinBox_ConnectionOffset->setValue(newOffset);
  152. ui->spinBox_ConnectionOffset->blockSignals(false);
  153. }
  154. void Editor::setConnectionsVisibility(bool visible) {
  155. for (QGraphicsPixmapItem* item : map->connection_items) {
  156. item->setVisible(visible);
  157. item->setActive(visible);
  158. }
  159. }
  160. void Editor::setMap(QString map_name) {
  161. if (map_name.isNull()) {
  162. return;
  163. }
  164. if (project) {
  165. map = project->loadMap(map_name);
  166. displayMap();
  167. selected_events->clear();
  168. updateSelectedObjects();
  169. }
  170. }
  171. void Editor::mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item) {
  172. if (map_edit_mode == "paint") {
  173. item->paint(event);
  174. } else if (map_edit_mode == "fill") {
  175. item->floodFill(event);
  176. } else if (map_edit_mode == "pick") {
  177. item->pick(event);
  178. } else if (map_edit_mode == "select") {
  179. item->select(event);
  180. }
  181. }
  182. void Editor::mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixmapItem *item) {
  183. if (map_edit_mode == "paint") {
  184. item->paint(event);
  185. } else if (map_edit_mode == "fill") {
  186. item->floodFill(event);
  187. } else if (map_edit_mode == "pick") {
  188. item->pick(event);
  189. } else if (map_edit_mode == "select") {
  190. item->select(event);
  191. }
  192. }
  193. void Editor::displayMap() {
  194. scene = new QGraphicsScene;
  195. map_item = new MapPixmapItem(map);
  196. connect(map_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*,MapPixmapItem*)),
  197. this, SLOT(mouseEvent_map(QGraphicsSceneMouseEvent*,MapPixmapItem*)));
  198. map_item->draw();
  199. scene->addItem(map_item);
  200. collision_item = new CollisionPixmapItem(map);
  201. connect(collision_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*,CollisionPixmapItem*)),
  202. this, SLOT(mouseEvent_collision(QGraphicsSceneMouseEvent*,CollisionPixmapItem*)));
  203. collision_item->draw();
  204. scene->addItem(collision_item);
  205. objects_group = new EventGroup;
  206. scene->addItem(objects_group);
  207. if (map_item) {
  208. map_item->setVisible(false);
  209. }
  210. if (collision_item) {
  211. collision_item->setVisible(false);
  212. }
  213. if (objects_group) {
  214. objects_group->setVisible(false);
  215. }
  216. int tw = 16;
  217. int th = 16;
  218. scene->setSceneRect(
  219. -6 * tw,
  220. -6 * th,
  221. map_item->pixmap().width() + 12 * tw,
  222. map_item->pixmap().height() + 12 * th
  223. );
  224. displayMetatiles();
  225. displayCollisionMetatiles();
  226. displayElevationMetatiles();
  227. displayMapObjects();
  228. displayMapConnections();
  229. displayMapBorder();
  230. displayMapGrid();
  231. }
  232. void Editor::displayMetatiles() {
  233. scene_metatiles = new QGraphicsScene;
  234. metatiles_item = new MetatilesPixmapItem(map);
  235. metatiles_item->draw();
  236. scene_metatiles->addItem(metatiles_item);
  237. }
  238. void Editor::displayCollisionMetatiles() {
  239. scene_collision_metatiles = new QGraphicsScene;
  240. collision_metatiles_item = new CollisionMetatilesPixmapItem(map);
  241. collision_metatiles_item->draw();
  242. scene_collision_metatiles->addItem(collision_metatiles_item);
  243. }
  244. void Editor::displayElevationMetatiles() {
  245. scene_elevation_metatiles = new QGraphicsScene;
  246. elevation_metatiles_item = new ElevationMetatilesPixmapItem(map);
  247. elevation_metatiles_item->draw();
  248. scene_elevation_metatiles->addItem(elevation_metatiles_item);
  249. }
  250. void Editor::displayMapObjects() {
  251. for (QGraphicsItem *child : objects_group->childItems()) {
  252. objects_group->removeFromGroup(child);
  253. }
  254. QList<Event *> events = map->getAllEvents();
  255. project->loadObjectPixmaps(events);
  256. for (Event *event : events) {
  257. addMapObject(event);
  258. }
  259. //objects_group->setFiltersChildEvents(false);
  260. objects_group->setHandlesChildEvents(false);
  261. emit objectsChanged();
  262. }
  263. DraggablePixmapItem *Editor::addMapObject(Event *event) {
  264. DraggablePixmapItem *object = new DraggablePixmapItem(event);
  265. object->editor = this;
  266. objects_group->addToGroup(object);
  267. return object;
  268. }
  269. void Editor::displayMapConnections() {
  270. for (Connection *connection : map->connections) {
  271. if (connection->direction == "dive" || connection->direction == "emerge") {
  272. continue;
  273. }
  274. Map *connected_map = project->getMap(connection->map_name);
  275. QPixmap pixmap = connected_map->renderConnection(*connection);
  276. int offset = connection->offset.toInt(nullptr, 0);
  277. int x = 0, y = 0;
  278. if (connection->direction == "up") {
  279. x = offset * 16;
  280. y = -pixmap.height();
  281. } else if (connection->direction == "down") {
  282. x = offset * 16;
  283. y = map->getHeight() * 16;
  284. } else if (connection->direction == "left") {
  285. x = -pixmap.width();
  286. y = offset * 16;
  287. } else if (connection->direction == "right") {
  288. x = map->getWidth() * 16;
  289. y = offset * 16;
  290. }
  291. QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
  292. item->setZValue(-1);
  293. item->setX(x);
  294. item->setY(y);
  295. scene->addItem(item);
  296. map->connection_items.insert(connection->direction, item);
  297. }
  298. }
  299. void Editor::displayMapBorder() {
  300. QPixmap pixmap = map->renderBorder();
  301. for (int y = -6; y < map->getHeight() + 6; y += 2)
  302. for (int x = -6; x < map->getWidth() + 6; x += 2) {
  303. QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
  304. item->setX(x * 16);
  305. item->setY(y * 16);
  306. item->setZValue(-2);
  307. scene->addItem(item);
  308. }
  309. }
  310. void Editor::displayMapGrid() {
  311. int pixelWidth = map->getWidth() * 16;
  312. int pixelHeight = map->getHeight() * 16;
  313. for (int i = 0; i <= map->getWidth(); i++) {
  314. int x = i * 16;
  315. QGraphicsLineItem *line = scene->addLine(x, 0, x, pixelHeight);
  316. line->setVisible(ui->checkBox_ToggleGrid->isChecked());
  317. connect(ui->checkBox_ToggleGrid, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);});
  318. }
  319. for (int j = 0; j <= map->getHeight(); j++) {
  320. int y = j * 16;
  321. QGraphicsLineItem *line = scene->addLine(0, y, pixelWidth, y);
  322. line->setVisible(ui->checkBox_ToggleGrid->isChecked());
  323. connect(ui->checkBox_ToggleGrid, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);});
  324. }
  325. }
  326. void Editor::updateConnectionOffset(int offset) {
  327. if (!connection_item)
  328. return;
  329. connection_item->blockSignals(true);
  330. connection_item->connection->offset = QString::number(offset);
  331. if (connection_item->connection->direction == "up" || connection_item->connection->direction == "down") {
  332. connection_item->setX(connection_item->initialX + (offset - connection_item->initialOffset) * 16);
  333. } else {
  334. connection_item->setY(connection_item->initialY + (offset - connection_item->initialOffset) * 16);
  335. }
  336. connection_item->blockSignals(false);
  337. }
  338. void Editor::updateConnectionMap(QString mapName, QString direction) {
  339. if (!mapName.isEmpty() && !project->mapNames->contains(mapName)) {
  340. qDebug() << "Invalid map name " << mapName << " specified for connection.";
  341. return;
  342. }
  343. if (connection_item) {
  344. // Find the connection we are updating.
  345. bool foundConnection = false;
  346. for (Connection* connection : map->connections) {
  347. if (connection->direction == direction) {
  348. foundConnection = true;
  349. if (mapName.isEmpty()) {
  350. map->connections.removeOne(connection);
  351. } else {
  352. connection->map_name = mapName;
  353. }
  354. break;
  355. }
  356. }
  357. } else if (!mapName.isEmpty()) {
  358. // Create a brand new connection.
  359. Connection* newConnection = new Connection;
  360. newConnection->direction = direction;
  361. newConnection->offset = "0";
  362. newConnection->map_name = mapName;
  363. map->connections.append(newConnection);
  364. }
  365. showCurrentConnectionMap(direction);
  366. }
  367. void MetatilesPixmapItem::paintTileChanged(Map *map) {
  368. draw();
  369. }
  370. void MetatilesPixmapItem::draw() {
  371. setPixmap(map->renderMetatiles());
  372. }
  373. void MetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
  374. int x = ((int)pos.x()) / 16;
  375. int y = ((int)pos.y()) / 16;
  376. int width = pixmap().width() / 16;
  377. int height = pixmap().height() / 16;
  378. if (x < 0 || x >= width || y < 0 || y >= height) {
  379. map->clearHoveredMetatile();
  380. } else {
  381. int block = y * width + x;
  382. map->hoveredMetatileChanged(block);
  383. }
  384. }
  385. void MetatilesPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
  386. updateCurHoveredMetatile(event->pos());
  387. }
  388. void MetatilesPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
  389. map->clearHoveredMetatile();
  390. }
  391. void MetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  392. QPointF pos = event->pos();
  393. int x = ((int)pos.x()) / 16;
  394. int y = ((int)pos.y()) / 16;
  395. map->paint_metatile_initial_x = x;
  396. map->paint_metatile_initial_y = y;
  397. updateSelection(event->pos(), event->button());
  398. }
  399. void MetatilesPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  400. updateCurHoveredMetatile(event->pos());
  401. Qt::MouseButton button = event->button();
  402. if (button == Qt::MouseButton::NoButton) {
  403. Qt::MouseButtons heldButtons = event->buttons();
  404. if (heldButtons & Qt::RightButton) {
  405. button = Qt::RightButton;
  406. } else if (heldButtons & Qt::LeftButton) {
  407. button = Qt::LeftButton;
  408. }
  409. }
  410. updateSelection(event->pos(), button);
  411. }
  412. void MetatilesPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  413. updateSelection(event->pos(), event->button());
  414. }
  415. void MetatilesPixmapItem::updateSelection(QPointF pos, Qt::MouseButton button) {
  416. int x = ((int)pos.x()) / 16;
  417. int y = ((int)pos.y()) / 16;
  418. int width = pixmap().width() / 16;
  419. int height = pixmap().height() / 16;
  420. if ((x >= 0 && x < width) && (y >=0 && y < height)) {
  421. int baseTileX = x < map->paint_metatile_initial_x ? x : map->paint_metatile_initial_x;
  422. int baseTileY = y < map->paint_metatile_initial_y ? y : map->paint_metatile_initial_y;
  423. map->paint_tile = baseTileY * 8 + baseTileX;
  424. map->paint_tile_width = abs(map->paint_metatile_initial_x - x) + 1;
  425. map->paint_tile_height = abs(map->paint_metatile_initial_y - y) + 1;
  426. map->smart_paths_enabled = button == Qt::RightButton
  427. && map->paint_tile_width == 3
  428. && map->paint_tile_height == 3;
  429. emit map->paintTileChanged(map);
  430. }
  431. }
  432. void CollisionMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
  433. int x = ((int)pos.x()) / 16;
  434. int y = ((int)pos.y()) / 16;
  435. int width = pixmap().width() / 16;
  436. int height = pixmap().height() / 16;
  437. if (x < 0 || x >= width || y < 0 || y >= height) {
  438. map->clearHoveredCollisionTile();
  439. } else {
  440. int collision = y * width + x;
  441. map->hoveredCollisionTileChanged(collision);
  442. }
  443. }
  444. QVariant ConnectionPixmapItem::itemChange(GraphicsItemChange change, const QVariant &value)
  445. {
  446. if (change == ItemPositionChange) {
  447. QPointF newPos = value.toPointF();
  448. qreal x, y;
  449. int newOffset = initialOffset;
  450. if (connection->direction == "up" || connection->direction == "down") {
  451. x = round(newPos.x() / 16) * 16;
  452. newOffset += (x - initialX) / 16;
  453. }
  454. else {
  455. x = initialX;
  456. }
  457. if (connection->direction == "right" || connection->direction == "left") {
  458. y = round(newPos.y() / 16) * 16;
  459. newOffset += (y - initialY) / 16;
  460. }
  461. else {
  462. y = initialY;
  463. }
  464. emit connectionMoved(newOffset);
  465. connection->offset = QString::number(newOffset);
  466. return QPointF(x, y);
  467. }
  468. else {
  469. return QGraphicsItem::itemChange(change, value);
  470. }
  471. }
  472. void ConnectionPixmapItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
  473. QPointF pos = event->pos();
  474. qDebug() << "enter: " << pos.x() << ", " << pos.y();
  475. }
  476. void ConnectionPixmapItem::dragMoveEvent(QGraphicsSceneDragDropEvent *event) {
  477. QPointF pos = event->pos();
  478. qDebug() << "drag: " << pos.x() << ", " << pos.y();
  479. }
  480. void ConnectionPixmapItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) {
  481. QPointF pos = event->pos();
  482. qDebug() << "leave: " << pos.x() << ", " << pos.y();
  483. }
  484. void ConnectionPixmapItem::dropEvent(QGraphicsSceneDragDropEvent *event) {
  485. QPointF pos = event->pos();
  486. qDebug() << "drop: " << pos.x() << ", " << pos.y();
  487. }
  488. void ConnectionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {
  489. QPointF pos = event->pos();
  490. }
  491. void ElevationMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
  492. int x = ((int)pos.x()) / 16;
  493. int y = ((int)pos.y()) / 16;
  494. int width = pixmap().width() / 16;
  495. int height = pixmap().height() / 16;
  496. if (x < 0 || x >= width || y < 0 || y >= height) {
  497. map->clearHoveredElevationTile();
  498. } else {
  499. int elevation = y * width + x;
  500. map->hoveredElevationTileChanged(elevation);
  501. }
  502. }
  503. void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
  504. if (map) {
  505. QPointF pos = event->pos();
  506. int x = (int)(pos.x()) / 16;
  507. int y = (int)(pos.y()) / 16;
  508. if (map->smart_paths_enabled) {
  509. paintSmartPath(x, y);
  510. } else {
  511. paintNormal(x, y);
  512. }
  513. if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  514. map->commit();
  515. }
  516. draw();
  517. }
  518. }
  519. void MapPixmapItem::paintNormal(int x, int y) {
  520. // Snap the selected position to the top-left of the block boundary.
  521. // This allows painting via dragging the mouse to tile the painted region.
  522. int xDiff = x - map->paint_tile_initial_x;
  523. int yDiff = y - map->paint_tile_initial_y;
  524. if (xDiff < 0 && xDiff % map->paint_tile_width != 0) xDiff -= map->paint_tile_width;
  525. if (yDiff < 0 && yDiff % map->paint_tile_height != 0) yDiff -= map->paint_tile_height;
  526. x = map->paint_tile_initial_x + (xDiff / map->paint_tile_width) * map->paint_tile_width;
  527. y = map->paint_tile_initial_y + (yDiff / map->paint_tile_height) * map->paint_tile_height;
  528. for (int i = 0; i < map->paint_tile_width && i + x < map->getWidth(); i++)
  529. for (int j = 0; j < map->paint_tile_height && j + y < map->getHeight(); j++) {
  530. int actualX = i + x;
  531. int actualY = j + y;
  532. Block *block = map->getBlock(actualX, actualY);
  533. if (block) {
  534. block->tile = map->paint_tile + i + (j * 8);
  535. map->_setBlock(actualX, actualY, *block);
  536. }
  537. }
  538. }
  539. // These are tile offsets from the top-left tile in the 3x3 smart path selection.
  540. // Each entry is for one possibility from the marching squares value for a tile.
  541. // (Marching Squares: https://en.wikipedia.org/wiki/Marching_squares)
  542. QList<int> MapPixmapItem::smartPathTable = QList<int>({
  543. 8 + 1, // 0000
  544. 8 + 1, // 0001
  545. 8 + 1, // 0010
  546. 16 + 0, // 0011
  547. 8 + 1, // 0100
  548. 8 + 1, // 0101
  549. 0 + 0, // 0110
  550. 8 + 0, // 0111
  551. 8 + 1, // 1000
  552. 16 + 2, // 1001
  553. 8 + 1, // 1010
  554. 16 + 1, // 1011
  555. 0 + 2, // 1100
  556. 8 + 2, // 1101
  557. 0 + 1, // 1110
  558. 8 + 1, // 1111
  559. });
  560. #define IS_SMART_PATH_TILE(block) ((block->tile >= map->paint_tile && block->tile < map->paint_tile + 3) \
  561. || (block->tile >= map->paint_tile + 8 && block->tile < map->paint_tile + 11) \
  562. || (block->tile >= map->paint_tile + 16 && block->tile < map->paint_tile + 19))
  563. void MapPixmapItem::paintSmartPath(int x, int y) {
  564. // Smart path should never be enabled without a 3x3 block selection.
  565. if (map->paint_tile_width != 3 || map->paint_tile_height != 3) return;
  566. // Shift to the middle tile of the smart path selection.
  567. int openTile = map->paint_tile + 8 + 1;
  568. // Fill the region with the open tile.
  569. for (int i = -1; i <= 1 && i + x < map->getWidth() && i + x >= 0; i++)
  570. for (int j = -1; j <= 1 && j + y < map->getHeight() && j + y >= 0; j++) {
  571. int actualX = i + x;
  572. int actualY = j + y;
  573. Block *block = map->getBlock(actualX, actualY);
  574. if (block) {
  575. block->tile = openTile;
  576. map->_setBlock(actualX, actualY, *block);
  577. }
  578. }
  579. // Go back and resolve the edge tiles
  580. for (int i = -2; i <= 2 && i + x < map->getWidth() && i + x >= 0; i++)
  581. for (int j = -2; j <= 2 && j + y < map->getHeight() && j + y >= 0; j++) {
  582. // Ignore the corners, which can't possible be affected by the smart path.
  583. if ((i == -2 && j == -2) || (i == 2 && j == -2) ||
  584. (i == -2 && j == 2) || (i == 2 && j == 2))
  585. continue;
  586. // Ignore tiles that aren't part of the smart path set.
  587. int actualX = i + x;
  588. int actualY = j + y;
  589. Block *block = map->getBlock(actualX, actualY);
  590. if (!block || !IS_SMART_PATH_TILE(block)) {
  591. continue;
  592. }
  593. int id = 0;
  594. Block *top = map->getBlock(actualX, actualY - 1);
  595. Block *right = map->getBlock(actualX + 1, actualY);
  596. Block *bottom = map->getBlock(actualX, actualY + 1);
  597. Block *left = map->getBlock(actualX - 1, actualY);
  598. // Get marching squares value, to determine which tile to use.
  599. if (top && IS_SMART_PATH_TILE(top))
  600. id += 1;
  601. if (right && IS_SMART_PATH_TILE(right))
  602. id += 2;
  603. if (bottom && IS_SMART_PATH_TILE(bottom))
  604. id += 4;
  605. if (left && IS_SMART_PATH_TILE(left))
  606. id += 8;
  607. if (block) {
  608. qDebug() << "tile: " << block->tile << "base: " << map->paint_tile << "id: " << id;
  609. }
  610. block->tile = map->paint_tile + smartPathTable[id];;
  611. map->_setBlock(actualX, actualY, *block);
  612. }
  613. }
  614. void MapPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
  615. if (map) {
  616. QPointF pos = event->pos();
  617. int x = (int)(pos.x()) / 16;
  618. int y = (int)(pos.y()) / 16;
  619. map->floodFill(x, y, map->paint_tile);
  620. draw();
  621. }
  622. }
  623. void MapPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
  624. QPointF pos = event->pos();
  625. int x = (int)(pos.x()) / 16;
  626. int y = (int)(pos.y()) / 16;
  627. Block *block = map->getBlock(x, y);
  628. if (block) {
  629. map->paint_tile = block->tile;
  630. map->paint_tile_width = 1;
  631. map->paint_tile_height = 1;
  632. emit map->paintTileChanged(map);
  633. }
  634. }
  635. #define SWAP(a, b) do { if (a != b) { a ^= b; b ^= a; a ^= b; } } while (0)
  636. void MapPixmapItem::select(QGraphicsSceneMouseEvent *event) {
  637. QPointF pos = event->pos();
  638. int x = (int)(pos.x()) / 16;
  639. int y = (int)(pos.y()) / 16;
  640. if (event->type() == QEvent::GraphicsSceneMousePress) {
  641. selection_origin = QPoint(x, y);
  642. selection.clear();
  643. } else if (event->type() == QEvent::GraphicsSceneMouseMove) {
  644. if (event->buttons() & Qt::LeftButton) {
  645. selection.clear();
  646. selection.append(QPoint(x, y));
  647. }
  648. } else if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  649. if (!selection.isEmpty()) {
  650. QPoint pos = selection.last();
  651. int x1 = selection_origin.x();
  652. int y1 = selection_origin.y();
  653. int x2 = pos.x();
  654. int y2 = pos.y();
  655. if (x1 > x2) SWAP(x1, x2);
  656. if (y1 > y2) SWAP(y1, y2);
  657. selection.clear();
  658. for (int y = y1; y <= y2; y++) {
  659. for (int x = x1; x <= x2; x++) {
  660. selection.append(QPoint(x, y));
  661. }
  662. }
  663. qDebug() << QString("selected (%1, %2) -> (%3, %4)").arg(x1).arg(y1).arg(x2).arg(y2);
  664. }
  665. }
  666. }
  667. void MapPixmapItem::draw() {
  668. if (map) {
  669. setPixmap(map->render());
  670. }
  671. }
  672. void MapPixmapItem::undo() {
  673. if (map) {
  674. map->undo();
  675. draw();
  676. }
  677. }
  678. void MapPixmapItem::redo() {
  679. if (map) {
  680. map->redo();
  681. draw();
  682. }
  683. }
  684. void MapPixmapItem::updateCurHoveredTile(QPointF pos) {
  685. int x = ((int)pos.x()) / 16;
  686. int y = ((int)pos.y()) / 16;
  687. int blockIndex = y * map->getWidth() + x;
  688. if (x < 0 || x >= map->getWidth() || y < 0 || y >= map->getHeight()) {
  689. map->clearHoveredTile();
  690. } else {
  691. int tile = map->blockdata->blocks->at(blockIndex).tile;
  692. map->hoveredTileChanged(x, y, tile);
  693. }
  694. }
  695. void MapPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
  696. updateCurHoveredTile(event->pos());
  697. }
  698. void MapPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
  699. map->clearHoveredTile();
  700. }
  701. void MapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  702. QPointF pos = event->pos();
  703. int x = ((int)pos.x()) / 16;
  704. int y = ((int)pos.y()) / 16;
  705. map->paint_tile_initial_x = x;
  706. map->paint_tile_initial_y = y;
  707. emit mouseEvent(event, this);
  708. }
  709. void MapPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  710. updateCurHoveredTile(event->pos());
  711. emit mouseEvent(event, this);
  712. }
  713. void MapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  714. emit mouseEvent(event, this);
  715. }
  716. void CollisionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  717. emit mouseEvent(event, this);
  718. }
  719. void CollisionPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  720. emit mouseEvent(event, this);
  721. }
  722. void CollisionPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  723. emit mouseEvent(event, this);
  724. }
  725. void CollisionPixmapItem::draw() {
  726. if (map) {
  727. setPixmap(map->renderCollision());
  728. }
  729. }
  730. void CollisionPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
  731. if (map) {
  732. QPointF pos = event->pos();
  733. int x = (int)(pos.x()) / 16;
  734. int y = (int)(pos.y()) / 16;
  735. Block *block = map->getBlock(x, y);
  736. if (block) {
  737. if (map->paint_collision >= 0) {
  738. block->collision = map->paint_collision;
  739. }
  740. if (map->paint_elevation >= 0) {
  741. block->elevation = map->paint_elevation;
  742. }
  743. map->_setBlock(x, y, *block);
  744. }
  745. if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  746. map->commit();
  747. }
  748. draw();
  749. }
  750. }
  751. void CollisionPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
  752. if (map) {
  753. QPointF pos = event->pos();
  754. int x = (int)(pos.x()) / 16;
  755. int y = (int)(pos.y()) / 16;
  756. bool collision = map->paint_collision >= 0;
  757. bool elevation = map->paint_elevation >= 0;
  758. if (collision && elevation) {
  759. map->floodFillCollisionElevation(x, y, map->paint_collision, map->paint_elevation);
  760. } else if (collision) {
  761. map->floodFillCollision(x, y, map->paint_collision);
  762. } else if (elevation) {
  763. map->floodFillElevation(x, y, map->paint_elevation);
  764. }
  765. draw();
  766. }
  767. }
  768. void CollisionPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
  769. QPointF pos = event->pos();
  770. int x = (int)(pos.x()) / 16;
  771. int y = (int)(pos.y()) / 16;
  772. Block *block = map->getBlock(x, y);
  773. if (block) {
  774. map->paint_collision = block->collision;
  775. map->paint_elevation = block->elevation;
  776. emit map->paintCollisionChanged(map);
  777. }
  778. }
  779. void DraggablePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *mouse) {
  780. active = true;
  781. clicking = true;
  782. last_x = (mouse->pos().x() + this->pos().x()) / 16;
  783. last_y = (mouse->pos().y() + this->pos().y()) / 16;
  784. //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("y"));
  785. }
  786. void DraggablePixmapItem::move(int x, int y) {
  787. event->setX(event->x() + x);
  788. event->setY(event->y() + y);
  789. updatePosition();
  790. emitPositionChanged();
  791. }
  792. void DraggablePixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouse) {
  793. if (active) {
  794. int x = (mouse->pos().x() + this->pos().x()) / 16;
  795. int y = (mouse->pos().y() + this->pos().y()) / 16;
  796. if (x != last_x || y != last_y) {
  797. clicking = false;
  798. if (editor->selected_events->contains(this)) {
  799. for (DraggablePixmapItem *item : *editor->selected_events) {
  800. item->move(x - last_x, y - last_y);
  801. }
  802. } else {
  803. move(x - last_x, y - last_y);
  804. }
  805. last_x = x;
  806. last_y = y;
  807. //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("x"));
  808. }
  809. }
  810. }
  811. void DraggablePixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouse) {
  812. if (clicking) {
  813. this->editor->selectMapObject(this, mouse->modifiers() & Qt::ControlModifier);
  814. this->editor->updateSelectedObjects();
  815. }
  816. active = false;
  817. }
  818. QList<DraggablePixmapItem *> *Editor::getObjects() {
  819. QList<DraggablePixmapItem *> *list = new QList<DraggablePixmapItem *>;
  820. for (Event *event : map->getAllEvents()) {
  821. for (QGraphicsItem *child : objects_group->childItems()) {
  822. DraggablePixmapItem *item = (DraggablePixmapItem *)child;
  823. if (item->event == event) {
  824. list->append(item);
  825. break;
  826. }
  827. }
  828. }
  829. return list;
  830. }
  831. void Editor::redrawObject(DraggablePixmapItem *item) {
  832. if (item) {
  833. item->setPixmap(item->event->pixmap);
  834. if (selected_events && selected_events->contains(item)) {
  835. QImage image = item->pixmap().toImage();
  836. QPainter painter(&image);
  837. painter.setPen(QColor(250, 100, 25));
  838. painter.drawRect(0, 0, image.width() - 1, image.height() - 1);
  839. painter.end();
  840. item->setPixmap(QPixmap::fromImage(image));
  841. }
  842. }
  843. }
  844. void Editor::updateSelectedObjects() {
  845. for (DraggablePixmapItem *item : *(getObjects())) {
  846. redrawObject(item);
  847. }
  848. emit selectedObjectsChanged();
  849. }
  850. void Editor::selectMapObject(DraggablePixmapItem *object) {
  851. selectMapObject(object, false);
  852. }
  853. void Editor::selectMapObject(DraggablePixmapItem *object, bool toggle) {
  854. if (selected_events && object) {
  855. if (selected_events->contains(object)) {
  856. if (toggle) {
  857. selected_events->removeOne(object);
  858. }
  859. } else {
  860. if (!toggle) {
  861. selected_events->clear();
  862. }
  863. selected_events->append(object);
  864. }
  865. updateSelectedObjects();
  866. }
  867. }
  868. DraggablePixmapItem* Editor::addNewEvent() {
  869. return addNewEvent("object");
  870. }
  871. DraggablePixmapItem* Editor::addNewEvent(QString event_type) {
  872. if (project && map) {
  873. Event *event = new Event;
  874. event->put("map_name", map->name);
  875. event->put("event_type", event_type);
  876. map->addEvent(event);
  877. project->loadObjectPixmaps(map->getAllEvents());
  878. DraggablePixmapItem *object = addMapObject(event);
  879. return object;
  880. }
  881. return NULL;
  882. }
  883. void Editor::deleteEvent(Event *event) {
  884. Map *map = project->getMap(event->get("map_name"));
  885. if (map) {
  886. map->removeEvent(event);
  887. }
  888. //selected_events->removeAll(event);
  889. //updateSelectedObjects();
  890. }
  891. // dunno how to detect bubbling. QMouseEvent::isAccepted seems to always be true
  892. // check if selected_events changed instead. this has the side effect of deselecting
  893. // when you click on a selected event, since selected_events doesn't change.
  894. QList<DraggablePixmapItem *> selected_events_test;
  895. bool clicking = false;
  896. void Editor::objectsView_onMousePress(QMouseEvent *event) {
  897. clicking = true;
  898. selected_events_test = *selected_events;
  899. }
  900. void Editor::objectsView_onMouseMove(QMouseEvent *event) {
  901. clicking = false;
  902. }
  903. void Editor::objectsView_onMouseRelease(QMouseEvent *event) {
  904. if (clicking) {
  905. if (selected_events_test.length()) {
  906. if (selected_events_test.length() == selected_events->length()) {
  907. bool deselect = true;
  908. for (int i = 0; i < selected_events_test.length(); i++) {
  909. if (selected_events_test.at(i) != selected_events->at(i)) {
  910. deselect = false;
  911. break;
  912. }
  913. }
  914. if (deselect) {
  915. selected_events->clear();
  916. updateSelectedObjects();
  917. }
  918. }
  919. }
  920. clicking = false;
  921. }
  922. }