Nessuna descrizione

editor.cpp 34KB

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