Nessuna descrizione

editor.cpp 18KB

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