Browse Source

Add map resizing functionality

Marcus Huderle 5 years ago
parent
commit
514ecb2907
3 changed files with 61 additions and 6 deletions
  1. 36
    6
      mainwindow.cpp
  2. 24
    0
      map.cpp
  3. 1
    0
      map.h

+ 36
- 6
mainwindow.cpp View File

@@ -851,21 +851,51 @@ void MainWindow::on_pushButton_clicked()
851 851
 
852 852
     QSpinBox *widthSpinBox = new QSpinBox();
853 853
     QSpinBox *heightSpinBox = new QSpinBox();
854
-    widthSpinBox->setValue(editor->map->getWidth());
855
-    heightSpinBox->setValue(editor->map->getHeight());
856 854
     widthSpinBox->setMinimum(1);
857 855
     heightSpinBox->setMinimum(1);
858
-    widthSpinBox->setMaximum(255);
859
-    heightSpinBox->setMaximum(255);
856
+    // See below for explanation of maximum map dimensions
857
+    widthSpinBox->setMaximum(0x1E7);
858
+    heightSpinBox->setMaximum(0x1D1);
859
+    widthSpinBox->setValue(editor->map->getWidth());
860
+    heightSpinBox->setValue(editor->map->getHeight());
860 861
     form.addRow(new QLabel("Width"), widthSpinBox);
861 862
     form.addRow(new QLabel("Height"), heightSpinBox);
862 863
 
864
+    QLabel *errorLabel = new QLabel();
865
+    QPalette errorPalette;
866
+    errorPalette.setColor(QPalette::WindowText, Qt::red);
867
+    errorLabel->setPalette(errorPalette);
868
+    errorLabel->setVisible(false);
869
+
863 870
     QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &dialog);
864 871
     form.addRow(&buttonBox);
865
-    connect(&buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
872
+    connect(&buttonBox, &QDialogButtonBox::accepted, [&dialog, &widthSpinBox, &heightSpinBox, &errorLabel](){
873
+        // Ensure width and height are an acceptable size.
874
+        // The maximum number of metatiles in a map is the following:
875
+        //    max = (width + 15) * (height + 14)
876
+        // This limit can be found in fieldmap.c in pokeruby/pokeemerald.
877
+        int realWidth = widthSpinBox->value() + 15;
878
+        int realHeight = heightSpinBox->value() + 14;
879
+        int numMetatiles = realWidth * realHeight;
880
+        if (numMetatiles <= 0x2800) {
881
+            dialog.accept();
882
+        } else {
883
+            QString errorText = QString("Error: The specified width and height are too large.\n"
884
+                    "The maximum width and height is the following: (width + 15) * (height + 14) <= 10240\n"
885
+                    "The specified width and height was: (%1 + 15) * (%2 + 14) = %3")
886
+                        .arg(widthSpinBox->value())
887
+                        .arg(heightSpinBox->value())
888
+                        .arg(numMetatiles);
889
+            errorLabel->setText(errorText);
890
+            errorLabel->setVisible(true);
891
+        }
892
+    });
866 893
     connect(&buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));
867 894
 
895
+    form.addRow(errorLabel);
896
+
868 897
     if (dialog.exec() == QDialog::Accepted) {
869
-        qDebug() << "Change width";
898
+        editor->map->setDimensions(widthSpinBox->value(), heightSpinBox->value());
899
+        setMap(editor->map->name);
870 900
     }
871 901
 }

+ 24
- 0
map.cpp View File

@@ -427,6 +427,30 @@ QPixmap Map::renderMetatiles() {
427 427
     return QPixmap::fromImage(image);
428 428
 }
429 429
 
430
+void Map::setDimensions(int newWidth, int newHeight) {
431
+    int oldWidth = getWidth();
432
+    int oldHeight = getHeight();
433
+
434
+    Blockdata* newBlockData = new Blockdata;
435
+
436
+    for (int y = 0; y < newHeight; y++)
437
+    for (int x = 0; x < newWidth; x++) {
438
+        if (x < oldWidth && y < oldHeight) {
439
+            int index = y * oldWidth + x;
440
+            newBlockData->addBlock(layout->blockdata->blocks->value(index));
441
+        } else {
442
+            newBlockData->addBlock(0);
443
+        }
444
+    }
445
+
446
+    layout->blockdata->copyFrom(newBlockData);
447
+    layout->width = QString::number(newWidth);
448
+    layout->height = QString::number(newHeight);
449
+    commit();
450
+
451
+    emit mapChanged(this);
452
+}
453
+
430 454
 Block* Map::getBlock(int x, int y) {
431 455
     if (layout->blockdata && layout->blockdata->blocks) {
432 456
         if (x >= 0 && x < getWidth())

+ 1
- 0
map.h View File

@@ -194,6 +194,7 @@ public:
194 194
 
195 195
     QList<Connection*> connections;
196 196
     QPixmap renderConnection(Connection);
197
+    void setDimensions(int, int);
197 198
 
198 199
     QPixmap renderBorder();
199 200
     void cacheBorder();