Selaa lähdekoodia

Add basic drop-and-drop connection editing

Marcus Huderle 6 vuotta sitten
vanhempi
commit
64007b5bae
7 muutettua tiedostoa jossa 229 lisäystä ja 88 poistoa
  1. 141
    1
      editor.cpp
  2. 35
    0
      editor.h
  3. 23
    9
      mainwindow.cpp
  4. 6
    0
      mainwindow.h
  5. 21
    78
      mainwindow.ui
  6. 2
    0
      map.h
  7. 1
    0
      project.cpp

+ 141
- 1
editor.cpp Näytä tiedosto

@@ -40,6 +40,7 @@ void Editor::setEditingMap() {
40 40
         map_item->draw();
41 41
         map_item->setVisible(true);
42 42
         map_item->setEnabled(true);
43
+        setConnectionsVisibility(true);
43 44
     }
44 45
     if (collision_item) {
45 46
         collision_item->setVisible(false);
@@ -52,8 +53,8 @@ void Editor::setEditingMap() {
52 53
 void Editor::setEditingCollision() {
53 54
     current_view = collision_item;
54 55
     if (collision_item) {
55
-        collision_item->draw();
56 56
         collision_item->setVisible(true);
57
+        setConnectionsVisibility(true);
57 58
     }
58 59
     if (map_item) {
59 60
         map_item->setVisible(false);
@@ -71,12 +72,88 @@ void Editor::setEditingObjects() {
71 72
     if (map_item) {
72 73
         map_item->setVisible(true);
73 74
         map_item->setEnabled(false);
75
+        setConnectionsVisibility(true);
74 76
     }
75 77
     if (collision_item) {
76 78
         collision_item->setVisible(false);
77 79
     }
78 80
 }
79 81
 
82
+void Editor::setEditingConnections(QString direction) {
83
+    current_view = map_item;
84
+    if (map_item) {
85
+        map_item->draw();
86
+        map_item->setVisible(true);
87
+        map_item->setEnabled(true);
88
+        setConnectionsVisibility(false);
89
+        showCurrentConnectionMap(direction);
90
+    }
91
+    if (collision_item) {
92
+        collision_item->setVisible(false);
93
+    }
94
+    if (objects_group) {
95
+        objects_group->setVisible(false);
96
+    }
97
+}
98
+
99
+void Editor::showCurrentConnectionMap(QString curDirection) {
100
+    bool connectionExists = false;
101
+    for (Connection* connection : map->connections) {
102
+        if (connection->direction != curDirection) continue;
103
+        if (connection_item) {
104
+            scene->removeItem(connection_item);
105
+            delete connection_item;
106
+            connection_item = NULL;
107
+        }
108
+
109
+        connectionExists = true;
110
+        Map *connected_map = project->getMap(connection->map_name);
111
+        QPixmap pixmap = connected_map->renderConnection(*connection);
112
+        int offset = connection->offset.toInt(nullptr, 0);
113
+        int x = 0, y = 0;
114
+        if (connection->direction == "up") {
115
+            x = offset * 16;
116
+            y = -pixmap.height();
117
+        } else if (connection->direction == "down") {
118
+            x = offset * 16;
119
+            y = map->getHeight() * 16;
120
+        } else if (connection->direction == "left") {
121
+            x = -pixmap.width();
122
+            y = offset * 16;
123
+        } else if (connection->direction == "right") {
124
+            x = map->getWidth() * 16;
125
+            y = offset * 16;
126
+        }
127
+        connection_item = new ConnectionPixmapItem(pixmap, connection, x, y);
128
+        connection_item->setX(x);
129
+        connection_item->setY(y);
130
+        connection_item->setZValue(21);
131
+        scene->addItem(connection_item);
132
+        scene->setSceneRect(0, 0, pixmap.width() + map_item->pixmap().width(), pixmap.height() + map_item->pixmap().height());
133
+
134
+        connect(connection_item, SIGNAL(connectionMoved(int)), this, SLOT(onConnectionOffsetChanged(int)));
135
+        onConnectionOffsetChanged(connection->offset.toInt());
136
+        break;
137
+    }
138
+
139
+    if (!connectionExists && connection_item) {
140
+        scene->removeItem(connection_item);
141
+        delete connection_item;
142
+        connection_item = NULL;
143
+    }
144
+}
145
+
146
+void Editor::onConnectionOffsetChanged(int newOffset) {
147
+    emit connectionOffsetChanged(newOffset);
148
+}
149
+
150
+void Editor::setConnectionsVisibility(bool visible) {
151
+    for (QGraphicsPixmapItem* item : map->connection_items) {
152
+        item->setVisible(visible);
153
+        item->setActive(visible);
154
+    }
155
+}
156
+
80 157
 void Editor::setMap(QString map_name) {
81 158
     if (map_name.isNull()) {
82 159
         return;
@@ -231,6 +308,7 @@ void Editor::displayMapConnections() {
231 308
         item->setX(x);
232 309
         item->setY(y);
233 310
         scene->addItem(item);
311
+        map->connection_items.insert(connection->direction, item);
234 312
     }
235 313
 }
236 314
 
@@ -263,6 +341,17 @@ void Editor::displayMapGrid() {
263 341
     }
264 342
 }
265 343
 
344
+void Editor::updateConnectionOffset(int offset) {
345
+    connection_item->blockSignals(true);
346
+    connection_item->connection->offset = QString::number(offset);
347
+    if (connection_item->connection->direction == "up" || connection_item->connection->direction == "down") {
348
+        connection_item->setX(connection_item->initialX + (offset - connection_item->initialOffset) * 16);
349
+    } else {
350
+        connection_item->setY(connection_item->initialY + (offset - connection_item->initialOffset) * 16);
351
+    }
352
+    connection_item->blockSignals(false);
353
+}
354
+
266 355
 void MetatilesPixmapItem::paintTileChanged(Map *map) {
267 356
     draw();
268 357
 }
@@ -345,6 +434,57 @@ void CollisionMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
345 434
     }
346 435
 }
347 436
 
437
+QVariant ConnectionPixmapItem::itemChange(GraphicsItemChange change, const QVariant &value)
438
+{
439
+    if (change == ItemPositionChange) {
440
+        QPointF newPos = value.toPointF();
441
+
442
+        qreal x, y;
443
+        int newOffset = initialOffset;
444
+        if (connection->direction == "up" || connection->direction == "down") {
445
+            x = round(newPos.x() / 16) * 16;
446
+            newOffset += (x - initialX) / 16;
447
+        }
448
+        else {
449
+            x = initialX;
450
+        }
451
+
452
+        if (connection->direction == "right" || connection->direction == "left") {
453
+            y = round(newPos.y() / 16) * 16;
454
+            newOffset += (y - initialY) / 16;
455
+        }
456
+        else {
457
+            y = initialY;
458
+        }
459
+
460
+        emit connectionMoved(newOffset);
461
+        connection->offset = QString::number(newOffset);
462
+        return QPointF(x, y);
463
+    }
464
+    else {
465
+        return QGraphicsItem::itemChange(change, value);
466
+    }
467
+}
468
+void ConnectionPixmapItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
469
+    QPointF pos = event->pos();
470
+    qDebug() << "enter: " << pos.x() << ", " << pos.y();
471
+}
472
+void ConnectionPixmapItem::dragMoveEvent(QGraphicsSceneDragDropEvent *event) {
473
+    QPointF pos = event->pos();
474
+    qDebug() << "drag: " << pos.x() << ", " << pos.y();
475
+}
476
+void ConnectionPixmapItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) {
477
+    QPointF pos = event->pos();
478
+    qDebug() << "leave: " << pos.x() << ", " << pos.y();
479
+}
480
+void ConnectionPixmapItem::dropEvent(QGraphicsSceneDragDropEvent *event) {
481
+    QPointF pos = event->pos();
482
+    qDebug() << "drop: " << pos.x() << ", " << pos.y();
483
+}
484
+void ConnectionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {
485
+    QPointF pos = event->pos();
486
+}
487
+
348 488
 void ElevationMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
