Brak opisu

editor.cpp 34KB

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