No Description

editor.cpp 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #include "editor.h"
  2. Editor::Editor()
  3. {
  4. }
  5. void Editor::saveProject() {
  6. if (project) {
  7. project->saveAllMaps();
  8. }
  9. }
  10. void Editor::save() {
  11. if (project && map) {
  12. project->saveMap(map);
  13. }
  14. }
  15. void Editor::undo() {
  16. if (current_view) {
  17. ((MapPixmapItem*)current_view)->undo();
  18. }
  19. }
  20. void Editor::redo() {
  21. if (current_view) {
  22. ((MapPixmapItem*)current_view)->redo();
  23. }
  24. }
  25. void Editor::setEditingMap() {
  26. current_view = map_item;
  27. map_item->draw();
  28. map_item->setVisible(true);
  29. map_item->setEnabled(true);
  30. collision_item->setVisible(false);
  31. objects_group->setVisible(false);
  32. }
  33. void Editor::setEditingCollision() {
  34. current_view = collision_item;
  35. collision_item->draw();
  36. collision_item->setVisible(true);
  37. map_item->setVisible(false);
  38. objects_group->setVisible(false);
  39. }
  40. void Editor::setEditingObjects() {
  41. objects_group->setVisible(true);
  42. map_item->setVisible(true);
  43. map_item->setEnabled(false);
  44. collision_item->setVisible(false);
  45. }
  46. void Editor::setMap(QString map_name) {
  47. if (map_name.isNull()) {
  48. return;
  49. }
  50. map = project->getMap(map_name);
  51. displayMap();
  52. }
  53. void Editor::displayMap() {
  54. scene = new QGraphicsScene;
  55. map_item = new MapPixmapItem(map);
  56. map_item->draw();
  57. scene->addItem(map_item);
  58. collision_item = new CollisionPixmapItem(map);
  59. collision_item->draw();
  60. scene->addItem(collision_item);
  61. objects_group = new QGraphicsItemGroup;
  62. scene->addItem(objects_group);
  63. map_item->setVisible(false);
  64. collision_item->setVisible(false);
  65. objects_group->setVisible(false);
  66. int tw = 16;
  67. int th = 16;
  68. scene->setSceneRect(
  69. -6 * tw,
  70. -6 * th,
  71. map_item->pixmap().width() + 12 * tw,
  72. map_item->pixmap().height() + 12 * th
  73. );
  74. displayMetatiles();
  75. displayCollisionMetatiles();
  76. displayElevationMetatiles();
  77. displayMapObjects();
  78. displayMapConnections();
  79. displayMapBorder();
  80. }
  81. void Editor::displayMetatiles() {
  82. scene_metatiles = new QGraphicsScene;
  83. metatiles_item = new MetatilesPixmapItem(map);
  84. metatiles_item->draw();
  85. scene_metatiles->addItem(metatiles_item);
  86. }
  87. void Editor::displayCollisionMetatiles() {
  88. scene_collision_metatiles = new QGraphicsScene;
  89. collision_metatiles_item = new CollisionMetatilesPixmapItem(map);
  90. collision_metatiles_item->draw();
  91. scene_collision_metatiles->addItem(collision_metatiles_item);
  92. }
  93. void Editor::displayElevationMetatiles() {
  94. scene_elevation_metatiles = new QGraphicsScene;
  95. elevation_metatiles_item = new ElevationMetatilesPixmapItem(map);
  96. elevation_metatiles_item->draw();
  97. scene_elevation_metatiles->addItem(elevation_metatiles_item);
  98. }
  99. void Editor::displayMapObjects() {
  100. for (QGraphicsItem *child : objects_group->childItems()) {
  101. objects_group->removeFromGroup(child);
  102. }
  103. project->loadObjectPixmaps(map->object_events);
  104. for (int i = 0; i < map->object_events.length(); i++) {
  105. ObjectEvent *object_event = map->object_events.value(i);
  106. DraggablePixmapItem *object = new DraggablePixmapItem(object_event);
  107. objects_group->addToGroup(object);
  108. }
  109. objects_group->setFiltersChildEvents(false);
  110. }
  111. void Editor::displayMapConnections() {
  112. for (Connection *connection : map->connections) {
  113. if (connection->direction == "dive" || connection->direction == "emerge") {
  114. continue;
  115. }
  116. Map *connected_map = project->getMap(connection->map_name);
  117. QPixmap pixmap = connected_map->renderConnection(*connection);
  118. int offset = connection->offset.toInt(nullptr, 0);
  119. int x, y;
  120. if (connection->direction == "up") {
  121. x = offset * 16;
  122. y = -pixmap.height();
  123. } else if (connection->direction == "down") {
  124. x = offset * 16;
  125. y = map->getHeight() * 16;
  126. } else if (connection->direction == "left") {
  127. x = -pixmap.width();
  128. y = offset * 16;
  129. } else if (connection->direction == "right") {
  130. x = map->getWidth() * 16;
  131. y = offset * 16;
  132. }
  133. QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
  134. item->setX(x);
  135. item->setY(y);
  136. scene->addItem(item);
  137. }
  138. }
  139. void Editor::displayMapBorder() {
  140. QPixmap pixmap = map->renderBorder();
  141. for (int y = -6; y < map->getHeight() + 6; y += 2)
  142. for (int x = -6; x < map->getWidth() + 6; x += 2) {
  143. QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
  144. item->setX(x * 16);
  145. item->setY(y * 16);
  146. item->setZValue(-1);
  147. scene->addItem(item);
  148. }
  149. }
  150. void MetatilesPixmapItem::draw() {
  151. setPixmap(map->renderMetatiles());
  152. }
  153. void MetatilesPixmapItem::pick(uint tile) {
  154. map->paint_tile = tile;
  155. draw();
  156. }
  157. void MetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  158. QPointF pos = event->pos();
  159. int x = ((int)pos.x()) / 16;
  160. int y = ((int)pos.y()) / 16;
  161. //qDebug() << QString("(%1, %2)").arg(x).arg(y);
  162. int width = pixmap().width() / 16;
  163. int height = pixmap().height() / 16;
  164. if ((x >= 0 && x < width) && (y >=0 && y < height)) {
  165. pick(y * width + x);
  166. }
  167. }
  168. void MetatilesPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  169. mousePressEvent(event);
  170. }
  171. void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
  172. if (map) {
  173. QPointF pos = event->pos();
  174. int x = (int)(pos.x()) / 16;
  175. int y = (int)(pos.y()) / 16;
  176. Block *block = map->getBlock(x, y);
  177. if (block) {
  178. block->tile = map->paint_tile;
  179. map->setBlock(x, y, *block);
  180. }
  181. draw();
  182. }
  183. }
  184. void MapPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
  185. if (map) {
  186. QPointF pos = event->pos();
  187. int x = (int)(pos.x()) / 16;
  188. int y = (int)(pos.y()) / 16;
  189. map->floodFill(x, y, map->paint_tile);
  190. draw();
  191. }
  192. }
  193. void MapPixmapItem::draw() {
  194. setPixmap(map->render());
  195. }
  196. void MapPixmapItem::undo() {
  197. map->undo();
  198. draw();
  199. }
  200. void MapPixmapItem::redo() {
  201. map->redo();
  202. draw();
  203. }
  204. void MapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  205. active = true;
  206. if (event->button() == Qt::RightButton) {
  207. right_click = true;
  208. floodFill(event);
  209. } else {
  210. right_click = false;
  211. paint(event);
  212. }
  213. }
  214. void MapPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  215. if (active) {
  216. if (right_click) {
  217. floodFill(event);
  218. } else {
  219. paint(event);
  220. }
  221. }
  222. }
  223. void MapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  224. active = false;
  225. }
  226. void CollisionPixmapItem::draw() {
  227. setPixmap(map->renderCollision());
  228. }
  229. void CollisionPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
  230. if (map) {
  231. QPointF pos = event->pos();
  232. int x = (int)(pos.x()) / 16;
  233. int y = (int)(pos.y()) / 16;
  234. Block *block = map->getBlock(x, y);
  235. if (block) {
  236. if (map->paint_collision >= 0) {
  237. block->collision = map->paint_collision;
  238. }
  239. if (map->paint_elevation >= 0) {
  240. block->elevation = map->paint_elevation;
  241. }
  242. map->setBlock(x, y, *block);
  243. }
  244. draw();
  245. }
  246. }
  247. void CollisionPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
  248. if (map) {
  249. QPointF pos = event->pos();
  250. int x = (int)(pos.x()) / 16;
  251. int y = (int)(pos.y()) / 16;
  252. bool collision = map->paint_collision >= 0;
  253. bool elevation = map->paint_elevation >= 0;
  254. if (collision && elevation) {
  255. map->floodFillCollisionElevation(x, y, map->paint_collision, map->paint_elevation);
  256. } else if (collision) {
  257. map->floodFillCollision(x, y, map->paint_collision);
  258. } else if (elevation) {
  259. map->floodFillElevation(x, y, map->paint_elevation);
  260. }
  261. draw();
  262. }
  263. }
  264. void DraggablePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *mouse) {
  265. active = true;
  266. last_x = mouse->pos().x() / 16;
  267. last_y = mouse->pos().y() / 16;
  268. qDebug() << event->x_ + ", " + event->y_;
  269. }
  270. void DraggablePixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouse) {
  271. if (active) {
  272. int x = mouse->pos().x() / 16;
  273. int y = mouse->pos().y() / 16;
  274. if (x != last_x || y != last_y) {
  275. event->setX(event->x() + x - last_x);
  276. event->setY(event->y() + y - last_y);
  277. update();
  278. }
  279. }
  280. }
  281. void DraggablePixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouse) {
  282. active = false;
  283. }