Add basic drop-and-drop connection editing

This commit is contained in:
Marcus Huderle 2018-03-07 20:22:18 -08:00
parent e2933fc801
commit 64007b5bae
7 changed files with 229 additions and 88 deletions

View file

@ -40,6 +40,7 @@ void Editor::setEditingMap() {
map_item->draw();
map_item->setVisible(true);
map_item->setEnabled(true);
setConnectionsVisibility(true);
}
if (collision_item) {
collision_item->setVisible(false);
@ -52,8 +53,8 @@ void Editor::setEditingMap() {
void Editor::setEditingCollision() {
current_view = collision_item;
if (collision_item) {
collision_item->draw();
collision_item->setVisible(true);
setConnectionsVisibility(true);
}
if (map_item) {
map_item->setVisible(false);
@ -71,12 +72,88 @@ void Editor::setEditingObjects() {
if (map_item) {
map_item->setVisible(true);
map_item->setEnabled(false);
setConnectionsVisibility(true);
}
if (collision_item) {
collision_item->setVisible(false);
}
}
void Editor::setEditingConnections(QString direction) {
current_view = map_item;
if (map_item) {
map_item->draw();
map_item->setVisible(true);
map_item->setEnabled(true);
setConnectionsVisibility(false);
showCurrentConnectionMap(direction);
}
if (collision_item) {
collision_item->setVisible(false);
}
if (objects_group) {
objects_group->setVisible(false);
}
}
void Editor::showCurrentConnectionMap(QString curDirection) {
bool connectionExists = false;
for (Connection* connection : map->connections) {
if (connection->direction != curDirection) continue;
if (connection_item) {
scene->removeItem(connection_item);
delete connection_item;
connection_item = NULL;
}
connectionExists = true;
Map *connected_map = project->getMap(connection->map_name);
QPixmap pixmap = connected_map->renderConnection(*connection);
int offset = connection->offset.toInt(nullptr, 0);
int x = 0, y = 0;
if (connection->direction == "up") {
x = offset * 16;
y = -pixmap.height();
} else if (connection->direction == "down") {
x = offset * 16;
y = map->getHeight() * 16;
} else if (connection->direction == "left") {
x = -pixmap.width();
y = offset * 16;
} else if (connection->direction == "right") {
x = map->getWidth() * 16;
y = offset * 16;
}
connection_item = new ConnectionPixmapItem(pixmap, connection, x, y);
connection_item->setX(x);
connection_item->setY(y);
connection_item->setZValue(21);
scene->addItem(connection_item);
scene->setSceneRect(0, 0, pixmap.width() + map_item->pixmap().width(), pixmap.height() + map_item->pixmap().height());
connect(connection_item, SIGNAL(connectionMoved(int)), this, SLOT(onConnectionOffsetChanged(int)));
onConnectionOffsetChanged(connection->offset.toInt());
break;
}
if (!connectionExists && connection_item) {
scene->removeItem(connection_item);
delete connection_item;
connection_item = NULL;
}
}
void Editor::onConnectionOffsetChanged(int newOffset) {
emit connectionOffsetChanged(newOffset);
}
void Editor::setConnectionsVisibility(bool visible) {
for (QGraphicsPixmapItem* item : map->connection_items) {
item->setVisible(visible);
item->setActive(visible);
}
}
void Editor::setMap(QString map_name) {
if (map_name.isNull()) {
return;
@ -231,6 +308,7 @@ void Editor::displayMapConnections() {
item->setX(x);
item->setY(y);
scene->addItem(item);
map->connection_items.insert(connection->direction, item);
}
}
@ -263,6 +341,17 @@ void Editor::displayMapGrid() {
}
}
void Editor::updateConnectionOffset(int offset) {
connection_item->blockSignals(true);
connection_item->connection->offset = QString::number(offset);
if (connection_item->connection->direction == "up" || connection_item->connection->direction == "down") {
connection_item->setX(connection_item->initialX + (offset - connection_item->initialOffset) * 16);
} else {
connection_item->setY(connection_item->initialY + (offset - connection_item->initialOffset) * 16);
}
connection_item->blockSignals(false);
}
void MetatilesPixmapItem::paintTileChanged(Map *map) {
draw();
}
@ -345,6 +434,57 @@ void CollisionMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
}
}
QVariant ConnectionPixmapItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionChange) {
QPointF newPos = value.toPointF();
qreal x, y;
int newOffset = initialOffset;
if (connection->direction == "up" || connection->direction == "down") {
x = round(newPos.x() / 16) * 16;
newOffset += (x - initialX) / 16;
}
else {
x = initialX;
}
if (connection->direction == "right" || connection->direction == "left") {
y = round(newPos.y() / 16) * 16;
newOffset += (y - initialY) / 16;
}
else {
y = initialY;
}
emit connectionMoved(newOffset);
connection->offset = QString::number(newOffset);
return QPointF(x, y);
}
else {
return QGraphicsItem::itemChange(change, value);
}
}
void ConnectionPixmapItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
QPointF pos = event->pos();
qDebug() << "enter: " << pos.x() << ", " << pos.y();
}
void ConnectionPixmapItem::dragMoveEvent(QGraphicsSceneDragDropEvent *event) {
QPointF pos = event->pos();
qDebug() << "drag: " << pos.x() << ", " << pos.y();
}
void ConnectionPixmapItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) {
QPointF pos = event->pos();
qDebug() << "leave: " << pos.x() << ", " << pos.y();
}
void ConnectionPixmapItem::dropEvent(QGraphicsSceneDragDropEvent *event) {
QPointF pos = event->pos();
qDebug() << "drop: " << pos.x() << ", " << pos.y();
}
void ConnectionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {
QPointF pos = event->pos();
}
void ElevationMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
int x = ((int)pos.x()) / 16;
int y = ((int)pos.y()) / 16;

