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

This commit is contained in:
Marcus Huderle 2018-03-08 20:34:04 -08:00
parent 073a13ac9c
commit c00d83da46
6 changed files with 78 additions and 24 deletions

View file

@ -3,8 +3,9 @@
#include <QPainter> #include <QPainter>
#include <QMouseEvent> #include <QMouseEvent>
Editor::Editor() Editor::Editor(Ui::MainWindow* ui)
{ {
this->ui = ui;
selected_events = new QList<DraggablePixmapItem*>; selected_events = new QList<DraggablePixmapItem*>;
} }
@ -85,6 +86,10 @@ void Editor::setEditingConnections(QString direction) {
map_item->draw(); map_item->draw();
map_item->setVisible(true); map_item->setVisible(true);
map_item->setEnabled(true); map_item->setEnabled(true);
ui->comboBox_ConnectedMap->blockSignals(true);
ui->comboBox_ConnectedMap->clear();
ui->comboBox_ConnectedMap->addItems(*project->mapNames);
ui->comboBox_ConnectedMap->blockSignals(false);
setConnectionsVisibility(false); setConnectionsVisibility(false);
showCurrentConnectionMap(direction); showCurrentConnectionMap(direction);
} }
@ -124,6 +129,11 @@ void Editor::showCurrentConnectionMap(QString curDirection) {
x = map->getWidth() * 16; x = map->getWidth() * 16;
y = offset * 16; y = offset * 16;
} }
QPainter painter(&pixmap);
painter.setPen(QColor(255, 0, 255));
painter.drawRect(0, 0, pixmap.width() - 1, pixmap.height() - 1);
painter.end();
connection_item = new ConnectionPixmapItem(pixmap, connection, x, y); connection_item = new ConnectionPixmapItem(pixmap, connection, x, y);
connection_item->setX(x); connection_item->setX(x);
connection_item->setY(y); connection_item->setY(y);
@ -133,18 +143,30 @@ void Editor::showCurrentConnectionMap(QString curDirection) {
connect(connection_item, SIGNAL(connectionMoved(int)), this, SLOT(onConnectionOffsetChanged(int))); connect(connection_item, SIGNAL(connectionMoved(int)), this, SLOT(onConnectionOffsetChanged(int)));
onConnectionOffsetChanged(connection->offset.toInt()); onConnectionOffsetChanged(connection->offset.toInt());
ui->comboBox_ConnectedMap->setCurrentText(connection->map_name);
break; break;
} }
if (!connectionExists && connection_item) { if (!connectionExists) {
scene->removeItem(connection_item); if (connection_item) {
delete connection_item; scene->removeItem(connection_item);
connection_item = NULL; delete connection_item;
connection_item = NULL;
}
ui->comboBox_ConnectedMap->setCurrentText("");
ui->spinBox_ConnectionOffset->setDisabled(true);
ui->spinBox_ConnectionOffset->setValue(0);
} else {
ui->spinBox_ConnectionOffset->setDisabled(false);
} }
} }
void Editor::onConnectionOffsetChanged(int newOffset) { void Editor::onConnectionOffsetChanged(int newOffset) {
emit connectionOffsetChanged(newOffset); ui->spinBox_ConnectionOffset->blockSignals(true);
ui->spinBox_ConnectionOffset->setValue(newOffset);
ui->spinBox_ConnectionOffset->blockSignals(false);
} }
void Editor::setConnectionsVisibility(bool visible) { void Editor::setConnectionsVisibility(bool visible) {
@ -330,18 +352,21 @@ void Editor::displayMapGrid() {
for (int i = 0; i <= map->getWidth(); i++) { for (int i = 0; i <= map->getWidth(); i++) {
int x = i * 16; int x = i * 16;
QGraphicsLineItem *line = scene->addLine(x, 0, x, pixelHeight); QGraphicsLineItem *line = scene->addLine(x, 0, x, pixelHeight);
line->setVisible(gridToggleCheckbox->isChecked()); line->setVisible(ui->checkBox_ToggleGrid->isChecked());
connect(gridToggleCheckbox, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);}); connect(ui->checkBox_ToggleGrid, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);});
} }
for (int j = 0; j <= map->getHeight(); j++) { for (int j = 0; j <= map->getHeight(); j++) {
int y = j * 16; int y = j * 16;
QGraphicsLineItem *line = scene->addLine(0, y, pixelWidth, y); QGraphicsLineItem *line = scene->addLine(0, y, pixelWidth, y);
line->setVisible(gridToggleCheckbox->isChecked()); line->setVisible(ui->checkBox_ToggleGrid->isChecked());
connect(gridToggleCheckbox, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);}); connect(ui->checkBox_ToggleGrid, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);});
} }
} }
void Editor::updateConnectionOffset(int offset) { void Editor::updateConnectionOffset(int offset) {
if (!connection_item)
return;
connection_item->blockSignals(true); connection_item->blockSignals(true);
connection_item->connection->offset = QString::number(offset); connection_item->connection->offset = QString::number(offset);
if (connection_item->connection->direction == "up" || connection_item->connection->direction == "down") { if (connection_item->connection->direction == "up" || connection_item->connection->direction == "down") {
@ -352,6 +377,38 @@ void Editor::updateConnectionOffset(int offset) {
connection_item->blockSignals(false); connection_item->blockSignals(false);
} }
void Editor::updateConnectionMap(QString mapName, QString direction) {
if (!mapName.isEmpty() && !project->mapNames->contains(mapName)) {
qDebug() << "Invalid map name " << mapName << " specified for connection.";
return;
}
if (connection_item) {
// Find the connection we are updating.
bool foundConnection = false;
for (Connection* connection : map->connections) {
if (connection->direction == direction) {
foundConnection = true;
if (mapName.isEmpty()) {
map->connections.removeOne(connection);
} else {
connection->map_name = mapName;
}
break;
}
}
} else if (!mapName.isEmpty()) {
// Create a brand new connection.
Connection* newConnection = new Connection;
newConnection->direction = direction;
newConnection->offset = "0";
newConnection->map_name = mapName;
map->connections.append(newConnection);
}
showCurrentConnectionMap(direction);
}
void MetatilesPixmapItem::paintTileChanged(Map *map) { void MetatilesPixmapItem::paintTileChanged(Map *map) {
draw(); draw();
} }

View file

@ -9,6 +9,7 @@
#include <QCheckBox> #include <QCheckBox>
#include "project.h" #include "project.h"
#include "ui_mainwindow.h"
class DraggablePixmapItem; class DraggablePixmapItem;
class MapPixmapItem; class MapPixmapItem;
@ -22,12 +23,12 @@ class Editor : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
Editor(); Editor(Ui::MainWindow* ui);
public: public:
Ui::MainWindow* ui;
QObject *parent = NULL; QObject *parent = NULL;
Project *project = NULL; Project *project = NULL;
Map *map = NULL; Map *map = NULL;
QCheckBox *gridToggleCheckbox = NULL;
void saveProject(); void saveProject();
void save(); void save();
void undo(); void undo();
@ -49,6 +50,7 @@ public:
void showCurrentConnectionMap(QString curDirection); void showCurrentConnectionMap(QString curDirection);
void setConnectionsVisibility(bool visible); void setConnectionsVisibility(bool visible);
void updateConnectionOffset(int offset); void updateConnectionOffset(int offset);
void updateConnectionMap(QString mapName, QString direction);
DraggablePixmapItem *addMapObject(Event *event); DraggablePixmapItem *addMapObject(Event *event);
void selectMapObject(DraggablePixmapItem *object); void selectMapObject(DraggablePixmapItem *object);
@ -91,7 +93,6 @@ private slots:
signals: signals:
void objectsChanged(); void objectsChanged();
void selectedObjectsChanged(); void selectedObjectsChanged();
void connectionOffsetChanged(int newOffset);
}; };
@ -254,7 +255,6 @@ public:
ConnectionPixmapItem(QPixmap pixmap, Connection* connection, int x, int y): QGraphicsPixmapItem(pixmap) { ConnectionPixmapItem(QPixmap pixmap, Connection* connection, int x, int y): QGraphicsPixmapItem(pixmap) {
this->connection = connection; this->connection = connection;
setFlag(ItemIsMovable); setFlag(ItemIsMovable);
setFlag(ItemIsSelectable);
setFlag(ItemSendsGeometryChanges); setFlag(ItemSendsGeometryChanges);
this->initialX = x; this->initialX = x;
this->initialY = y; this->initialY = y;

View file

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

View file

@ -4,7 +4,7 @@
#include <QGraphicsView> #include <QGraphicsView>
#include <QMouseEvent> #include <QMouseEvent>
#include "editor.h" class Editor;
/* /*
class GraphicsView_Object : public QObject class GraphicsView_Object : public QObject
@ -26,7 +26,7 @@ public:
public: public:
// GraphicsView_Object object; // GraphicsView_Object object;
Editor *editor = NULL; Editor *editor;
protected: protected:
void mousePressEvent(QMouseEvent *event); void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event);

View file

@ -26,11 +26,9 @@ MainWindow::MainWindow(QWidget *parent) :
ui->setupUi(this); ui->setupUi(this);
new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Z), this, SLOT(redo())); new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Z), this, SLOT(redo()));
editor = new Editor; editor = new Editor(ui);
editor->gridToggleCheckbox = ui->checkBox_ToggleGrid;
connect(editor, SIGNAL(objectsChanged()), this, SLOT(updateSelectedObjects())); connect(editor, SIGNAL(objectsChanged()), this, SLOT(updateSelectedObjects()));
connect(editor, SIGNAL(selectedObjectsChanged()), this, SLOT(updateSelectedObjects())); connect(editor, SIGNAL(selectedObjectsChanged()), this, SLOT(updateSelectedObjects()));
connect(editor, SIGNAL(connectionOffsetChanged(int)), this, SLOT(onConnectionOffsetChanged(int)));
on_toolButton_Paint_clicked(); on_toolButton_Paint_clicked();
@ -776,9 +774,7 @@ void MainWindow::on_spinBox_ConnectionOffset_valueChanged(int offset)
editor->updateConnectionOffset(offset); editor->updateConnectionOffset(offset);
} }
void MainWindow::onConnectionOffsetChanged(int offset) void MainWindow::on_comboBox_ConnectedMap_currentTextChanged(const QString &mapName)
{ {
ui->spinBox_ConnectionOffset->blockSignals(true); editor->updateConnectionMap(mapName, ui->comboBox_ConnectionDirection->currentText().toLower());
ui->spinBox_ConnectionOffset->setValue(offset);
ui->spinBox_ConnectionOffset->blockSignals(false);
} }

View file

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