porymap/editor.h

394 lines
14 KiB
C
Raw Normal View History

2016-09-07 04:50:47 +01:00
#ifndef EDITOR_H
#define EDITOR_H
#include <QGraphicsScene>
#include <QGraphicsItemGroup>
#include <QGraphicsSceneMouseEvent>
2017-11-28 04:46:27 +00:00
#include <QGraphicsItemAnimation>
#include <QComboBox>
2018-03-05 00:20:18 +00:00
#include <QCheckBox>
#include <QCursor>
2016-09-07 04:50:47 +01:00
2018-09-25 01:12:29 +01:00
#include "mapconnection.h"
#include "metatileselector.h"
#include "movementpermissionsselector.h"
2016-09-07 04:50:47 +01:00
#include "project.h"
#include "ui_mainwindow.h"
2016-09-07 04:50:47 +01:00
2017-11-28 04:46:27 +00:00
class DraggablePixmapItem;
class MapPixmapItem;
class CollisionPixmapItem;
class ConnectionPixmapItem;
2017-11-28 04:46:27 +00:00
class MetatilesPixmapItem;
2018-07-07 23:32:54 +01:00
class BorderMetatilesPixmapItem;
class CurrentSelectedMetatilesPixmapItem;
2016-09-07 04:50:47 +01:00
#define SWAP(a, b) do { if (a != b) { a ^= b; b ^= a; a ^= b; } } while (0)
2017-11-28 04:46:27 +00:00
class Editor : public QObject
{
Q_OBJECT
public:
Editor(Ui::MainWindow* ui);
2017-11-28 04:46:27 +00:00
public:
Ui::MainWindow* ui;
2018-09-15 00:37:36 +01:00
QObject *parent = nullptr;
Project *project = nullptr;
Map *map = nullptr;
2017-11-28 04:46:27 +00:00
void saveProject();
void save();
void undo();
void redo();
void setMap(QString map_name);
void displayMap();
void displayMetatileSelector();
2018-07-07 23:32:54 +01:00
void displayBorderMetatiles();
void displayCurrentMetatilesSelection();
void redrawCurrentMetatilesSelection();
void displayMovementPermissionSelector();
2017-11-28 04:46:27 +00:00
void displayElevationMetatiles();
void displayMapEvents();
2017-11-28 04:46:27 +00:00
void displayMapConnections();
void displayMapBorder();
2018-03-05 00:07:52 +00:00
void displayMapGrid();
2017-11-28 04:46:27 +00:00
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();
2018-03-10 19:25:34 +00:00
void updateDiveMap(QString mapName);
void updateEmergeMap(QString mapName);
void setSelectedConnectionFromMap(QString mapName);
2018-07-08 17:17:43 +01:00
void updatePrimaryTileset(QString tilesetLabel);
void updateSecondaryTileset(QString tilesetLabel);
void toggleBorderVisibility(bool visible);
2017-11-28 04:46:27 +00:00
DraggablePixmapItem *addMapEvent(Event *event);
void selectMapEvent(DraggablePixmapItem *object);
void selectMapEvent(DraggablePixmapItem *object, bool toggle);
2017-11-28 04:46:27 +00:00
DraggablePixmapItem *addNewEvent(QString event_type);
2018-07-07 16:58:25 +01:00
Event* createNewEvent(QString event_type);
2017-11-28 04:46:27 +00:00
void deleteEvent(Event *);
void updateSelectedEvents();
2017-11-28 04:46:27 +00:00
void redrawObject(DraggablePixmapItem *item);
QList<DraggablePixmapItem *> *getObjects();
2018-09-15 00:37:36 +01:00
QGraphicsScene *scene = nullptr;
QGraphicsPixmapItem *current_view = nullptr;
MapPixmapItem *map_item = nullptr;
ConnectionPixmapItem* selected_connection_item = nullptr;
2018-07-08 17:17:43 +01:00
QList<QGraphicsPixmapItem*> connection_items;
QList<ConnectionPixmapItem*> connection_edit_items;
2018-09-15 00:37:36 +01:00
CollisionPixmapItem *collision_item = nullptr;
QGraphicsItemGroup *events_group = nullptr;
QList<QGraphicsPixmapItem*> borderItems;
2018-07-08 17:17:43 +01:00
QList<QGraphicsLineItem*> gridLines;
2017-11-28 04:46:27 +00:00
2018-09-15 00:37:36 +01:00
QGraphicsScene *scene_metatiles = nullptr;
QGraphicsScene *scene_current_metatile_selection = nullptr;
QGraphicsScene *scene_selected_border_metatiles = nullptr;
QGraphicsScene *scene_collision_metatiles = nullptr;
QGraphicsScene *scene_elevation_metatiles = nullptr;
MetatileSelector *metatile_selector_item = nullptr;
2018-09-15 00:37:36 +01:00
BorderMetatilesPixmapItem *selected_border_metatiles_item = nullptr;
CurrentSelectedMetatilesPixmapItem *scene_current_metatile_selection_item = nullptr;
MovementPermissionsSelector *movement_permissions_selector_item = nullptr;
2017-11-28 04:46:27 +00:00
2018-09-15 00:37:36 +01:00
QList<DraggablePixmapItem*> *events = nullptr;
QList<DraggablePixmapItem*> *selected_events = nullptr;
2017-11-28 04:46:27 +00:00
QString map_edit_mode;
QString prev_edit_mode;
QCursor cursor;
2017-11-28 04:46:27 +00:00
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(MapConnection*);
void setConnectionEditControlsEnabled(bool);
void createConnectionItem(MapConnection* connection, bool hide);
2018-03-10 19:25:34 +00:00
void populateConnectionMapPickers();
void setDiveEmergeControls();
void updateDiveEmergeMap(QString mapName, QString direction);
void onConnectionOffsetChanged(int newOffset);
void removeMirroredConnection(MapConnection*);
void updateMirroredConnectionOffset(MapConnection*);
void updateMirroredConnectionDirection(MapConnection*, QString);
void updateMirroredConnectionMap(MapConnection*, QString);
void updateMirroredConnection(MapConnection*, QString, QString, bool isDelete = false);
2018-07-07 16:58:25 +01:00
Event* createNewObjectEvent();
Event* createNewWarpEvent();
2018-09-12 01:37:36 +01:00
Event* createNewHealLocationEvent();
2018-07-07 16:58:25 +01:00
Event* createNewCoordScriptEvent();
Event* createNewCoordWeatherEvent();
Event* createNewSignEvent();
Event* createNewHiddenItemEvent();
Event* createNewSecretBaseEvent();
2017-11-28 04:46:27 +00:00
private slots:
void mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item);
void mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixmapItem *item);
void onConnectionMoved(MapConnection*);
void onConnectionItemSelected(ConnectionPixmapItem* connectionItem);
void onConnectionItemDoubleClicked(ConnectionPixmapItem* connectionItem);
void onConnectionDirectionChanged(QString newDirection);
2018-07-07 23:32:54 +01:00
void onBorderMetatilesChanged();
void onHoveredMovementPermissionChanged(uint16_t, uint16_t);
void onHoveredMovementPermissionCleared();
void onHoveredMetatileSelectionChanged(uint16_t);
void onHoveredMetatileSelectionCleared();
void onSelectedMetatilesChanged();
2017-11-28 04:46:27 +00:00
signals:
void objectsChanged();
void selectedObjectsChanged();
void loadMapRequested(QString, QString);
2018-07-08 17:17:43 +01:00
void tilesetChanged(QString);
void warpEventDoubleClicked(QString mapName, QString warpNum);
void currentMetatilesSelectionChanged();
2017-11-28 04:46:27 +00:00
};
class DraggablePixmapItem : public QObject, public QGraphicsPixmapItem {
Q_OBJECT
2016-09-07 04:50:47 +01:00
public:
DraggablePixmapItem(QPixmap pixmap): QGraphicsPixmapItem(pixmap) {
}
2018-09-15 00:37:36 +01:00
Editor *editor = nullptr;
Event *event = nullptr;
QGraphicsItemAnimation *pos_anim = nullptr;
DraggablePixmapItem(Event *event_, Editor *editor_) : QGraphicsPixmapItem(event_->pixmap) {
2016-09-07 04:50:47 +01:00
event = event_;
editor = editor_;
2017-11-28 04:46:27 +00:00
updatePosition();
2016-09-07 04:50:47 +01:00
}
bool active;
int last_x;
int last_y;
2017-11-28 04:46:27 +00:00
void updatePosition() {
int x = event->getPixelX();
int y = event->getPixelY();
2016-09-07 04:50:47 +01:00
setX(x);
setY(y);
setZValue(event->y());
}
2017-11-28 04:46:27 +00:00
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);
this->updatePosition();
2017-11-28 04:46:27 +00:00
editor->redrawObject(this);
emit spriteChanged(event->pixmap);
}
void bind(QComboBox *combo, QString key) {
connect(combo, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::currentTextChanged),
2017-11-28 04:46:27 +00:00
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);
}
2016-09-07 04:50:47 +01:00
protected:
void mousePressEvent(QGraphicsSceneMouseEvent*);
void mouseMoveEvent(QGraphicsSceneMouseEvent*);
void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent*);
2016-09-07 04:50:47 +01:00
};
2017-11-28 04:46:27 +00:00
class EventGroup : public QGraphicsItemGroup {
};
class MapPixmapItem : public QObject, public QGraphicsPixmapItem {
Q_OBJECT
2016-09-07 04:50:47 +01:00
public:
MapPixmapItem(QPixmap pixmap): QGraphicsPixmapItem(pixmap) {
}
2018-09-15 00:37:36 +01:00
Map *map = nullptr;
Editor *editor = nullptr;
MapPixmapItem(Map *map_, Editor *editor_) {
2016-09-07 04:50:47 +01:00
map = map_;
editor = editor_;
setAcceptHoverEvents(true);
2016-09-07 04:50:47 +01:00
}
bool active;
bool right_click;
2017-11-28 04:46:27 +00:00
QPoint selection_origin;
QList<QPoint> selection;
2016-09-07 04:50:47 +01:00
virtual void paint(QGraphicsSceneMouseEvent*);
virtual void floodFill(QGraphicsSceneMouseEvent*);
void _floodFill(int x, int y);
void _floodFillSmartPath(int initialX, int initialY);
2017-11-28 04:46:27 +00:00
virtual void pick(QGraphicsSceneMouseEvent*);
virtual void select(QGraphicsSceneMouseEvent*);
virtual void shift(QGraphicsSceneMouseEvent*);
2018-06-20 23:43:20 +01:00
virtual void draw(bool ignoreCache = false);
void updateMetatileSelection(QGraphicsSceneMouseEvent *event);
2016-09-07 04:50:47 +01:00
private:
void updateCurHoveredTile(QPointF pos);
2018-03-06 00:47:00 +00:00
void paintNormal(int x, int y);
void paintSmartPath(int x, int y);
static QList<int> smartPathTable;
2017-11-28 04:46:27 +00:00
signals:
void mouseEvent(QGraphicsSceneMouseEvent *, MapPixmapItem *);
2016-09-07 04:50:47 +01:00
protected:
void hoverMoveEvent(QGraphicsSceneHoverEvent*);
void hoverLeaveEvent(QGraphicsSceneHoverEvent*);
2016-09-07 04:50:47 +01:00
void mousePressEvent(QGraphicsSceneMouseEvent*);
void mouseMoveEvent(QGraphicsSceneMouseEvent*);
void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
};
class CollisionPixmapItem : public MapPixmapItem {
2017-11-28 04:46:27 +00:00
Q_OBJECT
2016-09-07 04:50:47 +01:00
public:
CollisionPixmapItem(QPixmap pixmap): MapPixmapItem(pixmap) {
}
CollisionPixmapItem(Map *map_, Editor *editor_): MapPixmapItem(map_, editor_) {
2016-09-07 04:50:47 +01:00
}
2018-07-20 04:27:40 +01:00
void updateMovementPermissionSelection(QGraphicsSceneMouseEvent *event);
2016-09-07 04:50:47 +01:00
virtual void paint(QGraphicsSceneMouseEvent*);
virtual void floodFill(QGraphicsSceneMouseEvent*);
2017-11-28 04:46:27 +00:00
virtual void pick(QGraphicsSceneMouseEvent*);
2018-06-20 23:43:20 +01:00
virtual void draw(bool ignoreCache = false);
2017-11-28 04:46:27 +00:00
signals:
void mouseEvent(QGraphicsSceneMouseEvent *, CollisionPixmapItem *);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent*);
void mouseMoveEvent(QGraphicsSceneMouseEvent*);
void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
2016-09-07 04:50:47 +01:00
};
class ConnectionPixmapItem : public QObject, public QGraphicsPixmapItem {
Q_OBJECT
public:
ConnectionPixmapItem(QPixmap pixmap, MapConnection* 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);
2018-09-15 00:37:36 +01:00
int alpha = static_cast<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;
MapConnection* 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(MapConnection*);
};
2018-07-07 23:32:54 +01:00
class BorderMetatilesPixmapItem : public QObject, public QGraphicsPixmapItem {
Q_OBJECT
public:
BorderMetatilesPixmapItem(Map *map_, Editor *editor_) {
2018-07-07 23:32:54 +01:00
map = map_;
editor = editor_;
2018-07-07 23:32:54 +01:00
setAcceptHoverEvents(true);
}
Editor *editor = nullptr;
2018-09-15 00:37:36 +01:00
Map* map = nullptr;
2018-07-07 23:32:54 +01:00
virtual void draw();
signals:
void borderMetatilesChanged();
protected:
void mousePressEvent(QGraphicsSceneMouseEvent*);
};
class CurrentSelectedMetatilesPixmapItem : public QObject, public QGraphicsPixmapItem {
Q_OBJECT
public:
CurrentSelectedMetatilesPixmapItem(Map *map_, Editor *editor_) {
map = map_;
editor = editor_;
}
2018-09-15 00:37:36 +01:00
Map* map = nullptr;
Editor *editor = nullptr;
virtual void draw();
};
2016-09-07 04:50:47 +01:00
#endif // EDITOR_H