No Description

editor.h 14KB

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