Browse Source

Support changing connection maps, adding new connections, and removing connections

Marcus Huderle 6 years ago
parent
commit
c00d83da46
6 changed files with 78 additions and 24 deletions
  1. 67
    10
      editor.cpp
  2. 4
    4
      editor.h
  3. 1
    0
      graphicsview.cpp
  4. 2
    2
      graphicsview.h
  5. 3
    7
      mainwindow.cpp
  6. 1
    1
      mainwindow.h

+ 67
- 10
editor.cpp View File

@@ -3,8 +3,9 @@
3 3
 #include <QPainter>
4 4
 #include <QMouseEvent>
5 5
 
6
-Editor::Editor()
6
+Editor::Editor(Ui::MainWindow* ui)
7 7
 {
8
+    this->ui = ui;
8 9
     selected_events = new QList<DraggablePixmapItem*>;
9 10
 }
10 11
 
@@ -85,6 +86,10 @@ void Editor::setEditingConnections(QString direction) {
85 86
         map_item->draw();
86 87
         map_item->setVisible(true);
87 88
         map_item->setEnabled(true);
89
+        ui->comboBox_ConnectedMap->blockSignals(true);
90
+        ui->comboBox_ConnectedMap->clear();
91
+        ui->comboBox_ConnectedMap->addItems(*project->mapNames);
92
+        ui->comboBox_ConnectedMap->blockSignals(false);
88 93
         setConnectionsVisibility(false);
89 94
         showCurrentConnectionMap(direction);
90 95
     }
@@ -124,6 +129,11 @@ void Editor::showCurrentConnectionMap(QString curDirection) {
124 129
             x = map->getWidth() * 16;
125 130
             y = offset * 16;
126 131
         }
132
+
133
+        QPainter painter(&pixmap);
134
+        painter.setPen(QColor(255, 0, 255));
135
+        painter.drawRect(0, 0, pixmap.width() - 1, pixmap.height() - 1);
136
+        painter.end();
127 137
         connection_item = new ConnectionPixmapItem(pixmap, connection, x, y);
128 138
         connection_item->setX(x);
129 139
         connection_item->setY(y);
@@ -133,18 +143,30 @@ void Editor::showCurrentConnectionMap(QString curDirection) {
133 143
 
134 144
         connect(connection_item, SIGNAL(connectionMoved(int)), this, SLOT(onConnectionOffsetChanged(int)));
135 145
         onConnectionOffsetChanged(connection->offset.toInt());
146
+
147
+        ui->comboBox_ConnectedMap->setCurrentText(connection->map_name);
136 148
         break;
137 149
     }
138 150
 
139
-    if (!connectionExists && connection_item) {
140
-        scene->removeItem(connection_item);
141
-        delete connection_item;
142
-        connection_item = NULL;
151
+    if (!connectionExists) {
152
+        if (connection_item) {
153
+            scene->removeItem(connection_item);
154
+            delete connection_item;
155
+            connection_item = NULL;
156
+        }
157
+
158
+        ui->comboBox_ConnectedMap->setCurrentText("");
159
+        ui->spinBox_ConnectionOffset->setDisabled(true);
160
+        ui->spinBox_ConnectionOffset->setValue(0);
161
+    } else {
162
+        ui->spinBox_ConnectionOffset->setDisabled(false);
143 163
     }
144 164
 }
145 165
 
146 166
 void Editor::onConnectionOffsetChanged(int newOffset) {
147
-    emit connectionOffsetChanged(newOffset);
167
+    ui->spinBox_ConnectionOffset->blockSignals(true);
168
+    ui->spinBox_ConnectionOffset->setValue(newOffset);
169
+    ui->spinBox_ConnectionOffset->blockSignals(false);
148 170
 }
149 171
 