349 489
     int x = ((int)pos.x()) / 16;
350 490
     int y = ((int)pos.y()) / 16;

+ 35
- 0
editor.h Näytä tiedosto

@@ -13,6 +13,7 @@
13 13
 class DraggablePixmapItem;
14 14
 class MapPixmapItem;
15 15
 class CollisionPixmapItem;
16
+class ConnectionPixmapItem;
16 17
 class MetatilesPixmapItem;
17 18
 class CollisionMetatilesPixmapItem;
18 19
 class ElevationMetatilesPixmapItem;
@@ -44,6 +45,10 @@ public:
44 45
     void setEditingMap();
45 46
     void setEditingCollision();
46 47
     void setEditingObjects();
48
+    void setEditingConnections(QString direction);
49
+    void showCurrentConnectionMap(QString curDirection);
50
+    void setConnectionsVisibility(bool visible);
51
+    void updateConnectionOffset(int offset);
47 52
 
48 53
     DraggablePixmapItem *addMapObject(Event *event);
49 54
     void selectMapObject(DraggablePixmapItem *object);
@@ -58,6 +63,7 @@ public:
58 63
     QGraphicsScene *scene = NULL;
59 64
     QGraphicsPixmapItem *current_view = NULL;
60 65
     MapPixmapItem *map_item = NULL;
