MapEditor: Reworked resize function

This commit is contained in:
titison 2021-05-09 12:21:49 +02:00
parent 12bd61c218
commit 98c8cab708
5 changed files with 23 additions and 61 deletions

View File

@ -920,16 +920,12 @@ void MainWindow::onMenuEditResize(wxCommandEvent &event) {
SimpleDialog simpleDialog; SimpleDialog simpleDialog;
simpleDialog.addValue("Width", intToStr(program->getMap()->getW()),"(must be 16,32,64,128,256,512...)"); simpleDialog.addValue("Width", intToStr(program->getMap()->getW()),"(must be 16,32,64,128,256,512...)");
simpleDialog.addValue("Height", intToStr(program->getMap()->getH()),"(must be 16,32,64,128,256,512...)"); simpleDialog.addValue("Height", intToStr(program->getMap()->getH()),"(must be 16,32,64,128,256,512...)");
simpleDialog.addValue("Surface", "1","(surface material for new area around map)"); if (!simpleDialog.show("Resize")) return;
simpleDialog.addValue("Altitude", "10","(surface height for new area around map)");
if (!simpleDialog.show("Resize - expand around, shrink to topleft")) return;
try { try {
program->resize( program->resize(
strToInt(simpleDialog.getValue("Width")), strToInt(simpleDialog.getValue("Width")),
strToInt(simpleDialog.getValue("Height")), strToInt(simpleDialog.getValue("Height")));
strToInt(simpleDialog.getValue("Altitude")),
strToInt(simpleDialog.getValue("Surface")));
} }
catch (const exception &e) { catch (const exception &e) {
MsgDialog(this, ToUnicode(e.what()), wxT("Exception"), wxOK | wxICON_ERROR).ShowModal(); MsgDialog(this, ToUnicode(e.what()), wxT("Exception"), wxOK | wxICON_ERROR).ShowModal();

View File

@ -584,8 +584,8 @@ void Program::reset(int w, int h, int alt, int surf) {
if(map) map->reset(w, h, (float) alt, static_cast<MapSurfaceType>(surf)); if(map) map->reset(w, h, (float) alt, static_cast<MapSurfaceType>(surf));
} }
void Program::resize(int w, int h, int alt, int surf) { void Program::resize(int w, int h) {
if(map) map->resize(w, h, (float) alt, static_cast<MapSurfaceType>(surf)); if(map) map->resize(w, h);
} }
void Program::resetFactions(int maxFactions) { void Program::resetFactions(int maxFactions) {

View File

@ -134,7 +134,7 @@ public:
//map ops //map ops
void reset(int w, int h, int alt, int surf); void reset(int w, int h, int alt, int surf);
void resize(int w, int h, int alt, int surf); void resize(int w, int h);
void resetFactions(int maxFactions); void resetFactions(int maxFactions);
void setRefAlt(int x, int y); void setRefAlt(int x, int y);
void flipX(); void flipX();

View File

@ -206,7 +206,7 @@ public:
void copyXY(int x, int y, int sx, int sy); // destination x,y = source sx,sy void copyXY(int x, int y, int sx, int sy); // destination x,y = source sx,sy
void swapXY(int x, int y, int sx, int sy); void swapXY(int x, int y, int sx, int sy);
void reset(int w, int h, float alt, MapSurfaceType surf); void reset(int w, int h, float alt, MapSurfaceType surf);
void resize(int w, int h, float alt, MapSurfaceType surf); void resize(int w, int h);
void resetFactions(int maxFactions); void resetFactions(int maxFactions);
void randomizeHeights(bool withReset,int minimumHeight, int maximumHeight, int chanceDevider, int smoothRecursions); void randomizeHeights(bool withReset,int minimumHeight, int maximumHeight, int chanceDevider, int smoothRecursions);
void importMapHeights(unsigned char* data); void importMapHeights(unsigned char* data);

View File

@ -579,7 +579,7 @@ void MapPreview::reset(int w, int h, float alt, MapSurfaceType surf) {
hasChanged = true; hasChanged = true;
} }
void MapPreview::resize(int w, int h, float alt, MapSurfaceType surf) { void MapPreview::resize(int w, int h) {
if (w < MIN_MAP_CELL_DIMENSION || h < MIN_MAP_CELL_DIMENSION) { if (w < MIN_MAP_CELL_DIMENSION || h < MIN_MAP_CELL_DIMENSION) {
char szBuf[8096]=""; char szBuf[8096]="";
snprintf(szBuf,8096,"Size of map must be at least %dx%d",MIN_MAP_CELL_DIMENSION,MIN_MAP_CELL_DIMENSION); snprintf(szBuf,8096,"Size of map must be at least %dx%d",MIN_MAP_CELL_DIMENSION,MIN_MAP_CELL_DIMENSION);
@ -593,18 +593,6 @@ void MapPreview::resize(int w, int h, float alt, MapSurfaceType surf) {
throw megaglest_runtime_error(szBuf); throw megaglest_runtime_error(szBuf);
} }
if (alt < MIN_MAP_CELL_HEIGHT || alt > MAX_MAP_CELL_HEIGHT) {
char szBuf[8096]="";
snprintf(szBuf,8096,"Height must be in the range %d-%d",MIN_MAP_CELL_HEIGHT,MAX_MAP_CELL_HEIGHT);
throw megaglest_runtime_error(szBuf);
}
if (surf < st_Grass || surf > st_Ground) {
char szBuf[8096]="";
snprintf(szBuf,8096,"Surface must be in the range %d-%d",st_Grass,st_Ground);
throw megaglest_runtime_error(szBuf);
}
int oldW = this->w; int oldW = this->w;
int oldH = this->h; int oldH = this->h;
this->w = w; this->w = w;
@ -612,46 +600,24 @@ void MapPreview::resize(int w, int h, float alt, MapSurfaceType surf) {
//this->maxFactions = maxFactions; //this->maxFactions = maxFactions;
//create new cells //create new cells
//Cell **oldCells = cells;
std::vector<std::vector<Cell> > oldCells = cells; std::vector<std::vector<Cell> > oldCells = cells;
//cells = new Cell*[w]; cells.resize(w);
cells.resize(w); for (int i = 0; i < w; i++) {
for (int i = 0; i < w; i++) { cells[i].resize(h);
//cells[i] = new Cell[h]; for (int j = 0; j < h; j++) {
cells[i].resize(h); int from_x = (i*oldW)/w;
for (int j = 0; j < h; j++) { int from_y = (j*oldH)/h;
cells[i][j].height = alt; cells[i][j].height = oldCells[from_x][from_y].height;
cells[i][j].object = 0; cells[i][j].object = oldCells[from_x][from_y].object;
cells[i][j].resource = 0; cells[i][j].resource = oldCells[from_x][from_y].resource;
cells[i][j].surface = surf; cells[i][j].surface = oldCells[from_x][from_y].surface;
} }
} }
for (int i = 0; i < maxFactions; ++i) {
int wOffset = w < oldW ? 0 : (w - oldW) / 2; startLocations[i].x *= (float)w/oldW;
int hOffset = h < oldH ? 0 : (h - oldH) / 2; startLocations[i].y *= (float)h/oldH;
//assign old values to cells }
for (int i = 0; i < oldW; i++) {
for (int j = 0; j < oldH; j++) {
if (i + wOffset < w && j + hOffset < h) {
cells[i+wOffset][j+hOffset].height = oldCells[i][j].height;
cells[i+wOffset][j+hOffset].object = oldCells[i][j].object;
cells[i+wOffset][j+hOffset].resource = oldCells[i][j].resource;
cells[i+wOffset][j+hOffset].surface = oldCells[i][j].surface;
}
}
}
for (int i = 0; i < maxFactions; ++i) {
startLocations[i].x += wOffset;
startLocations[i].y += hOffset;
}
//delete old cells
//if (oldCells != NULL) {
// for (int i = 0; i < oldW; i++)
// delete [] oldCells[i];
// delete [] oldCells;
//}
hasChanged = true; hasChanged = true;
} }