150 172
 void Editor::setConnectionsVisibility(bool visible) {
@@ -330,18 +352,21 @@ void Editor::displayMapGrid() {
330 352
     for (int i = 0; i <= map->getWidth(); i++) {
331 353
         int x = i * 16;
332 354
         QGraphicsLineItem *line = scene->addLine(x, 0, x, pixelHeight);
333
-        line->setVisible(gridToggleCheckbox->isChecked());
334
-        connect(gridToggleCheckbox, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);});
355
+        line->setVisible(ui->checkBox_ToggleGrid->isChecked());
356
+        connect(ui->checkBox_ToggleGrid, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);});
335 357
     }
336 358
     for (int j = 0; j <= map->getHeight(); j++) {
337 359
         int y = j * 16;
338 360
         QGraphicsLineItem *line = scene->addLine(0, y, pixelWidth, y);
339
-        line->setVisible(gridToggleCheckbox->isChecked());
340
-        connect(gridToggleCheckbox, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);});
361
+        line->setVisible(ui->checkBox_ToggleGrid->isChecked());
362
+        connect(ui->checkBox_ToggleGrid, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);});
341 363
     }
342 364
 }
343 365
 
344 366
 void Editor::updateConnectionOffset(int offset) {
367
+    if (!connection_item)
368
+        return;
369
+
345 370
     connection_item->blockSignals(true);
346 371
     connection_item->connection->offset = QString::number(offset);
347 372
     if (connection_item->connection->direction == "up" || connection_item->connection->direction == "down") {
@@ -352,6 +377,38 @@ void Editor::updateConnectionOffset(int offset) {
352 377
     connection_item->blockSignals(false);
353 378
 }
354 379
 
380
+void Editor::updateConnectionMap(QString mapName, QString direction) {
381
+    if (!mapName.isEmpty() && !project->mapNames->contains(mapName)) {
382
+        qDebug() << "Invalid map name " << mapName << " specified for connection.";
383
+        return;
384
+    }
385
+
386
+    if (connection_item) {
387
+        // Find the connection we are updating.
388
+        bool foundConnection = false;
389
+        for (Connection* connection : map->connections) {
390
+            if (connection->direction == direction) {
391
+                foundConnection = true;
392
+                if (mapName.isEmpty()) {
393
+                    map->connections.removeOne(connection);
394
+                } else {
395
+                    connection->map_name = mapName;
396
+                }
397
+                break;
398
+            }
399
+        }
400
+    } else if (!mapName.isEmpty()) {
401
+        // Create a brand new connection.
402
+        Connection* newConnection = new Connection;
403
+        newConnection->direction = direction;
404
+        newConnection->offset = "0";
405
+        newConnection->map_name = mapName;
406
+        map->connections.append(newConnection);
407
+    }
408
+
409
+    showCurrentConnectionMap(direction);
410
+}
411
+
355 412
 void MetatilesPixmapItem::paintTileChanged(Map *map) {
356 413
     draw();
357 414
 }

+ 4
- 4
editor.h View File

@@ -9,6 +9,7 @@
9 9
 #include <QCheckBox>
10 10
 
11 11
 #include "project.h"
12
+#include "ui_mainwindow.h"
12 13
 
13 14
 class DraggablePixmapItem;
14 15
 class MapPixmapItem;
@@ -22,12 +23,12 @@ class Editor : public QObject
22 23
 {
23 24
     Q_OBJECT
24 25
 public:
25
-    Editor();
26
+    Editor(Ui::MainWindow* ui);
26 27
 public:
28
+    Ui::MainWindow* ui;
27 29
     QObject *parent = NULL;
28 30
     Project *project = NULL;
29 31
     Map *map = NULL;
30
-    QCheckBox *gridToggleCheckbox = NULL;
31 32
     void saveProject();
32 33
     void save();
33 34
     void undo();
@@ -49,6 +50,7 @@ public:
49 50
     void showCurrentConnectionMap(QString curDirection);
50 51
     void setConnectionsVisibility(bool visible);
51 52
     void updateConnectionOffset(int offset);
53
+    void updateConnectionMap(QString mapName, QString direction);
52 54
 
53 55
     DraggablePixmapItem *addMapObject(Event *event);
54 56
     void selectMapObject(DraggablePixmapItem *object);
@@ -91,7 +93,6 @@ private slots:
91 93
 signals:
92 94
     void objectsChanged();
93 95
     void selectedObjectsChanged();
94
-    void connectionOffsetChanged(int newOffset);
95 96
 };
96 97
 
97 98
 
@@ -254,7 +255,6 @@ public:
254 255
     ConnectionPixmapItem(QPixmap pixmap, Connection* connection, int x, int y): QGraphicsPixmapItem(pixmap) {
255 256
         this->connection = connection;
256 257
         setFlag(ItemIsMovable);
257
-        setFlag(ItemIsSelectable);
258 258
         setFlag(ItemSendsGeometryChanges);
259 259
         this->initialX = x;
260 260
         this->initialY = y;

+ 1
- 0
graphicsview.cpp View File

@@ -1,4 +1,5 @@
1 1
 #include "graphicsview.h"
2
+#include "editor.h"
2 3
 
3 4
 void GraphicsView::mousePressEvent(QMouseEvent *event) {
4 5
     QGraphicsView::mousePressEvent(event);

+ 2
- 2
graphicsview.h View File

@@ -4,7 +4,7 @@
4 4
 #include <QGraphicsView>
5 5
 #include <QMouseEvent>
6 6
 
7
-#include "editor.h"
7
+class Editor;
8 8
 
9 9
 /*
10 10
 class GraphicsView_Object : public QObject
@@ -26,7 +26,7 @@ public:
26 26
 
27 27
 public:
28 28
 //    GraphicsView_Object object;
29
-    Editor *editor = NULL;
29
+    Editor *editor;
30 30
 protected:
31 31
     void mousePressEvent(QMouseEvent *event);
32 32
     void mouseMoveEvent(QMouseEvent *event);

+ 3
- 7
mainwindow.cpp View File

@@ -26,11 +26,9 @@ MainWindow::MainWindow(QWidget *parent) :
26 26
     ui->setupUi(this);
27 27
     new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Z), this, SLOT(redo()));
28 28
 
29
-    editor = new Editor;
30
-    editor->gridToggleCheckbox = ui->checkBox_ToggleGrid;
29
+    editor = new Editor(ui);
31 30
     connect(editor, SIGNAL(objectsChanged()), this, SLOT(updateSelectedObjects()));
32 31
     connect(editor, SIGNAL(selectedObjectsChanged()), this, SLOT(updateSelectedObjects()));
33
-    connect(editor, SIGNAL(connectionOffsetChanged(int)), this, SLOT(onConnectionOffsetChanged(int)));
34 32
 
35 33
     on_toolButton_Paint_clicked();
36 34
 
@@ -776,9 +774,7 @@ void MainWindow::on_spinBox_ConnectionOffset_valueChanged(int offset)
776 774
     editor->updateConnectionOffset(offset);
777 775
 }
778 776
 
779
-void MainWindow::onConnectionOffsetChanged(int offset)
777
+void MainWindow::on_comboBox_ConnectedMap_currentTextChanged(const QString &mapName)
780 778
 {
781
-    ui->spinBox_ConnectionOffset->blockSignals(true);
782
-    ui->spinBox_ConnectionOffset->setValue(offset);
783
-    ui->spinBox_ConnectionOffset->blockSignals(false);
779
+    editor->updateConnectionMap(mapName, ui->comboBox_ConnectionDirection->currentText().toLower());
784 780
 }

+ 1
- 1
mainwindow.h View File

@@ -78,7 +78,7 @@ private slots:
78 78
 
79 79
     void on_spinBox_ConnectionOffset_valueChanged(int offset);
80 80
 
81
-    void onConnectionOffsetChanged(int offset);
81
+    void on_comboBox_ConnectedMap_currentTextChanged(const QString &mapName);
82 82
 
83 83
 private:
84 84
     Ui::MainWindow *ui;