Преглед на файлове

Support adding/removing connections via buttons

Marcus Huderle преди 6 години
родител
ревизия
5301b299e2
променени са 5 файла, в които са добавени 203 реда и са изтрити 44 реда
  1. 86
    40
      editor.cpp
  2. 3
    0
      editor.h
  3. 10
    0
      mainwindow.cpp
  4. 4
    0
      mainwindow.h
  5. 100
    4
      mainwindow.ui

+ 86
- 40
editor.cpp Целия файл

@@ -97,6 +97,7 @@ void Editor::setEditingConnections() {
97 97
         ui->comboBox_ConnectedMap->clear();
98 98
         ui->comboBox_ConnectedMap->addItems(*project->mapNames);
99 99
         ui->comboBox_ConnectedMap->blockSignals(false);
100
+        ui->label_NumConnections->setText(QString::number(map->connections.length()));
100 101
         setConnectionsVisibility(false);
101 102
         if (current_connection_edit_item) {
102 103
             onConnectionOffsetChanged(current_connection_edit_item->connection->offset.toInt());
@@ -195,6 +196,9 @@ void Editor::setConnectionEditControlsEnabled(bool enabled) {
195 196
 }
196 197
 
197 198
 void Editor::onConnectionItemSelected(ConnectionPixmapItem* connectionItem) {
199
+    if (!connectionItem)
200
+        return;
201
+
198 202
     for (ConnectionPixmapItem* item : connection_edit_items) {
199 203
         bool isSelectedItem = item == connectionItem;
200 204
         int zValue = isSelectedItem ? 0 : -1;
@@ -372,39 +376,7 @@ void Editor::displayMapConnections() {
372 376
         if (connection->direction == "dive" || connection->direction == "emerge") {
373 377
             continue;
374 378
         }
375
-        Map *connected_map = project->getMap(connection->map_name);
376
-        QPixmap pixmap = connected_map->renderConnection(*connection);
377
-        int offset = connection->offset.toInt(nullptr, 0);
378
-        int x = 0, y = 0;
379
-        if (connection->direction == "up") {
380
-            x = offset * 16;
381
-            y = -pixmap.height();
382
-        } else if (connection->direction == "down") {
383
-            x = offset * 16;
384
-            y = map->getHeight() * 16;
385
-        } else if (connection->direction == "left") {
386
-            x = -pixmap.width();
387
-            y = offset * 16;
388
-        } else if (connection->direction == "right") {
389
-            x = map->getWidth() * 16;
390
-            y = offset * 16;
391
-        }
392
-
393
-        QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
394
-        item->setZValue(-1);
395
-        item->setX(x);
396
-        item->setY(y);
397
-        scene->addItem(item);
398
-        map->connection_items.append(item);
399
-
400
-        ConnectionPixmapItem *connection_edit_item = new ConnectionPixmapItem(pixmap, connection, x, y);
401
-        connection_edit_item->setX(x);
402
-        connection_edit_item->setY(y);
403
-        connection_edit_item->setZValue(-1);
404
-        scene->addItem(connection_edit_item);
405
-        connect(connection_edit_item, SIGNAL(connectionMoved(int)), this, SLOT(onConnectionOffsetChanged(int)));
406
-        connect(connection_edit_item, SIGNAL(connectionItemSelected(ConnectionPixmapItem*)), this, SLOT(onConnectionItemSelected(ConnectionPixmapItem*)));
407
-        connection_edit_items.append(connection_edit_item);
379
+        createConnectionItem(connection, false);
408 380
     }
409 381
 
410 382
     if (!connection_edit_items.empty()) {
@@ -412,6 +384,43 @@ void Editor::displayMapConnections() {
412 384
     }
413 385
 }
414 386
 
387
+void Editor::createConnectionItem(Connection* connection, bool hide) {
388
+    Map *connected_map = project->getMap(connection->map_name);
389
+    QPixmap pixmap = connected_map->renderConnection(*connection);
390
+    int offset = connection->offset.toInt(nullptr, 0);
391
+    int x = 0, y = 0;
392
+    if (connection->direction == "up") {
393
+        x = offset * 16;
394
+        y = -pixmap.height();
395
+    } else if (connection->direction == "down") {
396
+        x = offset * 16;
397
+        y = map->getHeight() * 16;
398
+    } else if (connection->direction == "left") {
399
+        x = -pixmap.width();
400
+        y = offset * 16;
401
+    } else if (connection->direction == "right") {
402
+        x = map->getWidth() * 16;
403
+        y = offset * 16;
404
+    }
405
+
406
+    QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
407
+    item->setZValue(-1);
408
+    item->setX(x);
409
+    item->setY(y);
410
+    scene->addItem(item);
411
+    map->connection_items.append(item);
412
+    item->setVisible(!hide);
413
+
414
+    ConnectionPixmapItem *connection_edit_item = new ConnectionPixmapItem(pixmap, connection, x, y);
415
+    connection_edit_item->setX(x);
416
+    connection_edit_item->setY(y);
417
+    connection_edit_item->setZValue(-1);
418
+    scene->addItem(connection_edit_item);
419
+    connect(connection_edit_item, SIGNAL(connectionMoved(int)), this, SLOT(onConnectionOffsetChanged(int)));
420
+    connect(connection_edit_item, SIGNAL(connectionItemSelected(ConnectionPixmapItem*)), this, SLOT(onConnectionItemSelected(ConnectionPixmapItem*)));
421
+    connection_edit_items.append(connection_edit_item);
422
+}
423
+
415 424
 void Editor::displayMapBorder() {
416 425
     QPixmap pixmap = map->renderBorder();
417 426
     for (int y = -6; y < map->getHeight() + 6; y += 2)
@@ -465,13 +474,7 @@ void Editor::updateConnectionMap(QString mapName, QString direction) {
465 474
         return;
466 475
 
467 476
     if (mapName.isEmpty()) {
468
-        map->connections.removeOne(current_connection_edit_item->connection);
469
-        connection_edit_items.removeOne(current_connection_edit_item);
470
-        scene->removeItem(current_connection_edit_item);
471
-        delete current_connection_edit_item;
472
-        current_connection_edit_item = NULL;
473
-        setConnectionEditControlsEnabled(false);
474
-        ui->spinBox_ConnectionOffset->setValue(0);
477
+        removeCurrentConnection();
475 478
         return;
476 479
     } else {
477 480
         setConnectionEditControlsEnabled(true);
@@ -481,6 +484,49 @@ void Editor::updateConnectionMap(QString mapName, QString direction) {
481 484
     setCurrentConnectionDirection(direction);
482 485
 }
483 486
 
487
+void Editor::addNewConnection() {
488
+    // Find direction with least number of connections.
489
+    QMap<QString, int> directionCounts = QMap<QString, int>({{"up", 0}, {"right", 0}, {"down", 0}, {"left", 0}});
490
+    for (Connection* connection : map->connections) {
491
+        directionCounts[connection->direction]++;
492
+    }
493
+    QString minDirection = "up";
494
+    int minCount = INT_MAX;
495
+    for (QString direction : directionCounts.keys()) {
496
+        if (directionCounts[direction] < minCount) {
497
+            minDirection = direction;
498
+            minCount = directionCounts[direction];
499
+        }
500
+    }
501
+
502
+    Connection* newConnection = new Connection;
503
+    newConnection->direction = minDirection;
504
+    newConnection->offset = "0";
505
+    newConnection->map_name = project->mapNames->first();
506
+    map->connections.append(newConnection);
507
+    createConnectionItem(newConnection, true);
508
+    onConnectionItemSelected(connection_edit_items.last());
509
+    ui->label_NumConnections->setText(QString::number(map->connections.length()));
510
+}
511
+
512
+void Editor::removeCurrentConnection() {
513
+    if (!current_connection_edit_item)
514
+        return;
515
+
516
+    map->connections.removeOne(current_connection_edit_item->connection);
517
+    connection_edit_items.removeOne(current_connection_edit_item);
518
+    scene->removeItem(current_connection_edit_item);
519
+    delete current_connection_edit_item;
520
+    current_connection_edit_item = NULL;
521
+    setConnectionEditControlsEnabled(false);
522
+    ui->spinBox_ConnectionOffset->setValue(0);
523
+    ui->label_NumConnections->setText(QString::number(map->connections.length()));
524
+
525
+    if (connection_edit_items.length() > 0) {
526
+        onConnectionItemSelected(connection_edit_items.last());
527
+    }
528
+}
529
+
484 530
 void MetatilesPixmapItem::paintTileChanged(Map *map) {
485 531
     draw();
486 532
 }

+ 3
- 0
editor.h Целия файл

@@ -51,6 +51,8 @@ public:
51 51
     void setConnectionsVisibility(bool visible);
52 52
     void updateConnectionOffset(int offset);
53 53
     void updateConnectionMap(QString mapName, QString direction);
54
+    void addNewConnection();
55
+    void removeCurrentConnection();
54 56
 
55 57
     DraggablePixmapItem *addMapObject(Event *event);
56 58
     void selectMapObject(DraggablePixmapItem *object);
@@ -92,6 +94,7 @@ private:
92 94
     void setBorderItemsVisible(bool, qreal = 1);
93 95
     void setConnectionEditControlValues(Connection*);
94 96
     void setConnectionEditControlsEnabled(bool);
97
+    void createConnectionItem(Connection* connection, bool hide);
95 98
 
96 99
 private slots:
97 100
     void mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item);

+ 10
- 0
mainwindow.cpp Целия файл

@@ -780,3 +780,13 @@ void MainWindow::on_comboBox_ConnectedMap_currentTextChanged(const QString &mapN
780 780
 {
781 781
     editor->updateConnectionMap(mapName, ui->comboBox_ConnectionDirection->currentText().toLower());
782 782
 }
783
+
784
+void MainWindow::on_pushButton_AddConnection_clicked()
785
+{
786
+    editor->addNewConnection();
787
+}
788
+
789
+void MainWindow::on_pushButton_RemoveConnection_clicked()
790
+{
791
+    editor->removeCurrentConnection();
792
+}

+ 4
- 0
mainwindow.h Целия файл

@@ -80,6 +80,10 @@ private slots:
80 80
 
81 81
     void on_comboBox_ConnectedMap_currentTextChanged(const QString &mapName);
82 82
 
83
+    void on_pushButton_AddConnection_clicked();
84
+
85
+    void on_pushButton_RemoveConnection_clicked();
86
+
83 87
 private:
84 88
     Ui::MainWindow *ui;
85 89
     QStandardItemModel *mapListModel;

+ 100
- 4
mainwindow.ui Целия файл

@@ -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>
@@ -1246,7 +1246,7 @@
1246 1246
             <property name="spacing">
1247 1247
              <number>0</number>
1248 1248
             </property>
1249
-            <item row="0" column="0">
1249
+            <item row="1" column="0">
1250 1250
              <widget class="QFrame" name="horizontalFrame">
1251 1251
               <property name="sizePolicy">
1252 1252
                <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
@@ -1369,7 +1369,7 @@
1369 1369
               </layout>
1370 1370
              </widget>
1371 1371
             </item>
1372
-            <item row="1" column="0">
1372
+            <item row="2" column="0">
1373 1373
              <widget class="QFrame" name="gridFrame">
1374 1374
               <property name="frameShape">
1375 1375
                <enum>QFrame::StyledPanel</enum>
@@ -1410,7 +1410,7 @@
1410 1410
                     <x>0</x>
1411 1411
                     <y>0</y>
1412 1412
                     <width>826</width>
1413
-                    <height>621</height>
1413
+                    <height>587</height>
1414 1414
                    </rect>
1415 1415
                   </property>
1416 1416
                   <layout class="QGridLayout" name="gridLayout_14">
@@ -1486,6 +1486,102 @@
1486 1486
               </layout>
1487 1487
              </widget>
1488 1488
             </item>
1489
+            <item row="0" column="0">
1490
+             <widget class="QFrame" name="horizontalFrame">
1491
+              <property name="sizePolicy">
1492
+               <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
1493
+                <horstretch>0</horstretch>
1494
+                <verstretch>0</verstretch>
1495
+               </sizepolicy>
1496
+              </property>
1497
+              <property name="minimumSize">
1498
+               <size>
1499
+                <width>0</width>
1500
+                <height>32</height>
1501
+               </size>
1502
+              </property>
1503
+              <property name="frameShape">
1504
+               <enum>QFrame::StyledPanel</enum>
1505
+              </property>
1506
+              <property name="frameShadow">
1507
+               <enum>QFrame::Raised</enum>
1508
+              </property>
1509
+              <layout class="QHBoxLayout" name="horizontalLayout_4">
1510
+               <property name="spacing">
1511
+                <number>4</number>
1512
+               </property>
1513
+               <property name="leftMargin">
1514
+                <number>4</number>
1515
+               </property>
1516
+               <property name="topMargin">
1517
+                <number>4</number>
1518
+               </property>
1519
+               <property name="rightMargin">
1520
+                <number>4</number>
1521
+               </property>
1522
+               <property name="bottomMargin">
1523
+                <number>4</number>
1524
+               </property>
1525
+               <item>
1526
+                <widget class="QPushButton" name="pushButton_AddConnection">
1527
+                 <property name="sizePolicy">
1528
+                  <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
1529
+                   <horstretch>0</horstretch>
1530
+                   <verstretch>0</verstretch>
1531
+                  </sizepolicy>
1532
+                 </property>
1533
+                 <property name="text">
1534
+                  <string/>
1535
+                 </property>
1536
+                 <property name="icon">
1537
+                  <iconset>
1538
+                   <activeon>:/icons/add.ico</activeon>
1539
+                  </iconset>
1540
+                 </property>
1541
+                </widget>
1542
+               </item>
1543
+               <item>
1544
+                <widget class="QPushButton" name="pushButton_RemoveConnection">
1545
+                 <property name="text">
1546
+                  <string/>
1547
+                 </property>
1548
+                 <property name="icon">
1549
+                  <iconset>
1550
+                   <activeon>:/icons/delete.ico</activeon>
1551
+                  </iconset>
1552
+                 </property>
1553
+                </widget>
1554
+               </item>
1555
+               <item>
1556
+                <widget class="QLabel" name="label_13">
1557
+                 <property name="text">
1558
+                  <string>Number of Connections:</string>
1559
+                 </property>
1560
+                </widget>
1561
+               </item>
1562
+               <item>
1563
+                <widget class="QLabel" name="label_NumConnections">
1564
+                 <property name="text">
1565
+                  <string/>
1566
+                 </property>
1567
+                </widget>
1568
+               </item>
1569
+               <item>
1570
+                <spacer name="horizontalSpacer_9">
1571
+                 <property name="orientation">
1572
+                  <enum>Qt::Horizontal</enum>
1573
+                 </property>
1574
+                 <property name="sizeHint" stdset="0">
1575
+                  <size>
1576
+                   <width>40</width>
1577
+                   <height>20</height>
1578
+                  </size>
1579
+                 </property>
1580
+                </spacer>
1581
+               </item>
1582
+              </layout>
1583
+             </widget>
1584
+            </item>
1489 1585
            </layout>
1490 1586
           </widget>
1491 1587
          </item>