Skip to content

Commit

Permalink
Maps: Add confirmation on map shrink and move map resize to properties
Browse files Browse the repository at this point in the history
A confirmation message is displayed now if the user is about to shrink
the width and/or height of the map because this destroys map data.
Moreover the map resize has been moved from map_scene to
map_properties.
  • Loading branch information
rueter37 committed Nov 30, 2021
1 parent 399dddb commit d3c9871
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 44 deletions.
39 changes: 0 additions & 39 deletions src/ui/map/map_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,48 +242,9 @@ void MapScene::editMapProperties()

MapPropertiesDialog dlg(m_project, n_mapInfo, *m_map, m_view);
if (dlg.exec() == QDialog::Accepted) {
// Resize map
if (m_map->width != old_width || m_map->height != old_height) {
auto old_lower_layer = m_map->lower_layer;
auto old_upper_layer = m_map->upper_layer;
int old_tile_counter = 0;

m_map->lower_layer.clear();
m_map->upper_layer.clear();
for (int y = 0; y < m_map->height; y++) {
if (y < old_height) {
for (int x = 0; x < m_map->width; x++) {
if (x < old_width) {
m_map->lower_layer.push_back(old_lower_layer[old_tile_counter]);
m_map->upper_layer.push_back(old_upper_layer[old_tile_counter]);
old_tile_counter++;
} else {
m_map->lower_layer.push_back(0);
m_map->upper_layer.push_back(10000);
}
}
if (m_map->width < old_width) {
old_tile_counter += (old_width - m_map->width);
}
} else {
for (int x = 0; x < m_map->width; x++) {
m_map->lower_layer.push_back(0);
m_map->upper_layer.push_back(10000);
}
}
}
setLayerData(Core::LOWER, m_map->lower_layer);
setLayerData(Core::UPPER, m_map->upper_layer);

// Delete out of bounds events
if (m_map->width < old_width || m_map->height < old_height) {
std::vector<lcf::rpg::Event>::iterator ev;
for (ev = m_map->events.begin(); ev != m_map->events.end(); ++ev) {
if (ev->x >= m_map->width || ev->y >= m_map->height) {
m_map->events.erase(ev);
}
}
}
}

Save(true);
Expand Down
70 changes: 66 additions & 4 deletions src/ui/maptree/map_properties_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "ui/picker/picker_backdrop_widget.h"
#include "ui/picker/picker_panorama_widget.h"
#include "ui/picker/picker_dialog.h"
#include <QMessageBox>

MapPropertiesDialog::MapPropertiesDialog(ProjectData& project, lcf::rpg::MapInfo &info, lcf::rpg::Map &map, QWidget *parent) :
QDialog(parent),
Expand All @@ -32,7 +33,6 @@ MapPropertiesDialog::MapPropertiesDialog(ProjectData& project, lcf::rpg::MapInfo
m_project(project)
{
ui->setupUi(this);
connect(ui->buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(ok()));

auto& database = project.database();

Expand Down Expand Up @@ -214,6 +214,9 @@ MapPropertiesDialog::MapPropertiesDialog(ProjectData& project, lcf::rpg::MapInfo

new_panorama = map.parallax_name;
new_music = info.music;

old_width = map.width;
old_height = map.height;
}

MapPropertiesDialog::~MapPropertiesDialog()
Expand All @@ -232,11 +235,24 @@ MapPropertiesDialog::~MapPropertiesDialog()
delete ui;
}

void MapPropertiesDialog::ok() {
void MapPropertiesDialog::accept() {
int width = ui->spinWidth->value();
int height = ui->spinHeight->value();
if (width < old_width || height < old_height) {
int result = QMessageBox::question(this,
"Shrink map",
QString("You are about to shrink the current map. All out of bounds map data and events will be deleted. This cannot be undone. Do you want to continue?"),
QMessageBox::Yes | QMessageBox::No);

if (result != QMessageBox::Yes) {
return;
}
}

m_info.name = ToDBString(ui->lineName->text());
m_map.chipset_id = ui->comboTileset->currentIndex() + 1;
m_map.width = ui->spinWidth->value();
m_map.height = ui->spinHeight->value();
m_map.width = width;
m_map.height = height;
m_map.scroll_type = ui->comboWrapping->currentIndex();
if (ui->groupPanorama->isChecked()) {
m_map.parallax_flag = true;
Expand Down Expand Up @@ -295,6 +311,52 @@ void MapPropertiesDialog::ok() {
} else {
m_info.save = 2;
}

// Resize map if map bounds have been changed
if (width != old_width || height != old_height) {
auto old_lower_layer = m_map.lower_layer;
auto old_upper_layer = m_map.upper_layer;
int old_tile_counter = 0;

m_map.lower_layer.clear();
m_map.upper_layer.clear();
for (int y = 0; y < height; y++) {
if (y < old_height) {
for (int x = 0; x < width; x++) {
if (x < old_width) {
m_map.lower_layer.push_back(old_lower_layer[old_tile_counter]);
m_map.upper_layer.push_back(old_upper_layer[old_tile_counter]);
old_tile_counter++;
} else {
m_map.lower_layer.push_back(0);
m_map.upper_layer.push_back(10000);
}
}
if (width < old_width) {
old_tile_counter += (old_width - width);
}
} else {
for (int x = 0; x < width; x++) {
m_map.lower_layer.push_back(0);
m_map.upper_layer.push_back(10000);
}
}
}

// Delete out of bounds events
if (width < old_width || height < old_height) {
std::vector<lcf::rpg::Event>::iterator ev = m_map.events.begin();
while (ev != m_map.events.end()) {
if (ev->x >= width || ev->y >= height) {
ev = m_map.events.erase(ev);
} else {
++ev;
}
}
}
}

QDialog::accept();
}

void MapPropertiesDialog::on_groupPanorama_toggled(bool arg1)
Expand Down
5 changes: 4 additions & 1 deletion src/ui/maptree/map_properties_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class MapPropertiesDialog : public QDialog
~MapPropertiesDialog();

private slots:
void ok();
void accept();

void on_groupPanorama_toggled(bool arg1);

Expand Down Expand Up @@ -89,5 +89,8 @@ private slots:

lcf::DBString new_panorama;
lcf::rpg::Music new_music;

int old_width;
int old_height;
};

0 comments on commit d3c9871

Please sign in to comment.