-
Notifications
You must be signed in to change notification settings - Fork 224
/
common_windows.cpp
1468 lines (1213 loc) · 43.6 KB
/
common_windows.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////////////////////////////////////////////////////////
// This file is part of Remere's Map Editor
//////////////////////////////////////////////////////////////////////
// Remere's Map Editor is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Remere's Map Editor is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//////////////////////////////////////////////////////////////////////
#include "main.h"
#include "materials.h"
#include "brush.h"
#include "editor.h"
#include "items.h"
#include "map.h"
#include "item.h"
#include "complexitem.h"
#include "raw_brush.h"
#include "palette_window.h"
#include "gui.h"
#include "application.h"
#include "common_windows.h"
#include "positionctrl.h"
#include "iominimap.h"
#ifdef _MSC_VER
#pragma warning(disable:4018) // signed/unsigned mismatch
#endif
// ============================================================================
// Map Properties Window
BEGIN_EVENT_TABLE(MapPropertiesWindow, wxDialog)
EVT_CHOICE(MAP_PROPERTIES_VERSION, MapPropertiesWindow::OnChangeVersion)
EVT_BUTTON(wxID_OK, MapPropertiesWindow::OnClickOK)
EVT_BUTTON(wxID_CANCEL, MapPropertiesWindow::OnClickCancel)
END_EVENT_TABLE()
MapPropertiesWindow::MapPropertiesWindow(wxWindow* parent, MapTab* view, Editor& editor) :
wxDialog(parent, wxID_ANY, "Map Properties", wxDefaultPosition, wxSize(300, 200), wxRESIZE_BORDER | wxCAPTION),
view(view),
editor(editor)
{
// Setup data variabels
const Map& map = editor.getMap();
wxSizer* topsizer = newd wxBoxSizer(wxVERTICAL);
wxFlexGridSizer* grid_sizer = newd wxFlexGridSizer(2, 10, 10);
grid_sizer->AddGrowableCol(1);
// Description
grid_sizer->Add(newd wxStaticText(this, wxID_ANY, "Map Description"));
description_ctrl = newd wxTextCtrl(this, wxID_ANY, wxstr(map.getMapDescription()), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
grid_sizer->Add(description_ctrl, wxSizerFlags(1).Expand());
// Map version
grid_sizer->Add(newd wxStaticText(this, wxID_ANY, "Map Version"));
version_choice = newd wxChoice(this, MAP_PROPERTIES_VERSION);
version_choice->Append("OTServ 0.5.0");
version_choice->Append("OTServ 0.6.0");
version_choice->Append("OTServ 0.6.1");
version_choice->Append("OTServ 0.7.0 (revscriptsys)");
switch(map.getVersion().otbm) {
case MAP_OTBM_1:
version_choice->SetSelection(0);
break;
case MAP_OTBM_2:
version_choice->SetSelection(1);
break;
case MAP_OTBM_3:
version_choice->SetSelection(2);
break;
case MAP_OTBM_4:
version_choice->SetSelection(3);
break;
default:
version_choice->SetSelection(0);
}
grid_sizer->Add(version_choice, wxSizerFlags(1).Expand());
// Version
grid_sizer->Add(newd wxStaticText(this, wxID_ANY, "Client Version"));
protocol_choice = newd wxChoice(this, wxID_ANY);
protocol_choice->SetStringSelection(wxstr(g_gui.GetCurrentVersion().getName()));
grid_sizer->Add(protocol_choice, wxSizerFlags(1).Expand());
// Dimensions
grid_sizer->Add(newd wxStaticText(this, wxID_ANY, "Map Dimensions"));
{
wxSizer* subsizer = newd wxBoxSizer(wxHORIZONTAL);
subsizer->Add(
width_spin =
newd wxSpinCtrl(this, wxID_ANY, wxstr(i2s(map.getWidth())),
wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, rme::MapMinWidth, rme::MapMaxWidth), wxSizerFlags(1).Expand()
);
subsizer->Add(
height_spin =
newd wxSpinCtrl(this, wxID_ANY, wxstr(i2s(map.getHeight())),
wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, rme::MapMinHeight, rme::MapMaxHeight), wxSizerFlags(1).Expand()
);
grid_sizer->Add(subsizer, 1, wxEXPAND);
}
// External files
grid_sizer->Add(
newd wxStaticText(this, wxID_ANY, "External Housefile")
);
grid_sizer->Add(
house_filename_ctrl =
newd wxTextCtrl(this, wxID_ANY, wxstr(map.getHouseFilename())), 1, wxEXPAND
);
grid_sizer->Add(
newd wxStaticText(this, wxID_ANY, "External Spawnfile")
);
grid_sizer->Add(
spawn_filename_ctrl =
newd wxTextCtrl(this, wxID_ANY, wxstr(map.getSpawnFilename())), 1, wxEXPAND
);
topsizer->Add(grid_sizer, wxSizerFlags(1).Expand().Border(wxALL, 20));
wxSizer* subsizer = newd wxBoxSizer(wxHORIZONTAL);
subsizer->Add(newd wxButton(this, wxID_OK, "OK"), wxSizerFlags(1).Center());
subsizer->Add(newd wxButton(this, wxID_CANCEL, "Cancel"), wxSizerFlags(1).Center());
topsizer->Add(subsizer, wxSizerFlags(0).Center().Border(wxLEFT | wxRIGHT | wxBOTTOM, 20));
SetSizerAndFit(topsizer);
Centre(wxBOTH);
UpdateProtocolList();
ClientVersion* current_version = ClientVersion::get(map.getVersion().client);
protocol_choice->SetStringSelection(wxstr(current_version->getName()));
}
void MapPropertiesWindow::UpdateProtocolList()
{
wxString ver = version_choice->GetStringSelection();
wxString client = protocol_choice->GetStringSelection();
protocol_choice->Clear();
ClientVersionList versions;
if(g_settings.getInteger(Config::USE_OTBM_4_FOR_ALL_MAPS)) {
versions = ClientVersion::getAllVisible();
} else {
MapVersionID map_version = MAP_OTBM_1;
if(ver.Contains("0.5.0"))
map_version = MAP_OTBM_1;
else if(ver.Contains("0.6.0"))
map_version = MAP_OTBM_2;
else if(ver.Contains("0.6.1"))
map_version = MAP_OTBM_3;
else if(ver.Contains("0.7.0"))
map_version = MAP_OTBM_4;
ClientVersionList protocols = ClientVersion::getAllForOTBMVersion(map_version);
for(ClientVersionList::const_iterator p = protocols.begin(); p != protocols.end(); ++p)
protocol_choice->Append(wxstr((*p)->getName()));
}
protocol_choice->SetSelection(0);
protocol_choice->SetStringSelection(client);
}
void MapPropertiesWindow::OnChangeVersion(wxCommandEvent&)
{
UpdateProtocolList();
}
struct MapConversionContext
{
struct CreatureInfo
{
std::string name;
bool is_npc;
Outfit outfit;
};
typedef std::map<std::string, CreatureInfo> CreatureMap;
CreatureMap creature_types;
void operator()(Map& map, Tile* tile, long long done)
{
if(tile->creature) {
CreatureMap::iterator f = creature_types.find(tile->creature->getName());
if(f == creature_types.end()) {
CreatureInfo info = {
tile->creature->getName(),
tile->creature->isNpc(),
tile->creature->getLookType()
};
creature_types[tile->creature->getName()] = info;
}
}
}
};
void MapPropertiesWindow::OnClickOK(wxCommandEvent& WXUNUSED(event))
{
Map& map = editor.getMap();
MapVersion old_ver = map.getVersion();
MapVersion new_ver;
wxString ver = version_choice->GetStringSelection();
new_ver.client = ClientVersion::get(nstr(protocol_choice->GetStringSelection()))->getID();
if(ver.Contains("0.5.0")) {
new_ver.otbm = MAP_OTBM_1;
} else if(ver.Contains("0.6.0")) {
new_ver.otbm = MAP_OTBM_2;
} else if(ver.Contains("0.6.1")) {
new_ver.otbm = MAP_OTBM_3;
} else if(ver.Contains("0.7.0")) {
new_ver.otbm = MAP_OTBM_4;
}
if(new_ver.client != old_ver.client) {
if(g_gui.GetOpenMapCount() > 1) {
g_gui.PopupDialog(this, "Error",
"You can not change editor version with multiple maps open", wxOK);
return;
}
wxString error;
wxArrayString warnings;
// Switch version
g_gui.GetCurrentEditor()->getSelection().clear();
g_gui.GetCurrentEditor()->clearActions();
if(new_ver.client < old_ver.client) {
int ret = g_gui.PopupDialog(this, "Notice",
"Converting to a previous version may have serious side-effects, are you sure you want to do this?", wxYES | wxNO);
if(ret != wxID_YES) {
return;
}
UnnamedRenderingLock();
// Remember all creatures types on the map
MapConversionContext conversion_context;
foreach_TileOnMap(map, conversion_context);
// Perform the conversion
map.convert(new_ver, true);
// Load the new version
if(!g_gui.LoadVersion(new_ver.client, error, warnings)) {
g_gui.ListDialog(this, "Warnings", warnings);
g_gui.PopupDialog(this, "Map Loader Error", error, wxOK);
g_gui.PopupDialog(this, "Conversion Error", "Could not convert map. The map will now be closed.", wxOK);
EndModal(0);
return;
}
// Remove all creatures that were present are present in the new version
for(MapConversionContext::CreatureMap::iterator cs = conversion_context.creature_types.begin(); cs != conversion_context.creature_types.end();) {
if(g_creatures[cs->first])
cs = conversion_context.creature_types.erase(cs);
else
++cs;
}
if(conversion_context.creature_types.size() > 0) {
int add = g_gui.PopupDialog(this, "Unrecognized creatures", "There were creatures on the old version that are not present in this and were on the map, do you want to add them to this version as well?", wxYES | wxNO);
if(add == wxID_YES) {
for(MapConversionContext::CreatureMap::iterator cs = conversion_context.creature_types.begin(); cs != conversion_context.creature_types.end(); ++cs) {
MapConversionContext::CreatureInfo info = cs->second;
g_creatures.addCreatureType(info.name, info.is_npc, info.outfit);
}
}
}
map.cleanInvalidTiles(true);
} else {
UnnamedRenderingLock();
if(!g_gui.LoadVersion(new_ver.client, error, warnings)) {
g_gui.ListDialog(this, "Warnings", warnings);
g_gui.PopupDialog(this, "Map Loader Error", error, wxOK);
g_gui.PopupDialog(this, "Conversion Error", "Could not convert map. The map will now be closed.", wxOK);
EndModal(0);
return;
}
map.convert(new_ver, true);
}
} else {
map.convert(new_ver, true);
}
map.setMapDescription(nstr(description_ctrl->GetValue()));
map.setHouseFilename(nstr(house_filename_ctrl->GetValue()));
map.setSpawnFilename(nstr(spawn_filename_ctrl->GetValue()));
// Only resize if we have to
int new_map_width = width_spin->GetValue();
int new_map_height = height_spin->GetValue();
if(new_map_width != map.getWidth() || new_map_height != map.getHeight()) {
map.setWidth(new_map_width);
map.setHeight(new_map_height);
g_gui.FitViewToMap(view);
}
g_gui.RefreshPalettes();
EndModal(1);
}
void MapPropertiesWindow::OnClickCancel(wxCommandEvent& WXUNUSED(event))
{
// Just close this window
EndModal(1);
}
MapPropertiesWindow::~MapPropertiesWindow() = default;
// ============================================================================
// Map Import Window
BEGIN_EVENT_TABLE(ImportMapWindow, wxDialog)
EVT_BUTTON(MAP_WINDOW_FILE_BUTTON, ImportMapWindow::OnClickBrowse)
EVT_BUTTON(wxID_OK, ImportMapWindow::OnClickOK)
EVT_BUTTON(wxID_CANCEL, ImportMapWindow::OnClickCancel)
END_EVENT_TABLE()
ImportMapWindow::ImportMapWindow(wxWindow* parent, Editor& editor) :
wxDialog(parent, wxID_ANY, "Import Map", wxDefaultPosition, wxSize(420, 315)),
editor(editor)
{
wxBoxSizer* sizer = newd wxBoxSizer(wxVERTICAL);
wxStaticBoxSizer* tmpsizer;
// File
tmpsizer = newd wxStaticBoxSizer(new wxStaticBox(this, wxID_ANY, "Map File"), wxHORIZONTAL);
file_text_field = newd wxTextCtrl(tmpsizer->GetStaticBox(), wxID_ANY, "", wxDefaultPosition, wxSize(300, 23));
tmpsizer->Add(file_text_field, 0, wxALL, 5);
wxButton* browse_button = newd wxButton(tmpsizer->GetStaticBox(), MAP_WINDOW_FILE_BUTTON, "Browse...", wxDefaultPosition, wxSize(80, 23));
tmpsizer->Add(browse_button, 0, wxALL, 5);
sizer->Add(tmpsizer, 1, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, 5);
// Import offset
tmpsizer = newd wxStaticBoxSizer(new wxStaticBox(this, wxID_ANY, "Import Offset"), wxHORIZONTAL);
tmpsizer->Add(newd wxStaticText(tmpsizer->GetStaticBox(), wxID_ANY, "Offset X:"), 0, wxALL | wxEXPAND, 5);
x_offset_ctrl = newd wxSpinCtrl(tmpsizer->GetStaticBox(), wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(60, 23), wxSP_ARROW_KEYS, -rme::MapMaxHeight, rme::MapMaxHeight);
tmpsizer->Add(x_offset_ctrl, 0, wxALL, 5);
tmpsizer->Add(newd wxStaticText(tmpsizer->GetStaticBox(), wxID_ANY, "Offset Y:"), 0, wxALL, 5);
y_offset_ctrl = newd wxSpinCtrl(tmpsizer->GetStaticBox(), wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(60, 23), wxSP_ARROW_KEYS, -rme::MapMaxHeight, rme::MapMaxHeight);
tmpsizer->Add(y_offset_ctrl, 0, wxALL, 5);
tmpsizer->Add(newd wxStaticText(tmpsizer->GetStaticBox(), wxID_ANY, "Offset Z:"), 0, wxALL, 5);
z_offset_ctrl = newd wxSpinCtrl(tmpsizer->GetStaticBox(), wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(60, 23), wxSP_ARROW_KEYS, -rme::MapMaxLayer, rme::MapMaxLayer);
tmpsizer->Add(z_offset_ctrl, 0, wxALL, 5);
sizer->Add(tmpsizer, 1, wxEXPAND | wxLEFT | wxRIGHT, 5);
// Import options
wxArrayString house_choices;
house_choices.Add("Smart Merge");
house_choices.Add("Insert");
house_choices.Add("Merge");
house_choices.Add("Don't Import");
// House options
tmpsizer = newd wxStaticBoxSizer(new wxStaticBox(this, wxID_ANY, "House Import Behaviour"), wxVERTICAL);
house_options = newd wxChoice(tmpsizer->GetStaticBox(), wxID_ANY, wxDefaultPosition, wxDefaultSize, house_choices);
house_options->SetSelection(0);
tmpsizer->Add(house_options, 0, wxALL | wxEXPAND, 5);
sizer->Add(tmpsizer, 1, wxEXPAND | wxLEFT | wxRIGHT, 5);
// Import options
wxArrayString spawn_choices;
spawn_choices.Add("Merge");
spawn_choices.Add("Don't Import");
// Spawn options
tmpsizer = newd wxStaticBoxSizer(new wxStaticBox(this, wxID_ANY, "Spawn Import Behaviour"), wxVERTICAL);
spawn_options = newd wxChoice(tmpsizer->GetStaticBox(), wxID_ANY, wxDefaultPosition, wxDefaultSize, spawn_choices);
spawn_options->SetSelection(0);
tmpsizer->Add(spawn_options, 0, wxALL | wxEXPAND, 5);
sizer->Add(tmpsizer, 1, wxEXPAND | wxLEFT | wxRIGHT, 5);
// OK/Cancel buttons
wxBoxSizer* buttons = newd wxBoxSizer(wxHORIZONTAL);
buttons->Add(newd wxButton(this, wxID_OK, "Ok"), 0, wxALL, 5);
buttons->Add(newd wxButton(this, wxID_CANCEL, "Cancel"), 0, wxALL, 5);
sizer->Add(buttons, wxSizerFlags(1).Center());
SetSizer(sizer);
Layout();
Centre(wxBOTH);
}
ImportMapWindow::~ImportMapWindow() = default;
void ImportMapWindow::OnClickBrowse(wxCommandEvent& WXUNUSED(event))
{
wxFileDialog dialog(this, "Import...", "", "", "*.otbm", wxFD_OPEN | wxFD_FILE_MUST_EXIST);
int ok = dialog.ShowModal();
if(ok == wxID_OK)
file_text_field->ChangeValue(dialog.GetPath());
}
void ImportMapWindow::OnClickOK(wxCommandEvent& WXUNUSED(event))
{
if(Validate() && TransferDataFromWindow()) {
wxFileName fn = file_text_field->GetValue();
if(!fn.FileExists()) {
g_gui.PopupDialog(this, "Error", "The specified map file doesn't exist", wxOK);
return;
}
ImportType spawn_import_type = IMPORT_DONT;
ImportType house_import_type = IMPORT_DONT;
switch(spawn_options->GetSelection()) {
case 0: spawn_import_type = IMPORT_MERGE; break;
case 1: spawn_import_type = IMPORT_DONT; break;
}
switch(house_options->GetSelection()) {
case 0: house_import_type = IMPORT_SMART_MERGE; break;
case 1: house_import_type = IMPORT_MERGE; break;
case 2: house_import_type = IMPORT_INSERT; break;
case 3: house_import_type = IMPORT_DONT; break;
}
EndModal(1);
editor.importMap(fn, x_offset_ctrl->GetValue(), y_offset_ctrl->GetValue(), z_offset_ctrl->GetValue(), house_import_type, spawn_import_type);
}
}
void ImportMapWindow::OnClickCancel(wxCommandEvent& WXUNUSED(event))
{
// Just close this window
EndModal(0);
}
// ============================================================================
// Export Minimap window
BEGIN_EVENT_TABLE(ExportMiniMapWindow, wxDialog)
EVT_BUTTON(MAP_WINDOW_FILE_BUTTON, ExportMiniMapWindow::OnClickBrowse)
EVT_BUTTON(wxID_OK, ExportMiniMapWindow::OnClickOK)
EVT_BUTTON(wxID_CANCEL, ExportMiniMapWindow::OnClickCancel)
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)),
editor(editor)
{
wxSizer* sizer = newd wxBoxSizer(wxVERTICAL);
wxSizer* tmpsizer;
// Error field
error_field = newd wxStaticText(this, wxID_VIEW_DETAILS, "", wxDefaultPosition, wxDefaultSize);
error_field->SetForegroundColour(*wxRED);
tmpsizer = newd wxBoxSizer(wxHORIZONTAL);
tmpsizer->Add(error_field, 0, wxALL, 5);
sizer->Add(tmpsizer, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, 5);
// Output folder
directory_text_field = newd wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize);
directory_text_field->Bind(wxEVT_KEY_UP, &ExportMiniMapWindow::OnDirectoryChanged, this);
directory_text_field->SetValue(wxString(g_settings.getString(Config::MINIMAP_EXPORT_DIR)));
tmpsizer = newd wxStaticBoxSizer(wxHORIZONTAL, this, "Output Folder");
tmpsizer->Add(directory_text_field, 1, wxALL, 5);
tmpsizer->Add(newd wxButton(this, MAP_WINDOW_FILE_BUTTON, "Browse"), 0, 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);
file_name_text_field->Bind(wxEVT_KEY_UP, &ExportMiniMapWindow::OnFileNameChanged, this);
tmpsizer = newd wxStaticBoxSizer(wxHORIZONTAL, this, "File Name");
tmpsizer->Add(file_name_text_field, 1, wxALL, 5);
sizer->Add(tmpsizer, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, 5);
// Format options
wxArrayString format_choices;
format_choices.Add(".otmm (Client Minimap)");
format_choices.Add(".png (PNG Image)");
format_choices.Add(".bmp (Bitmap Image)");
format_options = new wxChoice(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, format_choices);
format_options->SetSelection(0);
tmpsizer->Add(format_options, 1, wxALL, 5);
// Export options
wxArrayString choices;
choices.Add("All Floors");
choices.Add("Ground Floor");
choices.Add("Specific Floor");
if(editor.hasSelection())
choices.Add("Selected Area");
// Area options
tmpsizer = newd wxStaticBoxSizer(wxHORIZONTAL, this, "Area Options");
floor_options = newd wxChoice(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, choices);
floor_number = newd wxSpinCtrl(this, wxID_ANY, i2ws(rme::MapGroundLayer), wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, rme::MapMinLayer, rme::MapMaxLayer, rme::MapGroundLayer);
floor_number->Enable(false);
floor_options->SetSelection(0);
tmpsizer->Add(floor_options, 1, wxALL, 5);
tmpsizer->Add(floor_number, 0, wxALL, 5);
sizer->Add(tmpsizer, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, 5);
// OK/Cancel buttons
tmpsizer = newd wxBoxSizer(wxHORIZONTAL);
tmpsizer->Add(ok_button = newd wxButton(this, wxID_OK, "OK"), wxSizerFlags(1).Center());
tmpsizer->Add(newd wxButton(this, wxID_CANCEL, "Cancel"), wxSizerFlags(1).Center());
sizer->Add(tmpsizer, 0, wxCENTER, 10);
SetSizer(sizer);
Layout();
Centre(wxBOTH);
CheckValues();
}
ExportMiniMapWindow::~ExportMiniMapWindow() = default;
void ExportMiniMapWindow::OnExportTypeChange(wxCommandEvent& event)
{
floor_number->Enable(event.GetSelection() == 2);
}
void ExportMiniMapWindow::OnClickBrowse(wxCommandEvent& WXUNUSED(event))
{
wxDirDialog dialog(NULL, "Select the output folder", "", wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST);
if(dialog.ShowModal() == wxID_OK) {
const wxString& directory = dialog.GetPath();
directory_text_field->ChangeValue(directory);
}
CheckValues();
}
void ExportMiniMapWindow::OnDirectoryChanged(wxKeyEvent& event)
{
CheckValues();
event.Skip();
}
void ExportMiniMapWindow::OnFileNameChanged(wxKeyEvent& event)
{
CheckValues();
event.Skip();
}
void ExportMiniMapWindow::OnClickOK(wxCommandEvent& WXUNUSED(event))
{
g_gui.CreateLoadBar("Exporting minimap...");
auto format = static_cast<MinimapExportFormat>(format_options->GetSelection());
auto mode = static_cast<MinimapExportMode>(floor_options->GetSelection());
std::string directory = directory_text_field->GetValue().ToStdString();
std::string file_name = file_name_text_field->GetValue().ToStdString();
int floor = floor_number->GetValue();
g_settings.setString(Config::MINIMAP_EXPORT_DIR, directory);
IOMinimap io(&editor, format, mode, true);
if (!io.saveMinimap(directory, file_name, floor)) {
g_gui.PopupDialog("Error", io.getError(), wxOK);
}
g_gui.DestroyLoadBar();
EndModal(wxID_OK);
}
void ExportMiniMapWindow::OnClickCancel(wxCommandEvent& WXUNUSED(event))
{
// Just close this window
EndModal(wxID_CANCEL);
}
void ExportMiniMapWindow::CheckValues()
{
if(directory_text_field->IsEmpty()) {
error_field->SetLabel("Type or select an output folder.");
ok_button->Enable(false);
return;
}
if(file_name_text_field->IsEmpty()) {
error_field->SetLabel("Type a name for the file.");
ok_button->Enable(false);
return;
}
FileName directory(directory_text_field->GetValue());
if(!directory.Exists()) {
error_field->SetLabel("Output folder not found.");
ok_button->Enable(false);
return;
}
if(!directory.IsDirWritable()) {
error_field->SetLabel("Output folder is not writable.");
ok_button->Enable(false);
return;
}
error_field->SetLabel(wxEmptyString);
ok_button->Enable(true);
}
// ============================================================================
// Numkey forwarding text control
BEGIN_EVENT_TABLE(KeyForwardingTextCtrl, wxTextCtrl)
EVT_KEY_DOWN(KeyForwardingTextCtrl::OnKeyDown)
END_EVENT_TABLE()
void KeyForwardingTextCtrl::OnKeyDown(wxKeyEvent& event)
{
if(event.GetKeyCode() == WXK_UP || event.GetKeyCode() == WXK_DOWN ||
event.GetKeyCode() == WXK_PAGEDOWN || event.GetKeyCode() == WXK_PAGEUP) {
GetParent()->GetEventHandler()->AddPendingEvent(event);
} else {
event.Skip();
}
}
// ============================================================================
// Find Item Dialog (Jump to item)
BEGIN_EVENT_TABLE(FindDialog, wxDialog)
EVT_TIMER(wxID_ANY, FindDialog::OnTextIdle)
EVT_TEXT(JUMP_DIALOG_TEXT, FindDialog::OnTextChange)
EVT_KEY_DOWN(FindDialog::OnKeyDown)
EVT_TEXT_ENTER(JUMP_DIALOG_TEXT, FindDialog::OnClickOK)
EVT_LISTBOX_DCLICK(JUMP_DIALOG_LIST, FindDialog::OnClickList)
EVT_BUTTON(wxID_OK, FindDialog::OnClickOK)
EVT_BUTTON(wxID_CANCEL, FindDialog::OnClickCancel)
END_EVENT_TABLE()
FindDialog::FindDialog(wxWindow* parent, wxString title) :
wxDialog(g_gui.root, wxID_ANY, title, wxDefaultPosition, wxDefaultSize, wxRESIZE_BORDER | wxCAPTION | wxCLOSE_BOX),
idle_input_timer(this),
result_brush(nullptr),
result_id(0)
{
wxSizer* sizer = newd wxBoxSizer(wxVERTICAL);
search_field = newd KeyForwardingTextCtrl(this, JUMP_DIALOG_TEXT, "", wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
search_field->SetFocus();
sizer->Add(search_field, 0, wxEXPAND);
item_list = newd FindDialogListBox(this, JUMP_DIALOG_LIST);
item_list->SetMinSize(wxSize(470, 400));
sizer->Add(item_list, wxSizerFlags(1).Expand().Border());
wxSizer* stdsizer = newd wxBoxSizer(wxHORIZONTAL);
stdsizer->Add(newd wxButton(this, wxID_OK, "OK"), wxSizerFlags(1).Center());
stdsizer->Add(newd wxButton(this, wxID_CANCEL, "Cancel"), wxSizerFlags(1).Center());
sizer->Add(stdsizer, wxSizerFlags(0).Center().Border());
SetSizerAndFit(sizer);
Centre(wxBOTH);
// We can't call it here since it calls an abstract function, call in child constructors instead.
// RefreshContents();
}
FindDialog::~FindDialog() = default;
void FindDialog::OnKeyDown(wxKeyEvent& event)
{
int w, h;
item_list->GetSize(&w, &h);
size_t amount = 1;
switch(event.GetKeyCode()) {
case WXK_PAGEUP:
amount = h / 32 + 1;
[[fallthrough]];
case WXK_UP: {
if(item_list->GetItemCount() > 0) {
ssize_t n = item_list->GetSelection();
if(n == wxNOT_FOUND)
n = 0;
else if(n != amount && n - amount < n) // latter is needed for unsigned overflow
n -= amount;
else
n = 0;
item_list->SetSelection(n);
}
break;
}
case WXK_PAGEDOWN:
amount = h / 32 + 1;
[[fallthrough]];
case WXK_DOWN: {
if(item_list->GetItemCount() > 0) {
ssize_t n = item_list->GetSelection();
size_t itemcount = item_list->GetItemCount();
if(n == wxNOT_FOUND)
n = 0;
else if(static_cast<uint32_t>(n) < itemcount - amount && itemcount - amount < itemcount)
n += amount;
else
n = item_list->GetItemCount() - 1;
item_list->SetSelection(n);
}
break;
}
default:
event.Skip();
break;
}
}
void FindDialog::OnTextIdle(wxTimerEvent& WXUNUSED(event))
{
RefreshContents();
}
void FindDialog::OnTextChange(wxCommandEvent& WXUNUSED(event))
{
idle_input_timer.Start(800, true);
}
void FindDialog::OnClickList(wxCommandEvent& event)
{
OnClickListInternal(event);
}
void FindDialog::OnClickOK(wxCommandEvent& WXUNUSED(event))
{
// This is to get virtual callback
OnClickOKInternal();
}
void FindDialog::OnClickCancel(wxCommandEvent& WXUNUSED(event))
{
EndModal(0);
}
void FindDialog::RefreshContents()
{
// This is to get virtual callback
RefreshContentsInternal();
}
// ============================================================================
// Find Brush Dialog (Jump to brush)
FindBrushDialog::FindBrushDialog(wxWindow* parent, wxString title) : FindDialog(parent, title)
{
RefreshContents();
}
FindBrushDialog::~FindBrushDialog() = default;
void FindBrushDialog::OnClickListInternal(wxCommandEvent& event)
{
Brush* brush = item_list->GetSelectedBrush();
if(brush) {
result_brush = brush;
EndModal(1);
}
}
void FindBrushDialog::OnClickOKInternal()
{
// This is kind of stupid as it would fail unless the "Please enter a search string" wasn't there
if(item_list->GetItemCount() > 0) {
if(item_list->GetSelection() == wxNOT_FOUND) {
item_list->SetSelection(0);
}
Brush* brush = item_list->GetSelectedBrush();
if(!brush) {
// It's either "Please enter a search string" or "No matches"
// Perhaps we can refresh now?
std::string search_string = as_lower_str(nstr(search_field->GetValue()));
bool do_search = (search_string.size() >= 2);
if(do_search) {
const BrushMap& map = g_brushes.getMap();
for(BrushMap::const_iterator iter = map.begin(); iter != map.end(); ++iter) {
const Brush* brush = iter->second;
if(as_lower_str(brush->getName()).find(search_string) == std::string::npos)
continue;
// Don't match RAWs now.
if(brush->isRaw())
continue;
// Found one!
result_brush = brush;
break;
}
// Did we not find a matching brush?
if(!result_brush) {
// Then let's search the RAWs
for(int id = 0; id <= g_items.getMaxID(); ++id) {
const ItemType& type = g_items.getItemType(id);
if(type.id == 0)
continue;
RAWBrush* raw_brush = type.raw_brush;
if(!raw_brush)
continue;
if(as_lower_str(raw_brush->getName()).find(search_string) == std::string::npos)
continue;
// Found one!
result_brush = raw_brush;
break;
}
}
// Done!
}
} else {
result_brush = brush;
}
}
EndModal(1);
}
void FindBrushDialog::RefreshContentsInternal()
{
item_list->Clear();
std::string search_string = as_lower_str(nstr(search_field->GetValue()));
bool do_search = (search_string.size() >= 2);
if(do_search) {
bool found_search_results = false;
const BrushMap& brushes_map = g_brushes.getMap();
// We store the raws so they display last of all results
std::deque<const RAWBrush*> raws;
for(BrushMap::const_iterator iter = brushes_map.begin(); iter != brushes_map.end(); ++iter) {
const Brush* brush = iter->second;
if(as_lower_str(brush->getName()).find(search_string) == std::string::npos)
continue;
if(brush->isRaw())
continue;
found_search_results = true;
item_list->AddBrush(const_cast<Brush*>(brush));
}
for(int id = 0; id <= g_items.getMaxID(); ++id) {
const ItemType& type = g_items.getItemType(id);
if(type.id == 0)
continue;
RAWBrush* raw_brush = type.raw_brush;
if(!raw_brush)
continue;
if(as_lower_str(raw_brush->getName()).find(search_string) == std::string::npos)
continue;
found_search_results = true;
item_list->AddBrush(raw_brush);
}
while(raws.size() > 0) {
item_list->AddBrush(const_cast<RAWBrush*>(raws.front()));
raws.pop_front();
}
if(found_search_results) {
item_list->SetSelection(0);
} else {
item_list->SetNoMatches();
}
}
item_list->Refresh();
}
// ============================================================================
// Listbox in find item / brush stuff
FindDialogListBox::FindDialogListBox(wxWindow* parent, wxWindowID id) :
wxVListBox(parent, id, wxDefaultPosition, wxDefaultSize, wxLB_SINGLE),
cleared(false),
no_matches(false)
{
Clear();
}
FindDialogListBox::~FindDialogListBox()
{
////
}
void FindDialogListBox::Clear()
{
cleared = true;
no_matches = false;
brushlist.clear();
SetItemCount(1);
}
void FindDialogListBox::SetNoMatches()
{
cleared = false;
no_matches = true;
brushlist.clear();
SetItemCount(1);
}
void FindDialogListBox::AddBrush(Brush* brush)
{
if(cleared || no_matches)
SetItemCount(0);
cleared = false;
no_matches = false;
SetItemCount(GetItemCount() + 1);
brushlist.push_back(brush);
}
Brush* FindDialogListBox::GetSelectedBrush()
{
ssize_t n = GetSelection();
if(n == wxNOT_FOUND || no_matches || cleared)
return nullptr;
return brushlist[n];
}
void FindDialogListBox::OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const
{
if(no_matches) {
dc.DrawText("No matches for your search.", rect.GetX() + 40, rect.GetY() + 6);
} else if(cleared) {
dc.DrawText("Please enter your search string.", rect.GetX() + 40, rect.GetY() + 6);
} else {
ASSERT(n < brushlist.size());
Sprite* spr = g_gui.gfx.getSprite(brushlist[n]->getLookID());
if(spr) {
spr->DrawTo(&dc, SPRITE_SIZE_32x32, rect.GetX(), rect.GetY(), rect.GetWidth(), rect.GetHeight());
} else {
auto creatureType = g_creatures[brushlist[n]->getName()];
if (!creatureType) {
return;
}
auto creatureSprite = g_gui.gfx.getCreatureSprite(creatureType->outfit.lookType);
if (creatureSprite) {
creatureSprite->DrawTo(&dc, rect, creatureType->outfit);
}
}
if(IsSelected(n)) {
if(HasFocus())
dc.SetTextForeground(wxColor(0xFF, 0xFF, 0xFF));
else
dc.SetTextForeground(wxColor(0x00, 0x00, 0xFF));
} else {
dc.SetTextForeground(wxColor(0x00, 0x00, 0x00));
}
dc.DrawText(wxstr(brushlist[n]->getName()), rect.GetX() + 40, rect.GetY() + 6);
}
}
wxCoord FindDialogListBox::OnMeasureItem(size_t n) const
{
return 32;
}
// ============================================================================
// wxListBox that can be sorted
SortableListBox::SortableListBox(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size)
: wxListBox(parent, id, pos, size, 0, nullptr, wxLB_SINGLE | wxLB_NEEDED_SB)
{}