No Description

editor.cpp 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  229. QPointF pos = event->pos();
  230. int x = ((int)pos.x()) / 16;
  231. int y = ((int)pos.y()) / 16;
  232. //qDebug() << QString("(%1, %2)").arg(x).arg(y);
  233. int width = pixmap().width() / 16;
  234. int height = pixmap().height() / 16;
  235. if ((x >= 0 && x < width) && (y >=0 && y < height)) {
  236. pick(y * width + x);
  237. }
  238. }
  239. void MetatilesPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  240. mousePressEvent(event);
  241. }
  242. void MetatilesPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  243. mousePressEvent(event);
  244. }
  245. void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
  246. if (map) {
  247. QPointF pos = event->pos();
  248. int x = (int)(pos.x()) / 16;
  249. int y = (int)(pos.y()) / 16;
  250. Block *block = map->getBlock(x, y);
  251. if (block) {
  252. block->tile = map->paint_tile;
  253. map->_setBlock(x, y, *block);
  254. }
  255. if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  256. map->commit();
  257. }
  258. draw();
  259. }
  260. }
  261. void MapPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
  262. if (map) {
  263. QPointF pos = event->pos();
  264. int x = (int)(pos.x()) / 16;
  265. int y = (int)(pos.y()) / 16;
  266. map->floodFill(x, y, map->paint_tile);
  267. draw();
  268. }
  269. }
  270. void MapPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
  271. QPointF pos = event->pos();
  272. int x = (int)(pos.x()) / 16;
  273. int y = (int)(pos.y()) / 16;
  274. Block *block = map->getBlock(x, y);
  275. if (block) {
  276. map->paint_tile = block->tile;
  277. emit map->paintTileChanged(map);
  278. }
  279. }
  280. #define SWAP(a, b) do { if (a != b) { a ^= b; b ^= a; a ^= b; } } while (0)
  281. void MapPixmapItem::select(QGraphicsSceneMouseEvent *event) {
  282. QPointF pos = event->pos();
  283. int x = (int)(pos.x()) / 16;
  284. int y = (int)(pos.y()) / 16;
  285. if (event->type() == QEvent::GraphicsSceneMousePress) {
  286. selection_origin = QPoint(x, y);
  287. selection.clear();
  288. } else if (event->type() == QEvent::GraphicsSceneMouseMove) {
  289. if (event->buttons() & Qt::LeftButton) {
  290. selection.clear();
  291. selection.append(QPoint(x, y));
  292. }
  293. } else if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  294. if (!selection.isEmpty()) {
  295. QPoint pos = selection.last();
  296. int x1 = selection_origin.x();
  297. int y1 = selection_origin.y();
  298. int x2 = pos.x();
  299. int y2 = pos.y();
  300. if (x1 > x2) SWAP(x1, x2);
  301. if (y1 > y2) SWAP(y1, y2);
  302. selection.clear();
  303. for (int y = y1; y <= y2; y++) {
  304. for (int x = x1; x <= x2; x++) {
  305. selection.append(QPoint(x, y));
  306. }
  307. }
  308. qDebug() << QString("selected (%1, %2) -> (%3, %4)").arg(x1).arg(y1).arg(x2).arg(y2);
  309. }
  310. }
  311. }
  312. void MapPixmapItem::draw() {
  313. if (map) {
  314. setPixmap(map->render());
  315. }
  316. }
  317. void MapPixmapItem::undo() {
  318. if (map) {
  319. map->undo();
  320. draw();
  321. }
  322. }
  323. void MapPixmapItem::redo() {
  324. if (map) {
  325. map->redo();
  326. draw();
  327. }
  328. }
  329. void MapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  330. emit mouseEvent(event, this);
  331. }
  332. void MapPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  333. emit mouseEvent(event, this);
  334. }
  335. void MapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  336. emit mouseEvent(event, this);
  337. }
  338. void CollisionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  339. emit mouseEvent(event, this);
  340. }
  341. void CollisionPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  342. emit mouseEvent(event, this);
  343. }
  344. void CollisionPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  345. emit mouseEvent(event, this);
  346. }
  347. void CollisionPixmapItem::draw() {
  348. if (map) {
  349. setPixmap(map->renderCollision());
  350. }
  351. }
  352. void CollisionPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
  353. if (map) {
  354. QPointF pos = event->pos();
  355. int x = (int)(pos.x()) / 16;
  356. int y = (int)(pos.y()) / 16;
  357. Block *block = map->getBlock(x, y);
  358. if (block) {
  359. if (map->paint_collision >= 0) {
  360. block->collision = map->paint_collision;
  361. }
  362. if (map->paint_elevation >= 0) {
  363. block->elevation = map->paint_elevation;
  364. }
  365. map->_setBlock(x, y, *block);
  366. }
  367. if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  368. map->commit();
  369. }
  370. draw();
  371. }
  372. }
  373. void CollisionPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
  374. if (map) {
  375. QPointF pos = event->pos();
  376. int x = (int)(pos.x()) / 16;
  377. int y = (int)(pos.y()) / 16;
  378. bool collision = map->paint_collision >= 0;
  379. bool elevation = map->paint_elevation >= 0;
  380. if (collision && elevation) {
  381. map->floodFillCollisionElevation(x, y, map->paint_collision, map->paint_elevation);
  382. } else if (collision) {
  383. map->floodFillCollision(x, y, map->paint_collision);
  384. } else if (elevation) {
  385. map->floodFillElevation(x, y, map->paint_elevation);
  386. }
  387. draw();
  388. }
  389. }
  390. void CollisionPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
  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. map->paint_collision = block->collision;
  397. map->paint_elevation = block->elevation;
  398. emit map->paintCollisionChanged(map);
  399. }
  400. }
  401. void DraggablePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *mouse) {
  402. active = true;
  403. clicking = true;
  404. last_x = (mouse->pos().x() + this->pos().x()) / 16;
  405. last_y = (mouse->pos().y() + this->pos().y()) / 16;
  406. //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("y"));
  407. }
  408. void DraggablePixmapItem::move(int x, int y) {
  409. event->setX(event->x() + x);
  410. event->setY(event->y() + y);
  411. updatePosition();
  412. emitPositionChanged();
  413. }
  414. void DraggablePixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouse) {
  415. if (active) {
  416. int x = (mouse->pos().x() + this->pos().x()) / 16;
  417. int y = (mouse->pos().y() + this->pos().y()) / 16;
  418. if (x != last_x || y != last_y) {
  419. clicking = false;
  420. if (editor->selected_events->contains(this)) {
  421. for (DraggablePixmapItem *item : *editor->selected_events) {
  422. item->move(x - last_x, y - last_y);
  423. }
  424. } else {
  425. move(x - last_x, y - last_y);
  426. }
  427. last_x = x;
  428. last_y = y;
  429. //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("x"));
  430. }
  431. }
  432. }
  433. void DraggablePixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouse) {
  434. if (clicking) {
  435. this->editor->selectMapObject(this, mouse->modifiers() & Qt::ControlModifier);
  436. this->editor->updateSelectedObjects();
  437. }
  438. active = false;
  439. }
  440. QList<DraggablePixmapItem *> *Editor::getObjects() {
  441. QList<DraggablePixmapItem *> *list = new QList<DraggablePixmapItem *>;
  442. for (Event *event : map->getAllEvents()) {
  443. for (QGraphicsItem *child : objects_group->childItems()) {
  444. DraggablePixmapItem *item = (DraggablePixmapItem *)child;
  445. if (item->event == event) {
  446. list->append(item);
  447. break;
  448. }
  449. }
  450. }
  451. return list;
  452. }
  453. void Editor::redrawObject(DraggablePixmapItem *item) {
  454. if (item) {
  455. item->setPixmap(item->event->pixmap);
  456. if (selected_events && selected_events->contains(item)) {
  457. QImage image = item->pixmap().toImage();
  458. QPainter painter(&image);
  459. painter.setPen(QColor(250, 100, 25));
  460. painter.drawRect(0, 0, image.width() - 1, image.height() - 1);
  461. painter.end();
  462. item->setPixmap(QPixmap::fromImage(image));
  463. }
  464. }
  465. }
  466. void Editor::updateSelectedObjects() {
  467. for (DraggablePixmapItem *item : *(getObjects())) {
  468. redrawObject(item);
  469. }
  470. emit selectedObjectsChanged();
  471. }
  472. void Editor::selectMapObject(DraggablePixmapItem *object) {
  473. selectMapObject(object, false);
  474. }
  475. void Editor::selectMapObject(DraggablePixmapItem *object, bool toggle) {
  476. if (selected_events && object) {
  477. if (selected_events->contains(object)) {
  478. if (toggle) {
  479. selected_events->removeOne(object);
  480. }
  481. } else {
  482. if (!toggle) {
  483. selected_events->clear();
  484. }
  485. selected_events->append(object);
  486. }
  487. updateSelectedObjects();
  488. }
  489. }
  490. DraggablePixmapItem* Editor::addNewEvent() {
  491. return addNewEvent("object");
  492. }
  493. DraggablePixmapItem* Editor::addNewEvent(QString event_type) {
  494. if (project && map) {
  495. Event *event = new Event;
  496. event->put("map_name", map->name);
  497. event->put("event_type", event_type);
  498. map->addEvent(event);
  499. project->loadObjectPixmaps(map->getAllEvents());
  500. DraggablePixmapItem *object = addMapObject(event);
  501. return object;
  502. }
  503. return NULL;
  504. }
  505. void Editor::deleteEvent(Event *event) {
  506. Map *map = project->getMap(event->get("map_name"));
  507. if (map) {
  508. map->removeEvent(event);
  509. }
  510. //selected_events->removeAll(event);
  511. //updateSelectedObjects();
  512. }
  513. // dunno how to detect bubbling. QMouseEvent::isAccepted seems to always be true
  514. // check if selected_events changed instead. this has the side effect of deselecting
  515. // when you click on a selected event, since selected_events doesn't change.
  516. QList<DraggablePixmapItem *> selected_events_test;
  517. bool clicking = false;
  518. void Editor::objectsView_onMousePress(QMouseEvent *event) {
  519. clicking = true;
  520. selected_events_test = *selected_events;
  521. }
  522. void Editor::objectsView_onMouseMove(QMouseEvent *event) {
  523. clicking = false;
  524. }
  525. void Editor::objectsView_onMouseRelease(QMouseEvent *event) {
  526. if (clicking) {
  527. if (selected_events_test.length()) {
  528. if (selected_events_test.length() == selected_events->length()) {
  529. bool deselect = true;
  530. for (int i = 0; i < selected_events_test.length(); i++) {
  531. if (selected_events_test.at(i) != selected_events->at(i)) {
  532. deselect = false;
  533. break;
  534. }
  535. }
  536. if (deselect) {
  537. selected_events->clear();
  538. updateSelectedObjects();
  539. }
  540. }
  541. }
  542. clicking = false;
  543. }
  544. }