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
 #include <QPainter>
3
 #include <QPainter>
4
 #include <QMouseEvent>
4
 #include <QMouseEvent>
5
 
5
 
6
-Editor::Editor()
6
+Editor::Editor(Ui::MainWindow* ui)
7
 {
7
 {
8
+    this->ui = ui;
8
     selected_events = new QList<DraggablePixmapItem*>;
9
     selected_events = new QList<DraggablePixmapItem*>;
9
 }
10
 }
10
 
11
 
85
         map_item->draw();
86
         map_item->draw();
86
         map_item->setVisible(true);
87
         map_item->setVisible(true);
87
         map_item->setEnabled(true);
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
         setConnectionsVisibility(false);
93
         setConnectionsVisibility(false);
89
         showCurrentConnectionMap(direction);
94
         showCurrentConnectionMap(direction);
90
     }
95
     }
124
             x = map->getWidth() * 16;
129
             x = map->getWidth() * 16;
125
             y = offset * 16;
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
         connection_item = new ConnectionPixmapItem(pixmap, connection, x, y);
137
         connection_item = new ConnectionPixmapItem(pixmap, connection, x, y);
128
         connection_item->setX(x);
138
         connection_item->setX(x);
129
         connection_item->setY(y);
139
         connection_item->setY(y);
133
 
143
 
134
         connect(connection_item, SIGNAL(connectionMoved(int)), this, SLOT(onConnectionOffsetChanged(int)));
144
         connect(connection_item, SIGNAL(connectionMoved(int)), this, SLOT(onConnectionOffsetChanged(int)));
135
         onConnectionOffsetChanged(connection->offset.toInt());
145
         onConnectionOffsetChanged(connection->offset.toInt());
146
+
147
+        ui->comboBox_ConnectedMap->setCurrentText(connection->map_name);
136
         break;
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
 void Editor::onConnectionOffsetChanged(int newOffset) {
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
 void Editor::setConnectionsVisibility(bool visible) {
172
 void Editor::setConnectionsVisibility(bool visible) {
330
     for (int i = 0; i <= map->getWidth(); i++) {
352
     for (int i = 0; i <= map->getWidth(); i++) {
331
         int x = i * 16;
353
         int x = i * 16;
332
         QGraphicsLineItem *line = scene->addLine(x, 0, x, pixelHeight);
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
     for (int j = 0; j <= map->getHeight(); j++) {
358
     for (int j = 0; j <= map->getHeight(); j++) {
337
         int y = j * 16;
359
         int y = j * 16;
338
         QGraphicsLineItem *line = scene->addLine(0, y, pixelWidth, y);
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
 void Editor::updateConnectionOffset(int offset) {
366
 void Editor::updateConnectionOffset(int offset) {
367
+    if (!connection_item)
368
+        return;
369
+
345
     connection_item->blockSignals(true);
370
     connection_item->blockSignals(true);
346
     connection_item->connection->offset = QString::number(offset);
371
     connection_item->connection->offset = QString::number(offset);
347
     if (connection_item->connection->direction == "up" || connection_item->connection->direction == "down") {
372
     if (connection_item->connection->direction == "up" || connection_item->connection->direction == "down") {
352
     connection_item->blockSignals(false);
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
 void MetatilesPixmapItem::paintTileChanged(Map *map) {
412
 void MetatilesPixmapItem::paintTileChanged(Map *map) {
356
     draw();
413
     draw();
357
 }
414
 }

+ 4
- 4
editor.h View File

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

+ 1
- 0
graphicsview.cpp View File

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

+ 2
- 2
graphicsview.h View File

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

+ 3
- 7
mainwindow.cpp View File

26
     ui->setupUi(this);
26
     ui->setupUi(this);
27
     new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Z), this, SLOT(redo()));
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
     connect(editor, SIGNAL(objectsChanged()), this, SLOT(updateSelectedObjects()));
30
     connect(editor, SIGNAL(objectsChanged()), this, SLOT(updateSelectedObjects()));
32
     connect(editor, SIGNAL(selectedObjectsChanged()), this, SLOT(updateSelectedObjects()));
31
     connect(editor, SIGNAL(selectedObjectsChanged()), this, SLOT(updateSelectedObjects()));
33
-    connect(editor, SIGNAL(connectionOffsetChanged(int)), this, SLOT(onConnectionOffsetChanged(int)));
34
 
32
 
35
     on_toolButton_Paint_clicked();
33
     on_toolButton_Paint_clicked();
36
 
34
 
776
     editor->updateConnectionOffset(offset);
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
 
78
 
79
     void on_spinBox_ConnectionOffset_valueChanged(int offset);
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
 private:
83
 private:
84
     Ui::MainWindow *ui;
84
     Ui::MainWindow *ui;