Нема описа

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