From 6fbc6d8d8604cdc6d2adf23627eeb29b5c72da0d Mon Sep 17 00:00:00 2001 From: GriffinR Date: Thu, 8 Aug 2024 21:24:15 -0400 Subject: [PATCH] Prompt for new map connections --- forms/newmapconnectiondialog.ui | 95 +++++++++++++++++++++++++++++ include/editor.h | 2 +- include/ui/newmapconnectiondialog.h | 29 +++++++++ porymap.pro | 3 + src/editor.cpp | 31 ++-------- src/mainwindow.cpp | 8 ++- src/ui/newmapconnectiondialog.cpp | 62 +++++++++++++++++++ 7 files changed, 201 insertions(+), 29 deletions(-) create mode 100644 forms/newmapconnectiondialog.ui create mode 100644 include/ui/newmapconnectiondialog.h create mode 100644 src/ui/newmapconnectiondialog.cpp diff --git a/forms/newmapconnectiondialog.ui b/forms/newmapconnectiondialog.ui new file mode 100644 index 00000000..da109604 --- /dev/null +++ b/forms/newmapconnectiondialog.ui @@ -0,0 +1,95 @@ + + + NewMapConnectionDialog + + + + 0 + 0 + 234 + 120 + + + + Add New Map Connection + + + + + + QFrame::NoFrame + + + QFrame::Plain + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Map + + + + + + + + + + Direction + + + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + NoScrollComboBox + QComboBox +
noscrollcombobox.h
+
+
+ + + + buttonBox + accepted() + NewMapConnectionDialog + accept() + + + buttonBox + rejected() + NewMapConnectionDialog + reject() + + +
diff --git a/include/editor.h b/include/editor.h index e1a24de6..db07fd87 100644 --- a/include/editor.h +++ b/include/editor.h @@ -77,7 +77,6 @@ public: void setMapEditingButtonsEnabled(bool enabled); void setConnectionsVisibility(bool visible); void updateDiveEmergeVisibility(); - void addNewConnection(); void addConnection(MapConnection* connection, bool addMirror = true); void removeConnection(MapConnection* connection, bool addMirror = true); void removeConnectionPixmap(ConnectionPixmapItem* connectionItem); @@ -86,6 +85,7 @@ public: void deleteWildMonGroup(); void updateDiveMap(QString mapName); void updateEmergeMap(QString mapName); + void selectLastConnection(); void setSelectedConnectionFromMap(QString mapName); void updatePrimaryTileset(QString tilesetLabel, bool forceLoad = false); void updateSecondaryTileset(QString tilesetLabel, bool forceLoad = false); diff --git a/include/ui/newmapconnectiondialog.h b/include/ui/newmapconnectiondialog.h new file mode 100644 index 00000000..8d053515 --- /dev/null +++ b/include/ui/newmapconnectiondialog.h @@ -0,0 +1,29 @@ +#ifndef NEWMAPCONNECTIONDIALOG_H +#define NEWMAPCONNECTIONDIALOG_H + +#include +#include "map.h" +#include "mapconnection.h" + +namespace Ui { +class NewMapConnectionDialog; +} + +class NewMapConnectionDialog : public QDialog +{ + Q_OBJECT + +public: + explicit NewMapConnectionDialog(QWidget *parent, Map* map, const QStringList &mapNames); + ~NewMapConnectionDialog(); + + MapConnection *result; + + virtual void accept() override; + +private: + Ui::NewMapConnectionDialog *ui; + Map *map; +}; + +#endif // NEWMAPCONNECTIONDIALOG_H diff --git a/porymap.pro b/porymap.pro index 8afc99a5..8f785541 100644 --- a/porymap.pro +++ b/porymap.pro @@ -55,6 +55,7 @@ SOURCES += src/core/block.cpp \ src/ui/collisionpixmapitem.cpp \ src/ui/connectionpixmapitem.cpp \ src/ui/currentselectedmetatilespixmapitem.cpp \ + src/ui/newmapconnectiondialog.cpp \ src/ui/overlay.cpp \ src/ui/prefab.cpp \ src/ui/projectsettingseditor.cpp \ @@ -149,6 +150,7 @@ HEADERS += include/core/block.h \ include/ui/collisionpixmapitem.h \ include/ui/connectionpixmapitem.h \ include/ui/currentselectedmetatilespixmapitem.h \ + include/ui/newmapconnectiondialog.h \ include/ui/prefabframe.h \ include/ui/projectsettingseditor.h \ include/ui/regionmaplayoutpixmapitem.h \ @@ -209,6 +211,7 @@ HEADERS += include/core/block.h \ FORMS += forms/mainwindow.ui \ forms/connectionslistitem.ui \ + forms/newmapconnectiondialog.ui \ forms/prefabcreationdialog.ui \ forms/prefabframe.ui \ forms/tileseteditor.ui \ diff --git a/src/editor.cpp b/src/editor.cpp index 64ba76d3..db93600c 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -752,6 +752,7 @@ void Editor::displayConnection(MapConnection* connection) { ConnectionPixmapItem *pixmapItem = new ConnectionPixmapItem(pixmap, connection, pos.x(), pos.y()); pixmapItem->render(); scene->addItem(pixmapItem); + maskNonVisibleConnectionTiles(); // Create item for the list panel ConnectionsListItem *listItem = new ConnectionsListItem(ui->scrollAreaContents_ConnectionsList, pixmapItem->connection, project->mapNames); @@ -812,32 +813,6 @@ void Editor::displayConnection(MapConnection* connection) { connection_items.append(pixmapItem); } - -void Editor::addNewConnection() { - // Find direction with least number of connections. - QMap directionCounts; - for (MapConnection* connection : map->connections) { - directionCounts[connection->direction()]++; - } - QString minDirection = "up"; - int minCount = INT_MAX; - for (QString direction : MapConnection::cardinalDirections) { - if (directionCounts[direction] < minCount) { - minDirection = direction; - minCount = directionCounts[direction]; - } - } - - // Prefer not to connect the map to itself (we have to if it's the only map). - QString defaultMapName = project->mapNames.first(); - if (defaultMapName == map->name && project->mapNames.length() > 1) { - defaultMapName = project->mapNames.at(1); - } - - addConnection(new MapConnection(minDirection, map->name, defaultMapName)); - setSelectedConnection(connection_items.last()); -} - void Editor::addConnection(MapConnection * connection, bool addMirror) { if (!connection) return; @@ -1056,6 +1031,10 @@ void Editor::setSelectedConnectionFromMap(QString mapName) { } } +void Editor::selectLastConnection() { + setSelectedConnection(connection_items.last()); +} + void Editor::onMapConnectionDoubleClicked(MapConnection* connection) { emit openConnectedMap(connection->targetMapName(), connection->hostMapName()); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 5f6e3573..81e19609 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -19,6 +19,7 @@ #include "prefab.h" #include "montabwidget.h" #include "imageexport.h" +#include "newmapconnectiondialog.h" #include #include @@ -2649,8 +2650,11 @@ void MainWindow::showExportMapImageWindow(ImageExporterMode mode) { void MainWindow::on_pushButton_AddConnection_clicked() { - // TODO: Bring up a prompt for information? - editor->addNewConnection(); + auto dialog = new NewMapConnectionDialog(this, this->editor->map, this->editor->project->mapNames); + if(dialog->exec() == QDialog::Accepted) { + this->editor->addConnection(dialog->result); + this->editor->selectLastConnection(); + } } void MainWindow::on_pushButton_NewWildMonGroup_clicked() { diff --git a/src/ui/newmapconnectiondialog.cpp b/src/ui/newmapconnectiondialog.cpp new file mode 100644 index 00000000..3a7aa481 --- /dev/null +++ b/src/ui/newmapconnectiondialog.cpp @@ -0,0 +1,62 @@ +#include "newmapconnectiondialog.h" +#include "ui_newmapconnectiondialog.h" + +NewMapConnectionDialog::NewMapConnectionDialog(QWidget *parent, Map* map, const QStringList &mapNames) : + QDialog(parent), + ui(new Ui::NewMapConnectionDialog) +{ + ui->setupUi(this); + setAttribute(Qt::WA_DeleteOnClose); + + this->map = map; + this->result = nullptr; + + ui->comboBox_Direction->setEditable(false); + ui->comboBox_Direction->setMinimumContentsLength(0); + ui->comboBox_Direction->addItems(MapConnection::cardinalDirections); + + ui->comboBox_Map->setMinimumContentsLength(6); + ui->comboBox_Map->addItems(mapNames); + + // Choose default direction + QMap directionCounts; + for (auto connection : map->connections) { + directionCounts[connection->direction()]++; + } + QString defaultDirection; + int minCount = INT_MAX; + for (auto direction : MapConnection::cardinalDirections) { + if (directionCounts[direction] < minCount) { + defaultDirection = direction; + minCount = directionCounts[direction]; + } + } + ui->comboBox_Direction->setTextItem(defaultDirection); + + // Choose default map + QString defaultMapName; + if (mapNames.isEmpty()) { + defaultMapName = QString(); + } else if (mapNames.first() == map->name && mapNames.length() > 1) { + // Prefer not to connect the map to itself + defaultMapName = mapNames.at(1); + } else { + defaultMapName = mapNames.first(); + } + ui->comboBox_Map->setTextItem(defaultMapName); +} + +NewMapConnectionDialog::~NewMapConnectionDialog() +{ + delete ui; +} + +void NewMapConnectionDialog::accept() { + QString direction = ui->comboBox_Direction->currentText(); + QString hostMapName = this->map ? this->map->name : QString(); + QString targetMapName = ui->comboBox_Map->currentText(); + + this->result = new MapConnection(direction, this->map->name, targetMapName); + + QDialog::accept(); +}