Без опису

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 displayMapEvents();
  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 *addMapEvent(Event *event);
  56. void selectMapEvent(DraggablePixmapItem *object);
  57. void selectMapEvent(DraggablePixmapItem *object, bool toggle);
  58. DraggablePixmapItem *addNewEvent(QString event_type);
  59. Event* createNewEvent(QString event_type);
  60. void deleteEvent(Event *);
  61. void updateSelectedEvents();
  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 *events_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. Event* createNewObjectEvent();
  100. Event* createNewWarpEvent();
  101. Event* createNewCoordScriptEvent();
  102. Event* createNewCoordWeatherEvent();
  103. Event* createNewSignEvent();
  104. Event* createNewHiddenItemEvent();
  105. Event* createNewSecretBaseEvent();
  106. private slots:
  107. void mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item);
  108. void mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixmapItem *item);
  109. void onConnectionMoved(Connection*);
  110. void onConnectionItemSelected(ConnectionPixmapItem* connectionItem);
  111. void onConnectionItemDoubleClicked(ConnectionPixmapItem* connectionItem);
  112. void onConnectionDirectionChanged(QString newDirection);
  113. signals:
  114. void objectsChanged();
  115. void selectedObjectsChanged();
  116. void loadMapRequested(QString, QString);
  117. };
  118. class DraggablePixmapItem : public QObject, public QGraphicsPixmapItem {
  119. Q_OBJECT
  120. public:
  121. DraggablePixmapItem(QPixmap pixmap): QGraphicsPixmapItem(pixmap) {
  122. }
  123. Editor *editor = NULL;
  124. Event *event = NULL;
  125. QGraphicsItemAnimation *pos_anim = NULL;
  126. DraggablePixmapItem(Event *event_) : QGraphicsPixmapItem(event_->pixmap) {
  127. event = event_;
  128. updatePosition();
  129. }
  130. bool active;
  131. bool right_click;
  132. bool clicking;
  133. int last_x;
  134. int last_y;
  135. void updatePosition() {
  136. int x = event->x() * 16;
  137. int y = event->y() * 16;
  138. x -= pixmap().width() / 32 * 16;
  139. y -= pixmap().height() - 16;
  140. setX(x);
  141. setY(y);
  142. setZValue(event->y());
  143. }
  144. void move(int x, int y);
  145. void emitPositionChanged() {
  146. emit xChanged(event->x());
  147. emit yChanged(event->y());
  148. emit elevationChanged(event->elevation());
  149. }
  150. void updatePixmap() {
  151. QList<Event*> objects;
  152. objects.append(event);
  153. event->pixmap = QPixmap();
  154. editor->project->loadEventPixmaps(objects);
  155. editor->redrawObject(this);
  156. emit spriteChanged(event->pixmap);
  157. }
  158. void bind(QComboBox *combo, QString key) {
  159. connect(combo, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::currentTextChanged),
  160. this, [this, key](QString value){
  161. this->event->put(key, value);
  162. });
  163. connect(this, &DraggablePixmapItem::onPropertyChanged,
  164. this, [combo, key](QString key2, QString value){
  165. if (key2 == key) {
  166. combo->addItem(value);
  167. combo->setCurrentText(value);
  168. }
  169. });
  170. }
  171. signals:
  172. void positionChanged(Event *event);
  173. void xChanged(int);
  174. void yChanged(int);
  175. void elevationChanged(int);
  176. void spriteChanged(QPixmap pixmap);
  177. void onPropertyChanged(QString key, QString value);
  178. public slots:
  179. void set_x(const QString &text) {
  180. event->put("x", text);
  181. updatePosition();
  182. }
  183. void set_y(const QString &text) {
  184. event->put("y", text);
  185. updatePosition();
  186. }
  187. void set_elevation(const QString &text) {
  188. event->put("elevation", text);
  189. updatePosition();
  190. }
  191. void set_sprite(const QString &text) {
  192. event->put("sprite", text);
  193. updatePixmap();
  194. }
  195. void set_script(const QString &text) {
  196. event->put("script_label", text);
  197. }
  198. protected:
  199. void mousePressEvent(QGraphicsSceneMouseEvent*);
  200. void mouseMoveEvent(QGraphicsSceneMouseEvent*);
  201. void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
  202. };
  203. class EventGroup : public QGraphicsItemGroup {
  204. };
  205. class MapPixmapItem : public QObject, public QGraphicsPixmapItem {
  206. Q_OBJECT
  207. public:
  208. MapPixmapItem(QPixmap pixmap): QGraphicsPixmapItem(pixmap) {
  209. }
  210. Map *map = NULL;
  211. MapPixmapItem(Map *map_) {
  212. map = map_;
  213. setAcceptHoverEvents(true);
  214. }
  215. bool active;
  216. bool right_click;
  217. QPoint selection_origin;
  218. QList<QPoint> selection;
  219. virtual void paint(QGraphicsSceneMouseEvent*);
  220. virtual void floodFill(QGraphicsSceneMouseEvent*);
  221. virtual void pick(QGraphicsSceneMouseEvent*);
  222. virtual void select(QGraphicsSceneMouseEvent*);
  223. virtual void undo();
  224. virtual void redo();
  225. virtual void draw(bool ignoreCache = false);
  226. private:
  227. void updateCurHoveredTile(QPointF pos);
  228. void paintNormal(int x, int y);
  229. void paintSmartPath(int x, int y);
  230. static QList<int> smartPathTable;
  231. signals:
  232. void mouseEvent(QGraphicsSceneMouseEvent *, MapPixmapItem *);
  233. protected:
  234. void hoverMoveEvent(QGraphicsSceneHoverEvent*);
  235. void hoverLeaveEvent(QGraphicsSceneHoverEvent*);
  236. void mousePressEvent(QGraphicsSceneMouseEvent*);
  237. void mouseMoveEvent(QGraphicsSceneMouseEvent*);
  238. void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
  239. };
  240. class CollisionPixmapItem : public MapPixmapItem {
  241. Q_OBJECT
  242. public:
  243. CollisionPixmapItem(QPixmap pixmap): MapPixmapItem(pixmap) {
  244. }
  245. CollisionPixmapItem(Map *map_): MapPixmapItem(map_) {
  246. }
  247. virtual void paint(QGraphicsSceneMouseEvent*);
  248. virtual void floodFill(QGraphicsSceneMouseEvent*);
  249. virtual void pick(QGraphicsSceneMouseEvent*);
  250. virtual void draw(bool ignoreCache = false);
  251. signals:
  252. void mouseEvent(QGraphicsSceneMouseEvent *, CollisionPixmapItem *);
  253. protected:
  254. void mousePressEvent(QGraphicsSceneMouseEvent*);
  255. void mouseMoveEvent(QGraphicsSceneMouseEvent*);
  256. void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
  257. };
  258. class ConnectionPixmapItem : public QObject, public QGraphicsPixmapItem {
  259. Q_OBJECT
  260. public:
  261. ConnectionPixmapItem(QPixmap pixmap, Connection* connection, int x, int y, int baseMapWidth, int baseMapHeight): QGraphicsPixmapItem(pixmap) {
  262. this->basePixmap = pixmap;
  263. this->connection = connection;
  264. setFlag(ItemIsMovable);
  265. setFlag(ItemSendsGeometryChanges);
  266. this->initialX = x;
  267. this->initialY = y;
  268. this->initialOffset = connection->offset.toInt();
  269. this->baseMapWidth = baseMapWidth;
  270. this->baseMapHeight = baseMapHeight;
  271. }
  272. void render(qreal opacity = 1) {
  273. QPixmap newPixmap = basePixmap.copy(0, 0, basePixmap.width(), basePixmap.height());
  274. if (opacity < 1) {
  275. QPainter painter(&newPixmap);
  276. int alpha = (int)(255 * (1 - opacity));
  277. painter.fillRect(0, 0, newPixmap.width(), newPixmap.height(), QColor(0, 0, 0, alpha));
  278. painter.end();
  279. }
  280. this->setPixmap(newPixmap);
  281. }
  282. int getMinOffset();
  283. int getMaxOffset();
  284. QPixmap basePixmap;
  285. Connection* connection;
  286. int initialX;
  287. int initialY;
  288. int initialOffset;
  289. int baseMapWidth;
  290. int baseMapHeight;
  291. protected:
  292. QVariant itemChange(GraphicsItemChange change, const QVariant &value);
  293. void mousePressEvent(QGraphicsSceneMouseEvent*);
  294. void mouseDoubleClickEvent(QGraphicsSceneMouseEvent*);
  295. signals:
  296. void connectionItemSelected(ConnectionPixmapItem* connectionItem);
  297. void connectionItemDoubleClicked(ConnectionPixmapItem* connectionItem);
  298. void connectionMoved(Connection*);
  299. };
  300. class MetatilesPixmapItem : public QObject, public QGraphicsPixmapItem {
  301. Q_OBJECT
  302. public:
  303. MetatilesPixmapItem(Map *map_) {
  304. map = map_;
  305. setAcceptHoverEvents(true);
  306. connect(map, SIGNAL(paintTileChanged(Map*)), this, SLOT(paintTileChanged(Map *)));
  307. }
  308. Map* map = NULL;
  309. virtual void draw();
  310. private:
  311. void updateSelection(QPointF pos, Qt::MouseButton button);
  312. protected:
  313. virtual void updateCurHoveredMetatile(QPointF pos);
  314. private slots:
  315. void paintTileChanged(Map *map);
  316. protected:
  317. void hoverMoveEvent(QGraphicsSceneHoverEvent*);
  318. void hoverLeaveEvent(QGraphicsSceneHoverEvent*);
  319. void mousePressEvent(QGraphicsSceneMouseEvent*);
  320. void mouseMoveEvent(QGraphicsSceneMouseEvent*);
  321. void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
  322. };
  323. class MovementPermissionsPixmapItem : public MetatilesPixmapItem {
  324. Q_OBJECT
  325. public:
  326. MovementPermissionsPixmapItem(Map *map_): MetatilesPixmapItem(map_) {}
  327. virtual void pick(uint collision) {
  328. map->paint_collision = collision;
  329. draw();
  330. }
  331. protected:
  332. void mousePressEvent(QGraphicsSceneMouseEvent*);
  333. void mouseMoveEvent(QGraphicsSceneMouseEvent*);
  334. void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
  335. };
  336. class CollisionMetatilesPixmapItem : public MovementPermissionsPixmapItem {
  337. Q_OBJECT
  338. public:
  339. CollisionMetatilesPixmapItem(Map *map_): MovementPermissionsPixmapItem(map_) {
  340. connect(map, SIGNAL(paintCollisionChanged(Map*)), this, SLOT(paintCollisionChanged(Map *)));
  341. }
  342. virtual void pick(uint collision) {
  343. map->paint_collision = collision;
  344. draw();
  345. }
  346. virtual void draw() {
  347. setPixmap(map->renderCollisionMetatiles());
  348. }
  349. protected:
  350. virtual void updateCurHoveredMetatile(QPointF pos);
  351. private slots:
  352. void paintCollisionChanged(Map *map) {
  353. draw();
  354. }
  355. };
  356. class ElevationMetatilesPixmapItem : public MovementPermissionsPixmapItem {
  357. Q_OBJECT
  358. public:
  359. ElevationMetatilesPixmapItem(Map *map_): MovementPermissionsPixmapItem(map_) {
  360. connect(map, SIGNAL(paintCollisionChanged(Map*)), this, SLOT(paintCollisionChanged(Map *)));
  361. }
  362. virtual void pick(uint elevation) {
  363. map->paint_elevation = elevation;
  364. draw();
  365. }
  366. virtual void draw() {
  367. setPixmap(map->renderElevationMetatiles());
  368. }
  369. protected:
  370. virtual void updateCurHoveredMetatile(QPointF pos);
  371. private slots:
  372. void paintCollisionChanged(Map *map) {
  373. draw();
  374. }
  375. };
  376. #endif // EDITOR_H