diff --git a/source/glest_map_editor/main.cpp b/source/glest_map_editor/main.cpp index 0e7dd647..ac64f286 100644 --- a/source/glest_map_editor/main.cpp +++ b/source/glest_map_editor/main.cpp @@ -18,6 +18,7 @@ #include #include"platform_util.h" #include +#include "wx/image.h" #ifndef WIN32 #include #endif @@ -181,6 +182,7 @@ void MainWindow::init(string fname) { // --------------------------------------------------------- menuEdit->Append(miEditRandomizeHeights, wxT("Randomize &Heights")); + menuEdit->Append(miEditImportHeights, wxT("Import Heights from Image")); menuEdit->Append(miEditRandomize, wxT("Randomi&ze Players")); menuEdit->Append(miEditSwitchSurfaces, wxT("Switch Sur&faces...")); menuEdit->Append(miEditInfo, wxT("&Info...")); @@ -1067,6 +1069,52 @@ void MainWindow::onMenuEditRandomizeHeights(wxCommandEvent &event) { } } +void MainWindow::onMenuEditImportHeights(wxCommandEvent &event) { + if(program == NULL) { + return; + } + try { + fileDialog->SetMessage(wxT("Select heigthmap image to load")); + fileDialog->SetWildcard(wxT("All Images|*.bmp;*.png;*.jpg;*.jpeg;*.gif;.*.tga;*.tiff;*.tif|PNG-Image (*.png)|*.png|JPEG-Image (*.jpg, *.jpeg)|*.jpg;*.jpeg|BMP-Image (*.bmp)|*.bmp|GIF-Image (*.gif)|*.gif|TIFF-Image (*.tif, *.tiff)|*.tiff;*.tif")); + if (fileDialog->ShowModal() == wxID_OK) { +#ifdef WIN32 + const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(wxFNCONV(fileDialog->GetPath())); + currentFile = tmp_buf; + + auto_ptr wstr(Ansi2WideString(currentFile.c_str())); + currentFile = utf8_encode(wstr.get()); +#elif wxCHECK_VERSION(2, 9, 1) + currentFile = fileDialog->GetPath().ToStdString(); +#else + const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(fileDialog->GetPath()); + currentFile = tmp_buf; +#endif + + wxImage* img=new wxImage(currentFile); + if(img != NULL) { + wxImage grey_img = img->ConvertToGreyscale(); + delete img; + int w =grey_img.GetWidth(); + int h =grey_img.GetHeight(); + int map_w = program->getMap()->getW(); + int map_h = program->getMap()->getH(); + program->setUndoPoint(ctAll); + if(w != map_w && h != map_h) { + grey_img.Rescale(map_w,map_h,wxIMAGE_QUALITY_HIGH); + } + program->importMapHeights(grey_img.GetData()); + setDirty(); + } + } + } + catch (const string &e) { + MsgDialog(this, ToUnicode(e), wxT("Exception"), wxOK | wxICON_ERROR).ShowModal(); + } + catch (const exception &e) { + MsgDialog(this, ToUnicode(e.what()), wxT("Exception"), wxOK | wxICON_ERROR).ShowModal(); + } +} + void MainWindow::onMenuEditRandomize(wxCommandEvent &event) { if(program == NULL) { return; @@ -1464,6 +1512,7 @@ BEGIN_EVENT_TABLE(MainWindow, wxFrame) EVT_MENU(miEditRotatecopyCorner, MainWindow::onMenuEditRotatecopyCorner) EVT_MENU(miEditRandomizeHeights, MainWindow::onMenuEditRandomizeHeights) + EVT_MENU(miEditImportHeights, MainWindow::onMenuEditImportHeights) EVT_MENU(miEditRandomize, MainWindow::onMenuEditRandomize) EVT_MENU(miEditSwitchSurfaces, MainWindow::onMenuEditSwitchSurfaces) EVT_MENU(miEditInfo, MainWindow::onMenuEditInfo) diff --git a/source/glest_map_editor/main.h b/source/glest_map_editor/main.h index 639900c1..846e3fa4 100644 --- a/source/glest_map_editor/main.h +++ b/source/glest_map_editor/main.h @@ -137,6 +137,7 @@ private: miEditMirror, miEditRandomizeHeights, + miEditImportHeights, miEditRandomize, miEditSwitchSurfaces, miEditInfo, @@ -254,6 +255,7 @@ public: void onMenuEditRotatecopyCorner(wxCommandEvent &event); // copy top left 1/4 to top right 1/4, rotated void onMenuEditRandomizeHeights(wxCommandEvent &event); + void onMenuEditImportHeights(wxCommandEvent &event); void onMenuEditRandomize(wxCommandEvent &event); void onMenuEditSwitchSurfaces(wxCommandEvent &event); void onMenuEditInfo(wxCommandEvent &event); diff --git a/source/glest_map_editor/program.cpp b/source/glest_map_editor/program.cpp index 68cc63dc..516b3477 100644 --- a/source/glest_map_editor/program.cpp +++ b/source/glest_map_editor/program.cpp @@ -565,6 +565,10 @@ void Program::randomizeMapHeights(bool withReset,int minimumHeight, int maximumH if(map) map->randomizeHeights(withReset, minimumHeight, maximumHeight, chanceDivider, smoothRecursions); } +void Program::importMapHeights(unsigned char* data) { + if(map) map->importMapHeights(data); +} + void Program::randomizeFactions() { if(map) map->randomizeFactions(); } diff --git a/source/glest_map_editor/program.h b/source/glest_map_editor/program.h index f332b419..702c9fe4 100644 --- a/source/glest_map_editor/program.h +++ b/source/glest_map_editor/program.h @@ -151,7 +151,8 @@ public: void shiftUp(); void shiftDown(); - void randomizeMapHeights(bool withReset, int minimumHeight, int maximumHeight, int chanceDivider, int smoothRecursions);; + void randomizeMapHeights(bool withReset, int minimumHeight, int maximumHeight, int chanceDivider, int smoothRecursions); + void importMapHeights(unsigned char* data); void randomizeFactions(); void switchMapSurfaces(int surf1, int surf2); void loadMap(const string &path); diff --git a/source/shared_lib/include/map/map_preview.h b/source/shared_lib/include/map/map_preview.h index 48ac8d62..01b70210 100644 --- a/source/shared_lib/include/map/map_preview.h +++ b/source/shared_lib/include/map/map_preview.h @@ -208,6 +208,7 @@ public: void resize(int w, int h, float alt, MapSurfaceType surf); void resetFactions(int maxFactions); void randomizeHeights(bool withReset,int minimumHeight, int maximumHeight, int chanceDevider, int smoothRecursions); + void importMapHeights(unsigned char* data); void randomizeFactions(); void smoothSurface(bool limitHeights); void switchSurfaces(MapSurfaceType surf1, MapSurfaceType surf2); diff --git a/source/shared_lib/sources/map/map_preview.cpp b/source/shared_lib/sources/map/map_preview.cpp index 285b08e3..d7471241 100644 --- a/source/shared_lib/sources/map/map_preview.cpp +++ b/source/shared_lib/sources/map/map_preview.cpp @@ -710,6 +710,14 @@ void MapPreview::randomizeHeights(bool withReset,int minimumHeight, int maximumH hasChanged = true; } +void MapPreview::importMapHeights(unsigned char* data) { + for (int i = 0; i < w; ++i) { + for (int j = 0; j < h; ++j) { + cells[i][j].height=(data[(i+j*w)*3]*MAX_MAP_CELL_HEIGHT/255)+MIN_MAP_CELL_HEIGHT; + } + } +} + void MapPreview::randomizeFactions() { int slPlaceFactorX = random.randRange(0, 1); int slPlaceFactorY = random.randRange(0, 1) * 2;