| | | @@ -0,0 +1,2017 @@ |
| 1 | + | ////////////////////////////////////////////////////////////////////// |
| 2 | + | // This file is part of Remere's Map Editor |
| 3 | + | ////////////////////////////////////////////////////////////////////// |
| 4 | + | // Remere's Map Editor is free software: you can redistribute it and/or modify |
| 5 | + | // it under the terms of the GNU General Public License as published by |
| 6 | + | // the Free Software Foundation, either version 3 of the License, or |
| 7 | + | // (at your option) any later version. |
| 8 | + | // |
| 9 | + | // Remere's Map Editor is distributed in the hope that it will be useful, |
| 10 | + | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + | // GNU General Public License for more details. |
| 13 | + | // |
| 14 | + | // You should have received a copy of the GNU General Public License |
| 15 | + | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + | ////////////////////////////////////////////////////////////////////// |
| 17 | + | |
| 18 | + | #include "main.h" |
| 19 | + | |
| 20 | + | #include "materials.h" |
| 21 | + | #include "brush.h" |
| 22 | + | #include "editor.h" |
| 23 | + | |
| 24 | + | #include "items.h" |
| 25 | + | #include "map.h" |
| 26 | + | #include "item.h" |
| 27 | + | #include "complexitem.h" |
| 28 | + | #include "raw_brush.h" |
| 29 | + | |
| 30 | + | #include "palette_window.h" |
| 31 | + | #include "gui.h" |
| 32 | + | #include "application.h" |
| 33 | + | #include "common_windows.h" |
| 34 | + | #include "positionctrl.h" |
| 35 | + | #include "string_utils.h" |
| 36 | + | |
| 37 | + | |
| 38 | + | #ifdef _MSC_VER |
| 39 | + | #pragma warning(disable : 4018) // signed/unsigned mismatch |
| 40 | + | #endif |
| 41 | + | |
| 42 | + | // ============================================================================ |
| 43 | + | // Map Properties Window |
| 44 | + | |
| 45 | + | BEGIN_EVENT_TABLE(MapPropertiesWindow, wxDialog) |
| 46 | + | EVT_CHOICE(MAP_PROPERTIES_VERSION, MapPropertiesWindow::OnChangeVersion) |
| 47 | + | EVT_BUTTON(wxID_OK, MapPropertiesWindow::OnClickOK) |
| 48 | + | EVT_BUTTON(wxID_CANCEL, MapPropertiesWindow::OnClickCancel) |
| 49 | + | END_EVENT_TABLE() |
| 50 | + | |
| 51 | + | MapPropertiesWindow::MapPropertiesWindow(wxWindow* parent, MapTab* view, Editor& editor) : |
| 52 | + | wxDialog(parent, wxID_ANY, "Map Properties", wxDefaultPosition, wxSize(300, 200), wxRESIZE_BORDER | wxCAPTION), |
| 53 | + | view(view), |
| 54 | + | editor(editor) { |
| 55 | + | // Setup data variabels |
| 56 | + | Map& map = editor.map; |
| 57 | + | |
| 58 | + | wxSizer* topsizer = newd wxBoxSizer(wxVERTICAL); |
| 59 | + | |
| 60 | + | wxFlexGridSizer* grid_sizer = newd wxFlexGridSizer(2, 10, 10); |
| 61 | + | grid_sizer->AddGrowableCol(1); |
| 62 | + | |
| 63 | + | // Description |
| 64 | + | grid_sizer->Add(newd wxStaticText(this, wxID_ANY, "Map Description")); |
| 65 | + | description_ctrl = newd wxTextCtrl(this, wxID_ANY, wxstr(map.getMapDescription()), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE); |
| 66 | + | grid_sizer->Add(description_ctrl, wxSizerFlags(1).Expand()); |
| 67 | + | |
| 68 | + | // Map version |
| 69 | + | grid_sizer->Add(newd wxStaticText(this, wxID_ANY, "Map Version")); |
| 70 | + | version_choice = newd wxChoice(this, MAP_PROPERTIES_VERSION); |
| 71 | + | version_choice->Append("OTServ 0.5.0"); |
| 72 | + | version_choice->Append("OTServ 0.6.0"); |
| 73 | + | version_choice->Append("OTServ 0.6.1"); |
| 74 | + | version_choice->Append("OTServ 0.7.0 (revscriptsys)"); |
| 75 | + | |
| 76 | + | switch (map.getVersion().otbm) { |
| 77 | + | case MAP_OTBM_1: |
| 78 | + | version_choice->SetSelection(0); |
| 79 | + | break; |
| 80 | + | case MAP_OTBM_2: |
| 81 | + | version_choice->SetSelection(1); |
| 82 | + | break; |
| 83 | + | case MAP_OTBM_3: |
| 84 | + | version_choice->SetSelection(2); |
| 85 | + | break; |
| 86 | + | case MAP_OTBM_4: |
| 87 | + | version_choice->SetSelection(3); |
| 88 | + | break; |
| 89 | + | default: |
| 90 | + | version_choice->SetSelection(0); |
| 91 | + | } |
| 92 | + | |
| 93 | + | grid_sizer->Add(version_choice, wxSizerFlags(1).Expand()); |
| 94 | + | |
| 95 | + | // Version |
| 96 | + | grid_sizer->Add(newd wxStaticText(this, wxID_ANY, "Client Version")); |
| 97 | + | protocol_choice = newd wxChoice(this, wxID_ANY); |
| 98 | + | |
| 99 | + | protocol_choice->SetStringSelection(wxstr(g_gui.GetCurrentVersion().getName())); |
| 100 | + | |
| 101 | + | grid_sizer->Add(protocol_choice, wxSizerFlags(1).Expand()); |
| 102 | + | |
| 103 | + | // Auto update checkbox |
| 104 | + | grid_sizer->Add(newd wxStaticText(this, wxID_ANY, "Auto Update OTBM")); |
| 105 | + | auto_update_checkbox = newd wxCheckBox(this, wxID_ANY, ""); |
| 106 | + | auto_update_checkbox->SetValue(true); // Default to enabled |
| 107 | + | grid_sizer->Add(auto_update_checkbox, wxSizerFlags(0).Left()); |
| 108 | + | |
| 109 | + | // Dimensions |
| 110 | + | grid_sizer->Add(newd wxStaticText(this, wxID_ANY, "Map Dimensions")); |
| 111 | + | { |
| 112 | + | wxSizer* subsizer = newd wxBoxSizer(wxHORIZONTAL); |
| 113 | + | subsizer->Add( |
| 114 | + | width_spin = newd wxSpinCtrl(this, wxID_ANY, wxstr(i2s(map.getWidth())), wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 256, MAP_MAX_WIDTH), wxSizerFlags(1).Expand() |
| 115 | + | ); |
| 116 | + | subsizer->Add( |
| 117 | + | height_spin = newd wxSpinCtrl(this, wxID_ANY, wxstr(i2s(map.getHeight())), wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 256, MAP_MAX_HEIGHT), wxSizerFlags(1).Expand() |
| 118 | + | ); |
| 119 | + | grid_sizer->Add(subsizer, 1, wxEXPAND); |
| 120 | + | } |
| 121 | + | |
| 122 | + | // External files |
| 123 | + | grid_sizer->Add( |
| 124 | + | newd wxStaticText(this, wxID_ANY, "External Housefile") |
| 125 | + | ); |
| 126 | + | |
| 127 | + | grid_sizer->Add( |
| 128 | + | house_filename_ctrl = newd wxTextCtrl(this, wxID_ANY, wxstr(map.getHouseFilename())), 1, wxEXPAND |
| 129 | + | ); |
| 130 | + | |
| 131 | + | grid_sizer->Add( |
| 132 | + | newd wxStaticText(this, wxID_ANY, "External Spawnfile") |
| 133 | + | ); |
| 134 | + | |
| 135 | + | grid_sizer->Add( |
| 136 | + | spawn_filename_ctrl = newd wxTextCtrl(this, wxID_ANY, wxstr(map.getSpawnFilename())), 1, wxEXPAND |
| 137 | + | ); |
| 138 | + | |
| 139 | + | topsizer->Add(grid_sizer, wxSizerFlags(1).Expand().Border(wxALL, 20)); |
| 140 | + | |
| 141 | + | wxSizer* subsizer = newd wxBoxSizer(wxHORIZONTAL); |
| 142 | + | subsizer->Add(newd wxButton(this, wxID_OK, "OK"), wxSizerFlags(1).Center()); |
| 143 | + | subsizer->Add(newd wxButton(this, wxID_CANCEL, "Cancel"), wxSizerFlags(1).Center()); |
| 144 | + | topsizer->Add(subsizer, wxSizerFlags(0).Center().Border(wxLEFT | wxRIGHT | wxBOTTOM, 20)); |
| 145 | + | |
| 146 | + | SetSizerAndFit(topsizer); |
| 147 | + | Centre(wxBOTH); |
| 148 | + | UpdateProtocolList(); |
| 149 | + | |
| 150 | + | ClientVersion* current_version = ClientVersion::get(map.getVersion().client); |
| 151 | + | protocol_choice->SetStringSelection(wxstr(current_version->getName())); |
| 152 | + | |
| 153 | + | // Bind the protocol choice event |
| 154 | + | protocol_choice->Bind(wxEVT_CHOICE, &MapPropertiesWindow::OnChangeVersion, this); |
| 155 | + | //protocol_choice->Bind(wxEVT_CHOICE, &MapPropertiesWindow::OnClientVersionChange, this); |
| 156 | + | } |
| 157 | + | |
| 158 | + | void MapPropertiesWindow::UpdateProtocolList() { |
| 159 | + | wxString ver = version_choice->GetStringSelection(); |
| 160 | + | wxString client = protocol_choice->GetStringSelection(); |
| 161 | + | |
| 162 | + | protocol_choice->Clear(); |
| 163 | + | |
| 164 | + | // Always show all visible versions |
| 165 | + | ClientVersionList versions = ClientVersion::getAllVisible(); |
| 166 | + | for(ClientVersionList::const_iterator p = versions.begin(); p != versions.end(); ++p) { |
| 167 | + | protocol_choice->Append(wxstr((*p)->getName())); |
| 168 | + | } |
| 169 | + | |
| 170 | + | protocol_choice->SetSelection(0); |
| 171 | + | protocol_choice->SetStringSelection(client); |
| 172 | + | } |
| 173 | + | |
| 174 | + | void MapPropertiesWindow::OnChangeVersion(wxCommandEvent& event) { |
| 175 | + | // Get selected client version |
| 176 | + | wxString client = protocol_choice->GetStringSelection(); |
| 177 | + | ClientVersion* version = ClientVersion::get(nstr(client)); |
| 178 | + | |
| 179 | + | // If client version changed, update map version |
| 180 | + | if(version) { |
| 181 | + | // Set map version based on client version |
| 182 | + | MapVersionID preferred_version = version->getPrefferedMapVersionID(); |
| 183 | + | switch(preferred_version) { |
| 184 | + | case MAP_OTBM_1: |
| 185 | + | version_choice->SetSelection(0); // OTBM 1 - 0.5.0 |
| 186 | + | break; |
| 187 | + | case MAP_OTBM_2: |
| 188 | + | version_choice->SetSelection(1); // OTBM 2 - 0.6.0 |
| 189 | + | break; |
| 190 | + | case MAP_OTBM_3: |
| 191 | + | version_choice->SetSelection(2); // OTBM 3 - 0.6.1 |
| 192 | + | break; |
| 193 | + | case MAP_OTBM_4: |
| 194 | + | version_choice->SetSelection(3); // OTBM 4 - 0.7.0 |
| 195 | + | break; |
| 196 | + | default: |
| 197 | + | version_choice->SetSelection(0); // Default to OTBM 1 |
| 198 | + | break; |
| 199 | + | } |
| 200 | + | version_choice->Refresh(); // Force visual refresh |
| 201 | + | } |
| 202 | + | |
| 203 | + | // Get selected map version |
| 204 | + | wxString map_ver = version_choice->GetStringSelection(); |
| 205 | + | |
| 206 | + | // If map version changed, update client version list |
| 207 | + | if(map_ver.Contains("0.5.0")) { |
| 208 | + | // Filter client versions for OTBM 1 |
| 209 | + | UpdateProtocolList(); |
| 210 | + | protocol_choice->SetStringSelection(client); |
| 211 | + | } else if(map_ver.Contains("0.6.0")) { |
| 212 | + | // Filter client versions for OTBM 2 |
| 213 | + | UpdateProtocolList(); |
| 214 | + | protocol_choice->SetStringSelection(client); |
| 215 | + | } else if(map_ver.Contains("0.6.1")) { |
| 216 | + | // Filter client versions for OTBM 3 |
| 217 | + | UpdateProtocolList(); |
| 218 | + | protocol_choice->SetStringSelection(client); |
| 219 | + | } else if(map_ver.Contains("0.7.0")) { |
| 220 | + | // Filter client versions for OTBM 4 |
| 221 | + | UpdateProtocolList(); |
| 222 | + | protocol_choice->SetStringSelection(client); |
| 223 | + | } |
| 224 | + | } |
| 225 | + | |
| 226 | + | void MapPropertiesWindow::OnClientVersionChange(wxCommandEvent& event) { |
| 227 | + | // Get selected client version |
| 228 | + | wxString client = protocol_choice->GetStringSelection(); |
| 229 | + | ClientVersion* version = ClientVersion::get(nstr(client)); |
| 230 | + | |
| 231 | + | // Only update OTBM version if auto-update is enabled |
| 232 | + | if(version && auto_update_checkbox->GetValue()) { |
| 233 | + | // Set map version based on client version |
| 234 | + | MapVersionID preferred_version = version->getPrefferedMapVersionID(); |
| 235 | + | switch(preferred_version) { |
| 236 | + | case MAP_OTBM_1: |
| 237 | + | version_choice->SetSelection(0); // OTBM 1 - 0.5.0 |
| 238 | + | break; |
| 239 | + | case MAP_OTBM_2: |
| 240 | + | version_choice->SetSelection(1); // OTBM 2 - 0.6.0 |
| 241 | + | break; |
| 242 | + | case MAP_OTBM_3: |
| 243 | + | version_choice->SetSelection(2); // OTBM 3 - 0.6.1 |
| 244 | + | break; |
| 245 | + | case MAP_OTBM_4: |
| 246 | + | version_choice->SetSelection(3); // OTBM 4 - 0.7.0 |
| 247 | + | break; |
| 248 | + | default: |
| 249 | + | version_choice->SetSelection(0); // Default to OTBM 1 |
| 250 | + | break; |
| 251 | + | } |
| 252 | + | version_choice->Refresh(); |
| 253 | + | } |
| 254 | + | |
| 255 | + | event.Skip(); |
| 256 | + | } |
| 257 | + | |
| 258 | + | struct MapConversionContext { |
| 259 | + | struct CreatureInfo { |
| 260 | + | std::string name; |
| 261 | + | bool is_npc; |
| 262 | + | Outfit outfit; |
| 263 | + | }; |
| 264 | + | typedef std::map<std::string, CreatureInfo> CreatureMap; |
| 265 | + | CreatureMap creature_types; |
| 266 | + | |
| 267 | + | void operator()(Map& map, Tile* tile, long long done) { |
| 268 | + | if (tile->creature) { |
| 269 | + | CreatureMap::iterator f = creature_types.find(tile->creature->getName()); |
| 270 | + | if (f == creature_types.end()) { |
| 271 | + | CreatureInfo info = { |
| 272 | + | tile->creature->getName(), |
| 273 | + | tile->creature->isNpc(), |
| 274 | + | tile->creature->getLookType() |
| 275 | + | }; |
| 276 | + | creature_types[tile->creature->getName()] = info; |
| 277 | + | } |
| 278 | + | } |
| 279 | + | } |
| 280 | + | }; |
| 281 | + | |
| 282 | + | void MapPropertiesWindow::OnClickOK(wxCommandEvent& WXUNUSED(event)) { |
| 283 | + | Map& map = editor.map; |
| 284 | + | |
| 285 | + | MapVersion old_ver = map.getVersion(); |
| 286 | + | MapVersion new_ver; |
| 287 | + | |
| 288 | + | wxString ver = version_choice->GetStringSelection(); |
| 289 | + | |
| 290 | + | new_ver.client = ClientVersion::get(nstr(protocol_choice->GetStringSelection()))->getID(); |
| 291 | + | if (ver.Contains("0.5.0")) { |
| 292 | + | new_ver.otbm = MAP_OTBM_1; |
| 293 | + | } else if (ver.Contains("0.6.0")) { |
| 294 | + | new_ver.otbm = MAP_OTBM_2; |
| 295 | + | } else if (ver.Contains("0.6.1")) { |
| 296 | + | new_ver.otbm = MAP_OTBM_3; |
| 297 | + | } else if (ver.Contains("0.7.0")) { |
| 298 | + | new_ver.otbm = MAP_OTBM_4; |
| 299 | + | } |
| 300 | + | |
| 301 | + | if (new_ver.client != old_ver.client) { |
| 302 | + | if (g_gui.GetOpenMapCount() > 1) { |
| 303 | + | g_gui.PopupDialog(this, "Error", "You can not change editor version with multiple maps open", wxOK); |
| 304 | + | return; |
| 305 | + | } |
| 306 | + | wxString error; |
| 307 | + | wxArrayString warnings; |
| 308 | + | |
| 309 | + | // Switch version |
| 310 | + | g_gui.GetCurrentEditor()->selection.clear(); |
| 311 | + | g_gui.GetCurrentEditor()->actionQueue->clear(); |
| 312 | + | |
| 313 | + | if (!g_gui.LoadVersion(new_ver.client, error, warnings)) { |
| 314 | + | g_gui.PopupDialog(this, "Error", error, wxOK); |
| 315 | + | g_gui.ListDialog(this, "Warnings", warnings); |
| 316 | + | return; |
| 317 | + | } |
| 318 | + | |
| 319 | + | if (!warnings.empty()) { |
| 320 | + | g_gui.ListDialog(this, "Warnings", warnings); |
| 321 | + | } |
| 322 | + | } |
| 323 | + | |
| 324 | + | map.convert(new_ver, true); |
| 325 | + | map.setMapDescription(nstr(description_ctrl->GetValue())); |
| 326 | + | map.setHouseFilename(nstr(house_filename_ctrl->GetValue())); |
| 327 | + | map.setSpawnFilename(nstr(spawn_filename_ctrl->GetValue())); |
| 328 | + | |
| 329 | + | // Only resize if we have to |
| 330 | + | int new_map_width = width_spin->GetValue(); |
| 331 | + | int new_map_height = height_spin->GetValue(); |
| 332 | + | if(new_map_width != map.getWidth() || new_map_height != map.getHeight()) { |
| 333 | + | map.setWidth(new_map_width); |
| 334 | + | map.setHeight(new_map_height); |
| 335 | + | g_gui.FitViewToMap(view); |
| 336 | + | } |
| 337 | + | g_gui.RefreshPalettes(); |
| 338 | + | |
| 339 | + | EndModal(1); |
| 340 | + | } |
| 341 | + | |
| 342 | + | void MapPropertiesWindow::OnClickCancel(wxCommandEvent& WXUNUSED(event)) { |
| 343 | + | // Just close this window |
| 344 | + | EndModal(1); |
| 345 | + | } |
| 346 | + | |
| 347 | + | MapPropertiesWindow::~MapPropertiesWindow() = default; |
| 348 | + | |
| 349 | + | // ============================================================================ |
| 350 | + | // Map Import Window |
| 351 | + | |
| 352 | + | BEGIN_EVENT_TABLE(ImportMapWindow, wxDialog) |
| 353 | + | EVT_BUTTON(MAP_WINDOW_FILE_BUTTON, ImportMapWindow::OnClickBrowse) |
| 354 | + | EVT_BUTTON(wxID_OK, ImportMapWindow::OnClickOK) |
| 355 | + | EVT_BUTTON(wxID_CANCEL, ImportMapWindow::OnClickCancel) |
| 356 | + | END_EVENT_TABLE() |
| 357 | + | |
| 358 | + | ImportMapWindow::ImportMapWindow(wxWindow* parent, Editor& editor) : |
| 359 | + | wxDialog(parent, wxID_ANY, "Import Map", wxDefaultPosition, wxSize(350, 315)), |
| 360 | + | editor(editor) { |
| 361 | + | wxBoxSizer* sizer = newd wxBoxSizer(wxVERTICAL); |
| 362 | + | wxStaticBoxSizer* tmpsizer; |
| 363 | + | |
| 364 | + | // File |
| 365 | + | tmpsizer = newd wxStaticBoxSizer(new wxStaticBox(this, wxID_ANY, "Map File"), wxHORIZONTAL); |
| 366 | + | file_text_field = newd wxTextCtrl(tmpsizer->GetStaticBox(), wxID_ANY, "", wxDefaultPosition, wxSize(230, 23)); |
| 367 | + | tmpsizer->Add(file_text_field, 0, wxALL, 5); |
| 368 | + | wxButton* browse_button = newd wxButton(tmpsizer->GetStaticBox(), MAP_WINDOW_FILE_BUTTON, "Browse...", wxDefaultPosition, wxSize(80, 23)); |
| 369 | + | tmpsizer->Add(browse_button, 0, wxALL, 5); |
| 370 | + | sizer->Add(tmpsizer, 1, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, 5); |
| 371 | + | |
| 372 | + | // Import offset |
| 373 | + | tmpsizer = newd wxStaticBoxSizer(new wxStaticBox(this, wxID_ANY, "Import Offset"), wxHORIZONTAL); |
| 374 | + | tmpsizer->Add(newd wxStaticText(tmpsizer->GetStaticBox(), wxID_ANY, "Offset X:"), 0, wxALL | wxEXPAND, 5); |
| 375 | + | x_offset_ctrl = newd wxSpinCtrl(tmpsizer->GetStaticBox(), wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(100, 23), wxSP_ARROW_KEYS, -MAP_MAX_HEIGHT, MAP_MAX_HEIGHT); |
| 376 | + | tmpsizer->Add(x_offset_ctrl, 0, wxALL, 5); |
| 377 | + | tmpsizer->Add(newd wxStaticText(tmpsizer->GetStaticBox(), wxID_ANY, "Offset Y:"), 0, wxALL, 5); |
| 378 | + | y_offset_ctrl = newd wxSpinCtrl(tmpsizer->GetStaticBox(), wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(100, 23), wxSP_ARROW_KEYS, -MAP_MAX_HEIGHT, MAP_MAX_HEIGHT); |
| 379 | + | tmpsizer->Add(y_offset_ctrl, 0, wxALL, 5); |
| 380 | + | sizer->Add(tmpsizer, 1, wxEXPAND | wxLEFT | wxRIGHT, 5); |
| 381 | + | |
| 382 | + | // Import options |
| 383 | + | wxArrayString house_choices; |
| 384 | + | house_choices.Add("Smart Merge"); |
| 385 | + | house_choices.Add("Insert"); |
| 386 | + | house_choices.Add("Merge"); |
| 387 | + | house_choices.Add("Don't Import"); |
| 388 | + | |
| 389 | + | // House options |
| 390 | + | tmpsizer = newd wxStaticBoxSizer(new wxStaticBox(this, wxID_ANY, "House Import Behaviour"), wxVERTICAL); |
| 391 | + | house_options = newd wxChoice(tmpsizer->GetStaticBox(), wxID_ANY, wxDefaultPosition, wxDefaultSize, house_choices); |
| 392 | + | house_options->SetSelection(0); |
| 393 | + | tmpsizer->Add(house_options, 0, wxALL | wxEXPAND, 5); |
| 394 | + | sizer->Add(tmpsizer, 1, wxEXPAND | wxLEFT | wxRIGHT, 5); |
| 395 | + | |
| 396 | + | // Import options |
| 397 | + | wxArrayString spawn_choices; |
| 398 | + | spawn_choices.Add("Merge"); |
| 399 | + | spawn_choices.Add("Don't Import"); |
| 400 | + | |
| 401 | + | // Spawn options |
| 402 | + | tmpsizer = newd wxStaticBoxSizer(new wxStaticBox(this, wxID_ANY, "Spawn Import Behaviour"), wxVERTICAL); |
| 403 | + | spawn_options = newd wxChoice(tmpsizer->GetStaticBox(), wxID_ANY, wxDefaultPosition, wxDefaultSize, spawn_choices); |
| 404 | + | spawn_options->SetSelection(0); |
| 405 | + | tmpsizer->Add(spawn_options, 0, wxALL | wxEXPAND, 5); |
| 406 | + | sizer->Add(tmpsizer, 1, wxEXPAND | wxLEFT | wxRIGHT, 5); |
| 407 | + | |
| 408 | + | // OK/Cancel buttons |
| 409 | + | wxBoxSizer* buttons = newd wxBoxSizer(wxHORIZONTAL); |
| 410 | + | buttons->Add(newd wxButton(this, wxID_OK, "Ok"), 0, wxALL, 5); |
| 411 | + | buttons->Add(newd wxButton(this, wxID_CANCEL, "Cancel"), 0, wxALL, 5); |
| 412 | + | sizer->Add(buttons, wxSizerFlags(1).Center()); |
| 413 | + | |
| 414 | + | SetSizer(sizer); |
| 415 | + | Layout(); |
| 416 | + | Centre(wxBOTH); |
| 417 | + | } |
| 418 | + | |
| 419 | + | ImportMapWindow::~ImportMapWindow() = default; |
| 420 | + | |
| 421 | + | void ImportMapWindow::OnClickBrowse(wxCommandEvent& WXUNUSED(event)) { |
| 422 | + | wxFileDialog dialog(this, "Import...", "", "", "*.otbm", wxFD_OPEN | wxFD_FILE_MUST_EXIST); |
| 423 | + | int ok = dialog.ShowModal(); |
| 424 | + | |
| 425 | + | if (ok == wxID_OK) { |
| 426 | + | file_text_field->ChangeValue(dialog.GetPath()); |
| 427 | + | } |
| 428 | + | } |
| 429 | + | |
| 430 | + | void ImportMapWindow::OnClickOK(wxCommandEvent& WXUNUSED(event)) { |
| 431 | + | if (Validate() && TransferDataFromWindow()) { |
| 432 | + | wxFileName fn = file_text_field->GetValue(); |
| 433 | + | if (!fn.FileExists()) { |
| 434 | + | g_gui.PopupDialog(this, "Error", "The specified map file doesn't exist", wxOK); |
| 435 | + | return; |
| 436 | + | } |
| 437 | + | |
| 438 | + | ImportType spawn_import_type = IMPORT_DONT; |
| 439 | + | ImportType house_import_type = IMPORT_DONT; |
| 440 | + | |
| 441 | + | switch (spawn_options->GetSelection()) { |
| 442 | + | case 0: |
| 443 | + | spawn_import_type = IMPORT_MERGE; |
| 444 | + | break; |
| 445 | + | case 1: |
| 446 | + | spawn_import_type = IMPORT_DONT; |
| 447 | + | break; |
| 448 | + | } |
| 449 | + | |
| 450 | + | switch (house_options->GetSelection()) { |
| 451 | + | case 0: |
| 452 | + | house_import_type = IMPORT_SMART_MERGE; |
| 453 | + | break; |
| 454 | + | case 1: |
| 455 | + | house_import_type = IMPORT_MERGE; |
| 456 | + | break; |
| 457 | + | case 2: |
| 458 | + | house_import_type = IMPORT_INSERT; |
| 459 | + | break; |
| 460 | + | case 3: |
| 461 | + | house_import_type = IMPORT_DONT; |
| 462 | + | break; |
| 463 | + | } |
| 464 | + | |
| 465 | + | EndModal(1); |
| 466 | + | |
| 467 | + | editor.importMap(fn, x_offset_ctrl->GetValue(), y_offset_ctrl->GetValue(), house_import_type, spawn_import_type); |
| 468 | + | } |
| 469 | + | } |
| 470 | + | |
| 471 | + | void ImportMapWindow::OnClickCancel(wxCommandEvent& WXUNUSED(event)) { |
| 472 | + | // Just close this window |
| 473 | + | EndModal(0); |
| 474 | + | } |
| 475 | + | |
| 476 | + | // ============================================================================ |
| 477 | + | // Export Minimap window |
| 478 | + | |
| 479 | + | BEGIN_EVENT_TABLE(ExportMiniMapWindow, wxDialog) |
| 480 | + | EVT_BUTTON(MAP_WINDOW_FILE_BUTTON, ExportMiniMapWindow::OnClickBrowse) |
| 481 | + | EVT_BUTTON(wxID_OK, ExportMiniMapWindow::OnClickOK) |
| 482 | + | EVT_BUTTON(wxID_CANCEL, ExportMiniMapWindow::OnClickCancel) |
| 483 | + | EVT_CHOICE(wxID_ANY, ExportMiniMapWindow::OnExportTypeChange) |
| 484 | + | END_EVENT_TABLE() |
| 485 | + | |
| 486 | + | ExportMiniMapWindow::ExportMiniMapWindow(wxWindow* parent, Editor& editor) : |
| 487 | + | wxDialog(parent, wxID_ANY, "Export Minimap", wxDefaultPosition, wxSize(400, 300)), |
| 488 | + | editor(editor) { |
| 489 | + | wxSizer* sizer = newd wxBoxSizer(wxVERTICAL); |
| 490 | + | wxSizer* tmpsizer; |
| 491 | + | |
| 492 | + | // Error field |
| 493 | + | error_field = newd wxStaticText(this, wxID_VIEW_DETAILS, "", wxDefaultPosition, wxDefaultSize); |
| 494 | + | error_field->SetForegroundColour(*wxRED); |
| 495 | + | tmpsizer = newd wxBoxSizer(wxHORIZONTAL); |
| 496 | + | tmpsizer->Add(error_field, 0, wxALL, 5); |
| 497 | + | sizer->Add(tmpsizer, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, 5); |
| 498 | + | |
| 499 | + | // Output folder |
| 500 | + | directory_text_field = newd wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize); |
| 501 | + | directory_text_field->Bind(wxEVT_KEY_UP, &ExportMiniMapWindow::OnDirectoryChanged, this); |
| 502 | + | directory_text_field->SetValue(wxString(g_settings.getString(Config::MINIMAP_EXPORT_DIR))); |
| 503 | + | tmpsizer = newd wxStaticBoxSizer(wxHORIZONTAL, this, "Output Folder"); |
| 504 | + | tmpsizer->Add(directory_text_field, 1, wxALL, 5); |
| 505 | + | tmpsizer->Add(newd wxButton(this, MAP_WINDOW_FILE_BUTTON, "Browse"), 0, wxALL, 5); |
| 506 | + | sizer->Add(tmpsizer, 0, wxALL | wxEXPAND, 5); |
| 507 | + | |
| 508 | + | // File name |
| 509 | + | wxString mapName(editor.map.getName().c_str(), wxConvUTF8); |
| 510 | + | file_name_text_field = newd wxTextCtrl(this, wxID_ANY, mapName.BeforeLast('.'), wxDefaultPosition, wxDefaultSize); |
| 511 | + | file_name_text_field->Bind(wxEVT_KEY_UP, &ExportMiniMapWindow::OnFileNameChanged, this); |
| 512 | + | tmpsizer = newd wxStaticBoxSizer(wxHORIZONTAL, this, "File Name"); |
| 513 | + | tmpsizer->Add(file_name_text_field, 1, wxALL, 5); |
| 514 | + | sizer->Add(tmpsizer, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, 5); |
| 515 | + | |
| 516 | + | // Export options |
| 517 | + | wxArrayString choices; |
| 518 | + | choices.Add("All Floors"); |
| 519 | + | choices.Add("Ground Floor"); |
| 520 | + | choices.Add("Specific Floor"); |
| 521 | + | |
| 522 | + | if (editor.hasSelection()) { |
| 523 | + | choices.Add("Selected Area"); |
| 524 | + | } |
| 525 | + | |
| 526 | + | // Area options |
| 527 | + | tmpsizer = newd wxStaticBoxSizer(wxHORIZONTAL, this, "Area Options"); |
| 528 | + | floor_options = newd wxChoice(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, choices); |
| 529 | + | floor_number = newd wxSpinCtrl(this, wxID_ANY, i2ws(GROUND_LAYER), wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 0, MAP_MAX_LAYER, GROUND_LAYER); |
| 530 | + | floor_number->Enable(false); |
| 531 | + | floor_options->SetSelection(0); |
| 532 | + | tmpsizer->Add(floor_options, 1, wxALL, 5); |
| 533 | + | tmpsizer->Add(floor_number, 0, wxALL, 5); |
| 534 | + | sizer->Add(tmpsizer, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, 5); |
| 535 | + | |
| 536 | + | // OK/Cancel buttons |
| 537 | + | tmpsizer = newd wxBoxSizer(wxHORIZONTAL); |
| 538 | + | tmpsizer->Add(ok_button = newd wxButton(this, wxID_OK, "OK"), wxSizerFlags(1).Center()); |
| 539 | + | tmpsizer->Add(newd wxButton(this, wxID_CANCEL, "Cancel"), wxSizerFlags(1).Center()); |
| 540 | + | sizer->Add(tmpsizer, 0, wxCENTER, 10); |
| 541 | + | |
| 542 | + | SetSizer(sizer); |
| 543 | + | Layout(); |
| 544 | + | Centre(wxBOTH); |
| 545 | + | CheckValues(); |
| 546 | + | } |
| 547 | + | |
| 548 | + | ExportMiniMapWindow::~ExportMiniMapWindow() = default; |
| 549 | + | |
| 550 | + | void ExportMiniMapWindow::OnExportTypeChange(wxCommandEvent& event) { |
| 551 | + | floor_number->Enable(event.GetSelection() == 2); |
| 552 | + | } |
| 553 | + | |
| 554 | + | void ExportMiniMapWindow::OnClickBrowse(wxCommandEvent& WXUNUSED(event)) { |
| 555 | + | wxDirDialog dialog(NULL, "Select the output folder", "", wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST); |
| 556 | + | if (dialog.ShowModal() == wxID_OK) { |
| 557 | + | const wxString& directory = dialog.GetPath(); |
| 558 | + | directory_text_field->ChangeValue(directory); |
| 559 | + | } |
| 560 | + | CheckValues(); |
| 561 | + | } |
| 562 | + | |
| 563 | + | void ExportMiniMapWindow::OnDirectoryChanged(wxKeyEvent& event) { |
| 564 | + | CheckValues(); |
| 565 | + | event.Skip(); |
| 566 | + | } |
| 567 | + | |
| 568 | + | void ExportMiniMapWindow::OnFileNameChanged(wxKeyEvent& event) { |
| 569 | + | CheckValues(); |
| 570 | + | event.Skip(); |
| 571 | + | } |
| 572 | + | |
| 573 | + | void ExportMiniMapWindow::OnClickOK(wxCommandEvent& WXUNUSED(event)) { |
| 574 | + | g_gui.CreateLoadBar("Exporting minimap"); |
| 575 | + | |
| 576 | + | try { |
| 577 | + | FileName directory(directory_text_field->GetValue()); |
| 578 | + | g_settings.setString(Config::MINIMAP_EXPORT_DIR, directory_text_field->GetValue().ToStdString()); |
| 579 | + | |
| 580 | + | switch (floor_options->GetSelection()) { |
| 581 | + | case 0: { // All floors |
| 582 | + | for (int floor = 0; floor < MAP_LAYERS; ++floor) { |
| 583 | + | g_gui.SetLoadScale(int(floor * (100.f / 16.f)), int((floor + 1) * (100.f / 16.f))); |
| 584 | + | FileName file(file_name_text_field->GetValue() + "_" + i2ws(floor) + ".bmp"); |
| 585 | + | file.Normalize(wxPATH_NORM_ALL, directory.GetFullPath()); |
| 586 | + | editor.exportMiniMap(file, floor, true); |
| 587 | + | } |
| 588 | + | break; |
| 589 | + | } |
| 590 | + | |
| 591 | + | case 1: { // Ground floor |
| 592 | + | FileName file(file_name_text_field->GetValue() + "_" + i2ws(GROUND_LAYER) + ".bmp"); |
| 593 | + | file.Normalize(wxPATH_NORM_ALL, directory.GetFullPath()); |
| 594 | + | editor.exportMiniMap(file, GROUND_LAYER, true); |
| 595 | + | break; |
| 596 | + | } |
| 597 | + | |
| 598 | + | case 2: { // Specific floors |
| 599 | + | int floor = floor_number->GetValue(); |
| 600 | + | FileName file(file_name_text_field->GetValue() + "_" + i2ws(floor) + ".bmp"); |
| 601 | + | file.Normalize(wxPATH_NORM_ALL, directory.GetFullPath()); |
| 602 | + | editor.exportMiniMap(file, floor, true); |
| 603 | + | break; |
| 604 | + | } |
| 605 | + | |
| 606 | + | case 3: { // Selected area |
| 607 | + | editor.exportSelectionAsMiniMap(directory, file_name_text_field->GetValue()); |
| 608 | + | break; |
| 609 | + | } |
| 610 | + | } |
| 611 | + | } catch (std::bad_alloc&) { |
| 612 | + | g_gui.PopupDialog("Error", "There is not enough memory available to complete the operation.", wxOK); |
| 613 | + | } |
| 614 | + | |
| 615 | + | g_gui.DestroyLoadBar(); |
| 616 | + | EndModal(1); |
| 617 | + | } |
| 618 | + | |
| 619 | + | void ExportMiniMapWindow::OnClickCancel(wxCommandEvent& WXUNUSED(event)) { |
| 620 | + | // Just close this window |
| 621 | + | EndModal(0); |
| 622 | + | } |
| 623 | + | |
| 624 | + | void ExportMiniMapWindow::CheckValues() { |
| 625 | + | if (directory_text_field->IsEmpty()) { |
| 626 | + | error_field->SetLabel("Type or select an output folder."); |
| 627 | + | ok_button->Enable(false); |
| 628 | + | return; |
| 629 | + | } |
| 630 | + | |
| 631 | + | if (file_name_text_field->IsEmpty()) { |
| 632 | + | error_field->SetLabel("Type a name for the file."); |
| 633 | + | ok_button->Enable(false); |
| 634 | + | return; |
| 635 | + | } |
| 636 | + | |
| 637 | + | FileName directory(directory_text_field->GetValue()); |
| 638 | + | |
| 639 | + | if (!directory.Exists()) { |
| 640 | + | error_field->SetLabel("Output folder not found."); |
| 641 | + | ok_button->Enable(false); |
| 642 | + | return; |
| 643 | + | } |
| 644 | + | |
| 645 | + | if (!directory.IsDirWritable()) { |
| 646 | + | error_field->SetLabel("Output folder is not writable."); |
| 647 | + | ok_button->Enable(false); |
| 648 | + | return; |
| 649 | + | } |
| 650 | + | |
| 651 | + | error_field->SetLabel(wxEmptyString); |
| 652 | + | ok_button->Enable(true); |
| 653 | + | } |
| 654 | + | |
| 655 | + | // ============================================================================ |
| 656 | + | // Export Tilesets window |
| 657 | + | |
| 658 | + | BEGIN_EVENT_TABLE(ExportTilesetsWindow, wxDialog) |
| 659 | + | EVT_BUTTON(TILESET_FILE_BUTTON, ExportTilesetsWindow::OnClickBrowse) |
| 660 | + | EVT_BUTTON(wxID_OK, ExportTilesetsWindow::OnClickOK) |
| 661 | + | EVT_BUTTON(wxID_CANCEL, ExportTilesetsWindow::OnClickCancel) |
| 662 | + | END_EVENT_TABLE() |
| 663 | + | |
| 664 | + | ExportTilesetsWindow::ExportTilesetsWindow(wxWindow* parent, Editor& editor) : |
| 665 | + | wxDialog(parent, wxID_ANY, "Export Tilesets", wxDefaultPosition, wxSize(400, 230)), |
| 666 | + | editor(editor) { |
| 667 | + | wxSizer* sizer = newd wxBoxSizer(wxVERTICAL); |
| 668 | + | wxSizer* tmpsizer; |
| 669 | + | |
| 670 | + | // Error field |
| 671 | + | error_field = newd wxStaticText(this, wxID_VIEW_DETAILS, "", wxDefaultPosition, wxDefaultSize); |
| 672 | + | error_field->SetForegroundColour(*wxRED); |
| 673 | + | tmpsizer = newd wxBoxSizer(wxHORIZONTAL); |
| 674 | + | tmpsizer->Add(error_field, 0, wxALL, 5); |
| 675 | + | sizer->Add(tmpsizer, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, 5); |
| 676 | + | |
| 677 | + | // Output folder |
| 678 | + | directory_text_field = newd wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize); |
| 679 | + | directory_text_field->Bind(wxEVT_KEY_UP, &ExportTilesetsWindow::OnDirectoryChanged, this); |
| 680 | + | directory_text_field->SetValue(wxString(g_settings.getString(Config::TILESET_EXPORT_DIR))); |
| 681 | + | tmpsizer = newd wxStaticBoxSizer(wxHORIZONTAL, this, "Output Folder"); |
| 682 | + | tmpsizer->Add(directory_text_field, 1, wxALL, 5); |
| 683 | + | tmpsizer->Add(newd wxButton(this, TILESET_FILE_BUTTON, "Browse"), 0, wxALL, 5); |
| 684 | + | sizer->Add(tmpsizer, 0, wxALL | wxEXPAND, 5); |
| 685 | + | |
| 686 | + | // File name |
| 687 | + | file_name_text_field = newd wxTextCtrl(this, wxID_ANY, "tilesets", wxDefaultPosition, wxDefaultSize); |
| 688 | + | file_name_text_field->Bind(wxEVT_KEY_UP, &ExportTilesetsWindow::OnFileNameChanged, this); |
| 689 | + | tmpsizer = newd wxStaticBoxSizer(wxHORIZONTAL, this, "File Name"); |
| 690 | + | tmpsizer->Add(file_name_text_field, 1, wxALL, 5); |
| 691 | + | sizer->Add(tmpsizer, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, 5); |
| 692 | + | |
| 693 | + | // OK/Cancel buttons |
| 694 | + | tmpsizer = newd wxBoxSizer(wxHORIZONTAL); |
| 695 | + | tmpsizer->Add(ok_button = newd wxButton(this, wxID_OK, "OK"), wxSizerFlags(1).Center()); |
| 696 | + | tmpsizer->Add(newd wxButton(this, wxID_CANCEL, "Cancel"), wxSizerFlags(1).Center()); |
| 697 | + | sizer->Add(tmpsizer, 0, wxCENTER, 10); |
| 698 | + | |
| 699 | + | SetSizer(sizer); |
| 700 | + | Layout(); |
| 701 | + | Centre(wxBOTH); |
| 702 | + | CheckValues(); |
| 703 | + | } |
| 704 | + | |
| 705 | + | ExportTilesetsWindow::~ExportTilesetsWindow() = default; |
| 706 | + | |
| 707 | + | void ExportTilesetsWindow::OnClickBrowse(wxCommandEvent& WXUNUSED(event)) { |
| 708 | + | wxDirDialog dialog(NULL, "Select the output folder", "", wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST); |
| 709 | + | if (dialog.ShowModal() == wxID_OK) { |
| 710 | + | const wxString& directory = dialog.GetPath(); |
| 711 | + | directory_text_field->ChangeValue(directory); |
| 712 | + | } |
| 713 | + | CheckValues(); |
| 714 | + | } |
| 715 | + | |
| 716 | + | void ExportTilesetsWindow::OnDirectoryChanged(wxKeyEvent& event) { |
| 717 | + | CheckValues(); |
| 718 | + | event.Skip(); |
| 719 | + | } |
| 720 | + | |
| 721 | + | void ExportTilesetsWindow::OnFileNameChanged(wxKeyEvent& event) { |
| 722 | + | CheckValues(); |
| 723 | + | event.Skip(); |
| 724 | + | } |
| 725 | + | |
| 726 | + | void ExportTilesetsWindow::OnClickOK(wxCommandEvent& WXUNUSED(event)) { |
| 727 | + | g_gui.CreateLoadBar("Exporting Tilesets"); |
| 728 | + | |
| 729 | + | try { |
| 730 | + | FileName directory(directory_text_field->GetValue()); |
| 731 | + | g_settings.setString(Config::TILESET_EXPORT_DIR, directory_text_field->GetValue().ToStdString()); |
| 732 | + | |
| 733 | + | FileName file(file_name_text_field->GetValue() + ".xml"); |
| 734 | + | file.Normalize(wxPATH_NORM_ALL, directory.GetFullPath()); |
| 735 | + | |
| 736 | + | pugi::xml_document doc; |
| 737 | + | pugi::xml_node node = doc.append_child("materials"); |
| 738 | + | |
| 739 | + | std::map<std::string, TilesetCategoryType> palettes { |
| 740 | + | { "Terrain", TILESET_TERRAIN }, |
| 741 | + | { "Doodad", TILESET_DOODAD }, |
| 742 | + | { "Items", TILESET_ITEM }, |
| 743 | + | { "Collection", TILESET_COLLECTION }, |
| 744 | + | { "Raw", TILESET_RAW } |
| 745 | + | }; |
| 746 | + | for (TilesetContainer::iterator iter = g_materials.tilesets.begin(); iter != g_materials.tilesets.end(); ++iter) { |
| 747 | + | std::string _data = iter->second->name; |
| 748 | + | std::transform(_data.begin(), _data.end(), _data.begin(), [](unsigned char c) { return std::tolower(c); }); |
| 749 | + | if (_data == "others") { |
| 750 | + | bool blocked = 1; |
| 751 | + | |
| 752 | + | for (const auto& kv : palettes) { |
| 753 | + | TilesetCategory* tilesetCategory = iter->second->getCategory(kv.second); |
| 754 | + | |
| 755 | + | if (kv.second != TILESET_RAW && tilesetCategory->brushlist.size() > 0) { |
| 756 | + | blocked = 0; |
| 757 | + | } |
| 758 | + | } |
| 759 | + | |
| 760 | + | if (blocked) { |
| 761 | + | continue; |
| 762 | + | } |
| 763 | + | } |
| 764 | + | |
| 765 | + | pugi::xml_node tileset = node.append_child("tileset"); |
| 766 | + | tileset.append_attribute("name") = iter->second->name.c_str(); |
| 767 | + | |
| 768 | + | for (const auto& kv : palettes) { |
| 769 | + | TilesetCategory* tilesetCategory = iter->second->getCategory(kv.second); |
| 770 | + | |
| 771 | + | if (tilesetCategory->brushlist.size() > 0) { |
| 772 | + | std::string data = kv.first; |
| 773 | + | std::transform(data.begin(), data.end(), data.begin(), [](unsigned char c) { return std::tolower(c); }); |
| 774 | + | |
| 775 | + | pugi::xml_node palette = tileset.append_child(data.c_str()); |
| 776 | + | for (BrushVector::const_iterator _iter = tilesetCategory->brushlist.begin(); _iter != tilesetCategory->brushlist.end(); ++_iter) { |
| 777 | + | if (!(*_iter)->isRaw()) { |
| 778 | + | pugi::xml_node brush = palette.append_child("brush"); |
| 779 | + | brush.append_attribute("name") = (*_iter)->getName().c_str(); |
| 780 | + | } else { |
| 781 | + | ItemType& it = g_items[(*_iter)->asRaw()->getItemID()]; |
| 782 | + | if (it.id != 0) { |
| 783 | + | pugi::xml_node item = palette.append_child("item"); |
| 784 | + | item.append_attribute("id") = it.id; |
| 785 | + | } |
| 786 | + | } |
| 787 | + | } |
| 788 | + | } |
| 789 | + | } |
| 790 | + | |
| 791 | + | size_t n = std::distance(tileset.begin(), tileset.end()); |
| 792 | + | if (n <= 0) { |
| 793 | + | node.remove_child(tileset); |
| 794 | + | } |
| 795 | + | } |
| 796 | + | |
| 797 | + | doc.save_file(file.GetFullPath().mb_str()); |
| 798 | + | g_gui.PopupDialog("Successfully saved Tilesets", "Saved tilesets to '" + std::string(file.GetFullPath().mb_str()) + "'", wxOK); |
| 799 | + | g_materials.modify(false); |
| 800 | + | } catch (std::bad_alloc&) { |
| 801 | + | g_gui.PopupDialog("Error", "There is not enough memory available to complete the operation.", wxOK); |
| 802 | + | } |
| 803 | + | |
| 804 | + | g_gui.DestroyLoadBar(); |
| 805 | + | EndModal(1); |
| 806 | + | } |
| 807 | + | |
| 808 | + | void ExportTilesetsWindow::OnClickCancel(wxCommandEvent& WXUNUSED(event)) { |
| 809 | + | // Just close this window |
| 810 | + | EndModal(0); |
| 811 | + | } |
| 812 | + | |
| 813 | + | void ExportTilesetsWindow::CheckValues() { |
| 814 | + | if (directory_text_field->IsEmpty()) { |
| 815 | + | error_field->SetLabel("Type or select an output folder."); |
| 816 | + | ok_button->Enable(false); |
| 817 | + | return; |
| 818 | + | } |
| 819 | + | |
| 820 | + | if (file_name_text_field->IsEmpty()) { |
| 821 | + | error_field->SetLabel("Type a name for the file."); |
| 822 | + | ok_button->Enable(false); |
| 823 | + | return; |
| 824 | + | } |
| 825 | + | |
| 826 | + | FileName directory(directory_text_field->GetValue()); |
| 827 | + | |
| 828 | + | if (!directory.Exists()) { |
| 829 | + | error_field->SetLabel("Output folder not found."); |
| 830 | + | ok_button->Enable(false); |
| 831 | + | return; |
| 832 | + | } |
| 833 | + | |
| 834 | + | if (!directory.IsDirWritable()) { |
| 835 | + | error_field->SetLabel("Output folder is not writable."); |
| 836 | + | ok_button->Enable(false); |
| 837 | + | return; |
| 838 | + | } |
| 839 | + | |
| 840 | + | error_field->SetLabel(wxEmptyString); |
| 841 | + | ok_button->Enable(true); |
| 842 | + | } |
| 843 | + | |
| 844 | + | // ============================================================================ |
| 845 | + | // Numkey forwarding text control |
| 846 | + | |
| 847 | + | BEGIN_EVENT_TABLE(KeyForwardingTextCtrl, wxTextCtrl) |
| 848 | + | EVT_KEY_DOWN(KeyForwardingTextCtrl::OnKeyDown) |
| 849 | + | END_EVENT_TABLE() |
| 850 | + | |
| 851 | + | void KeyForwardingTextCtrl::OnKeyDown(wxKeyEvent& event) { |
| 852 | + | if (event.GetKeyCode() == WXK_UP || event.GetKeyCode() == WXK_DOWN || event.GetKeyCode() == WXK_PAGEDOWN || event.GetKeyCode() == WXK_PAGEUP) { |
| 853 | + | GetParent()->GetEventHandler()->AddPendingEvent(event); |
| 854 | + | } else { |
| 855 | + | event.Skip(); |
| 856 | + | } |
| 857 | + | } |
| 858 | + | |
| 859 | + | // ============================================================================ |
| 860 | + | // Find Item Dialog (Jump to item) |
| 861 | + | |
| 862 | + | BEGIN_EVENT_TABLE(FindDialog, wxDialog) |
| 863 | + | EVT_TIMER(wxID_ANY, FindDialog::OnTextIdle) |
| 864 | + | EVT_TEXT(JUMP_DIALOG_TEXT, FindDialog::OnTextChange) |
| 865 | + | EVT_KEY_DOWN(FindDialog::OnKeyDown) |
| 866 | + | EVT_TEXT_ENTER(JUMP_DIALOG_TEXT, FindDialog::OnClickOK) |
| 867 | + | EVT_LISTBOX_DCLICK(JUMP_DIALOG_LIST, FindDialog::OnClickList) |
| 868 | + | EVT_BUTTON(wxID_OK, FindDialog::OnClickOK) |
| 869 | + | EVT_BUTTON(wxID_CANCEL, FindDialog::OnClickCancel) |
| 870 | + | END_EVENT_TABLE() |
| 871 | + | |
| 872 | + | FindDialog::FindDialog(wxWindow* parent, wxString title) : |
| 873 | + | wxDialog(g_gui.root, wxID_ANY, title, wxDefaultPosition, wxDefaultSize, wxRESIZE_BORDER | wxCAPTION | wxCLOSE_BOX), |
| 874 | + | idle_input_timer(this), |
| 875 | + | result_brush(nullptr), |
| 876 | + | result_id(0) { |
| 877 | + | wxSizer* sizer = newd wxBoxSizer(wxVERTICAL); |
| 878 | + | |
| 879 | + | search_field = newd KeyForwardingTextCtrl(this, JUMP_DIALOG_TEXT, "", wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER); |
| 880 | + | search_field->SetFocus(); |
| 881 | + | sizer->Add(search_field, 0, wxEXPAND); |
| 882 | + | |
| 883 | + | item_list = newd FindDialogListBox(this, JUMP_DIALOG_LIST); |
| 884 | + | item_list->SetMinSize(wxSize(470, 400)); |
| 885 | + | sizer->Add(item_list, wxSizerFlags(1).Expand().Border()); |
| 886 | + | |
| 887 | + | wxSizer* stdsizer = newd wxBoxSizer(wxHORIZONTAL); |
| 888 | + | stdsizer->Add(newd wxButton(this, wxID_OK, "OK"), wxSizerFlags(1).Center()); |
| 889 | + | stdsizer->Add(newd wxButton(this, wxID_CANCEL, "Cancel"), wxSizerFlags(1).Center()); |
| 890 | + | sizer->Add(stdsizer, wxSizerFlags(0).Center().Border()); |
| 891 | + | |
| 892 | + | SetSizerAndFit(sizer); |
| 893 | + | Centre(wxBOTH); |
| 894 | + | // We can't call it here since it calls an abstract function, call in child constructors instead. |
| 895 | + | // RefreshContents(); |
| 896 | + | } |
| 897 | + | |
| 898 | + | FindDialog::~FindDialog() = default; |
| 899 | + | |
| 900 | + | void FindDialog::OnKeyDown(wxKeyEvent& event) { |
| 901 | + | int w, h; |
| 902 | + | item_list->GetSize(&w, &h); |
| 903 | + | size_t amount = 1; |
| 904 | + | |
| 905 | + | switch (event.GetKeyCode()) { |
| 906 | + | case WXK_PAGEUP: |
| 907 | + | amount = h / 32 + 1; |
| 908 | + | [[fallthrough]]; |
| 909 | + | case WXK_UP: { |
| 910 | + | if (item_list->GetItemCount() > 0) { |
| 911 | + | ssize_t n = item_list->GetSelection(); |
| 912 | + | if (n == wxNOT_FOUND) { |
| 913 | + | n = 0; |
| 914 | + | } else if (n != amount && n - amount < n) { // latter is needed for unsigned overflow |
| 915 | + | n -= amount; |
| 916 | + | } else { |
| 917 | + | n = 0; |
| 918 | + | } |
| 919 | + | item_list->SetSelection(n); |
| 920 | + | } |
| 921 | + | break; |
| 922 | + | } |
| 923 | + | |
| 924 | + | case WXK_PAGEDOWN: |
| 925 | + | amount = h / 32 + 1; |
| 926 | + | [[fallthrough]]; |
| 927 | + | case WXK_DOWN: { |
| 928 | + | if (item_list->GetItemCount() > 0) { |
| 929 | + | ssize_t n = item_list->GetSelection(); |
| 930 | + | size_t itemcount = item_list->GetItemCount(); |
| 931 | + | if (n == wxNOT_FOUND) { |
| 932 | + | n = 0; |
| 933 | + | } else if (static_cast<uint32_t>(n) < itemcount - amount && itemcount - amount < itemcount) { |
| 934 | + | n += amount; |
| 935 | + | } else { |
| 936 | + | n = item_list->GetItemCount() - 1; |
| 937 | + | } |
| 938 | + | |
| 939 | + | item_list->SetSelection(n); |
| 940 | + | } |
| 941 | + | break; |
| 942 | + | } |
| 943 | + | default: |
| 944 | + | event.Skip(); |
| 945 | + | break; |
| 946 | + | } |
| 947 | + | } |
| 948 | + | |
| 949 | + | void FindDialog::OnTextIdle(wxTimerEvent& WXUNUSED(event)) { |
| 950 | + | RefreshContents(); |
| 951 | + | } |
| 952 | + | |
| 953 | + | void FindDialog::OnTextChange(wxCommandEvent& WXUNUSED(event)) { |
| 954 | + | idle_input_timer.Start(800, true); |
| 955 | + | } |
| 956 | + | |
| 957 | + | void FindDialog::OnClickList(wxCommandEvent& event) { |
| 958 | + | OnClickListInternal(event); |
| 959 | + | } |
| 960 | + | |
| 961 | + | void FindDialog::OnClickOK(wxCommandEvent& WXUNUSED(event)) { |
| 962 | + | // This is to get virtual callback |
| 963 | + | OnClickOKInternal(); |
| 964 | + | } |
| 965 | + | |
| 966 | + | void FindDialog::OnClickCancel(wxCommandEvent& WXUNUSED(event)) { |
| 967 | + | EndModal(0); |
| 968 | + | } |
| 969 | + | |
| 970 | + | void FindDialog::RefreshContents() { |
| 971 | + | // This is to get virtual callback |
| 972 | + | RefreshContentsInternal(); |
| 973 | + | } |
| 974 | + | |
| 975 | + | // ============================================================================ |
| 976 | + | // Find Brush Dialog (Jump to brush) |
| 977 | + | |
| 978 | + | FindBrushDialog::FindBrushDialog(wxWindow* parent, wxString title) : |
| 979 | + | FindDialog(parent, title) { |
| 980 | + | RefreshContents(); |
| 981 | + | } |
| 982 | + | |
| 983 | + | FindBrushDialog::~FindBrushDialog() = default; |
| 984 | + | |
| 985 | + | void FindBrushDialog::OnClickListInternal(wxCommandEvent& event) { |
| 986 | + | Brush* brush = item_list->GetSelectedBrush(); |
| 987 | + | if (brush) { |
| 988 | + | result_brush = brush; |
| 989 | + | EndModal(1); |
| 990 | + | } |
| 991 | + | } |
| 992 | + | |
| 993 | + | void FindBrushDialog::OnClickOKInternal() { |
| 994 | + | // This is kind of stupid as it would fail unless the "Please enter a search string" wasn't there |
| 995 | + | if (item_list->GetItemCount() > 0) { |
| 996 | + | if (item_list->GetSelection() == wxNOT_FOUND) { |
| 997 | + | item_list->SetSelection(0); |
| 998 | + | } |
| 999 | + | Brush* brush = item_list->GetSelectedBrush(); |
| 1000 | + | if (!brush) { |
| 1001 | + | // It's either "Please enter a search string" or "No matches" |
| 1002 | + | // Perhaps we can refresh now? |
| 1003 | + | std::string search_string = as_lower_str(nstr(search_field->GetValue())); |
| 1004 | + | bool do_search = (search_string.size() >= 2); |
| 1005 | + | |
| 1006 | + | if (do_search) { |
| 1007 | + | const BrushMap& map = g_brushes.getMap(); |
| 1008 | + | for (BrushMap::const_iterator iter = map.begin(); iter != map.end(); ++iter) { |
| 1009 | + | const Brush* brush = iter->second; |
| 1010 | + | if (as_lower_str(brush->getName()).find(search_string) == std::string::npos) { |
| 1011 | + | continue; |
| 1012 | + | } |
| 1013 | + | |
| 1014 | + | // Don't match RAWs now. |
| 1015 | + | if (brush->isRaw()) { |
| 1016 | + | continue; |
| 1017 | + | } |
| 1018 | + | |
| 1019 | + | // Found one! |
| 1020 | + | result_brush = brush; |
| 1021 | + | break; |
| 1022 | + | } |
| 1023 | + | |
| 1024 | + | // Did we not find a matching brush? |
| 1025 | + | if (!result_brush) { |
| 1026 | + | // Then let's search the RAWs |
| 1027 | + | for (int id = 0; id <= g_items.getMaxID(); ++id) { |
| 1028 | + | ItemType& it = g_items[id]; |
| 1029 | + | if (it.id == 0) { |
| 1030 | + | continue; |
| 1031 | + | } |
| 1032 | + | |
| 1033 | + | RAWBrush* raw_brush = it.raw_brush; |
| 1034 | + | if (!raw_brush) { |
| 1035 | + | continue; |
| 1036 | + | } |
| 1037 | + | |
| 1038 | + | if (as_lower_str(raw_brush->getName()).find(search_string) == std::string::npos) { |
| 1039 | + | continue; |
| 1040 | + | } |
| 1041 | + | |
| 1042 | + | // Found one! |
| 1043 | + | result_brush = raw_brush; |
| 1044 | + | break; |
| 1045 | + | } |
| 1046 | + | } |
| 1047 | + | // Done! |
| 1048 | + | } |
| 1049 | + | } else { |
| 1050 | + | result_brush = brush; |
| 1051 | + | } |
| 1052 | + | } |
| 1053 | + | EndModal(1); |
| 1054 | + | } |
| 1055 | + | |
| 1056 | + | void FindBrushDialog::RefreshContentsInternal() { |
| 1057 | + | item_list->Clear(); |
| 1058 | + | |
| 1059 | + | // We store the raws so they display last of all results |
| 1060 | + | bool found_search_results = false; |
| 1061 | + | |
| 1062 | + | std::string search_string = as_lower_str(nstr(search_field->GetValue())); |
| 1063 | + | std::vector<std::string> parts = splitString(search_string, '-'); |
| 1064 | + | if (parts.size() == 2 && isInteger(parts[0]) && isInteger(parts[1])) { |
| 1065 | + | uint16_t id_from = std::stoi(parts[0]); |
| 1066 | + | uint16_t id_to = std::stoi(parts[1]); |
| 1067 | + | for (int id = 0; id <= g_items.getMaxID(); ++id) { |
| 1068 | + | ItemType& it = g_items[id]; |
| 1069 | + | if (it.id == 0) { |
| 1070 | + | continue; |
| 1071 | + | } |
| 1072 | + | |
| 1073 | + | if (it.id < id_from || it.id > id_to) { |
| 1074 | + | continue; |
| 1075 | + | } |
| 1076 | + | |
| 1077 | + | RAWBrush* raw_brush = it.raw_brush; |
| 1078 | + | if (!raw_brush) { |
| 1079 | + | continue; |
| 1080 | + | } |
| 1081 | + | |
| 1082 | + | found_search_results = true; |
| 1083 | + | item_list->AddBrush(raw_brush); |
| 1084 | + | } |
| 1085 | + | } else if (!parts.empty()) { |
| 1086 | + | for (auto& search_text : parts) { |
| 1087 | + | const BrushMap& brushes_map = g_brushes.getMap(); |
| 1088 | + | for (BrushMap::const_iterator iter = brushes_map.begin(); iter != brushes_map.end(); ++iter) { |
| 1089 | + | const Brush* brush = iter->second; |
| 1090 | + | if (!brush || as_lower_str(brush->getName()).find(search_text) == std::string::npos) { |
| 1091 | + | continue; |
| 1092 | + | } |
| 1093 | + | |
| 1094 | + | if (brush->isRaw()) { |
| 1095 | + | continue; |
| 1096 | + | } |
| 1097 | + | |
| 1098 | + | found_search_results = true; |
| 1099 | + | item_list->AddBrush(const_cast<Brush*>(brush)); |
| 1100 | + | } |
| 1101 | + | |
| 1102 | + | for (int id = 0; id <= g_items.getMaxID(); ++id) { |
| 1103 | + | ItemType& it = g_items[id]; |
| 1104 | + | if (it.id == 0) { |
| 1105 | + | continue; |
| 1106 | + | } |
| 1107 | + | |
| 1108 | + | RAWBrush* raw_brush = it.raw_brush; |
| 1109 | + | if (!raw_brush) { |
| 1110 | + | continue; |
| 1111 | + | } |
| 1112 | + | |
| 1113 | + | if (as_lower_str(raw_brush->getName()).find(search_text) == std::string::npos) { |
| 1114 | + | continue; |
| 1115 | + | } |
| 1116 | + | |
| 1117 | + | found_search_results = true; |
| 1118 | + | item_list->AddBrush(raw_brush); |
| 1119 | + | } |
| 1120 | + | } |
| 1121 | + | } else if (search_string.size() >= 2) { |
| 1122 | + | const BrushMap& brushes_map = g_brushes.getMap(); |
| 1123 | + | for (BrushMap::const_iterator iter = brushes_map.begin(); iter != brushes_map.end(); ++iter) { |
| 1124 | + | const Brush* brush = iter->second; |
| 1125 | + | if (!brush || as_lower_str(brush->getName()).find(search_string) == std::string::npos) { |
| 1126 | + | continue; |
| 1127 | + | } |
| 1128 | + | |
| 1129 | + | if (brush->isRaw()) { |
| 1130 | + | continue; |
| 1131 | + | } |
| 1132 | + | |
| 1133 | + | found_search_results = true; |
| 1134 | + | item_list->AddBrush(const_cast<Brush*>(brush)); |
| 1135 | + | } |
| 1136 | + | |
| 1137 | + | for (int id = 0; id <= g_items.getMaxID(); ++id) { |
| 1138 | + | ItemType& it = g_items[id]; |
| 1139 | + | if (it.id == 0) { |
| 1140 | + | continue; |
| 1141 | + | } |
| 1142 | + | |
| 1143 | + | RAWBrush* raw_brush = it.raw_brush; |
| 1144 | + | if (!raw_brush) { |
| 1145 | + | continue; |
| 1146 | + | } |
| 1147 | + | |
| 1148 | + | if (as_lower_str(raw_brush->getName()).find(search_string) == std::string::npos) { |
| 1149 | + | continue; |
| 1150 | + | } |
| 1151 | + | |
| 1152 | + | found_search_results = true; |
| 1153 | + | item_list->AddBrush(raw_brush); |
| 1154 | + | } |
| 1155 | + | } |
| 1156 | + | |
| 1157 | + | if (found_search_results) { |
| 1158 | + | item_list->SetSelection(0); |
| 1159 | + | } else { |
| 1160 | + | item_list->SetNoMatches(); |
| 1161 | + | } |
| 1162 | + | item_list->Refresh(); |
| 1163 | + | } |
| 1164 | + | |
| 1165 | + | // ============================================================================ |
| 1166 | + | // Listbox in find item / brush stuff |
| 1167 | + | |
| 1168 | + | FindDialogListBox::FindDialogListBox(wxWindow* parent, wxWindowID id) : |
| 1169 | + | wxVListBox(parent, id, wxDefaultPosition, wxDefaultSize, wxLB_SINGLE), |
| 1170 | + | cleared(false), |
| 1171 | + | no_matches(false) { |
| 1172 | + | Clear(); |
| 1173 | + | } |
| 1174 | + | |
| 1175 | + | FindDialogListBox::~FindDialogListBox() { |
| 1176 | + | //// |
| 1177 | + | } |
| 1178 | + | |
| 1179 | + | void FindDialogListBox::Clear() { |
| 1180 | + | cleared = true; |
| 1181 | + | no_matches = false; |
| 1182 | + | brushlist.clear(); |
| 1183 | + | SetItemCount(1); |
| 1184 | + | } |
| 1185 | + | |
| 1186 | + | void FindDialogListBox::SetNoMatches() { |
| 1187 | + | cleared = false; |
| 1188 | + | no_matches = true; |
| 1189 | + | brushlist.clear(); |
| 1190 | + | SetItemCount(1); |
| 1191 | + | } |
| 1192 | + | |
| 1193 | + | void FindDialogListBox::AddBrush(Brush* brush) { |
| 1194 | + | if (cleared || no_matches) { |
| 1195 | + | SetItemCount(0); |
| 1196 | + | } |
| 1197 | + | |
| 1198 | + | cleared = false; |
| 1199 | + | no_matches = false; |
| 1200 | + | |
| 1201 | + | SetItemCount(GetItemCount() + 1); |
| 1202 | + | brushlist.push_back(brush); |
| 1203 | + | } |
| 1204 | + | |
| 1205 | + | Brush* FindDialogListBox::GetSelectedBrush() { |
| 1206 | + | ssize_t n = GetSelection(); |
| 1207 | + | if (n == wxNOT_FOUND || no_matches || cleared) { |
| 1208 | + | return nullptr; |
| 1209 | + | } |
| 1210 | + | return brushlist[n]; |
| 1211 | + | } |
| 1212 | + | |
| 1213 | + | void FindDialogListBox::OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const { |
| 1214 | + | if (no_matches) { |
| 1215 | + | dc.DrawText("No matches for your search.", rect.GetX() + 40, rect.GetY() + 6); |
| 1216 | + | } else if (cleared) { |
| 1217 | + | dc.DrawText("Please enter your search string.", rect.GetX() + 40, rect.GetY() + 6); |
| 1218 | + | } else { |
| 1219 | + | ASSERT(n < brushlist.size()); |
| 1220 | + | Sprite* spr = g_gui.gfx.getSprite(brushlist[n]->getLookID()); |
| 1221 | + | if (spr) { |
| 1222 | + | spr->DrawTo(&dc, SPRITE_SIZE_32x32, rect.GetX(), rect.GetY(), rect.GetWidth(), rect.GetHeight()); |
| 1223 | + | } |
| 1224 | + | |
| 1225 | + | if (IsSelected(n)) { |
| 1226 | + | if (HasFocus()) { |
| 1227 | + | dc.SetTextForeground(wxColor(0xFF, 0xFF, 0xFF)); |
| 1228 | + | } else { |
| 1229 | + | dc.SetTextForeground(wxColor(0x00, 0x00, 0xFF)); |
| 1230 | + | } |
| 1231 | + | } else { |
| 1232 | + | dc.SetTextForeground(wxColor(0x00, 0x00, 0x00)); |
| 1233 | + | } |
| 1234 | + | |
| 1235 | + | dc.DrawText(wxstr(brushlist[n]->getName()), rect.GetX() + 40, rect.GetY() + 6); |
| 1236 | + | } |
| 1237 | + | } |
| 1238 | + | |
| 1239 | + | wxCoord FindDialogListBox::OnMeasureItem(size_t n) const { |
| 1240 | + | return 32; |
| 1241 | + | } |
| 1242 | + | |
| 1243 | + | // ============================================================================ |
| 1244 | + | // wxListBox that can be sorted |
| 1245 | + | |
| 1246 | + | SortableListBox::SortableListBox(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size) : |
| 1247 | + | wxListBox(parent, id, pos, size, 0, nullptr, wxLB_SINGLE | wxLB_NEEDED_SB) { } |
| 1248 | + | |
| 1249 | + | SortableListBox::SortableListBox(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, int n, const wxString choices[], long style) : |
| 1250 | + | wxListBox(parent, id, pos, size, n, choices, style) { } |
| 1251 | + | |
| 1252 | + | SortableListBox::~SortableListBox() { } |
| 1253 | + | |
| 1254 | + | void SortableListBox::Sort() { |
| 1255 | + | |
| 1256 | + | if (GetCount() == 0) { |
| 1257 | + | return; |
| 1258 | + | } |
| 1259 | + | |
| 1260 | + | wxASSERT_MSG(GetClientDataType() != wxClientData_Object, "Sorting a list with data of type wxClientData_Object is currently not implemented"); |
| 1261 | + | |
| 1262 | + | DoSort(); |
| 1263 | + | } |
| 1264 | + | |
| 1265 | + | void SortableListBox::DoSort() { |
| 1266 | + | size_t count = GetCount(); |
| 1267 | + | int selection = GetSelection(); |
| 1268 | + | wxClientDataType dataType = GetClientDataType(); |
| 1269 | + | |
| 1270 | + | wxArrayString stringList; |
| 1271 | + | wxArrayPtrVoid dataList; |
| 1272 | + | |
| 1273 | + | for (size_t i = 0; i < count; ++i) { |
| 1274 | + | stringList.Add(GetString(i)); |
| 1275 | + | if (dataType == wxClientData_Void) { |
| 1276 | + | dataList.Add(GetClientData(i)); |
| 1277 | + | } |
| 1278 | + | } |
| 1279 | + | |
| 1280 | + | // Insertion sort |
| 1281 | + | for (size_t i = 0; i < count; ++i) { |
| 1282 | + | size_t j = i; |
| 1283 | + | while (j > 0 && stringList[j].CmpNoCase(stringList[j - 1]) < 0) { |
| 1284 | + | |
| 1285 | + | wxString tmpString = stringList[j]; |
| 1286 | + | stringList[j] = stringList[j - 1]; |
| 1287 | + | stringList[j - 1] = tmpString; |
| 1288 | + | |
| 1289 | + | if (dataType == wxClientData_Void) { |
| 1290 | + | void* tmpData = dataList[j]; |
| 1291 | + | dataList[j] = dataList[j - 1]; |
| 1292 | + | dataList[j - 1] = tmpData; |
| 1293 | + | } |
| 1294 | + | |
| 1295 | + | if (selection == j - 1) { |
| 1296 | + | selection++; |
| 1297 | + | } else if (selection == j) { |
| 1298 | + | selection--; |
| 1299 | + | } |
| 1300 | + | |
| 1301 | + | j--; |
| 1302 | + | } |
| 1303 | + | } |
| 1304 | + | |
| 1305 | + | Freeze(); |
| 1306 | + | Clear(); |
| 1307 | + | for (size_t i = 0; i < count; ++i) { |
| 1308 | + | if (dataType == wxClientData_Void) { |
| 1309 | + | Append(stringList[i], dataList[i]); |
| 1310 | + | } else { |
| 1311 | + | Append(stringList[i]); |
| 1312 | + | } |
| 1313 | + | } |
| 1314 | + | Thaw(); |
| 1315 | + | |
| 1316 | + | SetSelection(selection); |
| 1317 | + | } |
| 1318 | + | |
| 1319 | + | // ============================================================================ |
| 1320 | + | // Object properties base |
| 1321 | + | |
| 1322 | + | ObjectPropertiesWindowBase::ObjectPropertiesWindowBase(wxWindow* parent, wxString title, const Map* map, const Tile* tile, Item* item, wxPoint position /* = wxDefaultPosition */) : |
| 1323 | + | wxDialog(parent, wxID_ANY, title, position, wxSize(600, 400), wxCAPTION | wxCLOSE_BOX | wxRESIZE_BORDER), |
| 1324 | + | edit_map(map), |
| 1325 | + | edit_tile(tile), |
| 1326 | + | edit_item(item), |
| 1327 | + | edit_creature(nullptr), |
| 1328 | + | edit_spawn(nullptr) { |
| 1329 | + | //// |
| 1330 | + | } |
| 1331 | + | |
| 1332 | + | ObjectPropertiesWindowBase::ObjectPropertiesWindowBase(wxWindow* parent, wxString title, const Map* map, const Tile* tile, Creature* creature, wxPoint position /* = wxDefaultPosition */) : |
| 1333 | + | wxDialog(parent, wxID_ANY, title, position, wxSize(600, 400), wxCAPTION | wxCLOSE_BOX | wxRESIZE_BORDER), |
| 1334 | + | edit_map(map), |
| 1335 | + | edit_tile(tile), |
| 1336 | + | edit_item(nullptr), |
| 1337 | + | edit_creature(creature), |
| 1338 | + | edit_spawn(nullptr) { |
| 1339 | + | //// |
| 1340 | + | } |
| 1341 | + | |
| 1342 | + | ObjectPropertiesWindowBase::ObjectPropertiesWindowBase(wxWindow* parent, wxString title, const Map* map, const Tile* tile, Spawn* spawn, wxPoint position /* = wxDefaultPosition */) : |
| 1343 | + | wxDialog(parent, wxID_ANY, title, position, wxSize(600, 400), wxCAPTION | wxCLOSE_BOX | wxRESIZE_BORDER), |
| 1344 | + | edit_map(map), |
| 1345 | + | edit_tile(tile), |
| 1346 | + | edit_item(nullptr), |
| 1347 | + | edit_creature(nullptr), |
| 1348 | + | edit_spawn(spawn) { |
| 1349 | + | //// |
| 1350 | + | } |
| 1351 | + | |
| 1352 | + | ObjectPropertiesWindowBase::ObjectPropertiesWindowBase(wxWindow* parent, wxString title, wxPoint position /* = wxDefaultPosition */) : |
| 1353 | + | wxDialog(parent, wxID_ANY, title, position, wxSize(600, 400), wxCAPTION | wxCLOSE_BOX | wxRESIZE_BORDER) { |
| 1354 | + | //// |
| 1355 | + | } |
| 1356 | + | |
| 1357 | + | Item* ObjectPropertiesWindowBase::getItemBeingEdited() { |
| 1358 | + | return edit_item; |
| 1359 | + | } |
| 1360 | + | |
| 1361 | + | // ============================================================================ |
| 1362 | + | // Edit Towns Dialog |
| 1363 | + | |
| 1364 | + | BEGIN_EVENT_TABLE(EditTownsDialog, wxDialog) |
| 1365 | + | EVT_LISTBOX(EDIT_TOWNS_LISTBOX, EditTownsDialog::OnListBoxChange) |
| 1366 | + | |
| 1367 | + | EVT_BUTTON(EDIT_TOWNS_SELECT_TEMPLE, EditTownsDialog::OnClickSelectTemplePosition) |
| 1368 | + | EVT_BUTTON(EDIT_TOWNS_ADD, EditTownsDialog::OnClickAdd) |
| 1369 | + | EVT_BUTTON(EDIT_TOWNS_REMOVE, EditTownsDialog::OnClickRemove) |
| 1370 | + | EVT_BUTTON(wxID_OK, EditTownsDialog::OnClickOK) |
| 1371 | + | EVT_BUTTON(wxID_CANCEL, EditTownsDialog::OnClickCancel) |
| 1372 | + | EVT_BUTTON(EDIT_TOWNS_EXPORT, EditTownsDialog::OnClickExport) |
| 1373 | + | EVT_BUTTON(EDIT_TOWNS_IMPORT, EditTownsDialog::OnClickImport) |
| 1374 | + | END_EVENT_TABLE() |
| 1375 | + | |
| 1376 | + | EditTownsDialog::EditTownsDialog(wxWindow* parent, Editor& editor) : |
| 1377 | + | wxDialog(parent, wxID_ANY, "Towns", wxDefaultPosition, wxSize(280, 380)), |
| 1378 | + | editor(editor) |
| 1379 | + | { |
| 1380 | + | Map& map = editor.map; |
| 1381 | + | |
| 1382 | + | // Create topsizer |
| 1383 | + | wxSizer* sizer = newd wxBoxSizer(wxVERTICAL); |
| 1384 | + | wxSizer* tmpsizer; |
| 1385 | + | |
| 1386 | + | for (TownMap::const_iterator town_iter = map.towns.begin(); town_iter != map.towns.end(); ++town_iter) { |
| 1387 | + | Town* town = town_iter->second; |
| 1388 | + | town_list.push_back(newd Town(*town)); |
| 1389 | + | if (max_town_id < town->getID()) { |
| 1390 | + | max_town_id = town->getID(); |
| 1391 | + | } |
| 1392 | + | } |
| 1393 | + | |
| 1394 | + | // Town list |
| 1395 | + | town_listbox = newd wxListBox(this, EDIT_TOWNS_LISTBOX, wxDefaultPosition, wxSize(240, 100)); |
| 1396 | + | sizer->Add(town_listbox, 1, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, 10); |
| 1397 | + | |
| 1398 | + | tmpsizer = newd wxBoxSizer(wxHORIZONTAL); |
| 1399 | + | tmpsizer->Add(newd wxButton(this, EDIT_TOWNS_ADD, "Add"), 0, wxTOP, 5); |
| 1400 | + | tmpsizer->Add(remove_button = newd wxButton(this, EDIT_TOWNS_REMOVE, "Remove"), 0, wxRIGHT | wxTOP, 5); |
| 1401 | + | sizer->Add(tmpsizer, 0, wxEXPAND | wxLEFT | wxRIGHT, 10); |
| 1402 | + | |
| 1403 | + | // House options - town name and ID |
| 1404 | + | tmpsizer = newd wxStaticBoxSizer(wxHORIZONTAL, this, "Name / ID"); |
| 1405 | + | name_field = newd wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxSize(190, 20), 0, wxTextValidator(wxFILTER_ASCII, &town_name)); |
| 1406 | + | tmpsizer->Add(name_field, 2, wxEXPAND | wxLEFT | wxBOTTOM, 5); |
| 1407 | + | |
| 1408 | + | id_field = newd wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxSize(40, 20), 0, wxTextValidator(wxFILTER_NUMERIC, &town_id)); |
| 1409 | + | id_field->Enable(false); |
| 1410 | + | tmpsizer->Add(id_field, 1, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 5); |
| 1411 | + | sizer->Add(tmpsizer, 0, wxEXPAND | wxALL, 10); |
| 1412 | + | |
| 1413 | + | // Temple position section |
| 1414 | + | // Create the PositionCtrl for temple position entry. |
| 1415 | + | // Note: PositionCtrl is a sizer so we cannot bind events directly to it. |
| 1416 | + | temple_position = newd PositionCtrl(this, "Temple Position", 0, 0, 0, map.getWidth(), map.getHeight()); |
| 1417 | + | select_position_button = newd wxButton(this, EDIT_TOWNS_SELECT_TEMPLE, "Go To"); |
| 1418 | + | temple_position->Add(select_position_button, 0, wxLEFT | wxRIGHT | wxBOTTOM, 5); |
| 1419 | + | sizer->Add(temple_position, 0, wxEXPAND | wxLEFT | wxRIGHT, 10); |
| 1420 | + | |
| 1421 | + | // --- New: Extra text control for seamless temple position pasting --- |
| 1422 | + | // This control accepts a full position string (in any supported format) and updates the temple position fields. |
| 1423 | + | paste_temple_field = newd wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxSize(200, -1), wxTE_PROCESS_ENTER); |
| 1424 | + | paste_temple_field->SetHint("Paste temple position (e.g., {x = 0, y = 0, z = 0})"); |
| 1425 | + | paste_temple_field->Bind(wxEVT_TEXT, &EditTownsDialog::OnPasteTempleText, this); |
| 1426 | + | sizer->Add(paste_temple_field, 0, wxEXPAND | wxALL, 5); |
| 1427 | + | // --- End new section --- |
| 1428 | + | |
| 1429 | + | // OK/Cancel buttons |
| 1430 | + | tmpsizer = newd wxBoxSizer(wxHORIZONTAL); |
| 1431 | + | tmpsizer->Add(newd wxButton(this, wxID_OK, "OK"), wxSizerFlags(1).Center()); |
| 1432 | + | tmpsizer->Add(newd wxButton(this, wxID_CANCEL, "Cancel"), wxSizerFlags(1).Center()); |
| 1433 | + | sizer->Add(tmpsizer, 0, wxCENTER | wxALL, 10); |
| 1434 | + | |
| 1435 | + | // Add Import/Export buttons |
| 1436 | + | wxBoxSizer* importExportSizer = new wxBoxSizer(wxHORIZONTAL); |
| 1437 | + | importExportSizer->Add(new wxButton(this, EDIT_TOWNS_IMPORT, "Import XML"), 0, wxRIGHT, 5); |
| 1438 | + | importExportSizer->Add(new wxButton(this, EDIT_TOWNS_EXPORT, "Export XML"), 0, wxLEFT, 5); |
| 1439 | + | sizer->Add(importExportSizer, 0, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 10); |
| 1440 | + | |
| 1441 | + | SetSizerAndFit(sizer); |
| 1442 | + | Centre(wxBOTH); |
| 1443 | + | BuildListBox(true); |
| 1444 | + | } |
| 1445 | + | |
| 1446 | + | EditTownsDialog::~EditTownsDialog() { |
| 1447 | + | for (std::vector<Town*>::iterator town_iter = town_list.begin(); town_iter != town_list.end(); ++town_iter) { |
| 1448 | + | delete *town_iter; |
| 1449 | + | } |
| 1450 | + | } |
| 1451 | + | |
| 1452 | + | void EditTownsDialog::BuildListBox(bool doselect) { |
| 1453 | + | long tmplong = 0; |
| 1454 | + | max_town_id = 0; |
| 1455 | + | wxArrayString town_name_list; |
| 1456 | + | uint32_t selection_before = 0; |
| 1457 | + | |
| 1458 | + | if (doselect && id_field->GetValue().ToLong(&tmplong)) { |
| 1459 | + | uint32_t old_town_id = tmplong; |
| 1460 | + | |
| 1461 | + | for (std::vector<Town*>::iterator town_iter = town_list.begin(); town_iter != town_list.end(); ++town_iter) { |
| 1462 | + | if (old_town_id == (*town_iter)->getID()) { |
| 1463 | + | selection_before = (*town_iter)->getID(); |
| 1464 | + | break; |
| 1465 | + | } |
| 1466 | + | } |
| 1467 | + | } |
| 1468 | + | |
| 1469 | + | for (std::vector<Town*>::iterator town_iter = town_list.begin(); town_iter != town_list.end(); ++town_iter) { |
| 1470 | + | Town* town = *town_iter; |
| 1471 | + | town_name_list.Add(wxstr(town->getName())); |
| 1472 | + | if (max_town_id < town->getID()) { |
| 1473 | + | max_town_id = town->getID(); |
| 1474 | + | } |
| 1475 | + | } |
| 1476 | + | |
| 1477 | + | town_listbox->Set(town_name_list); |
| 1478 | + | remove_button->Enable(town_listbox->GetCount() != 0); |
| 1479 | + | select_position_button->Enable(false); |
| 1480 | + | |
| 1481 | + | if (doselect) { |
| 1482 | + | if (selection_before) { |
| 1483 | + | int i = 0; |
| 1484 | + | for (std::vector<Town*>::iterator town_iter = town_list.begin(); town_iter != town_list.end(); ++town_iter) { |
| 1485 | + | if (selection_before == (*town_iter)->getID()) { |
| 1486 | + | town_listbox->SetSelection(i); |
| 1487 | + | return; |
| 1488 | + | } |
| 1489 | + | ++i; |
| 1490 | + | } |
| 1491 | + | } |
| 1492 | + | UpdateSelection(0); |
| 1493 | + | } |
| 1494 | + | } |
| 1495 | + | |
| 1496 | + | void EditTownsDialog::UpdateSelection(int new_selection) { |
| 1497 | + | long tmplong; |
| 1498 | + | |
| 1499 | + | // Save old values |
| 1500 | + | if (town_list.size() > 0) { |
| 1501 | + | if (id_field->GetValue().ToLong(&tmplong)) { |
| 1502 | + | uint32_t old_town_id = tmplong; |
| 1503 | + | |
| 1504 | + | Town* old_town = nullptr; |
| 1505 | + | |
| 1506 | + | for (std::vector<Town*>::iterator town_iter = town_list.begin(); town_iter != town_list.end(); ++town_iter) { |
| 1507 | + | if (old_town_id == (*town_iter)->getID()) { |
| 1508 | + | old_town = *town_iter; |
| 1509 | + | break; |
| 1510 | + | } |
| 1511 | + | } |
| 1512 | + | |
| 1513 | + | if (old_town) { |
| 1514 | + | editor.map.getOrCreateTile(old_town->getTemplePosition())->getLocation()->decreaseTownCount(); |
| 1515 | + | |
| 1516 | + | Position templePos = temple_position->GetPosition(); |
| 1517 | + | |
| 1518 | + | editor.map.getOrCreateTile(templePos)->getLocation()->increaseTownCount(); |
| 1519 | + | |
| 1520 | + | // printf("Changed town %d:%s\n", old_town_id, old_town->getName().c_str()); |
| 1521 | + | // printf("New values %d:%s:%d:%d:%d\n", town_id, town_name.c_str(), templepos.x, templepos.y, templepos.z); |
| 1522 | + | old_town->setTemplePosition(templePos); |
| 1523 | + | |
| 1524 | + | wxString new_name = name_field->GetValue(); |
| 1525 | + | wxString old_name = wxstr(old_town->getName()); |
| 1526 | + | |
| 1527 | + | old_town->setName(nstr(new_name)); |
| 1528 | + | if (new_name != old_name) { |
| 1529 | + | // Name has changed, update list |
| 1530 | + | BuildListBox(false); |
| 1531 | + | } |
| 1532 | + | } |
| 1533 | + | } |
| 1534 | + | } |
| 1535 | + | |
| 1536 | + | // Clear fields |
| 1537 | + | town_name.Clear(); |
| 1538 | + | town_id.Clear(); |
| 1539 | + | |
| 1540 | + | if (town_list.size() > size_t(new_selection)) { |
| 1541 | + | name_field->Enable(true); |
| 1542 | + | temple_position->Enable(true); |
| 1543 | + | select_position_button->Enable(true); |
| 1544 | + | |
| 1545 | + | // Change the values to reflect the newd selection |
| 1546 | + | Town* town = town_list[new_selection]; |
| 1547 | + | ASSERT(town); |
| 1548 | + | |
| 1549 | + | // printf("Selected %d:%s\n", new_selection, town->getName().c_str()); |
| 1550 | + | town_name << wxstr(town->getName()); |
| 1551 | + | name_field->SetValue(town_name); |
| 1552 | + | town_id << long(town->getID()); |
| 1553 | + | id_field->SetValue(town_id); |
| 1554 | + | temple_position->SetPosition(town->getTemplePosition()); |
| 1555 | + | town_listbox->SetSelection(new_selection); |
| 1556 | + | } else { |
| 1557 | + | name_field->Enable(false); |
| 1558 | + | temple_position->Enable(false); |
| 1559 | + | select_position_button->Enable(false); |
| 1560 | + | } |
| 1561 | + | Refresh(); |
| 1562 | + | } |
| 1563 | + | |
| 1564 | + | void EditTownsDialog::OnListBoxChange(wxCommandEvent& event) { |
| 1565 | + | UpdateSelection(event.GetSelection()); |
| 1566 | + | } |
| 1567 | + | |
| 1568 | + | void EditTownsDialog::OnClickSelectTemplePosition(wxCommandEvent& WXUNUSED(event)) { |
| 1569 | + | Position templepos = temple_position->GetPosition(); |
| 1570 | + | g_gui.SetScreenCenterPosition(templepos); |
| 1571 | + | } |
| 1572 | + | |
| 1573 | + | void EditTownsDialog::OnClickAdd(wxCommandEvent& WXUNUSED(event)) { |
| 1574 | + | Town* new_town = newd Town(++max_town_id); |
| 1575 | + | new_town->setName("Unnamed Town"); |
| 1576 | + | new_town->setTemplePosition(Position(0, 0, 0)); |
| 1577 | + | town_list.push_back(new_town); |
| 1578 | + | |
| 1579 | + | editor.map.getOrCreateTile(Position(0, 0, 0))->getLocation()->increaseTownCount(); |
| 1580 | + | |
| 1581 | + | BuildListBox(false); |
| 1582 | + | UpdateSelection(town_list.size() - 1); |
| 1583 | + | town_listbox->SetSelection(town_list.size() - 1); |
| 1584 | + | } |
| 1585 | + | |
| 1586 | + | void EditTownsDialog::OnClickRemove(wxCommandEvent& WXUNUSED(event)) { |
| 1587 | + | int current_selection = town_listbox->GetSelection(); |
| 1588 | + | |
| 1589 | + | if (current_selection == wxNOT_FOUND) { |
| 1590 | + | return; |
| 1591 | + | } |
| 1592 | + | |
| 1593 | + | // Clear the current selection to prevent interference |
| 1594 | + | town_listbox->SetSelection(wxNOT_FOUND); |
| 1595 | + | name_field->Clear(); |
| 1596 | + | id_field->Clear(); |
| 1597 | + | temple_position->SetPosition(Position(0, 0, 0)); |
| 1598 | + | |
| 1599 | + | // Create and populate choices for the dialog |
| 1600 | + | wxArrayString choices; |
| 1601 | + | for (size_t i = 0; i < town_list.size(); i++) { |
| 1602 | + | wxString choice = wxString::Format("%s (ID: %d)", |
| 1603 | + | wxstr(town_list[i]->getName()).c_str(), |
| 1604 | + | town_list[i]->getID()); |
| 1605 | + | choices.Add(choice); |
| 1606 | + | } |
| 1607 | + | |
| 1608 | + | // Create custom dialog with dropdown |
| 1609 | + | wxDialog* dialog = new wxDialog(this, wxID_ANY, "Remove Town", |
| 1610 | + | wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE); |
| 1611 | + | |
| 1612 | + | wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL); |
| 1613 | + | sizer->Add(new wxStaticText(dialog, wxID_ANY, "Select town to remove:"), |
| 1614 | + | 0, wxALL, 5); |
| 1615 | + | |
| 1616 | + | wxChoice* townChoice = new wxChoice(dialog, wxID_ANY, |
| 1617 | + | wxDefaultPosition, wxDefaultSize, choices); |
| 1618 | + | townChoice->SetSelection(current_selection); |
| 1619 | + | sizer->Add(townChoice, 0, wxEXPAND | wxALL, 5); |
| 1620 | + | |
| 1621 | + | wxBoxSizer* buttonSizer = new wxBoxSizer(wxHORIZONTAL); |
| 1622 | + | buttonSizer->Add(new wxButton(dialog, wxID_OK, "OK"), 0, wxALL, 5); |
| 1623 | + | buttonSizer->Add(new wxButton(dialog, wxID_CANCEL, "Cancel"), 0, wxALL, 5); |
| 1624 | + | sizer->Add(buttonSizer, 0, wxALIGN_CENTER | wxALL, 5); |
| 1625 | + | |
| 1626 | + | dialog->SetSizer(sizer); |
| 1627 | + | dialog->Fit(); |
| 1628 | + | |
| 1629 | + | if (dialog->ShowModal() == wxID_OK) { |
| 1630 | + | int selected_town_index = townChoice->GetSelection(); |
| 1631 | + | if (selected_town_index != wxNOT_FOUND) { |
| 1632 | + | Town* town_to_remove = town_list[selected_town_index]; |
| 1633 | + | uint32_t town_id_to_remove = town_to_remove->getID(); |
| 1634 | + | |
| 1635 | + | // Count houses in this town |
| 1636 | + | int house_count = 0; |
| 1637 | + | for (HouseMap::iterator house_iter = editor.map.houses.begin(); |
| 1638 | + | house_iter != editor.map.houses.end(); ++house_iter) { |
| 1639 | + | if (house_iter->second->townid == town_id_to_remove) { |
| 1640 | + | house_count++; |
| 1641 | + | } |
| 1642 | + | } |
| 1643 | + | |
| 1644 | + | if (house_count > 0) { |
| 1645 | + | wxString msg; |
| 1646 | + | msg << "This town has " << house_count << " house" << (house_count > 1 ? "s" : "") |
| 1647 | + | << " that will be removed.\n\nDo you want to remove the houses?"; |
| 1648 | + | |
| 1649 | + | if (g_gui.PopupDialog(this, "Remove Houses", msg, wxYES | wxNO) == wxID_NO) { |
| 1650 | + | dialog->Destroy(); |
| 1651 | + | UpdateSelection(current_selection); |
| 1652 | + | return; |
| 1653 | + | } |
| 1654 | + | |
| 1655 | + | // Remove houses |
| 1656 | + | HouseMap::iterator house_iter = editor.map.houses.begin(); |
| 1657 | + | while (house_iter != editor.map.houses.end()) { |
| 1658 | + | if (house_iter->second->townid == town_id_to_remove) { |
| 1659 | + | House* house = house_iter->second; |
| 1660 | + | house->clean(); |
| 1661 | + | editor.map.houses.removeHouse(house); |
| 1662 | + | house_iter = editor.map.houses.begin(); |
| 1663 | + | } else { |
| 1664 | + | ++house_iter; |
| 1665 | + | } |
| 1666 | + | } |
| 1667 | + | } |
| 1668 | + | |
| 1669 | + | // Remove town flag from tile |
| 1670 | + | editor.map.getOrCreateTile(town_to_remove->getTemplePosition()) |
| 1671 | + | ->getLocation()->decreaseTownCount(); |
| 1672 | + | |
| 1673 | + | // Store old ID and remove the town |
| 1674 | + | uint32_t removed_id = town_to_remove->getID(); |
| 1675 | + | town_list.erase(town_list.begin() + selected_town_index); |
| 1676 | + | delete town_to_remove; |
| 1677 | + | |
| 1678 | + | // Reindex remaining towns and update their houses |
| 1679 | + | for (size_t i = 0; i < town_list.size(); i++) { |
| 1680 | + | Town* remaining_town = town_list[i]; |
| 1681 | + | if (remaining_town->getID() > removed_id) { |
| 1682 | + | uint32_t new_id = remaining_town->getID() - 1; |
| 1683 | + | // Update houses to point to the new town ID |
| 1684 | + | for (HouseMap::iterator house_iter = editor.map.houses.begin(); |
| 1685 | + | house_iter != editor.map.houses.end(); ++house_iter) { |
| 1686 | + | if (house_iter->second->townid == remaining_town->getID()) { |
| 1687 | + | house_iter->second->townid = new_id; |
| 1688 | + | } |
| 1689 | + | } |
| 1690 | + | remaining_town->setID(new_id); |
| 1691 | + | } |
| 1692 | + | } |
| 1693 | + | |
| 1694 | + | max_town_id = town_list.size(); |
| 1695 | + | BuildListBox(false); |
| 1696 | + | |
| 1697 | + | if (selected_town_index >= int(town_list.size())) { |
| 1698 | + | selected_town_index = town_list.size() - 1; |
| 1699 | + | } |
| 1700 | + | UpdateSelection(selected_town_index); |
| 1701 | + | } |
| 1702 | + | } else { |
| 1703 | + | UpdateSelection(current_selection); |
| 1704 | + | } |
| 1705 | + | |
| 1706 | + | dialog->Destroy(); |
| 1707 | + | } |
| 1708 | + | |
| 1709 | + | void EditTownsDialog::OnClickOK(wxCommandEvent& WXUNUSED(event)) { |
| 1710 | + | long tmplong = 0; |
| 1711 | + | |
| 1712 | + | if (Validate() && TransferDataFromWindow()) { |
| 1713 | + | // Save old values |
| 1714 | + | if (town_list.size() > 0 && id_field->GetValue().ToLong(&tmplong)) { |
| 1715 | + | uint32_t old_town_id = tmplong; |
| 1716 | + | |
| 1717 | + | Town* old_town = nullptr; |
| 1718 | + | |
| 1719 | + | for (std::vector<Town*>::iterator town_iter = town_list.begin(); town_iter != town_list.end(); ++town_iter) { |
| 1720 | + | if (old_town_id == (*town_iter)->getID()) { |
| 1721 | + | old_town = *town_iter; |
| 1722 | + | break; |
| 1723 | + | } |
| 1724 | + | } |
| 1725 | + | |
| 1726 | + | if (old_town) { |
| 1727 | + | editor.map.getOrCreateTile(old_town->getTemplePosition())->getLocation()->decreaseTownCount(); |
| 1728 | + | |
| 1729 | + | Position templePos = temple_position->GetPosition(); |
| 1730 | + | |
| 1731 | + | editor.map.getOrCreateTile(templePos)->getLocation()->increaseTownCount(); |
| 1732 | + | |
| 1733 | + | // printf("Changed town %d:%s\n", old_town_id, old_town->getName().c_str()); |
| 1734 | + | // printf("New values %d:%s:%d:%d:%d\n", town_id, town_name.c_str(), templepos.x, templepos.y, templepos.z); |
| 1735 | + | old_town->setTemplePosition(templePos); |
| 1736 | + | |
| 1737 | + | wxString new_name = name_field->GetValue(); |
| 1738 | + | wxString old_name = wxstr(old_town->getName()); |
| 1739 | + | |
| 1740 | + | old_town->setName(nstr(new_name)); |
| 1741 | + | if (new_name != old_name) { |
| 1742 | + | // Name has changed, update list |
| 1743 | + | BuildListBox(true); |
| 1744 | + | } |
| 1745 | + | } |
| 1746 | + | } |
| 1747 | + | |
| 1748 | + | Towns& towns = editor.map.towns; |
| 1749 | + | |
| 1750 | + | // Verify the newd information |
| 1751 | + | for (std::vector<Town*>::iterator town_iter = town_list.begin(); town_iter != town_list.end(); ++town_iter) { |
| 1752 | + | Town* town = *town_iter; |
| 1753 | + | if (town->getName() == "") { |
| 1754 | + | g_gui.PopupDialog(this, "Error", "You can't have a town with an empty name.", wxOK); |
| 1755 | + | return; |
| 1756 | + | } |
| 1757 | + | if (!town->getTemplePosition().isValid() || town->getTemplePosition().x > editor.map.getWidth() || town->getTemplePosition().y > editor.map.getHeight()) { |
| 1758 | + | wxString msg; |
| 1759 | + | msg << "The town " << wxstr(town->getName()) << " has an invalid temple position."; |
| 1760 | + | g_gui.PopupDialog(this, "Error", msg, wxOK); |
| 1761 | + | return; |
| 1762 | + | } |
| 1763 | + | } |
| 1764 | + | |
| 1765 | + | // Clear old towns |
| 1766 | + | towns.clear(); |
| 1767 | + | |
| 1768 | + | // Build the newd town map |
| 1769 | + | for (std::vector<Town*>::iterator town_iter = town_list.begin(); town_iter != town_list.end(); ++town_iter) { |
| 1770 | + | towns.addTown(*town_iter); |
| 1771 | + | } |
| 1772 | + | town_list.clear(); |
| 1773 | + | editor.map.doChange(); |
| 1774 | + | |
| 1775 | + | EndModal(1); |
| 1776 | + | g_gui.RefreshPalettes(); |
| 1777 | + | } |
| 1778 | + | } |
| 1779 | + | |
| 1780 | + | void EditTownsDialog::OnClickCancel(wxCommandEvent& WXUNUSED(event)) { |
| 1781 | + | // Just close this window |
| 1782 | + | EndModal(0); |
| 1783 | + | } |
| 1784 | + | |
| 1785 | + | void EditTownsDialog::OnPasteTempleText(wxCommandEvent& event) { |
| 1786 | + | Position pos; |
| 1787 | + | // Retrieve pasted text from the additional paste field. |
| 1788 | + | std::string input = paste_temple_field->GetValue().ToStdString(); |
| 1789 | + | // Attempt to parse input using supported position formats. |
| 1790 | + | if(posFromClipboard(pos, editor.map.getWidth(), editor.map.getHeight(), input)) { |
| 1791 | + | // Successfully parsed position; update the temple position fields. |
| 1792 | + | temple_position->SetPosition(pos); |
| 1793 | + | // Optionally, clear the paste field after successful parsing. |
| 1794 | + | paste_temple_field->Clear(); |
| 1795 | + | } |
| 1796 | + | event.Skip(); |
| 1797 | + | } |
| 1798 | + | |
| 1799 | + | void EditTownsDialog::OnClickExport(wxCommandEvent& WXUNUSED(event)) { |
| 1800 | + | int selection = town_listbox->GetSelection(); |
| 1801 | + | if (selection == wxNOT_FOUND) { |
| 1802 | + | g_gui.PopupDialog(this, "Error", "Please select a town to export.", wxOK); |
| 1803 | + | return; |
| 1804 | + | } |
| 1805 | + | |
| 1806 | + | Town* town = town_list[selection]; |
| 1807 | + | wxString defaultFileName = wxString::Format("%s.xml", wxstr(town->getName())); |
| 1808 | + | |
| 1809 | + | wxFileDialog dialog(this, "Export Town to XML", "", defaultFileName, |
| 1810 | + | "XML files (*.xml)|*.xml", wxFD_SAVE | wxFD_OVERWRITE_PROMPT); |
| 1811 | + | |
| 1812 | + | if (dialog.ShowModal() == wxID_OK) { |
| 1813 | + | ExportTownToXML(dialog.GetPath(), town); |
| 1814 | + | } |
| 1815 | + | } |
| 1816 | + | |
| 1817 | + | void EditTownsDialog::ExportTownToXML(const wxString& path, Town* town) { |
| 1818 | + | pugi::xml_document doc; |
| 1819 | + | pugi::xml_node root = doc.append_child("town"); |
| 1820 | + | |
| 1821 | + | // Export basic town information. |
| 1822 | + | root.append_attribute("id") = town->getID(); |
| 1823 | + | root.append_attribute("name") = town->getName().c_str(); |
| 1824 | + | |
| 1825 | + | Position temple_pos = town->getTemplePosition(); |
| 1826 | + | pugi::xml_node temple = root.append_child("temple"); |
| 1827 | + | temple.append_attribute("x") = temple_pos.x; |
| 1828 | + | temple.append_attribute("y") = temple_pos.y; |
| 1829 | + | temple.append_attribute("z") = temple_pos.z; |
| 1830 | + | |
| 1831 | + | // Export houses that belong to the selected town. |
| 1832 | + | pugi::xml_node houses = root.append_child("houses"); |
| 1833 | + | for (HouseMap::iterator house_iter = editor.map.houses.begin(); |
| 1834 | + | house_iter != editor.map.houses.end(); ++house_iter) { |
| 1835 | + | House* house = house_iter->second; |
| 1836 | + | if (house->townid == town->getID()) { |
| 1837 | + | pugi::xml_node house_node = houses.append_child("house"); |
| 1838 | + | house_node.append_attribute("id") = house->getID(); |
| 1839 | + | house_node.append_attribute("name") = house->name.c_str(); |
| 1840 | + | |
| 1841 | + | Position exit = house->getExit(); |
| 1842 | + | pugi::xml_node exit_node = house_node.append_child("exit"); |
| 1843 | + | exit_node.append_attribute("x") = exit.x; |
| 1844 | + | exit_node.append_attribute("y") = exit.y; |
| 1845 | + | exit_node.append_attribute("z") = exit.z; |
| 1846 | + | |
| 1847 | + | // Export all tile positions associated with this house. |
| 1848 | + | pugi::xml_node tiles = house_node.append_child("tiles"); |
| 1849 | + | const auto& tilePositions = house->getTilePositions(); |
| 1850 | + | for (auto it = tilePositions.begin(); it != tilePositions.end(); ++it) { |
| 1851 | + | const Position& pos = *it; |
| 1852 | + | pugi::xml_node tile = tiles.append_child("tile"); |
| 1853 | + | tile.append_attribute("x") = pos.x; |
| 1854 | + | tile.append_attribute("y") = pos.y; |
| 1855 | + | tile.append_attribute("z") = pos.z; |
| 1856 | + | } |
| 1857 | + | } |
| 1858 | + | } |
| 1859 | + | |
| 1860 | + | if (!doc.save_file(path.mb_str())) { |
| 1861 | + | g_gui.PopupDialog(this, "Error", "Failed to save town XML file.", wxOK); |
| 1862 | + | } |
| 1863 | + | } |
| 1864 | + | |
| 1865 | + | void EditTownsDialog::OnClickImport(wxCommandEvent& WXUNUSED(event)) { |
| 1866 | + | wxFileDialog dialog(this, "Import Town from XML", "", "", |
| 1867 | + | "XML files (*.xml)|*.xml", wxFD_OPEN | wxFD_FILE_MUST_EXIST); |
| 1868 | + | |
| 1869 | + | if (dialog.ShowModal() == wxID_OK) { |
| 1870 | + | ImportTownFromXML(dialog.GetPath()); |
| 1871 | + | } |
| 1872 | + | } |
| 1873 | + | |
| 1874 | + | void EditTownsDialog::ImportTownFromXML(const wxString& path) { |
| 1875 | + | pugi::xml_document doc; |
| 1876 | + | if (!doc.load_file(path.mb_str())) { |
| 1877 | + | g_gui.PopupDialog(this, "Error", "Failed to load town XML file.", wxOK); |
| 1878 | + | return; |
| 1879 | + | } |
| 1880 | + | |
| 1881 | + | pugi::xml_node root = doc.child("town"); |
| 1882 | + | if (!root) { |
| 1883 | + | g_gui.PopupDialog(this, "Error", "Invalid town XML format.", wxOK); |
| 1884 | + | return; |
| 1885 | + | } |
| 1886 | + | |
| 1887 | + | // Create new town with next available ID |
| 1888 | + | uint32_t new_town_id = editor.map.towns.getEmptyID(); |
| 1889 | + | Town* new_town = new Town(new_town_id); |
| 1890 | + | |
| 1891 | + | new_town->setName(root.attribute("name").as_string()); |
| 1892 | + | |
| 1893 | + | pugi::xml_node temple = root.child("temple"); |
| 1894 | + | if (temple) { |
| 1895 | + | Position temple_pos( |
| 1896 | + | temple.attribute("x").as_int(), |
| 1897 | + | temple.attribute("y").as_int(), |
| 1898 | + | temple.attribute("z").as_int() |
| 1899 | + | ); |
| 1900 | + | new_town->setTemplePosition(temple_pos); |
| 1901 | + | } |
| 1902 | + | |
| 1903 | + | // Import houses |
| 1904 | + | pugi::xml_node houses = root.child("houses"); |
| 1905 | + | if (houses) { |
| 1906 | + | for (pugi::xml_node house_node = houses.child("house"); |
| 1907 | + | house_node; house_node = house_node.next_sibling("house")) { |
| 1908 | + | |
| 1909 | + | uint32_t new_house_id = editor.map.houses.getEmptyID(); |
| 1910 | + | House* new_house = newd House(editor.map); |
| 1911 | + | new_house->setID(new_house_id); |
| 1912 | + | new_house->townid = new_town_id; |
| 1913 | + | new_house->name = house_node.attribute("name").as_string(); |
| 1914 | + | |
| 1915 | + | pugi::xml_node exit = house_node.child("exit"); |
| 1916 | + | if (exit) { |
| 1917 | + | Position exit_pos( |
| 1918 | + | exit.attribute("x").as_int(), |
| 1919 | + | exit.attribute("y").as_int(), |
| 1920 | + | exit.attribute("z").as_int() |
| 1921 | + | ); |
| 1922 | + | new_house->setExit(exit_pos); |
| 1923 | + | } |
| 1924 | + | |
| 1925 | + | // Import house tiles |
| 1926 | + | pugi::xml_node tiles = house_node.child("tiles"); |
| 1927 | + | for (pugi::xml_node tile = tiles.child("tile"); |
| 1928 | + | tile; tile = tile.next_sibling("tile")) { |
| 1929 | + | Position pos( |
| 1930 | + | tile.attribute("x").as_int(), |
| 1931 | + | tile.attribute("y").as_int(), |
| 1932 | + | tile.attribute("z").as_int() |
| 1933 | + | ); |
| 1934 | + | Tile* map_tile = editor.map.getOrCreateTile(pos); |
| 1935 | + | if (map_tile) { |
| 1936 | + | new_house->addTile(map_tile); |
| 1937 | + | } |
| 1938 | + | } |
| 1939 | + | |
| 1940 | + | editor.map.houses.addHouse(new_house); |
| 1941 | + | } |
| 1942 | + | } |
| 1943 | + | |
| 1944 | + | // Add town to map and update UI |
| 1945 | + | town_list.push_back(new_town); |
| 1946 | + | editor.map.towns.addTown(new_town); |
| 1947 | + | max_town_id = std::max(max_town_id, new_town_id); |
| 1948 | + | |
| 1949 | + | BuildListBox(true); |
| 1950 | + | UpdateSelection(town_list.size() - 1); |
| 1951 | + | } |
| 1952 | + | |
| 1953 | + | // ============================================================================ |
| 1954 | + | // Go To Position Dialog |
| 1955 | + | // Jump to a position on the map by entering XYZ coordinates |
| 1956 | + | |
| 1957 | + | BEGIN_EVENT_TABLE(GotoPositionDialog, wxDialog) |
| 1958 | + | EVT_BUTTON(wxID_OK, GotoPositionDialog::OnClickOK) |
| 1959 | + | EVT_BUTTON(wxID_CANCEL, GotoPositionDialog::OnClickCancel) |
| 1960 | + | END_EVENT_TABLE() |
| 1961 | + | |
| 1962 | + | GotoPositionDialog::GotoPositionDialog(wxWindow* parent, Editor& editor) : |
| 1963 | + | wxDialog(parent, wxID_ANY, "Go To Position", wxDefaultPosition, wxDefaultSize), |
| 1964 | + | editor(editor) { |
| 1965 | + | // Create topsizer |
| 1966 | + | wxSizer* sizer = newd wxBoxSizer(wxVERTICAL); |
| 1967 | + | |
| 1968 | + | // Create a static box with instructions |
| 1969 | + | wxStaticBoxSizer* input_sizer = newd wxStaticBoxSizer(wxVERTICAL, this, "Enter Position"); |
| 1970 | + | |
| 1971 | + | // Add some example formats as static text |
| 1972 | + | wxString hint = "Supported formats:\n" |
| 1973 | + | "{x = 0, y = 0, z = 0}\n" |
| 1974 | + | "{\"x\":0,\"y\":0,\"z\":0}\n" |
| 1975 | + | "x, y, z\n" |
| 1976 | + | "(x, y, z)\n" |
| 1977 | + | "Position(x, y, z)"; |
| 1978 | + | input_sizer->Add(newd wxStaticText(this, wxID_ANY, hint), 0, wxALL, 5); |
| 1979 | + | |
| 1980 | + | // Create the input field |
| 1981 | + | position_field = newd wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, |
| 1982 | + | wxSize(200, -1), wxTE_PROCESS_ENTER); |
| 1983 | + | position_field->SetHint("Enter position..."); |
| 1984 | + | input_sizer->Add(position_field, 0, wxEXPAND | wxALL, 5); |
| 1985 | + | |
| 1986 | + | sizer->Add(input_sizer, 0, wxEXPAND | wxALL, 5); |
| 1987 | + | |
| 1988 | + | // OK/Cancel buttons |
| 1989 | + | wxSizer* button_sizer = newd wxBoxSizer(wxHORIZONTAL); |
| 1990 | + | button_sizer->Add(newd wxButton(this, wxID_OK, "OK"), wxSizerFlags(1).Center()); |
| 1991 | + | button_sizer->Add(newd wxButton(this, wxID_CANCEL, "Cancel"), wxSizerFlags(1).Center()); |
| 1992 | + | sizer->Add(button_sizer, 0, wxALL | wxCENTER, 5); |
| 1993 | + | |
| 1994 | + | // Bind events |
| 1995 | + | position_field->Bind(wxEVT_TEXT_ENTER, &GotoPositionDialog::OnClickOK, this); |
| 1996 | + | |
| 1997 | + | SetSizerAndFit(sizer); |
| 1998 | + | Centre(wxBOTH); |
| 1999 | + | |
| 2000 | + | // Set focus to the input field |
| 2001 | + | position_field->SetFocus(); |
| 2002 | + | } |
| 2003 | + | |
| 2004 | + | void GotoPositionDialog::OnClickOK(wxCommandEvent& WXUNUSED(event)) { |
| 2005 | + | Position pos; |
| 2006 | + | std::string input = position_field->GetValue().ToStdString(); |
| 2007 | + | if (posFromClipboard(pos, editor.map.getWidth(), editor.map.getHeight(), input)) { |
| 2008 | + | g_gui.SetScreenCenterPosition(pos); |
| 2009 | + | EndModal(1); |
| 2010 | + | } else { |
| 2011 | + | g_gui.PopupDialog(this, "Error", "Invalid position format.", wxOK); |
| 2012 | + | } |
| 2013 | + | } |
| 2014 | + | |
| 2015 | + | void GotoPositionDialog::OnClickCancel(wxCommandEvent& WXUNUSED(event)) { |
| 2016 | + | EndModal(0); |
| 2017 | + | } |