Няма описание

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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();
  45. void setCurrentConnectionDirection(QString curDirection);
  46. void updateCurrentConnectionDirection(QString curDirection);
  47. void setConnectionsVisibility(bool visible);
  48. void updateConnectionOffset(int offset);
  49. void setConnectionMap(QString mapName);
  50. void addNewConnection();
  51. void removeCurrentConnection();
  52. void updateDiveMap(QString mapName);
  53. void updateEmergeMap(QString mapName);
  54. void setSelectedConnectionFromMap(QString mapName);
  55. DraggablePixmapItem *addMapObject(Event *event);
  56. void selectMapObject(DraggablePixmapItem *object);
  57. void selectMapObject(DraggablePixmapItem *object, bool toggle);
  58. DraggablePixmapItem *addNewEvent();
  59. DraggablePixmapItem *addNewEvent(QString event_type);
  60. void deleteEvent(Event *);
  61. void updateSelectedObjects();
  62. void redrawObject(DraggablePixmapItem *item);
  63. QList<DraggablePixmapItem *> *getObjects();
  64. QGraphicsScene *scene = NULL;
  65. QGraphicsPixmapItem *current_view = NULL;
  66. MapPixmapItem *map_item = NULL;
  67. ConnectionPixmapItem* selected_connection_item = NULL;
  68. QList<ConnectionPixmapItem*> connection_edit_items;
  69. CollisionPixmapItem *collision_item = NULL;
  70. QGraphicsItemGroup *objects_group = NULL;
  71. QList<QGraphicsPixmapItem*> borderItems;
  72. QGraphicsScene *scene_metatiles = NULL;
  73. QGraphicsScene *scene_collision_metatiles = NULL;
  74. QGraphicsScene *scene_elevation_metatiles = NULL;
  75. MetatilesPixmapItem *metatiles_item = NULL;
  76. CollisionMetatilesPixmapItem *collision_metatiles_item = NULL;
  77. ElevationMetatilesPixmapItem *elevation_metatiles_item = NULL;
  78. QList<DraggablePixmapItem*> *events = NULL;
  79. QList<DraggablePixmapItem*> *selected_events = NULL;
  80. QString map_edit_mode;
  81. void objectsView_onMousePress(QMouseEvent *event);
  82. void objectsView_onMouseMove(QMouseEvent *event);
  83. void objectsView_onMouseRelease(QMouseEvent *event);
  84. private:
  85. void setConnectionItemsVisible(bool);
  86. void setBorderItemsVisible(bool, qreal = 1);
  87. void setConnectionEditControlValues(Connection*);
  88. void setConnectionEditControlsEnabled(bool);
  89. void createConnectionItem(Connection* connection, bool hide);
  90. void populateConnectionMapPickers();
  91. void setDiveEmergeControls();
  92. void updateDiveEmergeMap(QString mapName, QString direction);
  93. void onConnectionOffsetChanged(int newOffset);
  94. void removeMirroredConnection(Connection*);
  95. void updateMirroredConnectionOffset(Connection*);
  96. void updateMirroredConnectionDirection(Connection*, QString);
  97. void updateMirroredConnectionMap(Connection*, QString);
  98. void updateMirroredConnection(Connection*, QString, QString, bool isDelete = false);
  99. private slots:
  100. void mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item);
  101. void mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixmapItem *item);
  102. void onConnectionMoved(Connection*);
  103. void onConnectionItemSelected(ConnectionPixmapItem* connectionItem);
  104. void onConnectionItemDoubleClicked(ConnectionPixmapItem* connectionItem);
  105. void onConnectionDirectionChanged(QString newDirection);
  106. signals:
  107. void objectsChanged();
  108. void selectedObjectsChanged();
  109. void loadMapRequested(QString, QString);
  110. };
  111. class DraggablePixmapItem : public QObject, public QGraphicsPixmapItem {
  112. Q_OBJECT
  113. public:
  114. DraggablePixmapItem(QPixmap pixmap): QGraphicsPixmapItem(pixmap) {
  115. }
  116. Editor *editor = NULL;
  117. Event *event = NULL;
  118. QGraphicsItemAnimation *pos_anim = NULL;
  119. DraggablePixmapItem(Event *event_) : QGraphicsPixmapItem(event_->pixmap) {
  120. event = event_;
  121. updatePosition();
  122. }
  123. bool active;
  124. bool right_click;
  125. bool clicking;
  126. int last_x;
  127. int last_y;
  128. void updatePosition() {
  129. int x = event->x() * 16;
  130. int y = event->y() * 16;
  131. x -= pixmap().width() / 32 * 16;
  132. y -= pixmap().height() - 16;
  133. setX(x);
  134. setY(y);
  135. setZValue(event->y());
  136. }
  137. void move(int x, int y);
  138. void emitPositionChanged() {
  139. emit xChanged(event->x());
  140. emit yChanged(event->y());
  141. emit elevationChanged(event->elevation());
  142. }
  143. void updatePixmap() {
  144. QList<Event*> objects;
  145. objects.append(event);
  146. event->pixmap = QPixmap();
  147. editor->project->loadObjectPixmaps(objects);
  148. editor->redrawObject(this);
  149. emit spriteChanged(event->pixmap);
  150. }
  151. void bind(QComboBox *combo, QString key) {
  152. connect(combo, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated),
  153. this, [this, key](QString value){
  154. this->event->put(key, value);
  155. });
  156. connect(this, &DraggablePixmapItem::onPropertyChanged,
  157. this, [combo, key](QString key2, QString value){
  158. if (key2 == key) {
  159. combo->addItem(value);
  160. combo->setCurrentText(value);
  161. }
  162. });
  163. }
  164. signals:
  165. void positionChanged(Event *event);
  166. void xChanged(int);
  167. void yChanged(int);
  168. void elevationChanged(int);
  169. void spriteChanged(QPixmap pixmap);
  170. void onPropertyChanged(QString key, QString value);
  171. public slots:
  172. void set_x(const QString &text) {
  173. event->put("x", text);
  174. updatePosition();
  175. }
  176. void set_y(const QString &text) {
  177. event->put("y", text);
  178. updatePosition();
  179. }
  180. void set_elevation(const QString &text) {
  181. event->put("elevation", text);
  182. updatePosition();
  183. }
  184. void set_sprite(const QString &text) {
  185. event->put("sprite", text);
  186. updatePixmap();
  187. }
  188. void set_script(const QString &text) {
  189. event->put("script_label", text);
  190. }
  191. protected:
  192. void mousePressEvent(QGraphicsSceneMouseEvent*);
  193. void mouseMoveEvent(QGraphicsSceneMouseEvent*);
  194. void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
  195. };
  196. class EventGroup : public QGraphicsItemGroup {
  197. };
  198. class MapPixmapItem : public QObject, public QGraphicsPixmapItem {
  199. Q_OBJECT
  200. public:
  201. MapPixmapItem(QPixmap pixmap): QGraphicsPixmapItem(pixmap) {
  202. }
  203. Map *map = NULL;
  204. MapPixmapItem(Map *map_) {
  205. map = map_;
  206. setAcceptHoverEvents(true);
  207. }
  208. bool active;
  209. bool right_click;
  210. QPoint selection_origin;
  211. QList<QPoint> selection;
  212. virtual void paint(QGraphicsSceneMouseEvent*);
  213. virtual void floodFill(QGraphicsSceneMouseEvent*);
  214. virtual void pick(QGraphicsSceneMouseEvent*);
  215. virtual void select(QGraphicsSceneMouseEvent*);
  216. virtual void undo();
  217. virtual void redo();
  218. virtual void draw();
  219. private:
  220. void updateCurHoveredTile(QPointF pos);
  221. void paintNormal(int x, int y);
  222. void paintSmartPath(int x, int y);
  223. static QList<int> smartPathTable;
  224. signals:
  225. void mouseEvent(QGraphicsSceneMouseEvent *, MapPixmapItem *);
  226. protected:
  227. void hoverMoveEvent(QGraphicsSceneHoverEvent*);
  228. void hoverLeaveEvent(QGraphicsSceneHoverEvent*);
  229. void mousePressEvent(QGraphicsSceneMouseEvent*);
  230. void mouseMoveEvent(QGraphicsSceneMouseEvent*);
  231. void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
  232. };
  233. class CollisionPixmapItem : public MapPixmapItem {
  234. Q_OBJECT
  235. public:
  236. CollisionPixmapItem(QPixmap pixmap): MapPixmapItem(pixmap) {
  237. }
  238. CollisionPixmapItem(Map *map_): MapPixmapItem(map_) {
  239. }
  240. virtual void paint(QGraphicsSceneMouseEvent*);
  241. virtual void floodFill(QGraphicsSceneMouseEvent*);
  242. virtual void pick(QGraphicsSceneMouseEvent*);
  243. virtual void draw();
  244. signals:
  245. void mouseEvent(QGraphicsSceneMouseEvent *, CollisionPixmapItem *);
  246. protected:
  247. void mousePressEvent(QGraphicsSceneMouseEvent*);
  248. void mouseMoveEvent(QGraphicsSceneMouseEvent*);
  249. void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
  250. };
  251. class ConnectionPixmapItem : public QObject, public QGraphicsPixmapItem {
  252. Q_OBJECT
  253. public:
  254. ConnectionPixmapItem(QPixmap pixmap, Connection* connection, int x, int y, int baseMapWidth, int baseMapHeight): QGraphicsPixmapItem(pixmap) {
  255. this->basePixmap = pixmap;
  256. this->connection = connection;
  257. setFlag(ItemIsMovable);
  258. setFlag(ItemSendsGeometryChanges);
  259. this->initialX = x;
  260. this->initialY = y;
  261. this->initialOffset = connection->offset.toInt();
  262. this->baseMapWidth = baseMapWidth;
  263. this->baseMapHeight = baseMapHeight;
  264. }
  265. void render(qreal opacity = 1) {
  266. QPixmap newPixmap = basePixmap.copy(0, 0, basePixmap.width(), basePixmap.height());
  267. if (opacity < 1) {
  268. QPainter painter(&newPixmap);
  269. int alpha = (int)(255 * (1 - opacity));
  270. painter.fillRect(0, 0, newPixmap.width(), newPixmap.height(), QColor(0, 0, 0, alpha));
  271. painter.end();
  272. }
  273. this->setPixmap(newPixmap);
  274. }
  275. int getMinOffset();
  276. int getMaxOffset();
  277. QPixmap basePixmap;
  278. Connection* connection;
  279. int initialX;
  280. int initialY;
  281. int initialOffset;
  282. int baseMapWidth;
  283. int baseMapHeight;
  284. protected:
  285. QVariant itemChange(GraphicsItemChange change, const QVariant &value);
  286. void mousePressEvent(QGraphicsSceneMouseEvent*);
  287. void mouseDoubleClickEvent(QGraphicsSceneMouseEvent*);
  288. signals:
  289. void connectionItemSelected(ConnectionPixmapItem* connectionItem);
  290. void connectionItemDoubleClicked(ConnectionPixmapItem* connectionItem);
  291. void connectionMoved(Connection*);
  292. };
  293. class MetatilesPixmapItem : public QObject, public QGraphicsPixmapItem {
  294. Q_OBJECT
  295. public:
  296. MetatilesPixmapItem(Map *map_) {
  297. map = map_;
  298. setAcceptHoverEvents(true);
  299. connect(map, SIGNAL(paintTileChanged(Map*)), this, SLOT(paintTileChanged(Map *)));
  300. }
  301. Map* map = NULL;
  302. virtual void draw();
  303. private:
  304. void updateSelection(QPointF pos, Qt::MouseButton button);
  305. protected:
  306. virtual void updateCurHoveredMetatile(QPointF pos);
  307. private slots:
  308. void paintTileChanged(Map *map);
  309. protected:
  310. void hoverMoveEvent(QGraphicsSceneHoverEvent*);
  311. void hoverLeaveEvent(QGraphicsSceneHoverEvent*);
  312. void mousePressEvent(QGraphicsSceneMouseEvent*);
  313. void mouseMoveEvent(QGraphicsSceneMouseEvent*);
  314. void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
  315. };
  316. class MovementPermissionsPixmapItem : public MetatilesPixmapItem {
  317. Q_OBJECT
  318. public:
  319. MovementPermissionsPixmapItem(Map *map_): MetatilesPixmapItem(map_) {}
  320. virtual void pick(uint collision) {
  321. map->paint_collision = collision;
  322. draw();
  323. }
  324. protected:
  325. void mousePressEvent(QGraphicsSceneMouseEvent*);
  326. void mouseMoveEvent(QGraphicsSceneMouseEvent*);
  327. void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
  328. };
  329. class CollisionMetatilesPixmapItem : public MovementPermissionsPixmapItem {
  330. Q_OBJECT
  331. public:
  332. CollisionMetatilesPixmapItem(Map *map_): MovementPermissionsPixmapItem(map_) {
  333. connect(map, SIGNAL(paintCollisionChanged(Map*)), this, SLOT(paintCollisionChanged(Map *)));
  334. }
  335. virtual void pick(uint collision) {
  336. map->paint_collision = collision;
  337. draw();
  338. }
  339. virtual void draw() {
  340. setPixmap(map->renderCollisionMetatiles());
  341. }
  342. protected:
  343. virtual void updateCurHoveredMetatile(QPointF pos);
  344. private slots:
  345. void paintCollisionChanged(Map *map) {
  346. draw();
  347. }
  348. };
  349. class ElevationMetatilesPixmapItem : public MovementPermissionsPixmapItem {
  350. Q_OBJECT
  351. public:
  352. ElevationMetatilesPixmapItem(Map *map_): MovementPermissionsPixmapItem(map_) {
  353. connect(map, SIGNAL(paintCollisionChanged(Map*)), this, SLOT(paintCollisionChanged(Map *)));
  354. }
  355. virtual void pick(uint elevation) {
  356. map->paint_elevation = elevation;
  357. draw();
  358. }
  359. virtual void draw() {
  360. setPixmap(map->renderElevationMetatiles());
  361. }
  362. protected:
  363. virtual void updateCurHoveredMetatile(QPointF pos);
  364. private slots:
  365. void paintCollisionChanged(Map *map) {
  366. draw();
  367. }
  368. };
  369. #endif // EDITOR_H