/
ExportAsModelDialog.cpp
277 lines (212 loc) · 9.02 KB
/
ExportAsModelDialog.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
#include "ExportAsModelDialog.h"
#include "i18n.h"
#include "imodel.h"
#include "iselection.h"
#include "ifiletypes.h"
#include "igame.h"
#include <stdexcept>
#include <sigc++/functors/mem_fun.h>
#include <wx/checkbox.h>
#include <wx/filepicker.h>
#include <wx/panel.h>
#include <wx/sizer.h>
#include "string/case_conv.h"
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include "selectionlib.h"
#include "os/path.h"
#include "os/file.h"
#include "os/dir.h"
#include "registry/registry.h"
#include "wxutil/dialog/MessageBox.h"
#include "wxutil/ChoiceHelper.h"
#include "wxutil/PathEntry.h"
#include "map/algorithm/Export.h"
namespace ui
{
namespace
{
const char* const WINDOW_TITLE = N_("Model Export");
const char* RKEY_MODEL_EXPORT_SKIP_CAULK = "user/ui/exportAsModel/skipCaulk";
const char* RKEY_MODEL_EXPORT_CENTER_OBJECTS = "user/ui/exportAsModel/centerObjects";
const char* RKEY_MODEL_EXPORT_REPLACE_WITH_MODEL = "user/ui/exportAsModel/replaceSelectionWithModel";
const char* RKEY_MODEL_EXPORT_OUTPUT_PATH = "user/ui/exportAsModel/outputPath";
const char* RKEY_MODEL_EXPORT_OUTPUT_FORMAT = "user/ui/exportAsModel/outputFormat";
const char* RKEY_MODEL_EXPORT_USE_ENTITY_ORIGIN = "user/ui/exportAsModel/keepEntityOrigin";
}
ExportAsModelDialog::ExportAsModelDialog(wxWindow* parent) :
DialogBase(_(WINDOW_TITLE), parent)
{
populateWindow();
}
void ExportAsModelDialog::populateWindow()
{
SetSizer(new wxBoxSizer(wxVERTICAL));
wxPanel* panel = loadNamedPanel(this, "ExportDialogMainPanel");
GetSizer()->Add(panel, 1, wxEXPAND);
makeLabelBold(this, "ExportDialogOutputLabel");
makeLabelBold(this, "ExportDialogOptionLabel");
wxButton* exportButton = findNamedObject<wxButton>(this, "ExportDialogExportButton");
wxButton* cancelButton = findNamedObject<wxButton>(this, "ExportDialogCancelButton");
exportButton->Bind(wxEVT_BUTTON, sigc::mem_fun(this, &ExportAsModelDialog::onExport));
cancelButton->Bind(wxEVT_BUTTON, sigc::mem_fun(this, &ExportAsModelDialog::onCancel));
wxChoice* formatChoice = findNamedObject<wxChoice>(this, "ExportDialogFormatChoice");
formatChoice->Clear();
formatChoice->Bind(wxEVT_CHOICE, sigc::mem_fun(this, &ExportAsModelDialog::onFormatSelection));
// Push the available formats to the wxChoice control
GlobalModelFormatManager().foreachExporter([&](const model::IModelExporterPtr& exporter)
{
// Generate the display name "<exporterName> (.<ext>)"
std::string displayName = exporter->getDisplayName();
displayName += " (." + string::to_lower_copy(exporter->getExtension()) + ")";
// Store the exporter extension as client data
formatChoice->Append(displayName, new wxStringClientData(exporter->getExtension()));
});
// Select the first format for starters
formatChoice->Select(0);
std::string recentFormat = registry::getValue<std::string>(RKEY_MODEL_EXPORT_OUTPUT_FORMAT);
std::string recentPath = registry::getValue<std::string>(RKEY_MODEL_EXPORT_OUTPUT_PATH);
// Default to the models path of the current mod or game
if (recentPath.empty())
{
recentPath = GlobalGameManager().getModPath();
if (recentPath.empty())
{
recentPath = GlobalGameManager().getUserEnginePath();
}
recentPath = os::standardPathWithSlash(recentPath) + "models/";
if (!os::fileOrDirExists(recentPath))
{
rMessage() << "Creating default model output folder: " << recentPath << std::endl;
os::makeDirectory(recentPath);
}
}
if (!recentFormat.empty())
{
wxutil::ChoiceHelper::SelectItemByStoredString(formatChoice, recentFormat);
}
// Replace the filepicker control with our own PathEntry
wxWindow* existing = findNamedObject<wxWindow>(this, "ExportDialogFilePicker");
wxutil::PathEntry* pathEntry = new wxutil::PathEntry(existing->GetParent(),
filetype::TYPE_MODEL_EXPORT, false, recentFormat);
pathEntry->setValue(recentPath);
pathEntry->SetName("ExportDialogFilePicker");
existing->GetContainingSizer()->Replace(existing, pathEntry);
existing->Destroy();
bool skipCaulk = registry::getValue<bool>(RKEY_MODEL_EXPORT_SKIP_CAULK);
findNamedObject<wxCheckBox>(this, "ExportDialogSkipCaulk")->SetValue(skipCaulk);
bool centerObjects = registry::getValue<bool>(RKEY_MODEL_EXPORT_CENTER_OBJECTS);
findNamedObject<wxCheckBox>(this, "ExportDialogCenterObjects")->SetValue(centerObjects);
bool replaceSelectionWithModel = registry::getValue<bool>(RKEY_MODEL_EXPORT_REPLACE_WITH_MODEL);
findNamedObject<wxCheckBox>(this, "ExportDialogReplaceWithModel")->SetValue(replaceSelectionWithModel);
bool keepEntityOrigin = registry::getValue<bool>(RKEY_MODEL_EXPORT_USE_ENTITY_ORIGIN);
wxCheckBox* keepOriginBox = findNamedObject<wxCheckBox>(this, "ExportDialogUseEntityOrigin");
const SelectionInfo& info = GlobalSelectionSystem().getSelectionInfo();
if (info.totalCount == 1 && info.entityCount == 1)
{
keepOriginBox->SetValue(keepEntityOrigin);
keepOriginBox->Enable(true);
}
else
{
keepOriginBox->SetValue(false);
keepOriginBox->Enable(false);
}
// Check if options are available for the current format
handleFormatSelectionChange();
Layout();
Fit();
CenterOnScreen();
}
void ExportAsModelDialog::onExport(wxCommandEvent& ev)
{
map::algorithm::ModelExportOptions options;
options.centerObjects = findNamedObject<wxCheckBox>(this, "ExportDialogCenterObjects")->GetValue();
options.skipCaulk = findNamedObject<wxCheckBox>(this, "ExportDialogSkipCaulk")->GetValue();
options.outputFilename = findNamedObject<wxutil::PathEntry>(this, "ExportDialogFilePicker")->getValue();
options.outputFormat = wxutil::ChoiceHelper::GetSelectedStoredString(findNamedObject<wxChoice>(this, "ExportDialogFormatChoice"));
options.replaceSelectionWithModel = findNamedObject<wxCheckBox>(this, "ExportDialogReplaceWithModel")->GetValue();
options.useEntityOrigin = findNamedObject<wxCheckBox>(this, "ExportDialogUseEntityOrigin")->GetValue();
if (options.outputFilename.empty())
{
wxutil::Messagebox::Show(_("Empty Filename"), _("No filename specified, cannot run exporter"), IDialog::MessageType::MESSAGE_ERROR);
return;
}
saveOptionsToRegistry();
try
{
map::algorithm::exportSelectedAsModel(options);
// Close the dialog
EndModal(wxID_OK);
}
catch (std::runtime_error& ex)
{
wxutil::Messagebox::Show(_("Export failed"), ex.what(), IDialog::MessageType::MESSAGE_ERROR);
}
}
void ExportAsModelDialog::onCancel(wxCommandEvent& ev)
{
// Remember stuff even when cancel is pressed
saveOptionsToRegistry();
// destroy dialog without saving
EndModal(wxID_CANCEL);
}
void ExportAsModelDialog::handleFormatSelectionChange()
{
std::string selectedFormat = wxutil::ChoiceHelper::GetSelectedStoredString(
findNamedObject<wxChoice>(this, "ExportDialogFormatChoice"));
if (!selectedFormat.empty())
{
findNamedObject<wxutil::PathEntry>(this, "ExportDialogFilePicker")->setDefaultExtension(selectedFormat);
std::string extLower = string::to_lower_copy(selectedFormat);
// Check if the replace current selection option is available
std::string extensions = GlobalGameManager().currentGame()->getKeyValue("modeltypes");
std::set<std::string> supportedExtensions;
boost::algorithm::split(supportedExtensions, extensions, boost::algorithm::is_any_of(" "));
wxCheckBox* replaceSelectionBox = findNamedObject<wxCheckBox>(this, "ExportDialogReplaceWithModel");
// If the current game supports the format, make the option available
bool formatSupportedByCurrentGame = supportedExtensions.find(extLower) != supportedExtensions.end();
replaceSelectionBox->Enable(formatSupportedByCurrentGame);
if (!formatSupportedByCurrentGame)
{
replaceSelectionBox->SetValue(false);
}
}
}
void ExportAsModelDialog::onFormatSelection(wxCommandEvent& ev)
{
handleFormatSelectionChange();
}
bool ExportAsModelDialog::_onDeleteEvent()
{
// Remember stuff even when X is pressed
saveOptionsToRegistry();
return DialogBase::_onDeleteEvent();
}
void ExportAsModelDialog::saveOptionsToRegistry()
{
registry::setValue(RKEY_MODEL_EXPORT_OUTPUT_FORMAT,
wxutil::ChoiceHelper::GetSelectedStoredString(findNamedObject<wxChoice>(this, "ExportDialogFormatChoice")));
registry::setValue(RKEY_MODEL_EXPORT_OUTPUT_PATH,
findNamedObject<wxutil::PathEntry>(this, "ExportDialogFilePicker")->getValue());
registry::setValue(RKEY_MODEL_EXPORT_SKIP_CAULK,
findNamedObject<wxCheckBox>(this, "ExportDialogSkipCaulk")->GetValue());
registry::setValue(RKEY_MODEL_EXPORT_CENTER_OBJECTS,
findNamedObject<wxCheckBox>(this, "ExportDialogCenterObjects")->GetValue());
registry::setValue(RKEY_MODEL_EXPORT_REPLACE_WITH_MODEL,
findNamedObject<wxCheckBox>(this, "ExportDialogReplaceWithModel")->GetValue());
registry::setValue(RKEY_MODEL_EXPORT_USE_ENTITY_ORIGIN,
findNamedObject<wxCheckBox>(this, "ExportDialogUseEntityOrigin")->GetValue());
}
void ExportAsModelDialog::ShowDialog(const cmd::ArgumentList& args)
{
if (GlobalSelectionSystem().countSelected() == 0)
{
wxutil::Messagebox::Show(_("Empty Selection"), _("Nothing selected, cannot run exporter"), IDialog::MessageType::MESSAGE_ERROR);
return;
}
ExportAsModelDialog* instance = new ExportAsModelDialog;
instance->ShowModal();
instance->Destroy();
}
}