Nav apraksta

editor.h 11KB

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