/
MapSelector.cpp
228 lines (178 loc) · 5.85 KB
/
MapSelector.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
#include "MapSelector.h"
#include "i18n.h"
#include "ifilesystem.h"
#include "string/case_conv.h"
#include "os/path.h"
#include <wx/sizer.h>
#include <wx/button.h>
namespace ui
{
// CONSTANTS
namespace
{
const char* const MAPSELECTOR_TITLE = N_("Choose Map File");
}
MapSelector::MapSelector() :
DialogBase(_(MAPSELECTOR_TITLE)),
_treeView(nullptr),
_buttons(nullptr),
_handlingSelectionChange(false)
{
SetSizer(new wxBoxSizer(wxVERTICAL));
wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL);
GetSizer()->Add(vbox, 1, wxEXPAND | wxALL, 12);
setupPathSelector(vbox);
setupTreeView(this);
vbox->Add(_treeView, 1, wxEXPAND);
_buttons = CreateStdDialogButtonSizer(wxOK | wxCANCEL);
wxButton* reloadButton = new wxButton(this, wxID_ANY, _("Refresh"));
reloadButton->Bind(wxEVT_BUTTON, &MapSelector::onRescanPath, this);
_buttons->Prepend(reloadButton, 0, wxRIGHT, 32);
vbox->Add(_buttons, 0, wxALIGN_RIGHT | wxTOP, 12);
// Set the default size of the window
_position.connect(this);
_position.readPosition();
FitToScreen(0.5f, 0.6f);
}
int MapSelector::ShowModal()
{
// Populate the tree
populateTree();
updateButtonSensitivity();
// Enter the main loop
return DialogBase::ShowModal();
}
std::string MapSelector::ChooseMapFile()
{
auto* dialog = new MapSelector();
std::string returnValue = "";
if (dialog->ShowModal() == wxID_OK)
{
returnValue = dialog->getSelectedPath();
}
// Use the instance to select a model.
return returnValue;
}
void MapSelector::OpenMapFromProject(const cmd::ArgumentList& args)
{
auto mapPath = ChooseMapFile();
if (!mapPath.empty())
{
GlobalCommandSystem().executeCommand("OpenMap", mapPath);
}
}
// Helper function to create the TreeView
void MapSelector::setupTreeView(wxWindow* parent)
{
_treeView = wxutil::FileSystemView::Create(parent, wxBORDER_STATIC | wxDV_NO_HEADER);
_treeView->Bind(wxutil::EV_FSVIEW_SELECTION_CHANGED, &MapSelector::onSelectionChanged, this);
_treeView->Bind(wxEVT_DATAVIEW_ITEM_ACTIVATED, &MapSelector::onItemActivated, this);;
_treeView->signal_TreePopulated().connect(sigc::mem_fun(this, &MapSelector::onFileViewTreePopulated));
// Get the extensions from all possible patterns (e.g. *.map or *.mapx)
FileTypePatterns patterns = GlobalFiletypes().getPatternsForType(filetype::TYPE_MAP);
for (const auto& pattern : patterns)
{
_mapFileExtensions.insert(pattern.extension);
}
std::set<std::string> fileExtensions = _mapFileExtensions;
// Add all PK extensions too
const auto& pakExtensions = GlobalFileSystem().getArchiveExtensions();
for (const auto& extension : pakExtensions)
{
fileExtensions.insert(string::to_lower_copy(extension));
}
_treeView->SetFileExtensions(fileExtensions);
}
void MapSelector::setupPathSelector(wxSizer* parentSizer)
{
// Path selection box
wxBoxSizer* hbox = new wxBoxSizer(wxHORIZONTAL);
_useModPath = new wxRadioButton(this, wxID_ANY, _("Browse mod resources"),
wxDefaultPosition, wxDefaultSize, wxRB_GROUP);
_useCustomPath = new wxRadioButton(this, wxID_ANY, _("Browse custom PAK:"));
_customPath = new wxutil::PathEntry(this, filetype::TYPE_PAK, true);
// Connect to the changed event
_customPath->Bind(wxutil::EV_PATH_ENTRY_CHANGED, [&](wxCommandEvent& ev)
{
if (!_customPath->getValue().empty())
{
_useCustomPath->SetValue(true);
}
else
{
_useModPath->SetValue(true);
}
onPathSelectionChanged();
});
hbox->Add(_useModPath, 0, wxALIGN_CENTER_VERTICAL | wxLEFT, 6);
hbox->Add(_useCustomPath, 0, wxLEFT | wxALIGN_CENTER_VERTICAL, 6);
hbox->Add(_customPath, 1, wxLEFT, 6);
parentSizer->Add(hbox, 0, wxBOTTOM | wxEXPAND, 12);
// Wire up the signals
_useModPath->Bind(wxEVT_RADIOBUTTON, [&](wxCommandEvent& ev)
{
onPathSelectionChanged();
});
_useCustomPath->Bind(wxEVT_RADIOBUTTON, [&](wxCommandEvent& ev)
{
onPathSelectionChanged();
});
}
void MapSelector::onPathSelectionChanged()
{
// Re-populate the tree if the base path is different
auto basePath = getBasePath();
if (_treeView->GetBasePath() != basePath)
{
populateTree();
}
}
std::string MapSelector::getBasePath()
{
if (_useCustomPath->GetValue() && !_customPath->getValue().empty())
{
return _customPath->getValue();
}
return ""; // use an empty path which resembles the VFS root
}
void MapSelector::populateTree()
{
_treeView->SetBasePath(getBasePath());
_treeView->Populate();
}
void MapSelector::onRescanPath(wxCommandEvent& ev)
{
populateTree();
}
void MapSelector::updateButtonSensitivity()
{
auto fileExtension = string::to_lower_copy(os::getExtension(_treeView->GetSelectedPath()));
bool okIsEnabled = !_treeView->GetIsFolderSelected() && _mapFileExtensions.count(fileExtension) > 0;
_buttons->GetAffirmativeButton()->Enable(okIsEnabled);
}
void MapSelector::onSelectionChanged(wxutil::FileSystemView::SelectionChangedEvent& ev)
{
updateButtonSensitivity();
}
void MapSelector::onItemActivated(wxDataViewEvent& ev)
{
auto selectedPath = _treeView->GetSelectedPath();
auto extension = string::to_lower_copy(os::getExtension(selectedPath));
// Check if this is a physical file
auto rootPath = GlobalFileSystem().findFile(selectedPath);
if (!rootPath.empty() && GlobalFileSystem().getArchiveExtensions().count(extension) > 0)
{
_useCustomPath->SetValue(true);
_customPath->setValue(os::standardPathWithSlash(rootPath) + selectedPath);
onPathSelectionChanged();
}
}
void MapSelector::onFileViewTreePopulated()
{
_treeView->ExpandPath("maps/");
}
std::string MapSelector::getSelectedPath()
{
return _treeView->GetSelectedPath();
}
}