Нет описания

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #ifndef EDITOR_H
  2. #define EDITOR_H
  3. #include <QGraphicsScene>
  4. #include <QGraphicsItemGroup>
  5. #include <QGraphicsSceneMouseEvent>
  6. #include <QGraphicsItemAnimation>
  7. #include <QComboBox>
  8. #include "project.h"
  9. class DraggablePixmapItem;
  10. class MapPixmapItem;
  11. class CollisionPixmapItem;
  12. class MetatilesPixmapItem;
  13. class CollisionMetatilesPixmapItem;
  14. class ElevationMetatilesPixmapItem;
  15. class Editor : public QObject
  16. {
  17. Q_OBJECT
  18. public:
  19. Editor();
  20. public:
  21. QObject *parent = NULL;
  22. Project *project = NULL;
  23. Map *map = NULL;
  24. void saveProject();
  25. void save();
  26. void undo();
  27. void redo();
  28. void setMap(QString map_name);
  29. void displayMap();
  30. void displayMetatiles();
  31. void displayCollisionMetatiles();
  32. void displayElevationMetatiles();
  33. void displayMapObjects();
  34. void displayMapConnections();
  35. void displayMapBorder();
  36. void setEditingMap();
  37. void setEditingCollision();
  38. void setEditingObjects();
  39. DraggablePixmapItem *addMapObject(Event *event);
  40. void selectMapObject(DraggablePixmapItem *object);
  41. void selectMapObject(DraggablePixmapItem *object, bool toggle);
  42. DraggablePixmapItem *addNewEvent();
  43. DraggablePixmapItem *addNewEvent(QString event_type);
  44. void deleteEvent(Event *);
  45. void updateSelectedObjects();
  46. void redrawObject(DraggablePixmapItem *item);
  47. QList<DraggablePixmapItem *> *getObjects();
  48. QGraphicsScene *scene = NULL;
  49. QGraphicsPixmapItem *current_view = NULL;
  50. MapPixmapItem *map_item = NULL;
  51. CollisionPixmapItem *collision_item = NULL;
  52. QGraphicsItemGroup *objects_group = NULL;
  53. QGraphicsScene *scene_metatiles = NULL;
  54. QGraphicsScene *scene_collision_metatiles = NULL;
  55. QGraphicsScene *scene_elevation_metatiles = NULL;
  56. MetatilesPixmapItem *metatiles_item = NULL;
  57. CollisionMetatilesPixmapItem *collision_metatiles_item = NULL;
  58. ElevationMetatilesPixmapItem *elevation_metatiles_item = NULL;
  59. QList<DraggablePixmapItem*> *events = NULL;
  60. QList<DraggablePixmapItem*> *selected_events = NULL;
  61. QString map_edit_mode;
  62. void objectsView_onMousePress(QMouseEvent *event);
  63. void objectsView_onMouseMove(QMouseEvent *event);
  64. void objectsView_onMouseRelease(QMouseEvent *event);
  65. private slots:
  66. void mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item);
  67. void mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixmapItem *item);
  68. signals:
  69. void objectsChanged();
  70. void selectedObjectsChanged();
  71. };
  72. class DraggablePixmapItem : public QObject, public QGraphicsPixmapItem {
  73. Q_OBJECT
  74. public:
  75. DraggablePixmapItem(QPixmap pixmap): QGraphicsPixmapItem(pixmap) {
  76. }
  77. Editor *editor = NULL;
  78. Event *event = NULL;
  79. QGraphicsItemAnimation *pos_anim = NULL;
  80. DraggablePixmapItem(Event *event_) : QGraphicsPixmapItem(event_->pixmap) {
  81. event = event_;
  82. updatePosition();
  83. }
  84. bool active;
  85. bool right_click;
  86. bool clicking;
  87. int last_x;
  88. int last_y;
  89. void updatePosition() {
  90. int x = event->x() * 16;
  91. int y = event->y() * 16;
  92. x -= pixmap().width() / 32 * 16;
  93. y -= pixmap().height() - 16;
  94. setX(x);
  95. setY(y);
  96. setZValue(event->y());
  97. }
  98. void move(int x, int y);
  99. void emitPositionChanged() {
  100. emit xChanged(event->x());
  101. emit yChanged(event->y());
  102. emit elevationChanged(event->elevation());
  103. }
  104. void updatePixmap() {
  105. QList<Event*> objects;
  106. objects.append(event);
  107. event->pixmap = QPixmap();
  108. editor->project->loadObjectPixmaps(objects);
  109. editor->redrawObject(this);
  110. emit spriteChanged(event->pixmap);
  111. }
  112. void bind(QComboBox *combo, QString key) {
  113. connect(combo, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated),
  114. this, [this, key](QString value){
  115. this->event->put(key, value);
  116. });
  117. connect(this, &DraggablePixmapItem::onPropertyChanged,
  118. this, [combo, key](QString key2, QString value){
  119. if (key2 == key) {
  120. combo->addItem(value);
  121. combo->setCurrentText(value);
  122. }
  123. });
  124. }
  125. signals:
  126. void positionChanged(Event *event);
  127. void xChanged(int);
  128. void yChanged(int);
  129. void elevationChanged(int);
  130. void spriteChanged(QPixmap pixmap);
  131. void onPropertyChanged(QString key, QString value);
  132. public slots:
  133. void set_x(const QString &text) {
  134. event->put("x", text);
  135. updatePosition();
  136. }
  137. void set_y(const QString &text) {
  138. event->put("y", text);
  139. updatePosition();
  140. }
  141. void set_elevation(const QString &text) {
  142. event->put("elevation", text);
  143. updatePosition();
  144. }
  145. void set_sprite(const QString &text) {
  146. event->put("sprite", text);
  147. updatePixmap();
  148. }
  149. void set_script(const QString &text) {
  150. event->put("script_label", text);
  151. }
  152. protected:
  153. void mousePressEvent(QGraphicsSceneMouseEvent*);
  154. void mouseMoveEvent(QGraphicsSceneMouseEvent*);
  155. void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
  156. };
  157. class EventGroup : public QGraphicsItemGroup {
  158. };
  159. class MapPixmapItem : public QObject, public QGraphicsPixmapItem {
  160. Q_OBJECT
  161. public:
  162. MapPixmapItem(QPixmap pixmap): QGraphicsPixmapItem(pixmap) {
  163. }
  164. Map *map = NULL;
  165. MapPixmapItem(Map *map_) {
  166. map = map_;
  167. }
  168. bool active;
  169. bool right_click;
  170. QPoint selection_origin;
  171. QList<QPoint> selection;
  172. virtual void paint(QGraphicsSceneMouseEvent*);
  173. virtual void floodFill(QGraphicsSceneMouseEvent*);
  174. virtual void pick(QGraphicsSceneMouseEvent*);
  175. virtual void select(QGraphicsSceneMouseEvent*);
  176. virtual void undo();
  177. virtual void redo();
  178. virtual void draw();
  179. signals:
  180. void mouseEvent(QGraphicsSceneMouseEvent *, MapPixmapItem *);
  181. protected:
  182. void mousePressEvent(QGraphicsSceneMouseEvent*);
  183. void mouseMoveEvent(QGraphicsSceneMouseEvent*);
  184. void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
  185. };
  186. class CollisionPixmapItem : public MapPixmapItem {
  187. Q_OBJECT
  188. public:
  189. CollisionPixmapItem(QPixmap pixmap): MapPixmapItem(pixmap) {
  190. }
  191. CollisionPixmapItem(Map *map_): MapPixmapItem(map_) {
  192. }
  193. virtual void paint(QGraphicsSceneMouseEvent*);
  194. virtual void floodFill(QGraphicsSceneMouseEvent*);
  195. virtual void pick(QGraphicsSceneMouseEvent*);
  196. virtual void draw();
  197. signals:
  198. void mouseEvent(QGraphicsSceneMouseEvent *, CollisionPixmapItem *);
  199. protected:
  200. void mousePressEvent(QGraphicsSceneMouseEvent*);
  201. void mouseMoveEvent(QGraphicsSceneMouseEvent*);
  202. void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
  203. };
  204. class MetatilesPixmapItem : public QObject, public QGraphicsPixmapItem {
  205. Q_OBJECT
  206. public:
  207. MetatilesPixmapItem(QPixmap pixmap): QGraphicsPixmapItem(pixmap) {
  208. }
  209. MetatilesPixmapItem(Map *map_) {
  210. map = map_;
  211. connect(map, SIGNAL(paintTileChanged(Map*)), this, SLOT(paintTileChanged(Map *)));
  212. }
  213. Map* map = NULL;
  214. virtual void pick(uint);
  215. virtual void draw();
  216. private slots:
  217. void paintTileChanged(Map *map);
  218. protected:
  219. void mousePressEvent(QGraphicsSceneMouseEvent*);
  220. void mouseMoveEvent(QGraphicsSceneMouseEvent*);
  221. void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
  222. };
  223. class CollisionMetatilesPixmapItem : public MetatilesPixmapItem {
  224. Q_OBJECT
  225. public:
  226. CollisionMetatilesPixmapItem(Map *map_): MetatilesPixmapItem(map_) {
  227. connect(map, SIGNAL(paintCollisionChanged(Map*)), this, SLOT(paintCollisionChanged(Map *)));
  228. }
  229. virtual void pick(uint collision) {
  230. map->paint_collision = collision;
  231. draw();
  232. }
  233. virtual void draw() {
  234. setPixmap(map->renderCollisionMetatiles());
  235. }
  236. private slots:
  237. void paintCollisionChanged(Map *map) {
  238. draw();
  239. }
  240. };
  241. class ElevationMetatilesPixmapItem : public MetatilesPixmapItem {
  242. Q_OBJECT
  243. public:
  244. ElevationMetatilesPixmapItem(Map *map_): MetatilesPixmapItem(map_) {
  245. connect(map, SIGNAL(paintCollisionChanged(Map*)), this, SLOT(paintCollisionChanged(Map *)));
  246. }
  247. virtual void pick(uint elevation) {
  248. map->paint_elevation = elevation;
  249. draw();
  250. }
  251. virtual void draw() {
  252. setPixmap(map->renderElevationMetatiles());
  253. }
  254. private slots:
  255. void paintCollisionChanged(Map *map) {
  256. draw();
  257. }
  258. };
  259. #endif // EDITOR_H