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 <QMouseEvent>
Editor::Editor()
Editor::Editor(Ui::MainWindow* ui)
{
this->ui = ui;
selected_events = new QList<DraggablePixmapItem*>;
}
@ -85,6 +86,10 @@ void Editor::setEditingConnections(QString direction) {
map_item->draw();
map_item->setVisible(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);
showCurrentConnectionMap(direction);
}
@ -124,6 +129,11 @@ void Editor::showCurrentConnectionMap(QString curDirection) {
x = map->getWidth() * 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->setX(x);
connection_item->setY(y);
@ -133,18 +143,30 @@ void Editor::showCurrentConnectionMap(QString curDirection) {
connect(connection_item, SIGNAL(connectionMoved(int)), this, SLOT(onConnectionOffsetChanged(int)));
onConnectionOffsetChanged(connection->offset.toInt());
ui->comboBox_ConnectedMap->setCurrentText(connection->map_name);
break;
}
if (!connectionExists && connection_item) {
scene->removeItem(connection_item);
delete connection_item;
connection_item = NULL;
if (!connectionExists) {
if (connection_item) {
scene->removeItem(connection_item);
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) {
emit connectionOffsetChanged(newOffset);
ui->spinBox_ConnectionOffset->blockSignals(true);
ui->spinBox_ConnectionOffset->setValue(newOffset);
ui->spinBox_ConnectionOffset->blockSignals(false);
}
void Editor::setConnectionsVisibility(bool visible) {
@ -330,18 +352,21 @@ void Editor::displayMapGrid() {
for (int i = 0; i <= map->getWidth(); i++) {
int x = i * 16;
QGraphicsLineItem *line = scene->addLine(x, 0, x, pixelHeight);
line->setVisible(gridToggleCheckbox->isChecked());
connect(gridToggleCheckbox, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);});
line->setVisible(ui->checkBox_ToggleGrid->isChecked());
connect(ui->checkBox_ToggleGrid, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);});
}
for (int j = 0; j <= map->getHeight(); j++) {
int y = j * 16;
QGraphicsLineItem *line = scene->addLine(0, y, pixelWidth, y);
line->setVisible(gridToggleCheckbox->isChecked());
connect(gridToggleCheckbox, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);});
line->setVisible(ui->checkBox_ToggleGrid->isChecked());
connect(ui->checkBox_ToggleGrid, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);});
}
}
void Editor::updateConnectionOffset(int offset) {
if (!connection_item)
return;
connection_item->blockSignals(true);
connection_item->connection->offset = QString::number(offset);
if (connection_item->connection->direction == "up" || connection_item->connection->direction == "down") {
@ -352,6 +377,38 @@ void Editor::updateConnectionOffset(int offset) {
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) {
draw();
}

View file

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

View file

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

View file

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

View file

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

View file

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