diff --git a/include/mainwindow.h b/include/mainwindow.h index 010563fc..2de8955b 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -34,6 +34,7 @@ #include "mapheaderform.h" #include "newlayoutdialog.h" #include "message.h" +#include "resizelayoutpopup.h" @@ -253,8 +254,8 @@ private slots: void on_actionTileset_Editor_triggered(); - void moveEvent(QMoveEvent *event); - void closeEvent(QCloseEvent *); + void moveEvent(QMoveEvent *event) override; + void closeEvent(QCloseEvent *) override; void eventTabChanged(int index); @@ -326,6 +327,7 @@ private: QPointer wildMonChart = nullptr; QPointer wildMonSearch = nullptr; QPointer fileWatcherWarning = nullptr; + QPointer resizeLayoutPopup = nullptr; QAction *undoAction = nullptr; QAction *redoAction = nullptr; diff --git a/include/ui/resizelayoutpopup.h b/include/ui/resizelayoutpopup.h index 1f79580e..5092d8b9 100644 --- a/include/ui/resizelayoutpopup.h +++ b/include/ui/resizelayoutpopup.h @@ -9,11 +9,13 @@ #include class ResizableRect; -class Editor; namespace Ui { class ResizeLayoutPopup; } +class Layout; +class Project; + /// Custom scene that paints its background a gray checkered pattern. @@ -68,7 +70,7 @@ class ResizeLayoutPopup : public QDialog Q_OBJECT public: - ResizeLayoutPopup(QWidget *parent, Editor *editor); + ResizeLayoutPopup(QWidget *parent, Layout *layout, Project *project); ~ResizeLayoutPopup(); void setupLayoutView(); @@ -96,7 +98,8 @@ private slots: private: QWidget *parent = nullptr; - Editor *editor = nullptr; + QPointer layout = nullptr; + QPointer project = nullptr; Ui::ResizeLayoutPopup *ui; diff --git a/src/editor.cpp b/src/editor.cpp index d0ebe59e..620c625a 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -1755,7 +1755,11 @@ void Editor::displayMapEvents() { events_group = new QGraphicsItemGroup; scene->addItem(events_group); - for (const auto &event : map->getEvents()) { + const auto events = map->getEvents(); + if (!events.isEmpty()) { + this->selectedEvents.append(events.first()); + } + for (const auto &event : events) { addEventPixmapItem(event); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index f798101d..3510ee6d 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1215,6 +1215,8 @@ bool MainWindow::setLayout(const QString &layoutId) { return true; } +// TODO: This is being used to do more work than necessary (e.g. when a layout is resized, +// we don't need to be recreating the metatile selector, all the events, etc.) void MainWindow::redrawMapScene() { editor->displayLayout(); editor->displayMap(); @@ -2623,8 +2625,12 @@ void MainWindow::onMapRulerStatusChanged(const QString &status) { void MainWindow::moveEvent(QMoveEvent *event) { QMainWindow::moveEvent(event); - if (label_MapRulerStatus && label_MapRulerStatus->isVisible() && label_MapRulerStatus->parentWidget()) + if (label_MapRulerStatus && label_MapRulerStatus->isVisible() && label_MapRulerStatus->parentWidget()) { label_MapRulerStatus->move(label_MapRulerStatus->parentWidget()->mapToGlobal(QPoint(6, 6))); + } + if (this->resizeLayoutPopup) { + this->resizeLayoutPopup->resetPosition(); + } } void MainWindow::on_action_Export_Map_Image_triggered() { @@ -2768,17 +2774,15 @@ void MainWindow::on_comboBox_SecondaryTileset_currentTextChanged(const QString & } void MainWindow::on_pushButton_ChangeDimensions_clicked() { - if (!editor || !editor->layout) return; + if (this->resizeLayoutPopup || !this->editor->layout || !this->editor->project) return; - ResizeLayoutPopup popup(this->ui->graphicsView_Map, this->editor); - popup.show(); - popup.setupLayoutView(); - if (popup.exec() == QDialog::Accepted) { + this->resizeLayoutPopup = new ResizeLayoutPopup(this->ui->graphicsView_Map, this->editor->layout, this->editor->project); + this->resizeLayoutPopup->show(); + this->resizeLayoutPopup->setupLayoutView(); + if (this->resizeLayoutPopup->exec() == QDialog::Accepted) { Layout *layout = this->editor->layout; - Map *map = this->editor->map; - - QMargins result = popup.getResult(); - QSize borderResult = popup.getBorderResult(); + QMargins result = this->resizeLayoutPopup->getResult(); + QSize borderResult = this->resizeLayoutPopup->getBorderResult(); QSize oldLayoutDimensions(layout->getWidth(), layout->getHeight()); QSize oldBorderDimensions(layout->getBorderWidth(), layout->getBorderHeight()); if (!result.isNull() || (borderResult != oldBorderDimensions)) { @@ -2795,6 +2799,7 @@ void MainWindow::on_pushButton_ChangeDimensions_clicked() { )); } // If we're in map-editing mode, adjust the events' position by the same amount. + Map *map = this->editor->map; if (map) { auto events = map->getEvents(); int deltaX = result.left(); @@ -2804,6 +2809,7 @@ void MainWindow::on_pushButton_ChangeDimensions_clicked() { } } } + this->resizeLayoutPopup->deleteLater(); } void MainWindow::setSmartPathsEnabled(bool enabled) diff --git a/src/ui/resizelayoutpopup.cpp b/src/ui/resizelayoutpopup.cpp index 2dc78d47..399923be 100644 --- a/src/ui/resizelayoutpopup.cpp +++ b/src/ui/resizelayoutpopup.cpp @@ -3,6 +3,7 @@ #include "movablerect.h" #include "config.h" #include "utility.h" +#include "message.h" #include "ui_resizelayoutpopup.h" @@ -70,10 +71,11 @@ QVariant BoundedPixmapItem::itemChange(GraphicsItemChange change, const QVariant ************************************************************************ ******************************************************************************/ -ResizeLayoutPopup::ResizeLayoutPopup(QWidget *parent, Editor *editor) : +ResizeLayoutPopup::ResizeLayoutPopup(QWidget *parent, Layout *layout, Project *project) : QDialog(parent), parent(parent), - editor(editor), + layout(layout), + project(project), ui(new Ui::ResizeLayoutPopup) { ui->setupUi(this); @@ -107,11 +109,10 @@ void ResizeLayoutPopup::on_buttonBox_clicked(QAbstractButton *button) { /// (1) pixmap representing the current layout / not resizable / drag-movable /// (1) layout outline / resizable / not movable void ResizeLayoutPopup::setupLayoutView() { - if (!this->editor || !this->editor->layout) return; + if (!this->layout || !this->project) return; // Border stuff - bool bordersEnabled = projectConfig.useCustomBorderSize; - if (bordersEnabled) { + if (projectConfig.useCustomBorderSize) { this->ui->spinBox_borderWidth->setMinimum(1); this->ui->spinBox_borderHeight->setMinimum(1); this->ui->spinBox_borderWidth->setMaximum(MAX_BORDER_WIDTH); @@ -119,15 +120,14 @@ void ResizeLayoutPopup::setupLayoutView() { } else { this->ui->frame_border->setVisible(false); } - this->ui->spinBox_borderWidth->setValue(this->editor->layout->getBorderWidth()); - this->ui->spinBox_borderHeight->setValue(this->editor->layout->getBorderHeight()); + this->ui->spinBox_borderWidth->setValue(this->layout->getBorderWidth()); + this->ui->spinBox_borderHeight->setValue(this->layout->getBorderHeight()); // Layout stuff - QPixmap pixmap = this->editor->layout->pixmap; - this->layoutPixmap = new BoundedPixmapItem(pixmap); + this->layoutPixmap = new BoundedPixmapItem(this->layout->pixmap); this->scene->addItem(layoutPixmap); - int maxWidth = this->editor->project->getMaxMapWidth(); - int maxHeight = this->editor->project->getMaxMapHeight(); + int maxWidth = this->project->getMaxMapWidth(); + int maxHeight = this->project->getMaxMapHeight(); QGraphicsRectItem *cover = new QGraphicsRectItem(-maxWidth * 8, -maxHeight * 8, maxWidth * 16, maxHeight * 16); this->scene->addItem(cover); @@ -138,33 +138,25 @@ void ResizeLayoutPopup::setupLayoutView() { static bool layoutSizeRectVisible = true; - this->outline = new ResizableRect(this, &layoutSizeRectVisible, this->editor->layout->getWidth(), this->editor->layout->getHeight(), qRgb(255, 0, 255)); + this->outline = new ResizableRect(this, &layoutSizeRectVisible, this->layout->getWidth(), this->layout->getHeight(), qRgb(255, 0, 255)); this->outline->setZValue(Editor::ZValue::ResizeLayoutPopup); // Ensure on top of view this->outline->setLimit(cover->rect().toAlignedRect()); connect(outline, &ResizableRect::rectUpdated, [=](QRect rect){ // Note: this extra limit check needs access to the project values, so it is done here and not ResizableRect::mouseMoveEvent - // Upper limits: maximum metatiles in a map formula: - // max = (width + 15) * (height + 14) - // This limit can be found in fieldmap.c in pokeruby/pokeemerald/pokefirered. - int size = editor->project->getMapDataSize(rect.width() / 16, rect.height() / 16); - int maxSize = editor->project->getMaxMapDataSize(); + int size = this->project->getMapDataSize(rect.width() / 16, rect.height() / 16); + int maxSize = this->project->getMaxMapDataSize(); if (size > maxSize) { - QSize addition = editor->project->getMapSizeAddition(); - QString errorText = QString("The maximum layout width and height is the following: (width + %1) * (height + %2) <= %3\n" - "The specified layout width and height was: (%4 + %1) * (%5 + %2) = %6") - .arg(addition.width()) - .arg(addition.height()) - .arg(maxSize) - .arg(rect.width() / 16) - .arg(rect.height() / 16) - .arg(size); - QMessageBox warning; - warning.setIcon(QMessageBox::Warning); - warning.setText("The specified width and height are too large."); - warning.setInformativeText(errorText); - warning.setStandardButtons(QMessageBox::Ok); - warning.setDefaultButton(QMessageBox::Ok); - warning.exec(); + QSize addition = this->project->getMapSizeAddition(); + WarningMessage::show(QStringLiteral("The specified width and height are too large."), + QString("The maximum layout width and height is the following: (width + %1) * (height + %2) <= %3\n" + "The specified layout width and height was: (%4 + %1) * (%5 + %2) = %6") + .arg(addition.width()) + .arg(addition.height()) + .arg(maxSize) + .arg(rect.width() / 16) + .arg(rect.height() / 16) + .arg(size), + this); // adjust rect to last accepted size rect = this->scene->getValidRect(); }