Bez popisu

editor.cpp 18KB

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