Ei kuvausta

editor.h 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. void warpEventDoubleClicked(QString mapName, QString warpNum);
  128. };
  129. class DraggablePixmapItem : public QObject, public QGraphicsPixmapItem {
  130. Q_OBJECT
  131. public:
  132. DraggablePixmapItem(QPixmap pixmap): QGraphicsPixmapItem(pixmap) {
  133. }
  134. Editor *editor = NULL;
  135. Event *event = NULL;
  136. QGraphicsItemAnimation *pos_anim = NULL;
  137. DraggablePixmapItem(Event *event_, Editor *editor_) : QGraphicsPixmapItem(event_->pixmap) {
  138. event = event_;
  139. editor = editor_;
  140. updatePosition();
  141. }
  142. bool active;
  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. void mouseDoubleClickEvent(QGraphicsSceneMouseEvent*);
  213. };
  214. class EventGroup : public QGraphicsItemGroup {
  215. };
  216. class MapPixmapItem : public QObject, public QGraphicsPixmapItem {
  217. Q_OBJECT
  218. public:
  219. MapPixmapItem(QPixmap pixmap): QGraphicsPixmapItem(pixmap) {
  220. }
  221. Map *map = NULL;
  222. MapPixmapItem(Map *map_) {
  223. map = map_;
  224. setAcceptHoverEvents(true);
  225. }
  226. bool active;
  227. bool right_click;
  228. QPoint selection_origin;
  229. QList<QPoint> selection;
  230. virtual void paint(QGraphicsSceneMouseEvent*);
  231. virtual void floodFill(QGraphicsSceneMouseEvent*);
  232. void _floodFill(int x, int y);
  233. void _floodFillSmartPath(int initialX, int initialY);
  234. virtual void pick(QGraphicsSceneMouseEvent*);
  235. virtual void select(QGraphicsSceneMouseEvent*);
  236. virtual void draw(bool ignoreCache = false);
  237. private:
  238. void updateCurHoveredTile(QPointF pos);
  239. void paintNormal(int x, int y);
  240. void paintSmartPath(int x, int y);
  241. static QList<int> smartPathTable;
  242. signals:
  243. void mouseEvent(QGraphicsSceneMouseEvent *, MapPixmapItem *);
  244. protected:
  245. void hoverMoveEvent(QGraphicsSceneHoverEvent*);
  246. void hoverLeaveEvent(QGraphicsSceneHoverEvent*);
  247. void mousePressEvent(QGraphicsSceneMouseEvent*);
  248. void mouseMoveEvent(QGraphicsSceneMouseEvent*);
  249. void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
  250. };
  251. class CollisionPixmapItem : public MapPixmapItem {
  252. Q_OBJECT
  253. public:
  254. CollisionPixmapItem(QPixmap pixmap): MapPixmapItem(pixmap) {
  255. }
  256. CollisionPixmapItem(Map *map_): MapPixmapItem(map_) {
  257. }
  258. virtual void paint(QGraphicsSceneMouseEvent*);
  259. virtual void floodFill(QGraphicsSceneMouseEvent*);
  260. virtual void pick(QGraphicsSceneMouseEvent*);
  261. virtual void draw(bool ignoreCache = false);
  262. signals:
  263. void mouseEvent(QGraphicsSceneMouseEvent *, CollisionPixmapItem *);
  264. protected:
  265. void mousePressEvent(QGraphicsSceneMouseEvent*);
  266. void mouseMoveEvent(QGraphicsSceneMouseEvent*);
  267. void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
  268. };
  269. class ConnectionPixmapItem : public QObject, public QGraphicsPixmapItem {
  270. Q_OBJECT
  271. public:
  272. ConnectionPixmapItem(QPixmap pixmap, Connection* connection, int x, int y, int baseMapWidth, int baseMapHeight): QGraphicsPixmapItem(pixmap) {
  273. this->basePixmap = pixmap;
  274. this->connection = connection;
  275. setFlag(ItemIsMovable);
  276. setFlag(ItemSendsGeometryChanges);
  277. this->initialX = x;
  278. this->initialY = y;
  279. this->initialOffset = connection->offset.toInt();
  280. this->baseMapWidth = baseMapWidth;
  281. this->baseMapHeight = baseMapHeight;
  282. }
  283. void render(qreal opacity = 1) {
  284. QPixmap newPixmap = basePixmap.copy(0, 0, basePixmap.width(), basePixmap.height());
  285. if (opacity < 1) {
  286. QPainter painter(&newPixmap);
  287. int alpha = (int)(255 * (1 - opacity));
  288. painter.fillRect(0, 0, newPixmap.width(), newPixmap.height(), QColor(0, 0, 0, alpha));
  289. painter.end();
  290. }
  291. this->setPixmap(newPixmap);
  292. }
  293. int getMinOffset();
  294. int getMaxOffset();
  295. QPixmap basePixmap;
  296. Connection* connection;
  297. int initialX;
  298. int initialY;
  299. int initialOffset;
  300. int baseMapWidth;
  301. int baseMapHeight;
  302. protected:
  303. QVariant itemChange(GraphicsItemChange change, const QVariant &value);
  304. void mousePressEvent(QGraphicsSceneMouseEvent*);
  305. void mouseDoubleClickEvent(QGraphicsSceneMouseEvent*);
  306. signals:
  307. void connectionItemSelected(ConnectionPixmapItem* connectionItem);
  308. void connectionItemDoubleClicked(ConnectionPixmapItem* connectionItem);
  309. void connectionMoved(Connection*);
  310. };
  311. class MetatilesPixmapItem : public QObject, public QGraphicsPixmapItem {
  312. Q_OBJECT
  313. public:
  314. MetatilesPixmapItem(Map *map_) {
  315. map = map_;
  316. setAcceptHoverEvents(true);
  317. connect(map, SIGNAL(paintTileChanged(Map*)), this, SLOT(paintTileChanged(Map *)));
  318. }
  319. Map* map = NULL;
  320. virtual void draw();
  321. private:
  322. void updateSelection(QPointF pos, Qt::MouseButton button);
  323. protected:
  324. virtual void updateCurHoveredMetatile(QPointF pos);
  325. private slots:
  326. void paintTileChanged(Map *map);
  327. protected:
  328. void hoverMoveEvent(QGraphicsSceneHoverEvent*);
  329. void hoverLeaveEvent(QGraphicsSceneHoverEvent*);
  330. void mousePressEvent(QGraphicsSceneMouseEvent*);
  331. void mouseMoveEvent(QGraphicsSceneMouseEvent*);
  332. void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
  333. };
  334. class BorderMetatilesPixmapItem : public QObject, public QGraphicsPixmapItem {
  335. Q_OBJECT
  336. public:
  337. BorderMetatilesPixmapItem(Map *map_) {
  338. map = map_;
  339. setAcceptHoverEvents(true);
  340. }
  341. Map* map = NULL;
  342. virtual void draw();
  343. signals:
  344. void borderMetatilesChanged();
  345. protected:
  346. void mousePressEvent(QGraphicsSceneMouseEvent*);
  347. };
  348. class MovementPermissionsPixmapItem : public MetatilesPixmapItem {
  349. Q_OBJECT
  350. public:
  351. MovementPermissionsPixmapItem(Map *map_): MetatilesPixmapItem(map_) {}
  352. virtual void pick(uint collision) {
  353. map->paint_collision = collision;
  354. draw();
  355. }
  356. protected:
  357. void mousePressEvent(QGraphicsSceneMouseEvent*);
  358. void mouseMoveEvent(QGraphicsSceneMouseEvent*);
  359. void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
  360. };
  361. class CollisionMetatilesPixmapItem : public MovementPermissionsPixmapItem {
  362. Q_OBJECT
  363. public:
  364. CollisionMetatilesPixmapItem(Map *map_): MovementPermissionsPixmapItem(map_) {
  365. connect(map, SIGNAL(paintCollisionChanged(Map*)), this, SLOT(paintCollisionChanged(Map *)));
  366. }
  367. virtual void pick(uint collision) {
  368. map->paint_collision = collision;
  369. draw();
  370. }
  371. virtual void draw() {
  372. setPixmap(map->renderCollisionMetatiles());
  373. }
  374. protected:
  375. virtual void updateCurHoveredMetatile(QPointF pos);
  376. private slots:
  377. void paintCollisionChanged(Map *map) {
  378. draw();
  379. }
  380. };
  381. class ElevationMetatilesPixmapItem : public MovementPermissionsPixmapItem {
  382. Q_OBJECT
  383. public:
  384. ElevationMetatilesPixmapItem(Map *map_): MovementPermissionsPixmapItem(map_) {
  385. connect(map, SIGNAL(paintCollisionChanged(Map*)), this, SLOT(paintCollisionChanged(Map *)));
  386. }
  387. virtual void pick(uint elevation) {
  388. map->paint_elevation = elevation;
  389. draw();
  390. }
  391. virtual void draw() {
  392. setPixmap(map->renderElevationMetatiles());
  393. }
  394. protected:
  395. virtual void updateCurHoveredMetatile(QPointF pos);
  396. private slots:
  397. void paintCollisionChanged(Map *map) {
  398. draw();
  399. }
  400. };
  401. #endif // EDITOR_H