No Description

editor.cpp 34KB

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