View file

@ -13,6 +13,7 @@
class DraggablePixmapItem;
class MapPixmapItem;
class CollisionPixmapItem;
class ConnectionPixmapItem;
class MetatilesPixmapItem;
class CollisionMetatilesPixmapItem;
class ElevationMetatilesPixmapItem;
@ -44,6 +45,10 @@ public:
void setEditingMap();
void setEditingCollision();
void setEditingObjects();
void setEditingConnections(QString direction);
void showCurrentConnectionMap(QString curDirection);
void setConnectionsVisibility(bool visible);
void updateConnectionOffset(int offset);
DraggablePixmapItem *addMapObject(Event *event);
void selectMapObject(DraggablePixmapItem *object);
@ -58,6 +63,7 @@ public:
QGraphicsScene *scene = NULL;
QGraphicsPixmapItem *current_view = NULL;
MapPixmapItem *map_item = NULL;
ConnectionPixmapItem *connection_item = NULL;
CollisionPixmapItem *collision_item = NULL;
QGraphicsItemGroup *objects_group = NULL;
@ -80,10 +86,12 @@ public:
private slots:
void mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item);
void mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixmapItem *item);
void onConnectionOffsetChanged(int newOffset);
signals:
void objectsChanged();
void selectedObjectsChanged();
void connectionOffsetChanged(int newOffset);
};
@ -240,6 +248,33 @@ protected:
void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
};
class ConnectionPixmapItem : public QObject, public QGraphicsPixmapItem {
Q_OBJECT
public:
ConnectionPixmapItem(QPixmap pixmap, Connection* connection, int x, int y): QGraphicsPixmapItem(pixmap) {
this->connection = connection;
setFlag(ItemIsMovable);
setFlag(ItemIsSelectable);
setFlag(ItemSendsGeometryChanges);
this->initialX = x;
this->initialY = y;
this->initialOffset = connection->offset.toInt();
}
Connection* connection;
int initialX;
int initialY;
int initialOffset;
protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
void dragEnterEvent(QGraphicsSceneDragDropEvent *event) override;
void dragLeaveEvent(QGraphicsSceneDragDropEvent *event) override;
void dropEvent(QGraphicsSceneDragDropEvent *event) override;
void dragMoveEvent(QGraphicsSceneDragDropEvent *event) override;
void mousePressEvent(QGraphicsSceneMouseEvent*);
signals:
void connectionMoved(int offset);
};
class MetatilesPixmapItem : public QObject, public QGraphicsPixmapItem {
Q_OBJECT
public:

View file

@ -30,6 +30,7 @@ MainWindow::MainWindow(QWidget *parent) :
editor->gridToggleCheckbox = ui->checkBox_ToggleGrid;
connect(editor, SIGNAL(objectsChanged()), this, SLOT(updateSelectedObjects()));
connect(editor, SIGNAL(selectedObjectsChanged()), this, SLOT(updateSelectedObjects()));
connect(editor, SIGNAL(connectionOffsetChanged(int)), this, SLOT(onConnectionOffsetChanged(int)));
on_toolButton_Paint_clicked();
@ -143,15 +144,7 @@ void MainWindow::setMap(QString map_name) {
}
editor->setMap(map_name);
if (ui->tabWidget->currentIndex() == 1) {
editor->setEditingObjects();
} else {
if (ui->tabWidget_2->currentIndex() == 1) {
editor->setEditingCollision();
} else {
editor->setEditingMap();
}
}
on_tabWidget_currentChanged(ui->tabWidget->currentIndex());
ui->graphicsView_Map->setScene(editor->scene);
ui->graphicsView_Map->setSceneRect(editor->scene->sceneRect());
@ -162,6 +155,8 @@ void MainWindow::setMap(QString map_name) {
ui->graphicsView_Objects_Map->setFixedSize(editor->scene->width() + 2, editor->scene->height() + 2);
ui->graphicsView_Objects_Map->editor = editor;
ui->graphicsView_Connections->setScene(editor->scene);
ui->graphicsView_Metatiles->setScene(editor->scene_metatiles);
//ui->graphicsView_Metatiles->setSceneRect(editor->scene_metatiles->sceneRect());
ui->graphicsView_Metatiles->setFixedSize(editor->metatiles_item->pixmap().width() + 2, editor->metatiles_item->pixmap().height() + 2);
@ -491,6 +486,8 @@ void MainWindow::on_tabWidget_currentChanged(int index)
on_tabWidget_2_currentChanged(ui->tabWidget_2->currentIndex());
} else if (index == 1) {
editor->setEditingObjects();
} else if (index == 3) {
editor->setEditingConnections(ui->comboBox_ConnectionDirection->currentText().toLower());
}
}
@ -768,3 +765,20 @@ void MainWindow::on_action_Export_Map_Image_triggered()
editor->map_item->pixmap().save(filepath);
}
}
void MainWindow::on_comboBox_ConnectionDirection_currentIndexChanged(const QString &direction)
{
editor->showCurrentConnectionMap(direction.toLower());
}
void MainWindow::on_spinBox_ConnectionOffset_valueChanged(int offset)
{
editor->updateConnectionOffset(offset);
}
void MainWindow::onConnectionOffsetChanged(int offset)
{
ui->spinBox_ConnectionOffset->blockSignals(true);
ui->spinBox_ConnectionOffset->setValue(offset);
ui->spinBox_ConnectionOffset->blockSignals(false);
}

