Brak opisu

editor.cpp 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. #include "editor.h"
  2. #include <QPainter>
  3. #include <QMouseEvent>
  4. Editor::Editor()
  5. {
  6. selected_events = new QList<DraggablePixmapItem*>;
  7. }
  8. void Editor::saveProject() {
  9. if (project) {
  10. project->saveAllMaps();
  11. project->saveAllDataStructures();
  12. }
  13. }
  14. void Editor::save() {
  15. if (project && map) {
  16. project->saveMap(map);
  17. project->saveAllDataStructures();
  18. }
  19. }
  20. void Editor::undo() {
  21. if (current_view) {
  22. ((MapPixmapItem*)current_view)->undo();
  23. }
  24. }
  25. void Editor::redo() {
  26. if (current_view) {
  27. ((MapPixmapItem*)current_view)->redo();
  28. }
  29. }
  30. void Editor::setEditingMap() {
  31. current_view = map_item;
  32. if (map_item) {
  33. map_item->draw();
  34. map_item->setVisible(true);
  35. map_item->setEnabled(true);
  36. }
  37. if (collision_item) {
  38. collision_item->setVisible(false);
  39. }
  40. if (objects_group) {
  41. objects_group->setVisible(false);
  42. }
  43. }
  44. void Editor::setEditingCollision() {
  45. current_view = collision_item;
  46. if (collision_item) {
  47. collision_item->draw();
  48. collision_item->setVisible(true);
  49. }
  50. if (map_item) {
  51. map_item->setVisible(false);
  52. }
  53. if (objects_group) {
  54. objects_group->setVisible(false);
  55. }
  56. }
  57. void Editor::setEditingObjects() {
  58. current_view = map_item;
  59. if (objects_group) {
  60. objects_group->setVisible(true);
  61. }
  62. if (map_item) {
  63. map_item->setVisible(true);
  64. map_item->setEnabled(false);
  65. }
  66. if (collision_item) {
  67. collision_item->setVisible(false);
  68. }
  69. }
  70. void Editor::setMap(QString map_name) {
  71. if (map_name.isNull()) {
  72. return;
  73. }
  74. if (project) {
  75. map = project->loadMap(map_name);
  76. displayMap();
  77. selected_events->clear();
  78. updateSelectedObjects();
  79. }
  80. }
  81. void Editor::mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item) {
  82. if (map_edit_mode == "paint") {
  83. item->paint(event);
  84. } else if (map_edit_mode == "fill") {
  85. item->floodFill(event);
  86. } else if (map_edit_mode == "pick") {
  87. item->pick(event);
  88. } else if (map_edit_mode == "select") {
  89. item->select(event);
  90. }
  91. }
  92. void Editor::mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixmapItem *item) {
  93. if (map_edit_mode == "paint") {
  94. item->paint(event);
  95. } else if (map_edit_mode == "fill") {
  96. item->floodFill(event);
  97. } else if (map_edit_mode == "pick") {
  98. item->pick(event);
  99. } else if (map_edit_mode == "select") {
  100. item->select(event);
  101. }
  102. }
  103. void Editor::displayMap() {
  104. scene = new QGraphicsScene;
  105. map_item = new MapPixmapItem(map);
  106. connect(map_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*,MapPixmapItem*)),
  107. this, SLOT(mouseEvent_map(QGraphicsSceneMouseEvent*,MapPixmapItem*)));
  108. map_item->draw();
  109. scene->addItem(map_item);
  110. collision_item = new CollisionPixmapItem(map);
  111. connect(collision_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*,CollisionPixmapItem*)),
  112. this, SLOT(mouseEvent_collision(QGraphicsSceneMouseEvent*,CollisionPixmapItem*)));
  113. collision_item->draw();
  114. scene->addItem(collision_item);
  115. objects_group = new EventGroup;
  116. scene->addItem(objects_group);
  117. if (map_item) {
  118. map_item->setVisible(false);
  119. }
  120. if (collision_item) {
  121. collision_item->setVisible(false);
  122. }
  123. if (objects_group) {
  124. objects_group->setVisible(false);
  125. }
  126. int tw = 16;
  127. int th = 16;
  128. scene->setSceneRect(
  129. -6 * tw,
  130. -6 * th,
  131. map_item->pixmap().width() + 12 * tw,
  132. map_item->pixmap().height() + 12 * th
  133. );
  134. displayMetatiles();
  135. displayCollisionMetatiles();
  136. displayElevationMetatiles();
  137. displayMapObjects();
  138. displayMapConnections();
  139. displayMapBorder();
  140. }
  141. void Editor::displayMetatiles() {
  142. scene_metatiles = new QGraphicsScene;
  143. metatiles_item = new MetatilesPixmapItem(map);
  144. metatiles_item->draw();
  145. scene_metatiles->addItem(metatiles_item);
  146. }
  147. void Editor::displayCollisionMetatiles() {
  148. scene_collision_metatiles = new QGraphicsScene;
  149. collision_metatiles_item = new CollisionMetatilesPixmapItem(map);
  150. collision_metatiles_item->draw();
  151. scene_collision_metatiles->addItem(collision_metatiles_item);
  152. }
  153. void Editor::displayElevationMetatiles() {
  154. scene_elevation_metatiles = new QGraphicsScene;
  155. elevation_metatiles_item = new ElevationMetatilesPixmapItem(map);
  156. elevation_metatiles_item->draw();
  157. scene_elevation_metatiles->addItem(elevation_metatiles_item);
  158. }
  159. void Editor::displayMapObjects() {
  160. for (QGraphicsItem *child : objects_group->childItems()) {
  161. objects_group->removeFromGroup(child);
  162. }
  163. QList<Event *> events = map->getAllEvents();
  164. project->loadObjectPixmaps(events);
  165. for (Event *event : events) {
  166. addMapObject(event);
  167. }
  168. //objects_group->setFiltersChildEvents(false);
  169. objects_group->setHandlesChildEvents(false);
  170. emit objectsChanged();
  171. }
  172. DraggablePixmapItem *Editor::addMapObject(Event *event) {
  173. DraggablePixmapItem *object = new DraggablePixmapItem(event);
  174. object->editor = this;
  175. objects_group->addToGroup(object);
  176. return object;
  177. }
  178. void Editor::displayMapConnections() {
  179. for (Connection *connection : map->connections) {
  180. if (connection->direction == "dive" || connection->direction == "emerge") {
  181. continue;
  182. }
  183. Map *connected_map = project->getMap(connection->map_name);
  184. QPixmap pixmap = connected_map->renderConnection(*connection);
  185. int offset = connection->offset.toInt(nullptr, 0);
  186. int x = 0, y = 0;
  187. if (connection->direction == "up") {
  188. x = offset * 16;
  189. y = -pixmap.height();
  190. } else if (connection->direction == "down") {
  191. x = offset * 16;
  192. y = map->getHeight() * 16;
  193. } else if (connection->direction == "left") {
  194. x = -pixmap.width();
  195. y = offset * 16;
  196. } else if (connection->direction == "right") {
  197. x = map->getWidth() * 16;
  198. y = offset * 16;
  199. }
  200. QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
  201. item->setZValue(-1);
  202. item->setX(x);
  203. item->setY(y);
  204. scene->addItem(item);
  205. }
  206. }
  207. void Editor::displayMapBorder() {
  208. QPixmap pixmap = map->renderBorder();
  209. for (int y = -6; y < map->getHeight() + 6; y += 2)
  210. for (int x = -6; x < map->getWidth() + 6; x += 2) {
  211. QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
  212. item->setX(x * 16);
  213. item->setY(y * 16);
  214. item->setZValue(-2);
  215. scene->addItem(item);
  216. }
  217. }
  218. void MetatilesPixmapItem::paintTileChanged(Map *map) {
  219. draw();
  220. }
  221. void MetatilesPixmapItem::draw() {
  222. setPixmap(map->renderMetatiles());
  223. }
  224. void MetatilesPixmapItem::pick(uint tile) {
  225. map->paint_tile = tile;
  226. emit map->paintTileChanged(map);
  227. }
  228. void MetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
  229. int x = ((int)pos.x()) / 16;
  230. int y = ((int)pos.y()) / 16;
  231. int width = pixmap().width() / 16;
  232. int height = pixmap().height() / 16;
  233. if (x < 0 || x >= width || y < 0 || y >= height) {
  234. map->clearHoveredMetatile();
  235. } else {
  236. int block = y * width + x;
  237. map->hoveredMetatileChanged(block);
  238. }
  239. }
  240. void MetatilesPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
  241. updateCurHoveredMetatile(event->pos());
  242. }
  243. void MetatilesPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
  244. map->clearHoveredMetatile();
  245. }
  246. void MetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  247. QPointF pos = event->pos();
  248. int x = ((int)pos.x()) / 16;
  249. int y = ((int)pos.y()) / 16;
  250. //qDebug() << QString("(%1, %2)").arg(x).arg(y);
  251. int width = pixmap().width() / 16;
  252. int height = pixmap().height() / 16;
  253. if ((x >= 0 && x < width) && (y >=0 && y < height)) {
  254. pick(y * width + x);
  255. }
  256. }
  257. void MetatilesPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  258. updateCurHoveredMetatile(event->pos());
  259. mousePressEvent(event);
  260. }
  261. void MetatilesPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  262. mousePressEvent(event);
  263. }
  264. void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
  265. if (map) {
  266. QPointF pos = event->pos();
  267. int x = (int)(pos.x()) / 16;
  268. int y = (int)(pos.y()) / 16;
  269. Block *block = map->getBlock(x, y);
  270. if (block) {
  271. block->tile = map->paint_tile;
  272. map->_setBlock(x, y, *block);
  273. }
  274. if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  275. map->commit();
  276. }
  277. draw();
  278. }
  279. }
  280. void MapPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
  281. if (map) {
  282. QPointF pos = event->pos();
  283. int x = (int)(pos.x()) / 16;
  284. int y = (int)(pos.y()) / 16;
  285. map->floodFill(x, y, map->paint_tile);
  286. draw();
  287. }
  288. }
  289. void MapPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
  290. QPointF pos = event->pos();
  291. int x = (int)(pos.x()) / 16;
  292. int y = (int)(pos.y()) / 16;
  293. Block *block = map->getBlock(x, y);
  294. if (block) {
  295. map->paint_tile = block->tile;
  296. emit map->paintTileChanged(map);
  297. }
  298. }
  299. #define SWAP(a, b) do { if (a != b) { a ^= b; b ^= a; a ^= b; } } while (0)
  300. void MapPixmapItem::select(QGraphicsSceneMouseEvent *event) {
  301. QPointF pos = event->pos();
  302. int x = (int)(pos.x()) / 16;
  303. int y = (int)(pos.y()) / 16;
  304. if (event->type() == QEvent::GraphicsSceneMousePress) {
  305. selection_origin = QPoint(x, y);
  306. selection.clear();
  307. } else if (event->type() == QEvent::GraphicsSceneMouseMove) {
  308. if (event->buttons() & Qt::LeftButton) {
  309. selection.clear();
  310. selection.append(QPoint(x, y));
  311. }
  312. } else if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  313. if (!selection.isEmpty()) {
  314. QPoint pos = selection.last();
  315. int x1 = selection_origin.x();
  316. int y1 = selection_origin.y();
  317. int x2 = pos.x();
  318. int y2 = pos.y();
  319. if (x1 > x2) SWAP(x1, x2);
  320. if (y1 > y2) SWAP(y1, y2);
  321. selection.clear();
  322. for (int y = y1; y <= y2; y++) {
  323. for (int x = x1; x <= x2; x++) {
  324. selection.append(QPoint(x, y));
  325. }
  326. }
  327. qDebug() << QString("selected (%1, %2) -> (%3, %4)").arg(x1).arg(y1).arg(x2).arg(y2);
  328. }
  329. }
  330. }
  331. void MapPixmapItem::draw() {
  332. if (map) {
  333. setPixmap(map->render());
  334. }
  335. }
  336. void MapPixmapItem::undo() {
  337. if (map) {
  338. map->undo();
  339. draw();
  340. }
  341. }
  342. void MapPixmapItem::redo() {
  343. if (map) {
  344. map->redo();
  345. draw();
  346. }
  347. }
  348. void MapPixmapItem::updateCurHoveredTile(QPointF pos) {
  349. int x = ((int)pos.x()) / 16;
  350. int y = ((int)pos.y()) / 16;
  351. int blockIndex = y * map->getWidth() + x;
  352. if (x < 0 || x >= map->getWidth() || y < 0 || y >= map->getHeight()) {
  353. map->clearHoveredTile();
  354. } else {
  355. int tile = map->blockdata->blocks->at(blockIndex).tile;
  356. map->hoveredTileChanged(x, y, tile);
  357. }
  358. }
  359. void MapPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
  360. updateCurHoveredTile(event->pos());
  361. }
  362. void MapPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
  363. map->clearHoveredTile();
  364. }
  365. void MapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  366. emit mouseEvent(event, this);
  367. }
  368. void MapPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  369. updateCurHoveredTile(event->pos());
  370. emit mouseEvent(event, this);
  371. }
  372. void MapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  373. emit mouseEvent(event, this);
  374. }
  375. void CollisionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  376. emit mouseEvent(event, this);
  377. }
  378. void CollisionPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  379. emit mouseEvent(event, this);
  380. }
  381. void CollisionPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  382. emit mouseEvent(event, this);
  383. }
  384. void CollisionPixmapItem::draw() {
  385. if (map) {
  386. setPixmap(map->renderCollision());
  387. }
  388. }
  389. void CollisionPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
  390. if (map) {
  391. QPointF pos = event->pos();
  392. int x = (int)(pos.x()) / 16;
  393. int y = (int)(pos.y()) / 16;
  394. Block *block = map->getBlock(x, y);
  395. if (block) {
  396. if (map->paint_collision >= 0) {
  397. block->collision = map->paint_collision;
  398. }
  399. if (map->paint_elevation >= 0) {
  400. block->elevation = map->paint_elevation;
  401. }
  402. map->_setBlock(x, y, *block);
  403. }
  404. if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  405. map->commit();
  406. }
  407. draw();
  408. }
  409. }
  410. void CollisionPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
  411. if (map) {
  412. QPointF pos = event->pos();
  413. int x = (int)(pos.x()) / 16;
  414. int y = (int)(pos.y()) / 16;
  415. bool collision = map->paint_collision >= 0;
  416. bool elevation = map->paint_elevation >= 0;
  417. if (collision && elevation) {
  418. map->floodFillCollisionElevation(x, y, map->paint_collision, map->paint_elevation);
  419. } else if (collision) {
  420. map->floodFillCollision(x, y, map->paint_collision);
  421. } else if (elevation) {
  422. map->floodFillElevation(x, y, map->paint_elevation);
  423. }
  424. draw();
  425. }
  426. }
  427. void CollisionPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
  428. QPointF pos = event->pos();
  429. int x = (int)(pos.x()) / 16;
  430. int y = (int)(pos.y()) / 16;
  431. Block *block = map->getBlock(x, y);
  432. if (block) {
  433. map->paint_collision = block->collision;
  434. map->paint_elevation = block->elevation;
  435. emit map->paintCollisionChanged(map);
  436. }
  437. }
  438. void DraggablePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *mouse) {
  439. active = true;
  440. clicking = true;
  441. last_x = (mouse->pos().x() + this->pos().x()) / 16;
  442. last_y = (mouse->pos().y() + this->pos().y()) / 16;
  443. //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("y"));
  444. }
  445. void DraggablePixmapItem::move(int x, int y) {
  446. event->setX(event->x() + x);
  447. event->setY(event->y() + y);
  448. updatePosition();
  449. emitPositionChanged();
  450. }
  451. void DraggablePixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouse) {
  452. if (active) {
  453. int x = (mouse->pos().x() + this->pos().x()) / 16;
  454. int y = (mouse->pos().y() + this->pos().y()) / 16;
  455. if (x != last_x || y != last_y) {
  456. clicking = false;
  457. if (editor->selected_events->contains(this)) {
  458. for (DraggablePixmapItem *item : *editor->selected_events) {
  459. item->move(x - last_x, y - last_y);
  460. }
  461. } else {
  462. move(x - last_x, y - last_y);
  463. }
  464. last_x = x;
  465. last_y = y;
  466. //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("x"));
  467. }
  468. }
  469. }
  470. void DraggablePixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouse) {
  471. if (clicking) {
  472. this->editor->selectMapObject(this, mouse->modifiers() & Qt::ControlModifier);
  473. this->editor->updateSelectedObjects();
  474. }
  475. active = false;
  476. }
  477. QList<DraggablePixmapItem *> *Editor::getObjects() {
  478. QList<DraggablePixmapItem *> *list = new QList<DraggablePixmapItem *>;
  479. for (Event *event : map->getAllEvents()) {
  480. for (QGraphicsItem *child : objects_group->childItems()) {
  481. DraggablePixmapItem *item = (DraggablePixmapItem *)child;
  482. if (item->event == event) {
  483. list->append(item);
  484. break;
  485. }
  486. }
  487. }
  488. return list;
  489. }
  490. void Editor::redrawObject(DraggablePixmapItem *item) {
  491. if (item) {
  492. item->setPixmap(item->event->pixmap);
  493. if (selected_events && selected_events->contains(item)) {
  494. QImage image = item->pixmap().toImage();
  495. QPainter painter(&image);
  496. painter.setPen(QColor(250, 100, 25));
  497. painter.drawRect(0, 0, image.width() - 1, image.height() - 1);
  498. painter.end();
  499. item->setPixmap(QPixmap::fromImage(image));
  500. }
  501. }
  502. }
  503. void Editor::updateSelectedObjects() {
  504. for (DraggablePixmapItem *item : *(getObjects())) {
  505. redrawObject(item);
  506. }
  507. emit selectedObjectsChanged();
  508. }
  509. void Editor::selectMapObject(DraggablePixmapItem *object) {
  510. selectMapObject(object, false);
  511. }
  512. void Editor::selectMapObject(DraggablePixmapItem *object, bool toggle) {
  513. if (selected_events && object) {
  514. if (selected_events->contains(object)) {
  515. if (toggle) {
  516. selected_events->removeOne(object);
  517. }
  518. } else {
  519. if (!toggle) {
  520. selected_events->clear();
  521. }
  522. selected_events->append(object);
  523. }
  524. updateSelectedObjects();
  525. }
  526. }
  527. DraggablePixmapItem* Editor::addNewEvent() {
  528. return addNewEvent("object");
  529. }
  530. DraggablePixmapItem* Editor::addNewEvent(QString event_type) {
  531. if (project && map) {
  532. Event *event = new Event;
  533. event->put("map_name", map->name);
  534. event->put("event_type", event_type);
  535. map->addEvent(event);
  536. project->loadObjectPixmaps(map->getAllEvents());
  537. DraggablePixmapItem *object = addMapObject(event);
  538. return object;
  539. }
  540. return NULL;
  541. }
  542. void Editor::deleteEvent(Event *event) {
  543. Map *map = project->getMap(event->get("map_name"));
  544. if (map) {
  545. map->removeEvent(event);
  546. }
  547. //selected_events->removeAll(event);
  548. //updateSelectedObjects();
  549. }
  550. // dunno how to detect bubbling. QMouseEvent::isAccepted seems to always be true
  551. // check if selected_events changed instead. this has the side effect of deselecting
  552. // when you click on a selected event, since selected_events doesn't change.
  553. QList<DraggablePixmapItem *> selected_events_test;
  554. bool clicking = false;
  555. void Editor::objectsView_onMousePress(QMouseEvent *event) {
  556. clicking = true;
  557. selected_events_test = *selected_events;
  558. }
  559. void Editor::objectsView_onMouseMove(QMouseEvent *event) {
  560. clicking = false;
  561. }
  562. void Editor::objectsView_onMouseRelease(QMouseEvent *event) {
  563. if (clicking) {
  564. if (selected_events_test.length()) {
  565. if (selected_events_test.length() == selected_events->length()) {
  566. bool deselect = true;
  567. for (int i = 0; i < selected_events_test.length(); i++) {
  568. if (selected_events_test.at(i) != selected_events->at(i)) {
  569. deselect = false;
  570. break;
  571. }
  572. }
  573. if (deselect) {
  574. selected_events->clear();
  575. updateSelectedObjects();
  576. }
  577. }
  578. }
  579. clicking = false;
  580. }
  581. }