Skip to content

Commit

Permalink
feat-fix: minimap export path and pre-defined image sizes selection (#…
Browse files Browse the repository at this point in the history
…117)

This fixes the export minimap path, it was exporting to the base of the application and adds a feature to select between some pre-defined image sizes to export. Now it will be exporting to the selected folder and given image size.
  • Loading branch information
phacUFPE authored Sep 1, 2024
1 parent f1f256b commit 64a0877
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
25 changes: 23 additions & 2 deletions source/common_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ EVT_CHOICE(wxID_ANY, ExportMiniMapWindow::OnExportTypeChange)
END_EVENT_TABLE()

ExportMiniMapWindow::ExportMiniMapWindow(wxWindow* parent, Editor &editor) :
wxDialog(parent, wxID_ANY, "Export Minimap", wxDefaultPosition, wxSize(400, 300)),
wxDialog(parent, wxID_ANY, "Export Minimap", wxDefaultPosition, wxSize(400, 350)),
editor(editor) {
wxSizer* sizer = newd wxBoxSizer(wxVERTICAL);
wxSizer* tmpsizer;
Expand All @@ -550,6 +550,18 @@ ExportMiniMapWindow::ExportMiniMapWindow(wxWindow* parent, Editor &editor) :
tmpsizer->Add(newd wxButton(this, MAP_WINDOW_FILE_BUTTON, "Browse"), 0, wxALL, 5);
sizer->Add(tmpsizer, 0, wxALL | wxEXPAND, 5);

// Format options
wxArrayString imageFormatChoices;
imageFormatChoices.Add("1024x1024");
imageFormatChoices.Add("2048x2048");
imageFormatChoices.Add("4096x4096");
imageSizeOptions = new wxChoice(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, imageFormatChoices);
imageSizeOptions->SetSelection(0);
imageSizeOptions->Disable();
tmpsizer = newd wxStaticBoxSizer(wxHORIZONTAL, this, "Image Size");
tmpsizer->Add(imageSizeOptions, 1, wxALL, 5);
sizer->Add(tmpsizer, 0, wxALL | wxEXPAND, 5);

// File name
wxString mapName(editor.getMap().getName().c_str(), wxConvUTF8);
file_name_text_field = newd wxTextCtrl(this, wxID_ANY, mapName.BeforeLast('.'), wxDefaultPosition, wxDefaultSize);
Expand Down Expand Up @@ -602,6 +614,7 @@ ExportMiniMapWindow::ExportMiniMapWindow(wxWindow* parent, Editor &editor) :
ExportMiniMapWindow::~ExportMiniMapWindow() = default;

void ExportMiniMapWindow::OnExportTypeChange(wxCommandEvent &event) {
imageSizeOptions->Enable(event.GetSelection() > 0);
floor_number->Enable(event.GetSelection() == 2);
}

Expand All @@ -627,6 +640,14 @@ void ExportMiniMapWindow::OnFileNameChanged(wxKeyEvent &event) {
void ExportMiniMapWindow::OnClickOK(wxCommandEvent &WXUNUSED(event)) {
g_gui.CreateLoadBar("Exporting minimap...");

const auto imageSizeSelection = imageSizeOptions->GetSelection();
auto imageSize = 1024;
if (imageSizeSelection == 1) {
imageSize = 2048;
} else if (imageSizeSelection == 2) {
imageSize = 4096;
}

auto format = static_cast<MinimapExportFormat>(format_options->GetSelection());
auto mode = static_cast<MinimapExportMode>(floor_options->GetSelection());
std::string directory = directory_text_field->GetValue().ToStdString();
Expand All @@ -635,7 +656,7 @@ void ExportMiniMapWindow::OnClickOK(wxCommandEvent &WXUNUSED(event)) {

g_settings.setString(Config::MINIMAP_EXPORT_DIR, directory);

IOMinimap io(&editor, format, mode, true);
IOMinimap io(&editor, format, mode, true, imageSize);
if (!io.saveMinimap(directory, file_name, floor)) {
g_gui.PopupDialog("Error", io.getError(), wxOK);
}
Expand Down
1 change: 1 addition & 0 deletions source/common_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class ExportMiniMapWindow : public wxDialog {

Editor &editor;

wxChoice* imageSizeOptions;
wxChoice* format_options;
wxStaticText* error_field;
wxTextCtrl* directory_text_field;
Expand Down
20 changes: 10 additions & 10 deletions source/iominimap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ void MinimapBlock::updateTile(int x, int y, const MinimapTile &tile) {
m_tiles[getTileIndex(x, y)] = tile;
}

IOMinimap::IOMinimap(Editor* editor, MinimapExportFormat format, MinimapExportMode mode, bool updateLoadbar) :
IOMinimap::IOMinimap(Editor* editor, MinimapExportFormat format, MinimapExportMode mode, bool updateLoadbar, int imageSize /* = 1024 */) :
m_editor(editor),
m_format(format),
m_mode(mode),
m_updateLoadbar(updateLoadbar) {
m_updateLoadbar(updateLoadbar),
m_imageSize(imageSize) {
}

bool IOMinimap::saveMinimap(const std::string &directory, const std::string &name, int floor) {
Expand Down Expand Up @@ -188,10 +189,9 @@ bool IOMinimap::exportMinimap(const std::string &directory) {
totalTiles++;
}

constexpr int image_size = 1024;
constexpr int pixels_size = image_size * image_size * rme::PixelFormatRGB;
int pixels_size = m_imageSize * m_imageSize * rme::PixelFormatRGB;
uint8_t* pixels = new uint8_t[pixels_size];
auto image = new wxImage(image_size, image_size, pixels, true);
auto image = new wxImage(m_imageSize, m_imageSize, pixels, true);

int processedTiles = 0;
int lastShownProgress = -1;
Expand All @@ -201,8 +201,8 @@ bool IOMinimap::exportMinimap(const std::string &directory) {
continue;
}

for (int h = 0; h < rme::MapMaxHeight; h += image_size) {
for (int w = 0; w < rme::MapMaxWidth; w += image_size) {
for (int h = 0; h < rme::MapMaxHeight; h += m_imageSize) {
for (int w = 0; w < rme::MapMaxWidth; w += m_imageSize) {
if (w < rect.x || w > rect.width || h < rect.y || h > rect.height) {
continue;
}
Expand All @@ -211,8 +211,8 @@ bool IOMinimap::exportMinimap(const std::string &directory) {
memset(pixels, 0, pixels_size);

int index = 0;
for (int y = 0; y < image_size; y++) {
for (int x = 0; x < image_size; x++) {
for (int y = 0; y < m_imageSize; y++) {
for (int x = 0; x < m_imageSize; x++) {
auto tile = map.getTile(w + x, h + y, z);
if (!tile || (!tile->ground && tile->items.empty())) {
index += rme::PixelFormatRGB;
Expand Down Expand Up @@ -243,7 +243,7 @@ bool IOMinimap::exportMinimap(const std::string &directory) {
wxBitmapType type = m_format == MinimapExportFormat::Png ? wxBITMAP_TYPE_PNG : wxBITMAP_TYPE_BMP;
wxString extension_wx = wxString::FromAscii(extension.mb_str());
wxFileName file = wxString::Format("%s-%s-%s.%s", std::to_string(h), std::to_string(w), std::to_string(z), extension_wx);
file.Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_TILDE | wxPATH_NORM_CASE, directory);
file.Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_TILDE | wxPATH_NORM_CASE | wxPATH_NORM_ABSOLUTE, directory);
image->SaveFile(file.GetFullPath(), type);
}
}
Expand Down
3 changes: 2 additions & 1 deletion source/iominimap.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class MinimapBlock {

class IOMinimap {
public:
IOMinimap(Editor* editor, MinimapExportFormat format, MinimapExportMode mode, bool updateLoadbar);
IOMinimap(Editor* editor, MinimapExportFormat format, MinimapExportMode mode, bool updateLoadbar, int imageSize = 1024);

bool saveMinimap(const std::string &directory, const std::string &name, int floor = -1);

Expand All @@ -95,6 +95,7 @@ class IOMinimap {
MinimapExportMode m_mode;
bool m_updateLoadbar = false;
int m_floor = -1;
int m_imageSize = 1024;
std::unordered_map<uint32_t, MinimapBlock> m_blocks[rme::MapLayers];
std::string m_error;
};
Expand Down

0 comments on commit 64a0877

Please sign in to comment.