Add map resizing functionality
This commit is contained in:
parent
eb0d238a01
commit
514ecb2907
3 changed files with 61 additions and 6 deletions
|
@ -851,21 +851,51 @@ void MainWindow::on_pushButton_clicked()
|
||||||
|
|
||||||
QSpinBox *widthSpinBox = new QSpinBox();
|
QSpinBox *widthSpinBox = new QSpinBox();
|
||||||
QSpinBox *heightSpinBox = new QSpinBox();
|
QSpinBox *heightSpinBox = new QSpinBox();
|
||||||
widthSpinBox->setValue(editor->map->getWidth());
|
|
||||||
heightSpinBox->setValue(editor->map->getHeight());
|
|
||||||
widthSpinBox->setMinimum(1);
|
widthSpinBox->setMinimum(1);
|
||||||
heightSpinBox->setMinimum(1);
|
heightSpinBox->setMinimum(1);
|
||||||
widthSpinBox->setMaximum(255);
|
// See below for explanation of maximum map dimensions
|
||||||
heightSpinBox->setMaximum(255);
|
widthSpinBox->setMaximum(0x1E7);
|
||||||
|
heightSpinBox->setMaximum(0x1D1);
|
||||||
|
widthSpinBox->setValue(editor->map->getWidth());
|
||||||
|
heightSpinBox->setValue(editor->map->getHeight());
|
||||||
form.addRow(new QLabel("Width"), widthSpinBox);
|
form.addRow(new QLabel("Width"), widthSpinBox);
|
||||||
form.addRow(new QLabel("Height"), heightSpinBox);
|
form.addRow(new QLabel("Height"), heightSpinBox);
|
||||||
|
|
||||||
|
QLabel *errorLabel = new QLabel();
|
||||||
|
QPalette errorPalette;
|
||||||
|
errorPalette.setColor(QPalette::WindowText, Qt::red);
|
||||||
|
errorLabel->setPalette(errorPalette);
|
||||||
|
errorLabel->setVisible(false);
|
||||||
|
|
||||||
QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &dialog);
|
QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &dialog);
|
||||||
form.addRow(&buttonBox);
|
form.addRow(&buttonBox);
|
||||||
connect(&buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
|
connect(&buttonBox, &QDialogButtonBox::accepted, [&dialog, &widthSpinBox, &heightSpinBox, &errorLabel](){
|
||||||
|
// Ensure width and height are an acceptable size.
|
||||||
|
// The maximum number of metatiles in a map is the following:
|
||||||
|
// max = (width + 15) * (height + 14)
|
||||||
|
// This limit can be found in fieldmap.c in pokeruby/pokeemerald.
|
||||||
|
int realWidth = widthSpinBox->value() + 15;
|
||||||
|
int realHeight = heightSpinBox->value() + 14;
|
||||||
|
int numMetatiles = realWidth * realHeight;
|
||||||
|
if (numMetatiles <= 0x2800) {
|
||||||
|
dialog.accept();
|
||||||
|
} else {
|
||||||
|
QString errorText = QString("Error: The specified width and height are too large.\n"
|
||||||
|
"The maximum width and height is the following: (width + 15) * (height + 14) <= 10240\n"
|
||||||
|
"The specified width and height was: (%1 + 15) * (%2 + 14) = %3")
|
||||||
|
.arg(widthSpinBox->value())
|
||||||
|
.arg(heightSpinBox->value())
|
||||||
|
.arg(numMetatiles);
|
||||||
|
errorLabel->setText(errorText);
|
||||||
|
errorLabel->setVisible(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
connect(&buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));
|
connect(&buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));
|
||||||
|
|
||||||
|
form.addRow(errorLabel);
|
||||||
|
|
||||||
if (dialog.exec() == QDialog::Accepted) {
|
if (dialog.exec() == QDialog::Accepted) {
|
||||||
qDebug() << "Change width";
|
editor->map->setDimensions(widthSpinBox->value(), heightSpinBox->value());
|
||||||
|
setMap(editor->map->name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
24
map.cpp
24
map.cpp
|
@ -427,6 +427,30 @@ QPixmap Map::renderMetatiles() {
|
||||||
return QPixmap::fromImage(image);
|
return QPixmap::fromImage(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Map::setDimensions(int newWidth, int newHeight) {
|
||||||
|
int oldWidth = getWidth();
|
||||||
|
int oldHeight = getHeight();
|
||||||
|
|
||||||
|
Blockdata* newBlockData = new Blockdata;
|
||||||
|
|
||||||
|
for (int y = 0; y < newHeight; y++)
|
||||||
|
for (int x = 0; x < newWidth; x++) {
|
||||||
|
if (x < oldWidth && y < oldHeight) {
|
||||||
|
int index = y * oldWidth + x;
|
||||||
|
newBlockData->addBlock(layout->blockdata->blocks->value(index));
|
||||||
|
} else {
|
||||||
|
newBlockData->addBlock(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
layout->blockdata->copyFrom(newBlockData);
|
||||||
|
layout->width = QString::number(newWidth);
|
||||||
|
layout->height = QString::number(newHeight);
|
||||||
|
commit();
|
||||||
|
|
||||||
|
emit mapChanged(this);
|
||||||
|
}
|
||||||
|
|
||||||
Block* Map::getBlock(int x, int y) {
|
Block* Map::getBlock(int x, int y) {
|
||||||
if (layout->blockdata && layout->blockdata->blocks) {
|
if (layout->blockdata && layout->blockdata->blocks) {
|
||||||
if (x >= 0 && x < getWidth())
|
if (x >= 0 && x < getWidth())
|
||||||
|
|
1
map.h
1
map.h
|
@ -194,6 +194,7 @@ public:
|
||||||
|
|
||||||
QList<Connection*> connections;
|
QList<Connection*> connections;
|
||||||
QPixmap renderConnection(Connection);
|
QPixmap renderConnection(Connection);
|
||||||
|
void setDimensions(int, int);
|
||||||
|
|
||||||
QPixmap renderBorder();
|
QPixmap renderBorder();
|
||||||
void cacheBorder();
|
void cacheBorder();
|
||||||
|
|
Loading…
Reference in a new issue