/
ExportAsModelDialog.cpp
320 lines (247 loc) · 11 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
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
#include "ExportAsModelDialog.h"
#include "i18n.h"
#include "imodel.h"
#include "iselection.h"
#include "ifiletypes.h"
#include "icommandsystem.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 "string/split.h"
#include "selectionlib.h"
#include "os/path.h"
#include "os/file.h"
#include "os/dir.h"
#include "os/fs.h"
#include "registry/registry.h"
#include "wxutil/dialog/MessageBox.h"
#include "wxutil/ChoiceHelper.h"
#include "wxutil/PathEntry.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";
const char* RKEY_MODEL_EXPORT_EXPORT_LIGHTS_AS_OBJECTS = "user/ui/exportAsModel/exportLightsAsObjects";
}
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");
// We don't want the FileChooser to ask for permission overwriting an existing file,
// we do this ourselves in this class when the user hits OK
pathEntry->setAskForOverwrite(false);
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");
bool exportLightsAsObjects = registry::getValue<bool>(RKEY_MODEL_EXPORT_EXPORT_LIGHTS_AS_OBJECTS);
findNamedObject<wxCheckBox>(this, "ExportDialogExportLightsAsObjects")->SetValue(exportLightsAsObjects);
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)
{
bool centerObjects = findNamedObject<wxCheckBox>(this, "ExportDialogCenterObjects")->GetValue();
bool skipCaulk = findNamedObject<wxCheckBox>(this, "ExportDialogSkipCaulk")->GetValue();
std::string outputFilename = findNamedObject<wxutil::PathEntry>(this, "ExportDialogFilePicker")->getValue();
std::string outputFormat = wxutil::ChoiceHelper::GetSelectedStoredString(findNamedObject<wxChoice>(this, "ExportDialogFormatChoice"));
bool replaceSelectionWithModel = findNamedObject<wxCheckBox>(this, "ExportDialogReplaceWithModel")->GetValue();
bool useEntityOrigin = findNamedObject<wxCheckBox>(this, "ExportDialogUseEntityOrigin")->GetValue();
bool exportLightsAsObjects = findNamedObject<wxCheckBox>(this, "ExportDialogExportLightsAsObjects")->GetValue();
if (outputFilename.empty())
{
wxutil::Messagebox::Show(_("Empty Filename"), _("No filename specified, cannot run exporter"), IDialog::MessageType::MESSAGE_ERROR);
return;
}
// Warn the user if the output file extension doesn't match what the format says (#5741)
if (string::to_lower_copy(os::getExtension(outputFilename)) != string::to_lower_copy(outputFormat) &&
wxutil::Messagebox::Show(_("Format/Extension Mismatch"), _("The file extension doesn't match the selected format - continue?"),
IDialog::MessageType::MESSAGE_ASK) == IDialog::RESULT_NO)
{
return; // abort
}
// Check if the target file already exists
if (os::fileOrDirExists(outputFilename) &&
wxutil::Messagebox::Show(_("Confirm Replacement"),
fmt::format(_("The file {0} already exists.\nReplace this file?"), outputFilename),
IDialog::MessageType::MESSAGE_ASK) != IDialog::RESULT_YES)
{
return; // abort
}
saveOptionsToRegistry();
try
{
// ExportSelectedAsModel <Path> <ExportFormat> [<CenterObjects>] [<SkipCaulk>] [<ReplaceSelectionWithModel>] [<UseEntityOrigin>] [<ExportLightsAsObjects>]
cmd::ArgumentList argList;
argList.push_back(outputFilename);
argList.push_back(outputFormat);
argList.push_back(centerObjects);
argList.push_back(skipCaulk);
argList.push_back(replaceSelectionWithModel);
argList.push_back(useEntityOrigin);
argList.push_back(exportLightsAsObjects);
GlobalCommandSystem().executeCommand("ExportSelectedAsModel", argList);
// 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())
{
wxutil::PathEntry* pathEntry = findNamedObject<wxutil::PathEntry>(this, "ExportDialogFilePicker");
pathEntry->setDefaultExtension(selectedFormat);
std::string extLower = string::to_lower_copy(selectedFormat);
// Adjust the extension of the current file name
pathEntry->setValue(os::replaceExtension(pathEntry->getValue(), extLower));
// Check if the replace current selection option is available
std::string extensions = GlobalGameManager().currentGame()->getKeyValue("modeltypes");
std::set<std::string> supportedExtensions;
string::split(supportedExtensions, extensions, " ");
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());
registry::setValue(RKEY_MODEL_EXPORT_EXPORT_LIGHTS_AS_OBJECTS,
findNamedObject<wxCheckBox>(this, "ExportDialogExportLightsAsObjects")->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();
}
}