66
+    ConnectionPixmapItem *connection_item = NULL;
61 67
     CollisionPixmapItem *collision_item = NULL;
62 68
     QGraphicsItemGroup *objects_group = NULL;
63 69
 
@@ -80,10 +86,12 @@ public:
80 86
 private slots:
81 87
     void mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item);
82 88
     void mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixmapItem *item);
89
+    void onConnectionOffsetChanged(int newOffset);
83 90
 
84 91
 signals:
85 92
     void objectsChanged();
86 93
     void selectedObjectsChanged();
94
+    void connectionOffsetChanged(int newOffset);
87 95
 };
88 96
 
89 97
 
@@ -240,6 +248,33 @@ protected:
240 248
     void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
241 249
 };
242 250
 
251
+class ConnectionPixmapItem : public QObject, public QGraphicsPixmapItem {
252
+    Q_OBJECT
253
+public:
254
+    ConnectionPixmapItem(QPixmap pixmap, Connection* connection, int x, int y): QGraphicsPixmapItem(pixmap) {
255
+        this->connection = connection;
256
+        setFlag(ItemIsMovable);
257
+        setFlag(ItemIsSelectable);
258
+        setFlag(ItemSendsGeometryChanges);
259
+        this->initialX = x;
260
+        this->initialY = y;
261
+        this->initialOffset = connection->offset.toInt();
262
+    }
263
+    Connection* connection;
264
+    int initialX;
265
+    int initialY;
266
+    int initialOffset;
267
+protected:
268
+    QVariant itemChange(GraphicsItemChange change, const QVariant &value);
269
+    void dragEnterEvent(QGraphicsSceneDragDropEvent *event) override;
270
+    void dragLeaveEvent(QGraphicsSceneDragDropEvent *event) override;
271
+    void dropEvent(QGraphicsSceneDragDropEvent *event) override;
272
+    void dragMoveEvent(QGraphicsSceneDragDropEvent *event) override;
273
+    void mousePressEvent(QGraphicsSceneMouseEvent*);
274
+signals:
275
+    void connectionMoved(int offset);
276
+};
277
+
243 278
 class MetatilesPixmapItem : public QObject, public QGraphicsPixmapItem {
244 279
     Q_OBJECT
245 280
 public:

+ 23
- 9
mainwindow.cpp Näytä tiedosto

@@ -30,6 +30,7 @@ MainWindow::MainWindow(QWidget *parent) :
30 30
     editor->gridToggleCheckbox = ui->checkBox_ToggleGrid;
31 31
     connect(editor, SIGNAL(objectsChanged()), this, SLOT(updateSelectedObjects()));
32 32
     connect(editor, SIGNAL(selectedObjectsChanged()), this, SLOT(updateSelectedObjects()));
33
+    connect(editor, SIGNAL(connectionOffsetChanged(int)), this, SLOT(onConnectionOffsetChanged(int)));
33 34
 
34 35
     on_toolButton_Paint_clicked();
35 36
 
@@ -143,15 +144,7 @@ void MainWindow::setMap(QString map_name) {
143 144
     }
144 145
     editor->setMap(map_name);
145 146
 
146
-    if (ui->tabWidget->currentIndex() == 1) {
147
-        editor->setEditingObjects();
148
-    } else {
149
-        if (ui->tabWidget_2->currentIndex() == 1) {
150
-            editor->setEditingCollision();
151
-        } else {
152
-            editor->setEditingMap();
153
-        }
154
-    }
147
+    on_tabWidget_currentChanged(ui->tabWidget->currentIndex());
155 148
 
156 149
     ui->graphicsView_Map->setScene(editor->scene);
157 150
     ui->graphicsView_Map->setSceneRect(editor->scene->sceneRect());
@@ -162,6 +155,8 @@ void MainWindow::setMap(QString map_name) {
162 155
     ui->graphicsView_Objects_Map->setFixedSize(editor->scene->width() + 2, editor->scene->height() + 2);
163 156
     ui->graphicsView_Objects_Map->editor = editor;
164 157
 
158
+    ui->graphicsView_Connections->setScene(editor->scene);
159
+
165 160
     ui->graphicsView_Metatiles->setScene(editor->scene_metatiles);
166 161
     //ui->graphicsView_Metatiles->setSceneRect(editor->scene_metatiles->sceneRect());
167 162
     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)
491 486
         on_tabWidget_2_currentChanged(ui->tabWidget_2->currentIndex());
492 487
     } else if (index == 1) {
493 488
         editor->setEditingObjects();
489
+    } else if (index == 3) {
490
+        editor->setEditingConnections(ui->comboBox_ConnectionDirection->currentText().toLower());
494 491
     }