View file

@ -74,6 +74,12 @@ private slots:
void on_action_Export_Map_Image_triggered();
void on_comboBox_ConnectionDirection_currentIndexChanged(const QString &arg1);
void on_spinBox_ConnectionOffset_valueChanged(int offset);
void onConnectionOffsetChanged(int offset);
private:
Ui::MainWindow *ui;
QStandardItemModel *mapListModel;

View file

@ -60,7 +60,7 @@
</sizepolicy>
</property>
<property name="currentIndex">
<number>0</number>
<number>3</number>
</property>
<property name="tabsClosable">
<bool>false</bool>
@ -1266,7 +1266,7 @@
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_3" stretch="0,0,0,0,0">
<layout class="QHBoxLayout" name="horizontalLayout_3" stretch="0,0,0,0,0,0,0">
<property name="spacing">
<number>4</number>
</property>
@ -1286,22 +1286,22 @@
<widget class="QComboBox" name="comboBox_ConnectionDirection">
<item>
<property name="text">
<string>North</string>
<string>Up</string>
</property>
</item>
<item>
<property name="text">
<string>East</string>
<string>Right</string>
</property>
</item>
<item>
<property name="text">
<string>South</string>
<string>Down</string>
</property>
</item>
<item>
<property name="text">
<string>West</string>
<string>Left</string>
</property>
</item>
</widget>
@ -1336,6 +1336,20 @@
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_12">
<property name="text">
<string>Offset</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="spinBox_ConnectionOffset">
<property name="minimum">
<number>-99</number>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_6">
<property name="orientation">
@ -1377,78 +1391,7 @@
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QScrollArea" name="scrollArea_5">
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents_3">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>826</width>
<height>621</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_15">
<item row="1" column="1">
<widget class="QGraphicsView" name="graphicsView"/>
</item>
<item row="0" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="1">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0">
<spacer name="horizontalSpacer_7">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="2">
<spacer name="horizontalSpacer_8">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</widget>
<widget class="QGraphicsView" name="graphicsView_Connections"/>
</item>
</layout>
</widget>

2
map.h
View file

@ -8,6 +8,7 @@
#include <QPixmap>
#include <QObject>
#include <QDebug>
#include <QGraphicsPixmapItem>
template <typename T>
@ -179,6 +180,7 @@ public:
QMap<QString, QList<Event*>> events;
QList<Connection*> connections;
QMap<QString, QGraphicsPixmapItem*> connection_items;
QPixmap renderConnection(Connection);
QImage border_image;

View file

@ -67,6 +67,7 @@ void Project::loadMapConnections(Map *map) {
}
map->connections.clear();
map->connection_items.clear();
if (!map->connections_label.isNull()) {
QString path = root + QString("/data/maps/%1/connections.inc").arg(map->name);
QString text = readTextFile(path);