/
Prefab.cpp
315 lines (255 loc) · 7.37 KB
/
Prefab.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
#include "Prefab.h"
#include "i18n.h"
#include "imainframe.h"
#include "iselection.h"
#include "ipatch.h"
#include "igrid.h"
#include "itextstream.h"
#include "scenelib.h"
#include "patch/Patch.h"
#include "map/Map.h"
#include "ui/texturebrowser/TextureBrowser.h"
#include "ui/patch/PatchCreateDialog.h"
#include "xyview/GlobalXYWnd.h"
#include "wxutil/dialog/MessageBox.h"
#include "selection/algorithm/General.h"
#include "selectionlib.h"
#include <boost/algorithm/string/case_conv.hpp>
namespace patch
{
namespace algorithm
{
void constructPrefab(const AABB& aabb, const std::string& shader, EPatchPrefab eType,
EViewType viewType, std::size_t width, std::size_t height)
{
GlobalSelectionSystem().setSelectedAll(false);
scene::INodePtr node(GlobalPatchCreator(PatchDefType::Def2).createPatch());
GlobalMap().findOrInsertWorldspawn()->addChildNode(node);
Patch* patch = Node_getPatch(node);
patch->setShader(shader);
patch->ConstructPrefab(aabb, eType, viewType, width, height);
patch->controlPointsChanged();
Node_setSelected(node, true);
}
AABB getDefaultBoundsFromSelection()
{
AABB aabb = GlobalSelectionSystem().getWorkZone().bounds;
float gridSize = GlobalGrid().getGridSize();
if (aabb.extents[0] == 0)
{
aabb.extents[0] = gridSize;
}
if (aabb.extents[1] == 0)
{
aabb.extents[1] = gridSize;
}
if (aabb.extents[2] == 0)
{
aabb.extents[2] = gridSize;
}
if (aabb.isValid())
{
return aabb;
}
return AABB(Vector3(0, 0, 0), Vector3(64, 64, 64));
}
void createPrefabInternal(EPatchPrefab prefabType, const std::string& undoCmdName)
{
UndoableCommand undo(undoCmdName);
constructPrefab(getDefaultBoundsFromSelection(),
GlobalTextureBrowser().getSelectedShader(),
prefabType,
GlobalXYWnd().getActiveViewType());
}
void createPrefab(const cmd::ArgumentList& args)
{
if (args.size() != 1)
{
rError() << "Usage: createPatchPrefab <type>" << std::endl
<< " with <type> being one of the following: " << std::endl
<< "cylinder, densecylinder, verydensecylinder, squarecylinder," << std::endl
<< "sphere, endcap, bevel, cone" << std::endl;
return;
}
std::string typeStr = boost::algorithm::to_lower_copy(args[0].getString());
if (typeStr == "cylinder")
{
createPrefabInternal(eCylinder, "patchCreateCylinder");
}
else if (typeStr == "densecylinder")
{
createPrefabInternal(eDenseCylinder, "patchCreateDenseCylinder");
}
else if (typeStr == "verydensecylinder")
{
createPrefabInternal(eVeryDenseCylinder, "patchCreateVeryDenseCylinder");
}
else if (typeStr == "squarecylinder")
{
createPrefabInternal(eSqCylinder, "patchCreateSquareCylinder");
}
else if (typeStr == "sphere")
{
createPrefabInternal(eSphere, "patchCreateSphere");
}
else if (typeStr == "endcap")
{
createPrefabInternal(eEndCap, "patchCreateCaps");
}
else if (typeStr == "bevel")
{
createPrefabInternal(eBevel, "patchCreateBevel");
}
else if (typeStr == "cone")
{
createPrefabInternal(eCone, "patchCreateCone");
}
}
void createCylinder(const cmd::ArgumentList& args)
{
createPrefabInternal(eCylinder, "patchCreateCylinder");
}
void createDenseCylinder(const cmd::ArgumentList& args)
{
createPrefabInternal(eDenseCylinder, "patchCreateDenseCylinder");
}
void createVeryDenseCylinder(const cmd::ArgumentList& args)
{
createPrefabInternal(eVeryDenseCylinder, "patchCreateVeryDenseCylinder");
}
void createSquareCylinder(const cmd::ArgumentList& args)
{
createPrefabInternal(eSqCylinder, "patchCreateSquareCylinder");
}
void createSphere(const cmd::ArgumentList& args)
{
createPrefabInternal(eSphere, "patchCreateSphere");
}
void createEndcap(const cmd::ArgumentList& args)
{
createPrefabInternal(eEndCap, "patchCreateCaps");
}
void createBevel(const cmd::ArgumentList& args)
{
createPrefabInternal(eBevel, "patchCreateBevel");
}
void createCone(const cmd::ArgumentList& args)
{
createPrefabInternal(eCone, "patchCreateCone");
}
// Sanitise the integer to specify a valid patch dimension
// will return 0 if the input is invalid
std::size_t checkPatchDimension(int input)
{
// Must be an odd number in [3..15]
if (input < 3 || input > 15 || input % 2 == 0)
{
return 0;
}
// all good
return static_cast<std::size_t>(input);
}
void createSimplePatch(const cmd::ArgumentList& args)
{
std::size_t width = 0;
std::size_t height = 0;
bool removeSelectedBrush = false;
if (args.size() == 1)
{
// Try to convert the arguments to actual integers and do the range checks
width = height = checkPatchDimension(args[0].getInt());
}
else if (args.size() == 2)
{
width = checkPatchDimension(args[0].getInt());
height = checkPatchDimension(args[1].getInt());
}
// Only fire the dialog if no or invalid command arguments are given
if (width == 0 || height == 0)
{
ui::PatchCreateDialog dialog;
if (dialog.run() == ui::IDialog::RESULT_OK)
{
width = dialog.getSelectedWidth();
height = dialog.getSelectedHeight();
removeSelectedBrush = dialog.getRemoveSelectedBrush();
}
else
{
return; // dialog cancelled
}
}
UndoableCommand undo("patchCreatePlane");
// Retrieve the boundaries before any delete operation
AABB bounds = getDefaultBoundsFromSelection();
if (removeSelectedBrush)
{
// Delete the selection, the should be only one brush selected
selection::algorithm::deleteSelection();
}
// Call the PatchConstruct routine (GtkRadiant legacy)
constructPrefab(bounds,
GlobalTextureBrowser().getSelectedShader(),
ePlane, GlobalXYWnd().getActiveViewType(),
width, height);
}
void createCaps(Patch& patch, const scene::INodePtr& parent, EPatchCap type, const std::string& shader)
{
if ((type == eCapEndCap || type == eCapIEndCap) && patch.getWidth() != 5)
{
rError() << "cannot create end-cap - patch width != 5" << std::endl;
wxutil::Messagebox::ShowError(_("Cannot create end-cap, patch must have a width of 5."));
return;
}
if ((type == eCapBevel || type == eCapIBevel) && patch.getWidth() != 3)
{
wxutil::Messagebox::ShowError(_("Cannot create bevel-cap, patch must have a width of 3."));
rError() << "cannot create bevel-cap - patch width != 3" << std::endl;
return;
}
if (type == eCapCylinder && patch.getWidth() != 9)
{
wxutil::Messagebox::ShowError(_("Cannot create cylinder-cap, patch must have a width of 9."));
rError() << "cannot create cylinder-cap - patch width != 9" << std::endl;
return;
}
assert(parent != NULL);
{
scene::INodePtr cap(GlobalPatchCreator(PatchDefType::Def2).createPatch());
parent->addChildNode(cap);
Patch* capPatch = Node_getPatch(cap);
assert(capPatch != NULL);
patch.MakeCap(capPatch, type, ROW, true);
capPatch->setShader(shader);
// greebo: Avoid creating "degenerate" patches (all vertices merged in one 3D point)
if (!capPatch->isDegenerate())
{
Node_setSelected(cap, true);
}
else
{
parent->removeChildNode(cap);
rWarning() << "Prevented insertion of degenerate patch." << std::endl;
}
}
{
scene::INodePtr cap(GlobalPatchCreator(PatchDefType::Def2).createPatch());
parent->addChildNode(cap);
Patch* capPatch = Node_getPatch(cap);
assert(capPatch != NULL);
patch.MakeCap(capPatch, type, ROW, false);
capPatch->setShader(shader);
// greebo: Avoid creating "degenerate" patches (all vertices merged in one 3D point)
if (!capPatch->isDegenerate())
{
Node_setSelected(cap, true);
}
else
{
parent->removeChildNode(cap);
rWarning() << "Prevented insertion of degenerate patch." << std::endl;
}
}
}
}
} // namespace