495 492
 }
496 493
 
@@ -768,3 +765,20 @@ void MainWindow::on_action_Export_Map_Image_triggered()
768 765
         editor->map_item->pixmap().save(filepath);
769 766
     }
770 767
 }
768
+
769
+void MainWindow::on_comboBox_ConnectionDirection_currentIndexChanged(const QString &direction)
770
+{
771
+    editor->showCurrentConnectionMap(direction.toLower());
772
+}
773
+
774
+void MainWindow::on_spinBox_ConnectionOffset_valueChanged(int offset)
775
+{
776
+    editor->updateConnectionOffset(offset);
777
+}
778
+
779
+void MainWindow::onConnectionOffsetChanged(int offset)
780
+{
781
+    ui->spinBox_ConnectionOffset->blockSignals(true);
782
+    ui->spinBox_ConnectionOffset->setValue(offset);
783
+    ui->spinBox_ConnectionOffset->blockSignals(false);
784
+}

+ 6
- 0
mainwindow.h Näytä tiedosto

@@ -74,6 +74,12 @@ private slots:
74 74
 
75 75
     void on_action_Export_Map_Image_triggered();
76 76
 
77
+    void on_comboBox_ConnectionDirection_currentIndexChanged(const QString &arg1);
78
+
79
+    void on_spinBox_ConnectionOffset_valueChanged(int offset);
80
+
81
+    void onConnectionOffsetChanged(int offset);
82
+
77 83
 private:
78 84
     Ui::MainWindow *ui;
79 85
     QStandardItemModel *mapListModel;

+ 21
- 78
mainwindow.ui Näytä tiedosto

@@ -60,7 +60,7 @@
60 60
         </sizepolicy>
61 61
        </property>
62 62
        <property name="currentIndex">
63
-        <number>0</number>
63
+        <number>3</number>
64 64
        </property>
65 65
        <property name="tabsClosable">
66 66
         <bool>false</bool>
@@ -1266,7 +1266,7 @@
1266 1266
               <property name="frameShadow">
1267 1267
                <enum>QFrame::Raised</enum>
1268 1268
               </property>
1269
-              <layout class="QHBoxLayout" name="horizontalLayout_3" stretch="0,0,0,0,0">
1269
+              <layout class="QHBoxLayout" name="horizontalLayout_3" stretch="0,0,0,0,0,0,0">
1270 1270
                <property name="spacing">
1271 1271
                 <number>4</number>
1272 1272
                </property>
@@ -1286,22 +1286,22 @@
1286 1286
                 <widget class="QComboBox" name="comboBox_ConnectionDirection">
1287 1287
                  <item>
1288 1288
                   <property name="text">
1289
-                   <string>North</string>
1289
+                   <string>Up</string>
1290 1290
                   </property>
1291 1291
                  </item>
1292 1292
                  <item>
1293 1293
                   <property name="text">
1294
-                   <string>East</string>
1294
+                   <string>Right</string>
1295 1295
                   </property>
1296 1296
                  </item>
1297 1297
                  <item>
1298 1298
                   <property name="text">
1299
-                   <string>South</string>
1299
+                   <string>Down</string>
1300 1300
                   </property>
1301 1301
                  </item>
1302 1302
                  <item>
1303 1303
                   <property name="text">
1304
-                   <string>West</string>
1304
+                   <string>Left</string>
1305 1305
                   </property>
1306 1306
                  </item>
1307 1307
                 </widget>
@@ -1337,6 +1337,20 @@
1337 1337
                 </widget>
1338 1338
                </item>
1339 1339
                <item>
