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