Нет описания

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