1340
+                <widget class="QLabel" name="label_12">
1341
+                 <property name="text">
1342
+                  <string>Offset</string>
1343
+                 </property>
1344
+                </widget>
1345
+               </item>
1346
+               <item>
1347
+                <widget class="QSpinBox" name="spinBox_ConnectionOffset">
1348
+                 <property name="minimum">
1349
+                  <number>-99</number>
1350
+                 </property>
1351
+                </widget>
1352
+               </item>
1353
+               <item>
1340 1354
                 <spacer name="horizontalSpacer_6">
1341 1355
                  <property name="orientation">
1342 1356
                   <enum>Qt::Horizontal</enum>
@@ -1377,78 +1391,7 @@
1377 1391
                 <number>0</number>
1378 1392
                </property>
1379 1393
                <item row="0" column="0">
1380
-                <widget class="QScrollArea" name="scrollArea_5">
1381
-                 <property name="widgetResizable">
1382
-                  <bool>true</bool>
1383
-                 </property>
1384
-                 <widget class="QWidget" name="scrollAreaWidgetContents_3">
1385
-                  <property name="geometry">
1386
-                   <rect>
1387
-                    <x>0</x>
1388
-                    <y>0</y>
1389
-                    <width>826</width>
1390
-                    <height>621</height>
1391
-                   </rect>
1392
-                  </property>
1393
-                  <layout class="QGridLayout" name="gridLayout_15">
1394
-                   <item row="1" column="1">
1395
-                    <widget class="QGraphicsView" name="graphicsView"/>
1396
-                   </item>
1397
-                   <item row="0" column="1">
1398
-                    <spacer name="verticalSpacer">
1399
-                     <property name="orientation">
1400
-                      <enum>Qt::Vertical</enum>
1401
-                     </property>
1402
-                     <property name="sizeHint" stdset="0">
1403
-                      <size>
1404
-                       <width>20</width>
1405
-                       <height>40</height>
1406
-                      </size>
1407
-                     </property>
1408
-                    </spacer>
1409
-                   </item>
1410
-                   <item row="2" column="1">
1411
-                    <spacer name="verticalSpacer_2">
1412
-                     <property name="orientation">
1413
-                      <enum>Qt::Vertical</enum>
1414
-                     </property>
1415
-                     <property name="sizeHint" stdset="0">
1416
-                      <size>
1417
-                       <width>20</width>
1418
-                       <height>40</height>
1419
-                      </size>
1420
-                     </property>
1421
-                    </spacer>
1422
-                   </item>
1423
-                   <item row="1" column="0">
1424
-                    <spacer name="horizontalSpacer_7">
1425
-                     <property name="orientation">
1426
-                      <enum>Qt::Horizontal</enum>
1427
-                     </property>
1428
-                     <property name="sizeHint" stdset="0">
1429
-                      <size>
1430
-                       <width>40</width>
1431
-                       <height>20</height>
1432
-                      </size>
1433
-                     </property>
1434
-                    </spacer>
1435
-                   </item>
1436
-                   <item row="1" column="2">
1437
-                    <spacer name="horizontalSpacer_8">
1438
-                     <property name="orientation">
1439
-                      <enum>Qt::Horizontal</enum>
1440
-                     </property>
1441
-                     <property name="sizeHint" stdset="0">
1442
-                      <size>
1443
-                       <width>40</width>
1444
-                       <height>20</height>
1445
-                      </size>
1446
-                     </property>
1447
-                    </spacer>
1448
-                   </item>
1449
-                  </layout>
1450
-                 </widget>
1451
-                </widget>
1394
+                <widget class="QGraphicsView" name="graphicsView_Connections"/>
1452 1395
                </item>
1453 1396
               </layout>
1454 1397
              </widget>

+ 2
- 0
map.h Näytä tiedosto

@@ -8,6 +8,7 @@
8 8
 #include <QPixmap>
9 9
 #include <QObject>
10 10
 #include <QDebug>
11
+#include <QGraphicsPixmapItem>
11 12
 
12 13
 
13 14
 template <typename T>
@@ -179,6 +180,7 @@ public:
179 180
     QMap<QString, QList<Event*>> events;
180 181
 
181 182
     QList<Connection*> connections;
183
+    QMap<QString, QGraphicsPixmapItem*> connection_items;
182 184
     QPixmap renderConnection(Connection);
183 185
 
184 186
     QImage border_image;

+ 1
- 0
project.cpp Näytä tiedosto

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